]> git.tdb.fi Git - libs/gui.git/blobdiff - source/graphics/image.cpp
Set multisample attributes correctly on glX
[libs/gui.git] / source / graphics / image.cpp
index 56e7c1b01f59c54338e5cfb671f9961ba9502c8b..46c2b3fab3c29f0ce952133b1d55966531252e94 100644 (file)
@@ -1,9 +1,11 @@
 #ifdef WITH_DEVIL
 #include <IL/il.h>
 #endif
+#include <msp/fs/utils.h>
 #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 +19,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,29 +39,26 @@ Image::~Image()
 #ifdef WITH_DEVIL
        if(priv->id)
                ilDeleteImages(1, &priv->id);
+       else
 #endif
-#ifdef WITH_LIBPNG
        delete[] priv->data;
-#endif
        delete priv;
 }
 
 void Image::load_file(const string &fn)
 {
+       string ext = FS::extpart(fn);
 #ifdef WITH_LIBPNG
-       if(fn.size()>4 && !fn.compare(fn.size()-4, 4, ".png"))
+       if(ext==".png")
        {
                IO::BufferedFile file(fn);
-               load_png(file, *priv);
+               load_png(file, *priv, 0, 0);
        }
        else
 #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
@@ -92,103 +66,42 @@ void Image::load_file(const string &fn)
        (void)fn;
 }
 
-void Image::load_memory(const void *data, unsigned size)
+void Image::load_io(IO::Base &io)
 {
+       char sig_buf[8];
+       unsigned sig_len = io.read(sig_buf, sizeof(sig_buf));
 #ifdef WITH_LIBPNG
-       if(size>=8 && is_png(data, 8))
-       {
-               IO::Memory mem(reinterpret_cast<const char *>(data), size);
-               load_png(mem, *priv);
-       }
+       if(sig_len==sizeof(sig_buf) && is_png(sig_buf, sig_len))
+               load_png(io, *priv, sig_buf, sig_len);
        else
 #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_io(io, *priv, sig_buf, sig_len);
 #else
                throw unsupported_image_format("DevIL needed for non-PNG images");
 #endif
        }
-       (void)data;
-       (void)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