保存 PNG 图像
我在保存 png 图像时遇到问题。
我想在png文件中添加一些二进制数据,例如以下结构。
struct Foo
{
int value;
char str[10];
double val;
double val2;
};
使用以下代码似乎保存得很好。但是,当我稍后加载 png 文件时,我发现我的块尚未保存,它消失了。我做错了什么?我的需求很简单,我只想在图像本身中嵌入一些额外的二进制信息。
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);
}
上面显示缓冲区已正确更新,并且我的数据已嵌入到图像中。
但是,当我稍后加载它时,它消失了。这是我如何从保存的 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
已经消失了。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
http://www.libpng.org/pub/png/ libpng-1.2.5-manual.html 说:
您是否正在调用它提到的三个 png_write_* 函数中的任何一个? png_set_unknown_chunks 似乎并没有自己进行写入。
http://www.libpng.org/pub/png/libpng-1.2.5-manual.html says:
Are you calling any of the three png_write_* functions it mentions? png_set_unknown_chunks doesn't seem to do the write itself.
是的,看起来 write 实际上没有被调用(正如 fp 提到的)。另外,根据 png_unknown_chunk 的定义...
您不需要执行第一个 memcpy,
因为前一行已经将块的数据指针设置为 foo 的位置。请注意,尽管 foo 已在该函数的堆栈上定义,因此必须在该函数内部执行写入操作以确保 foo 仍然有效。
Yep, looks like the write isn't actually called (as the fp mentioned). Also, according to a definition of png_unknown_chunk...
You don't need to perform the first memcpy
as the previous line has already set the chunk's data pointer to the location of foo. Be aware though that foo has been defined on the stack for this function so the write must be performed inside this function to ensure that foo is still valid.