6#include <TinyJPEGDecoder.h>
9#include "TinyGPU/Surface/ISurface.h"
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28template <
typename RGB_T =
RGB565>
40 decoder_.setSwapBytes(
true);
41 decoder_.setCallback(&JPEGParser::onBlock);
50 void setScale(uint8_t scale) { decoder_.setJpgScale(scale); }
54 bool decode(
const uint8_t* data, size_t size) {
55 uint16_t w = 0, h = 0;
56 if (decoder_.getJpgSize(&w, &h, data, size) != JDR_OK) {
60 return setError("Could not resize target surface");
62 decoder_.setUserData(
this);
63 return checkResult(decoder_.drawJpg(0, 0, data, size));
73 template <
typename FileT>
75 const size_t startPos = file.position();
76 uint16_t w = 0, h = 0;
77 if (decoder_.getJpgSize(&w, &h, file) != JDR_OK) {
81 return setError("Could not resize target surface");
84 decoder_.setUserData(
this);
85 return checkResult(decoder_.drawJpg(0, 0, file));
102 if (result == JDR_OK) {
109 static bool onBlock(tinyjpeg::TinyJPEGDecoder& decoder, int16_t x, int16_t y,
110 uint16_t w, uint16_t h, uint16_t* data) {
111 auto* self =
static_cast<JPEGParser*>(decoder.getUserData());
112 for (uint16_t row = 0; row < h; ++row) {
113 const size_t py =
static_cast<size_t>(y) + row;
114 for (uint16_t col = 0; col < w; ++col) {
115 const size_t px =
static_cast<size_t>(x) + col;
119 if (self->target_.contains(px, py)) {
120 self->target_.setPixel(px, py, RGB_T(data[(row * w) + col]));
Decodes baseline JPEG images into a TinyGPU ISurface, using pschatzmann/TinyJPEG (https://github....
Definition: JPEGParser.h:29
static bool onBlock(tinyjpeg::TinyJPEGDecoder &decoder, int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t *data)
Definition: JPEGParser.h:109
bool decode(FileT &file)
Definition: JPEGParser.h:74
void setScale(uint8_t scale)
Sets the output reduction factor: 1 (full size), 2, 4, or 8.
Definition: JPEGParser.h:50
bool setError(const char *message)
Definition: JPEGParser.h:96
bool decode(const uint8_t *data, size_t size)
Definition: JPEGParser.h:54
JPEGParser(ISurface< RGB_T > &target)
Definition: JPEGParser.h:31
const char * errorMessage_
Definition: JPEGParser.h:94
ISurface< RGB_T > & target_
Definition: JPEGParser.h:92
bool checkResult(JRESULT result)
Definition: JPEGParser.h:101
const char * errorMessage() const
Returns the latest error message, if any.
Definition: JPEGParser.h:89
void setSwapBytes(bool swap)
Definition: JPEGParser.h:47
tinyjpeg::TinyJPEGDecoder decoder_
Definition: JPEGParser.h:93
RGB color stored in 16-bit RGB565 format, byte-swapped from the conventional bit layout.
Definition: RGB565.h:24
Definition: DSIBusESP32.h:19