2016-03-01 15:47:10 +00:00
|
|
|
/*
|
|
|
|
** patchtexture.cpp
|
|
|
|
** Texture class for single Doom patches
|
|
|
|
**
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
** Copyright 2004-2006 Randy Heit
|
|
|
|
** 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 "w_wad.h"
|
|
|
|
#include "templates.h"
|
|
|
|
#include "v_palette.h"
|
2018-03-21 23:29:01 +00:00
|
|
|
#include "v_video.h"
|
|
|
|
#include "bitmap.h"
|
2016-03-01 15:47:10 +00:00
|
|
|
#include "textures/textures.h"
|
2018-03-18 11:36:14 +00:00
|
|
|
#include "r_data/r_translate.h"
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
// posts are runs of non masked source pixels
|
|
|
|
struct column_t
|
|
|
|
{
|
2017-03-08 17:44:37 +00:00
|
|
|
uint8_t topdelta; // -1 is the last post in a column
|
|
|
|
uint8_t length; // length data bytes follows
|
2016-03-01 15:47:10 +00:00
|
|
|
};
|
|
|
|
|
2018-03-21 23:29:01 +00:00
|
|
|
bool checkPatchForAlpha(const void *buffer, uint32_t length);
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// A texture that is just a single Doom format patch
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2018-03-18 11:36:14 +00:00
|
|
|
class FPatchTexture : public FWorldTexture
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2018-03-18 11:36:14 +00:00
|
|
|
bool badflag = false;
|
2018-03-21 23:29:01 +00:00
|
|
|
bool isalpha = false;
|
2016-03-01 15:47:10 +00:00
|
|
|
public:
|
2018-03-21 23:29:01 +00:00
|
|
|
FPatchTexture (int lumpnum, patch_t *header, bool isalphatex);
|
2018-03-18 11:36:14 +00:00
|
|
|
uint8_t *MakeTexture (FRenderStyle style) override;
|
2018-03-21 23:29:01 +00:00
|
|
|
int CopyTrueColorPixels(FBitmap *bmp, int x, int y, int rotate, FCopyInfo *inf) override;
|
2018-03-18 11:36:14 +00:00
|
|
|
void DetectBadPatches();
|
2018-03-21 23:29:01 +00:00
|
|
|
|
|
|
|
bool UseBasePalette() override { return !isalpha; }
|
|
|
|
FTextureFormat GetFormat() override { return isalpha ? TEX_RGB : TEX_Pal; } // should be TEX_Gray instead of TEX_RGB. Maybe later when all is working.
|
2016-03-01 15:47:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Checks if the currently open lump can be a Doom patch
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2018-03-21 23:29:01 +00:00
|
|
|
static bool CheckIfPatch(FileReader & file, bool &isalpha)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
if (file.GetLength() < 13) return false; // minimum length of a valid Doom patch
|
|
|
|
|
2017-03-08 17:44:37 +00:00
|
|
|
uint8_t *data = new uint8_t[file.GetLength()];
|
2018-03-11 17:32:00 +00:00
|
|
|
file.Seek(0, FileReader::SeekSet);
|
2016-03-01 15:47:10 +00:00
|
|
|
file.Read(data, file.GetLength());
|
|
|
|
|
|
|
|
const patch_t *foo = (const patch_t *)data;
|
|
|
|
|
|
|
|
int height = LittleShort(foo->height);
|
|
|
|
int width = LittleShort(foo->width);
|
|
|
|
|
|
|
|
if (height > 0 && height <= 2048 && width > 0 && width <= 2048 && width < file.GetLength()/4)
|
|
|
|
{
|
|
|
|
// The dimensions seem like they might be valid for a patch, so
|
|
|
|
// check the column directory for extra security. At least one
|
|
|
|
// column must begin exactly at the end of the column directory,
|
|
|
|
// and none of them must point past the end of the patch.
|
|
|
|
bool gapAtStart = true;
|
|
|
|
int x;
|
|
|
|
|
|
|
|
for (x = 0; x < width; ++x)
|
|
|
|
{
|
2017-03-08 17:44:37 +00:00
|
|
|
uint32_t ofs = LittleLong(foo->columnofs[x]);
|
|
|
|
if (ofs == (uint32_t)width * 4 + 8)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
gapAtStart = false;
|
|
|
|
}
|
2017-03-08 17:44:37 +00:00
|
|
|
else if (ofs >= (uint32_t)(file.GetLength())) // Need one byte for an empty column (but there's patches that don't know that!)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
delete [] data;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete [] data;
|
2018-03-21 23:29:01 +00:00
|
|
|
if (!gapAtStart)
|
|
|
|
{
|
|
|
|
// only check this if the texture passed validation.
|
|
|
|
// Here is a good point because we already have a valid buffer of the lump's data.
|
2018-03-21 23:54:03 +00:00
|
|
|
isalpha = checkPatchForAlpha(data, (uint32_t)file.GetLength());
|
2018-03-21 23:29:01 +00:00
|
|
|
}
|
2016-03-01 15:47:10 +00:00
|
|
|
return !gapAtStart;
|
|
|
|
}
|
|
|
|
delete [] data;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2018-03-11 17:32:00 +00:00
|
|
|
FTexture *PatchTexture_TryCreate(FileReader & file, int lumpnum)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
patch_t header;
|
2018-03-21 23:29:01 +00:00
|
|
|
bool isalpha;
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2018-03-21 23:29:01 +00:00
|
|
|
if (!CheckIfPatch(file, isalpha)) return NULL;
|
2018-03-11 17:32:00 +00:00
|
|
|
file.Seek(0, FileReader::SeekSet);
|
2018-03-11 17:20:49 +00:00
|
|
|
header.width = file.ReadUInt16();
|
|
|
|
header.height = file.ReadUInt16();
|
|
|
|
header.leftoffset = file.ReadInt16();
|
|
|
|
header.topoffset = file.ReadInt16();
|
2018-03-21 23:29:01 +00:00
|
|
|
return new FPatchTexture(lumpnum, &header, isalpha);
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2018-03-21 23:29:01 +00:00
|
|
|
FPatchTexture::FPatchTexture (int lumpnum, patch_t * header, bool isalphatex)
|
2018-03-18 11:36:14 +00:00
|
|
|
: FWorldTexture(NULL, lumpnum)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2018-03-21 23:29:01 +00:00
|
|
|
isalpha = isalphatex;
|
2016-03-01 15:47:10 +00:00
|
|
|
Width = header->width;
|
|
|
|
Height = header->height;
|
|
|
|
LeftOffset = header->leftoffset;
|
|
|
|
TopOffset = header->topoffset;
|
2018-03-18 11:36:14 +00:00
|
|
|
DetectBadPatches();
|
2016-03-01 15:47:10 +00:00
|
|
|
CalcBitSize ();
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2018-03-18 11:36:14 +00:00
|
|
|
uint8_t *FPatchTexture::MakeTexture (FRenderStyle style)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-03-08 17:44:37 +00:00
|
|
|
uint8_t *remap, remaptable[256];
|
2016-03-01 15:47:10 +00:00
|
|
|
int numspans;
|
|
|
|
const column_t *maxcol;
|
|
|
|
int x;
|
|
|
|
|
|
|
|
FMemLump lump = Wads.ReadLump (SourceLump);
|
|
|
|
const patch_t *patch = (const patch_t *)lump.GetMem();
|
|
|
|
|
2017-03-08 17:44:37 +00:00
|
|
|
maxcol = (const column_t *)((const uint8_t *)patch + Wads.LumpLength (SourceLump) - 3);
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2018-03-21 23:54:03 +00:00
|
|
|
remap = GetRemap(style, isalpha);
|
|
|
|
// Special case for skies
|
|
|
|
if (bNoRemap0 && remap == GPalette.Remap)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2018-03-21 23:29:01 +00:00
|
|
|
memcpy(remaptable, GPalette.Remap, 256);
|
2016-03-01 15:47:10 +00:00
|
|
|
remaptable[0] = 0;
|
|
|
|
remap = remaptable;
|
|
|
|
}
|
|
|
|
|
2018-03-18 11:36:14 +00:00
|
|
|
if (badflag)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2018-03-18 11:36:14 +00:00
|
|
|
auto Pixels = new uint8_t[Width * Height];
|
2017-03-08 17:44:37 +00:00
|
|
|
uint8_t *out;
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
// Draw the image to the buffer
|
|
|
|
for (x = 0, out = Pixels; x < Width; ++x)
|
|
|
|
{
|
2017-03-08 17:44:37 +00:00
|
|
|
const uint8_t *in = (const uint8_t *)patch + LittleLong(patch->columnofs[x]) + 3;
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
for (int y = Height; y > 0; --y)
|
|
|
|
{
|
|
|
|
*out = remap[*in];
|
|
|
|
out++, in++;
|
|
|
|
}
|
|
|
|
}
|
2018-03-18 11:36:14 +00:00
|
|
|
return Pixels;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add a little extra space at the end if the texture's height is not
|
|
|
|
// a power of 2, in case somebody accidentally makes it repeat vertically.
|
|
|
|
int numpix = Width * Height + (1 << HeightBits) - Height;
|
|
|
|
|
|
|
|
numspans = Width;
|
|
|
|
|
2018-03-18 11:36:14 +00:00
|
|
|
auto Pixels = new uint8_t[numpix];
|
2016-03-01 15:47:10 +00:00
|
|
|
memset (Pixels, 0, numpix);
|
|
|
|
|
|
|
|
// Draw the image to the buffer
|
|
|
|
for (x = 0; x < Width; ++x)
|
|
|
|
{
|
2017-03-08 17:44:37 +00:00
|
|
|
uint8_t *outtop = Pixels + x*Height;
|
|
|
|
const column_t *column = (const column_t *)((const uint8_t *)patch + LittleLong(patch->columnofs[x]));
|
2016-03-01 15:47:10 +00:00
|
|
|
int top = -1;
|
|
|
|
|
|
|
|
while (column < maxcol && column->topdelta != 0xFF)
|
|
|
|
{
|
|
|
|
if (column->topdelta <= top)
|
|
|
|
{
|
|
|
|
top += column->topdelta;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
top = column->topdelta;
|
|
|
|
}
|
|
|
|
|
|
|
|
int len = column->length;
|
2017-03-08 17:44:37 +00:00
|
|
|
uint8_t *out = outtop + top;
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
if (len != 0)
|
|
|
|
{
|
|
|
|
if (top + len > Height) // Clip posts that extend past the bottom
|
|
|
|
{
|
|
|
|
len = Height - top;
|
|
|
|
}
|
|
|
|
if (len > 0)
|
|
|
|
{
|
|
|
|
numspans++;
|
|
|
|
|
2017-03-08 17:44:37 +00:00
|
|
|
const uint8_t *in = (const uint8_t *)column + 3;
|
2016-03-01 15:47:10 +00:00
|
|
|
for (int i = 0; i < len; ++i)
|
|
|
|
{
|
|
|
|
out[i] = remap[in[i]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-08 17:44:37 +00:00
|
|
|
column = (const column_t *)((const uint8_t *)column + column->length + 4);
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-18 11:36:14 +00:00
|
|
|
return Pixels;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 23:29:01 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
int FPatchTexture::CopyTrueColorPixels(FBitmap *bmp, int x, int y, int rotate, FCopyInfo *inf)
|
|
|
|
{
|
|
|
|
if (!isalpha) return FTexture::CopyTrueColorPixels(bmp, x, y, rotate, inf);
|
|
|
|
else return CopyTrueColorTranslated(bmp, x, y, rotate, translationtables[TRANSLATION_Standard][isalpha ? STD_Gray : STD_Grayscale]->Palette, inf);
|
|
|
|
}
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Fix for certain special patches on single-patch textures.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2018-03-18 11:36:14 +00:00
|
|
|
void FPatchTexture::DetectBadPatches ()
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2018-03-18 11:36:14 +00:00
|
|
|
// The patch must look like it is large enough for the rules to apply to avoid using this on truly empty patches.
|
|
|
|
if (Wads.LumpLength(SourceLump) < Width * Height / 2) return;
|
|
|
|
|
2016-03-01 15:47:10 +00:00
|
|
|
// Check if this patch is likely to be a problem.
|
|
|
|
// It must be 256 pixels tall, and all its columns must have exactly
|
|
|
|
// one post, where each post has a supposed length of 0.
|
|
|
|
FMemLump lump = Wads.ReadLump (SourceLump);
|
|
|
|
const patch_t *realpatch = (patch_t *)lump.GetMem();
|
2017-03-08 17:44:37 +00:00
|
|
|
const uint32_t *cofs = realpatch->columnofs;
|
2016-03-01 15:47:10 +00:00
|
|
|
int x, x2 = LittleShort(realpatch->width);
|
|
|
|
|
|
|
|
if (LittleShort(realpatch->height) == 256)
|
|
|
|
{
|
|
|
|
for (x = 0; x < x2; ++x)
|
|
|
|
{
|
2017-03-08 17:44:37 +00:00
|
|
|
const column_t *col = (column_t*)((uint8_t*)realpatch+LittleLong(cofs[x]));
|
2016-03-01 15:47:10 +00:00
|
|
|
if (col->topdelta != 0 || col->length != 0)
|
|
|
|
{
|
2018-03-18 11:36:14 +00:00
|
|
|
return; // It's not bad!
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
2017-03-08 17:44:37 +00:00
|
|
|
col = (column_t *)((uint8_t *)col + 256 + 4);
|
2016-03-01 15:47:10 +00:00
|
|
|
if (col->topdelta != 0xFF)
|
|
|
|
{
|
2018-03-18 11:36:14 +00:00
|
|
|
return; // More than one post in a column!
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-18 11:36:14 +00:00
|
|
|
LeftOffset = 0;
|
|
|
|
TopOffset = 0;
|
|
|
|
badflag = true;
|
|
|
|
bMasked = false; // Hacked textures don't have transparent parts.
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
}
|