src/spriteloader/png.cpp
author rubidium
Wed, 09 Jul 2008 19:13:21 +0000
branch0.6
changeset 11128 b62a7b45babf
parent 9353 1127b484af20
permissions -rw-r--r--
(svn r13686) [0.6] -Backport from trunk:
- Fix: Memory leak when NewGRFs got forcefully disabled and they defined GOTO labels (r13675)
- Fix: Crash when drawing a non-real sprite caused by NewGRF interference [FS#2127] (r13674)
- Fix: Disable static NewGRFs when non-static NewGRFs query them in the context of network games. This makes it impossible for static NewGRFs to disable non-static NewGRFs and 'bad' things happening because the non-static NewGRF doesn't know about the static NewGRF (r13576)
- Fix: First determine where to *exactly* build a house before asking a NewGRF whether the location is good instead of possibly moving the house a tile after the NewGRF said the location is good (r13489)
- Fix: Do not crash when resolving vehicle sprite groups with zero sprites (r13397)
- Fix: In the purchase list, CB36 for capacity was not called for the first part of rail and road vehicles (r13385)
7392
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
     1
/* $Id$ */
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
     2
8844
b4f9ff470b85 (svn r11914) -Documentation: fix some @file statement
glx
parents: 8638
diff changeset
     3
/** @file png.cpp */
7392
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
     4
7594
63c4b8b72462 (svn r10365) -Fix: compiling without png and networking support under MSVC.
rubidium
parents: 7403
diff changeset
     5
#ifdef WITH_PNG
63c4b8b72462 (svn r10365) -Fix: compiling without png and networking support under MSVC.
rubidium
parents: 7403
diff changeset
     6
7392
a716551b0c7f (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"
8619
c2434269c3eb (svn r11684) -Codechange: split gfx.h in a type and functional header.
rubidium
parents: 8533
diff changeset
     8
#include "../gfx_func.h"
7392
a716551b0c7f (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"
a716551b0c7f (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"
8626
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8619
diff changeset
    11
#include "../core/alloc_func.hpp"
7392
a716551b0c7f (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"
a716551b0c7f (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>
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    14
a716551b0c7f (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
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    16
a716551b0c7f (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)
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    18
{
a716551b0c7f (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);
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    20
}
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    21
a716551b0c7f (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)
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    23
{
a716551b0c7f (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));
a716551b0c7f (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);
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    26
}
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    27
a716551b0c7f (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)
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    29
{
a716551b0c7f (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));
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    31
}
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    32
a716551b0c7f (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)
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    34
{
a716551b0c7f (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];
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    36
a716551b0c7f (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" : "");
a716551b0c7f (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)) {
a716551b0c7f (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);
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    40
		return true;
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    41
	}
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    42
a716551b0c7f (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 */
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    44
	return false;
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    45
}
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    46
11128
b62a7b45babf (svn r13686) [0.6] -Backport from trunk:
rubidium
parents: 9353
diff changeset
    47
static bool LoadPNG(SpriteLoader::Sprite *sprite, const char *filename, uint32 id, volatile bool mask)
7392
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    48
{
a716551b0c7f (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];
a716551b0c7f (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;
a716551b0c7f (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;
a716551b0c7f (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;
a716551b0c7f (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;
a716551b0c7f (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;
a716551b0c7f (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;
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    56
a716551b0c7f (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
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    58
a716551b0c7f (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 */
a716551b0c7f (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);
a716551b0c7f (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;
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    62
a716551b0c7f (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 */
a716551b0c7f (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);
a716551b0c7f (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;
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    66
a716551b0c7f (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 */
a716551b0c7f (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);
a716551b0c7f (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) {
a716551b0c7f (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);
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    71
		return false;
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    72
	}
a716551b0c7f (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);
a716551b0c7f (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) {
a716551b0c7f (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);
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    76
		return false;
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    77
	}
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    78
a716551b0c7f (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 */
a716551b0c7f (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))) {
a716551b0c7f (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);
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    82
		return false;
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    83
	}
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    84
a716551b0c7f (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 */
a716551b0c7f (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);
a716551b0c7f (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);
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    88
a716551b0c7f (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);
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    90
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
    91
	if (!mask) {
a716551b0c7f (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 */
a716551b0c7f (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;
a716551b0c7f (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;
a716551b0c7f (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);
a716551b0c7f (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);
a716551b0c7f (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++) {
a716551b0c7f (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 */
a716551b0c7f (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);
a716551b0c7f (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);
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   101
		}
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   102
a716551b0c7f (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;
a716551b0c7f (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;
a716551b0c7f (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);
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   106
	}
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   107
a716551b0c7f (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);
a716551b0c7f (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);
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   110
a716551b0c7f (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)) {
a716551b0c7f (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);
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   113
		return true;
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   114
	}
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   115
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   116
	if (!mask) {
a716551b0c7f (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);
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   118
7403
b49905191990 (svn r10156) -Fix: PNG loader can now load 8bpp palette images and greyscale images
truelight
parents: 7392
diff changeset
   119
		if (color_type == PNG_COLOR_TYPE_PALETTE) {
b49905191990 (svn r10156) -Fix: PNG loader can now load 8bpp palette images and greyscale images
truelight
parents: 7392
diff changeset
   120
			png_set_palette_to_rgb(png_ptr);
b49905191990 (svn r10156) -Fix: PNG loader can now load 8bpp palette images and greyscale images
truelight
parents: 7392
diff changeset
   121
			color_type = PNG_COLOR_TYPE_RGB;
b49905191990 (svn r10156) -Fix: PNG loader can now load 8bpp palette images and greyscale images
truelight
parents: 7392
diff changeset
   122
		}
b49905191990 (svn r10156) -Fix: PNG loader can now load 8bpp palette images and greyscale images
truelight
parents: 7392
diff changeset
   123
		if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
b49905191990 (svn r10156) -Fix: PNG loader can now load 8bpp palette images and greyscale images
truelight
parents: 7392
diff changeset
   124
			png_set_gray_to_rgb(png_ptr);
b49905191990 (svn r10156) -Fix: PNG loader can now load 8bpp palette images and greyscale images
truelight
parents: 7392
diff changeset
   125
			color_type = PNG_COLOR_TYPE_RGB;
b49905191990 (svn r10156) -Fix: PNG loader can now load 8bpp palette images and greyscale images
truelight
parents: 7392
diff changeset
   126
		}
7392
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   127
a716551b0c7f (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) {
a716551b0c7f (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);
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   130
		}
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   131
a716551b0c7f (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);
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   133
	} else {
a716551b0c7f (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);
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   135
	}
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   136
8533
a9b708fe4a00 (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: 8066
diff changeset
   137
	row_pointer = (png_byte *)MallocT<byte>(info_ptr->width * pixelsize);
7392
a716551b0c7f (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) {
a716551b0c7f (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);
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   140
		return false;
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   141
	}
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   142
a716551b0c7f (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++) {
a716551b0c7f (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);
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   145
a716551b0c7f (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;
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   147
a716551b0c7f (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++) {
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   149
			if (mask) {
a716551b0c7f (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) {
9353
1127b484af20 (svn r12478) [0.6] -Backport from trunk (12477, 12453, 12448, 12443, 12439, 12417):
rubidium
parents: 8870
diff changeset
   151
					dst[x].r = 0;
1127b484af20 (svn r12478) [0.6] -Backport from trunk (12477, 12453, 12448, 12443, 12439, 12417):
rubidium
parents: 8870
diff changeset
   152
					dst[x].g = 0;
7392
a716551b0c7f (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;
a716551b0c7f (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) */
a716551b0c7f (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)];
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   156
				}
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   157
			} else {
9353
1127b484af20 (svn r12478) [0.6] -Backport from trunk (12477, 12453, 12448, 12443, 12439, 12417):
rubidium
parents: 8870
diff changeset
   158
				dst[x].r = row_pointer[x * sizeof(uint32) + 0];
7392
a716551b0c7f (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];
9353
1127b484af20 (svn r12478) [0.6] -Backport from trunk (12477, 12453, 12448, 12443, 12439, 12417):
rubidium
parents: 8870
diff changeset
   160
				dst[x].b = row_pointer[x * sizeof(uint32) + 2];
7392
a716551b0c7f (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];
a716551b0c7f (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;
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   163
			}
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   164
		}
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   165
	}
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   166
a716551b0c7f (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);
a716551b0c7f (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);
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   169
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   170
	return true;
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   171
}
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   172
8870
461ed7760525 (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: 8844
diff changeset
   173
bool SpriteLoaderPNG::LoadSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, uint32 file_pos)
7392
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   174
{
8870
461ed7760525 (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: 8844
diff changeset
   175
	const char *filename = FioGetFilename(file_slot);
7392
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   176
	if (!LoadPNG(sprite, filename, file_pos, false)) return false;
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   177
	if (!LoadPNG(sprite, filename, file_pos, true)) return false;
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   178
	return true;
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents:
diff changeset
   179
}
7594
63c4b8b72462 (svn r10365) -Fix: compiling without png and networking support under MSVC.
rubidium
parents: 7403
diff changeset
   180
63c4b8b72462 (svn r10365) -Fix: compiling without png and networking support under MSVC.
rubidium
parents: 7403
diff changeset
   181
#endif /* WITH_PNG */