From 295cbdb0a1f2734cc6079035fadf775cee835762 Mon Sep 17 00:00:00 2001 From: Emil Kosz Date: Wed, 24 Sep 2025 20:14:07 +0200 Subject: [PATCH] Simplify PNG loading a bit --- src/common/world.c | 43 ++++++------------------------------------- 1 file changed, 6 insertions(+), 37 deletions(-) diff --git a/src/common/world.c b/src/common/world.c index 4084bf2..70781fc 100644 --- a/src/common/world.c +++ b/src/common/world.c @@ -11,7 +11,7 @@ static void glib_png_read (png_structp png_ptr, png_bytep out_bytes, png_size_t len) { PngBuffer *buf = (PngBuffer *) png_get_io_ptr (png_ptr); - + if (buf->offset + len > buf->size) { png_error (png_ptr, "Read beyond buffer"); @@ -24,8 +24,6 @@ glib_png_read (png_structp png_ptr, png_bytep out_bytes, png_size_t len) World * World_load_from_path (const char *path) { - UNUSED (path); - World *self = g_new0 (World, 1); self->buildings = g_array_new (FALSE, FALSE, sizeof (Building *)); @@ -69,41 +67,14 @@ World_load_from_path (const char *path) self->tiles = g_array_sized_new (FALSE, FALSE, sizeof (MapTile *), self->width * self->height); - png_byte color_type = png_get_color_type (png_ptr, info_ptr); - png_byte bit_depth = png_get_bit_depth (png_ptr, info_ptr); + png_set_expand (png_ptr); + png_set_gray_to_rgb (png_ptr); + png_set_filler (png_ptr, 0xFF, PNG_FILLER_AFTER); - if (bit_depth == 16) + if (png_get_bit_depth (png_ptr, info_ptr) == 16) { png_set_strip_16 (png_ptr); } - - if (color_type == PNG_COLOR_TYPE_PALETTE) - { - png_set_palette_to_rgb (png_ptr); - } - - if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) - { - png_set_expand_gray_1_2_4_to_8 (png_ptr); - } - - if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS)) - { - png_set_tRNS_to_alpha (png_ptr); - } - - if (color_type == PNG_COLOR_TYPE_RGB || - color_type == PNG_COLOR_TYPE_GRAY || - color_type == PNG_COLOR_TYPE_PALETTE) - { - png_set_filler (png_ptr, 0xFF, PNG_FILLER_AFTER); - } - - if (color_type == PNG_COLOR_TYPE_GRAY || - color_type == PNG_COLOR_TYPE_GRAY_ALPHA) - { - png_set_gray_to_rgb (png_ptr); - } png_read_update_info (png_ptr, info_ptr); @@ -127,9 +98,7 @@ World_load_from_path (const char *path) mt->y = y; unsigned char *px = &row_pointers[y][x * 4]; - unsigned char r = px[0]; - unsigned char g = px[1]; - unsigned char b = px[2]; + unsigned char r = px[0], g = px[1], b = px[2]; /* Improved color ranges (handles light blue water better) */ if (b > 120 && (b > g || b > r))