png.cpp

Go to the documentation of this file.
00001 /* $Id: png.cpp 15555 2009-02-23 10:50:25Z rubidium $ */
00002 
00005 #ifdef WITH_PNG
00006 
00007 #include "../stdafx.h"
00008 #include "../gfx_func.h"
00009 #include "../fileio_func.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, colour_type;
00053   uint i, pixelsize;
00054   SpriteLoader::CommonPixel *dst;
00055 
00056   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
00057 
00058   /* Check the header */
00059   FioReadBlock(header, 8);
00060   if (png_sig_cmp(header, 0, 8) != 0) return false;
00061 
00062   /* Create the reader */
00063   png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL, png_my_error, png_my_warning);
00064   if (png_ptr == NULL) return false;
00065 
00066   /* Create initial stuff */
00067   info_ptr = png_create_info_struct(png_ptr);
00068   if (info_ptr == NULL) {
00069     png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
00070     return false;
00071   }
00072   end_info = png_create_info_struct(png_ptr);
00073   if (end_info == NULL) {
00074     png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
00075     return false;
00076   }
00077 
00078   /* Make sure that upon error, we can clean up graceful */
00079   if (setjmp(png_jmpbuf(png_ptr))) {
00080     png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
00081     return false;
00082   }
00083 
00084   /* Read the file */
00085   png_set_read_fn(png_ptr, NULL, png_my_read);
00086   png_set_sig_bytes(png_ptr, 8);
00087 
00088   png_read_info(png_ptr, info_ptr);
00089 
00090   if (!mask) {
00091     /* Read the text chunks */
00092     png_textp text_ptr;
00093     int num_text = 0;
00094     png_get_text(png_ptr, info_ptr, &text_ptr, &num_text);
00095     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);
00096     for (int i = 0; i < num_text; i++) {
00097       /* x_offs and y_offs are in the text-chunk of PNG */
00098       if (strcmp("x_offs", text_ptr[i].key) == 0) sprite->x_offs = strtol(text_ptr[i].text, NULL, 0);
00099       if (strcmp("y_offs", text_ptr[i].key) == 0) sprite->y_offs = strtol(text_ptr[i].text, NULL, 0);
00100     }
00101 
00102     sprite->height = info_ptr->height;
00103     sprite->width  = info_ptr->width;
00104     sprite->AllocateData(sprite->width * sprite->height);
00105   }
00106 
00107   bit_depth  = png_get_bit_depth(png_ptr, info_ptr);
00108   colour_type = png_get_color_type(png_ptr, info_ptr);
00109 
00110   if (mask && (bit_depth != 8 || colour_type != PNG_COLOR_TYPE_PALETTE)) {
00111     DEBUG(misc, 0, "Ignoring mask for SpriteID %d as it isn't a 8 bit palette image", id);
00112     return true;
00113   }
00114 
00115   if (!mask) {
00116     if (bit_depth == 16) png_set_strip_16(png_ptr);
00117 
00118     if (colour_type == PNG_COLOR_TYPE_PALETTE) {
00119       png_set_palette_to_rgb(png_ptr);
00120       colour_type = PNG_COLOR_TYPE_RGB;
00121     }
00122     if (colour_type == PNG_COLOR_TYPE_GRAY || colour_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
00123       png_set_gray_to_rgb(png_ptr);
00124       colour_type = PNG_COLOR_TYPE_RGB;
00125     }
00126 
00127     if (colour_type == PNG_COLOR_TYPE_RGB) {
00128       png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
00129     }
00130 
00131     pixelsize = sizeof(uint32);
00132   } else {
00133     pixelsize = sizeof(uint8);
00134   }
00135 
00136   png_bytep row_pointer = AllocaM(png_byte, info_ptr->width * pixelsize);
00137 
00138   for (i = 0; i < info_ptr->height; i++) {
00139     png_read_row(png_ptr, row_pointer, NULL);
00140 
00141     dst = sprite->data + i * info_ptr->width;
00142 
00143     for (uint x = 0; x < info_ptr->width; x++) {
00144       if (mask) {
00145         if (row_pointer[x * sizeof(uint8)] != 0) {
00146           dst[x].r = 0;
00147           dst[x].g = 0;
00148           dst[x].b = 0;
00149           /* Alpha channel is used from the original image (to allow transparency in remap colours) */
00150           dst[x].m = row_pointer[x * sizeof(uint8)];
00151         }
00152       } else {
00153         dst[x].r = row_pointer[x * sizeof(uint32) + 0];
00154         dst[x].g = row_pointer[x * sizeof(uint32) + 1];
00155         dst[x].b = row_pointer[x * sizeof(uint32) + 2];
00156         dst[x].a = row_pointer[x * sizeof(uint32) + 3];
00157         dst[x].m = 0;
00158       }
00159     }
00160   }
00161 
00162   png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
00163 
00164   return true;
00165 }
00166 
00167 bool SpriteLoaderPNG::LoadSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_pos, SpriteType sprite_type)
00168 {
00169   const char *filename = FioGetFilename(file_slot);
00170   if (!LoadPNG(sprite, filename, (uint32)file_pos, false)) return false;
00171   if (!LoadPNG(sprite, filename, (uint32)file_pos, true)) return false;
00172   return true;
00173 }
00174 
00175 #endif /* WITH_PNG */

Generated on Tue Dec 1 00:06:19 2009 for OpenTTD by  doxygen 1.5.6