]> git.tdb.fi Git - libs/gui.git/blobdiff - source/graphics/png/pngloader.cpp
Add some state checking to ImageLoader
[libs/gui.git] / source / graphics / png / pngloader.cpp
index 15684b1ea5e57dfd678ef91de6d0db0304384c21..16ad36bbeb4bd3e7e53463d1ab056340e4a7dfe4 100644 (file)
@@ -31,7 +31,6 @@ struct PngLoader::Private
        png_info *info;
 };
 
-ImageLoader::Register<PngLoader> PngLoader::reg;
 
 PngLoader::PngLoader(IO::Base &io, unsigned sig_bytes):
        priv(new Private)
@@ -55,39 +54,63 @@ bool PngLoader::detect(const std::string &sig)
        return !png_sig_cmp(reinterpret_cast<png_byte *>(const_cast<char*>(sig.data())), 0, sig.size());
 }
 
-void PngLoader::load(Image::Data &data)
+void PngLoader::load_(Image::Data &data)
 {
+       png_byte **rows = 0;
+
        if(setjmp(png_jmpbuf(priv->png)))
+       {
+               delete[] rows;
                throw bad_image_data(priv->message);
+       }
 
        png_read_info(priv->png, priv->info);
        png_uint_32 width;
        png_uint_32 height;
        int depth;
        int color;
-       png_get_IHDR(priv->png, priv->info, &width, &height, &depth, &color, 0, 0, 0);
-       data.width = width;
-       data.height = height;
+       int interlace;
+       png_get_IHDR(priv->png, priv->info, &width, &height, &depth, &color, &interlace, 0, 0);
+       unsigned nchans = png_get_channels(priv->png, priv->info);
+
        if(depth!=8)
                throw unsupported_image_format("depth!=8");
+
+       data.width = width;
+       data.height = height;
+       data.stride = data.width*nchans;
+
        switch(color)
        {
        case PNG_COLOR_TYPE_PALETTE:    data.fmt = COLOR_INDEX; break;
        case PNG_COLOR_TYPE_GRAY:       data.fmt = LUMINANCE; break;
        case PNG_COLOR_TYPE_GRAY_ALPHA: data.fmt = LUMINANCE_ALPHA; break;
-       case PNG_COLOR_TYPE_RGB:        data.fmt = RGB; break;
+       case PNG_COLOR_TYPE_RGB:        data.fmt = (nchans==4 ? RGBX : RGB); break;
        case PNG_COLOR_TYPE_RGB_ALPHA:  data.fmt = RGBA; break;
        default: throw unsupported_image_format("unknown color type");
        }
 
-       unsigned nchans = png_get_channels(priv->png, priv->info);
-       if(nchans==4 && data.fmt==RGB)
-               png_set_strip_alpha(priv->png);
+       data.pixels = new char[data.stride*data.height];
+
+       if(interlace==PNG_INTERLACE_ADAM7)
+       {
+               // ADAM7 requires all rows to be loaded at once
+               unsigned n_passes = png_set_interlace_handling(priv->png);
+               rows = new png_byte *[data.height];
+               for(unsigned y=0; y<data.height; ++y)
+                       rows[y] = reinterpret_cast<png_byte *>(data.pixels+data.stride*(data.height-1-y));
 
-       unsigned rowstride = data.width*nchans;
-       data.data = new char[rowstride*data.height];
-       for(unsigned y=0; y<data.height; ++y)
-               png_read_row(priv->png, reinterpret_cast<png_byte *>(data.data+rowstride*(data.height-1-y)), 0);
+               for(unsigned i=0; i<n_passes; ++i)
+                       png_read_rows(priv->png, rows, 0, data.height);
+
+               delete[] rows;
+               rows = 0;
+       }
+       else
+       {
+               for(unsigned y=0; y<data.height; ++y)
+                       png_read_row(priv->png, reinterpret_cast<png_byte *>(data.pixels+data.stride*(data.height-1-y)), 0);
+       }
 
        png_read_end(priv->png, 0);
 }