2010-02-08 08:23:39 -08:00
|
|
|
/* Copyright (c) 2010 Wildfire Games
|
2009-04-18 10:00:33 -07:00
|
|
|
*
|
2010-02-08 08:23:39 -08:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
|
* the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
|
* in all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2009-04-18 10:00:33 -07:00
|
|
|
*/
|
|
|
|
|
|
2009-04-18 10:51:05 -07:00
|
|
|
/*
|
|
|
|
|
* PNG codec using libpng.
|
2006-04-23 16:14:18 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
2007-05-04 10:30:32 -07:00
|
|
|
#include "lib/external_libraries/png.h"
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
#include "lib/byte_order.h"
|
|
|
|
|
#include "tex_codec.h"
|
|
|
|
|
#include "lib/timer.h"
|
|
|
|
|
|
|
|
|
|
#if MSC_VERSION
|
|
|
|
|
|
|
|
|
|
// squelch "dtor / setjmp interaction" warnings.
|
|
|
|
|
// all attempts to resolve the underlying problem failed; apparently
|
|
|
|
|
// the warning is generated if setjmp is used at all in C++ mode.
|
|
|
|
|
// (png_*_impl have no code that would trigger ctors/dtors, nor are any
|
|
|
|
|
// called in their prolog/epilog code).
|
|
|
|
|
# pragma warning(disable: 4611)
|
|
|
|
|
|
|
|
|
|
#endif // MSC_VERSION
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// pass data from PNG file in memory to libpng
|
|
|
|
|
static void io_read(png_struct* png_ptr, u8* data, png_size_t length)
|
|
|
|
|
{
|
|
|
|
|
DynArray* da = (DynArray*)png_ptr->io_ptr;
|
|
|
|
|
if(da_read(da, data, length) != 0)
|
|
|
|
|
png_error(png_ptr, "io_read failed");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// write libpng output to PNG file
|
|
|
|
|
static void io_write(png_struct* png_ptr, u8* data, png_size_t length)
|
|
|
|
|
{
|
|
|
|
|
DynArray* da = (DynArray*)png_ptr->io_ptr;
|
|
|
|
|
if(da_append(da, data, length) != 0)
|
|
|
|
|
png_error(png_ptr, "io_write failed");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void io_flush(png_structp UNUSED(png_ptr))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 11:48:32 -07:00
|
|
|
static LibError png_transform(Tex* UNUSED(t), size_t UNUSED(transforms))
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2006-09-22 06:19:40 -07:00
|
|
|
return INFO::TEX_CODEC_CANNOT_HANDLE;
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// note: it's not worth combining png_encode and png_decode, due to
|
|
|
|
|
// libpng read/write interface differences (grr).
|
|
|
|
|
|
|
|
|
|
// split out of png_decode to simplify resource cleanup and avoid
|
|
|
|
|
// "dtor / setjmp interaction" warning.
|
2007-12-22 10:15:52 -08:00
|
|
|
static LibError png_decode_impl(DynArray* da, png_structp png_ptr, png_infop info_ptr, Tex* t)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
|
|
|
|
png_set_read_fn(png_ptr, da, io_read);
|
|
|
|
|
|
|
|
|
|
// read header and determine format
|
|
|
|
|
png_read_info(png_ptr, info_ptr);
|
|
|
|
|
png_uint_32 w, h;
|
|
|
|
|
int bit_depth, colour_type;
|
|
|
|
|
png_get_IHDR(png_ptr, info_ptr, &w, &h, &bit_depth, &colour_type, 0, 0, 0);
|
|
|
|
|
const size_t pitch = png_get_rowbytes(png_ptr, info_ptr);
|
|
|
|
|
const u32 bpp = (u32)(pitch/w * 8);
|
|
|
|
|
|
2008-09-18 04:27:55 -07:00
|
|
|
size_t flags = 0;
|
2006-04-23 16:14:18 -07:00
|
|
|
if(bpp == 32)
|
|
|
|
|
flags |= TEX_ALPHA;
|
|
|
|
|
if(colour_type == PNG_COLOR_TYPE_GRAY)
|
|
|
|
|
flags |= TEX_GREY;
|
|
|
|
|
|
|
|
|
|
// make sure format is acceptable
|
|
|
|
|
if(bit_depth != 8)
|
2006-09-22 06:19:40 -07:00
|
|
|
WARN_RETURN(ERR::TEX_NOT_8BIT_PRECISION);
|
2006-04-23 16:14:18 -07:00
|
|
|
if(colour_type & PNG_COLOR_MASK_PALETTE)
|
2006-09-22 06:19:40 -07:00
|
|
|
WARN_RETURN(ERR::TEX_INVALID_COLOR_TYPE);
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
const size_t img_size = pitch * h;
|
2007-12-20 12:14:21 -08:00
|
|
|
shared_ptr<u8> data = io_Allocate(img_size);
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2007-12-22 10:15:52 -08:00
|
|
|
shared_ptr<RowPtr> rows = tex_codec_alloc_rows(data.get(), h, pitch, TEX_TOP_DOWN, 0);
|
|
|
|
|
png_read_image(png_ptr, (png_bytepp)rows.get());
|
2006-04-23 16:14:18 -07:00
|
|
|
png_read_end(png_ptr, info_ptr);
|
|
|
|
|
|
|
|
|
|
// success; make sure all data was consumed.
|
|
|
|
|
debug_assert(da->pos == da->cur_size);
|
|
|
|
|
|
|
|
|
|
// store image info
|
2007-12-20 12:14:21 -08:00
|
|
|
t->data = data;
|
|
|
|
|
t->dataSize = img_size;
|
|
|
|
|
t->ofs = 0;
|
2006-04-23 16:14:18 -07:00
|
|
|
t->w = w;
|
|
|
|
|
t->h = h;
|
|
|
|
|
t->bpp = bpp;
|
|
|
|
|
t->flags = flags;
|
|
|
|
|
|
2006-09-22 06:19:40 -07:00
|
|
|
return INFO::OK;
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// split out of png_encode to simplify resource cleanup and avoid
|
|
|
|
|
// "dtor / setjmp interaction" warning.
|
2007-12-22 10:15:52 -08:00
|
|
|
static LibError png_encode_impl(Tex* t, png_structp png_ptr, png_infop info_ptr, DynArray* da)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2008-09-18 04:27:55 -07:00
|
|
|
const png_uint_32 w = (png_uint_32)t->w, h = (png_uint_32)t->h;
|
2006-04-23 16:14:18 -07:00
|
|
|
const size_t pitch = w * t->bpp / 8;
|
|
|
|
|
|
|
|
|
|
int colour_type;
|
|
|
|
|
switch(t->flags & (TEX_GREY|TEX_ALPHA))
|
|
|
|
|
{
|
|
|
|
|
case TEX_GREY|TEX_ALPHA:
|
|
|
|
|
colour_type = PNG_COLOR_TYPE_GRAY_ALPHA;
|
|
|
|
|
break;
|
|
|
|
|
case TEX_GREY:
|
|
|
|
|
colour_type = PNG_COLOR_TYPE_GRAY;
|
|
|
|
|
break;
|
|
|
|
|
case TEX_ALPHA:
|
|
|
|
|
colour_type = PNG_COLOR_TYPE_RGB_ALPHA;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
colour_type = PNG_COLOR_TYPE_RGB;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
png_set_write_fn(png_ptr, da, io_write, io_flush);
|
|
|
|
|
png_set_IHDR(png_ptr, info_ptr, w, h, 8, colour_type,
|
|
|
|
|
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
|
|
|
|
|
|
|
|
|
|
u8* data = tex_get_data(t);
|
2007-12-22 10:15:52 -08:00
|
|
|
shared_ptr<RowPtr> rows = tex_codec_alloc_rows(data, h, pitch, t->flags, TEX_TOP_DOWN);
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
// PNG is native RGB.
|
|
|
|
|
const int png_transforms = (t->flags & TEX_BGR)? PNG_TRANSFORM_BGR : PNG_TRANSFORM_IDENTITY;
|
|
|
|
|
|
2007-12-22 10:15:52 -08:00
|
|
|
png_set_rows(png_ptr, info_ptr, (png_bytepp)rows.get());
|
2006-04-23 16:14:18 -07:00
|
|
|
png_write_png(png_ptr, info_ptr, png_transforms, 0);
|
|
|
|
|
|
2006-09-22 06:19:40 -07:00
|
|
|
return INFO::OK;
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static bool png_is_hdr(const u8* file)
|
|
|
|
|
{
|
|
|
|
|
// don't use png_sig_cmp, so we don't pull in libpng for
|
|
|
|
|
// this check alone (it might not actually be used).
|
|
|
|
|
return *(u32*)file == FOURCC('\x89','P','N','G');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-11-03 13:46:35 -08:00
|
|
|
static bool png_is_ext(const std::wstring& extension)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2009-11-03 13:46:35 -08:00
|
|
|
return !wcscasecmp(extension.c_str(), L".png");
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static size_t png_hdr_size(const u8* UNUSED(file))
|
|
|
|
|
{
|
|
|
|
|
return 0; // libpng returns decoded image data; no header
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TIMER_ADD_CLIENT(tc_png_decode);
|
|
|
|
|
|
|
|
|
|
// limitation: palette images aren't supported
|
2007-01-05 17:33:13 -08:00
|
|
|
static LibError png_decode(DynArray* RESTRICT da, Tex* RESTRICT t)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
|
|
|
|
TIMER_ACCRUE(tc_png_decode);
|
|
|
|
|
|
2007-12-20 12:14:21 -08:00
|
|
|
LibError ret = ERR::FAIL;
|
2006-04-23 16:14:18 -07:00
|
|
|
png_infop info_ptr = 0;
|
|
|
|
|
|
|
|
|
|
// allocate PNG structures; use default stderr and longjmp error handlers
|
2007-12-20 12:14:21 -08:00
|
|
|
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
|
2006-04-23 16:14:18 -07:00
|
|
|
if(!png_ptr)
|
2007-12-20 12:14:21 -08:00
|
|
|
WARN_RETURN(ERR::FAIL);
|
2006-04-23 16:14:18 -07:00
|
|
|
info_ptr = png_create_info_struct(png_ptr);
|
|
|
|
|
if(!info_ptr)
|
|
|
|
|
goto fail;
|
|
|
|
|
// setup error handling
|
|
|
|
|
if(setjmp(png_jmpbuf(png_ptr)))
|
|
|
|
|
{
|
|
|
|
|
// libpng longjmps here after an error
|
|
|
|
|
goto fail;
|
2007-12-20 12:14:21 -08:00
|
|
|
}
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2007-12-22 10:15:52 -08:00
|
|
|
ret = png_decode_impl(da, png_ptr, info_ptr, t);
|
2007-12-20 12:14:21 -08:00
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
png_destroy_read_struct(&png_ptr, &info_ptr, 0);
|
|
|
|
|
|
|
|
|
|
return ret;
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// limitation: palette images aren't supported
|
2007-01-05 17:33:13 -08:00
|
|
|
static LibError png_encode(Tex* RESTRICT t, DynArray* RESTRICT da)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2007-12-20 12:14:21 -08:00
|
|
|
LibError ret = ERR::FAIL;
|
2006-04-23 16:14:18 -07:00
|
|
|
png_infop info_ptr = 0;
|
|
|
|
|
|
|
|
|
|
// allocate PNG structures; use default stderr and longjmp error handlers
|
2007-12-20 12:14:21 -08:00
|
|
|
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
|
2006-04-23 16:14:18 -07:00
|
|
|
if(!png_ptr)
|
2007-12-20 12:14:21 -08:00
|
|
|
WARN_RETURN(ERR::FAIL);
|
2006-04-23 16:14:18 -07:00
|
|
|
info_ptr = png_create_info_struct(png_ptr);
|
|
|
|
|
if(!info_ptr)
|
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
|
|
// setup error handling
|
|
|
|
|
if(setjmp(png_jmpbuf(png_ptr)))
|
|
|
|
|
{
|
|
|
|
|
// libpng longjmps here after an error
|
2007-12-20 12:14:21 -08:00
|
|
|
goto fail;
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
2007-12-22 10:15:52 -08:00
|
|
|
ret = png_encode_impl(t, png_ptr, info_ptr, da);
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
// shared cleanup
|
2007-12-20 12:14:21 -08:00
|
|
|
fail:
|
2006-04-23 16:14:18 -07:00
|
|
|
png_destroy_write_struct(&png_ptr, &info_ptr);
|
2007-12-20 12:14:21 -08:00
|
|
|
return ret;
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEX_CODEC_REGISTER(png);
|