2025-09-12 7:19 AM - last edited on 2025-09-12 7:22 AM by Andrew Neil
Hi, I'm using an STM32F746bgt6 on a custom board, and I'm using Keil.
I enabled LibJpeg in CubeMX, but I can't get an RGB JPEG.
I have a .jpeg file saved in flash, "frame99.jpg," which I read via littlefs. When I read it, I allocate the buffer to external SDRAM.
int ReadFileSDRAM(lfs_t *lfs, const char *filename)
{
lfs_file_t file;
int res = lfs_file_open(lfs, &file, filename, LFS_O_RDONLY);
if (res < 0)
{
printf("Errore apertura file in lettura\n");
return -1;
}
lfs_soff_t size = lfs_file_size(lfs, &file);
if (size <= 0 || size > SDRAM_SIZE_BYTES)
{
printf("File vuoto, troppo grande o errore lettura dimensione\n");
lfs_file_close(lfs, &file);
return -2;
}
res = lfs_file_read(lfs, &file, image_buffer, size);
if (res < 0)
{
printf("Errore lettura file errore = %d\n", res);
lfs_file_close(lfs, &file);
return -3;
}
uint16_t sample_u = 0;
int16_t sample_s = 0;
image_size1 = size;
while (image_size1 > 0 && image_buffer[image_size1 - 1] == 0xFF)
{
image_size1--;
}
lfs_file_close(lfs, &file);
return (int)size;
}
While the decompression part is
ReadFileSDRAM(&lfs_gd, "frame99.jpg");
int width, height;
int ret = DecodeJPEGtoRGB565_Static(image_buffer, image_size1, image_rgb, &image_size2, &width, &height);
if (ret != 0) {
printf("[JPEG] ERROR JPEG: %d\n", ret);
} else {
printf("[JPEG] OK, width=%d, height=%d, size=%lu\n",
width, height, image_size2);
}
int DecodeJPEGtoRGB565(uint8_t *jpeg_data, size_t jpeg_size, uint8_t *rgb565_buffer, uint32_t *out_size, int *width, int *height)
{
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
printf("[JPEG] Starting decode, size = %lu bytes\n", (unsigned long)jpeg_size);
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
// jpeg_mem_src(&cinfo, jpeg_data, jpeg_size); // Original
jpeg_memory_src(&cinfo, jpeg_data, jpeg_size);
if (jpeg_read_header(&cinfo, TRUE) != JPEG_HEADER_OK)
{
printf("[JPEG] Error: invalid JPEG header!\n");
jpeg_destroy_decompress(&cinfo);
return -1;
}
printf("[JPEG] Header successfully read\n");
jpeg_start_decompress(&cinfo);
*width = cinfo.output_width;
*height = cinfo.output_height;
printf("[JPEG] Image dimensions: %dx%d, components = %d\n", *width, *height, cinfo.output_components);
int row_stride = cinfo.output_width * cinfo.output_components;
uint8_t *row_buffer = (uint8_t *)malloc(row_stride);
if (!row_buffer)
{
printf("[JPEG] Error: malloc failed for row_buffer (%d bytes)\n", row_stride);
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
return -2;
}
printf("[JPEG] Allocated row_buffer of %d bytes\n", row_stride);
uint16_t *pDst = (uint16_t *)rgb565_buffer;
while (cinfo.output_scanline < cinfo.output_height)
{
jpeg_read_scanlines(&cinfo, &row_buffer, 1);
int y = cinfo.output_scanline - 1; // just read row
for (int i = 0; i < cinfo.output_width; i++)
{
uint8_t r = row_buffer[i * 3 + 0];
uint8_t g = row_buffer[i * 3 + 1];
uint8_t b = row_buffer[i * 3 + 2];
*pDst++ = ((r & 0xF8) << 8) |
((g & 0xFC) << 3) |
((b & 0xF8) >> 3);
}
// debug print every 50 rows
if ((y % 50) == 0)
{
printf("[JPEG] Decoded scanline %d/%d\n", y + 1, *height);
}
}
free(row_buffer);
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
*out_size = (*width) * (*height) * 2; // 2 bytes per RGB565 pixel
printf("[JPEG] Decoding completed: final buffer size = %lu bytes\n", (unsigned long)(*out_size));
return 0; // success
}
the code gets stuck in " jpeg_start_decompress(&cinfo); "
I followed it with the print and it gets stuck in - > main = (my_main_ptr)
(*cinfo->mem->alloc_small)((j_common_ptr)cinfo, JPOOL_IMAGE,
SIZEOF(my_main_controller));
how can i solve it?
"
2025-09-15 6:39 AM
Hello @Giovanni3,
Make sure decompression initialization is fully done before calling jpeg_start_decompress().
I also recommend trying the JPEG examples in STM32CubeF7, they will guide you in your application.
I hope my answer has helped you. When your question is answered, please select this topic as the solution that answered you, as it will help others find that answer faster.
Thanks for your contribution.
Dor_RH
2025-09-18 12:28 AM
I currently solved it by importing a third-party TJpg library
2025-09-19 7:08 AM
In terms of decoding performance, is it better to use the internal library? So enable Libjpg from cube?