forked from uxjulia/CrossInk
-
Notifications
You must be signed in to change notification settings - Fork 2
fix: garbled TRMNL sleep screen: unpack sub-8-bit grayscale PNGs when using trmnl.app #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mcmphd
wants to merge
4
commits into
yazdipour:development
Choose a base branch
from
mcmphd:fix/trmnl-2bit-grayscale-png
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
32fc82a
feat(opds): download opds files in specific dir
yazdipour 154b3bb
feat(TRMNL): implement TRMNL Lockscreen
yazdipour 3748a0a
fix: render sub-8-bit grayscale PNGs (fixes garbled TRMNL sleep screen)
mcmphd eaf0189
fix: address review -- extract shared PNG grayscale unpacker, add 16-…
mcmphd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| #include "TrmnlDisplayJsonParser.h" | ||
|
|
||
| #include <cstdlib> | ||
| #include <cstring> | ||
|
|
||
| namespace { | ||
| void safeCopy(char* dst, const size_t dstSize, const char* src, const size_t srcLen) { | ||
| const size_t n = srcLen < dstSize - 1 ? srcLen : dstSize - 1; | ||
| memcpy(dst, src, n); | ||
| dst[n] = '\0'; | ||
| } | ||
| } // namespace | ||
|
|
||
| TrmnlDisplayJsonParser::TrmnlDisplayJsonParser() | ||
| : parser(JsonCallbacks{this, sOnKey, sOnString, sOnNumber, sOnBool, sOnNull, sOnObjectStart, sOnObjectEnd, | ||
| sOnArrayStart, sOnArrayEnd}) { | ||
| reset(); | ||
| } | ||
|
|
||
| void TrmnlDisplayJsonParser::reset() { | ||
| parser.reset(); | ||
| lastKey = LastKey::NONE; | ||
| depth = 0; | ||
| imageUrl[0] = '\0'; | ||
| filename[0] = '\0'; | ||
| refreshRateSeconds = 0; | ||
| imageUrlFound = false; | ||
| } | ||
|
|
||
| void TrmnlDisplayJsonParser::feed(const char* data, const size_t len) { parser.feed(data, len); } | ||
| bool TrmnlDisplayJsonParser::foundImageUrl() const { return imageUrlFound; } | ||
| bool TrmnlDisplayJsonParser::hasError() const { return parser.hasError(); } | ||
| const char* TrmnlDisplayJsonParser::getImageUrl() const { return imageUrl; } | ||
| const char* TrmnlDisplayJsonParser::getFilename() const { return filename; } | ||
| uint32_t TrmnlDisplayJsonParser::getRefreshRateSeconds() const { return refreshRateSeconds; } | ||
|
|
||
| void TrmnlDisplayJsonParser::sOnKey(void* ctx, const char* key, const size_t len) { | ||
| auto* self = static_cast<TrmnlDisplayJsonParser*>(ctx); | ||
| if (self->depth != 1) { | ||
| self->lastKey = LastKey::NONE; | ||
| return; | ||
| } | ||
| if (len == 9 && memcmp(key, "image_url", 9) == 0) { | ||
| self->lastKey = LastKey::IMAGE_URL; | ||
| } else if ((len == 8 && memcmp(key, "filename", 8) == 0) || (len == 10 && memcmp(key, "image_name", 10) == 0)) { | ||
| self->lastKey = LastKey::FILENAME; | ||
| } else if (len == 12 && memcmp(key, "refresh_rate", 12) == 0) { | ||
| self->lastKey = LastKey::REFRESH_RATE; | ||
| } else { | ||
| self->lastKey = LastKey::NONE; | ||
| } | ||
| } | ||
|
|
||
| void TrmnlDisplayJsonParser::sOnString(void* ctx, const char* value, const size_t len) { | ||
| auto* self = static_cast<TrmnlDisplayJsonParser*>(ctx); | ||
| if (self->depth == 1 && self->lastKey == LastKey::IMAGE_URL) { | ||
| safeCopy(self->imageUrl, sizeof(self->imageUrl), value, len); | ||
| self->imageUrlFound = self->imageUrl[0] != '\0'; | ||
| } else if (self->depth == 1 && self->lastKey == LastKey::FILENAME) { | ||
| safeCopy(self->filename, sizeof(self->filename), value, len); | ||
| } | ||
| self->lastKey = LastKey::NONE; | ||
| } | ||
|
|
||
| void TrmnlDisplayJsonParser::sOnNumber(void* ctx, const char* value, const size_t /*len*/) { | ||
| auto* self = static_cast<TrmnlDisplayJsonParser*>(ctx); | ||
| if (self->depth == 1 && self->lastKey == LastKey::REFRESH_RATE) { | ||
| self->refreshRateSeconds = static_cast<uint32_t>(strtoul(value, nullptr, 10)); | ||
| } | ||
| self->lastKey = LastKey::NONE; | ||
| } | ||
|
|
||
| void TrmnlDisplayJsonParser::sOnBool(void* ctx, bool /*value*/) { | ||
| static_cast<TrmnlDisplayJsonParser*>(ctx)->lastKey = LastKey::NONE; | ||
| } | ||
| void TrmnlDisplayJsonParser::sOnNull(void* ctx) { static_cast<TrmnlDisplayJsonParser*>(ctx)->lastKey = LastKey::NONE; } | ||
| void TrmnlDisplayJsonParser::sOnObjectStart(void* ctx) { | ||
| auto* self = static_cast<TrmnlDisplayJsonParser*>(ctx); | ||
| self->depth++; | ||
| self->lastKey = LastKey::NONE; | ||
| } | ||
| void TrmnlDisplayJsonParser::sOnObjectEnd(void* ctx) { | ||
| auto* self = static_cast<TrmnlDisplayJsonParser*>(ctx); | ||
| if (self->depth > 0) self->depth--; | ||
| self->lastKey = LastKey::NONE; | ||
| } | ||
| void TrmnlDisplayJsonParser::sOnArrayStart(void* ctx) { sOnObjectStart(ctx); } | ||
| void TrmnlDisplayJsonParser::sOnArrayEnd(void* ctx) { sOnObjectEnd(ctx); } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| #pragma once | ||
|
|
||
| #include <cstddef> | ||
| #include <cstdint> | ||
|
|
||
| #include "StreamingJsonParser.h" | ||
|
|
||
| class TrmnlDisplayJsonParser { | ||
| public: | ||
| TrmnlDisplayJsonParser(); | ||
| void reset(); | ||
| void feed(const char* data, size_t len); | ||
|
|
||
| bool foundImageUrl() const; | ||
| bool hasError() const; | ||
| const char* getImageUrl() const; | ||
| const char* getFilename() const; | ||
| uint32_t getRefreshRateSeconds() const; | ||
|
|
||
| private: | ||
| enum class LastKey : uint8_t { NONE, IMAGE_URL, FILENAME, REFRESH_RATE }; | ||
|
|
||
| static void sOnKey(void* ctx, const char* key, size_t len); | ||
| static void sOnString(void* ctx, const char* value, size_t len); | ||
| static void sOnNumber(void* ctx, const char* value, size_t len); | ||
| static void sOnBool(void* ctx, bool value); | ||
| static void sOnNull(void* ctx); | ||
| static void sOnObjectStart(void* ctx); | ||
| static void sOnObjectEnd(void* ctx); | ||
| static void sOnArrayStart(void* ctx); | ||
| static void sOnArrayEnd(void* ctx); | ||
|
|
||
| StreamingJsonParser parser; | ||
| LastKey lastKey = LastKey::NONE; | ||
| uint16_t depth = 0; | ||
| char imageUrl[512] = ""; | ||
| char filename[128] = ""; | ||
| uint32_t refreshRateSeconds = 0; | ||
| bool imageUrlFound = false; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| #pragma once | ||
|
|
||
| #include <cstdint> | ||
|
|
||
| // Reads a single grayscale sample from a PNGdec scanline at column `x` and | ||
| // returns it scaled to a full 0..255 intensity. | ||
| // | ||
| // PNG grayscale images are encoded at 1, 2, 4, 8, or 16 bits per sample (PNG | ||
| // spec, IHDR bit depth for color type 0). PNGdec delivers each scanline | ||
| // packed at that native bit depth: | ||
| // - 1/2/4-bit: samples are packed MSB-first, multiple samples per byte. | ||
| // - 8-bit: one byte per sample. | ||
| // - 16-bit: two bytes per sample, big-endian per spec; we use the high | ||
| // byte as the 8-bit intensity, matching how the rest of the rendering | ||
| // pipeline treats channel data. | ||
| // Any other bpp is not a valid PNG grayscale bit depth and should not occur | ||
| // from PNGdec; rather than trust that invariant and read out of bounds or | ||
| // misinterpret the buffer, fall back to a neutral mid-gray. | ||
| inline uint8_t pngUnpackGraySample(const uint8_t* pixels, const int x, const int bpp) { | ||
| switch (bpp) { | ||
| case 8: | ||
| return pixels[x]; | ||
| case 16: | ||
| return pixels[x * 2]; | ||
| case 1: | ||
| case 2: | ||
| case 4: { | ||
| const int ppb = 8 / bpp; // samples per byte | ||
| const int mask = (1 << bpp) - 1; // max sample value | ||
| const uint8_t byte = pixels[x / ppb]; | ||
| const int shift = (ppb - 1 - (x % ppb)) * bpp; // MSB-first within the byte | ||
| const int sample = (byte >> shift) & mask; | ||
| return static_cast<uint8_t>(sample * 255 / mask); // scale sample to 0..255 | ||
| } | ||
| default: | ||
| return 128; // unexpected bit depth; avoid reading out of bounds or misinterpreting data | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.