STM32-LTDC, LCD-TFT, LVGL (MCU3) Lecture 10| Converting bmp images to ‘C’ byte array

 

Converting bmp images to ‘C’ byte array

 

In this article, we will understand how to store an image file in the form of a ‘C’ array in the application.

 

How to calculate how many bytes of memory does an image consume in the computer memory? 

The given resolution of the bitmap image is 480×270, and the pixel format of that image is ARGB8888, which means the Bit depth is 32 bits.

The total memory consumed you can easily calculate by calculating the number of pixels of that image. You just have to multiply width x height, and then the memory consumed by each pixel that is bpp divided by 8, which gives you 4  bytes.

Converting bmp images to 'C' byte array
Figure 1. Calculation of how many bytes of memory does an image consume

 

Look at Figure 1, shows 506.25KB is the total memory consumed by this image in the computer memory or on the disk. So, the computer also says the same thing and the computer says the same.

 

You can see that, the computer says around 506 kilobytes of memory have been consumed by this image. But, this is in the case of the BMP image.

 

BMP(Bitmap) images

  •  An BMP image file contains uncompressed pixel data, hence  consumes more space on the disk.

506.25KB this resolution consumes approximately half an MB, but it will not be the case if it is in JPEG format. Why? Because JPEG doesn’t store the raw pixel values. It uses some kind of image compression technique. But bitmap images generally don’t use any compression on the pixel data. It’s all uncompressed, hence it consumes more space on the disk.

Figure 2. BMP(Bitmap) images
Figure 2. BMP(Bitmap) images

 

  • The advantage of using bitmap images is you can directly read the pixel values and display them on the display module.

You can just send those bytes to the display module and the display module will display that. That means, there is a one-to-one relation between the pixel and the pixel value.

 

But, you cannot directly read and display the .jpg file contents on the display module. You need a JPEG decoder in between.

Converting bmp images to 'C' byte array
Figure 3. Need in between is a JPEG decoder

 

You first have to feed that pixel data to the JPEG decoder and then it gives you the bitmap data and then you can display that on the display module, as shown in Figure 3. 

JPEG decoder can be used in your application in two ways. 

  1. JPEG decoder middleware: There are a lot of open-source JPEG decoder implementations.
  2. JPEG decoder peripheral of the MCU

You can even use that peripheral to decode the JPEG contents. So, even STM32 microcontrollers support JPEG Decoder IP on the microcontroller. 

 

 

Storing Images in the application

  1. Convert the images to bitmap ‘C’  array(pixel value array) with desired pixel format. 
  2. Once you get the ‘C’ array, you store them in the internal flash memory of your microcontroller.
  3. Use the appropriate pixel format to display that information on the display module.

 

 

How to convert the images to a bitmap ‘C’ array?

There are a lot of online tools available to do this. You can use this online image converter tool available on the LVGL website.

In LVGL, just go to tools and go to image converter, as shown in Figure 4.

Figure 4. LVGL website
Figure 4. LVGL website

 

After that, in the image file section, you can upload any image.

After that, you can select the color format or pixel format. You can just keep this as ‘True color’;

Then you can mention the Output format is a ‘C’ array.

And then you need not use this big-endian format because the microcontroller uses little-endian convention.

 

After that you click on Convert.

Figure 5. Online image converter
Figure 5. Online image converter

 

 

Selecting pixel format for your application

That depends on,

  1. Pixel format supported by the LCD Driver chip. 
  2. Desired color range 
  3. Microcontrollers RAM space availability 
  4. Microcontroller Flash space availability 

 

Let’s see one example.

Consider our original image pixel format as ARGB8888. But, the same pixel format we cannot use in our application when we are using the STM32F429IDISCOVERY board.

Figure 6. STM32F429IDISCOVERY board
Figure 6. STM32F429IDISCOVERY board

 

Why? 

Because, this board has a display module, which uses the LCD driver Chip ILI9341. And ILI9341 driver chip supports RGB565/RGB666/RGB222 pixel formats only. It doesn’t support RGB888 or ARGB8888. 

Here we select the RGB565 pixel format in our application. That means, 1 pixel consumes 2 bytes. That’s why, the total flash consumed for 3 frames because we have got three frames in the application. So, here the number of pixels is 320×240, 2 bytes per pixel, and the number of frames is 3, which gives you 450KiB. So, the total flash memory consumed is 450 kilobytes. 

But, we have got  2MB of On-chip Flash on this microcontroller. That’s why 450KiB will not be an issue. 

 

Let’s analyze the same thing for the STM32F746GDISCOVERY board. 

Figure 7. STM32F746GDISCOVERY board
Figure 7. STM32F746GDISCOVERY board

Here, the LCD Driver is  OTA5180A, and it supports up to RGB888 Pixel formats. Because, between the microcontroller and display module, there are 24 data lines. That’s why, it supports up to RGB888 Pixel formats.

If you use RGB888 in your application, 1 pixel consumes 3 bytes. The total flash space consumed for 3 frames will be, here the resolution is 480×270 and into 3, and 3. This would become like 1.1 MB. But the total on-chip Flash on the microcontroller is just 1MB. That’s why your application won’t compile here. You should make sure that the total flash space consumed is less than this 1MB. This is just a flash space consumed by frames. That’s why these frames will not fit into the internal flash of the microcontroller.

 

FastBit Embedded Brain Academy Courses

Click here: https://fastbitlab.com/course1

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.