cocoaTouch/otherSrc/IMG_png.c
author nemo
Tue, 02 Mar 2010 19:46:35 +0000
changeset 2912 76d43f7458ad
parent 2698 90585aba87ad
permissions -rw-r--r--
French translation of 03-05. Still missing 05:11-15 and needs further proofing insertion of | in appropriate places
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2698
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
     1
/*
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
     2
    SDL_image:  An example image loading library for use with SDL
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
     3
    Copyright (C) 1997-2009 Sam Lantinga
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
     4
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
     5
    This library is free software; you can redistribute it and/or
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
     6
    modify it under the terms of the GNU Lesser General Public
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
     7
    License as published by the Free Software Foundation; either
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
     8
    version 2.1 of the License, or (at your option) any later version.
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
     9
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    10
    This library is distributed in the hope that it will be useful,
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    13
    Lesser General Public License for more details.
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    14
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    15
    You should have received a copy of the GNU Lesser General Public
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    16
    License along with this library; if not, write to the Free Software
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    18
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    19
    Sam Lantinga
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    20
    slouken@libsdl.org
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    21
*/
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    22
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    23
/* This is a PNG image file loading framework */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    24
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    25
#include <stdlib.h>
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    26
#include <stdio.h>
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    27
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    28
#include "SDL_image.h"
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    29
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    30
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    31
/*=============================================================================
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    32
        File: SDL_png.c
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    33
     Purpose: A PNG loader and saver for the SDL library      
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    34
    Revision: 
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    35
  Created by: Philippe Lavoie          (2 November 1998)
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    36
              lavoie@zeus.genie.uottawa.ca
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    37
 Modified by: 
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    38
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    39
 Copyright notice:
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    40
          Copyright (C) 1998 Philippe Lavoie
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    41
 
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    42
          This library is free software; you can redistribute it and/or
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    43
          modify it under the terms of the GNU Library General Public
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    44
          License as published by the Free Software Foundation; either
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    45
          version 2 of the License, or (at your option) any later version.
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    46
 
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    47
          This library is distributed in the hope that it will be useful,
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    48
          but WITHOUT ANY WARRANTY; without even the implied warranty of
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    49
          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    50
          Library General Public License for more details.
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    51
 
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    52
          You should have received a copy of the GNU Library General Public
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    53
          License along with this library; if not, write to the Free
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    54
          Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    55
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    56
    Comments: The load and save routine are basically the ones you can find
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    57
             in the example.c file from the libpng distribution.
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    58
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    59
  Changes:
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    60
    5/17/99 - Modified to use the new SDL data sources - Sam Lantinga
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    61
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    62
=============================================================================*/
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    63
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    64
#include "SDL_endian.h"
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    65
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    66
#ifdef macintosh
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    67
#define MACOS
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    68
#endif
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    69
#include "png.h"
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    70
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    71
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    72
static struct {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    73
	int loaded;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    74
	void *handle;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    75
	png_infop (*png_create_info_struct) (png_structp png_ptr);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    76
	png_structp (*png_create_read_struct) (png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warn_fn);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    77
	void (*png_destroy_read_struct) (png_structpp png_ptr_ptr, png_infopp info_ptr_ptr, png_infopp end_info_ptr_ptr);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    78
	png_uint_32 (*png_get_IHDR) (png_structp png_ptr, png_infop info_ptr, png_uint_32 *width, png_uint_32 *height, int *bit_depth, int *color_type, int *interlace_method, int *compression_method, int *filter_method);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    79
	png_voidp (*png_get_io_ptr) (png_structp png_ptr);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    80
	png_uint_32 (*png_get_tRNS) (png_structp png_ptr, png_infop info_ptr, png_bytep *trans, int *num_trans, png_color_16p *trans_values);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    81
	png_uint_32 (*png_get_valid) (png_structp png_ptr, png_infop info_ptr, png_uint_32 flag);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    82
	void (*png_read_image) (png_structp png_ptr, png_bytepp image);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    83
	void (*png_read_info) (png_structp png_ptr, png_infop info_ptr);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    84
	void (*png_read_update_info) (png_structp png_ptr, png_infop info_ptr);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    85
	void (*png_set_expand) (png_structp png_ptr);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    86
	void (*png_set_gray_to_rgb) (png_structp png_ptr);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    87
	void (*png_set_packing) (png_structp png_ptr);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    88
	void (*png_set_read_fn) (png_structp png_ptr, png_voidp io_ptr, png_rw_ptr read_data_fn);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    89
	void (*png_set_strip_16) (png_structp png_ptr);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    90
	int (*png_sig_cmp) (png_bytep sig, png_size_t start, png_size_t num_to_check);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    91
} lib;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    92
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    93
#ifdef LOAD_PNG_DYNAMIC
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    94
int IMG_InitPNG()
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    95
{
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    96
	if ( lib.loaded == 0 ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    97
		lib.handle = SDL_LoadObject(LOAD_PNG_DYNAMIC);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    98
		if ( lib.handle == NULL ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    99
			return -1;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   100
		}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   101
		lib.png_create_info_struct =
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   102
			(png_infop (*) (png_structp))
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   103
			SDL_LoadFunction(lib.handle, "png_create_info_struct");
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   104
		if ( lib.png_create_info_struct == NULL ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   105
			SDL_UnloadObject(lib.handle);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   106
			return -1;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   107
		}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   108
		lib.png_create_read_struct =
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   109
			(png_structp (*) (png_const_charp, png_voidp, png_error_ptr, png_error_ptr))
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   110
			SDL_LoadFunction(lib.handle, "png_create_read_struct");
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   111
		if ( lib.png_create_read_struct == NULL ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   112
			SDL_UnloadObject(lib.handle);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   113
			return -1;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   114
		}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   115
		lib.png_destroy_read_struct =
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   116
			(void (*) (png_structpp, png_infopp, png_infopp))
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   117
			SDL_LoadFunction(lib.handle, "png_destroy_read_struct");
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   118
		if ( lib.png_destroy_read_struct == NULL ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   119
			SDL_UnloadObject(lib.handle);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   120
			return -1;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   121
		}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   122
		lib.png_get_IHDR =
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   123
			(png_uint_32 (*) (png_structp, png_infop, png_uint_32 *, png_uint_32 *, int *, int *, int *, int *, int *))
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   124
			SDL_LoadFunction(lib.handle, "png_get_IHDR");
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   125
		if ( lib.png_get_IHDR == NULL ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   126
			SDL_UnloadObject(lib.handle);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   127
			return -1;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   128
		}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   129
		lib.png_get_io_ptr =
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   130
			(png_voidp (*) (png_structp))
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   131
			SDL_LoadFunction(lib.handle, "png_get_io_ptr");
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   132
		if ( lib.png_get_io_ptr == NULL ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   133
			SDL_UnloadObject(lib.handle);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   134
			return -1;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   135
		}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   136
		lib.png_get_tRNS =
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   137
			(png_uint_32 (*) (png_structp, png_infop, png_bytep *, int *, png_color_16p *))
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   138
			SDL_LoadFunction(lib.handle, "png_get_tRNS");
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   139
		if ( lib.png_get_tRNS == NULL ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   140
			SDL_UnloadObject(lib.handle);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   141
			return -1;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   142
		}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   143
		lib.png_get_valid =
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   144
			(png_uint_32 (*) (png_structp, png_infop, png_uint_32))
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   145
			SDL_LoadFunction(lib.handle, "png_get_valid");
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   146
		if ( lib.png_get_valid == NULL ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   147
			SDL_UnloadObject(lib.handle);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   148
			return -1;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   149
		}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   150
		lib.png_read_image =
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   151
			(void (*) (png_structp, png_bytepp))
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   152
			SDL_LoadFunction(lib.handle, "png_read_image");
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   153
		if ( lib.png_read_image == NULL ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   154
			SDL_UnloadObject(lib.handle);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   155
			return -1;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   156
		}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   157
		lib.png_read_info =
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   158
			(void (*) (png_structp, png_infop))
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   159
			SDL_LoadFunction(lib.handle, "png_read_info");
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   160
		if ( lib.png_read_info == NULL ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   161
			SDL_UnloadObject(lib.handle);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   162
			return -1;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   163
		}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   164
		lib.png_read_update_info =
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   165
			(void (*) (png_structp, png_infop))
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   166
			SDL_LoadFunction(lib.handle, "png_read_update_info");
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   167
		if ( lib.png_read_update_info == NULL ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   168
			SDL_UnloadObject(lib.handle);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   169
			return -1;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   170
		}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   171
		lib.png_set_expand =
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   172
			(void (*) (png_structp))
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   173
			SDL_LoadFunction(lib.handle, "png_set_expand");
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   174
		if ( lib.png_set_expand == NULL ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   175
			SDL_UnloadObject(lib.handle);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   176
			return -1;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   177
		}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   178
		lib.png_set_gray_to_rgb =
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   179
			(void (*) (png_structp))
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   180
			SDL_LoadFunction(lib.handle, "png_set_gray_to_rgb");
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   181
		if ( lib.png_set_gray_to_rgb == NULL ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   182
			SDL_UnloadObject(lib.handle);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   183
			return -1;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   184
		}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   185
		lib.png_set_packing =
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   186
			(void (*) (png_structp))
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   187
			SDL_LoadFunction(lib.handle, "png_set_packing");
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   188
		if ( lib.png_set_packing == NULL ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   189
			SDL_UnloadObject(lib.handle);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   190
			return -1;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   191
		}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   192
		lib.png_set_read_fn =
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   193
			(void (*) (png_structp, png_voidp, png_rw_ptr))
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   194
			SDL_LoadFunction(lib.handle, "png_set_read_fn");
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   195
		if ( lib.png_set_read_fn == NULL ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   196
			SDL_UnloadObject(lib.handle);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   197
			return -1;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   198
		}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   199
		lib.png_set_strip_16 =
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   200
			(void (*) (png_structp))
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   201
			SDL_LoadFunction(lib.handle, "png_set_strip_16");
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   202
		if ( lib.png_set_strip_16 == NULL ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   203
			SDL_UnloadObject(lib.handle);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   204
			return -1;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   205
		}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   206
		lib.png_sig_cmp =
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   207
			(int (*) (png_bytep, png_size_t, png_size_t))
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   208
			SDL_LoadFunction(lib.handle, "png_sig_cmp");
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   209
		if ( lib.png_sig_cmp == NULL ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   210
			SDL_UnloadObject(lib.handle);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   211
			return -1;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   212
		}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   213
	}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   214
	++lib.loaded;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   215
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   216
	return 0;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   217
}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   218
void IMG_QuitPNG()
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   219
{
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   220
	if ( lib.loaded == 0 ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   221
		return;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   222
	}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   223
	if ( lib.loaded == 1 ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   224
		SDL_UnloadObject(lib.handle);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   225
	}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   226
	--lib.loaded;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   227
}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   228
#else
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   229
int IMG_InitPNG()
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   230
{
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   231
	if ( lib.loaded == 0 ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   232
		lib.png_create_info_struct = png_create_info_struct;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   233
		lib.png_create_read_struct = png_create_read_struct;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   234
		lib.png_destroy_read_struct = png_destroy_read_struct;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   235
		lib.png_get_IHDR = png_get_IHDR;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   236
		lib.png_get_io_ptr = png_get_io_ptr;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   237
		lib.png_get_tRNS = png_get_tRNS;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   238
		lib.png_get_valid = png_get_valid;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   239
		lib.png_read_image = png_read_image;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   240
		lib.png_read_info = png_read_info;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   241
		lib.png_read_update_info = png_read_update_info;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   242
		lib.png_set_expand = png_set_expand;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   243
		lib.png_set_gray_to_rgb = png_set_gray_to_rgb;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   244
		lib.png_set_packing = png_set_packing;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   245
		lib.png_set_read_fn = png_set_read_fn;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   246
		lib.png_set_strip_16 = png_set_strip_16;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   247
		lib.png_sig_cmp = png_sig_cmp;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   248
	}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   249
	++lib.loaded;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   250
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   251
	return 0;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   252
}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   253
void IMG_QuitPNG()
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   254
{
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   255
	if ( lib.loaded == 0 ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   256
		return;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   257
	}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   258
	if ( lib.loaded == 1 ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   259
	}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   260
	--lib.loaded;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   261
}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   262
#endif /* LOAD_PNG_DYNAMIC */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   263
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   264
/* See if an image is contained in a data source */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   265
int IMG_isPNG(SDL_RWops *src)
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   266
{
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   267
	int start;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   268
	int is_PNG;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   269
	Uint8 magic[4];
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   270
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   271
	if ( !src )
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   272
		return 0;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   273
	start = SDL_RWtell(src);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   274
	is_PNG = 0;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   275
	if ( SDL_RWread(src, magic, 1, sizeof(magic)) == sizeof(magic) ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   276
                if ( magic[0] == 0x89 &&
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   277
                     magic[1] == 'P' &&
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   278
                     magic[2] == 'N' &&
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   279
                     magic[3] == 'G' ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   280
			is_PNG = 1;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   281
		}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   282
	}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   283
	SDL_RWseek(src, start, RW_SEEK_SET);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   284
	return(is_PNG);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   285
}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   286
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   287
/* Load a PNG type image from an SDL datasource */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   288
static void png_read_data(png_structp ctx, png_bytep area, png_size_t size)
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   289
{
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   290
	SDL_RWops *src;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   291
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   292
	src = (SDL_RWops *)lib.png_get_io_ptr(ctx);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   293
	SDL_RWread(src, area, size, 1);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   294
}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   295
SDL_Surface *IMG_LoadPNG_RW(SDL_RWops *src)
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   296
{
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   297
	int start;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   298
	const char *error;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   299
	SDL_Surface *volatile surface;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   300
	png_structp png_ptr;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   301
	png_infop info_ptr;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   302
	png_uint_32 width, height;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   303
	int bit_depth, color_type, interlace_type;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   304
	Uint32 Rmask;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   305
	Uint32 Gmask;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   306
	Uint32 Bmask;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   307
	Uint32 Amask;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   308
	SDL_Palette *palette;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   309
	png_bytep *volatile row_pointers;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   310
	int row, i;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   311
	volatile int ckey = -1;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   312
	png_color_16 *transv;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   313
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   314
	if ( !src ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   315
		/* The error message has been set in SDL_RWFromFile */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   316
		return NULL;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   317
	}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   318
	start = SDL_RWtell(src);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   319
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   320
	if ( !IMG_Init(IMG_INIT_PNG) ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   321
		return NULL;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   322
	}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   323
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   324
	/* Initialize the data we will clean up when we're done */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   325
	error = NULL;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   326
	png_ptr = NULL; info_ptr = NULL; row_pointers = NULL; surface = NULL;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   327
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   328
	/* Create the PNG loading context structure */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   329
	png_ptr = lib.png_create_read_struct(PNG_LIBPNG_VER_STRING,
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   330
					  NULL,NULL,NULL);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   331
	if (png_ptr == NULL){
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   332
		error = "Couldn't allocate memory for PNG file or incompatible PNG dll";
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   333
		goto done;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   334
	}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   335
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   336
	 /* Allocate/initialize the memory for image information.  REQUIRED. */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   337
	info_ptr = lib.png_create_info_struct(png_ptr);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   338
	if (info_ptr == NULL) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   339
		error = "Couldn't create image information for PNG file";
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   340
		goto done;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   341
	}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   342
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   343
	/* Set error handling if you are using setjmp/longjmp method (this is
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   344
	 * the normal method of doing things with libpng).  REQUIRED unless you
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   345
	 * set up your own error handlers in png_create_read_struct() earlier.
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   346
	 */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   347
	if ( setjmp(png_ptr->jmpbuf) ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   348
		error = "Error reading the PNG file.";
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   349
		goto done;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   350
	}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   351
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   352
	/* Set up the input control */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   353
	lib.png_set_read_fn(png_ptr, src, png_read_data);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   354
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   355
	/* Read PNG header info */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   356
	lib.png_read_info(png_ptr, info_ptr);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   357
	lib.png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   358
			&color_type, &interlace_type, NULL, NULL);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   359
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   360
	/* tell libpng to strip 16 bit/color files down to 8 bits/color */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   361
	lib.png_set_strip_16(png_ptr) ;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   362
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   363
	/* Extract multiple pixels with bit depths of 1, 2, and 4 from a single
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   364
	 * byte into separate bytes (useful for paletted and grayscale images).
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   365
	 */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   366
	lib.png_set_packing(png_ptr);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   367
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   368
	/* scale greyscale values to the range 0..255 */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   369
	if(color_type == PNG_COLOR_TYPE_GRAY)
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   370
		lib.png_set_expand(png_ptr);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   371
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   372
	/* For images with a single "transparent colour", set colour key;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   373
	   if more than one index has transparency, or if partially transparent
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   374
	   entries exist, use full alpha channel */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   375
	if (lib.png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   376
	        int num_trans;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   377
		Uint8 *trans;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   378
		lib.png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans,
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   379
			     &transv);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   380
		if(color_type == PNG_COLOR_TYPE_PALETTE) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   381
		    /* Check if all tRNS entries are opaque except one */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   382
		    int t = -1;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   383
		    for(i = 0; i < num_trans; i++)
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   384
			if(trans[i] == 0) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   385
			    if(t >= 0)
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   386
				break;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   387
			    t = i;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   388
			} else if(trans[i] != 255)
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   389
			    break;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   390
		    if(i == num_trans) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   391
			/* exactly one transparent index */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   392
			ckey = t;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   393
		    } else {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   394
			/* more than one transparent index, or translucency */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   395
			lib.png_set_expand(png_ptr);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   396
		    }
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   397
		} else
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   398
		    ckey = 0; /* actual value will be set later */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   399
	}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   400
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   401
	if ( color_type == PNG_COLOR_TYPE_GRAY_ALPHA )
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   402
		lib.png_set_gray_to_rgb(png_ptr);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   403
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   404
	lib.png_read_update_info(png_ptr, info_ptr);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   405
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   406
	lib.png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   407
			&color_type, &interlace_type, NULL, NULL);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   408
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   409
	/* Allocate the SDL surface to hold the image */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   410
	Rmask = Gmask = Bmask = Amask = 0 ; 
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   411
	if ( color_type != PNG_COLOR_TYPE_PALETTE ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   412
		if ( SDL_BYTEORDER == SDL_LIL_ENDIAN ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   413
			Rmask = 0x000000FF;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   414
			Gmask = 0x0000FF00;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   415
			Bmask = 0x00FF0000;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   416
			Amask = (info_ptr->channels == 4) ? 0xFF000000 : 0;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   417
		} else {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   418
		        int s = (info_ptr->channels == 4) ? 0 : 8;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   419
			Rmask = 0xFF000000 >> s;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   420
			Gmask = 0x00FF0000 >> s;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   421
			Bmask = 0x0000FF00 >> s;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   422
			Amask = 0x000000FF >> s;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   423
		}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   424
	}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   425
	surface = SDL_AllocSurface(SDL_SWSURFACE, width, height,
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   426
			bit_depth*info_ptr->channels, Rmask,Gmask,Bmask,Amask);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   427
	if ( surface == NULL ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   428
		error = "Out of memory";
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   429
		goto done;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   430
	}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   431
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   432
	if(ckey != -1) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   433
	        if(color_type != PNG_COLOR_TYPE_PALETTE)
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   434
			/* FIXME: Should these be truncated or shifted down? */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   435
		        ckey = SDL_MapRGB(surface->format,
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   436
			                 (Uint8)transv->red,
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   437
			                 (Uint8)transv->green,
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   438
			                 (Uint8)transv->blue);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   439
	        SDL_SetColorKey(surface, SDL_SRCCOLORKEY, ckey);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   440
	}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   441
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   442
	/* Create the array of pointers to image data */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   443
	row_pointers = (png_bytep*) malloc(sizeof(png_bytep)*height);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   444
	if ( (row_pointers == NULL) ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   445
		error = "Out of memory";
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   446
		goto done;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   447
	}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   448
	for (row = 0; row < (int)height; row++) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   449
		row_pointers[row] = (png_bytep)
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   450
				(Uint8 *)surface->pixels + row*surface->pitch;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   451
	}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   452
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   453
	/* Read the entire image in one go */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   454
	lib.png_read_image(png_ptr, row_pointers);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   455
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   456
	/* and we're done!  (png_read_end() can be omitted if no processing of
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   457
	 * post-IDAT text/time/etc. is desired)
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   458
	 * In some cases it can't read PNG's created by some popular programs (ACDSEE),
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   459
	 * we do not want to process comments, so we omit png_read_end
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   460
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   461
	lib.png_read_end(png_ptr, info_ptr);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   462
	*/
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   463
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   464
	/* Load the palette, if any */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   465
	palette = surface->format->palette;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   466
	if ( palette ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   467
	    if(color_type == PNG_COLOR_TYPE_GRAY) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   468
			palette->ncolors = 256;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   469
			for(i = 0; i < 256; i++) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   470
				palette->colors[i].r = i;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   471
				palette->colors[i].g = i;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   472
				palette->colors[i].b = i;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   473
			}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   474
		} else if (info_ptr->num_palette > 0 ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   475
			palette->ncolors = info_ptr->num_palette; 
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   476
			for( i=0; i<info_ptr->num_palette; ++i ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   477
				palette->colors[i].b = info_ptr->palette[i].blue;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   478
				palette->colors[i].g = info_ptr->palette[i].green;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   479
				palette->colors[i].r = info_ptr->palette[i].red;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   480
			}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   481
	    }
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   482
	}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   483
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   484
done:	/* Clean up and return */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   485
	if ( png_ptr ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   486
		lib.png_destroy_read_struct(&png_ptr,
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   487
		                        info_ptr ? &info_ptr : (png_infopp)0,
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   488
								(png_infopp)0);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   489
	}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   490
	if ( row_pointers ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   491
		free(row_pointers);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   492
	}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   493
	if ( error ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   494
		SDL_RWseek(src, start, RW_SEEK_SET);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   495
		if ( surface ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   496
			SDL_FreeSurface(surface);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   497
			surface = NULL;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   498
		}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   499
		IMG_SetError(error);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   500
	}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   501
	return(surface); 
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   502
}