Introduction to SDL_Image

Image Compression with SDL_Image

Can SDL_Image handle image compression, and if so, how do I implement it?

3D art representing computer programming

SDL_Image doesn't directly provide image compression functionality, but it does support saving images in compressed formats like PNG and JPEG.

The level of compression is determined by the format you choose and the parameters you provide when saving. Here's how you can implement basic image compression using SDL_Image:

Saving as PNG (Lossless Compression)

PNG uses lossless compression, which means the image quality isn't reduced, but the file size might not be as small as with lossy compression methods.

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

#include <iostream>

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

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

  int result = IMG_SavePNG(surface,
                           "compressed_example.png");
  if (result != 0) {
    std::cout << "Failed to save PNG: " <<
      IMG_GetError() << '\n';
  } else {
    std::cout <<
      "Image saved as compressed PNG\n";
  }

  SDL_FreeSurface(surface);
  IMG_Quit();
  SDL_Quit();

  return 0;
}

This code loads a BMP image and saves it as a PNG, which applies lossless compression.

Saving as JPEG (Lossy Compression)

JPEG uses lossy compression, which can significantly reduce file size but may also reduce image quality. You can control the compression level:

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

#include <iostream>

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

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

  int quality = 85; // 0 (worst) to 100 (best)
  int result = IMG_SaveJPG(surface,
                           "compressed_example.jpg",
                           quality);
  if (result != 0) {
    std::cout << "Failed to save JPEG: " <<
      IMG_GetError() << '\n';
  } else {
    std::cout <<
      "Image saved as compressed JPEG\n";
  }

  SDL_FreeSurface(surface);
  IMG_Quit();
  SDL_Quit();

  return 0;
}

This code loads a PNG image and saves it as a JPEG with a quality level of 85%.

Comparing File Sizes

To see the effects of compression, you can compare the file sizes:

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

#include <filesystem>
#include <iostream>

void
  printFileSize(const std::string& filename) {
  std::cout << filename << " size: " <<
    std::filesystem::file_size(filename)
    << " bytes\n";
}

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

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

  printFileSize("example.bmp");

  IMG_SavePNG(surface,
              "compressed_example.png");
  printFileSize("compressed_example.png");

  IMG_SaveJPG(surface, "compressed_example.jpg",
              85);
  printFileSize("compressed_example.jpg");

  SDL_FreeSurface(surface);
  IMG_Quit();
  SDL_Quit();

  return 0;
}

This code will print the file sizes of the original BMP and the compressed PNG and JPEG versions.

Remember, the effectiveness of compression depends on the content of the image. Images with large areas of solid color or gradual color changes typically compress better than images with lots of detail or sharp color transitions.

While SDL_Image doesn't provide direct control over compression algorithms, using these save functions allows you to benefit from the built-in compression of these image formats.

For more advanced compression techniques, you might need to use additional libraries specifically designed for image processing and compression.

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