From: Glenn Randers-P. <gl...@gm...> - 2012-03-17 04:43:58
|
On Sat, Mar 17, 2012 at 12:16 AM, Cinolt <cin...@gm...> wrote: > Is it possible to add a custom chunk to an existing PNG file without > decompressing the raw image data from the file than compressing it > back again? The libpng manual has sections for reading and writing PNG > files, but they are for doing it separately, whereas I want to just > add an arbitrary custom chunk to the file. There are other applications for doing that, if you don't want to decode/encode the image. Conceptually, you just need to split your file in two at the spot where you want to insert your custom chunk, and then just combine the two parts and your chunk (cat part1 mychunk part2 > new.png). pngsplit comes to mind. I think it comes with pngcheck which you can find at http://www.libpng.org/pub/png/apps/pngcheck.html It splits your file not into two but into multiple pieces, one per chunk. That leaves you with the task of creating your custom chunk, properly formatted with length, name, data, crc, but that's fairly simple to do in your own code. > > I tried just calling png_write_chunk but it appends it after the IEND chunk. :( OK, fine, just "pngsplit" the result and cat the resulting files in the correct order. Glenn > > #include<stdio.h> > #include<stdlib.h> > #include<png.h> > int main(int argc,char *argv[]){ > FILE *f=fopen(argv[1],"r+b"); > if(!f)exit(EXIT_FAILURE); > png_byte m[8]; > fread(m,1,sizeof(m),f); > if(!png_check_sig(m,sizeof(m)))exit(EXIT_FAILURE); > png_structp write_ptr=png_create_write_struct(PNG_LIBPNG_VER_STRING,0,0,0); > if(!write_ptr)exit(EXIT_FAILURE); > png_init_io(write_ptr,f); > png_write_chunk(write_ptr,"aaaa","a",1); > png_destroy_write_struct(&write_ptr,0); > fclose(f); > return(0); > } > > Any ideas? Thanks for any help. > > ------------------------------------------------------------------------------ > This SF email is sponsosred by: > Try Windows Azure free for 90 days Click Here > http://p.sf.net/sfu/sfd2d-msazure > _______________________________________________ > png-mng-implement mailing list > png...@li... > https://lists.sourceforge.net/lists/listinfo/png-mng-implement |