0

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

Y only 是灰度图像

这是我的代码:

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 的正确方法吗?

4

1 回答 1

0

您没有发布可编译的源代码;很难猜测 ipl2jpeg() 的实际调用方式

frame->nChannels 应该是 1

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

于 2014-01-21T16:51:30.967 回答