[英]libjpeg custom source manager
So I've been reading the libjpeg documentation and it is extremely lackluster. 因此,我一直在阅读libjpeg文档,这非常乏味。
I have been trying to figure out how to read from a custom memory buffer rather than a file and am not sure how to even test if my solution is working correctly. 我一直在尝试找出如何从自定义内存缓冲区而不是文件中读取内容,并且不确定如何测试我的解决方案是否正常工作。
At the moment my function for loading a jpeg from memory is like so: 目前,我从内存加载jpeg的功能如下:
struct error_mgr{
jpeg_error_mgr pub;
std::jmp_buf buf;
};
bool load_jpeg(void *mem, size_t size, output_struct &output){
jpeg_source_mgr src;
src.next_input_bytes = static_cast<JOCTET*>(mem)-size;
src.bytes_in_buffer = size;
src.init_source = [](j_compress_ptr){};
src.fill_input_buffer = [](j_decompress_ptr cinfo) -> boolean{
// should never reach end of buffer
throw "libjpeg tried to read past end of file";
return true;
};
src.skip_input_data = [](j_compress_ptr cinfo, long num_bytes){
if(num_bytes < 1) return; // negative or 0 us no-op
cinfo->src.next_input_byte+=num_bytes;
cinfo->src.bytes_in_buffer-=num_bytes;
};
src.resync_to_restart = jpeg_resync_to_restart;
src.term_source = [](j_decompress_ptr){};
struct jpeg_decompress_struct cinfo;
error_mgr err;
cinfo.err = jpeg_std_error(&err.pub);
err.pub.error_exit = [](j_common_ptr cinfo){
error_mgr ptr = reinterpret_cast<error_mgr*>(cinfo->err);
std::longjmp(ptr->buf, 1);
};
if(std::setjmp(err.buf)){
jpeg_destroy_decompress(&cinfo);
return false;
}
cinfo.src = &src;
jpeg_create_decompress(&cinfo);
(void) jpeg_read_header(&cinfo, TRUE);
// do the actual reading of the image
return true;
}
But it never makes it past jpeg_read_header
. 但是它永远不会超过
jpeg_read_header
。
I know that this is a jpeg file and I know that my memory is being passed correctly because I have libpng loading images with the same signature and calling function fine, so I'm sure it is how I am setting the source manager in cinfo
. 我知道这是一个jpeg文件,并且我知道内存传递正确,因为我有libpng加载具有相同签名的图像并调用了函数,所以我确定这是我在
cinfo
设置源管理器的方式。
Anybody with more experience in libjpeg know how to do this? 任何有libjpeg经验的人都知道该怎么做?
在我的代码中,我在调用cinfo.src
之前设置了jpeg_create_decompress
,之后jpeg_create_decompress
设置解决了这个问题:)
Using jpeg_mem_src
() works for me, with libjpeg-turbo: 使用
jpeg_mem_src
()对我jpeg_mem_src
,与libjpeg-turbo一起使用:
struct jpeg_decompress_struct cinfo;
/* ... */
char *ptr;
size_t buffer;
/* Initialize ptr, buffer, then: */
jpeg_mem_src(&cinfo, ptr, buffer);
/* Now, it's time for jpeg_read_header(), etc... */
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.