提问人:Martin Benes 提问时间:5/22/2023 最后编辑:Martin Benes 更新时间:7/28/2023 访问量:43
是否可以使用 libjpeg 将 JPEG 元数据从源复制到目标?
Is it possible to copy JPEG metadata from source to destination with libjpeg?
问:
我可以看到可以通过 libjpeg C API 加载和写入 JPEG 元数据。
但是,它是异步的。有什么好方法可以简单地将 JPEG 元数据从源 JPEG 复制到目标 JPEG?
这就是我处理它的方式。但对我来说,它看起来有点笨拙。
j_compress_ptr cinfo_out; // global variable
// basically getchar() on JPEG
int jpeg_getc(j_decompress_ptr cinfo) {
struct jpeg_source_mgr *datasrc = cinfo->src;
// no bytes in buffer
if (datasrc->bytes_in_buffer == 0) {
if (!(*datasrc->fill_input_buffer)(cinfo))
return -1; // return error
}
// read char
datasrc->bytes_in_buffer--;
return GETJOCTET(*datasrc->next_input_byte++);
}
// called asynchronously during libjpeg decompression
int jpeg_handle_marker(j_decompress_ptr cinfo_in) {
// get length
int length = (jpeg_getc(cinfo) << 8) + jpeg_getc(cinfo) - 2;
// copy
unsigned char *p = new unsigned char[length + 1];
int c, i = 0;
while (i < length) {
if ((c = jpeg_getc(cinfo)) == -1) return FALSE;
p[i++] = (unsigned char)c;
}
p[length] = 0; // last byte to 0
// write markers
int offset = 0;
for (int i = 0; i < num_markers; i++) {
jpeg_write_marker(
cinfo_out,
cinfo_in->unread_marker,
(const JOCTET *)p,
length
);
}
return TRUE;
}
// setup asynchronous routines
void set_marker_handlers(j_decompress_ptr cinfo_in) {
jpeg_set_marker_processor(cinfo_in, JPEG_COM, jpeg_handle_marker);
for (int i = 0; i <= 15; i++)
jpeg_set_marker_processor(cinfo_in, JPEG_APP0+i, jpeg_handle_marker);
}
有人有更好的主意吗?
答: 暂无答案
评论