src/spriteloader/png.cpp
author rubidium
Tue, 16 Dec 2008 17:58:27 +0000
changeset 10426 4a77f7049b5e
parent 10056 48659f7d4fa5
permissions -rw-r--r--
(svn r14679) -Fix [FS#2431]: opening the OSK on the chatbox did disable map scrolling (with keyboard) until another window with editbox was opened and closed. Just "refcount" the open edit boxes instead of setting/clearing a bit when opening/closing a window.
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
9111
48ce04029fe4 (svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents: 8757
diff changeset
     3
/** @file png.cpp Reading sprites from png files. */
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"
10039
1f236afd6cd1 (svn r14199) -Codechange: split fileio.h into fileio_type.h and fileio_func.h so not everything that includes saveload.h needs to include everything else too.
rubidium
parents: 9390
diff changeset
     9
#include "../fileio_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
    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"
6896
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
9151
5ddf1e17f22e (svn r13013) -Fix: GCC 4.3 warning about a clobbering mask due to longjmp. This can't be solved by using exceptions because the longjmp is needed for PNG (C-code) handling.
rubidium
parents: 9111
diff changeset
    47
static bool LoadPNG(SpriteLoader::Sprite *sprite, const char *filename, uint32 id, volatile bool mask)
6896
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
		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
   129
			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
   130
		}
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   131
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   132
		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
   133
	} else {
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   134
		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
   135
	}
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   136
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
   137
	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
   138
	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
   139
		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
   140
		return false;
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
	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
   144
		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
   145
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   146
		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
   147
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   148
		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
   149
			if (mask) {
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   150
				if (row_pointer[x * sizeof(uint8)] != 0) {
8757
50c0e625f849 (svn r12453) -Fix [FS#1880]: Remove broken endian-dependent code and unnecessary rgb to bgr swapping.
peter1138
parents: 8374
diff changeset
   151
					dst[x].r = 0;
50c0e625f849 (svn r12453) -Fix [FS#1880]: Remove broken endian-dependent code and unnecessary rgb to bgr swapping.
peter1138
parents: 8374
diff changeset
   152
					dst[x].g = 0;
6896
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   153
					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
   154
					/* 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
   155
					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
   156
				}
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   157
			} else {
8757
50c0e625f849 (svn r12453) -Fix [FS#1880]: Remove broken endian-dependent code and unnecessary rgb to bgr swapping.
peter1138
parents: 8374
diff changeset
   158
				dst[x].r = row_pointer[x * sizeof(uint32) + 0];
6896
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   159
				dst[x].g = row_pointer[x * sizeof(uint32) + 1];
8757
50c0e625f849 (svn r12453) -Fix [FS#1880]: Remove broken endian-dependent code and unnecessary rgb to bgr swapping.
peter1138
parents: 8374
diff changeset
   160
				dst[x].b = row_pointer[x * sizeof(uint32) + 2];
6896
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].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
   162
				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
   163
			}
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   164
		}
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   165
	}
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
	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
   168
	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
   169
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   170
	return true;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   171
}
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   172
10056
48659f7d4fa5 (svn r14223) -Codechange: make GetSprite aware of the 4 different types of sprites: fonts, recolour, mapgen and normal sprites.
rubidium
parents: 10039
diff changeset
   173
bool SpriteLoaderPNG::LoadSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_pos, SpriteType sprite_type)
6896
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   174
{
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
   175
	const char *filename = FioGetFilename(file_slot);
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9151
diff changeset
   176
	if (!LoadPNG(sprite, filename, (uint32)file_pos, false)) return false;
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9151
diff changeset
   177
	if (!LoadPNG(sprite, filename, (uint32)file_pos, true)) return false;
6896
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   178
	return true;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   179
}
7098
02887279c5fe (svn r10365) -Fix: compiling without png and networking support under MSVC.
rubidium
parents: 6907
diff changeset
   180
02887279c5fe (svn r10365) -Fix: compiling without png and networking support under MSVC.
rubidium
parents: 6907
diff changeset
   181
#endif /* WITH_PNG */