2006-08-20 12:55:46 +00:00
|
|
|
/*
|
2006-08-21 13:09:55 +00:00
|
|
|
** pngtexture.cpp
|
2006-08-20 12:55:46 +00:00
|
|
|
** Texture class for PNG images
|
|
|
|
**
|
|
|
|
**---------------------------------------------------------------------------
|
2007-12-20 04:36:43 +00:00
|
|
|
** Copyright 2004-2007 Randy Heit
|
2006-08-20 12:55:46 +00:00
|
|
|
** All rights reserved.
|
|
|
|
**
|
|
|
|
** Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions
|
|
|
|
** are met:
|
|
|
|
**
|
|
|
|
** 1. Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in the
|
|
|
|
** documentation and/or other materials provided with the distribution.
|
|
|
|
** 3. The name of the author may not be used to endorse or promote products
|
|
|
|
** derived from this software without specific prior written permission.
|
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
**
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "doomtype.h"
|
|
|
|
#include "files.h"
|
|
|
|
#include "r_local.h"
|
|
|
|
#include "w_wad.h"
|
|
|
|
#include "templates.h"
|
|
|
|
#include "m_png.h"
|
2008-04-15 18:05:39 +00:00
|
|
|
#include "bitmap.h"
|
2008-09-15 14:11:05 +00:00
|
|
|
#include "v_palette.h"
|
2006-08-20 12:55:46 +00:00
|
|
|
|
2008-06-01 20:43:02 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// A PNG texture
|
|
|
|
//
|
|
|
|
//==========================================================================
|
2006-08-20 12:55:46 +00:00
|
|
|
|
2008-06-01 20:43:02 +00:00
|
|
|
class FPNGTexture : public FTexture
|
2006-08-20 12:55:46 +00:00
|
|
|
{
|
2008-06-01 20:43:02 +00:00
|
|
|
public:
|
|
|
|
FPNGTexture (FileReader &lump, int lumpnum, const FString &filename, int width, int height, BYTE bitdepth, BYTE colortype, BYTE interlace);
|
|
|
|
~FPNGTexture ();
|
|
|
|
|
|
|
|
const BYTE *GetColumn (unsigned int column, const Span **spans_out);
|
|
|
|
const BYTE *GetPixels ();
|
|
|
|
void Unload ();
|
|
|
|
FTextureFormat GetFormat ();
|
2009-09-30 10:41:24 +00:00
|
|
|
int CopyTrueColorPixels(FBitmap *bmp, int x, int y, int rotate, FCopyInfo *inf = NULL);
|
2008-06-01 20:43:02 +00:00
|
|
|
bool UseBasePalette();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
FString SourceFile;
|
|
|
|
BYTE *Pixels;
|
|
|
|
Span **Spans;
|
|
|
|
|
|
|
|
BYTE BitDepth;
|
|
|
|
BYTE ColorType;
|
|
|
|
BYTE Interlace;
|
|
|
|
|
|
|
|
BYTE *PaletteMap;
|
|
|
|
int PaletteSize;
|
|
|
|
DWORD StartOfIDAT;
|
2006-08-20 12:55:46 +00:00
|
|
|
|
2008-06-01 20:43:02 +00:00
|
|
|
void MakeTexture ();
|
|
|
|
|
|
|
|
friend class FTexture;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
FTexture *PNGTexture_TryCreate(FileReader & data, int lumpnum)
|
2006-08-20 12:55:46 +00:00
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
|
|
|
DWORD dw;
|
|
|
|
WORD w[2];
|
|
|
|
BYTE b[4];
|
|
|
|
} first4bytes;
|
|
|
|
|
|
|
|
DWORD width, height;
|
|
|
|
BYTE bitdepth, colortype, compression, filter, interlace;
|
|
|
|
|
|
|
|
// This is most likely a PNG, but make sure. (Note that if the
|
|
|
|
// first 4 bytes match, but later bytes don't, we assume it's
|
|
|
|
// a corrupt PNG.)
|
2008-06-01 20:43:02 +00:00
|
|
|
|
|
|
|
data.Seek(0, SEEK_SET);
|
|
|
|
if (data.Read (first4bytes.b, 4) != 4) return NULL;
|
|
|
|
if (first4bytes.dw != MAKE_ID(137,'P','N','G')) return NULL;
|
|
|
|
if (data.Read (first4bytes.b, 4) != 4) return NULL;
|
2006-08-20 12:55:46 +00:00
|
|
|
if (first4bytes.dw != MAKE_ID(13,10,26,10)) return NULL;
|
2008-06-01 20:43:02 +00:00
|
|
|
if (data.Read (first4bytes.b, 4) != 4) return NULL;
|
2006-08-20 12:55:46 +00:00
|
|
|
if (first4bytes.dw != MAKE_ID(0,0,0,13)) return NULL;
|
2008-06-01 20:43:02 +00:00
|
|
|
if (data.Read (first4bytes.b, 4) != 4) return NULL;
|
2006-08-20 12:55:46 +00:00
|
|
|
if (first4bytes.dw != MAKE_ID('I','H','D','R')) return NULL;
|
|
|
|
|
|
|
|
// The PNG looks valid so far. Check the IHDR to make sure it's a
|
|
|
|
// type of PNG we support.
|
2009-09-04 22:59:41 +00:00
|
|
|
data.Read(&width, 4);
|
|
|
|
data.Read(&height, 4);
|
|
|
|
data >> bitdepth >> colortype >> compression >> filter >> interlace;
|
2006-08-20 12:55:46 +00:00
|
|
|
|
|
|
|
if (compression != 0 || filter != 0 || interlace > 1)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (!((1 << colortype) & 0x5D))
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (!((1 << bitdepth) & 0x116))
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-12-20 04:36:43 +00:00
|
|
|
// Just for completeness, make sure the PNG has something more than an IHDR.
|
2006-08-20 12:55:46 +00:00
|
|
|
data.Seek (4, SEEK_CUR);
|
|
|
|
data.Read (first4bytes.b, 4);
|
|
|
|
if (first4bytes.dw == 0)
|
|
|
|
{
|
|
|
|
data.Read (first4bytes.b, 4);
|
|
|
|
if (first4bytes.dw == MAKE_ID('I','E','N','D'))
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-28 01:54:14 +00:00
|
|
|
return new FPNGTexture (data, lumpnum, FString(), BigLong((int)width), BigLong((int)height),
|
2006-08-20 12:55:46 +00:00
|
|
|
bitdepth, colortype, interlace);
|
|
|
|
}
|
|
|
|
|
2008-06-01 20:43:02 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
FTexture *PNGTexture_CreateFromFile(PNGHandle *png, const FString &filename)
|
2007-12-28 01:54:14 +00:00
|
|
|
{
|
|
|
|
DWORD width, height;
|
|
|
|
BYTE bitdepth, colortype, compression, filter, interlace;
|
|
|
|
|
|
|
|
if (M_FindPNGChunk(png, MAKE_ID('I','H','D','R')) == 0)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the IHDR to make sure it's a type of PNG we support.
|
2009-09-04 22:59:41 +00:00
|
|
|
png->File->Read(&width, 4);
|
|
|
|
png->File->Read(&height, 4);
|
|
|
|
(*png->File) >> bitdepth >> colortype >> compression >> filter >> interlace;
|
2007-12-28 01:54:14 +00:00
|
|
|
|
|
|
|
if (compression != 0 || filter != 0 || interlace > 1)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (!((1 << colortype) & 0x5D))
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (!((1 << bitdepth) & 0x116))
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new FPNGTexture (*png->File, -1, filename, BigLong((int)width), BigLong((int)height),
|
|
|
|
bitdepth, colortype, interlace);
|
|
|
|
}
|
|
|
|
|
2008-06-01 20:43:02 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2007-12-28 01:54:14 +00:00
|
|
|
FPNGTexture::FPNGTexture (FileReader &lump, int lumpnum, const FString &filename, int width, int height,
|
2006-08-20 12:55:46 +00:00
|
|
|
BYTE depth, BYTE colortype, BYTE interlace)
|
2008-11-30 12:49:27 +00:00
|
|
|
: FTexture(NULL, lumpnum), SourceFile(filename), Pixels(0), Spans(0),
|
2006-08-20 12:55:46 +00:00
|
|
|
BitDepth(depth), ColorType(colortype), Interlace(interlace),
|
|
|
|
PaletteMap(0), PaletteSize(0), StartOfIDAT(0)
|
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
|
|
|
DWORD palette[256];
|
|
|
|
BYTE pngpal[256][3];
|
|
|
|
};
|
|
|
|
BYTE trans[256];
|
|
|
|
bool havetRNS = false;
|
|
|
|
DWORD len, id;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
UseType = TEX_MiscPatch;
|
|
|
|
LeftOffset = 0;
|
|
|
|
TopOffset = 0;
|
|
|
|
bMasked = false;
|
|
|
|
|
|
|
|
Width = width;
|
|
|
|
Height = height;
|
|
|
|
CalcBitSize ();
|
|
|
|
|
2009-09-04 22:59:41 +00:00
|
|
|
memset(trans, 255, 256);
|
2006-08-20 12:55:46 +00:00
|
|
|
|
|
|
|
// Parse pre-IDAT chunks. I skip the CRCs. Is that bad?
|
2009-09-04 22:59:41 +00:00
|
|
|
lump.Seek(33, SEEK_SET);
|
2006-08-20 12:55:46 +00:00
|
|
|
|
2009-09-04 22:59:41 +00:00
|
|
|
lump.Read(&len, 4);
|
|
|
|
lump.Read(&id, 4);
|
2006-08-20 12:55:46 +00:00
|
|
|
while (id != MAKE_ID('I','D','A','T') && id != MAKE_ID('I','E','N','D'))
|
|
|
|
{
|
|
|
|
len = BigLong((unsigned int)len);
|
|
|
|
switch (id)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
lump.Seek (len, SEEK_CUR);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MAKE_ID('g','r','A','b'):
|
|
|
|
// This is like GRAB found in an ILBM, except coordinates use 4 bytes
|
|
|
|
{
|
|
|
|
DWORD hotx, hoty;
|
2007-01-23 01:13:17 +00:00
|
|
|
int ihotx, ihoty;
|
|
|
|
|
2009-09-04 22:59:41 +00:00
|
|
|
lump.Read(&hotx, 4);
|
|
|
|
lump.Read(&hoty, 4);
|
2007-01-23 01:13:17 +00:00
|
|
|
ihotx = BigLong((int)hotx);
|
|
|
|
ihoty = BigLong((int)hoty);
|
|
|
|
if (ihotx < -32768 || ihotx > 32767)
|
|
|
|
{
|
|
|
|
Printf ("X-Offset for PNG texture %s is bad: %d (0x%08x)\n", Wads.GetLumpFullName (lumpnum), ihotx, ihotx);
|
|
|
|
ihotx = 0;
|
|
|
|
}
|
|
|
|
if (ihoty < -32768 || ihoty > 32767)
|
|
|
|
{
|
|
|
|
Printf ("Y-Offset for PNG texture %s is bad: %d (0x%08x)\n", Wads.GetLumpFullName (lumpnum), ihoty, ihoty);
|
|
|
|
ihoty = 0;
|
|
|
|
}
|
2009-09-04 22:59:41 +00:00
|
|
|
LeftOffset = ihotx;
|
|
|
|
TopOffset = ihoty;
|
2006-08-20 12:55:46 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MAKE_ID('P','L','T','E'):
|
|
|
|
PaletteSize = MIN<int> (len / 3, 256);
|
|
|
|
lump.Read (pngpal, PaletteSize * 3);
|
|
|
|
if (PaletteSize * 3 != (int)len)
|
|
|
|
{
|
|
|
|
lump.Seek (len - PaletteSize * 3, SEEK_CUR);
|
|
|
|
}
|
|
|
|
for (i = PaletteSize - 1; i >= 0; --i)
|
|
|
|
{
|
|
|
|
palette[i] = MAKERGB(pngpal[i][0], pngpal[i][1], pngpal[i][2]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MAKE_ID('t','R','N','S'):
|
|
|
|
lump.Read (trans, len);
|
|
|
|
havetRNS = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MAKE_ID('a','l','P','h'):
|
|
|
|
bAlphaTexture = true;
|
|
|
|
bMasked = true;
|
|
|
|
break;
|
|
|
|
}
|
2009-09-04 22:59:41 +00:00
|
|
|
lump.Seek(4, SEEK_CUR); // Skip CRC
|
|
|
|
lump.Read(&len, 4);
|
2006-08-20 12:55:46 +00:00
|
|
|
id = MAKE_ID('I','E','N','D');
|
2009-09-04 22:59:41 +00:00
|
|
|
lump.Read(&id, 4);
|
2006-08-20 12:55:46 +00:00
|
|
|
}
|
|
|
|
StartOfIDAT = lump.Tell() - 8;
|
|
|
|
|
|
|
|
switch (colortype)
|
|
|
|
{
|
|
|
|
case 4: // Grayscale + Alpha
|
|
|
|
bMasked = true;
|
|
|
|
// intentional fall-through
|
|
|
|
|
|
|
|
case 0: // Grayscale
|
|
|
|
if (!bAlphaTexture)
|
|
|
|
{
|
|
|
|
if (colortype == 0 && havetRNS && trans[0] != 0)
|
|
|
|
{
|
|
|
|
bMasked = true;
|
|
|
|
PaletteSize = 256;
|
|
|
|
PaletteMap = new BYTE[256];
|
|
|
|
memcpy (PaletteMap, GrayMap, 256);
|
|
|
|
PaletteMap[trans[0]] = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PaletteMap = GrayMap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3: // Paletted
|
|
|
|
PaletteMap = new BYTE[PaletteSize];
|
|
|
|
GPalette.MakeRemap (palette, PaletteMap, trans, PaletteSize);
|
|
|
|
for (i = 0; i < PaletteSize; ++i)
|
|
|
|
{
|
|
|
|
if (trans[i] == 0)
|
|
|
|
{
|
|
|
|
bMasked = true;
|
|
|
|
PaletteMap[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 6: // RGB + Alpha
|
|
|
|
bMasked = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-01 20:43:02 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2006-08-20 12:55:46 +00:00
|
|
|
FPNGTexture::~FPNGTexture ()
|
|
|
|
{
|
|
|
|
Unload ();
|
|
|
|
if (Spans != NULL)
|
|
|
|
{
|
|
|
|
FreeSpans (Spans);
|
|
|
|
Spans = NULL;
|
|
|
|
}
|
|
|
|
if (PaletteMap != NULL && PaletteMap != GrayMap)
|
|
|
|
{
|
|
|
|
delete[] PaletteMap;
|
|
|
|
PaletteMap = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-01 20:43:02 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2006-08-20 12:55:46 +00:00
|
|
|
void FPNGTexture::Unload ()
|
|
|
|
{
|
|
|
|
if (Pixels != NULL)
|
|
|
|
{
|
|
|
|
delete[] Pixels;
|
|
|
|
Pixels = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-01 20:43:02 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2007-12-20 04:36:43 +00:00
|
|
|
FTextureFormat FPNGTexture::GetFormat()
|
|
|
|
{
|
2007-12-27 11:53:09 +00:00
|
|
|
#if 0
|
2007-12-20 04:36:43 +00:00
|
|
|
switch (ColorType)
|
|
|
|
{
|
|
|
|
case 3: return TEX_Pal;
|
|
|
|
case 0: return TEX_Gray;
|
|
|
|
default: return TEX_RGB;
|
|
|
|
}
|
2007-12-27 11:53:09 +00:00
|
|
|
#else
|
|
|
|
// For now, create a true color texture to preserve all colors.
|
|
|
|
return TEX_RGB;
|
|
|
|
#endif
|
2007-12-20 04:36:43 +00:00
|
|
|
}
|
|
|
|
|
2008-06-01 20:43:02 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2006-08-20 12:55:46 +00:00
|
|
|
const BYTE *FPNGTexture::GetColumn (unsigned int column, const Span **spans_out)
|
|
|
|
{
|
|
|
|
if (Pixels == NULL)
|
|
|
|
{
|
|
|
|
MakeTexture ();
|
|
|
|
}
|
|
|
|
if ((unsigned)column >= (unsigned)Width)
|
|
|
|
{
|
|
|
|
if (WidthMask + 1 == Width)
|
|
|
|
{
|
|
|
|
column &= WidthMask;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
column %= Width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (spans_out != NULL)
|
|
|
|
{
|
2008-04-14 12:10:45 +00:00
|
|
|
if (Spans == NULL)
|
|
|
|
{
|
|
|
|
Spans = CreateSpans (Pixels);
|
|
|
|
}
|
2006-08-20 12:55:46 +00:00
|
|
|
*spans_out = Spans[column];
|
|
|
|
}
|
|
|
|
return Pixels + column*Height;
|
|
|
|
}
|
|
|
|
|
2008-06-01 20:43:02 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2006-08-20 12:55:46 +00:00
|
|
|
const BYTE *FPNGTexture::GetPixels ()
|
|
|
|
{
|
|
|
|
if (Pixels == NULL)
|
|
|
|
{
|
|
|
|
MakeTexture ();
|
|
|
|
}
|
|
|
|
return Pixels;
|
|
|
|
}
|
|
|
|
|
2008-06-01 20:43:02 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2006-08-20 12:55:46 +00:00
|
|
|
void FPNGTexture::MakeTexture ()
|
|
|
|
{
|
2007-12-28 01:54:14 +00:00
|
|
|
FileReader *lump;
|
|
|
|
|
|
|
|
if (SourceLump >= 0)
|
|
|
|
{
|
|
|
|
lump = new FWadLump(Wads.OpenLumpNum(SourceLump));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lump = new FileReader(SourceFile.GetChars());
|
|
|
|
}
|
2006-08-20 12:55:46 +00:00
|
|
|
|
|
|
|
Pixels = new BYTE[Width*Height];
|
|
|
|
if (StartOfIDAT == 0)
|
|
|
|
{
|
|
|
|
memset (Pixels, 0x99, Width*Height);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DWORD len, id;
|
2007-12-28 01:54:14 +00:00
|
|
|
lump->Seek (StartOfIDAT, SEEK_SET);
|
2009-09-04 22:59:41 +00:00
|
|
|
lump->Read(&len, 4);
|
|
|
|
lump->Read(&id, 4);
|
2006-08-20 12:55:46 +00:00
|
|
|
|
|
|
|
if (ColorType == 0 || ColorType == 3) /* Grayscale and paletted */
|
|
|
|
{
|
2007-12-28 01:54:14 +00:00
|
|
|
M_ReadIDAT (lump, Pixels, Width, Height, Width, BitDepth, ColorType, Interlace, BigLong((unsigned int)len));
|
2006-08-20 12:55:46 +00:00
|
|
|
|
|
|
|
if (Width == Height)
|
|
|
|
{
|
|
|
|
if (PaletteMap != NULL)
|
|
|
|
{
|
|
|
|
FlipSquareBlockRemap (Pixels, Width, Height, PaletteMap);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FlipSquareBlock (Pixels, Width, Height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
BYTE *newpix = new BYTE[Width*Height];
|
|
|
|
if (PaletteMap != NULL)
|
|
|
|
{
|
2008-02-09 03:09:56 +00:00
|
|
|
FlipNonSquareBlockRemap (newpix, Pixels, Width, Height, Width, PaletteMap);
|
2006-08-20 12:55:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FlipNonSquareBlock (newpix, Pixels, Width, Height, Width);
|
|
|
|
}
|
|
|
|
BYTE *oldpix = Pixels;
|
|
|
|
Pixels = newpix;
|
|
|
|
delete[] oldpix;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else /* RGB and/or Alpha present */
|
|
|
|
{
|
|
|
|
int bytesPerPixel = ColorType == 2 ? 3 : ColorType == 4 ? 2 : 4;
|
|
|
|
BYTE *tempix = new BYTE[Width * Height * bytesPerPixel];
|
|
|
|
BYTE *in, *out;
|
|
|
|
int x, y, pitch, backstep;
|
|
|
|
|
2007-12-28 01:54:14 +00:00
|
|
|
M_ReadIDAT (lump, tempix, Width, Height, Width*bytesPerPixel, BitDepth, ColorType, Interlace, BigLong((unsigned int)len));
|
2006-08-20 12:55:46 +00:00
|
|
|
in = tempix;
|
|
|
|
out = Pixels;
|
|
|
|
|
|
|
|
// Convert from source format to paletted, column-major.
|
|
|
|
// Formats with alpha maps are reduced to only 1 bit of alpha.
|
|
|
|
switch (ColorType)
|
|
|
|
{
|
|
|
|
case 2: // RGB
|
|
|
|
pitch = Width * 3;
|
|
|
|
backstep = Height * pitch - 3;
|
|
|
|
for (x = Width; x > 0; --x)
|
|
|
|
{
|
|
|
|
for (y = Height; y > 0; --y)
|
|
|
|
{
|
|
|
|
*out++ = RGB32k[in[0]>>3][in[1]>>3][in[2]>>3];
|
|
|
|
in += pitch;
|
|
|
|
}
|
|
|
|
in -= backstep;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4: // Grayscale + Alpha
|
|
|
|
pitch = Width * 2;
|
|
|
|
backstep = Height * pitch - 2;
|
|
|
|
if (PaletteMap != NULL)
|
|
|
|
{
|
|
|
|
for (x = Width; x > 0; --x)
|
|
|
|
{
|
|
|
|
for (y = Height; y > 0; --y)
|
|
|
|
{
|
|
|
|
*out++ = in[1] < 128 ? 0 : PaletteMap[in[0]];
|
|
|
|
in += pitch;
|
|
|
|
}
|
|
|
|
in -= backstep;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (x = Width; x > 0; --x)
|
|
|
|
{
|
|
|
|
for (y = Height; y > 0; --y)
|
|
|
|
{
|
|
|
|
*out++ = in[1] < 128 ? 0 : in[0];
|
|
|
|
in += pitch;
|
|
|
|
}
|
|
|
|
in -= backstep;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 6: // RGB + Alpha
|
|
|
|
pitch = Width * 4;
|
|
|
|
backstep = Height * pitch - 4;
|
|
|
|
for (x = Width; x > 0; --x)
|
|
|
|
{
|
|
|
|
for (y = Height; y > 0; --y)
|
|
|
|
{
|
|
|
|
*out++ = in[3] < 128 ? 0 : RGB32k[in[0]>>3][in[1]>>3][in[2]>>3];
|
|
|
|
in += pitch;
|
|
|
|
}
|
|
|
|
in -= backstep;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
delete[] tempix;
|
|
|
|
}
|
|
|
|
}
|
2007-12-28 01:54:14 +00:00
|
|
|
delete lump;
|
2006-08-20 12:55:46 +00:00
|
|
|
}
|
|
|
|
|
2007-12-20 18:53:35 +00:00
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// FPNGTexture::CopyTrueColorPixels
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
2009-09-30 10:41:24 +00:00
|
|
|
int FPNGTexture::CopyTrueColorPixels(FBitmap *bmp, int x, int y, int rotate, FCopyInfo *inf)
|
2007-12-20 18:53:35 +00:00
|
|
|
{
|
|
|
|
// Parse pre-IDAT chunks. I skip the CRCs. Is that bad?
|
|
|
|
PalEntry pe[256];
|
|
|
|
DWORD len, id;
|
2007-12-28 01:54:14 +00:00
|
|
|
FileReader *lump;
|
2009-09-04 22:59:41 +00:00
|
|
|
static char bpp[] = {1, 0, 3, 1, 2, 0, 4};
|
2007-12-20 18:53:35 +00:00
|
|
|
int pixwidth = Width * bpp[ColorType];
|
2009-09-04 22:59:41 +00:00
|
|
|
int transpal = false;
|
2007-12-20 18:53:35 +00:00
|
|
|
|
2007-12-28 01:54:14 +00:00
|
|
|
if (SourceLump >= 0)
|
|
|
|
{
|
|
|
|
lump = new FWadLump(Wads.OpenLumpNum(SourceLump));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lump = new FileReader(SourceFile.GetChars());
|
|
|
|
}
|
|
|
|
|
2009-09-04 22:59:41 +00:00
|
|
|
lump->Seek(33, SEEK_SET);
|
|
|
|
for(int i = 0; i < 256; i++) // default to a gray map
|
|
|
|
pe[i] = PalEntry(255,i,i,i);
|
2007-12-20 18:53:35 +00:00
|
|
|
|
2009-09-04 22:59:41 +00:00
|
|
|
lump->Read(&len, 4);
|
|
|
|
lump->Read(&id, 4);
|
2007-12-20 18:53:35 +00:00
|
|
|
while (id != MAKE_ID('I','D','A','T') && id != MAKE_ID('I','E','N','D'))
|
|
|
|
{
|
|
|
|
len = BigLong((unsigned int)len);
|
|
|
|
switch (id)
|
|
|
|
{
|
|
|
|
default:
|
2007-12-28 01:54:14 +00:00
|
|
|
lump->Seek (len, SEEK_CUR);
|
2007-12-20 18:53:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MAKE_ID('P','L','T','E'):
|
2009-09-04 22:59:41 +00:00
|
|
|
for(int i = 0; i < PaletteSize; i++)
|
2007-12-27 04:30:12 +00:00
|
|
|
{
|
2007-12-28 01:54:14 +00:00
|
|
|
(*lump) >> pe[i].r >> pe[i].g >> pe[i].b;
|
2007-12-27 04:30:12 +00:00
|
|
|
}
|
2007-12-20 18:53:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MAKE_ID('t','R','N','S'):
|
2009-09-04 22:59:41 +00:00
|
|
|
for(DWORD i = 0; i < len; i++)
|
2007-12-20 18:53:35 +00:00
|
|
|
{
|
2007-12-28 01:54:14 +00:00
|
|
|
(*lump) >> pe[i].a;
|
2009-09-04 22:59:41 +00:00
|
|
|
if (pe[i].a != 0 && pe[i].a != 255)
|
|
|
|
transpal = true;
|
2007-12-20 18:53:35 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2009-09-04 22:59:41 +00:00
|
|
|
lump->Seek(4, SEEK_CUR); // Skip CRC
|
|
|
|
lump->Read(&len, 4);
|
2007-12-20 18:53:35 +00:00
|
|
|
id = MAKE_ID('I','E','N','D');
|
2009-09-04 22:59:41 +00:00
|
|
|
lump->Read(&id, 4);
|
2007-12-20 18:53:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BYTE * Pixels = new BYTE[pixwidth * Height];
|
|
|
|
|
2007-12-28 01:54:14 +00:00
|
|
|
lump->Seek (StartOfIDAT, SEEK_SET);
|
2009-09-04 22:59:41 +00:00
|
|
|
lump->Read(&len, 4);
|
2009-09-07 05:48:34 +00:00
|
|
|
lump->Read(&id, 4);
|
2007-12-28 01:54:14 +00:00
|
|
|
M_ReadIDAT (lump, Pixels, Width, Height, pixwidth, BitDepth, ColorType, Interlace, BigLong((unsigned int)len));
|
|
|
|
delete lump;
|
2007-12-20 18:53:35 +00:00
|
|
|
|
|
|
|
switch (ColorType)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
case 3:
|
2009-09-30 10:41:24 +00:00
|
|
|
bmp->CopyPixelData(x, y, Pixels, Width, Height, 1, Width, rotate, pe, inf);
|
2007-12-20 18:53:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
2009-09-30 10:41:24 +00:00
|
|
|
bmp->CopyPixelDataRGB(x, y, Pixels, Width, Height, 3, pixwidth, rotate, CF_RGB, inf);
|
2007-12-20 18:53:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
2009-09-30 10:41:24 +00:00
|
|
|
bmp->CopyPixelDataRGB(x, y, Pixels, Width, Height, 2, pixwidth, rotate, CF_IA, inf);
|
2007-12-20 18:53:35 +00:00
|
|
|
transpal = -1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 6:
|
2009-09-30 10:41:24 +00:00
|
|
|
bmp->CopyPixelDataRGB(x, y, Pixels, Width, Height, 4, pixwidth, rotate, CF_RGBA, inf);
|
2007-12-20 18:53:35 +00:00
|
|
|
transpal = -1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
delete[] Pixels;
|
|
|
|
return transpal;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// This doesn't check if the palette is identical with the base palette
|
|
|
|
// I don't think it's worth the hassle because it's only of importance
|
|
|
|
// when compositing multipatch textures.
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
bool FPNGTexture::UseBasePalette()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|