gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 11:50:46 32 4
gpt4 key购买 nike

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

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com