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:
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.
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%.
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.
Answers to questions are automatically generated and may not have been reviewed.
SDL_Image
Learn to load, manipulate, and save various image formats using SDL_Image
.