I'm having trouble saving png images.
I want to add some binary data into the png file, such as the following structure.
struct Foo
{
int value;
char str[10];
double val;
double val2;
};
It seems to save just fine with the following code. However, when I later load up the png file, I see that my chunk has not been saved, its gone. What am I doing wrong? My needs are simple, I just want to embed a little bit of extra binary information with the image itself.
Foo foo;
png_unknown_chunk chunks[1];
strcpy((png_charp)chunks[0].name, "fFoo");
chunks[0].data = &foo;
memcpy(chunks[0].data,&foo,sizeof(Foo)*1);
chunks[0].size = sizeof(Foo);
png_set_unknown_chunks(png_ptr, info_ptr, chunks, 1);
printf("1: %d\n",(int)info_ptr->unknown_chunks_num);
for (int n = 0; n < info_ptr->unknown_chunks_num; ++n)
{
tag_pngmeta p;
memcpy(&p,info_ptr->unknown_chunks[n].data,info_ptr->unknown_chunks[n].size);
printf("2: %s,%d\n",info_ptr->unknown_chunks[n].name,
(int)info_ptr->unknown_chunks[n].size);
printf("3: %s\n",p.name);
}
The above shows that that the buffer was updatted properly and my data is embedded into the image.I
However, when I later load it up, its gone. Heres how I load it up again from a saved png.
png_unknown_chunkp unknowns;
int num_unknowns = (int)png_get_unknown_chunks(png_ptr, info_ptr, &unknowns);
printf("%d-----\n",(int)num_unknowns);
printf("%d\n",(int)info_ptr->unknown_chunks_num);
for (int n = 0; n < info_ptr->unknown_chunks_num; ++n)
{
printf("%s,%d\n",info_ptr->unknown_chunks[n].name, (int)info_ptr->unknown_chunks[n].size);
}
Foo
has disappered.