# png.cpp

Go to the documentation of this file.

    00001 /* $Id: png.cpp 13686 2008-07-09 19:13:21Z rubidium $ */
    00002
    00005 #ifdef WITH_PNG
    00006
    00007 #include "../stdafx.h"
    00008 #include "../gfx_func.h"
    00009 #include "../fileio.h"
    00010 #include "../debug.h"
    00011 #include "../core/alloc_func.hpp"
    00012 #include "png.hpp"
    00013 #include <png.h>
    00014
    00015 #define PNG_SLOT 62
    00016
    00017 static void PNGAPI png_my_read(png_structp png_ptr, png_bytep data, png_size_t length)
    00018 {
    00019   FioReadBlock(data, length);
    00020 }
    00021
    00022 static void PNGAPI png_my_error(png_structp png_ptr, png_const_charp message)
    00023 {
    00024   DEBUG(sprite, 0, "ERROR (libpng): %s - %s", message, (char *)png_get_error_ptr(png_ptr));
    00025   longjmp(png_ptr->jmpbuf, 1);
    00026 }
    00027
    00028 static void PNGAPI png_my_warning(png_structp png_ptr, png_const_charp message)
    00029 {
    00030   DEBUG(sprite, 0, "WARNING (libpng): %s - %s", message, (char *)png_get_error_ptr(png_ptr));
    00031 }
    00032
    00033 static bool OpenPNGFile(const char *filename, uint32 id, bool mask)
    00034 {
    00035   char png_file[MAX_PATH];
    00036
    00037   snprintf(png_file, sizeof(png_file), "sprites" PATHSEP "%s" PATHSEP "%d%s.png", filename, id, mask ? "m" : "");
    00038   if (FioCheckFileExists(png_file)) {
    00039     FioOpenFile(PNG_SLOT, png_file);
    00040     return true;
    00041   }
    00042
    00043   /* TODO -- Add TAR support */
    00044   return false;
    00045 }
    00046
    00047 static bool LoadPNG(SpriteLoader::Sprite *sprite, const char *filename, uint32 id, volatile bool mask)
    00048 {
    00049   png_byte header[8];
    00050   png_structp png_ptr;
    00051   png_infop info_ptr, end_info;
    00052   uint bit_depth, color_type;
    00053   uint i, pixelsize;
    00054   png_bytep row_pointer;
    00055   SpriteLoader::CommonPixel *dst;
    00056
    00057   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
    00058
    00059   /* Check the header */
    00060   FioReadBlock(header, 8);
    00061   if (png_sig_cmp(header, 0, 8) != 0) return false;
    00062
    00063   /* Create the reader */
    00064   png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL, png_my_error, png_my_warning);
    00065   if (png_ptr == NULL) return false;
    00066
    00067   /* Create initial stuff */
    00068   info_ptr = png_create_info_struct(png_ptr);
    00069   if (info_ptr == NULL) {
    00070     png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
    00071     return false;
    00072   }
    00073   end_info = png_create_info_struct(png_ptr);
    00074   if (end_info == NULL) {
    00075     png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
    00076     return false;
    00077   }
    00078
    00079   /* Make sure that upon error, we can clean up graceful */
    00080   if (setjmp(png_jmpbuf(png_ptr))) {
    00081     png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
    00082     return false;
    00083   }
    00084
    00085   /* Read the file */
    00086   png_set_read_fn(png_ptr, NULL, png_my_read);
    00087   png_set_sig_bytes(png_ptr, 8);
    00088
    00089   png_read_info(png_ptr, info_ptr);
    00090
    00091   if (!mask) {
    00092     /* Read the text chunks */
    00093     png_textp text_ptr;
    00094     int num_text = 0;
    00095     png_get_text(png_ptr, info_ptr, &text_ptr, &num_text);
    00096     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);
    00097     for (int i = 0; i < num_text; i++) {
    00098       /* x_offs and y_offs are in the text-chunk of PNG */
    00099       if (strcmp("x_offs", text_ptr[i].key) == 0) sprite->x_offs = strtol(text_ptr[i].text, NULL, 0);
    00100       if (strcmp("y_offs", text_ptr[i].key) == 0) sprite->y_offs = strtol(text_ptr[i].text, NULL, 0);
    00101     }
    00102
    00103     sprite->height = info_ptr->height;
    00104     sprite->width  = info_ptr->width;
    00105     sprite->data = CallocT<SpriteLoader::CommonPixel>(sprite->width * sprite->height);
    00106   }
    00107
    00108   bit_depth  = png_get_bit_depth(png_ptr, info_ptr);
    00109   color_type = png_get_color_type(png_ptr, info_ptr);
    00110
    00111   if (mask && (bit_depth != 8 || color_type != PNG_COLOR_TYPE_PALETTE)) {
    00112     DEBUG(misc, 0, "Ignoring mask for SpriteID %d as it isn't a 8 bit palette image", id);
    00113     return true;
    00114   }
    00115
    00116   if (!mask) {
    00117     if (bit_depth == 16) png_set_strip_16(png_ptr);
    00118
    00119     if (color_type == PNG_COLOR_TYPE_PALETTE) {
    00120       png_set_palette_to_rgb(png_ptr);
    00121       color_type = PNG_COLOR_TYPE_RGB;
    00122     }
    00123     if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
    00124       png_set_gray_to_rgb(png_ptr);
    00125       color_type = PNG_COLOR_TYPE_RGB;
    00126     }
    00127
    00128     if (color_type == PNG_COLOR_TYPE_RGB) {
    00129       png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
    00130     }
    00131
    00132     pixelsize = sizeof(uint32);
    00133   } else {
    00134     pixelsize = sizeof(uint8);
    00135   }
    00136
    00137   row_pointer = (png_byte *)MallocT<byte>(info_ptr->width * pixelsize);
    00138   if (row_pointer == NULL) {
    00139     png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
    00140     return false;
    00141   }
    00142
    00143   for (i = 0; i < info_ptr->height; i++) {
    00144     png_read_row(png_ptr, row_pointer, NULL);
    00145
    00146     dst = sprite->data + i * info_ptr->width;
    00147
    00148     for (uint x = 0; x < info_ptr->width; x++) {
    00149       if (mask) {
    00150         if (row_pointer[x * sizeof(uint8)] != 0) {
    00151           dst[x].r = 0;
    00152           dst[x].g = 0;
    00153           dst[x].b = 0;
    00154           /* Alpha channel is used from the original image (to allow transparency in remap colors) */
    00155           dst[x].m = row_pointer[x * sizeof(uint8)];
    00156         }
    00157       } else {
    00158         dst[x].r = row_pointer[x * sizeof(uint32) + 0];
    00159         dst[x].g = row_pointer[x * sizeof(uint32) + 1];
    00160         dst[x].b = row_pointer[x * sizeof(uint32) + 2];
    00161         dst[x].a = row_pointer[x * sizeof(uint32) + 3];
    00162         dst[x].m = 0;
    00163       }
    00164     }
    00165   }
    00166
    00167   free(row_pointer);
    00168   png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
    00169
    00170   return true;
    00171 }
    00172
    00173 bool SpriteLoaderPNG::LoadSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, uint32 file_pos)
    00174 {
    00175   const char *filename = FioGetFilename(file_slot);
    00176   if (!LoadPNG(sprite, filename, file_pos, false)) return false;
    00177   if (!LoadPNG(sprite, filename, file_pos, true)) return false;
    00178   return true;
    00179 }
    00180
    00181 #endif /* WITH_PNG */