From 8add255bc3a13bbeab48956ac042ae081380c084 Mon Sep 17 00:00:00 2001 From: jpirnay Date: Sat, 11 Apr 2026 19:44:40 +0200 Subject: [PATCH] Add some linter comments --- .../converters/JpegToFramebufferConverter.cpp | 38 ++++++------------ .../converters/PngToFramebufferConverter.cpp | 39 ++++++------------- src/activities/boot_sleep/SleepActivity.cpp | 11 +++--- 3 files changed, 29 insertions(+), 59 deletions(-) diff --git a/lib/Epub/Epub/converters/JpegToFramebufferConverter.cpp b/lib/Epub/Epub/converters/JpegToFramebufferConverter.cpp index d05c3d34..ca632eb8 100644 --- a/lib/Epub/Epub/converters/JpegToFramebufferConverter.cpp +++ b/lib/Epub/Epub/converters/JpegToFramebufferConverter.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include "DirectPixelWriter.h" @@ -43,21 +44,13 @@ struct JpegContext { // See PngContext for the rationale: monochromeOutput requests a 1-bit Atkinson dither // emitting only 0/3 so the BW DirectPixelWriter (`pixelValue < 3` rule) maps cleanly. int oneBitDitherRow{-1}; - Atkinson1BitDitherer* atkinson1BitDitherer{nullptr}; + std::unique_ptr atkinson1BitDitherer; #ifdef ENABLE_IMAGE_DITHERING_EXTENSION int currentDitherRow{-1}; - AtkinsonDitherer* atkinsonDitherer{nullptr}; - DiffusedBayerDitherer* diffusedBayerDitherer{nullptr}; + std::unique_ptr atkinsonDitherer; + std::unique_ptr diffusedBayerDitherer; #endif - - ~JpegContext() { - delete atkinson1BitDitherer; -#ifdef ENABLE_IMAGE_DITHERING_EXTENSION - delete atkinsonDitherer; - delete diffusedBayerDitherer; -#endif - } }; // Advance the 1-bit Atkinson ditherer to the requested destination row. @@ -136,9 +129,9 @@ uint8_t ditherGray(JpegContext& ctx, uint8_t gray, int localX, int outX, int out // File I/O callbacks use pFile->fHandle to access the FsFile*, // avoiding the need for global file state. void* jpegOpen(const char* filename, int32_t* size) { - FsFile* f = new FsFile(); + FsFile* f = new FsFile(); // NOLINT(cppcoreguidelines-owning-memory) — ownership transferred via void* to JPEGDEC callbacks if (!Storage.openFileForRead("JPG", std::string(filename), *f)) { - delete f; + delete f; // NOLINT(cppcoreguidelines-owning-memory) return nullptr; } *size = f->size(); @@ -149,7 +142,7 @@ void jpegClose(void* handle) { FsFile* f = reinterpret_cast(handle); if (f) { f->close(); - delete f; + delete f; // NOLINT(cppcoreguidelines-owning-memory) } } @@ -413,7 +406,7 @@ bool JpegToFramebufferConverter::getDimensionsStatic(const std::string& imagePat return false; } - JPEGDEC* jpeg = new (std::nothrow) JPEGDEC(); + std::unique_ptr jpeg(new (std::nothrow) JPEGDEC()); if (!jpeg) { LOG_ERR("JPG", "Failed to allocate JPEG decoder for dimensions"); return false; @@ -422,7 +415,6 @@ bool JpegToFramebufferConverter::getDimensionsStatic(const std::string& imagePat int rc = jpeg->open(imagePath.c_str(), jpegOpen, jpegClose, jpegRead, jpegSeek, nullptr); if (rc != 1) { LOG_ERR("JPG", "Failed to open JPEG for dimensions (err=%d): %s", jpeg->getLastError(), imagePath.c_str()); - delete jpeg; return false; } @@ -431,7 +423,6 @@ bool JpegToFramebufferConverter::getDimensionsStatic(const std::string& imagePat LOG_DBG("JPG", "Image dimensions: %dx%d", out.width, out.height); jpeg->close(); - delete jpeg; return true; } @@ -445,7 +436,7 @@ bool JpegToFramebufferConverter::decodeToFramebuffer(const std::string& imagePat return false; } - JPEGDEC* jpeg = new (std::nothrow) JPEGDEC(); + std::unique_ptr jpeg(new (std::nothrow) JPEGDEC()); if (!jpeg) { LOG_ERR("JPG", "Failed to allocate JPEG decoder"); return false; @@ -460,7 +451,6 @@ bool JpegToFramebufferConverter::decodeToFramebuffer(const std::string& imagePat int rc = jpeg->open(imagePath.c_str(), jpegOpen, jpegClose, jpegRead, jpegSeek, jpegDrawCallback); if (rc != 1) { LOG_ERR("JPG", "Failed to open JPEG (err=%d): %s", jpeg->getLastError(), imagePath.c_str()); - delete jpeg; return false; } @@ -470,13 +460,11 @@ bool JpegToFramebufferConverter::decodeToFramebuffer(const std::string& imagePat if (srcWidth <= 0 || srcHeight <= 0) { LOG_ERR("JPG", "Invalid JPEG dimensions: %dx%d", srcWidth, srcHeight); jpeg->close(); - delete jpeg; return false; } if (!validateImageDimensions(srcWidth, srcHeight, "JPEG")) { jpeg->close(); - delete jpeg; return false; } @@ -543,7 +531,7 @@ bool JpegToFramebufferConverter::decodeToFramebuffer(const std::string& imagePat // See PngToFramebufferConverter for rationale: BW-only display needs a 1-bit // dither so mid-grays don't collapse to black under DirectPixelWriter's `< 3` rule. if (config.monochromeOutput) { - ctx.atkinson1BitDitherer = new (std::nothrow) Atkinson1BitDitherer(destWidth); + ctx.atkinson1BitDitherer.reset(new (std::nothrow) Atkinson1BitDitherer(destWidth)); if (!ctx.atkinson1BitDitherer) { LOG_ERR("JPG", "Failed to allocate 1-bit Atkinson ditherer, falling back to 4-level dither"); } @@ -553,13 +541,13 @@ bool JpegToFramebufferConverter::decodeToFramebuffer(const std::string& imagePat #ifdef ENABLE_IMAGE_DITHERING_EXTENSION switch (config.ditherMode) { case ImageDitherMode::Atkinson: - ctx.atkinsonDitherer = new (std::nothrow) AtkinsonDitherer(destWidth); + ctx.atkinsonDitherer.reset(new (std::nothrow) AtkinsonDitherer(destWidth)); if (!ctx.atkinsonDitherer) { LOG_ERR("JPG", "Failed to allocate Atkinson ditherer, falling back to Bayer"); } break; case ImageDitherMode::DiffusedBayer: - ctx.diffusedBayerDitherer = new (std::nothrow) DiffusedBayerDitherer(destWidth); + ctx.diffusedBayerDitherer.reset(new (std::nothrow) DiffusedBayerDitherer(destWidth)); if (!ctx.diffusedBayerDitherer) { LOG_ERR("JPG", "Failed to allocate diffused Bayer ditherer, falling back to Bayer"); } @@ -579,12 +567,10 @@ bool JpegToFramebufferConverter::decodeToFramebuffer(const std::string& imagePat if (rc != 1) { LOG_ERR("JPG", "Decode failed (rc=%d, lastError=%d)", rc, jpeg->getLastError()); jpeg->close(); - delete jpeg; return false; } jpeg->close(); - delete jpeg; LOG_DBG("JPG", "JPEG decoding complete - render time: %lu ms", decodeTime); // Write cache file if caching was enabled diff --git a/lib/Epub/Epub/converters/PngToFramebufferConverter.cpp b/lib/Epub/Epub/converters/PngToFramebufferConverter.cpp index 014cdceb..5f6dcc07 100644 --- a/lib/Epub/Epub/converters/PngToFramebufferConverter.cpp +++ b/lib/Epub/Epub/converters/PngToFramebufferConverter.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include "DirectPixelWriter.h" @@ -44,21 +45,13 @@ struct PngContext { // `pixelValue < 3` rule. The 4-level dither path collapses mid-grays to solid // black under that rule. int oneBitDitherRow{-1}; - Atkinson1BitDitherer* atkinson1BitDitherer{nullptr}; + std::unique_ptr atkinson1BitDitherer; #ifdef ENABLE_IMAGE_DITHERING_EXTENSION int currentDitherRow{-1}; - AtkinsonDitherer* atkinsonDitherer{nullptr}; - DiffusedBayerDitherer* diffusedBayerDitherer{nullptr}; + std::unique_ptr atkinsonDitherer; + std::unique_ptr diffusedBayerDitherer; #endif - - ~PngContext() { - delete atkinson1BitDitherer; -#ifdef ENABLE_IMAGE_DITHERING_EXTENSION - delete atkinsonDitherer; - delete diffusedBayerDitherer; -#endif - } }; // Advance the 1-bit Atkinson ditherer to the requested destination row. @@ -140,9 +133,9 @@ uint8_t ditherGray(PngContext& ctx, uint8_t gray, int localX, int outX, int outY // File I/O callbacks use pFile->fHandle to access the FsFile*, // avoiding the need for global file state. void* pngOpenWithHandle(const char* filename, int32_t* size) { - FsFile* f = new FsFile(); + FsFile* f = new FsFile(); // NOLINT(cppcoreguidelines-owning-memory) — ownership transferred via void* to PNGdec callbacks if (!Storage.openFileForRead("PNG", std::string(filename), *f)) { - delete f; + delete f; // NOLINT(cppcoreguidelines-owning-memory) return nullptr; } *size = f->size(); @@ -153,7 +146,7 @@ void pngCloseWithHandle(void* handle) { FsFile* f = reinterpret_cast(handle); if (f) { f->close(); - delete f; + delete f; // NOLINT(cppcoreguidelines-owning-memory) } } @@ -341,7 +334,7 @@ bool PngToFramebufferConverter::getDimensionsStatic(const std::string& imagePath return false; } - PNG* png = new (std::nothrow) PNG(); + std::unique_ptr png(new (std::nothrow) PNG()); if (!png) { LOG_ERR("PNG", "Failed to allocate PNG decoder for dimensions"); return false; @@ -352,7 +345,6 @@ bool PngToFramebufferConverter::getDimensionsStatic(const std::string& imagePath if (rc != 0) { LOG_ERR("PNG", "Failed to open PNG for dimensions: %d", rc); - delete png; return false; } @@ -360,7 +352,6 @@ bool PngToFramebufferConverter::getDimensionsStatic(const std::string& imagePath out.height = png->getHeight(); png->close(); - delete png; return true; } @@ -375,7 +366,7 @@ bool PngToFramebufferConverter::decodeToFramebuffer(const std::string& imagePath } // Heap-allocate PNG decoder (~42 KB) - freed at end of function - PNG* png = new (std::nothrow) PNG(); + std::unique_ptr png(new (std::nothrow) PNG()); if (!png) { LOG_ERR("PNG", "Failed to allocate PNG decoder"); return false; @@ -391,13 +382,11 @@ bool PngToFramebufferConverter::decodeToFramebuffer(const std::string& imagePath pngDrawCallback); if (rc != PNG_SUCCESS) { LOG_ERR("PNG", "Failed to open PNG: %d", rc); - delete png; return false; } if (!validateImageDimensions(png->getWidth(), png->getHeight(), "PNG")) { png->close(); - delete png; return false; } @@ -433,7 +422,6 @@ bool PngToFramebufferConverter::decodeToFramebuffer(const std::string& imagePath requiredInternal, ctx.srcWidth, pixelType, PNG_MAX_BUFFERED_PIXELS); LOG_ERR("PNG", "Aborting decode to avoid PNGdec internal buffer overflow"); png->close(); - delete png; return false; } @@ -447,7 +435,6 @@ bool PngToFramebufferConverter::decodeToFramebuffer(const std::string& imagePath if (!ctx.grayLineBuffer) { LOG_ERR("PNG", "Failed to allocate gray line buffer"); png->close(); - delete png; return false; } @@ -475,7 +462,7 @@ bool PngToFramebufferConverter::decodeToFramebuffer(const std::string& imagePath // The 1-bit ditherer emits only 0 or 3 so the BW writer maps cleanly to // black/white. if (config.monochromeOutput) { - ctx.atkinson1BitDitherer = new (std::nothrow) Atkinson1BitDitherer(ctx.dstWidth); + ctx.atkinson1BitDitherer.reset(new (std::nothrow) Atkinson1BitDitherer(ctx.dstWidth)); if (!ctx.atkinson1BitDitherer) { LOG_ERR("PNG", "Failed to allocate 1-bit Atkinson ditherer, falling back to 4-level dither"); } @@ -485,13 +472,13 @@ bool PngToFramebufferConverter::decodeToFramebuffer(const std::string& imagePath #ifdef ENABLE_IMAGE_DITHERING_EXTENSION switch (config.ditherMode) { case ImageDitherMode::Atkinson: - ctx.atkinsonDitherer = new (std::nothrow) AtkinsonDitherer(ctx.dstWidth); + ctx.atkinsonDitherer.reset(new (std::nothrow) AtkinsonDitherer(ctx.dstWidth)); if (!ctx.atkinsonDitherer) { LOG_ERR("PNG", "Failed to allocate Atkinson ditherer, falling back to Bayer"); } break; case ImageDitherMode::DiffusedBayer: - ctx.diffusedBayerDitherer = new (std::nothrow) DiffusedBayerDitherer(ctx.dstWidth); + ctx.diffusedBayerDitherer.reset(new (std::nothrow) DiffusedBayerDitherer(ctx.dstWidth)); if (!ctx.diffusedBayerDitherer) { LOG_ERR("PNG", "Failed to allocate diffused Bayer ditherer, falling back to Bayer"); } @@ -514,12 +501,10 @@ bool PngToFramebufferConverter::decodeToFramebuffer(const std::string& imagePath if (rc != PNG_SUCCESS) { LOG_ERR("PNG", "Decode failed: %d", rc); png->close(); - delete png; return false; } png->close(); - delete png; LOG_DBG("PNG", "PNG decoding complete - render time: %lu ms", decodeTime); // Write cache file if caching was enabled and buffer was allocated diff --git a/src/activities/boot_sleep/SleepActivity.cpp b/src/activities/boot_sleep/SleepActivity.cpp index 8a473fd3..8c8dce03 100644 --- a/src/activities/boot_sleep/SleepActivity.cpp +++ b/src/activities/boot_sleep/SleepActivity.cpp @@ -13,6 +13,7 @@ #include #include +#include #include #include "../reader/EpubReaderActivity.h" @@ -47,9 +48,9 @@ struct PngOverlayCtx { // PNGdec file I/O callbacks — mirror the pattern in PngToFramebufferConverter.cpp. void* pngSleepOpen(const char* filename, int32_t* size) { - FsFile* f = new FsFile(); + FsFile* f = new FsFile(); // NOLINT(cppcoreguidelines-owning-memory) — ownership transferred via void* to PNGdec callbacks if (!Storage.openFileForRead("SLP", std::string(filename), *f)) { - delete f; + delete f; // NOLINT(cppcoreguidelines-owning-memory) return nullptr; } *size = f->size(); @@ -59,7 +60,7 @@ void pngSleepClose(void* handle) { FsFile* f = reinterpret_cast(handle); if (f) { f->close(); - delete f; + delete f; // NOLINT(cppcoreguidelines-owning-memory) } } int32_t pngSleepRead(PNGFILE* pFile, uint8_t* pBuf, int32_t len) { @@ -869,13 +870,12 @@ void SleepActivity::renderOverlaySleepScreen() const { LOG_ERR("SLP", "Not enough heap for PNG overlay decoder"); return false; } - PNG* png = new (std::nothrow) PNG(); + std::unique_ptr png(new (std::nothrow) PNG()); if (!png) return false; int rc = png->open(filename.c_str(), pngSleepOpen, pngSleepClose, pngSleepRead, pngSleepSeek, pngOverlayDraw); if (rc != PNG_SUCCESS) { LOG_DBG("SLP", "PNG open failed: %s (%d)", filename.c_str(), rc); - delete png; return false; } @@ -905,7 +905,6 @@ void SleepActivity::renderOverlaySleepScreen() const { rc = png->decode(&ctx, 0); png->close(); - delete png; return rc == PNG_SUCCESS; };