微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

libjpeg 自定义错误处理 - 无法覆盖消息输出 - ubunu

如何解决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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?