Introduction to SDL_Image

Applying Filters to Images with SDL_Image

Is it possible to apply filters or effects to images loaded with SDL_Image?

3D art representing computer programming

Yes, it's definitely possible to apply filters or effects to images loaded with SDL_Image. While SDL_Image itself doesn't provide built-in filter functions, you can manipulate the pixel data of the loaded surfaces to create various effects.

Here's a guide on how to apply a simple grayscale filter to an image:

Loading the Image

First, let's load an image using SDL_Image:

#include <SDL.h>
#include <SDL_image.h>

#include <iostream>

int main() {
  SDL_Init(SDL_INIT_VIDEO);
  IMG_Init(IMG_INIT_PNG);

  SDL_Window* window =
    SDL_CreateWindow("Image Filter Example",
                     SDL_WINDOWPOS_UNDEFINED,
                     SDL_WINDOWPOS_UNDEFINED,
                     640, 480, 0);
  SDL_Renderer* renderer = SDL_CreateRenderer(
    window, -1, 0);

  SDL_Surface* surface =
    IMG_Load("example.png");
  if (!surface) {
    std::cout << "Failed to load image: " <<
      IMG_GetError() << '\n';
    return 1;
  }

  // ... (rest of the code)
}

Applying a Grayscale Filter

Now, let's implement a function to apply a grayscale filter:

void applyGrayscaleFilter(
  SDL_Surface* surface) {
  SDL_LockSurface(surface);

  Uint32* pixels = static_cast<Uint32*>(surface
    ->pixels);
  int pixelCount = surface->w * surface->h;

  for (int i = 0; i < pixelCount; ++i) {
    Uint8 r, g, b;
    SDL_GetRGB(pixels[i], surface->format, &r,
               &g, &b);

    // Calculate grayscale value
    Uint8 gray = static_cast<Uint8>(0.3 * r +
      0.59 * g + 0.11 * b);

    pixels[i] = SDL_MapRGB(surface->format,
                           gray, gray, gray);
  }

  SDL_UnlockSurface(surface);
}

This function iterates through each pixel of the surface, calculates its grayscale value, and then sets the pixel to that grayscale color.

Displaying the Filtered Image

Now let's apply the filter and display the result:

int main() {
  // ... (previous code)

  applyGrayscaleFilter(surface);

  SDL_Texture* texture =
    SDL_CreateTextureFromSurface(
      renderer, surface);
  SDL_FreeSurface(surface);

  bool quit = false;
  SDL_Event event;

  while (!quit) {
    while (SDL_PollEvent(&event)) {
      if (event.type == SDL_QUIT) {
        quit = true;
      }
    }

    SDL_RenderClear(renderer);
    SDL_RenderCopy(renderer, texture, nullptr,
                   nullptr);
    SDL_RenderPresent(renderer);
  }

  SDL_DestroyTexture(texture);
  SDL_DestroyRenderer(renderer);
  SDL_DestroyWindow(window);
  IMG_Quit();
  SDL_Quit();

  return 0;
}

This code applies the grayscale filter to the loaded image and then displays it in the window.

Other Filter Ideas

You can create various other filters using similar techniques. Here are a few ideas:

  • Sepia tone: Similar to grayscale, but with a brownish tint
  • Brightness adjustment: Increase or decrease RGB values uniformly
  • Contrast adjustment: Expand or compress the range of color values
  • Color inversion: Subtract each color component from 255
  • Blur: Average the color values of neighboring pixels

Remember, when working with pixel data, always lock and unlock the surface to ensure you're not modifying it while SDL is using it.

By understanding these basic principles, you can create a wide variety of filters and effects for images loaded with SDL_Image!

This Question is from the Lesson:

Introduction to SDL_Image

Learn to load, manipulate, and save various image formats using SDL_Image.

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Introduction to SDL_Image

Learn to load, manipulate, and save various image formats using SDL_Image.

sdl2-promo.jpg
Part of the course:

Game Dev with SDL2

Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games

Free, unlimited access

This course includes:

  • 46 Lessons
  • 100+ Code Samples
  • 91% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved