2021-05-02 04:27 AM
Hello, I am trying to implement bmp to jpeg encode application. I have used official STM code example. There is no compile error.
I gather RGB565 data from OV7670 camera. I can successfully write to SD card as 16 bit bitmap file and open it on a Windows PC. Colors sorting is like BGR. I save my file as 24 bit to encode jpeg. When I encode it to jpeg, I see that color sorting is not match with bmp file.
Jpeg sorting is RGB
bmp sorting is BGR
How can I solve this issue ?
In addition, do I need to convert my raw data from 16bit to 24bit for encoding to jpeg ?
Can I convert 16 bit bmp file to 16 bit jpeg file ? , If so, How can I ?
Best Regards
void bmp2jpeg(void)
{
static struct jpeg_compress_struct cinfo;
/* This struct represents a JPEG error handler */
static struct jpeg_error_mgr jerr;
/* Encode BMP Image to JPEG */
JSAMPROW row_pointer; /* Pointer to a single row */
uint32_t bytesread;
uint32_t index;
/*##-3- Register the file system object to the FatFs module ##############*/
if(f_mount(&fs, "", 0) == FR_OK)
{
/*##-4- Create and Open a new jpg image file with write access #########*/
if(f_open(&MyFile1, "image.jpg", FA_CREATE_ALWAYS | FA_WRITE) == FR_OK)
{
/*##-5- Open the BMP image with read access ##########################*/
if(f_open(&MyFile, "image.bmp", FA_READ) == FR_OK)
{
/*##-6- Jpeg encoding ##############################################*/
//jpeg_encode(&MyFile, &MyFile1, 240, 320, 90, _aucLine);
/* Step 1: allocate and initialize JPEG compression object */
/* Set up the error handler */
cinfo.err = jpeg_std_error(&jerr);
/* Initialize the JPEG compression object */
jpeg_create_compress(&cinfo);
/* Step 2: specify data destination */
jpeg_stdio_dest(&cinfo, &MyFile1);
/* Step 3: set parameters for compression */
cinfo.image_width = 320;
cinfo.image_height = 240;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
/* Set default compression parameters */
jpeg_set_defaults(&cinfo);
cinfo.dct_method = JDCT_FLOAT;
jpeg_set_quality(&cinfo, 90, TRUE);
/* Step 4: start compressor */
jpeg_start_compress(&cinfo, TRUE);
/* Get bitmap data address offset */
f_read(&MyFile, _aucLine, 14, (UINT*)&bytesread);
index = *(__IO uint16_t *) (_aucLine + 10);
index |= (*(__IO uint16_t *) (_aucLine + 12)) << 16;
while (cinfo.next_scanline < cinfo.image_height)
{
/* In this application, the input file is a BMP, which first encodes the bottom of the picture */
/* JPEG encodes the highest part of the picture first. We need to read the lines upside down */
/* Move the read pointer to 'last line of the picture - next_scanline' */
f_lseek(&MyFile, ((cinfo.image_height-1-cinfo.next_scanline)*320*3)+index);
if(f_read(&MyFile, _aucLine, 320*3, (UINT*)&bytesread) == FR_OK)
{
row_pointer = (JSAMPROW)_aucLine;
jpeg_write_scanlines(&cinfo, &row_pointer, 1);
}
}
/* Step 5: finish compression */
jpeg_finish_compress(&cinfo);
/* Step 6: release JPEG compression object */
jpeg_destroy_compress(&cinfo);
/* Close the BMP and JPEG files */
f_close(&MyFile1);
f_close(&MyFile);
}
else
{
Error_Handler();
}
}
else
{
Error_Handler();
}
}
else
{
Error_Handler();
}
}