如何解决libjpeg 自定义错误处理 - 无法覆盖消息输出 - ubunu
我在 Ubuntu 上使用带有直接 C 的 libjpeg,我想覆盖标准错误处理。
我有一个 jpeg 文件,错误为“JPEG 文件过早结束”。
这是我的示例程序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "jpeglib.h"
#include <setjmp.h>
struct my_error_mgr {
struct jpeg_error_mgr pub; /* "public" fields */
jmp_buf setjmp_buffer; /* for return to caller */
};
typedef struct my_error_mgr * my_error_ptr;
/*
* Here's the routine that will replace the standard error_exit method:
*/
METHODDEF(void)
my_error_exit (j_common_ptr cinfo)
{
/* cinfo->err really points to a my_error_mgr struct,so coerce pointer */
my_error_ptr myerr = (my_error_ptr) cinfo->err;
/* Always display the message. */
/* We Could postpone this until after returning,if we chose. */
// (*cinfo->err->output_message) (cinfo);
char err_msg[JMSG_LENGTH_MAX];
(*cinfo->err->format_message) (cinfo,err_msg);
fprintf(stderr,"My error message: %s",err_msg);
/* Return control to the setjmp point */
longjmp(myerr->setjmp_buffer,1);
}
GLOBAL(int)
read_JPEG_file (char * filename)
{
struct jpeg_decompress_struct cinfo;
struct my_error_mgr jerr;
/* More stuff */
FILE * infile; /* source file */
JSAMPARRAY buffer; /* Output row buffer */
int row_stride; /* physical row width in output buffer */
if ((infile = fopen(filename,"rb")) == NULL) {
fprintf(stderr,"can't open %s\n",filename);
return 0;
}
cinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = my_error_exit;
if (setjmp(jerr.setjmp_buffer)) {
/* If we get here,the JPEG code has signaled an error.
* We need to clean up the JPEG object,close the input file,and return.
*/
jpeg_destroy_decompress(&cinfo);
fclose(infile);
return 0;
}
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo,infile);
(void) jpeg_read_header(&cinfo,TRUE);
(void) jpeg_start_decompress(&cinfo);
row_stride = cinfo.output_width * cinfo.output_components;
buffer = (*cinfo.mem->alloc_sarray)
((j_common_ptr) &cinfo,JPOOL_IMAGE,row_stride,1);
while (cinfo.output_scanline < cinfo.output_height) {
(void) jpeg_read_scanlines(&cinfo,buffer,1);
}
(void) jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose(infile);
return 1;
}
int
main() {
read_JPEG_file("P026451.jpg");
}
我错过了什么。当我运行程序时,它不会输出“我的错误消息:...”
谢谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。