roo_display
API Documentation for roo_display
Loading...
Searching...
No Matches
jpeg.cpp
Go to the documentation of this file.
2
3#include "roo_backport.h"
4#include "roo_backport/byte.h"
5#include "roo_display.h"
7
8namespace roo_display {
9
10// Per http://elm-chan.org/fsw/tjpgd/en/appnote.html: 3100 is the minimum;
11// 'FASTDECODE = 1' needs extra 320 bytes; 'FASTDECODE = 2' needs extra 6144
12// bytes.
13#if JD_FASTDECODE == 0
14#define TJPGD_WORKSPACE_SIZE 3100
15#elif JD_FASTDECODE == 1
16#define TJPGD_WORKSPACE_SIZE 3500
17#elif JD_FASTDECODE == 2
18#define TJPGD_WORKSPACE_SIZE (3500 + 6144)
19#endif
20
22 : workspace_(new uint8_t[TJPGD_WORKSPACE_SIZE]),
23 jdec_(),
24 input_(nullptr),
25 surface_(nullptr) {}
26
27size_t jpeg_read(JDEC* jdec, uint8_t* buf, size_t size) {
29 if (buf != nullptr) {
30 return decoder->input_->read((roo::byte*)buf, size);
31 } else {
32 decoder->input_->skip(size);
33 return size;
34 }
35}
36
37int jpeg_draw_rect(JDEC* jdec, void* data, JRECT* rect) {
39 const Surface* surface = decoder->surface_;
40 if (rect->top + surface->dy() > surface->clip_box().yMax()) {
41 // The rest of the image will be clipped out; we can finish early.
42 return 0;
43 }
44 Box box(rect->left, rect->top, rect->right, rect->bottom);
45 ConstDramRaster<Rgb888> raster(box, (const roo::byte*)data);
46 {
48 surface->drawObject(raster);
49 }
50 return 1;
51}
52
53bool JpegDecoder::getDimensions(const roo_io::MultipassResource& resource,
54 int16_t& width, int16_t& height) {
55 if (!open(resource, width, height)) {
56 width = 0;
57 height = 0;
58 return false;
59 }
60 close();
61 return true;
62}
63
64bool JpegDecoder::open(const roo_io::MultipassResource& resource,
65 int16_t& width, int16_t& height) {
66 input_ = resource.open();
67 if (jd_prepare(&jdec_, &jpeg_read, workspace_.get(), TJPGD_WORKSPACE_SIZE,
68 this) == JDR_OK) {
69 width = jdec_.width;
70 height = jdec_.height;
71 return true;
72 }
73 close();
74 return false;
75}
76
77void JpegDecoder::draw(const roo_io::MultipassResource& resource,
78 const Surface& s, uint8_t scale, int16_t& width,
79 int16_t& height) {
80 PauseOutput pause(s.out());
81 if (!open(resource, width, height)) {
82 return;
83 }
84
85 surface_ = &s;
86 jd_decomp(&jdec_, &jpeg_draw_rect, scale);
87 surface_ = nullptr;
88
89 close();
90}
91
92} // namespace roo_display
Axis-aligned integer rectangle.
Definition box.h:12
int16_t yMax() const
Maximum y (inclusive).
Definition box.h:74
JPEG decoder (stateful, reusable).
Definition jpeg.h:24
friend int jpeg_draw_rect(JDEC *jdec, void *data, JRECT *rect)
Definition jpeg.cpp:37
friend size_t jpeg_read(JDEC *, uint8_t *, size_t)
Definition jpeg.cpp:27
JpegDecoder()
Construct a JPEG decoder instance.
Definition jpeg.cpp:21
Resumes a paused output transaction.
Low-level handle used to draw to an underlying device.
Definition drawable.h:60
int16_t dy() const
Return the y offset to apply to drawn objects.
Definition drawable.h:128
const Box & clip_box() const
Return the clip box in device coordinates (independent of offsets).
Definition drawable.h:134
void drawObject(const Drawable &object) const
Draw a drawable object to this surface.
Definition drawable.h:278
DisplayOutput & out() const
Return the device output.
Definition drawable.h:119
#define TJPGD_WORKSPACE_SIZE
Definition jpeg.cpp:14
Defines 140 opaque HTML named colors.
int jpeg_draw_rect(JDEC *jdec, void *data, JRECT *rect)
Definition jpeg.cpp:37
size_t jpeg_read(JDEC *jdec, uint8_t *buf, size_t size)
Definition jpeg.cpp:27
const Surface * surface
Definition png.cpp:29
Public API surface for roo_display display, touch, and drawing utilities.
Definition tjpgd.h:58
uint16_t width
Definition tjpgd.h:69
uint16_t height
Definition tjpgd.h:69
Definition tjpgd.h:47
JRESULT jd_prepare(JDEC *jd, size_t(*infunc)(JDEC *, uint8_t *, size_t), void *pool, size_t sz_pool, void *dev)
Definition tjpgd.c:962
JRESULT jd_decomp(JDEC *jd, int(*outfunc)(JDEC *, void *, JRECT *), uint8_t scale)
Definition tjpgd.c:1118
@ JDR_OK
Definition tjpgd.h:33