# 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)
    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.png", sep, filename, id, mask ? "m" : "");
    00045   if (FioCheckFileExists(png_file, NEWGRF_DIR)) {
    00046     FioOpenFile(PNG_SLOT, png_file, NEWGRF_DIR);
    00047     return true;
    00048   }
    00049
    00050   /* TODO -- Add TAR support */
    00051   return false;
    00052 }
    00053
    00054 static bool LoadPNG(SpriteLoader::Sprite *sprite, const char *filename, uint32 id, volatile bool mask)
    00055 {
    00056   png_byte header[8];
    00057   png_structp png_ptr;
    00058   png_infop info_ptr, end_info;
    00059   uint bit_depth, colour_type;
    00060   uint i, pixelsize;
    00061   SpriteLoader::CommonPixel *dst;
    00062
    00063   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
    00064
    00065   /* Check the header */
    00066   FioReadBlock(header, 8);
    00067   if (png_sig_cmp(header, 0, 8) != 0) return false;
    00068
    00069   /* Create the reader */
    00070   png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL, png_my_error, png_my_warning);
    00071   if (png_ptr == NULL) return false;
    00072
    00073   /* Create initial stuff */
    00074   info_ptr = png_create_info_struct(png_ptr);
    00075   if (info_ptr == NULL) {
    00076     png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
    00077     return false;
    00078   }
    00079   end_info = png_create_info_struct(png_ptr);
    00080   if (end_info == NULL) {
    00081     png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
    00082     return false;
    00083   }
    00084
    00085   /* Make sure that upon error, we can clean up graceful */
    00086   if (setjmp(png_jmpbuf(png_ptr))) {
    00087     png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
    00088     return false;
    00089   }
    00090
    00091   /* Read the file */
    00092   png_set_read_fn(png_ptr, NULL, png_my_read);
    00093   png_set_sig_bytes(png_ptr, 8);
    00094
    00095   png_read_info(png_ptr, info_ptr);
    00096
    00097   if (!mask) {
    00098     /* Read the text chunks */
    00099     png_textp text_ptr;
    00100     int num_text = 0;
    00101     png_get_text(png_ptr, info_ptr, &text_ptr, &num_text);
    00102     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);
    00103     for (int i = 0; i < num_text; i++) {
    00104       /* x_offs and y_offs are in the text-chunk of PNG */
    00105       if (strcmp("x_offs", text_ptr[i].key) == 0) sprite->x_offs = strtol(text_ptr[i].text, NULL, 0);
    00106       if (strcmp("y_offs", text_ptr[i].key) == 0) sprite->y_offs = strtol(text_ptr[i].text, NULL, 0);
    00107     }
    00108
    00109     uint height = png_get_image_height(png_ptr, info_ptr);
    00110     uint width  = png_get_image_width(png_ptr, info_ptr);
    00111     /* Check if sprite dimensions aren't larger than what is allowed in GRF-files. */
    00112     if (height > UINT8_MAX || width > UINT16_MAX) {
    00113       png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
    00114       return false;
    00115     }
    00116     sprite->height = height;
    00117     sprite->width  = width;
    00118     sprite->AllocateData(sprite->width * sprite->height * ZOOM_LVL_BASE * ZOOM_LVL_BASE);
    00119   } else if (sprite->height != png_get_image_height(png_ptr, info_ptr) || sprite->width != png_get_image_width(png_ptr, info_ptr)) {
    00120     /* Make sure the mask image isn't larger than the sprite image. */
    00121     DEBUG(misc, 0, "Ignoring mask for SpriteID %d as it isn't the same dimension as the masked sprite", id);
    00122     png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
    00123     return true;
    00124   }
    00125
    00126   bit_depth  = png_get_bit_depth(png_ptr, info_ptr);
    00127   colour_type = png_get_color_type(png_ptr, info_ptr);
    00128
    00129   if (mask && (bit_depth != 8 || colour_type != PNG_COLOR_TYPE_PALETTE)) {
    00130     DEBUG(misc, 0, "Ignoring mask for SpriteID %d as it isn't a 8 bit palette image", id);
    00131     png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
    00132     return true;
    00133   }
    00134
    00135   bool win_palette = false;
    00136   if (!mask) {
    00137     if (bit_depth == 16) png_set_strip_16(png_ptr);
    00138
    00139     if (colour_type == PNG_COLOR_TYPE_PALETTE) {
    00140       png_set_palette_to_rgb(png_ptr);
    00141       colour_type = PNG_COLOR_TYPE_RGB;
    00142     }
    00143     if (colour_type == PNG_COLOR_TYPE_GRAY || colour_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
    00144       png_set_gray_to_rgb(png_ptr);
    00145       colour_type = PNG_COLOR_TYPE_RGB;
    00146     }
    00147
    00148     if (colour_type == PNG_COLOR_TYPE_RGB) {
    00149       if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
    00150         /* Create an alpha channel when there is a tRNS chunk */
    00151         png_set_tRNS_to_alpha(png_ptr);
    00152       } else {
    00153         png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
    00154       }
    00155     }
    00156
    00157     pixelsize = sizeof(uint32);
    00158   } else {
    00159     pixelsize = sizeof(uint8);
    00160
    00161     /* Check whether the DOS greyscale is in place. If not, we assume WIN palette */
    00162     png_colorp palette;
    00163     int num_palette;
    00164     if (png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette) && num_palette == 256) {
    00165       for (uint i = 3; i <= 5; i++) {
    00166         /* Allow differences between -4 and +4 */
    00167         uint8 intensity = i * 16 - 4;
    00168         uint8 diff_r = palette[i].red - intensity;
    00169         uint8 diff_g = palette[i].green - intensity;
    00170         uint8 diff_b = palette[i].blue - intensity;
    00171         if (diff_r > 8 || diff_g > 8 || diff_b > 8) {
    00172           win_palette = true;
    00173           break;
    00174         }
    00175       }
    00176     }
    00177   }
    00178
    00179   png_bytep row_pointer = AllocaM(png_byte, png_get_image_width(png_ptr, info_ptr) * pixelsize);
    00180
    00181   for (i = 0; i < png_get_image_height(png_ptr, info_ptr); i++) {
    00182     png_read_row(png_ptr, row_pointer, NULL);
    00183
    00184     dst = sprite->data + i * png_get_image_width(png_ptr, info_ptr);
    00185
    00186     for (uint x = 0; x < png_get_image_width(png_ptr, info_ptr); x++) {
    00187       if (mask) {
    00188         if (row_pointer[x * sizeof(uint8)] != 0) {
    00189           /* Alpha channel is used from the original image (to allow transparency in remap colours) */
    00190           extern const byte _palmap_w2d[];
    00191           byte colour = row_pointer[x * sizeof(uint8)];
    00192           dst[x].m = win_palette ? _palmap_w2d[colour] : colour;
    00193         }
    00194       } else {
    00195         dst[x].r = row_pointer[x * sizeof(uint32) + 0];
    00196         dst[x].g = row_pointer[x * sizeof(uint32) + 1];
    00197         dst[x].b = row_pointer[x * sizeof(uint32) + 2];
    00198         dst[x].a = row_pointer[x * sizeof(uint32) + 3];
    00199         dst[x].m = 0;
    00200       }
    00201     }
    00202   }
    00203
    00204   png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
    00205
    00206   return true;
    00207 }
    00208
    00209 bool SpriteLoaderPNG::LoadSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_pos, SpriteType sprite_type)
    00210 {
    00211   const char *filename = FioGetFilename(file_slot);
    00212   if (!LoadPNG(sprite, filename, (uint32)file_pos, false)) return false;
    00213   if (!LoadPNG(sprite, filename, (uint32)file_pos, true)) return false;
    00214
    00215   if (ZOOM_LVL_BASE != 1 && sprite_type == ST_NORMAL) {
    00216     /* Simple scaling, back-to-front so that no intermediate buffers are needed. */
    00217     int width  = sprite->width  * ZOOM_LVL_BASE;
    00218     int height = sprite->height * ZOOM_LVL_BASE;
    00219     for (int y = height - 1; y >= 0; y--) {
    00220       for (int x = width - 1; x >= 0; x--) {
    00221         sprite->data[y * width + x] = sprite->data[y / ZOOM_LVL_BASE * sprite->width + x / ZOOM_LVL_BASE];
    00222       }
    00223     }
    00224
    00225     sprite->width  *= ZOOM_LVL_BASE;
    00226     sprite->height *= ZOOM_LVL_BASE;
    00227     sprite->x_offs *= ZOOM_LVL_BASE;
    00228     sprite->y_offs *= ZOOM_LVL_BASE;
    00229   }
    00230
    00231   return true;
    00232 }
    00233
    00234 #endif /* WITH_PNG */