Skip to content

Add a RAII wrapper around pngdec #15

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

Merged
merged 2 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/OpenStreetMap-esp32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
5 changes: 1 addition & 4 deletions src/OpenStreetMap-esp32.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,16 @@
#define OPENSTREETMAP_ESP32_H

#include <Arduino.h>
#include <HTTPClient.h>
#include <WiFiClient.h>
#include <SD.h>
#include <vector>
#include <optional>
#include <memory>
#include <LovyanGFX.hpp>
#include <PNGdec.h>

#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;
Expand Down Expand Up @@ -77,7 +75,6 @@ class OpenStreetMap

std::vector<CachedTile> tilesCache;
uint16_t *currentTileBuffer = nullptr;
PNG png;

uint16_t mapWidth = 320;
uint16_t mapHeight = 240;
Expand Down
79 changes: 79 additions & 0 deletions src/pngdecRAII.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
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

#include <PNGdec.h>

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