0
votes

I need to compress the Y ONLY in a yuv 422 image to a jpeg image using LibJpeg

Y only is a grayscale image

here is my code:

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;
}

I am getting a segmentation fault in jpeg_write_scanlines I am not sure I am doing it right. is this the right way to convert grayscale image to jpeg ??

1
in the code i am writing a comment where I am getting a segmentation fault, no it is not working I am asking is this the right way to convert a grayscale image into a jpeg ?? - Ahmed Kato
Ok, you should make it much more obvious what the problem is. For instance, change your title to "getting seg fault when using libjpeg" or something. - Oliver Charlesworth
sorry that I didn't, I am in a hurry, I know how to write good questions but I am under stress and it is not working for me. - Ahmed Kato

1 Answers

0
votes

you are not posting compilable source code; it is hard to guess how ipl2jpeg() is actually called

frame->nChannels should be 1

row_ptr[0] should point to the input frame data