使用 FreeImage载入图形文件

 代码如下:

 

/* 使用FreeImage载入图形 核动力机器人 2009.5.16 */ #ifndef G_IMG_WRAPPER_H #define G_IMG_WRAPPER_H #include <string> using std::string; namespace g { //初始化图形库选项 void InitImageLib(); class Image { public: Image(); Image(const string &filename); ~Image(); public: bool Load(const string &filename); int GetWidth(); int GetHeight(); unsigned char* GetData(); int GetBpp(); bool IsValid(); private: string file; int width; int height; unsigned char* ptr; int bpp; bool flag; }; void UnInitImageLib(); } #endif #include <FreeImage.h> #include "ImageWrapper.hpp" namespace g { static int index = -1; void InitImageLib() { FreeImage_Initialise(); } void UnInitImageLib() { FreeImage_DeInitialise(); } Image::Image(): file(""), width(-1), height(-1), ptr(0), bpp(-1), flag(false) {} Image::Image(const string &filename): file(filename), width(-1), height(-1), bpp(-1), ptr(0) { flag = Load(file); } Image::~Image() { } bool Image::Load(const string &filename) { FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; FIBITMAP *dib(0); fif = FreeImage_GetFileType(filename.c_str(), 0); if(fif == FIF_UNKNOWN) fif = FreeImage_GetFIFFromFilename(filename.c_str()); if(fif == FIF_UNKNOWN) return false; if(FreeImage_FIFSupportsReading(fif)) dib = FreeImage_Load(fif, filename.c_str()); if(!dib) return false; bpp = FreeImage_GetBPP(dib); ptr = FreeImage_GetBits(dib); width = FreeImage_GetWidth(dib); height = FreeImage_GetHeight(dib); if((ptr== 0) || (width == 0) || (height == 0)) return false; flag = true; FreeImage_Unload(dib); return true; } int Image::GetWidth() { return width; } int Image::GetHeight() { return height; } unsigned char* Image::GetData() { return ptr; } int Image::GetBpp() { return bpp; } bool Image::IsValid() { return flag; } };

你可能感兴趣的:(String,image,File,Class,图形,wrapper)