src/spriteloader/png.cpp
author rubidium
Thu, 15 Nov 2007 18:07:30 +0000
changeset 7887 b72ef3f45a38
parent 7570 5d5d9b6af0ef
child 8037 8aa4ace04383
permissions -rw-r--r--
(svn r11438) -Change: do not create shores in canyons. Based on a patch by LeviathNL.
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
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
     3
/** @file grf.cpp */
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"
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
     8
#include "../gfx.h"
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 "../variables.h"
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    11
#include "../debug.h"
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    12
#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
    13
#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
    14
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    15
#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
    16
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    17
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
    18
{
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    19
	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
    20
}
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
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
    23
{
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    24
	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
    25
	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
    26
}
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
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
    29
{
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    30
	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
    31
}
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
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
    34
{
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    35
	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
    36
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    37
	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
    38
	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
    39
		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
    40
		return true;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    41
	}
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
	/* 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
    44
	return false;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    45
}
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
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
    48
{
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    49
	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
    50
	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
    51
	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
    52
	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
    53
	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
    54
	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
    55
	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
    56
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    57
	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
    58
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    59
	/* 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
    60
	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
    61
	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
    62
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    63
	/* 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
    64
	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
    65
	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
    66
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    67
	/* 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
    68
	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
    69
	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
    70
		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
    71
		return false;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    72
	}
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    73
	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
    74
	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
    75
		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
    76
		return false;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    77
	}
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
	/* 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
    80
	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
    81
		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
    82
		return false;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    83
	}
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
	/* 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
    86
	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
    87
	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
    88
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    89
	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
    90
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    91
	if (!mask) {
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    92
		/* 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
    93
		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
    94
		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
    95
		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
    96
		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
    97
		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
    98
			/* 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
    99
			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
   100
			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
   101
		}
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
		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
   104
		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
   105
		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
   106
	}
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
	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
   109
	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
   110
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   111
	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
   112
		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
   113
		return true;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   114
	}
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
	if (!mask) {
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   117
		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
   118
6907
f4409419df57 (svn r10156) -Fix: PNG loader can now load 8bpp palette images and greyscale images
truelight
parents: 6896
diff changeset
   119
		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
   120
			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
   121
			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
   122
		}
f4409419df57 (svn r10156) -Fix: PNG loader can now load 8bpp palette images and greyscale images
truelight
parents: 6896
diff changeset
   123
		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
   124
			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
   125
			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
   126
		}
6896
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   127
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   128
#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
   129
		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
   130
#else
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   131
		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
   132
#endif
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   133
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   134
		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
   135
#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
   136
			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
   137
#else
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   138
			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
   139
#endif
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   140
		}
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
		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
   143
	} else {
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   144
		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
   145
	}
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
	row_pointer = (png_byte *)malloc(info_ptr->width * pixelsize);
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   148
	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
   149
		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
   150
		return false;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   151
	}
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
	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
   154
		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
   155
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   156
		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
   157
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   158
		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
   159
			if (mask) {
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   160
				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
   161
					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
   162
					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
   163
					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
   164
					/* 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
   165
					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
   166
				}
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   167
			} else {
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   168
				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
   169
				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
   170
				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
   171
				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
   172
				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
   173
			}
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
	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
   178
	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
   179
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   180
	return true;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   181
}
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   182
7570
5d5d9b6af0ef (svn r11095) -Codechange: don't abuse 'file_pos' by storing the file_slot in it too, but use a nice seperate variable for it
truelight
parents: 7098
diff changeset
   183
bool SpriteLoaderPNG::LoadSprite(SpriteLoader::Sprite *sprite, const char *filename, 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
   184
{
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   185
	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
   186
	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
   187
	return true;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   188
}
7098
02887279c5fe (svn r10365) -Fix: compiling without png and networking support under MSVC.
rubidium
parents: 6907
diff changeset
   189
02887279c5fe (svn r10365) -Fix: compiling without png and networking support under MSVC.
rubidium
parents: 6907
diff changeset
   190
#endif /* WITH_PNG */