summaryrefslogtreecommitdiff
path: root/src/components
diff options
context:
space:
mode:
authorJF <jf@codingfield.com>2021-03-07 18:45:35 +0100
committerGitea <gitea@fake.local>2021-03-07 18:45:35 +0100
commitc5dc4c55a79e0e3393df22e77825f24b6130a0bb (patch)
tree6f81eb191aa0c749cbef9b856d43ed4546302802 /src/components
parentada942535718d48eec37cca4f50d678e7201dc67 (diff)
parent5845fd98ba68e12f1e57d50ed06abd7ccf47e029 (diff)
Merge branch 'recovery-firmware' of JF/PineTime into develop
Diffstat (limited to 'src/components')
-rw-r--r--src/components/rle/RleDecoder.cpp39
-rw-r--r--src/components/rle/RleDecoder.h33
2 files changed, 72 insertions, 0 deletions
diff --git a/src/components/rle/RleDecoder.cpp b/src/components/rle/RleDecoder.cpp
new file mode 100644
index 00000000..19a90fda
--- /dev/null
+++ b/src/components/rle/RleDecoder.cpp
@@ -0,0 +1,39 @@
+#include "RleDecoder.h"
+
+using namespace Pinetime::Tools;
+
+RleDecoder::RleDecoder(const uint8_t *buffer, size_t size) : buffer{buffer}, size{size} {
+
+}
+
+RleDecoder::RleDecoder(const uint8_t *buffer, size_t size, uint16_t foregroundColor, uint16_t backgroundColor) : RleDecoder{buffer, size} {
+ this->foregroundColor = foregroundColor;
+ this->backgroundColor = backgroundColor;
+}
+
+
+void RleDecoder::DecodeNext(uint8_t *output, size_t maxBytes) {
+ for (;encodedBufferIndex<size; encodedBufferIndex++) {
+ uint8_t rl = buffer[encodedBufferIndex] - processedCount;
+ while (rl) {
+ output[bp] = color >> 8;
+ output[bp + 1] = color & 0xff;
+ bp += 2;
+ rl -= 1;
+ processedCount++;
+
+ if (bp >= maxBytes) {
+ bp = 0;
+ y += 1;
+ return;
+ }
+ }
+ processedCount = 0;
+
+ if (color == backgroundColor)
+ color = foregroundColor;
+ else
+ color = backgroundColor;
+ }
+}
+
diff --git a/src/components/rle/RleDecoder.h b/src/components/rle/RleDecoder.h
new file mode 100644
index 00000000..0f607fb8
--- /dev/null
+++ b/src/components/rle/RleDecoder.h
@@ -0,0 +1,33 @@
+#pragma once
+
+#include <cstdint>
+#include <cstddef>
+
+namespace Pinetime {
+ namespace Tools {
+ /* 1-bit RLE decoder. Provide the encoded buffer to the constructor and then call DecodeNext() by
+ * specifying the output (decoded) buffer and the maximum number of bytes this buffer can handle.
+ *
+ * Code from https://github.com/daniel-thompson/wasp-bootloader by Daniel Thompson released under the MIT license.
+ */
+ class RleDecoder {
+ public:
+ RleDecoder(const uint8_t* buffer, size_t size);
+ RleDecoder(const uint8_t* buffer, size_t size, uint16_t foregroundColor, uint16_t backgroundColor);
+
+ void DecodeNext(uint8_t* output, size_t maxBytes);
+
+ private:
+ const uint8_t* buffer;
+ size_t size;
+
+ int encodedBufferIndex = 0;
+ int y = 0;
+ uint16_t bp = 0;
+ uint16_t foregroundColor = 0xffff;
+ uint16_t backgroundColor = 0;
+ uint16_t color = backgroundColor;
+ int processedCount = 0;
+ };
+ }
+}