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