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
|
|
|
|
|
|
|
/*
|
2019-05-22 12:21:11 +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
|
|
|
|
2019-05-22 12:21:11 +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"
|
2022-08-09 08:47:10 +00:00
|
|
|
#include "jpeg_decoder.h"
|
2018-03-09 04:40:12 +00:00
|
|
|
#include "esp_log.h"
|
2022-08-09 08:47:10 +00:00
|
|
|
#include "esp_check.h"
|
2018-03-09 04:40:12 +00:00
|
|
|
#include <string.h>
|
2022-08-09 08:47:10 +00:00
|
|
|
#include "freertos/FreeRTOS.h"
|
2018-03-09 04:40:12 +00:00
|
|
|
|
|
|
|
//Reference the binary-included jpeg file
|
2019-05-22 12:21:11 +00:00
|
|
|
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.
|
|
|
|
|
2019-05-22 12:21:11 +00:00
|
|
|
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.
|
2022-08-09 08:47:10 +00:00
|
|
|
esp_err_t decode_image(uint16_t **pixels)
|
2018-03-09 04:40:12 +00:00
|
|
|
{
|
2019-05-22 12:21:11 +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.
|
2022-08-09 08:47:10 +00:00
|
|
|
*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
|
|
|
|
2022-08-09 08:47:10 +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
|
|
|
|
2022-08-09 08:47:10 +00:00
|
|
|
//JPEG decode
|
|
|
|
esp_jpeg_image_output_t outimg;
|
|
|
|
esp_jpeg_decode(&jpeg_cfg, &outimg);
|
2019-05-22 12:21:11 +00:00
|
|
|
|
2022-08-09 08:47:10 +00:00
|
|
|
ESP_LOGI(TAG, "JPEG image decoded! Size of the decoded image is: %dpx x %dpx", outimg.width, outimg.height);
|
2019-05-22 12:21:11 +00:00
|
|
|
|
2018-03-09 04:40:12 +00:00
|
|
|
return ret;
|
|
|
|
err:
|
|
|
|
//Something went wrong! Exit cleanly, de-allocating everything we allocated.
|
2019-05-22 12:21:11 +00:00
|
|
|
if (*pixels != NULL) {
|
2018-03-09 04:40:12 +00:00
|
|
|
free(*pixels);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|