]> git.tdb.fi Git - libs/gui.git/blobdiff - source/graphics/image.cpp
Separate PNG loading to a separate file
[libs/gui.git] / source / graphics / image.cpp
index 3360b72e7610d0cdf68eb8d9e75a6993682e1a91..56e7c1b01f59c54338e5cfb671f9961ba9502c8b 100644 (file)
@@ -1,33 +1,17 @@
 #ifdef WITH_DEVIL
 #include <IL/il.h>
 #endif
-#ifdef WITH_LIBPNG
-#include <png.h>
 #include <msp/io/file.h>
 #include <msp/io/memory.h>
-#endif
 #include "image.h"
+#include "image_png.h"
+#include "image_private.h"
 
 using namespace std;
 
 namespace Msp {
 namespace Graphics {
 
-struct Image::Private
-{
-#ifdef WITH_DEVIL
-       unsigned id;
-#endif
-#ifdef WITH_LIBPNG
-       PixelFormat fmt;
-       unsigned width;
-       unsigned height;
-       char *data;
-#endif
-
-       Private();
-};
-
 Image::Private::Private()
 {
 #ifdef WITH_DEVIL
@@ -44,69 +28,6 @@ Image::Private::Private()
 
 namespace {
 
-#ifdef WITH_LIBPNG
-void read(png_struct *png, png_byte *data, png_size_t size)
-{
-       IO::Base *in = reinterpret_cast<IO::Base *>(png_get_io_ptr(png));
-       in->read(reinterpret_cast<char *>(data), size);
-}
-
-void load_png(IO::Base &in, Image::Private &priv)
-{
-       png_struct *png = 0;
-       png_info *info = 0;
-       priv.data = 0;
-
-       try
-       {
-               png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
-               info = png_create_info_struct(png);
-
-               if(setjmp(png_jmpbuf(png)))
-                       throw bad_image_data("PNG error");
-
-               png_set_read_fn(png, &in, read);
-               png_read_info(png, info);
-               png_uint_32 width;
-               png_uint_32 height;
-               int depth;
-               int color;
-               png_get_IHDR(png, info, &width, &height, &depth, &color, 0, 0, 0);
-               priv.width = width;
-               priv.height = height;
-               if(depth!=8)
-                       throw unsupported_image_format("depth!=8");
-               switch(color)
-               {
-               case PNG_COLOR_TYPE_PALETTE:    priv.fmt = COLOR_INDEX; break;
-               case PNG_COLOR_TYPE_GRAY:       priv.fmt = LUMINANCE; break;
-               case PNG_COLOR_TYPE_GRAY_ALPHA: priv.fmt = LUMINANCE_ALPHA; break;
-               case PNG_COLOR_TYPE_RGB:        priv.fmt = RGB; break;
-               case PNG_COLOR_TYPE_RGB_ALPHA:  priv.fmt = RGBA; break;
-               default: throw unsupported_image_format("unknown color type");
-               }
-
-               unsigned nchans = png_get_channels(png, info);
-               if(nchans==4 && priv.fmt==RGB)
-                       png_set_strip_alpha(png);
-
-               unsigned rowstride = priv.width*nchans;
-               priv.data = new char[rowstride*priv.height];
-               for(unsigned y=0; y<priv.height; ++y)
-                       png_read_row(png, reinterpret_cast<png_byte *>(priv.data+rowstride*(priv.height-1-y)), 0);
-
-               png_read_end(png, 0);
-               png_destroy_read_struct(&png, &info, 0);
-       }
-       catch(...)
-       {
-               png_destroy_read_struct(&png, &info, 0);
-               delete[] priv.data;
-               throw;
-       }
-}
-#endif
-
 #ifdef WITH_DEVIL
 void ensure_devil_image(unsigned &id)
 {
@@ -174,7 +95,7 @@ void Image::load_file(const string &fn)
 void Image::load_memory(const void *data, unsigned size)
 {
 #ifdef WITH_LIBPNG
-       if(!png_sig_cmp(reinterpret_cast<png_byte *>(const_cast<void *>(data)), 0, 8))
+       if(size>=8 && is_png(data, 8))
        {
                IO::Memory mem(reinterpret_cast<const char *>(data), size);
                load_png(mem, *priv);