# png.cpp

Go to the documentation of this file.

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