diff options
| -rw-r--r-- | src/cart.c | 18 | ||||
| -rw-r--r-- | src/debug.c | 8 | ||||
| -rw-r--r-- | src/png.c | 25 | ||||
| -rw-r--r-- | src/png.h | 12 |
4 files changed, 17 insertions, 46 deletions
@@ -145,7 +145,7 @@ void pw_carts_free(struct pw_carts *carts) { SDL_free(carts); } -static bool blit_image(pw_png image, SDL_Texture *texture) { +static bool blit_image(union pw_png_pixel const *image, SDL_Texture *texture) { SDL_Surface *surface = NULL; if (!SDL_LockTextureToSurface(texture, NULL, &surface)) { return false; @@ -164,9 +164,7 @@ static bool blit_image(pw_png 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; - pw_png_pixel(image, i, &px); - + union pw_png_pixel px = image[i]; px.raw &= 0xfcfcfcfc; px.ch.a |= 0x03; pixels[i] = px.raw; @@ -192,9 +190,9 @@ struct pw_cart_png *pw_cart_png_init(SDL_Renderer *renderer) { goto error; } - 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); + 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); if (!image) { goto error; } @@ -235,7 +233,7 @@ static bool load_cart_png(char const *path, SDL_Texture *texture, bool result = false; uint8_t *data = NULL; - pw_png image = NULL; + union pw_png_pixel *image = NULL; int64_t ssize = SDL_GetIOSize(png); if (ssize < 0) { @@ -268,9 +266,7 @@ 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; - pw_png_pixel(image, i, &px); - + union pw_png_pixel px = image[i]; 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 f9248c1..f232490 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; - pw_png image = NULL; + union pw_png_pixel *image = NULL; SDL_Texture *font = NULL; struct pw_debug *debug = NULL; @@ -47,14 +47,10 @@ 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) { - union pw_png_pixel px; - pw_png_pixel(image, i, &px); - - pixels[i] = !!(px.raw & mask.raw); + pixels[i] = !!(image[i].raw & mask.raw); } SDL_free(image); image = NULL; @@ -18,15 +18,10 @@ static int stbi__mul2shorts_valid(int, int) __attribute__((unused)); #endif #include <stb/stb_image.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; - } - +union pw_png_pixel *pw_png_load(uint8_t const *data, size_t sz, int w, int h) { int x, y, n; int ok = stbi_info_from_memory(data, sz, &x, &y, &n); - if (!ok || x != (int)w || y != (int)h) { + if (!ok || x != w || y != h) { SDL_SetError("unexpected PNG dimensions"); return NULL; } @@ -37,17 +32,11 @@ pw_png pw_png_load(uint8_t const *data, size_t sz, size_t w, size_t 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. - SDL_memcpy(&pixel->raw, png + i * sizeof(pixel->raw), sizeof(pixel->raw)); + // allowing us to treat the image as x-by-y 32-bit pixels. Furthermore, + // the `image` is the result of an `SDL_malloc` which is guaranteed to + // alias to any type (as is guaranteed by `malloc`), meaning we do not + // violate any strict-aliasing rules. + return (union pw_png_pixel *)image; } @@ -4,11 +4,6 @@ #include <SDL3/SDL_stdinc.h> /** - * A PNG image. - */ -typedef uint8_t *pw_png; - -/** * A pixel from a loaded PNG image. */ union pw_png_pixel { @@ -24,11 +19,6 @@ union pw_png_pixel { /** * Loads a PNG with exact dimensions. */ -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); +union pw_png_pixel *pw_png_load(uint8_t const *data, size_t sz, int w, int h); #endif |
