X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fgraphics%2Fpng%2Fpngloader.cpp;h=1dfa77362d2111d88153003f30155a8ff56edd59;hb=917c43de100807f4397e36b66b1a748dbf44370e;hp=50d58b3e3d9fabef282e1f2bf6574ba228f00998;hpb=7f7f3c7494fe10a91215eaff208465636e9979ab;p=libs%2Fgui.git diff --git a/source/graphics/png/pngloader.cpp b/source/graphics/png/pngloader.cpp index 50d58b3..1dfa773 100644 --- a/source/graphics/png/pngloader.cpp +++ b/source/graphics/png/pngloader.cpp @@ -29,9 +29,9 @@ struct PngLoader::Private std::string message; png_struct *png; png_info *info; + int interlace; }; -ImageLoader::Register PngLoader::reg; PngLoader::PngLoader(IO::Base &io, unsigned sig_bytes): priv(new Private) @@ -55,17 +55,19 @@ bool PngLoader::detect(const std::string &sig) return !png_sig_cmp(reinterpret_cast(const_cast(sig.data())), 0, sig.size()); } -void PngLoader::load(Image::Data &data) +void PngLoader::load_headers_(Image::Data &data) { if(setjmp(png_jmpbuf(priv->png))) + { 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); + png_get_IHDR(priv->png, priv->info, &width, &height, &depth, &color, &priv->interlace, 0, 0); unsigned nchans = png_get_channels(priv->png, priv->info); if(depth!=8) @@ -80,17 +82,41 @@ void PngLoader::load(Image::Data &data) 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"); } +} - if(nchans==4 && data.fmt==RGB) - png_set_strip_alpha(priv->png); +void PngLoader::load_pixels_(Image::Data &data) +{ + png_byte **rows = 0; - data.data = new char[data.stride*data.height]; - for(unsigned y=0; ypng, reinterpret_cast(data.data+data.stride*(data.height-1-y)), 0); + if(setjmp(png_jmpbuf(priv->png))) + { + delete[] rows; + throw bad_image_data(priv->message); + } + + if(priv->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.pixels+data.stride*(data.height-1-y)); + + for(unsigned i=0; ipng, rows, 0, data.height); + + delete[] rows; + rows = 0; + } + else + { + for(unsigned y=0; ypng, reinterpret_cast(data.pixels+data.stride*(data.height-1-y)), 0); + } png_read_end(priv->png, 0); }