Vinh La Kiến

51 bundles
1 filea month ago
4

FreeImage Example

main.cpp

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
#include <stdio.h> #include "FreeImage.h" void generateImage() { const char* img_path = "stdio-image-demo.png"; int img_width = 32, img_height = 32, img_bpp = 32; FIBITMAP* img_bm = FreeImage_Allocate(img_width, img_height, img_bpp); int* pixels = (int*)FreeImage_GetBits(img_bm); int size = img_width * img_height; for (int i = 0; i < size; i++) pixels[i] = 0xffffbb33; if (FreeImage_Save(FREE_IMAGE_FORMAT::FIF_PNG, img_bm, img_path)) { printf("Info: Save success !!"); } else { printf("Info: Save fail !!"); } // Clean up FreeImage_Unload(img_bm); } int readImageInfo() { const char* img_path = "stdio-image-demo.png"; int img_width = 0, img_height = 0, img_bpp = 0; FREE_IMAGE_FORMAT img_format = FreeImage_GetFileType(img_path, 0); if (img_format == FREE_IMAGE_FORMAT::FIF_UNKNOWN) { printf("Error: Unknown format !"); return 1; } FIBITMAP* img_bm = FreeImage_Load(img_format, img_path); if (img_bm == NULL) { printf("Error: Image load FAIL !"); return 1; } img_width = FreeImage_GetWidth(img_bm); img_height = FreeImage_GetHeight(img_bm); img_bpp = FreeImage_GetBPP(img_bm); char* pixels = (char*)FreeImage_GetBits(img_bm); // Print image's info printf("Image path: %s\n", img_path); printf("Image width: %d\n", img_width); printf("Image height: %d\n", img_height); printf("Image BPP (bit-per-pixel): %d", img_bpp); // Clean up FreeImage_Unload(img_bm); } int main() { readImageInfo(); return 0; }