【问题标题】:write a jpeg with libjpeg (seg fault)用 libjpeg 编写 jpeg(段错误)
【发布时间】:2011-01-12 00:04:25
【问题描述】:

尝试使用 libjpeg 从一些原始数据写入 jpeg 文件。

它在jpeg_start_compress() 中触发分段错误

这是代码的相关部分:

void write_sub_image(char *filename, int start, int end)
{
    struct jpeg_compress_struct cinfo;
    unsigned char *stride;
    JSAMPROW row_pointer[1];
    unsigned long new_width = end-start;
    int i;
    FILE *fp;

    stride = (unsigned char *)malloc( new_width * 3);

    fp = fopen(filename, "w+");

    jpeg_create_compress(&cinfo);

    jpeg_stdio_dest(&cinfo, fp);

    cinfo.image_width = new_width;
    cinfo.image_height = height;
    cinfo.input_components = 3;
    cinfo.in_color_space = JCS_RGB;

    jpeg_set_defaults(&cinfo);

    jpeg_start_compress(&cinfo, FALSE);

    for (i=0; i<height; i++) {
        memcpy (stride, image + (start + i * width) * 3, new_width * 3);
        row_pointer[0] = stride;
        jpeg_write_scanlines(&cinfo, &stride, 1);
    }

    jpeg_finish_compress(&cinfo);
    jpeg_destroy_compress(&cinfo);

    fclose(fp);
}

问题不在于 memcpy,它甚至没有进入 for 循环...只是在 _start_compress 处崩溃。

如果相关,系统是 Ubuntu 10.10。

【问题讨论】:

  • 您是否检查过new_widthheight 是否有效(即> 0)?
  • 事实上,这不可能是完整的代码,因为height 没有被声明。
  • 它是在外面声明的,这是一个更大的代码的一部分,但应该是一个可以自己工作的部分。我在测试中检查了 new_width=96 和 height=200。

标签: c linux libjpeg


【解决方案1】:

你需要设置一个错误管理器:

struct jpeg_error_mgr jerr; 
....
cinfo.err = jpeg_std_error(&jerr); 
jpeg_set_defaults(&cinfo);
....

【讨论】:

  • 工作完美......由于某种原因,在我从中获取代码的示例中它不存在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-18
  • 2013-06-04
  • 2013-11-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多