Skip to content

Commit

Permalink
+ read whole image at once if we have enough memory and x0==0 && y0==0
Browse files Browse the repository at this point in the history
  (adds support for Adam7 interlacing )
  • Loading branch information
artemp committed Apr 10, 2012
1 parent ee2674c commit 7c0824f
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions src/png_reader.cpp
Expand Up @@ -190,22 +190,43 @@ void png_reader::read(unsigned x0, unsigned y0,image_data_32& image)
if (png_get_gAMA(png_ptr, info_ptr, &gamma))
png_set_gamma(png_ptr, 2.2, gamma);

png_read_update_info(png_ptr, info_ptr);

//START read image rows
unsigned w=std::min(unsigned(image.width()),width_);
unsigned h=std::min(unsigned(image.height()),height_);
unsigned rowbytes=png_get_rowbytes(png_ptr, info_ptr);
boost::scoped_array<png_byte> row(new png_byte[rowbytes]);
for (unsigned i=0;i<height_;++i)
if (x0 == 0 && y0 == 0 && image.width() >= width_ && image.height() >= height_)
{
png_read_row(png_ptr,row.get(),0);
if (i>=y0 && i<h)

if (png_get_interlace_type(png_ptr,info_ptr) == PNG_INTERLACE_ADAM7)
{
png_set_interlace_handling(png_ptr); // FIXME: libpng bug?
// according to docs png_read_image
// "..automatically handles interlacing,
// so you don't need to call png_set_interlace_handling()"
}
png_read_update_info(png_ptr, info_ptr);
// we can read whole image at once
// alloc row pointers
boost::scoped_array<png_byte*> rows(new png_bytep[height_]);
for (unsigned i=0; i<height_; ++i)
rows[i] = (png_bytep)image.getRow(i);
png_read_image(png_ptr, rows.get());
}
else
{
png_read_update_info(png_ptr, info_ptr);
unsigned w=std::min(unsigned(image.width()),width_);
unsigned h=std::min(unsigned(image.height()),height_);
unsigned rowbytes=png_get_rowbytes(png_ptr, info_ptr);
boost::scoped_array<png_byte> row(new png_byte[rowbytes]);
//START read image rows
for (unsigned i=0;i<height_;++i)
{
image.setRow(i-y0,reinterpret_cast<unsigned*>(&row[x0]),w);
png_read_row(png_ptr,row.get(),0);
if (i>=y0 && i<h)
{
image.setRow(i-y0,reinterpret_cast<unsigned*>(&row[x0]),w);
}
}
//END
}
//END

png_read_end(png_ptr,0);
png_destroy_read_struct(&png_ptr, &info_ptr,0);
fclose(fp);
Expand Down

0 comments on commit 7c0824f

Please sign in to comment.