2007-01-15 12:09:25 -08:00
|
|
|
/*
|
|
|
|
This file is part of Warzone 2100.
|
|
|
|
Copyright (C) 1999-2004 Eidos Interactive
|
|
|
|
Copyright (C) 2005-2007 Warzone Resurrection Project
|
|
|
|
|
|
|
|
Warzone 2100 is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Warzone 2100 is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Warzone 2100; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2007-06-28 10:47:08 -07:00
|
|
|
#include <png.h>
|
|
|
|
#include <setjmp.h>
|
2006-11-06 06:40:07 -08:00
|
|
|
#include <string.h>
|
2007-04-16 15:29:44 -07:00
|
|
|
#include <physfs.h>
|
2006-11-06 06:40:07 -08:00
|
|
|
|
2006-06-02 12:34:58 -07:00
|
|
|
#include "lib/framework/frame.h"
|
2007-06-28 10:47:08 -07:00
|
|
|
#include "ivisdef.h"
|
|
|
|
|
|
|
|
#include "ivispatch.h"
|
|
|
|
|
2007-04-16 15:29:44 -07:00
|
|
|
static const unsigned int PNG_BYTES_TO_CHECK = 4;
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
static void wzpng_read_data(png_structp ctx, png_bytep area, png_size_t size)
|
|
|
|
{
|
|
|
|
|
2007-04-16 15:29:44 -07:00
|
|
|
PHYSFS_file* fileHandle = (PHYSFS_file*)png_get_io_ptr(ctx);
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2007-04-16 15:29:44 -07:00
|
|
|
PHYSFS_read(fileHandle, area, 1, size);
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2007-04-16 15:29:44 -07:00
|
|
|
static inline void PNGCleanup(png_infop *info_ptr, png_structp *png_ptr, PHYSFS_file* fileHandle)
|
2007-04-16 10:34:11 -07:00
|
|
|
{
|
|
|
|
if (*info_ptr != NULL)
|
|
|
|
png_destroy_info_struct(*png_ptr, info_ptr);
|
2007-04-16 10:49:15 -07:00
|
|
|
if (*png_ptr != NULL)
|
2007-04-16 10:34:11 -07:00
|
|
|
png_destroy_read_struct(png_ptr, NULL, NULL);
|
2007-04-16 15:29:44 -07:00
|
|
|
if (fileHandle != NULL)
|
|
|
|
PHYSFS_close(fileHandle);
|
2007-04-16 10:34:11 -07:00
|
|
|
}
|
|
|
|
|
2007-04-16 15:29:44 -07:00
|
|
|
BOOL pie_PNGLoadFile(const char *fileName, iTexture *s)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
|
|
|
png_structp png_ptr = NULL;
|
|
|
|
png_infop info_ptr = NULL;
|
|
|
|
|
2007-04-17 05:23:54 -07:00
|
|
|
unsigned char PNGheader[PNG_BYTES_TO_CHECK];
|
2007-04-16 15:29:44 -07:00
|
|
|
PHYSFS_sint64 readSize;
|
|
|
|
|
|
|
|
// Open file
|
|
|
|
PHYSFS_file* fileHandle = PHYSFS_openRead(fileName);
|
|
|
|
if (fileHandle == NULL)
|
|
|
|
{
|
|
|
|
debug(LOG_ERROR, "pie_PNGLoadFile: PHYSFS_openRead failed with error: %s\n", PHYSFS_getLastError());
|
|
|
|
PNGCleanup(&info_ptr, &png_ptr, fileHandle);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2006-06-03 16:46:03 -07:00
|
|
|
|
2007-04-16 15:29:44 -07:00
|
|
|
// Read PNG header from file
|
|
|
|
readSize = PHYSFS_read(fileHandle, PNGheader, 1, PNG_BYTES_TO_CHECK);
|
|
|
|
if (readSize < PNG_BYTES_TO_CHECK)
|
|
|
|
{
|
|
|
|
debug(LOG_ERROR, "pie_PNGLoadFile: PHYSFS_read failed with error: %s\n", PHYSFS_getLastError());
|
|
|
|
PNGCleanup(&info_ptr, &png_ptr, fileHandle);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2007-04-16 15:29:44 -07:00
|
|
|
// Verify the PNG header to be correct
|
|
|
|
if (png_sig_cmp(PNGheader, 0, PNG_BYTES_TO_CHECK)) {
|
2007-06-28 10:47:08 -07:00
|
|
|
debug(LOG_3D, "pie_PNGLoadMem: Did not recognize PNG header in buffer");
|
2007-04-16 15:29:44 -07:00
|
|
|
PNGCleanup(&info_ptr, &png_ptr, fileHandle);
|
2007-04-16 10:34:11 -07:00
|
|
|
return FALSE;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2007-04-16 15:29:44 -07:00
|
|
|
// Seek back to start of file, libpng needs this
|
|
|
|
PHYSFS_seek(fileHandle, 0);
|
|
|
|
|
|
|
|
|
|
|
|
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
if (png_ptr == NULL) {
|
|
|
|
debug(LOG_3D, "pie_PNGLoadMem: Unable to create png struct");
|
2007-04-16 15:29:44 -07:00
|
|
|
PNGCleanup(&info_ptr, &png_ptr, fileHandle);
|
2007-04-16 10:34:11 -07:00
|
|
|
return FALSE;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
info_ptr = png_create_info_struct(png_ptr);
|
|
|
|
|
|
|
|
if (info_ptr == NULL) {
|
|
|
|
debug(LOG_3D, "pie_PNGLoadMem: Unable to create png info struct");
|
2007-04-16 15:29:44 -07:00
|
|
|
PNGCleanup(&info_ptr, &png_ptr, fileHandle);
|
2007-04-16 10:34:11 -07:00
|
|
|
return FALSE;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2007-04-16 15:29:44 -07:00
|
|
|
// Set libpng's failure jump position to the if branch,
|
|
|
|
// setjmp evaluates to false so the else branch will be executed at first
|
2007-06-28 10:47:08 -07:00
|
|
|
if (setjmp(png_jmpbuf(png_ptr))) {
|
|
|
|
debug(LOG_3D, "pie_PNGLoadMem: Error decoding PNG data");
|
2007-04-16 15:29:44 -07:00
|
|
|
PNGCleanup(&info_ptr, &png_ptr, fileHandle);
|
2007-04-16 10:34:11 -07:00
|
|
|
return FALSE;
|
2007-06-28 10:47:08 -07:00
|
|
|
} else {
|
|
|
|
int bit_depth, color_type, interlace_type;
|
|
|
|
png_uint_32 width, height;
|
|
|
|
|
|
|
|
/* Set up the input control */
|
2007-04-16 15:29:44 -07:00
|
|
|
png_set_read_fn(png_ptr, fileHandle, wzpng_read_data);
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
/* Read PNG header info */
|
|
|
|
png_read_info(png_ptr, info_ptr);
|
|
|
|
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
|
|
|
|
&color_type, &interlace_type, NULL, NULL);
|
|
|
|
|
|
|
|
/* tell libpng to strip 16 bit/color files down to 8 bits/color */
|
|
|
|
png_set_strip_16(png_ptr) ;
|
|
|
|
|
|
|
|
/* Extract multiple pixels with bit depths of 1, 2, and 4 from a single
|
|
|
|
* byte into separate bytes (useful for paletted and grayscale images).
|
|
|
|
*/
|
|
|
|
png_set_packing(png_ptr);
|
|
|
|
|
|
|
|
/* More transformations to ensure we end up with 32bpp, 4 channel RGBA */
|
|
|
|
png_set_gray_to_rgb(png_ptr);
|
|
|
|
png_set_palette_to_rgb(png_ptr);
|
|
|
|
png_set_tRNS_to_alpha(png_ptr);
|
|
|
|
png_set_filler(png_ptr, 0xFF, PNG_FILLER_AFTER);
|
|
|
|
png_set_gray_1_2_4_to_8(png_ptr);
|
|
|
|
|
|
|
|
/* scale greyscale values to the range 0..255 */
|
|
|
|
if(color_type == PNG_COLOR_TYPE_GRAY)
|
|
|
|
png_set_expand(png_ptr);
|
|
|
|
|
|
|
|
png_read_update_info(png_ptr, info_ptr);
|
|
|
|
|
|
|
|
{
|
|
|
|
png_uint_32 w, h;
|
|
|
|
|
|
|
|
png_get_IHDR(png_ptr, info_ptr, (png_uint_32*)(&w),
|
|
|
|
(png_uint_32*)(&h), &bit_depth,
|
|
|
|
&color_type, &interlace_type, NULL, NULL);
|
|
|
|
|
|
|
|
s->width = w;
|
|
|
|
s->height = h;
|
2006-07-03 12:43:12 -07:00
|
|
|
// Freeing s->bmp before allocating new mem would give a HEAP error on Windows (Invalid Address specified to RtlFreeHeap( x, x )).
|
2006-09-17 13:39:25 -07:00
|
|
|
s->bmp = (iBitmap*)malloc(w*h*info_ptr->channels);
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
png_bytep* row_pointers = (png_bytep*)malloc(s->height*sizeof(png_bytep));
|
2007-02-19 06:10:44 -08:00
|
|
|
char* pdata;
|
2007-03-16 09:20:16 -07:00
|
|
|
int i;
|
2007-06-28 10:47:08 -07:00
|
|
|
const unsigned int line_size = s->width*info_ptr->channels;
|
|
|
|
|
|
|
|
for (i = 0, pdata = s->bmp;
|
|
|
|
i < s->height;
|
|
|
|
++i, pdata += line_size) {
|
|
|
|
row_pointers[i] = (png_bytep)pdata;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the entire image in one go */
|
|
|
|
png_read_image(png_ptr, row_pointers);
|
|
|
|
|
|
|
|
free(row_pointers);
|
|
|
|
|
|
|
|
/* read rest of file, get additional chunks in info_ptr - REQUIRED */
|
|
|
|
png_read_end(png_ptr, info_ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-16 15:29:44 -07:00
|
|
|
PNGCleanup(&info_ptr, &png_ptr, fileHandle);
|
2007-06-28 10:47:08 -07:00
|
|
|
return TRUE;
|
|
|
|
}
|