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 "../fileio_func.h"
00016 #include "../debug.h"
00017 #include "png.hpp"
00018 #include <png.h>
00019 
00020 #define PNG_SLOT 62
00021 
00022 static void PNGAPI png_my_read(png_structp png_ptr, png_bytep data, png_size_t length)
00023 {
00024   FioReadBlock(data, length);
00025 }
00026 
00027 static void PNGAPI png_my_error(png_structp png_ptr, png_const_charp message)
00028 {
00029   DEBUG(sprite, 0, "ERROR (libpng): %s - %s", message, (char *)png_get_error_ptr(png_ptr));
00030   longjmp(png_jmpbuf(png_ptr), 1);
00031 }
00032 
00033 static void PNGAPI png_my_warning(png_structp png_ptr, png_const_charp message)
00034 {
00035   DEBUG(sprite, 0, "WARNING (libpng): %s - %s", message, (char *)png_get_error_ptr(png_ptr));
00036 }
00037 
00038 static bool OpenPNGFile(const char *filename, uint32 id, bool mask, ZoomLevel zoom)
00039 {
00040   char png_file[MAX_PATH];
00041 
00042   /* Add path separator after 'sprites' if not present */
00043   const char *sep = (filename[0] == PATHSEPCHAR) ? "" : PATHSEP;
00044   snprintf(png_file, sizeof(png_file), "sprites%s%s" PATHSEP "%d_%s%d%s.png", sep, filename, id, "z",zoom,mask ? "m" : "");
00045   if (FioCheckFileExists(png_file)) {
00046     FioOpenFile(PNG_SLOT, png_file);
00047     return true;
00048   }
00049   /* if failed, try to find it in the trunk tars, only for default zoom */
00050   if ( zoom == ZOOM_LVL_NORMAL) {
00051     snprintf(png_file, sizeof(png_file), "sprites%s%s" PATHSEP "%d%s.png", sep, filename, id, mask ? "m" : "");
00052     if (FioCheckFileExists(png_file)) {
00053       FioOpenFile(PNG_SLOT, png_file);
00054       return true;
00055     }
00056   }
00057   /* TODO -- Add TAR support */
00058   return false;
00059 }
00060 
00061 static bool LoadPNG(SpriteLoader::Sprite *sprite, const char *filename, uint32 id, volatile bool mask, ZoomLevel zoom)
00062 {
00063   png_byte header[8];
00064   png_structp png_ptr;
00065   png_infop info_ptr, end_info;
00066   uint bit_depth, colour_type;
00067   uint i, pixelsize;
00068   SpriteLoader::CommonPixel *dst;
00069 
00070   if (!OpenPNGFile(filename, id, mask, zoom)) return mask; // If mask is true, and file not found, continue true anyway, as it isn't a show-stopper
00071 
00072   /* Check the header */
00073   FioReadBlock(header, 8);
00074   if (png_sig_cmp(header, 0, 8) != 0) return false;
00075 
00076   /* Create the reader */
00077   png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL, png_my_error, png_my_warning);
00078   if (png_ptr == NULL) return false;
00079 
00080   /* Create initial stuff */
00081   info_ptr = png_create_info_struct(png_ptr);
00082   if (info_ptr == NULL) {
00083     png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
00084     return false;
00085   }
00086   end_info = png_create_info_struct(png_ptr);
00087   if (end_info == NULL) {
00088     png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
00089     return false;
00090   }
00091 
00092   /* Make sure that upon error, we can clean up graceful */
00093   if (setjmp(png_jmpbuf(png_ptr))) {
00094     png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
00095     return false;
00096   }
00097 
00098   /* Read the file */
00099   png_set_read_fn(png_ptr, NULL, png_my_read);
00100   png_set_sig_bytes(png_ptr, 8);
00101 
00102   png_read_info(png_ptr, info_ptr);
00103 
00104   if (!mask) {
00105     /* Read the text chunks */
00106     png_textp text_ptr;
00107     int num_text = 0;
00108     png_get_text(png_ptr, info_ptr, &text_ptr, &num_text);
00109     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);
00110     for (int i = 0; i < num_text; i++) {
00111       /* x_offs and y_offs are in the text-chunk of PNG */
00112       if (strcmp("x_offs", text_ptr[i].key) == 0) sprite->x_offs = strtol(text_ptr[i].text, NULL, 0);
00113       if (strcmp("y_offs", text_ptr[i].key) == 0) sprite->y_offs = strtol(text_ptr[i].text, NULL, 0);
00114     }
00115 
00116     sprite->height = png_get_image_height(png_ptr, info_ptr);
00117     sprite->width  = png_get_image_width(png_ptr, info_ptr);
00118     sprite->AllocateData(sprite->width * sprite->height);
00119   }
00120 
00121   bit_depth  = png_get_bit_depth(png_ptr, info_ptr);
00122   colour_type = png_get_color_type(png_ptr, info_ptr);
00123 
00124   if (mask && colour_type != PNG_COLOR_TYPE_PALETTE) {
00125     DEBUG(misc, 0, "Ignoring mask for SpriteID %d as it isn't a 8 bit palette image", id);
00126     return true;
00127   }
00128   /* Convert mask file with bit depth of 1, 2 or 4 into 8 bits */
00129   if (mask && bit_depth < 8 && colour_type == PNG_COLOR_TYPE_PALETTE) {
00130     png_set_packing(png_ptr);
00131   }
00132 
00133   if (!mask) {
00134     if (bit_depth == 16) png_set_strip_16(png_ptr);
00135 
00136     if (colour_type == PNG_COLOR_TYPE_PALETTE) {
00137       png_set_palette_to_rgb(png_ptr);
00138       colour_type = PNG_COLOR_TYPE_RGB;
00139     }
00140     if (colour_type == PNG_COLOR_TYPE_GRAY || colour_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
00141       png_set_gray_to_rgb(png_ptr);
00142       colour_type = PNG_COLOR_TYPE_RGB;
00143     }
00144 
00145     if (colour_type == PNG_COLOR_TYPE_RGB) {
00146       if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
00147         /* Create an alpha channel when there is a tRNS chunk */
00148         png_set_tRNS_to_alpha(png_ptr);
00149       } else {
00150         png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
00151       }
00152     }
00153 
00154     pixelsize = sizeof(uint32);
00155   } else {
00156     pixelsize = sizeof(uint8);
00157   }
00158 
00159   png_bytep row_pointer = AllocaM(png_byte, png_get_image_width(png_ptr, info_ptr) * pixelsize);
00160 
00161   for (i = 0; i < png_get_image_height(png_ptr, info_ptr); i++) {
00162     png_read_row(png_ptr, row_pointer, NULL);
00163 
00164     dst = sprite->data + i * png_get_image_width(png_ptr, info_ptr);
00165 
00166     for (uint x = 0; x < png_get_image_width(png_ptr, info_ptr); x++) {
00167       if (mask) {
00168         if (row_pointer[x * sizeof(uint8)] != 0) {
00169           /* GeekToo: dont overwrite sprite rgb data if mask is present */
00170           /* Alpha channel is used from the original image (to allow transparency in remap colours) */
00171           /* Force the value of the palette if it is not in the correct range */
00172           if (bit_depth < 8 && (row_pointer[x * sizeof(uint8)] < 196 || row_pointer[x * sizeof(uint8)] > 203)) {
00173             dst[x].m = 199;
00174           } else {
00175             dst[x].m = row_pointer[x * sizeof(uint8)];
00176           }
00177         }
00178       } else {
00179         dst[x].r = row_pointer[x * sizeof(uint32) + 0];
00180         dst[x].g = row_pointer[x * sizeof(uint32) + 1];
00181         dst[x].b = row_pointer[x * sizeof(uint32) + 2];
00182         dst[x].a = row_pointer[x * sizeof(uint32) + 3];
00183         dst[x].m = 0;
00184       }
00185     }
00186   }
00187 
00188   png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
00189 
00190   return true;
00191 }
00192 
00193 bool SpriteLoaderPNG::LoadSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_pos, SpriteType sprite_type, ZoomLevel zoom)
00194 {
00195   const char *filename = FioGetFilename(file_slot);
00196 
00197   if (!LoadPNG(sprite, filename, (uint32)file_pos, false, zoom)) return false;
00198   if (!LoadPNG(sprite, filename, (uint32)file_pos, true, zoom)) return false;
00199 
00200   return true;
00201 }
00202 
00203 #endif /* WITH_PNG */

Generated on Thu Apr 14 00:48:20 2011 for OpenTTD by  doxygen 1.6.1