FCT
载入中...
搜索中...
未找到
FreeImage_ImageLoader.cpp
浏览该文件的文档.
2
3#include <iostream>
4#include <stdexcept>
5
6namespace FCT {
8 {
9 FreeImage_Initialise();
10 FreeImage_SetOutputMessage(freeImageErrorHandler);
11 }
13 {
14 FreeImage_DeInitialise();
15 }
18
21
23 FREE_IMAGE_FORMAT fif = FreeImage_GetFileType(filename.c_str(), 0);
24 if (fif == FIF_UNKNOWN) {
25 fif = FreeImage_GetFIFFromFilename(filename.c_str());
26 }
27
28 if (fif == FIF_UNKNOWN) {
29 throw std::runtime_error("Unknown image format");
30 }
31
32 FIBITMAP* dib = FreeImage_Load(fif, filename.c_str());
33 if (!dib) {
34 throw std::runtime_error("Failed to load image");
35 }
36
37 FIBITMAP* dibConverted = FreeImage_ConvertTo32Bits(dib);
38 FreeImage_Unload(dib);
39
40 if (!dibConverted) {
41 throw std::runtime_error("Failed to convert image to 32 bits");
42 }
43
44 ImageData imageData;
45 imageData.width = FreeImage_GetWidth(dibConverted);
46 imageData.height = FreeImage_GetHeight(dibConverted);
47 imageData.channels = 4; // RGBA
48
49 unsigned int pitch = FreeImage_GetPitch(dibConverted);
50 imageData.data.resize(imageData.height * pitch);
51
52 FreeImage_ConvertToRawBits(imageData.data.data(), dibConverted, pitch, 32,
53 FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, TRUE);
54
55 for (unsigned int y = 0; y < imageData.height; ++y) {
56 for (unsigned int x = 0; x < imageData.width; ++x) {
57 unsigned int offset = (y * pitch) + (x * 4);
58 std::swap(imageData.data[offset], imageData.data[offset + 2]);
59 }
60 }
61
62
63 FreeImage_Unload(dibConverted);
64
65 return imageData;
66 }
67
68 bool FreeImage_ImageLoader::save(const std::string& filename, const ImageData& imageData) {
69 FIBITMAP* dib = FreeImage_AllocateT(FIT_BITMAP, imageData.width, imageData.height, 32);
70 if (!dib) {
71 return false;
72 }
73
74 unsigned int pitch = FreeImage_GetPitch(dib);
75 FreeImage_ConvertFromRawBits(
76 (BYTE*)imageData.data.data(),
77 imageData.width,
78 imageData.height,
79 pitch,
80 32,
81 FI_RGBA_RED_MASK,
82 FI_RGBA_GREEN_MASK,
83 FI_RGBA_BLUE_MASK
84 );
85
86 FREE_IMAGE_FORMAT fif = FreeImage_GetFIFFromFilename(filename.c_str());
87 bool success = FreeImage_Save(fif, dib, filename.c_str());
88
89 FreeImage_Unload(dib);
90
91 return success;
92 }
93
94 std::vector<std::string> FreeImage_ImageLoader::getSupportedExtensions() const {
95 return {".bmp", ".png", ".jpg", ".jpeg", ".tif", ".tiff"}; // Add more as needed
96 }
97
98 void FreeImage_ImageLoader::freeImageErrorHandler(FREE_IMAGE_FORMAT fif, const char *message) {
99 std::cerr << "FreeImage error: ";
100 if (fif != FIF_UNKNOWN) {
101 std::cerr << FreeImage_GetFormatFromFIF(fif) << " Format - ";
102 }
103 std::cerr << message << std::endl;
104 }
105
106} // namespace FCT
static void freeImageErrorHandler(FREE_IMAGE_FORMAT fif, const char *message)
bool save(const std::string &filename, const ImageData &imageData) override
std::vector< std::string > getSupportedExtensions() const override
ImageData load(const std::string &filename) override
std::vector< unsigned char > data