00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef __GLFXFI_H__
00018 #define __GLFXFI_H__
00019
00020 #include <FreeImage.h>
00021
00022 #ifdef _MSC_VER
00023 #pragma comment(lib, "FreeImage")
00024 #endif
00025
00026 namespace GLFX {
00027
00030 class EffectManager_FreeImage : public EffectManager
00031 {
00032 public:
00033
00034 EffectManager_FreeImage() {
00035 #ifdef LINUX
00036 FreeImage_Initialise(FALSE);
00037 #endif
00038 ErrorReportingInstance(this, true);
00039 FreeImage_SetOutputMessage(MyFreeImageErrorHandler);
00040 MessageFormat(LOG_NOTICE, "Initialising FreeImage %s: %s.",
00041 FreeImage_GetVersion(), FreeImage_GetCopyrightMessage());
00042 }
00043
00044 ~EffectManager_FreeImage() {
00045 #ifdef LINUX
00046 FreeImage_DeInitialise();
00047 #endif
00048 }
00049
00054 bool FillTextureData(GLenum target, const char* name) {
00055 if(EffectManager::FillTextureData(target, name)) {
00056 return true;
00057 }
00058 if(target == GL_TEXTURE_1D) {
00059 return false;
00060 } else if(target == GL_TEXTURE_3D) {
00061 return false;
00062 } else if(target == GL_TEXTURE_CUBE_MAP) {
00063 return false;
00064 } else {
00065 FreeImageIO io;
00066 io.read_proc = MyReadProc;
00067 io.write_proc = MyWriteProc;
00068 io.seek_proc = MySeekProc;
00069 io.tell_proc = MyTellProc;
00070 FileHandle file;
00071 file.fxMgr = this;
00072 file.file = OpenFile(name, false);
00073 bool ok = false;
00074 if(file.file) {
00075 FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromHandle(&io, (fi_handle)&file, 0);
00076
00077 if(fif != FIF_UNKNOWN) {
00078
00079
00080 FIBITMAP* dib = FreeImage_LoadFromHandle(fif, &io, (fi_handle)&file, 0);
00081 FIBITMAP* dib32 = FreeImage_ConvertTo32Bits(dib);
00082
00083 int width = FreeImage_GetWidth(dib32);
00084 int height = FreeImage_GetHeight(dib32);
00085 void* data = FreeImage_GetBits(dib32);
00086
00087 gluBuild2DMipmaps(target, 4, width, height, GL_BGRA, GL_UNSIGNED_BYTE, data);
00088
00089 ok = true;
00090
00091
00092 FreeImage_Unload(dib);
00093 FreeImage_Unload(dib32);
00094 }
00095 CloseFile(file.file);
00096 }
00097 if(!ok) {
00098 return false;
00099 }
00100 return true;
00101 }
00102 }
00103
00104 private:
00105
00106 struct FileHandle
00107 {
00108 EffectManager_FreeImage* fxMgr;
00109 void* file;
00110 };
00111
00112 static EffectManager* ErrorReportingInstance(EffectManager* mgr = 0, bool setMgr = false) {
00113 static EffectManager* manager = 0;
00114 if(setMgr) {
00115 manager = mgr;
00116 }
00117 return manager;
00118 }
00119
00120 static unsigned DLL_CALLCONV MyReadProc(void *buffer, unsigned size, unsigned count, fi_handle handle) {
00121 FileHandle* fh = (FileHandle*)handle;
00122 return fh->fxMgr->ReadFile(fh->file, buffer, size * count);
00123 }
00124
00125 static unsigned DLL_CALLCONV MyWriteProc(void *buffer, unsigned size, unsigned count, fi_handle handle) {
00126 return 0;
00127 }
00128
00129 static int DLL_CALLCONV MySeekProc(fi_handle handle, long offset, int origin) {
00130 FileHandle* fh = (FileHandle*)handle;
00131 return fh->fxMgr->SeekFile(fh->file, offset, origin);
00132 }
00133
00134 static long DLL_CALLCONV MyTellProc(fi_handle handle) {
00135 FileHandle* fh = (FileHandle*)handle;
00136 return fh->fxMgr->TellFile(fh->file);
00137 }
00138
00139 static void MyFreeImageErrorHandler(FREE_IMAGE_FORMAT fif, const char *message) {
00140 EffectManager* inst = ErrorReportingInstance();
00141 inst->MessageFormat(LOG_ERROR, "%s format: %s", FreeImage_GetFormatFromFIF(fif), message);
00142 }
00143
00144 };
00145
00146 }
00147
00148 #endif // __GLFXFI_H__