How to compress a RGB image to jpeg with libjpeg

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to compress a RGB image to jpeg with libjpeg

1,908 Views
kevin_hector
Contributor II

I'm working on a i.MX.RT 1060 and I try to convert a RGB image coming from a camera to a jpeg image. But I don't succeed to do that because when the code arrived at the line "jpeg_start_compress(&cinfo, TRUE);" the i.MX.RT a problem is generated.

Who can help me ? Thank you for your futur help.

I put below my code :

void encode_jpeg_to_memory(unsigned char* image, int width, int height, int quality,
const char* comment, unsigned long* jpegSize, unsigned char** jpegBuf)

{
   struct jpeg_compress_struct cinfo;
   struct jpeg_error_mgr jerr;

   JSAMPROW row_pointer[1];
   int row_stride;

   cinfo.err = jpeg_std_error(&jerr);


   jpeg_create_compress(&cinfo);

   cinfo.image_width = width;
   cinfo.image_height = height;

   cinfo.input_components = 3;
   cinfo.in_color_space = JCS_RGB;

   jpeg_set_defaults(&cinfo);

   jpeg_set_quality(&cinfo, quality, TRUE);

   jpeg_mem_dest(&cinfo, jpegBuf, jpegSize);

   jpeg_start_compress(&cinfo, TRUE);

   if (comment)

   {
      jpeg_write_marker(&cinfo, JPEG_COM, (const JOCTET*)comment, strlen(comment));
   }

   row_stride = width*3;

   // Encode

   while (cinfo.next_scanline < cinfo.image_height)

   {
      row_pointer[0] = &image[cinfo.next_scanline * row_stride];
      jpeg_write_scanlines(&cinfo, row_pointer, 1);
   }

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

0 Kudos
1 Reply

1,823 Views
Sabina_Bruce
NXP Employee
NXP Employee

Hello Kevin,

Hope you are doing well.

I recommend that you use the Developing a Camera Application with iMXRT Series. This application note includes source code that you may use as a reference. You will find that this example captures images compresses the jpeg and saves it to an SD card.

pastedImage_1.png

Best Regards,

Sabina

-----------------------------------------------------------------------------------------------------------------------

Note: If this post answers your question, please click the Correct Answer button. Thank you!

----------------------------------------------------------------------------------------------------------------------- 

0 Kudos