FastLED 3.9.15
Loading...
Searching...
No Matches
gif.cpp.hpp
Go to the documentation of this file.
1#include "fl/codec/gif.h"
2// IWYU pragma: begin_keep
4// IWYU pragma: end_keep
6
7namespace fl {
8
9IDecoderPtr Gif::createDecoder(const GifConfig& config, fl::string* error_message) {
10 // Create the software GIF decoder
12
13 if (!decoder) {
14 if (error_message) {
15 *error_message = "Failed to allocate GIF decoder";
16 }
17 return nullptr;
18 }
19
20 return decoder;
21}
22
24 // libnsgif is always available since it's included directly
25 return true;
26}
27
29 GifInfo info;
30
31 // Validate input data
32 if (data.empty()) {
33 if (error_message) {
34 *error_message = "Empty GIF data";
35 }
36 return info; // returns invalid info
37 }
38
39 // Minimum size check - GIF must have at least the header (6 bytes) + some content
40 if (data.size() < 13) { // GIF header (6) + screen descriptor (7)
41 if (error_message) {
42 *error_message = "GIF data too small";
43 }
44 return info;
45 }
46
47 // Check GIF signature
48 if (data[0] != 'G' || data[1] != 'I' || data[2] != 'F') {
49 if (error_message) {
50 *error_message = "Invalid GIF signature";
51 }
52 return info;
53 }
54
55 // Check version (87a or 89a)
56 if (!((data[3] == '8' && data[4] == '7' && data[5] == 'a') ||
57 (data[3] == '8' && data[4] == '9' && data[5] == 'a'))) {
58 if (error_message) {
59 *error_message = "Unsupported GIF version";
60 }
61 return info;
62 }
63
64 // Parse logical screen descriptor (starts at byte 6)
65 // Width (2 bytes, little endian) at offset 6-7
66 // Height (2 bytes, little endian) at offset 8-9
67 fl::u16 width = data[6] | (data[7] << 8);
68 fl::u16 height = data[8] | (data[9] << 8);
69
70 if (width == 0 || height == 0) {
71 if (error_message) {
72 *error_message = "Invalid GIF dimensions";
73 }
74 return info;
75 }
76
77 // For a more complete parsing, we would need to create a temporary decoder
78 // and let libnsgif parse the structure. Let's use the existing decoder briefly.
80 auto stream = fl::make_shared<fl::memorybuf>(data.size());
81 stream->write(data);
82
83 if (decoder->begin(stream)) {
84 // Now we can extract more detailed info using the decoder's methods
85 info.width = decoder->getWidth();
86 info.height = decoder->getHeight();
87 info.frameCount = decoder->getFrameCount();
88 info.loopCount = decoder->getLoopCount();
89 info.isAnimated = decoder->isAnimated();
90 info.bitsPerPixel = 8; // GIF is always 8 bits per pixel
91 info.isValid = true;
92
93 decoder->end();
94 } else {
95 // Fallback to basic header parsing if decoder fails
96 info.width = width;
97 info.height = height;
98 info.frameCount = 1; // Assume static image
99 info.loopCount = 0;
100 info.isAnimated = false;
101 info.bitsPerPixel = 8;
102 info.isValid = true;
103
104 if (error_message) {
105 *error_message = ""; // Clear any error message for fallback case
106 }
107 }
108
109 return info;
110}
111
112} // namespace fl
static GifInfo parseGifInfo(fl::span< const fl::u8 > data, fl::string *error_message=nullptr)
Definition gif.cpp.hpp:28
static IDecoderPtr createDecoder(const GifConfig &config, fl::string *error_message=nullptr)
Definition gif.cpp.hpp:9
static bool isSupported()
Definition gif.cpp.hpp:23
constexpr bool empty() const FL_NOEXCEPT
Definition span.h:510
constexpr fl::size size() const FL_NOEXCEPT
Definition span.h:458
u8 u8 height
Definition blur.h:186
u8 width
Definition blur.h:186
shared_ptr< T > make_shared(Args &&... args) FL_NOEXCEPT
Definition shared_ptr.h:414
Base definition for an LED controller.
Definition crgb.hpp:179
PixelFormat format
Definition gif.h:33
fl::u16 height
Definition gif.h:13
fl::u32 loopCount
Definition gif.h:15
bool isValid
Definition gif.h:18
bool isAnimated
Definition gif.h:17
fl::u8 bitsPerPixel
Definition gif.h:16
fl::u32 frameCount
Definition gif.h:14
fl::u16 width
Definition gif.h:12