From 7e37f3252c4130e23b294c2210a427d98f0754ca Mon Sep 17 00:00:00 2001 From: jpirnay Date: Mon, 6 Apr 2026 18:03:50 +0200 Subject: [PATCH] Review comments --- lib/Epub/Epub/converters/DitherUtils.h | 7 +----- src/activities/util/BmpViewerActivity.cpp | 27 ++++++++++++++++------- src/activities/util/BmpViewerActivity.h | 3 +++ 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/lib/Epub/Epub/converters/DitherUtils.h b/lib/Epub/Epub/converters/DitherUtils.h index 19de605c..ef3889e9 100644 --- a/lib/Epub/Epub/converters/DitherUtils.h +++ b/lib/Epub/Epub/converters/DitherUtils.h @@ -12,12 +12,7 @@ inline const uint8_t bayer4x4[4][4] = { {15, 7, 13, 5}, }; -inline uint8_t quantizeGray4Level(uint8_t gray) { - if (gray < 64) return 0; - if (gray < 128) return 1; - if (gray < 192) return 2; - return 3; -} +inline uint8_t quantizeGray4Level(uint8_t gray) { return gray >= 213 ? 3 : static_cast((gray + 42) / 85); } // Apply Bayer dithering and quantize to 4 levels (0-3) // Stateless - works correctly with any pixel processing order diff --git a/src/activities/util/BmpViewerActivity.cpp b/src/activities/util/BmpViewerActivity.cpp index b0126c24..416922a1 100644 --- a/src/activities/util/BmpViewerActivity.cpp +++ b/src/activities/util/BmpViewerActivity.cpp @@ -92,7 +92,9 @@ BmpViewerActivity::BmpViewerActivity(GfxRenderer& renderer, MappedInputManager& filePath(std::move(path)) #ifdef ENABLE_IMAGE_DITHERING_EXTENSION , - imageDitherMode(normalizeImageDitherModeValue(SETTINGS.imageDithering)) { + imageDitherMode(normalizeImageDitherModeValue(SETTINGS.imageDithering)), + initialImageDitherMode(imageDitherMode), + imageDitherSettingsDirty(false) { } #else { @@ -117,6 +119,9 @@ void BmpViewerActivity::onEnter() { } void BmpViewerActivity::onExit() { +#ifdef ENABLE_IMAGE_DITHERING_EXTENSION + saveDitherSettingsIfNeeded(); +#endif Activity::onExit(); renderer.clearScreen(); renderer.displayBuffer(HalDisplay::FULL_REFRESH); @@ -148,12 +153,7 @@ bool BmpViewerActivity::renderBmpImage(const bool showControls) { renderer.clearScreen(); renderer.drawBitmap(bitmap, x, y, pageWidth, pageHeight, 0, 0); if (showControls) { -#ifdef ENABLE_IMAGE_DITHERING_EXTENSION - const auto labels = - mappedInput.mapLabels(tr(STR_BACK), "", I18N.get(getCurrentDitherModeLabel()), tr(STR_SET_SLEEP_SCREEN)); -#else const auto labels = mappedInput.mapLabels(tr(STR_BACK), "", "", tr(STR_SET_SLEEP_SCREEN)); -#endif GUI.drawButtonHints(renderer, labels.btn1, labels.btn2, labels.btn3, labels.btn4); } renderer.displayBuffer(HalDisplay::HALF_REFRESH); @@ -232,12 +232,23 @@ StrId BmpViewerActivity::getCurrentDitherModeLabel() const { void BmpViewerActivity::cycleDitherMode() { imageDitherMode = (imageDitherMode + 1) % CrossPointSettings::IMAGE_DITHERING_COUNT; SETTINGS.imageDithering = imageDitherMode; - SETTINGS.saveToFile(); + imageDitherSettingsDirty = (imageDitherMode != initialImageDitherMode); if (!renderCurrentImage()) { renderError("Could not render image"); } } + +void BmpViewerActivity::saveDitherSettingsIfNeeded() { + if (!imageDitherSettingsDirty) { + return; + } + + SETTINGS.imageDithering = imageDitherMode; + SETTINGS.saveToFile(); + initialImageDitherMode = imageDitherMode; + imageDitherSettingsDirty = false; +} #endif void BmpViewerActivity::renderError(const char* message) { @@ -302,7 +313,7 @@ void BmpViewerActivity::loop() { } #ifdef ENABLE_IMAGE_DITHERING_EXTENSION - if (mappedInput.wasReleased(MappedInputManager::Button::Left)) { + if (!isBmpFile(filePath) && mappedInput.wasReleased(MappedInputManager::Button::Left)) { cycleDitherMode(); return; } diff --git a/src/activities/util/BmpViewerActivity.h b/src/activities/util/BmpViewerActivity.h index 4e88166a..076835a6 100644 --- a/src/activities/util/BmpViewerActivity.h +++ b/src/activities/util/BmpViewerActivity.h @@ -21,6 +21,8 @@ class BmpViewerActivity final : public Activity { std::string filePath; #ifdef ENABLE_IMAGE_DITHERING_EXTENSION uint8_t imageDitherMode; + uint8_t initialImageDitherMode; + bool imageDitherSettingsDirty; #endif bool renderCurrentImage(bool showControls = true); bool renderBmpImage(bool showControls = true); @@ -28,6 +30,7 @@ class BmpViewerActivity final : public Activity { #ifdef ENABLE_IMAGE_DITHERING_EXTENSION void cycleDitherMode(); StrId getCurrentDitherModeLabel() const; + void saveDitherSettingsIfNeeded(); #endif void renderError(const char* message); void setAsSleepScreen();