FastLED 3.9.15
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) : 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 : 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
34Frame::Frame(const Frame& other)
35 : mPixelsCount(other.mPixelsCount), mRgb(),
36 mWidth(other.mWidth), mHeight(other.mHeight), mFormat(other.mFormat),
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
49void Frame::draw(fl::span<CRGB> leds, DrawMode draw_mode) const {
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
67void Frame::drawXY(fl::span<CRGB> leds, const XYMap &xyMap, DrawMode draw_mode) const {
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(
77 "Frame::drawXY: in index out of range: " << in_idx);
78 continue;
79 }
80 if (out_idx >= mPixelsCount) {
81 FL_WARN(
82 "Frame::drawXY: out index out of range: " << out_idx);
83 continue;
84 }
85 switch (draw_mode) {
86 case DrawMode::DRAW_MODE_BLEND: // gfx primitives only; treat as overwrite for Frame
88 leds[out_idx] = mRgb[in_idx];
89 break;
90 }
92 leds[out_idx] =
93 CRGB::blendAlphaMaxChannel(mRgb[in_idx], leds[in_idx]);
94 break;
95 }
96 }
97 }
98 }
99}
100
102 if (mPixelsCount > 0 && !mRgb.empty()) {
103 fl::memset((u8*)mRgb.data(), 0, mPixelsCount * sizeof(CRGB));
104 }
105}
106
107void Frame::interpolate(const Frame &frame1, const Frame &frame2,
108 u8 amountofFrame2, fl::span<CRGB> pixels) {
109 if (frame1.size() != frame2.size()) {
110 return; // Frames must have the same size
111 }
112
113 const CRGB *rgbFirst = frame1.rgb().data();
114 const CRGB *rgbSecond = frame2.rgb().data();
115
116 if (frame1.mRgb.empty() || frame2.mRgb.empty()) {
117 // Error, why are we getting null pointers?
118 return;
119 }
120
121 for (size_t i = 0; i < frame2.size(); ++i) {
122 pixels[i] = CRGB::blend(rgbFirst[i], rgbSecond[i], amountofFrame2);
123 }
124 // We will eventually do something with alpha.
125}
126
127void Frame::interpolate(const Frame &frame1, const Frame &frame2,
128 u8 amountOfFrame2) {
129 if (frame1.size() != frame2.size() || frame1.size() != mPixelsCount) {
130 FL_DBG("Frames must have the same size");
131 return; // Frames must have the same size
132 }
133 interpolate(frame1, frame2, amountOfFrame2, rgb());
134}
135
136bool Frame::isValid() const {
137 if (mIsFromCodec) {
138 return mWidth > 0 && mHeight > 0 && !mRgb.empty();
139 }
140 return !mRgb.empty();
141}
142
144 CRGB* rgbData = mRgb.data();
145
146 switch (format) {
147 case PixelFormat::RGB888: {
148 for (size_t i = 0; i < mPixelsCount; i++) {
149 fl::u8* pixel = &pixels[i * 3];
150 rgbData[i] = CRGB(pixel[0], pixel[1], pixel[2]);
151 }
152 break;
153 }
154 case PixelFormat::RGB565: {
155 fl::u16* pixel565 = fl::bit_cast<fl::u16*>(pixels);
156 for (size_t i = 0; i < mPixelsCount; i++) {
157 fl::u8 r, g, b;
158 rgb565ToRgb888(pixel565[i], r, g, b);
159 rgbData[i] = CRGB(r, g, b);
160 }
161 break;
162 }
164 for (size_t i = 0; i < mPixelsCount; i++) {
165 fl::u8* pixel = &pixels[i * 4];
166 rgbData[i] = CRGB(pixel[0], pixel[1], pixel[2]);
167 // Ignoring alpha channel for now
168 }
169 break;
170 }
171 case PixelFormat::YUV420: {
172 // Basic YUV420 to RGB conversion - simplified implementation
173 // For now, just use the Y (luminance) component as grayscale
174 for (size_t i = 0; i < mPixelsCount; i++) {
175 fl::u8 y = pixels[i];
176 rgbData[i] = CRGB(y, y, y);
177 }
178 break;
179 }
180 default: {
181 // Fallback: fill with black
182 if (mPixelsCount > 0 && !mRgb.empty()) {
183 fl::memset((u8*)rgbData, 0, mPixelsCount * sizeof(CRGB));
184 }
185 break;
186 }
187 }
188}
189
190
191} // namespace fl
fl::XYMap xyMap
fl::CRGB leds[NUM_LEDS]
PixelFormat mFormat
Definition frame.h:70
fl::u16 mHeight
Definition frame.h:69
size_t size() const
Definition frame.h:44
fl::vector_psram< CRGB > mRgb
Definition frame.h:65
void convertPixelsToRgb(fl::u8 *pixels, PixelFormat format)
void drawXY(fl::span< CRGB > leds, const XYMap &xyMap, DrawMode draw_mode=DrawMode::DRAW_MODE_OVERWRITE) const
Definition frame.cpp.hpp:67
~Frame() FL_NOEXCEPT
Definition frame.cpp.hpp:45
bool mIsFromCodec
Definition frame.h:72
void draw(fl::span< CRGB > leds, DrawMode draw_mode=DrawMode::DRAW_MODE_OVERWRITE) const
Definition frame.cpp.hpp:49
void interpolate(const Frame &frame1, const Frame &frame2, u8 amountOfFrame2)
fl::span< CRGB > rgb()
Definition frame.h:42
const size_t mPixelsCount
Definition frame.h:64
Frame(int pixels_per_frame)
Definition frame.cpp.hpp:14
fl::u32 mTimestamp
Definition frame.h:71
bool isValid() const
void clear()
fl::u16 mWidth
Definition frame.h:68
#define FL_WARN(X)
Definition log.h:276
#define FL_DBG
Definition log.h:388
Centralized logging categories for FastLED hardware interfaces and subsystems.
unsigned char u8
Definition s16x16x4.h:132
void * memcpy(void *dest, const void *src, size_t n) FL_NOEXCEPT
unsigned char u8
Definition stdint.h:131
fl::CRGB CRGB
Definition video.h:15
u8 u8 height
Definition blur.h:186
void * memset(void *s, int c, size_t n) FL_NOEXCEPT
u8 width
Definition blur.h:186
void rgb565ToRgb888(fl::u16 rgb565, fl::u8 &r, fl::u8 &g, fl::u8 &b)
Definition pixel.cpp.hpp:23
fl::size size_t
Definition s16x16x4.h:223
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_NOEXCEPT
Definition bit_cast.h:48
fl::string format(const char *fmt)
Format with no arguments.
Definition format.h:439
PixelFormat
Definition pixel.h:7
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT
static CRGB blend(const CRGB &p1, const CRGB &p2, fract8 amountOfP2) FL_NOEXCEPT
Definition crgb.cpp.hpp:54
static CRGB blendAlphaMaxChannel(const CRGB &upper, const CRGB &lower) FL_NOEXCEPT
Definition crgb.cpp.hpp:59
Representation of an 8-bit RGB pixel (Red, Green, Blue)
Definition crgb.h:38