Factor out a single PNG-reading function to encapsulate the libpng API (#1765)

This commit is contained in:
Rangi
2025-07-23 15:53:33 -04:00
committed by GitHub
parent 2ce4cdbff6
commit 7e151f16c3
19 changed files with 395 additions and 543 deletions

View File

@@ -5,7 +5,6 @@
#include <array>
#include <optional>
#include <png.h>
#include <vector>
#include "gfx/rgba.hpp"
@@ -16,13 +15,7 @@ static constexpr size_t NB_COLOR_SLOTS = (1 << (5 * 3)) + 1;
struct Palette;
void sortIndexed(
std::vector<Palette> &palettes,
int palSize,
png_color const *palRGB,
int palAlphaSize,
png_byte *palAlpha
);
void sortIndexed(std::vector<Palette> &palettes, std::vector<Rgba> const &embPal);
void sortGrayscale(
std::vector<Palette> &palettes, std::array<std::optional<Rgba>, NB_COLOR_SLOTS> const &colors
);

21
include/gfx/png.hpp Normal file
View File

@@ -0,0 +1,21 @@
// SPDX-License-Identifier: MIT
#ifndef RGBDS_GFX_PNG_HPP
#define RGBDS_GFX_PNG_HPP
#include <fstream>
#include <stdint.h>
#include <vector>
#include "gfx/rgba.hpp"
struct Png {
uint32_t width, height;
std::vector<Rgba> pixels{};
std::vector<Rgba> palette{};
Png() {}
Png(char const *filename, std::streambuf &file);
};
#endif // RGBDS_GFX_PNG_HPP

View File

@@ -17,16 +17,16 @@ struct Rgba {
explicit constexpr Rgba(uint32_t rgba = 0)
: red(rgba >> 24), green(rgba >> 16), blue(rgba >> 8), alpha(rgba) {}
static constexpr Rgba fromCGBColor(uint16_t cgbColor) {
constexpr auto _5to8 = [](uint8_t fiveBpp) -> uint8_t {
fiveBpp &= 0b11111; // For caller's convenience
return fiveBpp << 3 | fiveBpp >> 2;
static constexpr Rgba fromCGBColor(uint16_t color) {
constexpr auto _5to8 = [](uint8_t channel) -> uint8_t {
channel &= 0b11111; // For caller's convenience
return channel << 3 | channel >> 2;
};
return {
_5to8(cgbColor),
_5to8(cgbColor >> 5),
_5to8(cgbColor >> 10),
static_cast<uint8_t>(cgbColor & 0x8000 ? 0x00 : 0xFF),
_5to8(color),
_5to8(color >> 5),
_5to8(color >> 10),
static_cast<uint8_t>(color & 0x8000 ? 0x00 : 0xFF),
};
}