IMG_png.c
author Ozkan Sezer <sezeroz@gmail.com>
Thu, 18 Oct 2018 11:57:19 +0300
changeset 612 ca95d0e31aec
parent 596 4b70bfe18fb7
child 614 fa3c53a205a9
permissions -rw-r--r--
use less ancient versions of autofoo scripts
slouken@0
     1
/*
slouken@280
     2
  SDL_image:  An example image loading library for use with SDL
slouken@575
     3
  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@280
     5
  This software is provided 'as-is', without any express or implied
slouken@280
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@280
     7
  arising from the use of this software.
slouken@0
     8
slouken@280
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@280
    10
  including commercial applications, and to alter it and redistribute it
slouken@280
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@280
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@280
    14
     claim that you wrote the original software. If you use this software
slouken@280
    15
     in a product, an acknowledgment in the product documentation would be
slouken@280
    16
     appreciated but is not required.
slouken@280
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@280
    18
     misrepresented as being the original software.
slouken@280
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
slouken@0
    21
slouken@0
    22
/* This is a PNG image file loading framework */
slouken@0
    23
slouken@391
    24
#include "SDL_image.h"
slouken@0
    25
slouken@508
    26
#if !(defined(__APPLE__) || defined(SDL_IMAGE_USE_WIC_BACKEND)) || defined(SDL_IMAGE_USE_COMMON_BACKEND)
slouken@0
    27
slouken@0
    28
#ifdef LOAD_PNG
slouken@0
    29
slouken@506
    30
#define USE_LIBPNG
slouken@506
    31
slouken@0
    32
/*=============================================================================
slouken@0
    33
        File: SDL_png.c
slouken@368
    34
     Purpose: A PNG loader and saver for the SDL library
slouken@368
    35
    Revision:
slouken@0
    36
  Created by: Philippe Lavoie          (2 November 1998)
slouken@0
    37
              lavoie@zeus.genie.uottawa.ca
slouken@368
    38
 Modified by:
slouken@0
    39
slouken@0
    40
 Copyright notice:
slouken@0
    41
          Copyright (C) 1998 Philippe Lavoie
slouken@368
    42
slouken@0
    43
          This library is free software; you can redistribute it and/or
slouken@0
    44
          modify it under the terms of the GNU Library General Public
slouken@0
    45
          License as published by the Free Software Foundation; either
slouken@0
    46
          version 2 of the License, or (at your option) any later version.
slouken@368
    47
slouken@0
    48
          This library is distributed in the hope that it will be useful,
slouken@0
    49
          but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    50
          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@0
    51
          Library General Public License for more details.
slouken@368
    52
slouken@0
    53
          You should have received a copy of the GNU Library General Public
slouken@0
    54
          License along with this library; if not, write to the Free
slouken@0
    55
          Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
slouken@0
    56
slouken@0
    57
    Comments: The load and save routine are basically the ones you can find
slouken@0
    58
             in the example.c file from the libpng distribution.
slouken@0
    59
slouken@0
    60
  Changes:
slouken@0
    61
    5/17/99 - Modified to use the new SDL data sources - Sam Lantinga
slouken@0
    62
slouken@0
    63
=============================================================================*/
slouken@0
    64
slouken@0
    65
#include "SDL_endian.h"
slouken@0
    66
slouken@9
    67
#ifdef macintosh
slouken@9
    68
#define MACOS
slouken@9
    69
#endif
slouken@0
    70
#include <png.h>
slouken@0
    71
slouken@265
    72
/* Check for the older version of libpng */
slouken@383
    73
#if (PNG_LIBPNG_VER_MAJOR == 1) 
slouken@430
    74
#if (PNG_LIBPNG_VER_MINOR < 5)
slouken@265
    75
#define LIBPNG_VERSION_12
slouken@463
    76
typedef png_bytep png_const_bytep;
sezeroz@596
    77
typedef png_color *png_const_colorp;
slouken@383
    78
#endif
slouken@383
    79
#if (PNG_LIBPNG_VER_MINOR < 6)
slouken@380
    80
typedef png_structp png_const_structrp;
slouken@380
    81
typedef png_infop png_const_inforp;
flibitijibibo@548
    82
typedef png_structp png_structrp;
flibitijibibo@548
    83
typedef png_infop png_inforp;
slouken@383
    84
#endif
slouken@265
    85
#endif
slouken@0
    86
slouken@143
    87
static struct {
slouken@368
    88
    int loaded;
slouken@368
    89
    void *handle;
slouken@378
    90
    png_infop (*png_create_info_struct) (png_const_structrp png_ptr);
slouken@368
    91
    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);
slouken@368
    92
    void (*png_destroy_read_struct) (png_structpp png_ptr_ptr, png_infopp info_ptr_ptr, png_infopp end_info_ptr_ptr);
slouken@378
    93
    png_uint_32 (*png_get_IHDR) (png_const_structrp png_ptr, png_const_inforp 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);
slouken@378
    94
    png_voidp (*png_get_io_ptr) (png_const_structrp png_ptr);
slouken@378
    95
    png_byte (*png_get_channels) (png_const_structrp png_ptr, png_const_inforp info_ptr);
slouken@529
    96
    png_uint_32 (*png_get_PLTE) (png_const_structrp png_ptr, png_inforp info_ptr, png_colorp *palette, int *num_palette);
slouken@529
    97
    png_uint_32 (*png_get_tRNS) (png_const_structrp png_ptr, png_inforp info_ptr, png_bytep *trans, int *num_trans, png_color_16p *trans_values);
slouken@378
    98
    png_uint_32 (*png_get_valid) (png_const_structrp png_ptr, png_const_inforp info_ptr, png_uint_32 flag);
slouken@529
    99
    void (*png_read_image) (png_structrp png_ptr, png_bytepp image);
slouken@529
   100
    void (*png_read_info) (png_structrp png_ptr, png_inforp info_ptr);
slouken@529
   101
    void (*png_read_update_info) (png_structrp png_ptr, png_inforp info_ptr);
slouken@529
   102
    void (*png_set_expand) (png_structrp png_ptr);
slouken@529
   103
    void (*png_set_gray_to_rgb) (png_structrp png_ptr);
slouken@529
   104
    void (*png_set_packing) (png_structrp png_ptr);
slouken@529
   105
    void (*png_set_read_fn) (png_structrp png_ptr, png_voidp io_ptr, png_rw_ptr read_data_fn);
slouken@529
   106
    void (*png_set_strip_16) (png_structrp png_ptr);
slouken@571
   107
    int (*png_set_interlace_handling) (png_structrp png_ptr);
slouken@378
   108
    int (*png_sig_cmp) (png_const_bytep sig, png_size_t start, png_size_t num_to_check);
slouken@265
   109
#ifndef LIBPNG_VERSION_12
slouken@529
   110
    jmp_buf* (*png_set_longjmp_fn) (png_structrp, png_longjmp_ptr, size_t);
slouken@265
   111
#endif
slouken@506
   112
    png_structp (*png_create_write_struct) (png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warn_fn);
slouken@506
   113
    void (*png_destroy_write_struct) (png_structpp png_ptr_ptr, png_infopp info_ptr_ptr);
slouken@529
   114
    void (*png_set_write_fn) (png_structrp png_ptr, png_voidp io_ptr, png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn);
slouken@533
   115
    void (*png_set_IHDR) (png_const_structrp png_ptr, png_inforp info_ptr, png_uint_32 width, png_uint_32 height, int bit_depth, int color_type, int interlace_type, int compression_type, int filter_type);
slouken@533
   116
    void (*png_write_info) (png_structrp png_ptr, png_const_inforp info_ptr);
slouken@533
   117
    void (*png_set_rows) (png_const_structrp png_ptr, png_inforp info_ptr, png_bytepp row_pointers);
slouken@529
   118
    void (*png_write_png) (png_structrp png_ptr, png_inforp info_ptr, int transforms, png_voidp params);
slouken@533
   119
    void (*png_set_PLTE) (png_structrp png_ptr, png_inforp info_ptr, png_const_colorp palette, int num_palette);
slouken@143
   120
} lib;
slouken@143
   121
slouken@143
   122
#ifdef LOAD_PNG_DYNAMIC
slouken@537
   123
#define FUNCTION_LOADER(FUNC, SIG) \
slouken@537
   124
    lib.FUNC = (SIG) SDL_LoadFunction(lib.handle, #FUNC); \
slouken@537
   125
    if (lib.FUNC == NULL) { SDL_UnloadObject(lib.handle); return -1; }
slouken@537
   126
#else
slouken@537
   127
#define FUNCTION_LOADER(FUNC, SIG) \
slouken@537
   128
    lib.FUNC = FUNC;
slouken@537
   129
#endif
slouken@537
   130
slouken@205
   131
int IMG_InitPNG()
slouken@143
   132
{
slouken@368
   133
    if ( lib.loaded == 0 ) {
slouken@537
   134
#ifdef LOAD_PNG_DYNAMIC
slouken@368
   135
        lib.handle = SDL_LoadObject(LOAD_PNG_DYNAMIC);
slouken@368
   136
        if ( lib.handle == NULL ) {
slouken@368
   137
            return -1;
slouken@368
   138
        }
slouken@537
   139
#endif
slouken@537
   140
        FUNCTION_LOADER(png_create_info_struct, png_infop (*) (png_const_structrp png_ptr))
slouken@537
   141
        FUNCTION_LOADER(png_create_read_struct, png_structp (*) (png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warn_fn))
slouken@537
   142
        FUNCTION_LOADER(png_destroy_read_struct, void (*) (png_structpp png_ptr_ptr, png_infopp info_ptr_ptr, png_infopp end_info_ptr_ptr))
slouken@537
   143
        FUNCTION_LOADER(png_get_IHDR, png_uint_32 (*) (png_const_structrp png_ptr, png_const_inforp 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))
slouken@537
   144
        FUNCTION_LOADER(png_get_io_ptr, png_voidp (*) (png_const_structrp png_ptr))
slouken@537
   145
        FUNCTION_LOADER(png_get_channels, png_byte (*) (png_const_structrp png_ptr, png_const_inforp info_ptr))
slouken@537
   146
        FUNCTION_LOADER(png_get_PLTE, png_uint_32 (*) (png_const_structrp png_ptr, png_inforp info_ptr, png_colorp *palette, int *num_palette))
slouken@537
   147
        FUNCTION_LOADER(png_get_tRNS, png_uint_32 (*) (png_const_structrp png_ptr, png_inforp info_ptr, png_bytep *trans, int *num_trans, png_color_16p *trans_values))
slouken@537
   148
        FUNCTION_LOADER(png_get_valid, png_uint_32 (*) (png_const_structrp png_ptr, png_const_inforp info_ptr, png_uint_32 flag))
slouken@537
   149
        FUNCTION_LOADER(png_read_image, void (*) (png_structrp png_ptr, png_bytepp image))
slouken@537
   150
        FUNCTION_LOADER(png_read_info, void (*) (png_structrp png_ptr, png_inforp info_ptr))
slouken@537
   151
        FUNCTION_LOADER(png_read_update_info, void (*) (png_structrp png_ptr, png_inforp info_ptr))
slouken@537
   152
        FUNCTION_LOADER(png_set_expand, void (*) (png_structrp png_ptr))
slouken@537
   153
        FUNCTION_LOADER(png_set_gray_to_rgb, void (*) (png_structrp png_ptr))
slouken@537
   154
        FUNCTION_LOADER(png_set_packing, void (*) (png_structrp png_ptr))
slouken@537
   155
        FUNCTION_LOADER(png_set_read_fn, void (*) (png_structrp png_ptr, png_voidp io_ptr, png_rw_ptr read_data_fn))
slouken@537
   156
        FUNCTION_LOADER(png_set_strip_16, void (*) (png_structrp png_ptr))
slouken@571
   157
        FUNCTION_LOADER(png_set_interlace_handling, int (*) (png_structrp png_ptr))
slouken@537
   158
        FUNCTION_LOADER(png_sig_cmp, int (*) (png_const_bytep sig, png_size_t start, png_size_t num_to_check))
slouken@265
   159
#ifndef LIBPNG_VERSION_12
slouken@537
   160
        FUNCTION_LOADER(png_set_longjmp_fn, jmp_buf* (*) (png_structrp, png_longjmp_ptr, size_t))
slouken@265
   161
#endif
slouken@537
   162
        FUNCTION_LOADER(png_create_write_struct, png_structp (*) (png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warn_fn))
slouken@537
   163
        FUNCTION_LOADER(png_destroy_write_struct, void (*) (png_structpp png_ptr_ptr, png_infopp info_ptr_ptr))
slouken@537
   164
        FUNCTION_LOADER(png_set_write_fn, void (*) (png_structrp png_ptr, png_voidp io_ptr, png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn))
slouken@537
   165
        FUNCTION_LOADER(png_set_IHDR, void (*) (png_const_structrp png_ptr, png_inforp info_ptr, png_uint_32 width, png_uint_32 height, int bit_depth, int color_type, int interlace_type, int compression_type, int filter_type))
slouken@537
   166
        FUNCTION_LOADER(png_write_info, void (*) (png_structrp png_ptr, png_const_inforp info_ptr))
slouken@537
   167
        FUNCTION_LOADER(png_set_rows, void (*) (png_const_structrp png_ptr, png_inforp info_ptr, png_bytepp row_pointers))
slouken@537
   168
        FUNCTION_LOADER(png_write_png, void (*) (png_structrp png_ptr, png_inforp info_ptr, int transforms, png_voidp params))
slouken@537
   169
        FUNCTION_LOADER(png_set_PLTE, void (*) (png_structrp png_ptr, png_inforp info_ptr, png_const_colorp palette, int num_palette))
slouken@368
   170
    }
slouken@368
   171
    ++lib.loaded;
slouken@143
   172
slouken@368
   173
    return 0;
slouken@143
   174
}
slouken@205
   175
void IMG_QuitPNG()
slouken@143
   176
{
slouken@368
   177
    if ( lib.loaded == 0 ) {
slouken@368
   178
        return;
slouken@368
   179
    }
slouken@368
   180
    if ( lib.loaded == 1 ) {
slouken@537
   181
#ifdef LOAD_PNG_DYNAMIC
slouken@368
   182
        SDL_UnloadObject(lib.handle);
slouken@537
   183
#endif
slouken@368
   184
    }
slouken@368
   185
    --lib.loaded;
slouken@143
   186
}
slouken@143
   187
slouken@0
   188
/* See if an image is contained in a data source */
slouken@0
   189
int IMG_isPNG(SDL_RWops *src)
slouken@0
   190
{
slouken@368
   191
    Sint64 start;
slouken@368
   192
    int is_PNG;
slouken@368
   193
    Uint8 magic[4];
slouken@0
   194
slouken@390
   195
    if ( !src ) {
slouken@368
   196
        return 0;
slouken@390
   197
    }
slouken@390
   198
slouken@368
   199
    start = SDL_RWtell(src);
slouken@368
   200
    is_PNG = 0;
slouken@368
   201
    if ( SDL_RWread(src, magic, 1, sizeof(magic)) == sizeof(magic) ) {
slouken@390
   202
        if ( magic[0] == 0x89 &&
slouken@390
   203
             magic[1] == 'P' &&
slouken@390
   204
             magic[2] == 'N' &&
slouken@390
   205
             magic[3] == 'G' ) {
slouken@368
   206
            is_PNG = 1;
slouken@368
   207
        }
slouken@368
   208
    }
slouken@368
   209
    SDL_RWseek(src, start, RW_SEEK_SET);
slouken@368
   210
    return(is_PNG);
slouken@0
   211
}
slouken@0
   212
slouken@0
   213
/* Load a PNG type image from an SDL datasource */
slouken@0
   214
static void png_read_data(png_structp ctx, png_bytep area, png_size_t size)
slouken@0
   215
{
slouken@368
   216
    SDL_RWops *src;
slouken@0
   217
slouken@368
   218
    src = (SDL_RWops *)lib.png_get_io_ptr(ctx);
slouken@368
   219
    SDL_RWread(src, area, size, 1);
slouken@0
   220
}
slouken@0
   221
SDL_Surface *IMG_LoadPNG_RW(SDL_RWops *src)
slouken@0
   222
{
slouken@368
   223
    Sint64 start;
slouken@368
   224
    const char *error;
slouken@368
   225
    SDL_Surface *volatile surface;
slouken@368
   226
    png_structp png_ptr;
slouken@368
   227
    png_infop info_ptr;
slouken@368
   228
    png_uint_32 width, height;
slouken@368
   229
    int bit_depth, color_type, interlace_type, num_channels;
slouken@368
   230
    Uint32 Rmask;
slouken@368
   231
    Uint32 Gmask;
slouken@368
   232
    Uint32 Bmask;
slouken@368
   233
    Uint32 Amask;
slouken@368
   234
    SDL_Palette *palette;
slouken@368
   235
    png_bytep *volatile row_pointers;
slouken@368
   236
    int row, i;
slouken@368
   237
    int ckey = -1;
slouken@368
   238
    png_color_16 *transv;
slouken@0
   239
slouken@368
   240
    if ( !src ) {
slouken@368
   241
        /* The error message has been set in SDL_RWFromFile */
slouken@368
   242
        return NULL;
slouken@368
   243
    }
slouken@368
   244
    start = SDL_RWtell(src);
slouken@98
   245
slouken@490
   246
    if ( (IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG) == 0 ) {
slouken@368
   247
        return NULL;
slouken@368
   248
    }
slouken@143
   249
slouken@368
   250
    /* Initialize the data we will clean up when we're done */
slouken@368
   251
    error = NULL;
slouken@368
   252
    png_ptr = NULL; info_ptr = NULL; row_pointers = NULL; surface = NULL;
slouken@0
   253
slouken@368
   254
    /* Create the PNG loading context structure */
slouken@368
   255
    png_ptr = lib.png_create_read_struct(PNG_LIBPNG_VER_STRING,
slouken@368
   256
                      NULL,NULL,NULL);
slouken@368
   257
    if (png_ptr == NULL){
slouken@368
   258
        error = "Couldn't allocate memory for PNG file or incompatible PNG dll";
slouken@368
   259
        goto done;
slouken@368
   260
    }
slouken@0
   261
slouken@368
   262
     /* Allocate/initialize the memory for image information.  REQUIRED. */
slouken@368
   263
    info_ptr = lib.png_create_info_struct(png_ptr);
slouken@368
   264
    if (info_ptr == NULL) {
slouken@368
   265
        error = "Couldn't create image information for PNG file";
slouken@368
   266
        goto done;
slouken@368
   267
    }
slouken@0
   268
slouken@368
   269
    /* Set error handling if you are using setjmp/longjmp method (this is
slouken@368
   270
     * the normal method of doing things with libpng).  REQUIRED unless you
slouken@368
   271
     * set up your own error handlers in png_create_read_struct() earlier.
slouken@368
   272
     */
slouken@265
   273
#ifndef LIBPNG_VERSION_12
slouken@368
   274
    if ( setjmp(*lib.png_set_longjmp_fn(png_ptr, longjmp, sizeof (jmp_buf))) )
slouken@265
   275
#else
slouken@368
   276
    if ( setjmp(png_ptr->jmpbuf) )
slouken@265
   277
#endif
slouken@368
   278
    {
slouken@368
   279
        error = "Error reading the PNG file.";
slouken@368
   280
        goto done;
slouken@368
   281
    }
slouken@0
   282
slouken@368
   283
    /* Set up the input control */
slouken@368
   284
    lib.png_set_read_fn(png_ptr, src, png_read_data);
slouken@0
   285
slouken@368
   286
    /* Read PNG header info */
slouken@368
   287
    lib.png_read_info(png_ptr, info_ptr);
slouken@368
   288
    lib.png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
slouken@368
   289
            &color_type, &interlace_type, NULL, NULL);
slouken@0
   290
slouken@368
   291
    /* tell libpng to strip 16 bit/color files down to 8 bits/color */
slouken@571
   292
    lib.png_set_strip_16(png_ptr);
slouken@571
   293
slouken@571
   294
    /* tell libpng to de-interlace (if the image is interlaced) */
slouken@571
   295
    lib.png_set_interlace_handling(png_ptr);
slouken@0
   296
slouken@368
   297
    /* Extract multiple pixels with bit depths of 1, 2, and 4 from a single
slouken@368
   298
     * byte into separate bytes (useful for paletted and grayscale images).
slouken@368
   299
     */
slouken@368
   300
    lib.png_set_packing(png_ptr);
slouken@0
   301
slouken@368
   302
    /* scale greyscale values to the range 0..255 */
slouken@368
   303
    if (color_type == PNG_COLOR_TYPE_GRAY)
slouken@368
   304
        lib.png_set_expand(png_ptr);
slouken@0
   305
slouken@368
   306
    /* For images with a single "transparent colour", set colour key;
slouken@368
   307
       if more than one index has transparency, or if partially transparent
slouken@368
   308
       entries exist, use full alpha channel */
slouken@368
   309
    if (lib.png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
slouken@390
   310
        int num_trans;
slouken@368
   311
        Uint8 *trans;
slouken@390
   312
        lib.png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans, &transv);
slouken@368
   313
        if (color_type == PNG_COLOR_TYPE_PALETTE) {
slouken@368
   314
            /* Check if all tRNS entries are opaque except one */
slouken@368
   315
            int j, t = -1;
slouken@368
   316
            for (j = 0; j < num_trans; j++) {
slouken@390
   317
                if (trans[j] == 0) {
slouken@390
   318
                    if (t >= 0) {
slouken@390
   319
                        break;
slouken@361
   320
                    }
slouken@390
   321
                    t = j;
slouken@390
   322
                } else if (trans[j] != 255) {
slouken@390
   323
                    break;
slouken@390
   324
                }
slouken@390
   325
            }
slouken@368
   326
            if (j == num_trans) {
slouken@390
   327
                /* exactly one transparent index */
slouken@390
   328
                ckey = t;
slouken@368
   329
            } else {
slouken@390
   330
                /* more than one transparent index, or translucency */
slouken@390
   331
                lib.png_set_expand(png_ptr);
slouken@368
   332
            }
slouken@390
   333
        } else {
slouken@368
   334
            ckey = 0; /* actual value will be set later */
slouken@390
   335
        }
slouken@368
   336
    }
slouken@0
   337
slouken@368
   338
    if ( color_type == PNG_COLOR_TYPE_GRAY_ALPHA )
slouken@368
   339
        lib.png_set_gray_to_rgb(png_ptr);
slouken@0
   340
slouken@368
   341
    lib.png_read_update_info(png_ptr, info_ptr);
slouken@0
   342
slouken@368
   343
    lib.png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
slouken@368
   344
            &color_type, &interlace_type, NULL, NULL);
slouken@0
   345
slouken@368
   346
    /* Allocate the SDL surface to hold the image */
slouken@368
   347
    Rmask = Gmask = Bmask = Amask = 0 ;
slouken@368
   348
    num_channels = lib.png_get_channels(png_ptr, info_ptr);
slouken@368
   349
    if ( num_channels >= 3 ) {
aschiffler@343
   350
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@390
   351
        Rmask = 0x000000FF;
slouken@390
   352
        Gmask = 0x0000FF00;
slouken@390
   353
        Bmask = 0x00FF0000;
slouken@390
   354
        Amask = (num_channels == 4) ? 0xFF000000 : 0;
aschiffler@343
   355
#else
slouken@390
   356
        int s = (num_channels == 4) ? 0 : 8;
slouken@390
   357
        Rmask = 0xFF000000 >> s;
slouken@390
   358
        Gmask = 0x00FF0000 >> s;
slouken@390
   359
        Bmask = 0x0000FF00 >> s;
slouken@390
   360
        Amask = 0x000000FF >> s;
aschiffler@343
   361
#endif
slouken@368
   362
    }
slouken@368
   363
    surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height,
slouken@368
   364
            bit_depth*num_channels, Rmask,Gmask,Bmask,Amask);
slouken@368
   365
    if ( surface == NULL ) {
slouken@368
   366
        error = SDL_GetError();
slouken@368
   367
        goto done;
slouken@368
   368
    }
slouken@0
   369
slouken@368
   370
    if (ckey != -1) {
slouken@390
   371
        if (color_type != PNG_COLOR_TYPE_PALETTE) {
slouken@368
   372
            /* FIXME: Should these be truncated or shifted down? */
slouken@390
   373
            ckey = SDL_MapRGB(surface->format,
slouken@390
   374
                         (Uint8)transv->red,
slouken@390
   375
                         (Uint8)transv->green,
slouken@390
   376
                         (Uint8)transv->blue);
slouken@390
   377
        }
slouken@390
   378
        SDL_SetColorKey(surface, SDL_TRUE, ckey);
slouken@368
   379
    }
slouken@0
   380
slouken@368
   381
    /* Create the array of pointers to image data */
slouken@368
   382
    row_pointers = (png_bytep*) SDL_malloc(sizeof(png_bytep)*height);
slouken@424
   383
    if (!row_pointers) {
slouken@368
   384
        error = "Out of memory";
slouken@368
   385
        goto done;
slouken@368
   386
    }
slouken@368
   387
    for (row = 0; row < (int)height; row++) {
slouken@368
   388
        row_pointers[row] = (png_bytep)
slouken@368
   389
                (Uint8 *)surface->pixels + row*surface->pitch;
slouken@368
   390
    }
slouken@0
   391
slouken@368
   392
    /* Read the entire image in one go */
slouken@368
   393
    lib.png_read_image(png_ptr, row_pointers);
slouken@0
   394
slouken@368
   395
    /* and we're done!  (png_read_end() can be omitted if no processing of
slouken@368
   396
     * post-IDAT text/time/etc. is desired)
slouken@368
   397
     * In some cases it can't read PNG's created by some popular programs (ACDSEE),
slouken@368
   398
     * we do not want to process comments, so we omit png_read_end
slouken@100
   399
slouken@368
   400
    lib.png_read_end(png_ptr, info_ptr);
slouken@368
   401
    */
slouken@0
   402
slouken@368
   403
    /* Load the palette, if any */
slouken@368
   404
    palette = surface->format->palette;
slouken@368
   405
    if ( palette ) {
slouken@368
   406
        int png_num_palette;
slouken@368
   407
        png_colorp png_palette;
slouken@368
   408
        lib.png_get_PLTE(png_ptr, info_ptr, &png_palette, &png_num_palette);
slouken@368
   409
        if (color_type == PNG_COLOR_TYPE_GRAY) {
slouken@390
   410
            palette->ncolors = 256;
slouken@390
   411
            for (i = 0; i < 256; i++) {
slouken@529
   412
                palette->colors[i].r = (Uint8)i;
slouken@529
   413
                palette->colors[i].g = (Uint8)i;
slouken@529
   414
                palette->colors[i].b = (Uint8)i;
slouken@390
   415
            }
slouken@368
   416
        } else if (png_num_palette > 0 ) {
slouken@390
   417
            palette->ncolors = png_num_palette;
slouken@390
   418
            for ( i=0; i<png_num_palette; ++i ) {
slouken@390
   419
                palette->colors[i].b = png_palette[i].blue;
slouken@390
   420
                palette->colors[i].g = png_palette[i].green;
slouken@390
   421
                palette->colors[i].r = png_palette[i].red;
slouken@390
   422
            }
slouken@368
   423
        }
slouken@368
   424
    }
slouken@0
   425
slouken@368
   426
done:   /* Clean up and return */
slouken@368
   427
    if ( png_ptr ) {
slouken@368
   428
        lib.png_destroy_read_struct(&png_ptr,
slouken@368
   429
                                info_ptr ? &info_ptr : (png_infopp)0,
slouken@368
   430
                                (png_infopp)0);
slouken@368
   431
    }
slouken@368
   432
    if ( row_pointers ) {
slouken@368
   433
        SDL_free(row_pointers);
slouken@368
   434
    }
slouken@368
   435
    if ( error ) {
slouken@368
   436
        SDL_RWseek(src, start, RW_SEEK_SET);
slouken@368
   437
        if ( surface ) {
slouken@368
   438
            SDL_FreeSurface(surface);
slouken@368
   439
            surface = NULL;
slouken@368
   440
        }
slouken@451
   441
        IMG_SetError("%s", error);
slouken@368
   442
    }
slouken@368
   443
    return(surface);
slouken@0
   444
}
slouken@0
   445
slouken@0
   446
#else
slouken@0
   447
slouken@205
   448
int IMG_InitPNG()
slouken@205
   449
{
slouken@368
   450
    IMG_SetError("PNG images are not supported");
slouken@368
   451
    return(-1);
slouken@205
   452
}
slouken@205
   453
slouken@205
   454
void IMG_QuitPNG()
slouken@205
   455
{
slouken@205
   456
}
slouken@205
   457
slouken@0
   458
/* See if an image is contained in a data source */
slouken@0
   459
int IMG_isPNG(SDL_RWops *src)
slouken@0
   460
{
slouken@368
   461
    return(0);
slouken@0
   462
}
slouken@0
   463
slouken@0
   464
/* Load a PNG type image from an SDL datasource */
slouken@0
   465
SDL_Surface *IMG_LoadPNG_RW(SDL_RWops *src)
slouken@0
   466
{
slouken@368
   467
    return(NULL);
slouken@0
   468
}
slouken@0
   469
slouken@0
   470
#endif /* LOAD_PNG */
slouken@237
   471
slouken@237
   472
#endif /* !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND) */
slouken@391
   473
slouken@391
   474
/* We'll always have PNG save support */
slouken@391
   475
#define SAVE_PNG
slouken@391
   476
slouken@391
   477
#ifdef SAVE_PNG
slouken@391
   478
slouken@391
   479
int IMG_SavePNG(SDL_Surface *surface, const char *file)
slouken@391
   480
{
slouken@391
   481
    SDL_RWops *dst = SDL_RWFromFile(file, "wb");
slouken@391
   482
    if (dst) {
slouken@391
   483
        return IMG_SavePNG_RW(surface, dst, 1);
slouken@391
   484
    } else {
slouken@391
   485
        return -1;
slouken@391
   486
    }
slouken@391
   487
}
slouken@391
   488
slouken@506
   489
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@506
   490
static const Uint32 png_format = SDL_PIXELFORMAT_ABGR8888;
slouken@506
   491
#else
slouken@506
   492
static const Uint32 png_format = SDL_PIXELFORMAT_RGBA8888;
slouken@506
   493
#endif
slouken@506
   494
slouken@506
   495
#ifdef USE_LIBPNG
slouken@506
   496
slouken@506
   497
static void png_write_data(png_structp png_ptr, png_bytep src, png_size_t size)
slouken@506
   498
{
slouken@506
   499
    SDL_RWops *dst = (SDL_RWops *)lib.png_get_io_ptr(png_ptr);
slouken@506
   500
    SDL_RWwrite(dst, src, size, 1);
slouken@506
   501
}
slouken@506
   502
slouken@506
   503
static void png_flush_data(png_structp png_ptr)
slouken@506
   504
{
slouken@506
   505
}
slouken@506
   506
slouken@506
   507
static int IMG_SavePNG_RW_libpng(SDL_Surface *surface, SDL_RWops *dst, int freedst)
slouken@506
   508
{
slouken@506
   509
    if (dst) {
slouken@506
   510
        png_structp png_ptr;
slouken@506
   511
        png_infop info_ptr;
slouken@506
   512
        png_colorp color_ptr = NULL;
slouken@506
   513
        SDL_Surface *source = surface;
slouken@506
   514
        SDL_Palette *palette;
slouken@506
   515
        int png_color_type = PNG_COLOR_TYPE_RGB_ALPHA;
slouken@506
   516
slouken@506
   517
        png_ptr = lib.png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
slouken@506
   518
        if (png_ptr == NULL) {
slouken@506
   519
            SDL_SetError("Couldn't allocate memory for PNG file or incompatible PNG dll");
slouken@506
   520
            return -1;
slouken@506
   521
        }
slouken@506
   522
slouken@506
   523
        info_ptr = lib.png_create_info_struct(png_ptr);
slouken@506
   524
        if (info_ptr == NULL) {
slouken@506
   525
            lib.png_destroy_write_struct(&png_ptr, NULL);
slouken@506
   526
            SDL_SetError("Couldn't create image information for PNG file");
slouken@506
   527
            return -1;
slouken@506
   528
        }
slouken@506
   529
slouken@506
   530
#ifndef LIBPNG_VERSION_12
slouken@506
   531
        if (setjmp(*lib.png_set_longjmp_fn(png_ptr, longjmp, sizeof (jmp_buf))))
slouken@506
   532
#else
slouken@506
   533
        if (setjmp(png_ptr->jmpbuf))
slouken@506
   534
#endif
slouken@506
   535
        {
slouken@506
   536
            lib.png_destroy_write_struct(&png_ptr, &info_ptr);
slouken@506
   537
            SDL_SetError("Error writing the PNG file.");
slouken@506
   538
            return -1;
slouken@506
   539
        }
slouken@506
   540
slouken@506
   541
        palette = surface->format->palette;
slouken@506
   542
        if (palette) {
slouken@506
   543
            const int ncolors = palette->ncolors;
slouken@506
   544
            int i;
slouken@506
   545
slouken@506
   546
            color_ptr = SDL_malloc(sizeof(png_colorp) * ncolors);
slouken@506
   547
            if (color_ptr == NULL)
slouken@506
   548
            {
slouken@506
   549
                lib.png_destroy_write_struct(&png_ptr, &info_ptr);
slouken@506
   550
                SDL_SetError("Couldn't create palette for PNG file");
slouken@506
   551
                return -1;
slouken@506
   552
            }
slouken@506
   553
            for (i = 0; i < ncolors; i++) {
slouken@506
   554
                color_ptr[i].red = palette->colors[i].r;
slouken@506
   555
                color_ptr[i].green = palette->colors[i].g;
slouken@506
   556
                color_ptr[i].blue = palette->colors[i].b;
slouken@506
   557
            }
slouken@506
   558
            lib.png_set_PLTE(png_ptr, info_ptr, color_ptr, ncolors);
slouken@506
   559
            png_color_type = PNG_COLOR_TYPE_PALETTE;
slouken@506
   560
        }
slouken@506
   561
        else if (surface->format->format != png_format) {
slouken@506
   562
            source = SDL_ConvertSurfaceFormat(surface, png_format, 0);
slouken@506
   563
        }
slouken@506
   564
slouken@506
   565
        lib.png_set_write_fn(png_ptr, dst, png_write_data, png_flush_data);
slouken@506
   566
slouken@506
   567
        lib.png_set_IHDR(png_ptr, info_ptr, surface->w, surface->h,
slouken@506
   568
                         8, png_color_type, PNG_INTERLACE_NONE,
slouken@506
   569
                         PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
slouken@506
   570
slouken@506
   571
        if (source) {
slouken@506
   572
            png_bytep *row_pointers;
slouken@506
   573
            int row;
slouken@506
   574
slouken@506
   575
            row_pointers = (png_bytep *) SDL_malloc(sizeof(png_bytep) * source->h);
slouken@506
   576
            if (!row_pointers) {
slouken@506
   577
                lib.png_destroy_write_struct(&png_ptr, &info_ptr);
slouken@506
   578
                SDL_SetError("Out of memory");
slouken@506
   579
                return -1;
slouken@506
   580
            }
slouken@506
   581
            for (row = 0; row < (int)source->h; row++) {
slouken@506
   582
                row_pointers[row] = (png_bytep) (Uint8 *) source->pixels + row * source->pitch;
slouken@506
   583
            }
slouken@506
   584
slouken@506
   585
            lib.png_set_rows(png_ptr, info_ptr, row_pointers);
slouken@506
   586
            lib.png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
slouken@506
   587
slouken@506
   588
            SDL_free(row_pointers);
slouken@506
   589
            if (source != surface) {
slouken@506
   590
                SDL_FreeSurface(source);
slouken@506
   591
            }
slouken@506
   592
        }
slouken@506
   593
        lib.png_destroy_write_struct(&png_ptr, &info_ptr);
slouken@506
   594
        if (color_ptr) {
slouken@506
   595
            SDL_free(color_ptr);
slouken@506
   596
        }
slouken@506
   597
        if (freedst) {
slouken@506
   598
            SDL_RWclose(dst);
slouken@506
   599
        }
slouken@506
   600
    } else {
slouken@506
   601
        SDL_SetError("Passed NULL dst");
slouken@506
   602
        return -1;
slouken@506
   603
    }
slouken@506
   604
    return 0;
slouken@506
   605
}
slouken@506
   606
slouken@506
   607
#endif /* USE_LIBPNG */
slouken@506
   608
slouken@545
   609
/* Replace C runtime functions with SDL C runtime functions for building on Windows */
slouken@545
   610
#define MINIZ_NO_STDIO
slouken@545
   611
#define MINIZ_NO_TIME
slouken@545
   612
#define MINIZ_SDL_MALLOC
slouken@545
   613
#define MZ_ASSERT(x) SDL_assert(x)
slouken@589
   614
#undef memcpy
slouken@589
   615
#define memcpy  SDL_memcpy
slouken@545
   616
#undef memset
slouken@571
   617
#define memset  SDL_memset
slouken@571
   618
#define strlen  SDL_strlen
slouken@545
   619
slouken@506
   620
#include "miniz.h"
slouken@506
   621
slouken@506
   622
static int IMG_SavePNG_RW_miniz(SDL_Surface *surface, SDL_RWops *dst, int freedst)
slouken@391
   623
{
slouken@391
   624
    int result = -1;
slouken@391
   625
slouken@391
   626
    if (dst) {
slouken@529
   627
        size_t size = 0;
slouken@427
   628
        void *png = NULL;
slouken@391
   629
slouken@391
   630
        if (surface->format->format == png_format) {
slouken@431
   631
            png = tdefl_write_image_to_png_file_in_memory(surface->pixels, surface->w, surface->h, surface->format->BytesPerPixel, surface->pitch, &size);
slouken@391
   632
        } else {
slouken@391
   633
            SDL_Surface *cvt = SDL_ConvertSurfaceFormat(surface, png_format, 0);
slouken@391
   634
            if (cvt) {
slouken@431
   635
                png = tdefl_write_image_to_png_file_in_memory(cvt->pixels, cvt->w, cvt->h, cvt->format->BytesPerPixel, cvt->pitch, &size);
slouken@391
   636
                SDL_FreeSurface(cvt);
slouken@391
   637
            }
slouken@391
   638
        }
slouken@391
   639
        if (png) {
slouken@391
   640
            if (SDL_RWwrite(dst, png, size, 1)) {
slouken@391
   641
                result = 0;
slouken@391
   642
            }
slouken@391
   643
            SDL_free(png);
slouken@391
   644
        } else {
slouken@391
   645
            SDL_SetError("Failed to convert and save image");
slouken@391
   646
        }
slouken@391
   647
        if (freedst) {
slouken@391
   648
            SDL_RWclose(dst);
slouken@391
   649
        }
slouken@391
   650
    } else {
slouken@391
   651
        SDL_SetError("Passed NULL dst");
slouken@391
   652
    }
slouken@391
   653
    return result;
slouken@391
   654
}
slouken@391
   655
slouken@506
   656
int IMG_SavePNG_RW(SDL_Surface *surface, SDL_RWops *dst, int freedst)
slouken@506
   657
{
slouken@506
   658
    static int (*rw_func)(SDL_Surface *surface, SDL_RWops *dst, int freedst);
slouken@506
   659
slouken@506
   660
    if (!rw_func)
slouken@506
   661
    {
slouken@506
   662
#ifdef USE_LIBPNG
slouken@506
   663
        if (IMG_Init(IMG_INIT_PNG)) {
slouken@506
   664
            rw_func = IMG_SavePNG_RW_libpng;
slouken@506
   665
        } else
slouken@506
   666
#endif
slouken@506
   667
            rw_func = IMG_SavePNG_RW_miniz;
slouken@506
   668
    }
slouken@506
   669
slouken@506
   670
    return rw_func(surface, dst, freedst);
slouken@506
   671
}
slouken@506
   672
slouken@391
   673
#endif /* SAVE_PNG */