2021-09-07 23:32:26 -04:00
|
|
|
/* SPI Master example: jpeg decoder.
|
|
|
|
|
|
|
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, this
|
|
|
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
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.
|
|
|
|
|
|
|
|
Keep in mind that the decoder library cannot handle progressive files (will give
|
|
|
|
``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 04:47:10 -04:00
|
|
|
#include "jpeg_decoder.h"
|
2021-09-07 23:32:26 -04:00
|
|
|
#include "esp_log.h"
|
2022-08-09 04:47:10 -04:00
|
|
|
#include "esp_check.h"
|
2021-09-07 23:32:26 -04:00
|
|
|
#include <string.h>
|
2022-08-09 04:47:10 -04:00
|
|
|
#include "freertos/FreeRTOS.h"
|
2021-09-07 23:32:26 -04: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");
|
|
|
|
//Define the height and width of the jpeg file. Make sure this matches the actual jpeg
|
|
|
|
//dimensions.
|
|
|
|
|
|
|
|
const char *TAG = "ImageDec";
|
|
|
|
|
|
|
|
//Decode the embedded image into pixel lines that can be used with the rest of the logic.
|
2022-08-09 04:47:10 -04:00
|
|
|
esp_err_t decode_image(uint16_t **pixels)
|
2021-09-07 23:32:26 -04:00
|
|
|
{
|
|
|
|
*pixels = NULL;
|
|
|
|
esp_err_t ret = ESP_OK;
|
|
|
|
|
|
|
|
//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 04:47:10 -04: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");
|
|
|
|
|
|
|
|
//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,
|
2021-09-07 23:32:26 -04:00
|
|
|
}
|
2022-08-09 04:47:10 -04:00
|
|
|
};
|
2021-09-07 23:32:26 -04:00
|
|
|
|
2022-08-09 04:47:10 -04:00
|
|
|
//JPEG decode
|
|
|
|
esp_jpeg_image_output_t outimg;
|
|
|
|
esp_jpeg_decode(&jpeg_cfg, &outimg);
|
2021-09-07 23:32:26 -04:00
|
|
|
|
2022-08-09 04:47:10 -04:00
|
|
|
ESP_LOGI(TAG, "JPEG image decoded! Size of the decoded image is: %dpx x %dpx", outimg.width, outimg.height);
|
2021-09-07 23:32:26 -04:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
err:
|
|
|
|
//Something went wrong! Exit cleanly, de-allocating everything we allocated.
|
|
|
|
if (*pixels != NULL) {
|
|
|
|
free(*pixels);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|