]> git.tdb.fi Git - libs/gui.git/commitdiff
Put (most of) DevIL code in its own file
authorMikko Rasa <tdb@tdb.fi>
Sat, 27 Aug 2011 19:23:10 +0000 (22:23 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 27 Aug 2011 19:23:10 +0000 (22:23 +0300)
Always store image information in the private struct

source/graphics/image.cpp
source/graphics/image_devil.cpp [new file with mode: 0644]
source/graphics/image_devil.h [new file with mode: 0644]
source/graphics/image_private.h

index 56e7c1b01f59c54338e5cfb671f9961ba9502c8b..6a20178c5d4a5d115ed1748100374d13a3e03f38 100644 (file)
@@ -4,6 +4,7 @@
 #include <msp/io/file.h>
 #include <msp/io/memory.h>
 #include "image.h"
+#include "image_devil.h"
 #include "image_png.h"
 #include "image_private.h"
 
@@ -17,35 +18,10 @@ Image::Private::Private()
 #ifdef WITH_DEVIL
        id = 0;
 #endif
-#ifdef WITH_LIBPNG
        fmt = RGB;
        width = 0;
        height = 0;
        data = 0;
-#endif
-}
-
-
-namespace {
-
-#ifdef WITH_DEVIL
-void ensure_devil_image(unsigned &id)
-{
-       static bool init_done = false;
-
-       if(!init_done)
-       {
-               ilInit();
-               ilEnable(IL_ORIGIN_SET);
-               ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
-               init_done = true;
-       }
-
-       if(!id)
-               ilGenImages(1, &id);
-}
-#endif
-
 }
 
 
@@ -62,10 +38,9 @@ Image::~Image()
 #ifdef WITH_DEVIL
        if(priv->id)
                ilDeleteImages(1, &priv->id);
+       else
 #endif
-#ifdef WITH_LIBPNG
        delete[] priv->data;
-#endif
        delete priv;
 }
 
@@ -81,10 +56,7 @@ void Image::load_file(const string &fn)
 #endif
        {
 #ifdef WITH_DEVIL
-               ensure_devil_image(priv->id);
-               ilBindImage(priv->id);
-               if(!ilLoadImage(const_cast<char *>(fn.c_str())))
-                       throw bad_image_data("IL error");
+               load_devil_file(fn, *priv);
 #else
                throw unsupported_image_format("DevIL needed for non-PNG images");
 #endif
@@ -104,10 +76,7 @@ void Image::load_memory(const void *data, unsigned size)
 #endif
        {
 #ifdef WITH_DEVIL
-               ensure_devil_image(priv->id);
-               ilBindImage(priv->id);
-               if(!ilLoadL(IL_TYPE_UNKNOWN, const_cast<void *>(data), size))
-                       throw bad_image_data("IL error");
+               load_devil_mem(data, size, *priv);
 #else
                throw unsupported_image_format("DevIL needed for non-PNG images");
 #endif
@@ -118,77 +87,22 @@ void Image::load_memory(const void *data, unsigned size)
 
 PixelFormat Image::get_format() const
 {
-#ifdef WITH_LIBPNG
-       if(priv->data)
-               return priv->fmt;
-#endif
-#ifdef WITH_DEVIL
-       if(priv->id)
-       {
-               ilBindImage(priv->id);
-               switch(ilGetInteger(IL_IMAGE_FORMAT))
-               {
-               case IL_COLOR_INDEX: return COLOR_INDEX;
-               case IL_LUMINANCE: return LUMINANCE;
-               case IL_LUMINANCE_ALPHA: return LUMINANCE_ALPHA;
-               case IL_RGB: return RGB;
-               case IL_RGBA: return RGBA;
-               case IL_BGR: return BGR;
-               case IL_BGRA: return BGRA;
-               // XXX bad, should throw when loading
-               default: throw invalid_argument("unknown pixel format in image");
-               }
-       }
-#endif
-       return RGB;
+       return priv->fmt;
 }
 
 unsigned Image::get_width() const
 {
-#ifdef WITH_LIBPNG
-       if(priv->data)
-               return priv->width;
-#endif
-#ifdef WITH_DEVIL
-       if(priv->id)
-       {
-               ilBindImage(priv->id);
-               return ilGetInteger(IL_IMAGE_WIDTH);
-       }
-#endif
-       return 0;
+       return priv->width;
 }
 
 unsigned Image::get_height() const
 {
-#ifdef WITH_LIBPNG
-       if(priv->data)
-               return priv->height;
-#endif
-#ifdef WITH_DEVIL
-       if(priv->id)
-       {
-               ilBindImage(priv->id);
-               return ilGetInteger(IL_IMAGE_HEIGHT);
-       }
-#endif
-       return 0;
+       return priv->height;
 }
 
 const void *Image::get_data() const
 {
-#ifdef WITH_LIBPNG
-       if(priv->data)
-               return priv->data;
-#endif
-#ifdef WITH_DEVIL
-       if(priv->id)
-       {
-               ilBindImage(priv->id);
-               return ilGetData();
-       }
-#endif
-       return 0;
+       return priv->data;
 }
 
 } // namespace Graphics
diff --git a/source/graphics/image_devil.cpp b/source/graphics/image_devil.cpp
new file mode 100644 (file)
index 0000000..d355e55
--- /dev/null
@@ -0,0 +1,75 @@
+#ifdef WITH_DEVIL
+#include <IL/il.h>
+#include "image_devil.h"
+#include "image_private.h"
+
+using namespace std;
+
+namespace {
+
+using namespace Msp::Graphics;
+
+void init_load(Image::Private &priv)
+{
+       static bool il_init_done = false;
+
+       if(!il_init_done)
+       {
+               ilInit();
+               ilEnable(IL_ORIGIN_SET);
+               ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
+               il_init_done = true;
+       }
+
+       if(!priv.id)
+               ilGenImages(1, &priv.id);
+       ilBindImage(priv.id);
+}
+
+void finish_load(Image::Private &priv)
+{
+       switch(ilGetInteger(IL_IMAGE_FORMAT))
+       {
+       case IL_COLOR_INDEX:     priv.fmt = COLOR_INDEX; break;
+       case IL_LUMINANCE:       priv.fmt = LUMINANCE; break;
+       case IL_LUMINANCE_ALPHA: priv.fmt = LUMINANCE_ALPHA; break;
+       case IL_RGB:             priv.fmt = RGB; break;
+       case IL_RGBA:            priv.fmt = RGBA; break;
+       case IL_BGR:             priv.fmt = BGR; break;
+       case IL_BGRA:            priv.fmt = BGRA; break;
+       default: throw unsupported_image_format("unknown pixel format");
+       }
+
+       priv.width = ilGetInteger(IL_IMAGE_WIDTH);
+       priv.height = ilGetInteger(IL_IMAGE_HEIGHT);
+       priv.data = reinterpret_cast<char *>(ilGetData());
+
+       ilBindImage(0);
+}
+
+}
+
+
+namespace Msp {
+namespace Graphics {
+
+void load_devil_file(const string &fn, Image::Private &priv)
+{
+       init_load(priv);
+       if(!ilLoadImage(const_cast<char *>(fn.c_str())))
+               throw bad_image_data("IL error");
+       finish_load(priv);
+}
+
+void load_devil_mem(const void *data, unsigned size, Image::Private &priv)
+{
+       init_load(priv);
+       if(!ilLoadL(IL_TYPE_UNKNOWN, const_cast<void *>(data), size))
+               throw bad_image_data("IL error");
+       finish_load(priv);
+}
+
+} // namespace Graphics
+} // namespace Msp
+
+#endif
diff --git a/source/graphics/image_devil.h b/source/graphics/image_devil.h
new file mode 100644 (file)
index 0000000..4e669e7
--- /dev/null
@@ -0,0 +1,17 @@
+#ifndef MSP_GRAPHICS_IMAGE_DEVIL_H_
+#define MSP_GRAPHICS_IMAGE_DEVIL_H_
+
+#include "image.h"
+
+namespace Msp {
+namespace Graphics {
+
+#ifdef WITH_DEVIL
+void load_devil_file(const std::string &, Image::Private &);
+void load_devil_mem(const void *, unsigned, Image::Private &);
+#endif
+
+} // namespace Graphics
+} // namespace Msp
+
+#endif
index 19f0cefdb976ade4466289efb0f28db414c39990..72ceedda82e7dc7bac4fb511bf52875eb7c9308f 100644 (file)
@@ -11,12 +11,10 @@ struct Image::Private
 #ifdef WITH_DEVIL
        unsigned id;
 #endif
-#ifdef WITH_LIBPNG
        PixelFormat fmt;
        unsigned width;
        unsigned height;
        char *data;
-#endif
 
        Private();
 };