FastLED 3.9.15
Loading...
Searching...
No Matches
jpeg.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/codec/common.h" // IWYU pragma: keep
4#include "fl/stl/function.h"
5#include "fl/stl/noexcept.h"
6
7namespace fl {
8
9// Forward declarations
10class JpegDecoder;
12
13// JPEG metadata information structure
14struct JpegInfo {
15 fl::u16 width = 0;
16 fl::u16 height = 0;
19 bool is_grayscale = false;
20 bool is_valid = false;
21
22 JpegInfo() FL_NOEXCEPT = default;
23 JpegInfo(fl::u16 w, fl::u16 h, fl::u8 comp);
24};
25
26// JPEG decoder configuration
36
37// Progressive processing configuration
39 fl::u16 max_mcus_per_tick = 2;
41 fl::size input_buffer_size = 512;
43
45};
46
47// JPEG decoder with progressive processing support
48class JpegDecoder : public IDecoder {
49public:
57
58
59 using YieldFunction = fl::function<bool()>;
60 static bool NeverYeildUntilDone() {
61 return false; // never signal to yield
62 }
63
64 explicit JpegDecoder(const JpegConfig& config);
66
67 // IDecoder interface
68 bool begin(fl::filebuf_ptr stream) override;
69 void end() override;
70 bool isReady() const override;
71 bool hasError(fl::string* msg = nullptr) const override;
72 DecodeResult decode() override;
73 // Partial, incremental decoding is supported. This will
74 // process until the jpeg is decoded or should_yield returns true.
75 DecodeResult decode(fl::optional<fl::function<bool()>> should_yield); // Decode with optional yield callback
76 Frame getCurrentFrame() override;
77 bool hasMoreFrames() const override;
78
79 // Progressive configuration
80 void setProgressiveConfig(const ProgressiveConfig& config);
82 float getProgress() const; // 0.0 to 1.0 completion
83
84 // Incremental output access
85 bool hasPartialImage() const;
87 fl::u16 getDecodedRows() const;
88
89 // Stream interface extensions
90 bool feedData(fl::span<const fl::u8> data);
91 bool needsMoreData() const;
92 fl::size getBytesProcessed() const;
93
94 // State access
95 State getState() const;
96
97private:
98 class Impl;
100};
101
102// Main JPEG codec interface
103class Jpeg {
104public:
105 // Synchronous decode interface
106 static bool decode(const JpegConfig& config, fl::span<const fl::u8> data,
107 Frame* frame, fl::string* error_message = nullptr);
108 static FramePtr decode(const JpegConfig& config, fl::span<const fl::u8> data,
109 fl::string* error_message = nullptr);
110
111 // Simplified decode with defaults
112 static FramePtr decode(fl::span<const fl::u8> data, fl::string* error_message = nullptr);
113
114 // Progressive decoder factory
115 static JpegDecoderPtr createDecoder(const JpegConfig& config);
116
117 // Utility methods
118 static bool decodeWithTimeout(const JpegConfig& config, fl::span<const fl::u8> data,
119 Frame* frame, fl::u32 timeout_ms,
120 float* progress_out = nullptr,
121 fl::string* error_message = nullptr);
122
123 static bool decodeStream(const JpegConfig& config, fl::filebuf_ptr input_stream,
124 Frame* frame, fl::u32 max_time_per_chunk_ms = 4,
125 fl::function<bool(float)> progress_callback = {});
126
127 // Metadata parsing
128 static JpegInfo parseInfo(fl::span<const fl::u8> data, fl::string* error_message = nullptr);
129
130 static bool isSupported();
131};
132
133} // namespace fl
static bool decode(const JpegConfig &config, fl::span< const fl::u8 > data, Frame *frame, fl::string *error_message=nullptr)
Definition jpeg.cpp.hpp:296
static JpegDecoderPtr createDecoder(const JpegConfig &config)
Definition jpeg.cpp.hpp:372
static bool isSupported()
Definition jpeg.cpp.hpp:376
static bool decodeWithTimeout(const JpegConfig &config, fl::span< const fl::u8 > data, Frame *frame, fl::u32 timeout_ms, float *progress_out=nullptr, fl::string *error_message=nullptr)
Definition jpeg.cpp.hpp:380
static bool decodeStream(const JpegConfig &config, fl::filebuf_ptr input_stream, Frame *frame, fl::u32 max_time_per_chunk_ms=4, fl::function< bool(float)> progress_callback={})
Definition jpeg.cpp.hpp:443
static JpegInfo parseInfo(fl::span< const fl::u8 > data, fl::string *error_message=nullptr)
Definition jpeg.cpp.hpp:488
fl::unique_ptr< Impl > mImpl
Definition jpeg.h:99
bool hasPartialImage() const
Definition jpeg.cpp.hpp:264
bool begin(fl::filebuf_ptr stream) override
Definition jpeg.cpp.hpp:220
void end() override
Definition jpeg.cpp.hpp:224
static bool NeverYeildUntilDone()
Definition jpeg.h:60
~JpegDecoder() FL_NOEXCEPT override
JpegDecoder(const JpegConfig &config)
Definition jpeg.cpp.hpp:215
bool needsMoreData() const
Definition jpeg.cpp.hpp:280
bool hasMoreFrames() const override
Definition jpeg.cpp.hpp:248
void setProgressiveConfig(const ProgressiveConfig &config)
Definition jpeg.cpp.hpp:252
Frame getPartialFrame()
Definition jpeg.cpp.hpp:268
ProgressiveConfig getProgressiveConfig() const
Definition jpeg.cpp.hpp:256
bool feedData(fl::span< const fl::u8 > data)
Definition jpeg.cpp.hpp:276
DecodeResult decode() override
Definition jpeg.cpp.hpp:236
bool hasError(fl::string *msg=nullptr) const override
Definition jpeg.cpp.hpp:232
Frame getCurrentFrame() override
Definition jpeg.cpp.hpp:244
float getProgress() const
Definition jpeg.cpp.hpp:260
bool isReady() const override
Definition jpeg.cpp.hpp:228
fl::u16 getDecodedRows() const
Definition jpeg.cpp.hpp:272
State getState() const
Definition jpeg.cpp.hpp:288
fl::function< bool()> YieldFunction
Definition jpeg.h:59
fl::size getBytesProcessed() const
Definition jpeg.cpp.hpp:284
unsigned char u8
Definition s16x16x4.h:132
unsigned char u8
Definition stdint.h:131
Optional< T > optional
Definition optional.h:16
fl::shared_ptr< filebuf > filebuf_ptr
Definition idecoder.h:15
DecodeResult
Definition idecoder.h:18
fl::shared_ptr< JpegDecoder > JpegDecoderPtr
Definition jpeg.h:11
PixelFormat
Definition pixel.h:7
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT
PixelFormat format
Definition jpeg.h:31
JpegConfig() FL_NOEXCEPT=default
Quality quality
Definition jpeg.h:30
fl::u8 bits_per_component
Definition jpeg.h:18
fl::u16 width
Definition jpeg.h:15
bool is_valid
Definition jpeg.h:20
JpegInfo() FL_NOEXCEPT=default
fl::u16 height
Definition jpeg.h:16
fl::u8 components
Definition jpeg.h:17
bool is_grayscale
Definition jpeg.h:19
ProgressiveConfig() FL_NOEXCEPT=default
fl::u16 max_mcus_per_tick
Definition jpeg.h:39
bool yield_on_row_complete
Definition jpeg.h:42
fl::size input_buffer_size
Definition jpeg.h:41
fl::u32 max_time_per_tick_ms
Definition jpeg.h:40