From 04dac509b143fd761a8838791eefa6a39f6d9623 Mon Sep 17 00:00:00 2001 From: Cellie Date: Wed, 26 Mar 2025 23:09:39 +0100 Subject: [PATCH 1/2] Add a RAII wrapper around pngdec --- src/OpenStreetMap-esp32.cpp | 6 ++-- src/OpenStreetMap-esp32.h | 1 + src/pngdecRAII.h | 56 +++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 src/pngdecRAII.h diff --git a/src/OpenStreetMap-esp32.cpp b/src/OpenStreetMap-esp32.cpp index 852641c..8b7e2ed 100644 --- a/src/OpenStreetMap-esp32.cpp +++ b/src/OpenStreetMap-esp32.cpp @@ -383,10 +383,10 @@ bool OpenStreetMap::fetchTile(CachedTile &tile, uint32_t x, uint32_t y, uint8_t if (!buffer) return false; - const int16_t rc = png.openRAM(buffer.value()->get(), buffer.value()->size(), PNGDraw); - if (rc != PNG_SUCCESS) + PNGDecoderRAII png(PNGDraw); + if (!png.open(buffer.value()->get(), buffer.value()->size())) { - result = "PNG Decoder Error: " + String(rc); + result = "PNG Decoder Error"; return false; } diff --git a/src/OpenStreetMap-esp32.h b/src/OpenStreetMap-esp32.h index 5d16e4a..2940787 100644 --- a/src/OpenStreetMap-esp32.h +++ b/src/OpenStreetMap-esp32.h @@ -37,6 +37,7 @@ #include "CachedTile.h" #include "MemoryBuffer.h" #include "HTTPClientRAII.h" +#include "pngdecRAII.h" constexpr uint16_t OSM_TILESIZE = 256; constexpr uint16_t OSM_TILE_TIMEOUT_MS = 2500; diff --git a/src/pngdecRAII.h b/src/pngdecRAII.h new file mode 100644 index 0000000..bef6e73 --- /dev/null +++ b/src/pngdecRAII.h @@ -0,0 +1,56 @@ +#ifndef PNGDEC_RAII_H +#define PNGDEC_RAII_H + +#include + +class PNGDecoderRAII +{ +public: + explicit PNGDecoderRAII(PNG_DRAW_CALLBACK *drawCallback) + : callback(drawCallback), isOpen(false) {} + + PNGDecoderRAII(const PNGDecoderRAII &) = delete; + PNGDecoderRAII &operator=(const PNGDecoderRAII &) = delete; + + ~PNGDecoderRAII() + { + close(); + } + + bool open(uint8_t *pngData, size_t dataSize) + { + if (isOpen) + close(); + + int result = png.openRAM(pngData, dataSize, callback); + isOpen = (result == PNG_SUCCESS); + return isOpen; + } + + int decode(void *pPriv = NULL, uint8_t options = 0) + { + if (!isOpen) + return PNG_INVALID_PARAMETER; + + return png.decode(pPriv, options); + } + + int getWidth() const { return png.getWidth(); } + int getHeight() const { return png.getHeight(); } + + void close() + { + if (isOpen) + { + png.close(); + isOpen = false; + } + } + +private: + PNGdec png; + PNG_DRAW_CALLBACK *callback; + bool isOpen; +}; + +#endif // PNGDEC_RAII_H From a1cbd9b4f80a02f3671b2f51363b0263bf28da62 Mon Sep 17 00:00:00 2001 From: Cellie Date: Wed, 26 Mar 2025 23:30:21 +0100 Subject: [PATCH 2/2] Add MIT license Cleanup --- src/OpenStreetMap-esp32.h | 4 ---- src/pngdecRAII.h | 23 +++++++++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/OpenStreetMap-esp32.h b/src/OpenStreetMap-esp32.h index 2940787..fc59089 100644 --- a/src/OpenStreetMap-esp32.h +++ b/src/OpenStreetMap-esp32.h @@ -25,14 +25,11 @@ #define OPENSTREETMAP_ESP32_H #include -#include #include #include #include #include -#include #include -#include #include "CachedTile.h" #include "MemoryBuffer.h" @@ -78,7 +75,6 @@ class OpenStreetMap std::vector tilesCache; uint16_t *currentTileBuffer = nullptr; - PNG png; uint16_t mapWidth = 320; uint16_t mapHeight = 240; diff --git a/src/pngdecRAII.h b/src/pngdecRAII.h index bef6e73..d64f309 100644 --- a/src/pngdecRAII.h +++ b/src/pngdecRAII.h @@ -1,3 +1,26 @@ +/* + Copyright (c) 2025 Cellie https://github.com/CelliesProjects/OpenStreetMap-esp32 + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + SPDX-License-Identifier: MIT + */ + #ifndef PNGDEC_RAII_H #define PNGDEC_RAII_H