FastLED 3.9.15
Loading...
Searching...
No Matches
TJpg_Decoder.cpp.hpp
Go to the documentation of this file.
1/*
2TJpg_Decoder.cpp
3
4Created by Bodmer 18/10/19
5
6Latest version here:
7https://github.com/Bodmer/TJpg_Decoder
8*/
9
10#include "TJpg_Decoder.h"
12#include "fl/stl/string.h"
13#include "fl/stl/cstring.h" // for fl::memset() and fl::memcpy()
14#include "fl/stl/noexcept.h"
15
16namespace fl {
17namespace third_party {
18
19/***************************************************************************************
20** Function name: TJpg_Decoder
21** Description: Constructor
22***************************************************************************************/
24 // Constructor
25}
26
27/***************************************************************************************
28** Function name: ~TJpg_Decoder
29** Description: Destructor
30***************************************************************************************/
34
35/***************************************************************************************
36** Function name: setSwapBytes
37** Description: Set byte swapping for output
38***************************************************************************************/
40 _swap = swapBytes;
41}
42
43/***************************************************************************************
44** Function name: setJpgScale
45** Description: Set the reduction scale factor (1, 2, 4 or 8)
46***************************************************************************************/
48{
49 switch (scaleFactor)
50 {
51 case 1:
52 jpgScale = 0;
53 break;
54 case 2:
55 jpgScale = 1;
56 break;
57 case 4:
58 jpgScale = 2;
59 break;
60 case 8:
61 jpgScale = 3;
62 break;
63 default:
64 jpgScale = 0;
65 }
66}
67
68/***************************************************************************************
69** Function name: setCallback
70** Description: Set the sketch callback function to render decoded blocks
71***************************************************************************************/
73{
74 tft_output = sketchCallback;
75}
76
77/***************************************************************************************
78** Function name: jd_input (declared static)
79** Description: Called by tjpgd.c to get more data
80***************************************************************************************/
81size_t TJpg_Decoder::jd_input(JDEC* jdec, uint8_t* buf, size_t len) FL_NOEXCEPT
82{
83 // Retrieve instance pointer from JDEC device context
84 TJpg_Decoder *thisPtr = static_cast<TJpg_Decoder*>(jdec->device);
85
86 // Handle an array input
87 if (thisPtr->jpg_source == TJPG_ARRAY) {
88 // Avoid running off end of array
89 if (thisPtr->array_index + len > thisPtr->array_size) {
90 len = thisPtr->array_size - thisPtr->array_index;
91 }
92
93 // If buf is valid then copy len bytes to buffer
94 if (buf) fl::memcpy(buf, (const uint8_t *)(thisPtr->array_data + thisPtr->array_index), len);
95
96 // Move pointer
97 thisPtr->array_index += len;
98 }
99
100 return len;
101}
102
103/***************************************************************************************
104** Function name: jd_output (declared static)
105** Description: Called by tjpgd.c with an image block for rendering
106***************************************************************************************/
107// Pass image block back to the sketch for rendering, may be a complete or partial MCU
108int TJpg_Decoder::jd_output(JDEC* jdec, void* bitmap, JRECT* jrect) FL_NOEXCEPT
109{
110 // Retrieve instance pointer from JDEC device context
111 TJpg_Decoder *thisPtr = static_cast<TJpg_Decoder*>(jdec->device);
112
113 // Check if we have a valid thisPtr
114 if (!thisPtr) {
115 return 0; // Indicate failure
116 }
117
118 // Check if we have a callback function
119 if (!thisPtr->tft_output) {
120 return 0; // Indicate failure
121 }
122
123 // Retrieve rendering parameters and add any offset
124 int16_t x = jrect->left + thisPtr->jpeg_x;
125 int16_t y = jrect->top + thisPtr->jpeg_y;
126 uint16_t w = jrect->right + 1 - jrect->left;
127 uint16_t h = jrect->bottom + 1 - jrect->top;
128
129 // Pass the image block and rendering parameters in a callback to the sketch
130 return thisPtr->tft_output(x, y, w, h, (uint16_t*)bitmap);
131}
132
133/***************************************************************************************
134** Function name: drawJpg
135** Description: Draw a jpg saved in a FLASH memory array
136***************************************************************************************/
137JRESULT TJpg_Decoder::drawJpg(int32_t x, int32_t y, const uint8_t jpeg_data[], size_t data_size) FL_NOEXCEPT {
138 JDEC jdec;
139 JRESULT jresult = JDR_OK;
140
142 array_index = 0;
143 array_data = jpeg_data;
144 array_size = data_size;
145
146 jpeg_x = x;
147 jpeg_y = y;
148
149 jdec.swap = _swap;
150
151 // Analyse input data - pass 'this' as device pointer for callbacks
152 jresult = jd_prepare(&jdec, jd_input, workspace, TJPGD_WORKSPACE_SIZE, this);
153
154 // Extract image and render
155 if (jresult == JDR_OK) {
156 // Debug: Check image dimensions from jdec
157 // jdec should now have width/height populated
158 jresult = jd_decomp(&jdec, jd_output, jpgScale);
159 }
160
161 return jresult;
162}
163
164/***************************************************************************************
165** Function name: getJpgSize
166** Description: Get width and height of a jpg saved in a FLASH memory array
167***************************************************************************************/
168JRESULT TJpg_Decoder::getJpgSize(uint16_t *w, uint16_t *h, const uint8_t jpeg_data[], size_t data_size) FL_NOEXCEPT {
169 JDEC jdec;
170 JRESULT jresult = JDR_OK;
171
172 *w = 0;
173 *h = 0;
174
176 array_index = 0;
177 array_data = jpeg_data;
178 array_size = data_size;
179
180 // Analyse input data - pass 'this' as device pointer for callbacks
181 jresult = jd_prepare(&jdec, jd_input, workspace, TJPGD_WORKSPACE_SIZE, this);
182
183 if (jresult == JDR_OK) {
184 *w = jdec.width;
185 *h = jdec.height;
186 }
187
188 return jresult;
189}
190
191} // namespace third_party
192} // namespace fl
static int jd_output(JDEC *jdec, void *bitmap, JRECT *jrect) FL_NOEXCEPT
void setJpgScale(uint8_t scale) FL_NOEXCEPT
void setSwapBytes(bool swap) FL_NOEXCEPT
void setCallback(SketchCallback sketchCallback) FL_NOEXCEPT
JRESULT getJpgSize(uint16_t *w, uint16_t *h, const uint8_t array[], size_t array_size) FL_NOEXCEPT
JRESULT drawJpg(int32_t x, int32_t y, const uint8_t array[], size_t array_size) FL_NOEXCEPT
uint8_t workspace[TJPGD_WORKSPACE_SIZE]
static size_t jd_input(JDEC *jdec, uint8_t *buf, size_t len) FL_NOEXCEPT
JRESULT jd_decomp(JDEC *jd, int(*outfunc)(JDEC *, void *, JRECT *), uint8_t scale) FL_NOEXCEPT
fl::u16 uint16_t
Definition coder.h:214
JRESULT jd_prepare(JDEC *jd, size_t(*infunc)(JDEC *, uint8_t *, size_t), void *pool, size_t sz_pool, void *dev) FL_NOEXCEPT
bool(* SketchCallback)(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t *data)
fl::i16 int16_t
Definition coder.h:215
fl::i32 int32_t
Definition coder.h:220
unsigned char uint8_t
Definition coder.h:209
uint16_t height
Definition tjpgd.h:61
void * memcpy(void *dest, const void *src, size_t n) FL_NOEXCEPT
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT
#define TJPGD_WORKSPACE_SIZE
Definition tjpgdcnf.h:39