roo_display
API Documentation for roo_display
Loading...
Searching...
No Matches
jpeg.h
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4
9#include "roo_io.h"
10#include "roo_io/core/multipass_input_stream.h"
11#include "roo_io/core/resource.h"
12#include "roo_io/fs/filesystem.h"
13#include "roo_logging.h"
14
15#ifdef ARDUINO
16#include "roo_io/fs/arduino/file_resource.h"
17#else
18#include "roo_io/fs/file_resource.h"
19#endif
20
21namespace roo_display {
22
23/// JPEG decoder (stateful, reusable).
25 public:
26 /// Construct a JPEG decoder instance.
28
29 private:
30 friend class JpegImage;
31 friend size_t jpeg_read(JDEC*, uint8_t*, size_t);
32 friend int jpeg_draw_rect(JDEC* jdec, void* data, JRECT* rect);
33
34 bool getDimensions(const roo_io::MultipassResource& resource, int16_t& width,
35 int16_t& height);
36
37 bool open(const roo_io::MultipassResource& resource, int16_t& width,
38 int16_t& height);
39
40 void draw(const roo_io::MultipassResource& resource, const Surface& s,
41 uint8_t scale, int16_t& width, int16_t& height);
42
43 void close() { input_ = nullptr; }
44
45 std::unique_ptr<uint8_t[]> workspace_;
46 JDEC jdec_;
47
48 std::unique_ptr<roo_io::MultipassInputStream> input_;
49 const Surface* surface_;
50};
51
52/// Drawable JPEG image backed by a multipass resource.
53class JpegImage : public Drawable {
54 public:
55 /// Create a JPEG image using a decoder and resource.
56 JpegImage(JpegDecoder& decoder, roo_io::MultipassResource& resource)
57 : decoder_(decoder), resource_(resource) {
58 decoder_.getDimensions(resource_, width_, height_);
59 }
60
61 Box extents() const override { return Box(0, 0, width_ - 1, height_ - 1); }
62
63 private:
64 void drawTo(const Surface& s) const override {
65 decoder_.draw(resource_, s, 0, width_, height_);
66 }
67
68 JpegDecoder& decoder_;
69
70 roo_io::MultipassResource& resource_;
71 mutable int16_t width_;
72 mutable int16_t height_;
73};
74
75#ifdef ARDUINO
76/// Drawable JPEG image backed by a file resource (Arduino).
77class JpegFile : public Drawable {
78 public:
79 /// Create a JPEG file drawable using an Arduino FS and path.
80 JpegFile(JpegDecoder& decoder, ::fs::FS& fs, String path)
81 : resource_(fs, path.c_str()), img_(decoder, resource_) {}
82
83 /// Create a JPEG file drawable using a roo_io filesystem and Arduino String.
84 JpegFile(JpegDecoder& decoder, roo_io::Filesystem& fs, String path)
85 : resource_(fs, path.c_str()), img_(decoder, resource_) {}
86
87 /// Create a JPEG file drawable using a roo_io filesystem and std::string.
88 JpegFile(JpegDecoder& decoder, roo_io::Filesystem& fs, std::string path)
89 : resource_(fs, std::move(path)), img_(decoder, resource_) {}
90
91 Box extents() const override { return img_.extents(); }
92
93 private:
94 void drawTo(const Surface& s) const override { s.drawObject(img_); }
95
96 roo_io::ExtendedArduinoFileResource resource_;
97 JpegImage img_;
98};
99#else
100/// Drawable JPEG image backed by a file resource (non-Arduino).
101class JpegFile : public Drawable {
102 public:
103 /// Create a JPEG file drawable using a roo_io filesystem and path.
104 JpegFile(JpegDecoder& decoder, roo_io::Filesystem& fs, std::string path)
105 : resource_(fs, std::move(path)), img_(decoder, resource_) {}
106
107 Box extents() const override { return img_.extents(); }
108
109 private:
110 void drawTo(const Surface& s) const override { s.drawObject(img_); }
111
112 roo_io::FileResource resource_;
113 JpegImage img_;
114};
115#endif
116
117} // namespace roo_display
Axis-aligned integer rectangle.
Definition box.h:12
Interface for objects that can be drawn to an output device.
Definition drawable.h:229
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
Drawable JPEG image backed by a file resource (non-Arduino).
Definition jpeg.h:101
JpegFile(JpegDecoder &decoder, roo_io::Filesystem &fs, std::string path)
Create a JPEG file drawable using a roo_io filesystem and path.
Definition jpeg.h:104
Box extents() const override
Return the bounding box encompassing all pixels that need to be drawn.
Definition jpeg.h:107
Drawable JPEG image backed by a multipass resource.
Definition jpeg.h:53
JpegImage(JpegDecoder &decoder, roo_io::MultipassResource &resource)
Create a JPEG image using a decoder and resource.
Definition jpeg.h:56
Box extents() const override
Return the bounding box encompassing all pixels that need to be drawn.
Definition jpeg.h:61
Low-level handle used to draw to an underlying device.
Definition drawable.h:60
Defines 140 opaque HTML named colors.
Definition tjpgd.h:58
Definition tjpgd.h:47