FastLED 3.9.3
Loading...
Searching...
No Matches
frame.cpp
1
2#include <string.h>
3
4#include "frame.h"
5#include "crgb.h"
6#include "namespace.h"
7#include "ref.h"
8#include "allocator.h"
9
10
11FASTLED_NAMESPACE_BEGIN
12
13
14Frame::Frame(int pixels_count, bool has_alpha)
15 : mPixelsCount(pixels_count), mRgb() {
16 mRgb.reset(reinterpret_cast<CRGB *>(LargeBlockAllocate(pixels_count * sizeof(CRGB))));
17 memset(mRgb.get(), 0, pixels_count * sizeof(CRGB));
18 if (has_alpha) {
19 mAlpha.reset(reinterpret_cast<uint8_t *>(LargeBlockAllocate(pixels_count)));
20 memset(mAlpha.get(), 0, pixels_count);
21 }
22}
23
24Frame::~Frame() {
25 if (mRgb) {
26 LargeBlockDeallocate(mRgb.release());
27 }
28 if (mAlpha) {
29 LargeBlockDeallocate(mAlpha.release());
30 }
31}
32
33void Frame::draw(CRGB* leds, uint8_t* alpha) const {
34 if (mRgb) {
35 memcpy(leds, mRgb.get(), mPixelsCount * sizeof(CRGB));
36 }
37 if (alpha && mAlpha) {
38 memcpy(alpha, mAlpha.get(), mPixelsCount);
39 }
40}
41
42void Frame::interpolate(const Frame& frame1, const Frame& frame2, uint8_t amountofFrame2, CRGB* pixels, uint8_t* alpha) {
43 (void)alpha; // Reserved for future use.
44 if (frame1.size() != frame2.size()) {
45 return; // Frames must have the same size
46 }
47
48 const CRGB* rgbFirst = frame1.rgb();
49 const CRGB* rgbSecond = frame2.rgb();
50
51 if (!rgbFirst || !rgbSecond) {
52 // Error, why are we getting null pointers?
53 return;
54 }
55
56 for (size_t i = 0; i < frame2.size(); ++i) {
57 pixels[i] = CRGB::blend(rgbFirst[i], rgbSecond[i], amountofFrame2);
58 }
59 // We will eventually do something with alpha.
60}
61
62void Frame::interpolate(const Frame& frame1, const Frame& frame2, uint8_t progress) {
63 if (frame1.size() != frame2.size() || frame1.size() != mPixelsCount) {
64 return; // Frames must have the same size
65 }
66 interpolate(frame1, frame2, progress, mRgb.get(), mAlpha.get());
67}
68
69FASTLED_NAMESPACE_END
Definition frame.h:18
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:39