roo_display
API Documentation for roo_display
Loading...
Searching...
No Matches
png.h
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4
5#include "roo_display.h"
10#include "roo_io/core/multipass_input_stream.h"
11
12#ifdef ARDUINO
13#include "roo_io/fs/arduino/file_resource.h"
14#else
15#include "roo_io/fs/file_resource.h"
16#endif
17
18namespace roo_display {
19
20/// PNG decoder (stateful, reusable).
22 public:
23 /// Construct a PNG decoder instance.
24 PngDecoder();
25
26 private:
27 friend class PngImage;
30
31 bool getDimensions(const roo_io::MultipassResource& resource, int16_t& width,
32 int16_t& height);
33
34 bool open(const roo_io::MultipassResource& resource, int16_t& width,
35 int16_t& height);
36
37 void draw(const roo_io::MultipassResource& resource, const Surface& s,
38 uint8_t scale, int16_t& width, int16_t& height);
39
40 void close() { input_ = nullptr; }
41
42 std::unique_ptr<PNGIMAGE> pngdec_;
43
44 std::unique_ptr<roo_io::MultipassInputStream> input_;
45
46 // Used for indexed color modes.
47 Palette palette_;
48};
49
50/// Drawable PNG image backed by a multipass resource.
51class PngImage : public Drawable {
52 public:
53 /// Create a PNG image using a decoder and resource.
54 PngImage(PngDecoder& decoder, roo_io::MultipassResource& resource)
55 : decoder_(decoder), resource_(resource) {
56 decoder_.getDimensions(resource_, width_, height_);
57 }
58
59 Box extents() const override { return Box(0, 0, width_ - 1, height_ - 1); }
60
61 private:
62 void drawTo(const Surface& s) const override {
63 // We update the width and height during drawing, so that the file does not
64 // need to be re-read just to fetch the dimensions.
65 decoder_.draw(resource_, s, 0, width_, height_);
66 }
67
68 PngDecoder& decoder_;
69
70 roo_io::MultipassResource& resource_;
71 mutable int16_t width_;
72 mutable int16_t height_;
73};
74
75#ifdef ARDUINO
76/// Drawable PNG image backed by a file resource (Arduino).
77class PngFile : public Drawable {
78 public:
79 /// Create a PNG file drawable using an Arduino FS and path.
80 PngFile(PngDecoder& decoder, ::fs::FS& fs, String path)
81 : resource_(fs, path.c_str()), img_(decoder, resource_) {}
82
83 /// Create a PNG file drawable using a roo_io filesystem and Arduino String.
84 PngFile(PngDecoder& decoder, roo_io::Filesystem& fs, String path)
85 : resource_(fs, path.c_str()), img_(decoder, resource_) {}
86
87 /// Create a PNG file drawable using a roo_io filesystem and std::string.
88 PngFile(PngDecoder& decoder, roo_io::Filesystem& fs, std::string path)
89 : resource_(fs, path.c_str()), 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 PngImage img_;
98};
99#else
100/// Drawable PNG image backed by a file resource (non-Arduino).
101class PngFile : public Drawable {
102 public:
103 /// Create a PNG file drawable using a roo_io filesystem and path.
104 PngFile(PngDecoder& 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 PngImage 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
Palette storage for IndexedN color modes.
PNG decoder (stateful, reusable).
Definition png.h:21
PngDecoder()
Construct a PNG decoder instance.
Definition png.cpp:104
friend int32_t png_seek(PNGFILE *pFile, int32_t iPosition)
Definition png.cpp:19
friend int32_t png_read(PNGFILE *pFile, uint8_t *pBuf, int32_t iLen)
Definition png.cpp:14
Drawable PNG image backed by a file resource (non-Arduino).
Definition png.h:101
Box extents() const override
Return the bounding box encompassing all pixels that need to be drawn.
Definition png.h:107
PngFile(PngDecoder &decoder, roo_io::Filesystem &fs, std::string path)
Create a PNG file drawable using a roo_io filesystem and path.
Definition png.h:104
Drawable PNG image backed by a multipass resource.
Definition png.h:51
PngImage(PngDecoder &decoder, roo_io::MultipassResource &resource)
Create a PNG image using a decoder and resource.
Definition png.h:54
Box extents() const override
Return the bounding box encompassing all pixels that need to be drawn.
Definition png.h:59
Low-level handle used to draw to an underlying device.
Definition drawable.h:60
Defines 140 opaque HTML named colors.
Public API surface for roo_display display, touch, and drawing utilities.