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

时间:2021-04-15 20:14:47

标签: linux ubuntu libjpeg custom-error-handling

我在 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");
}

我错过了什么。当我运行程序时,它不会输出“我的错误消息:...”

谢谢!

0 个答案:

没有答案
相关问题