Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- // FreeImage static linkage
- #define FREEIMAGE_LIB
- #include "FreeImage/FreeImage.h"
- #include "FreeImage/Utilities.h"
- #pragma comment(lib, "FreeImage/FreeImaged.lib")
- using namespace std;
- static const wchar_t* sk_Filename = L"Test.tga";
- // Error handler to use in callback
- void FreeImageErrorHandler(FREE_IMAGE_FORMAT fif, const char *msg)
- {
- char buf[1024];
- sprintf_s(buf, 1024, "Error: %s", FreeImage_GetFormatFromFIF(fif));
- cout << buf;
- }
- // Bitmap loader from FreeImage samples
- FIBITMAP* GenericLoaderU(const wchar_t* lpszPathName, int flag)
- {
- FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
- fif = FreeImage_GetFileTypeU(lpszPathName, 0);
- if(fif == FIF_UNKNOWN)
- {
- fif = FreeImage_GetFIFFromFilenameU(lpszPathName);
- }
- if((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsReading(fif))
- {
- FIBITMAP *dib = FreeImage_LoadU(fif, lpszPathName, flag);
- return dib;
- }
- return NULL;
- }
- // Function gets filename and returns bitmap data array, its size and bits per pixel
- void GetData(const wchar_t* szFilename, unsigned char* data, unsigned int& width, unsigned int& height, unsigned int& bpp)
- {
- FIBITMAP* src = GenericLoaderU(szFilename, 0);
- if(src == 0)
- return;
- FIBITMAP* src32 = FreeImage_ConvertTo32Bits(src);
- FreeImage_Unload(src);
- // Get picture info
- width = FreeImage_GetWidth(src32);
- height = FreeImage_GetHeight(src32);
- bpp = FreeImage_GetBPP(src32);
- unsigned int scan_width = width * bpp/8;
- if((width == 0) || (height == 0) || (bpp == 0))
- return;
- memset(data, 0, height * scan_width);
- SwapRedBlue32(src32); // Convert BGR to RGB
- // Get bitmap data
- FreeImage_ConvertToRawBits(data, src32, scan_width, bpp, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, TRUE);
- FreeImage_Unload(src32);
- return;
- }
- int main()
- {
- FreeImage_Initialise();
- FreeImage_SetOutputMessage(FreeImageErrorHandler);
- //Creating bitmap data array (size is unknown here)
- unsigned char* data = new unsigned char[];
- unsigned int width(0), height(0), bpp(0);
- // Loading data here
- GetData(sk_Filename, data, width, height, bpp);
- //Using data here
- cout << width << "x" << height << "x" << bpp << endl;
- for (unsigned int i = 0; i < width * height * bpp/8; )
- {
- cout << "("
- << (unsigned int)data[i] << ", "
- << (unsigned int)data[i+1] << ", "
- << (unsigned int)data[i+2] << ", "
- << (unsigned int)data[i+3] << ")"
- << endl;
- i += 4;
- }
- cout << endl;
- //Cleanup
- delete[] data; // <-- Breaks with _ASSERTE(_CrtIsValidHeapPointer(pUserData));
- // What's wrong here?
- system("pause");
- return 0;
- }
Add Comment
Please, Sign In to add comment