glfxfi.h

00001 /*-------------------------------------------------------------------------
00002 This source code is part of GLfx library (OpenGL effect library)
00003 For more information and updates see http://glfx.sourceforge.net/
00004 Copyright (C) 2005-6 Tomek Rozen <trozen at users sourceforge net>
00005 
00006 This library is free software; you can redistribute it and/or modify it
00007 under the terms of the GNU Lesser General Public License as published by
00008 the Free Software Foundation; either version 2.1 of the License,
00009 or (at your option) any later version.
00010 
00011 This library is distributed in the hope that it will be useful, but WITHOUT
00012 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00013 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00014 for more details.
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                 //FREE_IMAGE_FORMAT fif = FreeImage_GetFileType(name, 0);
00077                 if(fif != FIF_UNKNOWN) {
00078                     // load from the file handle
00079                     // TODO handle luminance images
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                     // free the loaded FIBITMAP
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__

SourceForge.net Logo