FastLED 3.10.8
Loading...
Searching...
No Matches
frame.cpp.hpp
Go to the documentation of this file.
1
2#include "crgb.h"
3#include "fl/stl/allocator.h"
4#include "fl/stl/bit_cast.h"
5#include "fl/log/log.h"
6#include "fl/log/log.h"
7#include "fl/math/xymap.h"
8#include "fl/fx/frame.h"
9
10#include "fl/stl/cstring.h"
11#include "fl/stl/noexcept.h"
12namespace fl {
13
14Frame::Frame(int pixels_count) FL_NO_EXCEPT : mPixelsCount(pixels_count), mRgb(), mIsFromCodec(false) {
15 mRgb.resize(pixels_count);
16 if (pixels_count > 0) {
17 fl::memset((u8*)mRgb.data(), 0, pixels_count * sizeof(CRGB));
18 }
19}
20
21Frame::Frame(fl::u8* pixels, fl::u16 width, fl::u16 height, PixelFormat format, fl::u32 timestamp)
22 FL_NO_EXCEPT : mPixelsCount(static_cast<size_t>(width) * height), mRgb(),
24
25 mRgb.resize(mPixelsCount);
26
27 if (pixels && width > 0 && height > 0) {
29 } else if (mPixelsCount > 0) {
30 fl::memset((u8*)mRgb.data(), 0, mPixelsCount * sizeof(CRGB));
31 }
32}
33
35 : mPixelsCount(other.mPixelsCount), mRgb(),
36 mWidth(other.mWidth), mHeight(other.mHeight), mFormat(other.mFormat),
37 mTimestamp(other.mTimestamp), mIsFromCodec(other.mIsFromCodec) {
38
39 mRgb.resize(mPixelsCount);
40 if (!other.mRgb.empty()) {
41 fl::memcpy(mRgb.data(), other.mRgb.data(), mPixelsCount * sizeof(CRGB));
42 }
43}
44
46 // Vector will handle memory cleanup automatically
47}
48
50 if (!mRgb.empty()) {
51 switch (draw_mode) {
52 case DrawMode::DRAW_MODE_BLEND: // DRAW_MODE_BLEND is for gfx primitives; treat as overwrite for Frame
54 fl::memcpy(leds.data(), mRgb.data(), mPixelsCount * sizeof(CRGB));
55 break;
56 }
58 for (size_t i = 0; i < mPixelsCount; ++i) {
60 }
61 break;
62 }
63 }
64 }
65}
66
68 const u16 width = xyMap.getWidth();
69 const u16 height = xyMap.getHeight();
70 fl::u32 count = 0;
71 for (u16 h = 0; h < height; ++h) {
72 for (u16 w = 0; w < width; ++w) {
73 fl::u32 in_idx = xyMap(w, h);
74 fl::u32 out_idx = count++;
75 if (in_idx >= mPixelsCount) {
76 FL_WARN("Frame::drawXY: in index out of range: " << in_idx);
77 continue;
78 }
79 if (out_idx >= mPixelsCount) {
80 FL_WARN("Frame::drawXY: out index out of range: " << out_idx);
81 continue;
82 }
83 switch (draw_mode) {
84 case DrawMode::DRAW_MODE_BLEND: // gfx primitives only; treat as overwrite for Frame
86 leds[out_idx] = mRgb[in_idx];
87 break;
88 }
90 leds[out_idx] =
91 CRGB::blendAlphaMaxChannel(mRgb[in_idx], leds[in_idx]);
92 break;
93 }
94 }
95 }
96 }
97}
98
100 if (mPixelsCount > 0 && !mRgb.empty()) {
101 fl::memset((u8*)mRgb.data(), 0, mPixelsCount * sizeof(CRGB));
102 }
103}
104
105void Frame::interpolate(const Frame &frame1, const Frame &frame2,
106 u8 amountofFrame2, fl::span<CRGB> pixels) FL_NO_EXCEPT {
107 if (frame1.size() != frame2.size()) {
108 return; // Frames must have the same size
109 }
110
111 const CRGB *rgbFirst = frame1.rgb().data();
112 const CRGB *rgbSecond = frame2.rgb().data();
113
114 if (frame1.mRgb.empty() || frame2.mRgb.empty()) {
115 // Error, why are we getting null pointers?
116 return;
117 }
118
119 for (size_t i = 0; i < frame2.size(); ++i) {
120 pixels[i] = CRGB::blend(rgbFirst[i], rgbSecond[i], amountofFrame2);
121 }
122 // We will eventually do something with alpha.
123}
124
125void Frame::interpolate(const Frame &frame1, const Frame &frame2,
126 u8 amountOfFrame2) FL_NO_EXCEPT {
127 if (frame1.size() != frame2.size() || frame1.size() != mPixelsCount) {
128 FL_DBG("Frames must have the same size");
129 return; // Frames must have the same size
130 }
131 interpolate(frame1, frame2, amountOfFrame2, rgb());
132}
133
135 if (mIsFromCodec) {
136 return mWidth > 0 && mHeight > 0 && !mRgb.empty();
137 }
138 return !mRgb.empty();
139}
140
142 CRGB* rgbData = mRgb.data();
143
144 switch (format) {
145 case PixelFormat::RGB888: {
146 for (size_t i = 0; i < mPixelsCount; i++) {
147 fl::u8* pixel = &pixels[i * 3];
148 rgbData[i] = CRGB(pixel[0], pixel[1], pixel[2]);
149 }
150 break;
151 }
152 case PixelFormat::RGB565: {
153 fl::u16* pixel565 = fl::bit_cast<fl::u16*>(pixels);
154 for (size_t i = 0; i < mPixelsCount; i++) {
155 fl::u8 r, g, b;
156 rgb565ToRgb888(pixel565[i], r, g, b);
157 rgbData[i] = CRGB(r, g, b);
158 }
159 break;
160 }
162 for (size_t i = 0; i < mPixelsCount; i++) {
163 fl::u8* pixel = &pixels[i * 4];
164 rgbData[i] = CRGB(pixel[0], pixel[1], pixel[2]);
165 // Ignoring alpha channel for now
166 }
167 break;
168 }
169 case PixelFormat::YUV420: {
170 // Basic YUV420 to RGB conversion - simplified implementation
171 // For now, just use the Y (luminance) component as grayscale
172 for (size_t i = 0; i < mPixelsCount; i++) {
173 fl::u8 y = pixels[i];
174 rgbData[i] = CRGB(y, y, y);
175 }
176 break;
177 }
178 default: {
179 // Fallback: fill with black
180 if (mPixelsCount > 0 && !mRgb.empty()) {
181 fl::memset((u8*)rgbData, 0, mPixelsCount * sizeof(CRGB));
182 }
183 break;
184 }
185 }
186}
187
188
189} // namespace fl
CRGB leds[1]
fl::XYMap xyMap
Frame(int pixels_per_frame) FL_NO_EXCEPT
Definition frame.cpp.hpp:14
PixelFormat mFormat
Definition frame.h:70
void interpolate(const Frame &frame1, const Frame &frame2, u8 amountOfFrame2) FL_NO_EXCEPT
bool isValid() const FL_NO_EXCEPT
fl::u16 mHeight
Definition frame.h:69
fl::vector_psram< CRGB > mRgb
Definition frame.h:65
~Frame() FL_NO_EXCEPT
Definition frame.cpp.hpp:45
void convertPixelsToRgb(fl::u8 *pixels, PixelFormat format) FL_NO_EXCEPT
void drawXY(fl::span< CRGB > leds, const XYMap &xyMap, DrawMode draw_mode=DrawMode::DRAW_MODE_OVERWRITE) const FL_NO_EXCEPT
Definition frame.cpp.hpp:67
void clear() FL_NO_EXCEPT
Definition frame.cpp.hpp:99
bool mIsFromCodec
Definition frame.h:72
const size_t mPixelsCount
Definition frame.h:64
fl::u32 mTimestamp
Definition frame.h:71
fl::u16 mWidth
Definition frame.h:68
void draw(fl::span< CRGB > leds, DrawMode draw_mode=DrawMode::DRAW_MODE_OVERWRITE) const FL_NO_EXCEPT
Definition frame.cpp.hpp:49
fl::span< CRGB > rgb() FL_NO_EXCEPT
Definition frame.h:42
#define FL_WARN(...)
Definition log.h:437
#define FL_DBG
Definition log.h:553
Centralized logging categories for FastLED hardware interfaces and subsystems.
unsigned char u8
Definition stdint.h:131
fl::CRGB CRGB
Definition video.h:18
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
u8 u8 height
Definition blur.h:186
fl::string format(const char *fmt) FL_NO_EXCEPT
Format with no arguments.
Definition format.h:439
FASTLED_FORCE_INLINE void * memset(void *s, int c, size_t n) FL_NO_EXCEPT
Definition cstring.h:148
FASTLED_FORCE_INLINE void * memcpy(void *dest, const void *src, size_t n) FL_NO_EXCEPT
Definition cstring.h:136
void rgb565ToRgb888(fl::u16 rgb565, fl::u8 &r, fl::u8 &g, fl::u8 &b) FL_NO_EXCEPT
Definition pixel.cpp.hpp:24
u8 width
Definition blur.h:186
DrawMode
Definition draw_mode.h:5
@ DRAW_MODE_OVERWRITE
Definition draw_mode.h:5
@ DRAW_MODE_BLEND_BY_MAX_BRIGHTNESS
Definition draw_mode.h:5
@ DRAW_MODE_BLEND
Definition draw_mode.h:5
To bit_cast(const From &from) FL_NO_EXCEPT
Definition bit_cast.h:48
InputGamut g
Definition rgbw.h:125
PixelFormat
Definition pixel.h:8
Base definition for an LED controller.
Definition crgb.hpp:179
static CRGB blendAlphaMaxChannel(const CRGB &upper, const CRGB &lower) FL_NO_EXCEPT
Definition crgb.cpp.hpp:60
static CRGB blend(const CRGB &p1, const CRGB &p2, fract8 amountOfP2) FL_NO_EXCEPT
Definition crgb.cpp.hpp:55
Representation of an 8-bit RGB pixel (Red, Green, Blue)
Definition crgb.h:38