@geastack/engine 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +29 -0
- package/bitmap_font.cpp +165 -0
- package/canvas.cpp +4608 -0
- package/components/Button.d.ts +4 -0
- package/components/Button.tsx +32 -0
- package/components/Image.d.ts +3 -0
- package/components/Image.tsx +17 -0
- package/components/Text.d.ts +3 -0
- package/components/Text.tsx +9 -0
- package/components/View.d.ts +3 -0
- package/components/View.tsx +24 -0
- package/components/index.d.ts +8 -0
- package/components/index.ts +4 -0
- package/css/animation.h +252 -0
- package/css/declarative.h +131 -0
- package/css/easing.h +160 -0
- package/css/engine.h +223 -0
- package/css/interpolate.h +38 -0
- package/image_store.cpp +807 -0
- package/package.json +62 -0
- package/rasterized_font.cpp +570 -0
- package/touch_runtime.cpp +513 -0
- package/ui/absolute_leaf_refresh.cpp +279 -0
- package/ui/absolute_leaf_refresh.h +12 -0
- package/ui/canvas_element.cpp +2314 -0
- package/ui/canvas_element.h +353 -0
- package/ui/canvas_store.cpp +188 -0
- package/ui/canvas_store.h +59 -0
- package/ui/core.cpp +135 -0
- package/ui/css_atom.cpp +155 -0
- package/ui/css_atom.h +21 -0
- package/ui/dirty_regions.cpp +66 -0
- package/ui/dirty_regions.h +34 -0
- package/ui/display_invalidation.cpp +101 -0
- package/ui/display_invalidation.h +15 -0
- package/ui/document.cpp +356 -0
- package/ui/document.h +101 -0
- package/ui/image.h +16 -0
- package/ui/image_element.cpp +13 -0
- package/ui/image_node.cpp +165 -0
- package/ui/input.cpp +936 -0
- package/ui/input_render.cpp +213 -0
- package/ui/internal.h +567 -0
- package/ui/layout.cpp +2025 -0
- package/ui/layout_snapshot.cpp +82 -0
- package/ui/layout_snapshot.h +12 -0
- package/ui/mirror.h +25 -0
- package/ui/node.cpp +243 -0
- package/ui/node.h +154 -0
- package/ui/node_lifecycle.cpp +137 -0
- package/ui/node_lifecycle.h +13 -0
- package/ui/node_model.h +597 -0
- package/ui/refresh_perf.h +235 -0
- package/ui/refresh_trace.h +68 -0
- package/ui/render.cpp +12097 -0
- package/ui/root_scroll_refresh.cpp +859 -0
- package/ui/root_scroll_refresh.h +30 -0
- package/ui/state_init.h +21 -0
- package/ui/style.cpp +14994 -0
- package/ui/style.h +727 -0
- package/ui/style_values.h +31 -0
- package/ui/text.cpp +2010 -0
- package/ui/text.h +15 -0
- package/ui/text_element.cpp +15 -0
- package/ui/tree_events.cpp +621 -0
- package/ui/tree_events.h +69 -0
- package/ui/tree_internal.h +191 -0
- package/ui/tree_nodes.cpp +628 -0
- package/ui/tree_render.cpp +3138 -0
- package/ui/tree_scroll_mirror.cpp +101 -0
- package/ui/tree_state.cpp +574 -0
- package/ui/tree_state.h +235 -0
- package/ui/tree_style.cpp +698 -0
- package/ui/ui.h +9 -0
- package/ui/view.cpp +2232 -0
- package/ui/view.h +14 -0
- package/ui/view_element.cpp +13 -0
- package/ui/viewport_region.cpp +36 -0
- package/ui/viewport_region.h +12 -0
- package/ui/virtual_keyboard.cpp +908 -0
- package/ui/virtual_keyboard.h +59 -0
- package/vendor/AnimatedGIF/AnimatedGIF.c +982 -0
- package/vendor/AnimatedGIF/AnimatedGIF.h +216 -0
- package/vendor/stb/stb_image.h +7988 -0
- package/vendor/stb/stb_truetype.h +5079 -0
package/image_store.cpp
ADDED
|
@@ -0,0 +1,807 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#include "image.h"
|
|
3
|
+
#include "memory.h"
|
|
4
|
+
#include "pixel.h"
|
|
5
|
+
|
|
6
|
+
#include <cstring>
|
|
7
|
+
|
|
8
|
+
// Route stb_image's allocations through the framework allocator (PSRAM-first
|
|
9
|
+
// on embedded targets). Plain malloc would land on the tiny SRAM newlib heap
|
|
10
|
+
// on the rp2350 — where pico_malloc PANICS on failure — so decoding a large
|
|
11
|
+
// PNG killed the boot. Mirrors rasterized_font.cpp's STBTT_malloc routing.
|
|
12
|
+
namespace {
|
|
13
|
+
void *gea_stbi_malloc(std::size_t size)
|
|
14
|
+
{
|
|
15
|
+
return gea::framework::memory::Allocator::allocatePreferSpiram(size, alignof(std::max_align_t));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
void gea_stbi_free(void *ptr)
|
|
19
|
+
{
|
|
20
|
+
if (ptr) gea::framework::memory::Allocator::free(ptr);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
void *gea_stbi_realloc_sized(void *ptr, std::size_t oldSize, std::size_t newSize)
|
|
24
|
+
{
|
|
25
|
+
void *next = gea_stbi_malloc(newSize);
|
|
26
|
+
if (!next) return nullptr;
|
|
27
|
+
if (ptr) {
|
|
28
|
+
std::memcpy(next, ptr, oldSize < newSize ? oldSize : newSize);
|
|
29
|
+
gea_stbi_free(ptr);
|
|
30
|
+
}
|
|
31
|
+
return next;
|
|
32
|
+
}
|
|
33
|
+
} // namespace
|
|
34
|
+
|
|
35
|
+
#define STBI_MALLOC(sz) gea_stbi_malloc(sz)
|
|
36
|
+
#define STBI_REALLOC_SIZED(p, oldsz, newsz) gea_stbi_realloc_sized((p), (oldsz), (newsz))
|
|
37
|
+
#define STBI_FREE(p) gea_stbi_free(p)
|
|
38
|
+
|
|
39
|
+
#define STBI_NO_STDIO
|
|
40
|
+
#define STBI_NO_HDR
|
|
41
|
+
#define STBI_NO_PSD
|
|
42
|
+
#define STBI_NO_TGA
|
|
43
|
+
#define STBI_NO_BMP
|
|
44
|
+
#define STBI_NO_PIC
|
|
45
|
+
#define STBI_NO_PNM
|
|
46
|
+
#define STBI_NO_GIF
|
|
47
|
+
#define STBI_NO_FAILURE_STRINGS
|
|
48
|
+
#define STBI_NO_THREAD_LOCALS
|
|
49
|
+
#define STB_IMAGE_IMPLEMENTATION
|
|
50
|
+
#include "stb_image.h"
|
|
51
|
+
|
|
52
|
+
#if defined(GEA_EMBEDDED_ROM_TJPGD) && GEA_EMBEDDED_ROM_TJPGD
|
|
53
|
+
// ESP32 ROM TinyJPEG decompressor. Block-streaming with a small work pool and
|
|
54
|
+
// 1/2–1/8 descaling, so a full-page cover decodes in a few KB of scratch instead
|
|
55
|
+
// of stb_image's several MB of large contiguous buffers (which the reader's
|
|
56
|
+
// fragmented PSRAM cannot satisfy).
|
|
57
|
+
#include "esp32/rom/tjpgd.h"
|
|
58
|
+
#endif
|
|
59
|
+
|
|
60
|
+
#include "AnimatedGIF.h"
|
|
61
|
+
|
|
62
|
+
#if GEA_EMBEDDED_SINGLE_THREAD_IMAGE_STORE
|
|
63
|
+
namespace gea::framework::graphics {
|
|
64
|
+
class ImageStoreLock {
|
|
65
|
+
public:
|
|
66
|
+
explicit ImageStoreLock(ImageStoreMutex &) {}
|
|
67
|
+
};
|
|
68
|
+
} // namespace gea::framework::graphics
|
|
69
|
+
#else
|
|
70
|
+
#include <mutex>
|
|
71
|
+
namespace gea::framework::graphics {
|
|
72
|
+
using ImageStoreLock = std::lock_guard<ImageStoreMutex>;
|
|
73
|
+
} // namespace gea::framework::graphics
|
|
74
|
+
#endif
|
|
75
|
+
|
|
76
|
+
extern "C" {
|
|
77
|
+
int GIF_openRAM(GIFIMAGE *pGIF, uint8_t *pData, int iDataSize, GIF_DRAW_CALLBACK *pfnDraw);
|
|
78
|
+
void GIF_close(GIFIMAGE *pGIF);
|
|
79
|
+
void GIF_begin(GIFIMAGE *pGIF, int iEndian, unsigned char ucPaletteType);
|
|
80
|
+
void GIF_reset(GIFIMAGE *pGIF);
|
|
81
|
+
int GIF_playFrame(GIFIMAGE *pGIF, int *delayMilliseconds);
|
|
82
|
+
int GIF_getCanvasWidth(GIFIMAGE *pGIF);
|
|
83
|
+
int GIF_getCanvasHeight(GIFIMAGE *pGIF);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
namespace gea::framework::graphics {
|
|
87
|
+
|
|
88
|
+
namespace {
|
|
89
|
+
|
|
90
|
+
class GifDrawTarget {
|
|
91
|
+
public:
|
|
92
|
+
static GifDrawTarget &instance()
|
|
93
|
+
{
|
|
94
|
+
static GifDrawTarget target;
|
|
95
|
+
return target;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
void bind(pixel::native_t *pixels, int width)
|
|
99
|
+
{
|
|
100
|
+
pixels_ = pixels;
|
|
101
|
+
width_ = width;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
void clear()
|
|
105
|
+
{
|
|
106
|
+
pixels_ = nullptr;
|
|
107
|
+
width_ = 0;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
static void draw(GIFDRAW *draw)
|
|
111
|
+
{
|
|
112
|
+
instance().drawFrame(draw);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
private:
|
|
116
|
+
void drawFrame(GIFDRAW *draw)
|
|
117
|
+
{
|
|
118
|
+
if (!pixels_) return;
|
|
119
|
+
|
|
120
|
+
const int canvasY = draw->iY + draw->y;
|
|
121
|
+
pixel::native_t *dst = pixels_ + canvasY * width_ + draw->iX;
|
|
122
|
+
std::uint16_t *palette = draw->pPalette;
|
|
123
|
+
std::uint8_t *src = draw->pPixels;
|
|
124
|
+
const int width = draw->iWidth;
|
|
125
|
+
|
|
126
|
+
// Palette entries are RGB565; convert once to the native pixel (identity on
|
|
127
|
+
// RGB565 targets, so this stays byte-identical there).
|
|
128
|
+
if (draw->ucHasTransparency) {
|
|
129
|
+
const std::uint8_t transparent = draw->ucTransparent;
|
|
130
|
+
for (int i = 0; i < width; i++) {
|
|
131
|
+
const std::uint8_t index = src[i];
|
|
132
|
+
if (index != transparent) dst[i] = pixel::toNative(palette[index]);
|
|
133
|
+
}
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
for (int i = 0; i < width; i++) dst[i] = pixel::toNative(palette[src[i]]);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
pixel::native_t *pixels_ = nullptr;
|
|
141
|
+
int width_ = 0;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
class ImageMemory {
|
|
145
|
+
public:
|
|
146
|
+
template <typename T>
|
|
147
|
+
static T *allocate(std::size_t count = 1)
|
|
148
|
+
{
|
|
149
|
+
// Cache-line alignment (128B covers the P4's largest L2 line config):
|
|
150
|
+
// decoded pixel buffers are DMA sources for PPA blits, and alignment
|
|
151
|
+
// keeps cache writeback/invalidate exact at the buffer edges. Cost: a
|
|
152
|
+
// few bytes per allocation.
|
|
153
|
+
constexpr std::size_t kDmaAlign = 128;
|
|
154
|
+
const std::size_t align = alignof(T) > kDmaAlign ? alignof(T) : kDmaAlign;
|
|
155
|
+
return static_cast<T *>(memory::Allocator::allocatePreferSpiram(sizeof(T) * count, align));
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
static void release(void *ptr)
|
|
159
|
+
{
|
|
160
|
+
memory::Allocator::free(ptr);
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
// Convert one decoded RGBA8888 source pixel to this target's native framebuffer
|
|
165
|
+
// pixel. RGB565 targets get the exact original truncating pack (byte-identical);
|
|
166
|
+
// full-colour targets keep all 8 bits per channel plus the source alpha; the
|
|
167
|
+
// grayscale targets take the luma level (0..15 GRAY4, 0..3 GRAY2 — native_t
|
|
168
|
+
// there is a gray LEVEL, not an RGB565 pack; routing through fromRgb888
|
|
169
|
+
// truncates a uint16 into the byte and produces garbage tones).
|
|
170
|
+
inline pixel::native_t rgbaToNative(std::uint8_t r, std::uint8_t g, std::uint8_t b, std::uint8_t a)
|
|
171
|
+
{
|
|
172
|
+
#if GEA_PIXEL_FORMAT_IS_8888
|
|
173
|
+
return pixel::packNative8(r, g, b, a);
|
|
174
|
+
#elif GEA_PIXEL_FORMAT_IS_GRAY4
|
|
175
|
+
(void)a;
|
|
176
|
+
return pixel::gray4FromRgb888(r, g, b);
|
|
177
|
+
#elif GEA_PIXEL_FORMAT_IS_GRAY2
|
|
178
|
+
(void)a;
|
|
179
|
+
return pixel::gray2FromRgb888(r, g, b);
|
|
180
|
+
#else
|
|
181
|
+
(void)a;
|
|
182
|
+
return pixel::fromRgb888(r, g, b);
|
|
183
|
+
#endif
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Decode RGBA8888 into the native pixel buffer, building a separate 8-bit alpha
|
|
187
|
+
// plane for the framebuffer blit path (unchanged across targets).
|
|
188
|
+
int convertRgbaToNative(const std::uint8_t *rgba, pixel::native_t *out, std::uint8_t **alphaOut,
|
|
189
|
+
int pixelCount, bool opaqueHint)
|
|
190
|
+
{
|
|
191
|
+
std::uint8_t *alpha = nullptr;
|
|
192
|
+
bool hasZero = false;
|
|
193
|
+
|
|
194
|
+
for (int i = 0; i < pixelCount; i++) {
|
|
195
|
+
const int off = i * 4;
|
|
196
|
+
const std::uint8_t a = rgba[off + 3];
|
|
197
|
+
out[i] = rgbaToNative(rgba[off], rgba[off + 1], rgba[off + 2], a);
|
|
198
|
+
|
|
199
|
+
if (opaqueHint) {
|
|
200
|
+
if (a == 0) {
|
|
201
|
+
hasZero = true;
|
|
202
|
+
if (!alpha) {
|
|
203
|
+
alpha = ImageMemory::allocate<std::uint8_t>(static_cast<std::size_t>(pixelCount));
|
|
204
|
+
if (!alpha) return -1;
|
|
205
|
+
std::memset(alpha, 255, static_cast<std::size_t>(pixelCount));
|
|
206
|
+
}
|
|
207
|
+
alpha[i] = 0;
|
|
208
|
+
}
|
|
209
|
+
} else if (a != 255) {
|
|
210
|
+
if (!alpha) {
|
|
211
|
+
alpha = ImageMemory::allocate<std::uint8_t>(static_cast<std::size_t>(pixelCount));
|
|
212
|
+
if (!alpha) return -1;
|
|
213
|
+
std::memset(alpha, 255, static_cast<std::size_t>(pixelCount));
|
|
214
|
+
}
|
|
215
|
+
alpha[i] = a;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (opaqueHint && alpha && !hasZero) {
|
|
220
|
+
ImageMemory::release(alpha);
|
|
221
|
+
alpha = nullptr;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
*alphaOut = alpha;
|
|
225
|
+
return 0;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
} // namespace
|
|
229
|
+
|
|
230
|
+
ImageStore &ImageStore::instance()
|
|
231
|
+
{
|
|
232
|
+
static ImageStore store;
|
|
233
|
+
return store;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
ImageSlot *ImageStore::slot(int id)
|
|
237
|
+
{
|
|
238
|
+
if (id < 0 || id >= kImageMax || !used_[id]) return nullptr;
|
|
239
|
+
return &images_[id];
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const ImageSlot *ImageStore::slot(int id) const
|
|
243
|
+
{
|
|
244
|
+
if (id < 0 || id >= kImageMax || !used_[id]) return nullptr;
|
|
245
|
+
return &images_[id];
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
ImageFormat ImageStore::detectFormat(const std::uint8_t *data, int length) const
|
|
249
|
+
{
|
|
250
|
+
if (!data || length < 4) return ImageFormat::Unknown;
|
|
251
|
+
if (data[0] == 0xFF && data[1] == 0xD8) return ImageFormat::Jpeg;
|
|
252
|
+
if (data[0] == 0x89 && data[1] == 'P' && data[2] == 'N' && data[3] == 'G') return ImageFormat::Png;
|
|
253
|
+
if (data[0] == 'G' && data[1] == 'I' && data[2] == 'F') return ImageFormat::Gif;
|
|
254
|
+
return ImageFormat::Unknown;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
int ImageStore::findFreeSlot(int preferredSlot) const
|
|
258
|
+
{
|
|
259
|
+
if (preferredSlot >= 0 && preferredSlot < kImageMax && !used_[preferredSlot]) return preferredSlot;
|
|
260
|
+
for (int i = 0; i < kImageMax; i++) {
|
|
261
|
+
if (!used_[i]) return i;
|
|
262
|
+
}
|
|
263
|
+
return -1;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
int ImageStore::decodeNextGifFrame(ImageSlot &image)
|
|
267
|
+
{
|
|
268
|
+
auto *gif = static_cast<GIFIMAGE *>(image.gifDecoder);
|
|
269
|
+
if (!gif) return -1;
|
|
270
|
+
|
|
271
|
+
GifDrawTarget::instance().bind(image.pixels, image.width);
|
|
272
|
+
|
|
273
|
+
int delay = 0;
|
|
274
|
+
const int rc = GIF_playFrame(gif, &delay);
|
|
275
|
+
|
|
276
|
+
GifDrawTarget::instance().clear();
|
|
277
|
+
|
|
278
|
+
if (rc < 0) return -1;
|
|
279
|
+
if (delay <= 0) delay = 100;
|
|
280
|
+
image.currentDelayMs = delay;
|
|
281
|
+
return rc;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
int ImageStore::decodeGif(ImageSlot &image, const std::uint8_t *data, int length)
|
|
285
|
+
{
|
|
286
|
+
auto *gifData = static_cast<std::uint8_t *>(ImageMemory::allocate<std::uint8_t>(static_cast<std::size_t>(length)));
|
|
287
|
+
if (!gifData) return -1;
|
|
288
|
+
std::memcpy(gifData, data, static_cast<std::size_t>(length));
|
|
289
|
+
|
|
290
|
+
auto *gif = static_cast<GIFIMAGE *>(ImageMemory::allocate<GIFIMAGE>());
|
|
291
|
+
if (!gif) {
|
|
292
|
+
ImageMemory::release(gifData);
|
|
293
|
+
return -1;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
GIF_begin(gif, GEA_EMBEDDED_PIXEL_PANEL_ENDIAN ? BIG_ENDIAN_PIXELS : LITTLE_ENDIAN_PIXELS, GIF_PALETTE_RGB565);
|
|
297
|
+
if (!GIF_openRAM(gif, gifData, length, GifDrawTarget::draw)) {
|
|
298
|
+
ImageMemory::release(gif);
|
|
299
|
+
ImageMemory::release(gifData);
|
|
300
|
+
return -1;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
const int width = GIF_getCanvasWidth(gif);
|
|
304
|
+
const int height = GIF_getCanvasHeight(gif);
|
|
305
|
+
if (width <= 0 || height <= 0) {
|
|
306
|
+
GIF_close(gif);
|
|
307
|
+
ImageMemory::release(gif);
|
|
308
|
+
ImageMemory::release(gifData);
|
|
309
|
+
return -1;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
auto *pixels = ImageMemory::allocate<pixel::native_t>(static_cast<std::size_t>(width) * height);
|
|
313
|
+
if (!pixels) {
|
|
314
|
+
GIF_close(gif);
|
|
315
|
+
ImageMemory::release(gif);
|
|
316
|
+
ImageMemory::release(gifData);
|
|
317
|
+
return -1;
|
|
318
|
+
}
|
|
319
|
+
std::memset(pixels, 0, static_cast<std::size_t>(width) * height * sizeof(pixel::native_t));
|
|
320
|
+
|
|
321
|
+
image.width = width;
|
|
322
|
+
image.height = height;
|
|
323
|
+
image.pixels = pixels;
|
|
324
|
+
image.ownsPixels = true;
|
|
325
|
+
image.gifDecoder = gif;
|
|
326
|
+
image.gifData = gifData;
|
|
327
|
+
image.gifDataLength = length;
|
|
328
|
+
image.loopCount = -1;
|
|
329
|
+
image.playing = true;
|
|
330
|
+
image.frameCount = 2;
|
|
331
|
+
|
|
332
|
+
if (decodeNextGifFrame(image) >= 0) return 0;
|
|
333
|
+
|
|
334
|
+
ImageMemory::release(pixels);
|
|
335
|
+
GIF_close(gif);
|
|
336
|
+
ImageMemory::release(gif);
|
|
337
|
+
ImageMemory::release(gifData);
|
|
338
|
+
image = ImageSlot{};
|
|
339
|
+
return -1;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
#if defined(GEA_EMBEDDED_ROM_TJPGD) && GEA_EMBEDDED_ROM_TJPGD
|
|
343
|
+
namespace {
|
|
344
|
+
// Shared input+output context for one ROM-tjpgd session (jd->device carries it
|
|
345
|
+
// to both callbacks).
|
|
346
|
+
struct TjpgdCtx {
|
|
347
|
+
const std::uint8_t *in;
|
|
348
|
+
std::size_t len;
|
|
349
|
+
std::size_t pos;
|
|
350
|
+
pixel::native_t *out;
|
|
351
|
+
int outW;
|
|
352
|
+
int outH;
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
// Stream bytes from the in-memory JPEG. A null buf means "skip nbyte".
|
|
356
|
+
UINT geaTjpgdInput(JDEC *jd, BYTE *buf, UINT nbyte)
|
|
357
|
+
{
|
|
358
|
+
auto *c = static_cast<TjpgdCtx *>(jd->device);
|
|
359
|
+
const std::size_t avail = c->len - c->pos;
|
|
360
|
+
const std::size_t take = nbyte < avail ? nbyte : avail;
|
|
361
|
+
if (buf) std::memcpy(buf, c->in + c->pos, take);
|
|
362
|
+
c->pos += take;
|
|
363
|
+
return static_cast<UINT>(take);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// Receive a decoded RGB888 block and write it (as luma) into the native buffer.
|
|
367
|
+
UINT geaTjpgdOutput(JDEC *jd, void *bitmap, JRECT *rect)
|
|
368
|
+
{
|
|
369
|
+
auto *c = static_cast<TjpgdCtx *>(jd->device);
|
|
370
|
+
const std::uint8_t *rgb = static_cast<const std::uint8_t *>(bitmap);
|
|
371
|
+
for (int y = rect->top; y <= rect->bottom; y++) {
|
|
372
|
+
for (int x = rect->left; x <= rect->right; x++) {
|
|
373
|
+
const std::uint8_t r = rgb[0];
|
|
374
|
+
const std::uint8_t g = rgb[1];
|
|
375
|
+
const std::uint8_t b = rgb[2];
|
|
376
|
+
rgb += 3;
|
|
377
|
+
if (x < c->outW && y < c->outH) {
|
|
378
|
+
const std::uint8_t lum = static_cast<std::uint8_t>((r * 77 + g * 150 + b * 29) >> 8);
|
|
379
|
+
c->out[y * c->outW + x] = rgbaToNative(lum, lum, lum, 255);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
return 1;
|
|
384
|
+
}
|
|
385
|
+
} // namespace
|
|
386
|
+
|
|
387
|
+
// Decode a (large) JPEG with the ROM tinyjpeg decompressor, descaling so the
|
|
388
|
+
// output fits the panel. Returns 0 on success (fills `image`), -1 otherwise.
|
|
389
|
+
int ImageStore::decodeJpegTjpgd(ImageSlot &image, const std::uint8_t *data, int length)
|
|
390
|
+
{
|
|
391
|
+
TjpgdCtx ctx{data, static_cast<std::size_t>(length), 0, nullptr, 0, 0};
|
|
392
|
+
// Work pool for the huffman/quant tables + one-MCU scratch. Generous and
|
|
393
|
+
// PSRAM-backed so jd_prepare never fails for lack of pool.
|
|
394
|
+
const std::size_t poolSize = 64 * 1024;
|
|
395
|
+
void *pool = memory::Allocator::allocatePreferSpiram(poolSize, alignof(std::max_align_t));
|
|
396
|
+
if (!pool) return -1;
|
|
397
|
+
JDEC jd;
|
|
398
|
+
if (jd_prepare(&jd, geaTjpgdInput, pool, poolSize, &ctx) != JDR_OK) {
|
|
399
|
+
memory::Allocator::free(pool);
|
|
400
|
+
return -1;
|
|
401
|
+
}
|
|
402
|
+
// Descale by powers of two until both axes sit at/under the panel's long
|
|
403
|
+
// side; a grayscale e-paper contain-fitting the result shows no visible loss.
|
|
404
|
+
int scale = 0;
|
|
405
|
+
while (((static_cast<int>(jd.width) >> scale) > 480 || (static_cast<int>(jd.height) >> scale) > 480) && scale < 3) scale++;
|
|
406
|
+
ctx.outW = static_cast<int>(jd.width) >> scale;
|
|
407
|
+
ctx.outH = static_cast<int>(jd.height) >> scale;
|
|
408
|
+
if (ctx.outW <= 0 || ctx.outH <= 0) {
|
|
409
|
+
memory::Allocator::free(pool);
|
|
410
|
+
return -1;
|
|
411
|
+
}
|
|
412
|
+
auto *px = ImageMemory::allocate<pixel::native_t>(static_cast<std::size_t>(ctx.outW) * ctx.outH);
|
|
413
|
+
if (!px) {
|
|
414
|
+
memory::Allocator::free(pool);
|
|
415
|
+
return -1;
|
|
416
|
+
}
|
|
417
|
+
ctx.out = px;
|
|
418
|
+
const JRESULT rc = jd_decomp(&jd, geaTjpgdOutput, static_cast<BYTE>(scale));
|
|
419
|
+
memory::Allocator::free(pool);
|
|
420
|
+
if (rc != JDR_OK) {
|
|
421
|
+
ImageMemory::release(px);
|
|
422
|
+
return -1;
|
|
423
|
+
}
|
|
424
|
+
image.width = ctx.outW;
|
|
425
|
+
image.height = ctx.outH;
|
|
426
|
+
image.frameCount = 1;
|
|
427
|
+
image.pixels = px;
|
|
428
|
+
image.ownsPixels = true;
|
|
429
|
+
image.alpha = nullptr;
|
|
430
|
+
image.loopCount = 0;
|
|
431
|
+
image.playing = false;
|
|
432
|
+
return 0;
|
|
433
|
+
}
|
|
434
|
+
#endif
|
|
435
|
+
|
|
436
|
+
int ImageStore::decodeStatic(ImageSlot &image, const std::uint8_t *data, int length, bool opaqueHint)
|
|
437
|
+
{
|
|
438
|
+
int width = 0;
|
|
439
|
+
int height = 0;
|
|
440
|
+
int components = 0;
|
|
441
|
+
|
|
442
|
+
// "GTHM": raw 8-bit grayscale thumbnail with a fixed 8-byte header
|
|
443
|
+
// (magic, u16le width, u16le height), written by app-side thumbnailers
|
|
444
|
+
// (e-reader library covers). Decodes straight to native pixels — no codec.
|
|
445
|
+
if (length >= 8 && data[0] == 'G' && data[1] == 'T' && data[2] == 'H' && data[3] == 'M') {
|
|
446
|
+
const int tw = data[4] | (data[5] << 8);
|
|
447
|
+
const int th = data[6] | (data[7] << 8);
|
|
448
|
+
if (tw <= 0 || th <= 0 || length < 8 + tw * th) return -1;
|
|
449
|
+
auto *px = ImageMemory::allocate<pixel::native_t>(static_cast<std::size_t>(tw) * th);
|
|
450
|
+
if (!px) return -1;
|
|
451
|
+
const std::uint8_t *gray = data + 8;
|
|
452
|
+
const int count = tw * th;
|
|
453
|
+
for (int i = 0; i < count; i++) px[i] = rgbaToNative(gray[i], gray[i], gray[i], 255);
|
|
454
|
+
image.width = tw;
|
|
455
|
+
image.height = th;
|
|
456
|
+
image.frameCount = 1;
|
|
457
|
+
image.pixels = px;
|
|
458
|
+
image.ownsPixels = true;
|
|
459
|
+
image.alpha = nullptr;
|
|
460
|
+
image.loopCount = 0;
|
|
461
|
+
image.playing = false;
|
|
462
|
+
return 0;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
#if defined(GEA_EMBEDDED_ROM_TJPGD) && GEA_EMBEDDED_ROM_TJPGD
|
|
466
|
+
// Large JPEGs (covers/photos) go through the low-memory ROM decoder. Small
|
|
467
|
+
// JPEGs keep the stb path (its fixed cost is fine and avoids the pool alloc).
|
|
468
|
+
if (detectFormat(data, length) == ImageFormat::Jpeg) {
|
|
469
|
+
int jw = 0, jh = 0, jc = 0;
|
|
470
|
+
if (stbi_info_from_memory(data, length, &jw, &jh, &jc) && static_cast<long long>(jw) * jh > 200000) {
|
|
471
|
+
const int rc = decodeJpegTjpgd(image, data, length);
|
|
472
|
+
if (rc == 0) return 0;
|
|
473
|
+
// Fall through to stb on tjpgd failure (best-effort).
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
#endif
|
|
477
|
+
|
|
478
|
+
#if (defined(GEA_EMBEDDED_DISPLAY_MONOCHROME) && GEA_EMBEDDED_DISPLAY_MONOCHROME) || \
|
|
479
|
+
(defined(GEA_EMBEDDED_IMAGE_GRAYSCALE_DECODE) && GEA_EMBEDDED_IMAGE_GRAYSCALE_DECODE)
|
|
480
|
+
// Grayscale panel (1-bit e-paper OR 16-level e-paper like M5Paper) + a LARGE
|
|
481
|
+
// image (a full-page cover / photo): decode to a single grayscale channel
|
|
482
|
+
// (1 B/px) instead of RGBA (4 B/px). The panel shows no color so this is
|
|
483
|
+
// visually identical, and it cuts the decode transient 4× — a 525×735 cover
|
|
484
|
+
// drops from a 1.5 MB RGBA buffer to 386 KB, which fits the device's ~1.7 MB
|
|
485
|
+
// free PSRAM (the RGBA path OOM-aborted a bystander `new`, or failed soft to a
|
|
486
|
+
// blank image). Gated on size (grayscale drops the alpha plane) so small
|
|
487
|
+
// transparent icons keep the full RGBA path; only the memory-heavy images
|
|
488
|
+
// take the grayscale route.
|
|
489
|
+
{
|
|
490
|
+
int infoW = 0, infoH = 0, infoC = 0;
|
|
491
|
+
if (stbi_info_from_memory(data, length, &infoW, &infoH, &infoC) && static_cast<long long>(infoW) * infoH > 200000) {
|
|
492
|
+
stbi_uc *gray = stbi_load_from_memory(data, length, &width, &height, &components, 1);
|
|
493
|
+
if (!gray) return -1;
|
|
494
|
+
// Box-downsample large covers so the RESIDENT native buffer (and the
|
|
495
|
+
// transient during conversion) stays small. On the ~1.7 MB-free device
|
|
496
|
+
// a full-res 525×735 native buffer (771 KB) added to the still-held
|
|
497
|
+
// grayscale (386 KB) and compressed file (~450 KB) buffers overflowed
|
|
498
|
+
// the heap → the native allocation failed and the decode returned -1
|
|
499
|
+
// (a blank cover). Halving each axis while the source stays over the
|
|
500
|
+
// panel's short side (540) cuts the native buffer 4× (→ ~190 KB); a
|
|
501
|
+
// grayscale e-paper contain-fitting the result shows no visible loss.
|
|
502
|
+
int factor = 1;
|
|
503
|
+
while ((width / factor > 480 || height / factor > 480) && factor < 8) factor *= 2;
|
|
504
|
+
const int outW = width / factor;
|
|
505
|
+
const int outH = height / factor;
|
|
506
|
+
const int outPixelCount = outW * outH;
|
|
507
|
+
auto *grayPixels = ImageMemory::allocate<pixel::native_t>(static_cast<std::size_t>(outPixelCount));
|
|
508
|
+
if (!grayPixels) {
|
|
509
|
+
stbi_image_free(gray);
|
|
510
|
+
return -1;
|
|
511
|
+
}
|
|
512
|
+
for (int oy = 0; oy < outH; oy++) {
|
|
513
|
+
for (int ox = 0; ox < outW; ox++) {
|
|
514
|
+
// Average the factor×factor source block for a clean downscale.
|
|
515
|
+
int sum = 0;
|
|
516
|
+
for (int by = 0; by < factor; by++) {
|
|
517
|
+
const std::uint8_t *row = gray + static_cast<std::size_t>(oy * factor + by) * width + ox * factor;
|
|
518
|
+
for (int bx = 0; bx < factor; bx++) sum += row[bx];
|
|
519
|
+
}
|
|
520
|
+
const std::uint8_t g = static_cast<std::uint8_t>(sum / (factor * factor));
|
|
521
|
+
grayPixels[oy * outW + ox] = rgbaToNative(g, g, g, 255);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
stbi_image_free(gray);
|
|
525
|
+
image.width = outW;
|
|
526
|
+
image.height = outH;
|
|
527
|
+
image.frameCount = 1;
|
|
528
|
+
image.pixels = grayPixels;
|
|
529
|
+
image.ownsPixels = true;
|
|
530
|
+
image.alpha = nullptr;
|
|
531
|
+
image.loopCount = 0;
|
|
532
|
+
image.playing = false;
|
|
533
|
+
return 0;
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
#endif
|
|
537
|
+
|
|
538
|
+
stbi_uc *rgba = stbi_load_from_memory(data, length, &width, &height, &components, 4);
|
|
539
|
+
if (!rgba) return -1;
|
|
540
|
+
|
|
541
|
+
const int pixelCount = width * height;
|
|
542
|
+
auto *pixels = ImageMemory::allocate<pixel::native_t>(static_cast<std::size_t>(pixelCount));
|
|
543
|
+
if (!pixels) {
|
|
544
|
+
stbi_image_free(rgba);
|
|
545
|
+
return -1;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
// Decode straight into the native pixel: RGB565 targets get the 565 copy
|
|
549
|
+
// (byte-identical to before), full-colour targets keep the 8-bit channels +
|
|
550
|
+
// source alpha — no separate retained RGBA8888 buffer, no dual-path.
|
|
551
|
+
std::uint8_t *alpha = nullptr;
|
|
552
|
+
if (convertRgbaToNative(rgba, pixels, &alpha, pixelCount, opaqueHint) < 0) {
|
|
553
|
+
ImageMemory::release(pixels);
|
|
554
|
+
stbi_image_free(rgba);
|
|
555
|
+
return -1;
|
|
556
|
+
}
|
|
557
|
+
stbi_image_free(rgba);
|
|
558
|
+
|
|
559
|
+
image.width = width;
|
|
560
|
+
image.height = height;
|
|
561
|
+
image.frameCount = 1;
|
|
562
|
+
image.pixels = pixels;
|
|
563
|
+
image.ownsPixels = true;
|
|
564
|
+
image.alpha = alpha;
|
|
565
|
+
image.loopCount = 0;
|
|
566
|
+
image.playing = false;
|
|
567
|
+
return 0;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
int ImageStore::decodeWithHint(const std::uint8_t *data, int length, int preferredSlot, bool opaqueHint)
|
|
571
|
+
{
|
|
572
|
+
if (!data || length <= 0) return -1;
|
|
573
|
+
|
|
574
|
+
// Reserve the slot under the lock, decode without it: the async tile
|
|
575
|
+
// loader decodes on the second core while the frame task allocates and
|
|
576
|
+
// disposes other slots. The reserved id isn't visible to the app until the
|
|
577
|
+
// loader delivers it, so nothing reads the slot mid-decode.
|
|
578
|
+
int id = -1;
|
|
579
|
+
{
|
|
580
|
+
ImageStoreLock lock(slotMutex_);
|
|
581
|
+
id = findFreeSlot(preferredSlot);
|
|
582
|
+
if (id < 0) return -1;
|
|
583
|
+
if (used_[id]) disposeLocked(id);
|
|
584
|
+
images_[id] = ImageSlot{};
|
|
585
|
+
used_[id] = true;
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
ImageSlot &image = images_[id];
|
|
589
|
+
image.format = detectFormat(data, length);
|
|
590
|
+
|
|
591
|
+
const int rc = image.format == ImageFormat::Gif ? decodeGif(image, data, length)
|
|
592
|
+
: decodeStatic(image, data, length, opaqueHint);
|
|
593
|
+
if (rc < 0) {
|
|
594
|
+
ImageStoreLock lock(slotMutex_);
|
|
595
|
+
disposeLocked(id);
|
|
596
|
+
return -1;
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
image.currentFrame = 0;
|
|
600
|
+
image.elapsedMs = 0;
|
|
601
|
+
return id;
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
int ImageStore::decodeDeferred(const std::uint8_t *data, int length, int preferredSlot)
|
|
605
|
+
{
|
|
606
|
+
if (!data || length <= 0) return -1;
|
|
607
|
+
// Animated content needs its decoder state up-front; only defer stills.
|
|
608
|
+
if (detectFormat(data, length) == ImageFormat::Gif) return decode(data, length, preferredSlot);
|
|
609
|
+
|
|
610
|
+
int width = 0;
|
|
611
|
+
int height = 0;
|
|
612
|
+
int components = 0;
|
|
613
|
+
if (!stbi_info_from_memory(data, length, &width, &height, &components)) {
|
|
614
|
+
return decode(data, length, preferredSlot);
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
int id = -1;
|
|
618
|
+
{
|
|
619
|
+
ImageStoreLock lock(slotMutex_);
|
|
620
|
+
id = findFreeSlot(preferredSlot);
|
|
621
|
+
if (id < 0) return -1;
|
|
622
|
+
if (used_[id]) disposeLocked(id);
|
|
623
|
+
images_[id] = ImageSlot{};
|
|
624
|
+
used_[id] = true;
|
|
625
|
+
}
|
|
626
|
+
ImageSlot &image = images_[id];
|
|
627
|
+
image.format = detectFormat(data, length);
|
|
628
|
+
image.width = width;
|
|
629
|
+
image.height = height;
|
|
630
|
+
image.frameCount = 1;
|
|
631
|
+
image.deferredData = data;
|
|
632
|
+
image.deferredLength = length;
|
|
633
|
+
image.deferredOpaque = components == 1 || components == 3;
|
|
634
|
+
return id;
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
bool ImageStore::materializeOneDeferred()
|
|
638
|
+
{
|
|
639
|
+
for (int i = 0; i < kImageMax; i++) {
|
|
640
|
+
if (used_[i] && images_[i].deferredData) {
|
|
641
|
+
materializeDeferred(i);
|
|
642
|
+
return true;
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
return false;
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
void ImageStore::materializeDeferred(int id) const
|
|
649
|
+
{
|
|
650
|
+
auto *self = const_cast<ImageStore *>(this);
|
|
651
|
+
ImageSlot *image = self->slot(id);
|
|
652
|
+
if (!image || !image->deferredData) return;
|
|
653
|
+
ImageStoreLock lock(self->slotMutex_);
|
|
654
|
+
if (!image->deferredData) return; // raced with another materializer
|
|
655
|
+
const std::uint8_t *data = image->deferredData;
|
|
656
|
+
const int length = image->deferredLength;
|
|
657
|
+
const bool opaque = image->deferredOpaque;
|
|
658
|
+
image->deferredData = nullptr;
|
|
659
|
+
image->deferredLength = 0;
|
|
660
|
+
image->deferredOpaque = false;
|
|
661
|
+
self->decodeStatic(*image, data, length, opaque);
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
int ImageStore::decode(const std::uint8_t *data, int length, int preferredSlot)
|
|
665
|
+
{
|
|
666
|
+
return decodeWithHint(data, length, preferredSlot, false);
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
int ImageStore::decodeOpaque(const std::uint8_t *data, int length, int preferredSlot)
|
|
670
|
+
{
|
|
671
|
+
return decodeWithHint(data, length, preferredSlot, true);
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
int ImageStore::width(int id) const { const ImageSlot *image = slot(id); return image ? image->width : 0; }
|
|
675
|
+
int ImageStore::height(int id) const { const ImageSlot *image = slot(id); return image ? image->height : 0; }
|
|
676
|
+
int ImageStore::frameCount(int id) const { const ImageSlot *image = slot(id); return image ? image->frameCount : 0; }
|
|
677
|
+
bool ImageStore::isAnimated(int id) const { const ImageSlot *image = slot(id); return image && image->frameCount > 1; }
|
|
678
|
+
ImageFormat ImageStore::format(int id) const { const ImageSlot *image = slot(id); return image ? image->format : ImageFormat::Unknown; }
|
|
679
|
+
bool ImageStore::playing(int id) const { const ImageSlot *image = slot(id); return image && image->playing; }
|
|
680
|
+
int ImageStore::currentFrame(int id) const { const ImageSlot *image = slot(id); return image ? image->currentFrame : 0; }
|
|
681
|
+
const pixel::native_t *ImageStore::currentPixels(int id) const { materializeDeferred(id); const ImageSlot *image = slot(id); return image ? image->pixels : nullptr; }
|
|
682
|
+
const std::uint8_t *ImageStore::currentAlpha(int id) const { materializeDeferred(id); const ImageSlot *image = slot(id); return image ? image->alpha : nullptr; }
|
|
683
|
+
|
|
684
|
+
void ImageStore::setPlaying(int id, bool playing)
|
|
685
|
+
{
|
|
686
|
+
ImageSlot *image = slot(id);
|
|
687
|
+
if (image) image->playing = playing;
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
void ImageStore::seek(int id, int frame)
|
|
691
|
+
{
|
|
692
|
+
ImageSlot *image = slot(id);
|
|
693
|
+
if (!image || !image->gifDecoder || image->frameCount <= 1) return;
|
|
694
|
+
if (frame < 0) frame = 0;
|
|
695
|
+
if (frame >= image->frameCount) frame = image->frameCount - 1;
|
|
696
|
+
|
|
697
|
+
auto *gif = static_cast<GIFIMAGE *>(image->gifDecoder);
|
|
698
|
+
GIF_reset(gif);
|
|
699
|
+
std::memset(image->pixels, 0, static_cast<std::size_t>(image->width) * image->height * sizeof(pixel::native_t));
|
|
700
|
+
for (int f = 0; f <= frame; f++) decodeNextGifFrame(*image);
|
|
701
|
+
image->currentFrame = frame;
|
|
702
|
+
image->elapsedMs = 0;
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
bool ImageStore::advance(int id, int deltaMs)
|
|
706
|
+
{
|
|
707
|
+
ImageSlot *image = slot(id);
|
|
708
|
+
if (!image || !image->playing || image->frameCount <= 1 || !image->gifDecoder) return false;
|
|
709
|
+
|
|
710
|
+
image->elapsedMs += deltaMs;
|
|
711
|
+
bool changed = false;
|
|
712
|
+
while (image->elapsedMs >= image->currentDelayMs && image->currentDelayMs > 0) {
|
|
713
|
+
image->elapsedMs -= image->currentDelayMs;
|
|
714
|
+
const int rc = decodeNextGifFrame(*image);
|
|
715
|
+
if (rc < 0) break;
|
|
716
|
+
image->currentFrame++;
|
|
717
|
+
if (rc == 0) image->currentFrame = 0;
|
|
718
|
+
changed = true;
|
|
719
|
+
}
|
|
720
|
+
return changed;
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
const pixel::native_t *ImageStore::framePixels(int id, int frame) const
|
|
724
|
+
{
|
|
725
|
+
materializeDeferred(id);
|
|
726
|
+
const ImageSlot *image = slot(id);
|
|
727
|
+
if (!image || !image->pixels) return nullptr;
|
|
728
|
+
if (image->frameCount > 1 && frame != image->currentFrame) return nullptr;
|
|
729
|
+
return image->pixels;
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
bool ImageStore::rotate90(int id)
|
|
733
|
+
{
|
|
734
|
+
ImageSlot *image = slot(id);
|
|
735
|
+
if (!image || !image->pixels || image->alpha) return false;
|
|
736
|
+
const int w = image->width;
|
|
737
|
+
const int h = image->height;
|
|
738
|
+
if (w != h || w <= 0) return false;
|
|
739
|
+
pixel::native_t *scratch = ImageMemory::allocate<pixel::native_t>(static_cast<std::size_t>(w) * h);
|
|
740
|
+
if (!scratch) return false;
|
|
741
|
+
// panel-local (px,py) <- landscape-local (lx,ly): px=ly, py=w-1-lx
|
|
742
|
+
// => rotated[py*w+px] = src[px*w + (w-1-py)]
|
|
743
|
+
for (int py = 0; py < h; py++) {
|
|
744
|
+
pixel::native_t *dst = scratch + static_cast<std::size_t>(py) * w;
|
|
745
|
+
const int sx = w - 1 - py;
|
|
746
|
+
for (int px = 0; px < w; px++) {
|
|
747
|
+
dst[px] = image->pixels[static_cast<std::size_t>(px) * w + sx];
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
std::memcpy(image->pixels, scratch, static_cast<std::size_t>(w) * h * sizeof(pixel::native_t));
|
|
751
|
+
ImageMemory::release(scratch);
|
|
752
|
+
return true;
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
void ImageStore::dispose(int id)
|
|
756
|
+
{
|
|
757
|
+
ImageStoreLock lock(slotMutex_);
|
|
758
|
+
disposeLocked(id);
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
void ImageStore::disposeLocked(int id)
|
|
762
|
+
{
|
|
763
|
+
if (id < 0 || id >= kImageMax) return;
|
|
764
|
+
ImageSlot *image = &images_[id];
|
|
765
|
+
|
|
766
|
+
if (image->gifDecoder) {
|
|
767
|
+
GIF_close(static_cast<GIFIMAGE *>(image->gifDecoder));
|
|
768
|
+
ImageMemory::release(image->gifDecoder);
|
|
769
|
+
}
|
|
770
|
+
ImageMemory::release(image->gifData);
|
|
771
|
+
if (image->ownsPixels) ImageMemory::release(image->pixels);
|
|
772
|
+
ImageMemory::release(image->alpha);
|
|
773
|
+
*image = ImageSlot{};
|
|
774
|
+
used_[id] = false;
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
void ImageStore::disposeAll()
|
|
778
|
+
{
|
|
779
|
+
ImageStoreLock lock(slotMutex_);
|
|
780
|
+
for (int i = 0; i < kImageMax; i++) {
|
|
781
|
+
if (used_[i]) disposeLocked(i);
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
int ImageStore::registerBuffer(pixel::native_t *pixels, int width, int height, int preferredSlot, bool takeOwnership)
|
|
786
|
+
{
|
|
787
|
+
if (!pixels || width <= 0 || height <= 0) return -1;
|
|
788
|
+
|
|
789
|
+
ImageStoreLock lock(slotMutex_);
|
|
790
|
+
const int id = findFreeSlot(preferredSlot);
|
|
791
|
+
if (id < 0) return -1;
|
|
792
|
+
if (used_[id]) disposeLocked(id);
|
|
793
|
+
|
|
794
|
+
ImageSlot &image = images_[id];
|
|
795
|
+
image = ImageSlot{};
|
|
796
|
+
image.width = width;
|
|
797
|
+
image.height = height;
|
|
798
|
+
image.frameCount = 1;
|
|
799
|
+
image.pixels = pixels;
|
|
800
|
+
image.ownsPixels = takeOwnership;
|
|
801
|
+
image.loopCount = 0;
|
|
802
|
+
image.playing = false;
|
|
803
|
+
used_[id] = true;
|
|
804
|
+
return id;
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
} // namespace gea::framework::graphics
|