esp-idf/examples/peripherals/lcd/tjpgd/main/decode_image.c

69 wiersze
2.3 KiB
C
Czysty Zwykły widok Historia

2021-09-23 04:06:13 +00:00
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*/
2018-03-09 04:40:12 +00:00
/*
The image used for the effect on the LCD in the SPI master example is stored in flash
as a jpeg file. This file contains the decode_image routine, which uses the tiny JPEG
decoder library to decode this JPEG into a format that can be sent to the display.
2018-03-09 04:40:12 +00:00
Keep in mind that the decoder library cannot handle progressive files (will give
2018-03-09 04:40:12 +00:00
``Image decoder: jd_prepare failed (8)`` as an error) so make sure to save in the correct
format if you want to use a different image file.
*/
#include "decode_image.h"
#include "jpeg_decoder.h"
2018-03-09 04:40:12 +00:00
#include "esp_log.h"
#include "esp_check.h"
2018-03-09 04:40:12 +00:00
#include <string.h>
#include "freertos/FreeRTOS.h"
2018-03-09 04:40:12 +00:00
//Reference the binary-included jpeg file
extern const uint8_t image_jpg_start[] asm("_binary_image_jpg_start");
extern const uint8_t image_jpg_end[] asm("_binary_image_jpg_end");
2018-03-09 04:40:12 +00:00
//Define the height and width of the jpeg file. Make sure this matches the actual jpeg
//dimensions.
const char *TAG = "ImageDec";
2018-03-09 04:40:12 +00:00
//Decode the embedded image into pixel lines that can be used with the rest of the logic.
esp_err_t decode_image(uint16_t **pixels)
2018-03-09 04:40:12 +00:00
{
*pixels = NULL;
esp_err_t ret = ESP_OK;
2018-03-09 04:40:12 +00:00
//Alocate pixel memory. Each line is an array of IMAGE_W 16-bit pixels; the `*pixels` array itself contains pointers to these lines.
*pixels = calloc(IMAGE_H * IMAGE_W, sizeof(uint16_t));
ESP_GOTO_ON_FALSE((*pixels), ESP_ERR_NO_MEM, err, TAG, "Error allocating memory for lines");
2018-03-09 04:40:12 +00:00
//JPEG decode config
esp_jpeg_image_cfg_t jpeg_cfg = {
.indata = (uint8_t *)image_jpg_start,
.indata_size = image_jpg_end - image_jpg_start,
.outbuf = (uint8_t*)(*pixels),
.outbuf_size = IMAGE_W * IMAGE_H * sizeof(uint16_t),
.out_format = JPEG_IMAGE_FORMAT_RGB565,
.out_scale = JPEG_IMAGE_SCALE_0,
.flags = {
.swap_color_bytes = 1,
}
};
2018-03-09 04:40:12 +00:00
//JPEG decode
esp_jpeg_image_output_t outimg;
esp_jpeg_decode(&jpeg_cfg, &outimg);
ESP_LOGI(TAG, "JPEG image decoded! Size of the decoded image is: %dpx x %dpx", outimg.width, outimg.height);
2018-03-09 04:40:12 +00:00
return ret;
err:
//Something went wrong! Exit cleanly, de-allocating everything we allocated.
if (*pixels != NULL) {
2018-03-09 04:40:12 +00:00
free(*pixels);
}
return ret;
}