Discussion:
[png-mng-implement] Write pre filtered and compressed data
Sagen de Jonge
2017-02-26 16:44:28 UTC
Permalink
Hi,

Is there a way to write the pixel data from a single buffer that has all
rows pre compressed and filtered? I'm extracting images from PDF files
which use deflate compression and PNG filters so rather than decompressing
and then recompressing, I'd like to write directly.

-sagen
John Bowler
2017-02-27 01:33:22 UTC
Permalink
Post by Sagen de Jonge
Hi,
Is there a way to write the pixel data from a single buffer that has all
rows pre compressed and filtered? I'm extracting images from PDF files
which use deflate compression and PNG filters so rather than decompressing
and then recompressing, I'd like to write directly.
The answer is yes, in principle (I haven't tested what I'm going to
describe), but why do you need to do it? If you just want to write
the PNG data out, unmodified, just write it directly rather than using
libpng.

If you *must* use libpng for some reason you have to tell it to treat
IDAT chunks (that's the image data) as "unknown" while reading the
file; if you don't do this the filtering and compressed data is lost.
libpng will buffer the IDAT chunks (totally unmodfiied) in the
png_info. Use two png_infos; a separate one to handle the chunks
*after* the IDAT (i.e. the onese returned by png_read_end).

After this you simply create a write structure and do a write with the
respective png_info (i.e. you don't need to create a new png_info, you
just use the ones from the read). This should work with libpng so
long as you only do it only once. If you try to write a second PNG
you will find all the text chunks disappear; it is necessary to use
png_get_text then png_set_text to make it work the second time.
(Fixed in 1.7 ;-)
--
John Bowler <***@gmail.com>
+1 (541) 450-9885
PO BOX 3151
KERBY OR 97531-3151
USA
John Bowler
2017-02-27 01:40:49 UTC
Permalink
Incidentally, if you cannot do it the way I described and have to
rewrite all the image data the IDAT chunks will change and then you
cannot copy unknown chunks unless they are marked safe-to-copy. This
is because they may depend on the precise form of the IDAT chunks.

Also, when you mark IDAT as "unknown" make sure to specify the default
for unknown chunks as "save". This will allow you to copy any PNG
(even if it has an unknown critical chunk.)

John Bowler

Loading...