1# Images
2
3An image can be a file or a variable which stores the bitmap itself and some metadata.
4
5## Store images
6You can store images in two places
7- as a variable in internal memory (RAM or ROM)
8- as a file
9
10### Variables
11Images stored internally in a variable are composed mainly of an `lv_img_dsc_t` structure with the following fields:
12- **header**
13  - *cf* Color format. See [below](#color-format)
14  - *w* width in pixels (<= 2048)
15  - *h* height in pixels (<= 2048)
16  - *always zero* 3 bits which need to be always zero
17  - *reserved* reserved for future use
18- **data** pointer to an array where the image itself is stored
19- **data_size** length of `data` in bytes
20
21These are usually stored within a project as C files. They are linked into the resulting executable like any other constant data.
22
23### Files
24To deal with files you need to add a storage *Drive* to LVGL. In short, a *Drive* is a collection of functions (*open*, *read*, *close*, etc.) registered in LVGL to make file operations.
25You can add an interface to a standard file system (FAT32 on SD card) or you create your simple file system to read data from an SPI Flash memory.
26In every case, a *Drive* is just an abstraction to read and/or write data to memory.
27See the [File system](/overview/file-system) section to learn more.
28
29Images stored as files are not linked into the resulting executable, and must be read into RAM before being drawn. As a result, they are not as resource-friendly as images linked at compile time. However, they are easier to replace without needing to rebuild the main program.
30
31## Color formats
32Various built-in color formats are supported:
33- **LV_IMG_CF_TRUE_COLOR** Simply stores the RGB colors (in whatever color depth LVGL is configured for).
34- **LV_IMG_CF_TRUE_COLOR_ALPHA** Like `LV_IMG_CF_TRUE_COLOR` but it also adds an alpha (transparency) byte for every pixel.
35- **LV_IMG_CF_TRUE_COLOR_CHROMA_KEYED** Like `LV_IMG_CF_TRUE_COLOR` but if a pixel has the `LV_COLOR_TRANSP` color (set in *lv_conf.h*) it will be transparent.
36- **LV_IMG_CF_INDEXED_1/2/4/8BIT** Uses a palette with 2, 4, 16 or 256 colors and stores each pixel in 1, 2, 4 or 8 bits.
37- **LV_IMG_CF_ALPHA_1/2/4/8BIT** **Only stores the Alpha value with 1, 2, 4 or 8 bits.** The pixels take the color of `style.img_recolor` and the set opacity. The source image has to be an alpha channel. This is ideal for bitmaps similar to fonts where the whole image is one color that can be altered.
38
39The bytes of `LV_IMG_CF_TRUE_COLOR` images are stored in the following order.
40
41For 32-bit color depth:
42- Byte 0: Blue
43- Byte 1: Green
44- Byte 2: Red
45- Byte 3: Alpha
46
47For 16-bit color depth:
48- Byte 0: Green 3 lower bit, Blue 5 bit
49- Byte 1: Red 5 bit, Green 3 higher bit
50- Byte 2: Alpha byte (only with LV_IMG_CF_TRUE_COLOR_ALPHA)
51
52For 8-bit color depth:
53- Byte 0: Red 3 bit, Green 3 bit, Blue 2 bit
54- Byte 2: Alpha byte (only with LV_IMG_CF_TRUE_COLOR_ALPHA)
55
56
57You can store images in a *Raw* format to indicate that it's not encoded with one of the built-in color formats and an external [Image decoder](#image-decoder) needs to be used to decode the image.
58- **LV_IMG_CF_RAW** Indicates a basic raw image (e.g. a PNG or JPG image).
59- **LV_IMG_CF_RAW_ALPHA** Indicates that an image has alpha and an alpha byte is added for every pixel.
60- **LV_IMG_CF_RAW_CHROMA_KEYED** Indicates that an image is chroma-keyed as described in `LV_IMG_CF_TRUE_COLOR_CHROMA_KEYED` above.
61
62
63## Add and use images
64
65You can add images to LVGL in two ways:
66- using the online converter
67- manually create images
68
69### Online converter
70The online Image converter is available here: https://lvgl.io/tools/imageconverter
71
72Adding an image to LVGL via the online converter is easy.
73
741. You need to select a *BMP*, *PNG* or *JPG* image first.
752. Give the image a name that will be used within LVGL.
763. Select the [Color format](#color-formats).
774. Select the type of image you want. Choosing a binary will generate a `.bin` file that must be stored separately and read using the [file support](#files). Choosing a variable will generate a standard C file that can be linked into your project.
785. Hit the *Convert* button. Once the conversion is finished, your browser will automatically download the resulting file.
79
80In the generated C arrays (variables), bitmaps for all the color depths (1, 8, 16 or 32) are included in the C file, but only the color depth that matches `LV_COLOR_DEPTH` in *lv_conf.h* will actually be linked into the resulting executable.
81
82In the case of binary files, you need to specify the color format you want:
83- RGB332 for 8-bit color depth
84- RGB565 for 16-bit color depth
85- RGB565 Swap for 16-bit color depth (two bytes are swapped)
86- RGB888 for 32-bit color depth
87
88### Manually create an image
89If you are generating an image at run-time, you can craft an image variable to display it using LVGL. For example:
90
91```c
92uint8_t my_img_data[] = {0x00, 0x01, 0x02, ...};
93
94static lv_img_dsc_t my_img_dsc = {
95    .header.always_zero = 0,
96    .header.w = 80,
97    .header.h = 60,
98    .data_size = 80 * 60 * LV_COLOR_DEPTH / 8,
99    .header.cf = LV_IMG_CF_TRUE_COLOR,          /*Set the color format*/
100    .data = my_img_data,
101};
102
103```
104
105If the color format is `LV_IMG_CF_TRUE_COLOR_ALPHA` you can set `data_size` like `80 * 60 * LV_IMG_PX_SIZE_ALPHA_BYTE`.
106
107Another (possibly simpler) option to create and display an image at run-time is to use the [Canvas](/widgets/core/canvas) object.
108
109### Use images
110
111The simplest way to use an image in LVGL is to display it with an [lv_img](/widgets/core/img) object:
112
113```c
114lv_obj_t * icon = lv_img_create(lv_scr_act(), NULL);
115
116/*From variable*/
117lv_img_set_src(icon, &my_icon_dsc);
118
119/*From file*/
120lv_img_set_src(icon, "S:my_icon.bin");
121```
122
123If the image was converted with the online converter, you should use `LV_IMG_DECLARE(my_icon_dsc)` to declare the image in the file where you want to use it.
124
125
126## Image decoder
127As you can see in the [Color formats](#color-formats) section, LVGL supports several built-in image formats. In many cases, these will be all you need. LVGL doesn't directly support, however, generic image formats like PNG or JPG.
128
129To handle non-built-in image formats, you need to use external libraries and attach them to LVGL via the *Image decoder* interface.
130
131An image decoder consists of 4 callbacks:
132- **info** get some basic info about the image (width, height and color format).
133- **open** open an image: either store a decoded image or set it to `NULL` to indicate the image can be read line-by-line.
134- **read** if *open* didn't fully open an image this function should give some decoded data (max 1 line) from a given position.
135- **close** close an opened image, free the allocated resources.
136
137You can add any number of image decoders. When an image needs to be drawn, the library will try all the registered image decoders until it finds one which can open the image, i.e. one which knows that format.
138
139The `LV_IMG_CF_TRUE_COLOR_...`, `LV_IMG_INDEXED_...` and `LV_IMG_ALPHA_...` formats (essentially, all non-`RAW` formats) are understood by the built-in decoder.
140
141### Custom image formats
142
143The easiest way to create a custom image is to use the online image converter and select `Raw`, `Raw with alpha` or `Raw with chroma-keyed` format. It will just take every byte of the binary file you uploaded and write it as an image "bitmap". You then need to attach an image decoder that will parse that bitmap and generate the real, renderable bitmap.
144
145`header.cf` will be `LV_IMG_CF_RAW`, `LV_IMG_CF_RAW_ALPHA` or `LV_IMG_CF_RAW_CHROMA_KEYED` accordingly. You should choose the correct format according to your needs: a fully opaque image, using an alpha channel or using a chroma key.
146
147After decoding, the *raw* formats are considered *True color* by the library. In other words, the image decoder must decode the *Raw* images to *True color* according to the format described in the [Color formats](#color-formats) section.
148
149If you want to create a custom image, you should use `LV_IMG_CF_USER_ENCODED_0..7` color formats. However, the library can draw images only in *True color* format (or *Raw* but ultimately it will be in *True color* format).
150The `LV_IMG_CF_USER_ENCODED_...` formats are not known by the library and therefore they should be decoded to one of the known formats from the [Color formats](#color-formats) section.
151It's possible to decode an image to a non-true color format first (for example: `LV_IMG_INDEXED_4BITS`) and then call the built-in decoder functions to convert it to *True color*.
152
153With *User encoded* formats, the color format in the open function (`dsc->header.cf`) should be changed according to the new format.
154
155
156### Register an image decoder
157
158Here's an example of getting LVGL to work with PNG images.
159
160First, you need to create a new image decoder and set some functions to open/close the PNG files. It should look like this:
161
162```c
163/*Create a new decoder and register functions */
164lv_img_decoder_t * dec = lv_img_decoder_create();
165lv_img_decoder_set_info_cb(dec, decoder_info);
166lv_img_decoder_set_open_cb(dec, decoder_open);
167lv_img_decoder_set_close_cb(dec, decoder_close);
168
169
170/**
171 * Get info about a PNG image
172 * @param decoder pointer to the decoder where this function belongs
173 * @param src can be file name or pointer to a C array
174 * @param header store the info here
175 * @return LV_RES_OK: no error; LV_RES_INV: can't get the info
176 */
177static lv_res_t decoder_info(lv_img_decoder_t * decoder, const void * src, lv_img_header_t * header)
178{
179  /*Check whether the type `src` is known by the decoder*/
180  if(is_png(src) == false) return LV_RES_INV;
181
182  /* Read the PNG header and find `width` and `height` */
183  ...
184
185  header->cf = LV_IMG_CF_RAW_ALPHA;
186  header->w = width;
187  header->h = height;
188}
189
190/**
191 * Open a PNG image and return the decided image
192 * @param decoder pointer to the decoder where this function belongs
193 * @param dsc pointer to a descriptor which describes this decoding session
194 * @return LV_RES_OK: no error; LV_RES_INV: can't get the info
195 */
196static lv_res_t decoder_open(lv_img_decoder_t * decoder, lv_img_decoder_dsc_t * dsc)
197{
198
199  /*Check whether the type `src` is known by the decoder*/
200  if(is_png(src) == false) return LV_RES_INV;
201
202  /*Decode and store the image. If `dsc->img_data` is `NULL`, the `read_line` function will be called to get the image data line-by-line*/
203  dsc->img_data = my_png_decoder(src);
204
205  /*Change the color format if required. For PNG usually 'Raw' is fine*/
206  dsc->header.cf = LV_IMG_CF_...
207
208  /*Call a built in decoder function if required. It's not required if`my_png_decoder` opened the image in true color format.*/
209  lv_res_t res = lv_img_decoder_built_in_open(decoder, dsc);
210
211  return res;
212}
213
214/**
215 * Decode `len` pixels starting from the given `x`, `y` coordinates and store them in `buf`.
216 * Required only if the "open" function can't open the whole decoded pixel array. (dsc->img_data == NULL)
217 * @param decoder pointer to the decoder the function associated with
218 * @param dsc pointer to decoder descriptor
219 * @param x start x coordinate
220 * @param y start y coordinate
221 * @param len number of pixels to decode
222 * @param buf a buffer to store the decoded pixels
223 * @return LV_RES_OK: ok; LV_RES_INV: failed
224 */
225lv_res_t decoder_built_in_read_line(lv_img_decoder_t * decoder, lv_img_decoder_dsc_t * dsc, lv_coord_t x,
226                                                  lv_coord_t y, lv_coord_t len, uint8_t * buf)
227{
228   /*With PNG it's usually not required*/
229
230   /*Copy `len` pixels from `x` and `y` coordinates in True color format to `buf` */
231
232}
233
234/**
235 * Free the allocated resources
236 * @param decoder pointer to the decoder where this function belongs
237 * @param dsc pointer to a descriptor which describes this decoding session
238 */
239static void decoder_close(lv_img_decoder_t * decoder, lv_img_decoder_dsc_t * dsc)
240{
241  /*Free all allocated data*/
242
243  /*Call the built-in close function if the built-in open/read_line was used*/
244  lv_img_decoder_built_in_close(decoder, dsc);
245
246}
247
248```
249
250So in summary:
251- In `decoder_info`, you should collect some basic information about the image and store it in `header`.
252- In `decoder_open`, you should try to open the image source pointed by `dsc->src`. Its type is already in `dsc->src_type == LV_IMG_SRC_FILE/VARIABLE`.
253If this format/type is not supported by the decoder, return `LV_RES_INV`.
254However, if you can open the image, a pointer to the decoded *True color* image should be set in `dsc->img_data`.
255If the format is known, but you don't want to decode the entire image (e.g. no memory for it), set `dsc->img_data = NULL` and use `read_line` to get the pixel data.
256- In `decoder_close` you should free all allocated resources.
257- `decoder_read` is optional. Decoding the whole image requires extra memory and some computational overhead.
258However, it can decode one line of the image without decoding the whole image, you can save memory and time.
259To indicate that the *line read* function should be used, set `dsc->img_data = NULL` in the open function.
260
261
262### Manually use an image decoder
263
264LVGL will use registered image decoders automatically if you try and draw a raw image (i.e. using the `lv_img` object) but you can use them manually too. Create an `lv_img_decoder_dsc_t` variable to describe the decoding session and call `lv_img_decoder_open()`.
265
266The `color` parameter is used only with `LV_IMG_CF_ALPHA_1/2/4/8BIT` images to tell color of the image.
267`frame_id` can be used if the image to open is an animation.
268
269
270```c
271
272lv_res_t res;
273lv_img_decoder_dsc_t dsc;
274res = lv_img_decoder_open(&dsc, &my_img_dsc, color, frame_id);
275
276if(res == LV_RES_OK) {
277  /*Do something with `dsc->img_data`*/
278  lv_img_decoder_close(&dsc);
279}
280
281```
282
283
284## Image caching
285Sometimes it takes a lot of time to open an image.
286Continuously decoding a PNG image or loading images from a slow external memory would be inefficient and detrimental to the user experience.
287
288Therefore, LVGL caches a given number of images. Caching means some images will be left open, hence LVGL can quickly access them from `dsc->img_data` instead of needing to decode them again.
289
290Of course, caching images is resource intensive as it uses more RAM to store the decoded image. LVGL tries to optimize the process as much as possible (see below), but you will still need to evaluate if this would be beneficial for your platform or not. Image caching may not be worth it if you have a deeply embedded target which decodes small images from a relatively fast storage medium.
291
292### Cache size
293The number of cache entries can be defined with `LV_IMG_CACHE_DEF_SIZE` in *lv_conf.h*. The default value is 1 so only the most recently used image will be left open.
294
295The size of the cache can be changed at run-time with `lv_img_cache_set_size(entry_num)`.
296
297### Value of images
298When you use more images than cache entries, LVGL can't cache all the images. Instead, the library will close one of the cached images to free space.
299
300To decide which image to close, LVGL uses a measurement it previously made of how long it took to open the image. Cache entries that hold slower-to-open images are considered more valuable and are kept in the cache as long as possible.
301
302If you want or need to override LVGL's measurement, you can manually set the *time to open* value in the decoder open function in `dsc->time_to_open = time_ms` to give a higher or lower value. (Leave it unchanged to let LVGL control it.)
303
304Every cache entry has a *"life"* value. Every time an image is opened through the cache, the *life* value of all entries is decreased to make them older.
305When a cached image is used, its *life* value is increased by the *time to open* value to make it more alive.
306
307If there is no more space in the cache, the entry with the lowest life value will be closed.
308
309### Memory usage
310Note that a cached image might continuously consume memory. For example, if three PNG images are cached, they will consume memory while they are open.
311
312Therefore, it's the user's responsibility to be sure there is enough RAM to cache even the largest images at the same time.
313
314### Clean the cache
315Let's say you have loaded a PNG image into a `lv_img_dsc_t my_png` variable and use it in an `lv_img` object. If the image is already cached and you then change the underlying PNG file, you need to notify LVGL to cache the image again. Otherwise, there is no easy way of detecting that the underlying file changed and LVGL will still draw the old image from cache.
316
317To do this, use `lv_img_cache_invalidate_src(&my_png)`. If `NULL` is passed as a parameter, the whole cache will be cleaned.
318
319
320## API
321
322
323### Image buffer
324
325```eval_rst
326
327.. doxygenfile:: lv_img_buf.h
328  :project: lvgl
329
330```
331