使用LibJpeg将YUV转换为jpeg时出现段错误

时间:2013-06-08 19:03:21

标签: c jpeg grayscale libjpeg

我需要使用LibJpeg将yuv 422图像中的Y压缩为jpeg图像

Y只是灰度图像

这是我的代码:

y = (char*) malloc(640*480);
for (i = 0, j = 0; j < n; i++, j += 2)
       y[i] = raw[j];
Image *dst;

dst = CreateImage(sz, 8, 1);
dst->imageData = y;

//frame is the dst image
bool ipl2jpeg(Image *frame, unsigned char **outbuffer, long unsigned int *outlen) {
    unsigned char *outdata = (uchar *) frame->imageData;
    struct jpeg_compress_struct cinfo ;
    struct jpeg_error_mgr jerr;
    JSAMPROW row_ptr[1];
    int row_stride;

    *outbuffer = NULL;
    *outlen = 0;

    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_compress(&cinfo);
    jpeg_mem_dest(&cinfo, outbuffer, outlen);

    cinfo.image_width = frame->width;
    cinfo.image_height = frame->height;
    cinfo.input_components = frame->nChannels;

    cinfo.in_color_space = JCS_GRAYSCALE;

    jpeg_set_defaults(&cinfo);
    jpeg_set_quality (&cinfo, 50, true);
    jpeg_start_compress(&cinfo, TRUE);
    row_stride = frame->width * frame->nChannels;

    while (cinfo.next_scanline < cinfo.image_height) {
        row_ptr[0] = &outdata[cinfo.next_scanline * row_stride];
        jpeg_write_scanlines(&cinfo, row_ptr, 1); // Iam getting segm. fault here
    }

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

    return true;
}

我在jpeg_write_scanlines中遇到分段错误我不确定我做得对。 这是将灰度图像转换为jpeg的正确方法吗?

1 个答案:

答案 0 :(得分:0)

您没有发布可编辑的源代码;很难猜出ipl2jpeg()实际上是如何调用的

frame-&gt; nChannels应为1

row_ptr [0]应指向输入帧数据