richk@6720: /* $Id$ */ richk@6720: richk@10724: /** @file png.cpp Reading sprites from png files. */ richk@6720: richk@6720: #ifdef WITH_PNG richk@6720: richk@6720: #include "../stdafx.h" rubidium@6872: #include "../gfx_func.h" richk@6720: #include "../fileio.h" richk@6720: #include "../debug.h" rubidium@6872: #include "../core/alloc_func.hpp" richk@6720: #include "png.hpp" richk@6720: #include richk@6720: richk@6720: #define PNG_SLOT 62 richk@6720: richk@6720: static void PNGAPI png_my_read(png_structp png_ptr, png_bytep data, png_size_t length) richk@6720: { richk@6720: FioReadBlock(data, length); richk@6720: } richk@6720: richk@6720: static void PNGAPI png_my_error(png_structp png_ptr, png_const_charp message) richk@6720: { richk@6720: DEBUG(sprite, 0, "ERROR (libpng): %s - %s", message, (char *)png_get_error_ptr(png_ptr)); richk@6720: longjmp(png_ptr->jmpbuf, 1); richk@6720: } richk@6720: richk@6720: static void PNGAPI png_my_warning(png_structp png_ptr, png_const_charp message) richk@6720: { richk@6720: DEBUG(sprite, 0, "WARNING (libpng): %s - %s", message, (char *)png_get_error_ptr(png_ptr)); richk@6720: } richk@6720: richk@6720: static bool OpenPNGFile(const char *filename, uint32 id, bool mask) richk@6720: { richk@6720: char png_file[MAX_PATH]; richk@6720: richk@6720: snprintf(png_file, sizeof(png_file), "sprites" PATHSEP "%s" PATHSEP "%d%s.png", filename, id, mask ? "m" : ""); richk@6720: if (FioCheckFileExists(png_file)) { richk@6720: FioOpenFile(PNG_SLOT, png_file); richk@6720: return true; richk@6720: } richk@6720: richk@6720: /* TODO -- Add TAR support */ richk@6720: return false; richk@6720: } richk@6720: richk@10724: static bool LoadPNG(SpriteLoader::Sprite *sprite, const char *filename, uint32 id, volatile bool mask) richk@6720: { richk@6720: png_byte header[8]; richk@6720: png_structp png_ptr; richk@6720: png_infop info_ptr, end_info; richk@6720: uint bit_depth, color_type; richk@6720: uint i, pixelsize; richk@6720: png_bytep row_pointer; richk@6720: SpriteLoader::CommonPixel *dst; richk@6720: richk@6720: if (!OpenPNGFile(filename, id, mask)) return mask; // If mask is true, and file not found, continue true anyway, as it isn't a show-stopper richk@6720: richk@6720: /* Check the header */ richk@6720: FioReadBlock(header, 8); richk@6720: if (png_sig_cmp(header, 0, 8) != 0) return false; richk@6720: richk@6720: /* Create the reader */ richk@6720: png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL, png_my_error, png_my_warning); richk@6720: if (png_ptr == NULL) return false; richk@6720: richk@6720: /* Create initial stuff */ richk@6720: info_ptr = png_create_info_struct(png_ptr); richk@6720: if (info_ptr == NULL) { richk@6720: png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL); richk@6720: return false; richk@6720: } richk@6720: end_info = png_create_info_struct(png_ptr); richk@6720: if (end_info == NULL) { richk@6720: png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); richk@6720: return false; richk@6720: } richk@6720: richk@6720: /* Make sure that upon error, we can clean up graceful */ richk@6720: if (setjmp(png_jmpbuf(png_ptr))) { richk@6720: png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); richk@6720: return false; richk@6720: } richk@6720: richk@6720: /* Read the file */ richk@6720: png_set_read_fn(png_ptr, NULL, png_my_read); richk@6720: png_set_sig_bytes(png_ptr, 8); richk@6720: richk@6720: png_read_info(png_ptr, info_ptr); richk@6720: richk@6720: if (!mask) { richk@6720: /* Read the text chunks */ richk@6720: png_textp text_ptr; richk@6720: int num_text = 0; richk@6720: png_get_text(png_ptr, info_ptr, &text_ptr, &num_text); richk@6720: if (num_text == 0) DEBUG(misc, 0, "Warning: PNG Sprite '%s/%d.png' doesn't have x_offs and y_offs; expect graphical problems", filename, id); richk@6720: for (int i = 0; i < num_text; i++) { richk@6720: /* x_offs and y_offs are in the text-chunk of PNG */ richk@6720: if (strcmp("x_offs", text_ptr[i].key) == 0) sprite->x_offs = strtol(text_ptr[i].text, NULL, 0); richk@6720: if (strcmp("y_offs", text_ptr[i].key) == 0) sprite->y_offs = strtol(text_ptr[i].text, NULL, 0); richk@6720: } richk@6720: richk@6720: sprite->height = info_ptr->height; richk@6720: sprite->width = info_ptr->width; richk@6720: sprite->data = CallocT(sprite->width * sprite->height); richk@6720: } richk@6720: richk@6720: bit_depth = png_get_bit_depth(png_ptr, info_ptr); richk@6720: color_type = png_get_color_type(png_ptr, info_ptr); richk@6720: richk@6720: if (mask && (bit_depth != 8 || color_type != PNG_COLOR_TYPE_PALETTE)) { richk@6720: DEBUG(misc, 0, "Ignoring mask for SpriteID %d as it isn't a 8 bit palette image", id); richk@6720: return true; richk@6720: } richk@6720: richk@6720: if (!mask) { richk@6720: if (bit_depth == 16) png_set_strip_16(png_ptr); richk@6720: richk@6720: if (color_type == PNG_COLOR_TYPE_PALETTE) { richk@6720: png_set_palette_to_rgb(png_ptr); richk@6720: color_type = PNG_COLOR_TYPE_RGB; richk@6720: } richk@6720: if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) { richk@6720: png_set_gray_to_rgb(png_ptr); richk@6720: color_type = PNG_COLOR_TYPE_RGB; richk@6720: } richk@6720: richk@6720: if (color_type == PNG_COLOR_TYPE_RGB) { richk@6720: png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER); richk@6720: } richk@6720: richk@6720: pixelsize = sizeof(uint32); richk@6720: } else { richk@6720: pixelsize = sizeof(uint8); richk@6720: } richk@6720: rubidium@6872: row_pointer = (png_byte *)MallocT(info_ptr->width * pixelsize); richk@6720: if (row_pointer == NULL) { richk@6720: png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); richk@6720: return false; richk@6720: } richk@6720: richk@6720: for (i = 0; i < info_ptr->height; i++) { richk@6720: png_read_row(png_ptr, row_pointer, NULL); richk@6720: richk@6720: dst = sprite->data + i * info_ptr->width; richk@6720: richk@6720: for (uint x = 0; x < info_ptr->width; x++) { richk@6720: if (mask) { richk@6720: if (row_pointer[x * sizeof(uint8)] != 0) { richk@10184: dst[x].r = 0; richk@10184: dst[x].g = 0; richk@6720: dst[x].b = 0; richk@6720: /* Alpha channel is used from the original image (to allow transparency in remap colors) */ richk@6720: dst[x].m = row_pointer[x * sizeof(uint8)]; richk@6720: } richk@6720: } else { richk@10184: dst[x].r = row_pointer[x * sizeof(uint32) + 0]; richk@6720: dst[x].g = row_pointer[x * sizeof(uint32) + 1]; richk@10184: dst[x].b = row_pointer[x * sizeof(uint32) + 2]; richk@6720: dst[x].a = row_pointer[x * sizeof(uint32) + 3]; richk@6720: dst[x].m = 0; richk@6720: } richk@6720: } richk@6720: } richk@6720: richk@6720: free(row_pointer); richk@6720: png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); richk@6720: richk@6720: return true; richk@6720: } richk@6720: richk@10991: bool SpriteLoaderPNG::LoadSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_pos) richk@6720: { rubidium@6872: const char *filename = FioGetFilename(file_slot); richk@10991: if (!LoadPNG(sprite, filename, (uint32)file_pos, false)) return false; richk@10991: if (!LoadPNG(sprite, filename, (uint32)file_pos, true)) return false; richk@6720: return true; richk@6720: } richk@6720: richk@6720: #endif /* WITH_PNG */