src/spriteloader/png.cpp
author smatz
Sun, 17 Feb 2008 17:00:43 +0000
changeset 8586 cb0e7e00f1d2
parent 8374 7a1b6c89cb89
child 8757 50c0e625f849
permissions -rw-r--r--
(svn r12167) -Feature(tte): change colour of autorail and autoroad selection when Ctrl is pressed
6896
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
     1
/* $Id$ */
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
     2
8348
4d7c1c5055b3 (svn r11914) -Documentation: fix some @file statement
glx
parents: 8142
diff changeset
     3
/** @file png.cpp */
6896
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
     4
7098
02887279c5fe (svn r10365) -Fix: compiling without png and networking support under MSVC.
rubidium
parents: 6907
diff changeset
     5
#ifdef WITH_PNG
02887279c5fe (svn r10365) -Fix: compiling without png and networking support under MSVC.
rubidium
parents: 6907
diff changeset
     6
6896
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
     7
#include "../stdafx.h"
8123
ce31d2843a95 (svn r11684) -Codechange: split gfx.h in a type and functional header.
rubidium
parents: 8037
diff changeset
     8
#include "../gfx_func.h"
6896
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
     9
#include "../fileio.h"
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    10
#include "../debug.h"
8130
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8123
diff changeset
    11
#include "../core/alloc_func.hpp"
8132
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8130
diff changeset
    12
#include "../core/endian_func.hpp"
6896
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    13
#include "png.hpp"
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    14
#include <png.h>
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    15
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    16
#define PNG_SLOT 62
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    17
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    18
static void PNGAPI png_my_read(png_structp png_ptr, png_bytep data, png_size_t length)
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    19
{
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    20
	FioReadBlock(data, length);
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    21
}
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    22
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    23
static void PNGAPI png_my_error(png_structp png_ptr, png_const_charp message)
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    24
{
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    25
	DEBUG(sprite, 0, "ERROR (libpng): %s - %s", message, (char *)png_get_error_ptr(png_ptr));
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    26
	longjmp(png_ptr->jmpbuf, 1);
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    27
}
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    28
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    29
static void PNGAPI png_my_warning(png_structp png_ptr, png_const_charp message)
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    30
{
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    31
	DEBUG(sprite, 0, "WARNING (libpng): %s - %s", message, (char *)png_get_error_ptr(png_ptr));
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    32
}
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    33
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    34
static bool OpenPNGFile(const char *filename, uint32 id, bool mask)
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    35
{
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    36
	char png_file[MAX_PATH];
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    37
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    38
	snprintf(png_file, sizeof(png_file), "sprites" PATHSEP "%s" PATHSEP "%d%s.png", filename, id, mask ? "m" : "");
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    39
	if (FioCheckFileExists(png_file)) {
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    40
		FioOpenFile(PNG_SLOT, png_file);
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    41
		return true;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    42
	}
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    43
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    44
	/* TODO -- Add TAR support */
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    45
	return false;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    46
}
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    47
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    48
static bool LoadPNG(SpriteLoader::Sprite *sprite, const char *filename, uint32 id, bool mask)
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    49
{
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    50
	png_byte header[8];
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    51
	png_structp png_ptr;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    52
	png_infop info_ptr, end_info;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    53
	uint bit_depth, color_type;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    54
	uint i, pixelsize;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    55
	png_bytep row_pointer;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    56
	SpriteLoader::CommonPixel *dst;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    57
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    58
	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
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    59
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    60
	/* Check the header */
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    61
	FioReadBlock(header, 8);
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    62
	if (png_sig_cmp(header, 0, 8) != 0) return false;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    63
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    64
	/* Create the reader */
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    65
	png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL, png_my_error, png_my_warning);
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    66
	if (png_ptr == NULL) return false;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    67
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    68
	/* Create initial stuff */
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    69
	info_ptr = png_create_info_struct(png_ptr);
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    70
	if (info_ptr == NULL) {
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    71
		png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    72
		return false;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    73
	}
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    74
	end_info = png_create_info_struct(png_ptr);
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    75
	if (end_info == NULL) {
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    76
		png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    77
		return false;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    78
	}
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    79
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    80
	/* Make sure that upon error, we can clean up graceful */
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    81
	if (setjmp(png_jmpbuf(png_ptr))) {
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    82
		png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    83
		return false;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    84
	}
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    85
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    86
	/* Read the file */
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    87
	png_set_read_fn(png_ptr, NULL, png_my_read);
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    88
	png_set_sig_bytes(png_ptr, 8);
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    89
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    90
	png_read_info(png_ptr, info_ptr);
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    91
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    92
	if (!mask) {
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    93
		/* Read the text chunks */
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    94
		png_textp text_ptr;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    95
		int num_text = 0;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    96
		png_get_text(png_ptr, info_ptr, &text_ptr, &num_text);
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    97
		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);
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    98
		for (int i = 0; i < num_text; i++) {
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    99
			/* x_offs and y_offs are in the text-chunk of PNG */
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   100
			if (strcmp("x_offs", text_ptr[i].key) == 0) sprite->x_offs = strtol(text_ptr[i].text, NULL, 0);
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   101
			if (strcmp("y_offs", text_ptr[i].key) == 0) sprite->y_offs = strtol(text_ptr[i].text, NULL, 0);
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   102
		}
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   103
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   104
		sprite->height = info_ptr->height;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   105
		sprite->width  = info_ptr->width;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   106
		sprite->data = CallocT<SpriteLoader::CommonPixel>(sprite->width * sprite->height);
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   107
	}
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   108
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   109
	bit_depth  = png_get_bit_depth(png_ptr, info_ptr);
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   110
	color_type = png_get_color_type(png_ptr, info_ptr);
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   111
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   112
	if (mask && (bit_depth != 8 || color_type != PNG_COLOR_TYPE_PALETTE)) {
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   113
		DEBUG(misc, 0, "Ignoring mask for SpriteID %d as it isn't a 8 bit palette image", id);
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   114
		return true;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   115
	}
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   116
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   117
	if (!mask) {
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   118
		if (bit_depth == 16) png_set_strip_16(png_ptr);
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   119
6907
f4409419df57 (svn r10156) -Fix: PNG loader can now load 8bpp palette images and greyscale images
truelight
parents: 6896
diff changeset
   120
		if (color_type == PNG_COLOR_TYPE_PALETTE) {
f4409419df57 (svn r10156) -Fix: PNG loader can now load 8bpp palette images and greyscale images
truelight
parents: 6896
diff changeset
   121
			png_set_palette_to_rgb(png_ptr);
f4409419df57 (svn r10156) -Fix: PNG loader can now load 8bpp palette images and greyscale images
truelight
parents: 6896
diff changeset
   122
			color_type = PNG_COLOR_TYPE_RGB;
f4409419df57 (svn r10156) -Fix: PNG loader can now load 8bpp palette images and greyscale images
truelight
parents: 6896
diff changeset
   123
		}
f4409419df57 (svn r10156) -Fix: PNG loader can now load 8bpp palette images and greyscale images
truelight
parents: 6896
diff changeset
   124
		if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
f4409419df57 (svn r10156) -Fix: PNG loader can now load 8bpp palette images and greyscale images
truelight
parents: 6896
diff changeset
   125
			png_set_gray_to_rgb(png_ptr);
f4409419df57 (svn r10156) -Fix: PNG loader can now load 8bpp palette images and greyscale images
truelight
parents: 6896
diff changeset
   126
			color_type = PNG_COLOR_TYPE_RGB;
f4409419df57 (svn r10156) -Fix: PNG loader can now load 8bpp palette images and greyscale images
truelight
parents: 6896
diff changeset
   127
		}
6896
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   128
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   129
#ifdef TTD_LITTLE_ENDIAN
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   130
		png_set_bgr(png_ptr);
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   131
#else
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   132
		if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) png_set_swap_alpha(png_ptr);
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   133
#endif
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   134
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   135
		if (color_type == PNG_COLOR_TYPE_RGB) {
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   136
#ifdef TTD_LITTLE_ENDIAN
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   137
			png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   138
#else
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   139
			png_set_filler(png_ptr, 0xff, PNG_FILLER_BEFORE);
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   140
#endif
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   141
		}
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   142
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   143
		pixelsize = sizeof(uint32);
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   144
	} else {
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   145
		pixelsize = sizeof(uint8);
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   146
	}
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   147
8037
8aa4ace04383 (svn r11597) -Change: replace all remaining instances of (re|m|c)alloc with (Re|M|C)allocT and add a check for out-of-memory situations to the *allocT functions.
rubidium
parents: 7570
diff changeset
   148
	row_pointer = (png_byte *)MallocT<byte>(info_ptr->width * pixelsize);
6896
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   149
	if (row_pointer == NULL) {
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   150
		png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   151
		return false;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   152
	}
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   153
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   154
	for (i = 0; i < info_ptr->height; i++) {
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   155
		png_read_row(png_ptr, row_pointer, NULL);
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   156
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   157
		dst = sprite->data + i * info_ptr->width;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   158
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   159
		for (uint x = 0; x < info_ptr->width; x++) {
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   160
			if (mask) {
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   161
				if (row_pointer[x * sizeof(uint8)] != 0) {
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   162
					dst[x].b = 0;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   163
					dst[x].g = 0;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   164
					dst[x].r = 0;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   165
					/* Alpha channel is used from the original image (to allow transparency in remap colors) */
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   166
					dst[x].m = row_pointer[x * sizeof(uint8)];
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   167
				}
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   168
			} else {
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   169
				dst[x].b = row_pointer[x * sizeof(uint32) + 0];
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   170
				dst[x].g = row_pointer[x * sizeof(uint32) + 1];
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   171
				dst[x].r = row_pointer[x * sizeof(uint32) + 2];
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   172
				dst[x].a = row_pointer[x * sizeof(uint32) + 3];
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   173
				dst[x].m = 0;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   174
			}
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   175
		}
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   176
	}
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   177
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   178
	free(row_pointer);
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   179
	png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   180
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   181
	return true;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   182
}
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   183
8374
7a1b6c89cb89 (svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.
peter1138
parents: 8348
diff changeset
   184
bool SpriteLoaderPNG::LoadSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, uint32 file_pos)
6896
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   185
{
8374
7a1b6c89cb89 (svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.
peter1138
parents: 8348
diff changeset
   186
	const char *filename = FioGetFilename(file_slot);
6896
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   187
	if (!LoadPNG(sprite, filename, file_pos, false)) return false;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   188
	if (!LoadPNG(sprite, filename, file_pos, true)) return false;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   189
	return true;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   190
}
7098
02887279c5fe (svn r10365) -Fix: compiling without png and networking support under MSVC.
rubidium
parents: 6907
diff changeset
   191
02887279c5fe (svn r10365) -Fix: compiling without png and networking support under MSVC.
rubidium
parents: 6907
diff changeset
   192
#endif /* WITH_PNG */