diff options
| -rw-r--r-- | src/cart.c | 18 | ||||
| -rw-r--r-- | src/debug.c | 8 | ||||
| -rw-r--r-- | src/png.c | 20 | ||||
| -rw-r--r-- | src/png.h | 12 |
4 files changed, 45 insertions, 13 deletions
@@ -145,7 +145,7 @@ void pw_carts_free(struct pw_carts *carts) { SDL_free(carts); } -static bool blit_image(union pw_png_pixel const *image, SDL_Texture *texture) { +static bool blit_image(pw_png image, SDL_Texture *texture) { SDL_Surface *surface = NULL; if (!SDL_LockTextureToSurface(texture, NULL, &surface)) { return false; @@ -164,7 +164,9 @@ static bool blit_image(union pw_png_pixel const *image, SDL_Texture *texture) { // steganographic encoding of the cart data. uint32_t *pixels = surface->pixels; for (size_t i = 0; i < PW_CART_PNG_WIDTH * PW_CART_PNG_HEIGHT; i++) { - union pw_png_pixel px = image[i]; + union pw_png_pixel px; + pw_png_pixel(image, i, &px); + px.raw &= 0xfcfcfcfc; px.ch.a |= 0x03; pixels[i] = px.raw; @@ -190,9 +192,9 @@ struct pw_cart_png *pw_cart_png_init(SDL_Renderer *renderer) { goto error; } - union pw_png_pixel *image = pw_png_load( - pw_sysroot_unknown_p8_png.data, pw_sysroot_unknown_p8_png.size, - PW_CART_PNG_WIDTH, PW_CART_PNG_HEIGHT); + pw_png image = pw_png_load(pw_sysroot_unknown_p8_png.data, + pw_sysroot_unknown_p8_png.size, PW_CART_PNG_WIDTH, + PW_CART_PNG_HEIGHT); if (!image) { goto error; } @@ -233,7 +235,7 @@ static bool load_cart_png(char const *path, SDL_Texture *texture, bool result = false; uint8_t *data = NULL; - union pw_png_pixel *image = NULL; + pw_png image = NULL; int64_t ssize = SDL_GetIOSize(png); if (ssize < 0) { @@ -266,7 +268,9 @@ static bool load_cart_png(char const *path, SDL_Texture *texture, // Read steganographically encoded cart ROM. for (size_t i = 0; i < PW_CART_ROM_SIZE; i++) { - union pw_png_pixel px = image[i]; + union pw_png_pixel px; + pw_png_pixel(image, i, &px); + px.raw &= 0x03030303; rom[i] = (px.ch.a << 6) | (px.ch.r << 4) | (px.ch.g << 2) | px.ch.b; } diff --git a/src/debug.c b/src/debug.c index f232490..f9248c1 100644 --- a/src/debug.c +++ b/src/debug.c @@ -19,7 +19,7 @@ struct pw_debug *pw_debug_init(SDL_Renderer *renderer) { SDL_Palette *palette = NULL; SDL_Surface *surface = NULL; - union pw_png_pixel *image = NULL; + pw_png image = NULL; SDL_Texture *font = NULL; struct pw_debug *debug = NULL; @@ -47,10 +47,14 @@ struct pw_debug *pw_debug_init(SDL_Renderer *renderer) { if (!image) { goto error; } + union pw_png_pixel mask = {.ch = {0xff, 0xff, 0xff, 0x00}}; uint8_t *pixels = surface->pixels; for (size_t i = 0; i < PW_FONT_WIDTH * PW_FONT_HEIGHT; ++i) { - pixels[i] = !!(image[i].raw & mask.raw); + union pw_png_pixel px; + pw_png_pixel(image, i, &px); + + pixels[i] = !!(px.raw & mask.raw); } SDL_free(image); image = NULL; @@ -18,10 +18,15 @@ static int stbi__mul2shorts_valid(int, int) __attribute__((unused)); #endif #include <stb/stb_image.h> -union pw_png_pixel *pw_png_load(uint8_t const *data, size_t sz, int w, int h) { +pw_png pw_png_load(uint8_t const *data, size_t sz, size_t w, size_t h) { + if (w > STBI_MAX_DIMENSIONS || h > STBI_MAX_DIMENSIONS) { + SDL_SetError("image dimensions too large"); + return NULL; + } + int x, y, n; int ok = stbi_info_from_memory(data, sz, &x, &y, &n); - if (!ok || x != w || y != h) { + if (!ok || x != (int)w || y != (int)h) { SDL_SetError("unexpected PNG dimensions"); return NULL; } @@ -32,8 +37,17 @@ union pw_png_pixel *pw_png_load(uint8_t const *data, size_t sz, int w, int h) { return NULL; } + return image; +} + +void pw_png_pixel(pw_png png, size_t i, union pw_png_pixel *pixel) { + if (!png) { + SDL_zerop(pixel); + return; + } + // The desired channels are set to 4, making the STB image library ensure // that the resulting image has exactly 4 8-bit colour channels per pixel; // allowing us to treat the image as x-by-y 32-bit pixels. - return (union pw_png_pixel *)image; + SDL_memcpy(&pixel->raw, png + i * sizeof(pixel->raw), sizeof(pixel->raw)); } @@ -4,6 +4,11 @@ #include <SDL3/SDL_stdinc.h> /** + * A PNG image. + */ +typedef uint8_t *pw_png; + +/** * A pixel from a loaded PNG image. */ union pw_png_pixel { @@ -19,6 +24,11 @@ union pw_png_pixel { /** * Loads a PNG with exact dimensions. */ -union pw_png_pixel *pw_png_load(uint8_t const *data, size_t sz, int w, int h); +pw_png pw_png_load(uint8_t const *data, size_t sz, size_t w, size_t h); + +/** + * Loads a pixel from the PNG image. + */ +void pw_png_pixel(pw_png png, size_t i, union pw_png_pixel *pixel); #endif |
