c - 用 libjpeg 写一个 jpeg(seg 错误)

标签 c linux libjpeg

尝试使用 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。

最佳答案

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

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

关于c - 用 libjpeg 写一个 jpeg(seg 错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4664087/

相关文章:

c - 为什么在ping程序中内核没有去掉IP层

c - 通过 mmap 配置焊盘

c - 返回 libc - 问题

C++ 套接字 recv() 系统调用返回 -1

c++ - 使用 libjpeg 读取 .jpeg 文件时出错

linux - 写出 libjpeg 图像时内存泄漏

C异常处理

c - 为什么链表没有排序?

c# - C# 中 libJPEG 中的 height_in_blocks

c - 使用 flock() 锁定用 fopen() 打开的文件