- swapped the image source implementations with GZDoom's.

This commit is contained in:
Christoph Oelckers 2020-05-23 23:46:44 +02:00
parent ab6e87b5f8
commit d0cbf21dbe
12 changed files with 584 additions and 192 deletions

View File

@ -41,7 +41,7 @@
#include "build.h" #include "build.h"
#if 0
//========================================================================== //==========================================================================
// //
// an AET texture // an AET texture
@ -172,3 +172,4 @@ int FArtTexture::CopyPixels(FBitmap *bmp, int conversion)
return 0; return 0;
} }
#endif

View File

@ -163,9 +163,9 @@ class FDDSTexture : public FImageSource
PIX_ARGB = 2 PIX_ARGB = 2
}; };
public: public:
FDDSTexture (FileReader &lump, void *surfdesc); FDDSTexture (FileReader &lump, int lumpnum, void *surfdesc);
void CreatePalettedPixels(uint8_t *destbuffer) override; TArray<uint8_t> CreatePalettedPixels(int conversion) override;
protected: protected:
uint32_t Format; uint32_t Format;
@ -219,7 +219,7 @@ static bool CheckDDS (FileReader &file)
// //
//========================================================================== //==========================================================================
FImageSource *DDSImage_TryCreate (FileReader &data) FImageSource *DDSImage_TryCreate (FileReader &data, int lumpnum)
{ {
union union
{ {
@ -274,7 +274,7 @@ FImageSource *DDSImage_TryCreate (FileReader &data)
{ {
return NULL; return NULL;
} }
return new FDDSTexture (data, &surfdesc); return new FDDSTexture (data, lumpnum, &surfdesc);
} }
//========================================================================== //==========================================================================
@ -283,7 +283,8 @@ FImageSource *DDSImage_TryCreate (FileReader &data)
// //
//========================================================================== //==========================================================================
FDDSTexture::FDDSTexture (FileReader &lump, void *vsurfdesc) FDDSTexture::FDDSTexture (FileReader &lump, int lumpnum, void *vsurfdesc)
: FImageSource(lumpnum)
{ {
DDSURFACEDESC2 *surf = (DDSURFACEDESC2 *)vsurfdesc; DDSURFACEDESC2 *surf = (DDSURFACEDESC2 *)vsurfdesc;
@ -372,30 +373,32 @@ void FDDSTexture::CalcBitShift (uint32_t mask, uint8_t *lshiftp, uint8_t *rshift
// //
//========================================================================== //==========================================================================
void FDDSTexture::CreatePalettedPixels(uint8_t *buffer) TArray<uint8_t> FDDSTexture::CreatePalettedPixels(int conversion)
{ {
auto lump = fileSystem.OpenFileReader(Name); auto lump = fileSystem.OpenFileReader (SourceLump);
if (!lump.isOpen()) return; // Just leave the texture blank.
TArray<uint8_t> Pixels(Width*Height, true);
lump.Seek (sizeof(DDSURFACEDESC2) + 4, FileReader::SeekSet); lump.Seek (sizeof(DDSURFACEDESC2) + 4, FileReader::SeekSet);
int pmode = PIX_Palette; int pmode = conversion == luminance ? PIX_Alphatex : PIX_Palette;
if (Format >= 1 && Format <= 4) // RGB: Format is # of bytes per pixel if (Format >= 1 && Format <= 4) // RGB: Format is # of bytes per pixel
{ {
ReadRGB (lump, buffer, pmode); ReadRGB (lump, Pixels.Data(), pmode);
} }
else if (Format == ID_DXT1) else if (Format == ID_DXT1)
{ {
DecompressDXT1 (lump, buffer, pmode); DecompressDXT1 (lump, Pixels.Data(), pmode);
} }
else if (Format == ID_DXT3 || Format == ID_DXT2) else if (Format == ID_DXT3 || Format == ID_DXT2)
{ {
DecompressDXT3 (lump, Format == ID_DXT2, buffer, pmode); DecompressDXT3 (lump, Format == ID_DXT2, Pixels.Data(), pmode);
} }
else if (Format == ID_DXT5 || Format == ID_DXT4) else if (Format == ID_DXT5 || Format == ID_DXT4)
{ {
DecompressDXT5 (lump, Format == ID_DXT4, buffer, pmode); DecompressDXT5 (lump, Format == ID_DXT4, Pixels.Data(), pmode);
} }
return Pixels;
} }
//========================================================================== //==========================================================================
@ -442,7 +445,7 @@ void FDDSTexture::ReadRGB (FileReader &lump, uint8_t *buffer, int pixelmode)
uint32_t g = (c & GMask) << GShiftL; g |= g >> GShiftR; uint32_t g = (c & GMask) << GShiftL; g |= g >> GShiftR;
uint32_t b = (c & BMask) << BShiftL; b |= b >> BShiftR; uint32_t b = (c & BMask) << BShiftL; b |= b >> BShiftR;
uint32_t a = (c & AMask) << AShiftL; a |= a >> AShiftR; uint32_t a = (c & AMask) << AShiftL; a |= a >> AShiftR;
*pixelp = ImageHelpers::RGBToPalette(false, r >> 24, g >> 24, b >> 24, a >> 24); *pixelp = ImageHelpers::RGBToPalette(pixelmode == PIX_Alphatex, r >> 24, g >> 24, b >> 24, a >> 24);
} }
else else
{ {
@ -781,8 +784,7 @@ void FDDSTexture::DecompressDXT5 (FileReader &lump, bool premultiplied, uint8_t
int FDDSTexture::CopyPixels(FBitmap *bmp, int conversion) int FDDSTexture::CopyPixels(FBitmap *bmp, int conversion)
{ {
auto lump = fileSystem.OpenFileReader(Name); auto lump = fileSystem.OpenFileReader (SourceLump);
if (!lump.isOpen()) return -1; // Just leave the texture blank.
uint8_t *TexBuffer = bmp->GetPixels(); uint8_t *TexBuffer = bmp->GetPixels();

View File

@ -35,20 +35,18 @@
*/ */
#include <stdio.h> #include <stdio.h>
#include "files.h"
#include "printf.h"
#include "bitmap.h"
#include "image.h"
#include "filesystem.h"
#include "imagehelpers.h"
#include "v_text.h"
extern "C" extern "C"
{ {
#include <jpeglib.h> #include <jpeglib.h>
} }
#include "files.h"
#include "filesystem.h"
#include "printf.h"
#include "bitmap.h"
#include "imagehelpers.h"
#include "image.h"
struct FLumpSourceMgr : public jpeg_source_mgr struct FLumpSourceMgr : public jpeg_source_mgr
{ {
@ -184,10 +182,10 @@ void JPEG_OutputMessage (j_common_ptr cinfo)
class FJPEGTexture : public FImageSource class FJPEGTexture : public FImageSource
{ {
public: public:
FJPEGTexture (int width, int height); FJPEGTexture (int lumpnum, int width, int height);
void CreatePalettedPixels(uint8_t* destbuffer) override;
int CopyPixels(FBitmap *bmp, int conversion) override; int CopyPixels(FBitmap *bmp, int conversion) override;
TArray<uint8_t> CreatePalettedPixels(int conversion) override;
}; };
//========================================================================== //==========================================================================
@ -196,7 +194,7 @@ public:
// //
//========================================================================== //==========================================================================
FImageSource *JPEGImage_TryCreate(FileReader & data) FImageSource *JPEGImage_TryCreate(FileReader & data, int lumpnum)
{ {
union union
{ {
@ -237,7 +235,7 @@ FImageSource *JPEGImage_TryCreate(FileReader & data)
{ {
return NULL; return NULL;
} }
return new FJPEGTexture (BigShort(first4bytes.w[1]), BigShort(first4bytes.w[0])); return new FJPEGTexture (lumpnum, BigShort(first4bytes.w[1]), BigShort(first4bytes.w[0]));
} }
//========================================================================== //==========================================================================
@ -246,7 +244,8 @@ FImageSource *JPEGImage_TryCreate(FileReader & data)
// //
//========================================================================== //==========================================================================
FJPEGTexture::FJPEGTexture (int width, int height) FJPEGTexture::FJPEGTexture (int lumpnum, int width, int height)
: FImageSource(lumpnum)
{ {
bMasked = false; bMasked = false;
@ -260,10 +259,9 @@ FJPEGTexture::FJPEGTexture (int width, int height)
// //
//========================================================================== //==========================================================================
void FJPEGTexture::CreatePalettedPixels(uint8_t *buffer) TArray<uint8_t> FJPEGTexture::CreatePalettedPixels(int conversion)
{ {
auto lump = fileSystem.OpenFileReader(Name); auto lump = fileSystem.OpenFileReader (SourceLump);
if (!lump.isOpen()) return; // Just leave the texture blank.
JSAMPLE *buff = NULL; JSAMPLE *buff = NULL;
jpeg_decompress_struct cinfo; jpeg_decompress_struct cinfo;
@ -280,13 +278,14 @@ void FJPEGTexture::CreatePalettedPixels(uint8_t *buffer)
FLumpSourceMgr sourcemgr(&lump, &cinfo); FLumpSourceMgr sourcemgr(&lump, &cinfo);
try try
{ {
bool doalpha = conversion == luminance;
jpeg_read_header(&cinfo, TRUE); jpeg_read_header(&cinfo, TRUE);
if (!((cinfo.out_color_space == JCS_RGB && cinfo.num_components == 3) || if (!((cinfo.out_color_space == JCS_RGB && cinfo.num_components == 3) ||
(cinfo.out_color_space == JCS_CMYK && cinfo.num_components == 4) || (cinfo.out_color_space == JCS_CMYK && cinfo.num_components == 4) ||
(cinfo.out_color_space == JCS_YCbCr && cinfo.num_components == 3) || (cinfo.out_color_space == JCS_YCbCr && cinfo.num_components == 3) ||
(cinfo.out_color_space == JCS_GRAYSCALE && cinfo.num_components == 1))) (cinfo.out_color_space == JCS_GRAYSCALE && cinfo.num_components == 1)))
{ {
Printf(TEXTCOLOR_ORANGE "Unsupported color format in %s\n", Name.GetChars()); Printf(TEXTCOLOR_ORANGE "Unsupported color format in %s\n", fileSystem.GetFileFullPath(SourceLump).GetChars());
} }
else else
{ {
@ -305,7 +304,7 @@ void FJPEGTexture::CreatePalettedPixels(uint8_t *buffer)
case JCS_RGB: case JCS_RGB:
for (int x = Width; x > 0; --x) for (int x = Width; x > 0; --x)
{ {
*out = ImageHelpers::RGBToPalette(false, in[0], in[1], in[2]); *out = ImageHelpers::RGBToPalette(doalpha, in[0], in[1], in[2]);
out += Height; out += Height;
in += 3; in += 3;
} }
@ -313,7 +312,7 @@ void FJPEGTexture::CreatePalettedPixels(uint8_t *buffer)
case JCS_GRAYSCALE: case JCS_GRAYSCALE:
{ {
auto remap = GPalette.GrayMap; auto remap = ImageHelpers::GetRemap(doalpha, true);
for (int x = Width; x > 0; --x) for (int x = Width; x > 0; --x)
{ {
*out = remap[in[0]]; *out = remap[in[0]];
@ -331,7 +330,7 @@ void FJPEGTexture::CreatePalettedPixels(uint8_t *buffer)
int r = in[3] - (((256 - in[0])*in[3]) >> 8); int r = in[3] - (((256 - in[0])*in[3]) >> 8);
int g = in[3] - (((256 - in[1])*in[3]) >> 8); int g = in[3] - (((256 - in[1])*in[3]) >> 8);
int b = in[3] - (((256 - in[2])*in[3]) >> 8); int b = in[3] - (((256 - in[2])*in[3]) >> 8);
*out = ImageHelpers::RGBToPalette(false, r, g, b); *out = ImageHelpers::RGBToPalette(doalpha, r, g, b);
out += Height; out += Height;
in += 4; in += 4;
} }
@ -345,7 +344,7 @@ void FJPEGTexture::CreatePalettedPixels(uint8_t *buffer)
int r = clamp((int)(Y + 1.40200 * (Cr - 0x80)), 0, 255); int r = clamp((int)(Y + 1.40200 * (Cr - 0x80)), 0, 255);
int g = clamp((int)(Y - 0.34414 * (Cb - 0x80) - 0.71414 * (Cr - 0x80)), 0, 255); int g = clamp((int)(Y - 0.34414 * (Cb - 0x80) - 0.71414 * (Cr - 0x80)), 0, 255);
int b = clamp((int)(Y + 1.77200 * (Cb - 0x80)), 0, 255); int b = clamp((int)(Y + 1.77200 * (Cb - 0x80)), 0, 255);
*out = ImageHelpers::RGBToPalette(false, r, g, b); *out = ImageHelpers::RGBToPalette(doalpha, r, g, b);
out += Height; out += Height;
in += 4; in += 4;
} }
@ -363,13 +362,14 @@ void FJPEGTexture::CreatePalettedPixels(uint8_t *buffer)
} }
catch (int) catch (int)
{ {
Printf(TEXTCOLOR_ORANGE "JPEG error in %s\n", Name.GetChars()); Printf(TEXTCOLOR_ORANGE "JPEG error in %s\n", fileSystem.GetFileFullPath(SourceLump).GetChars());
} }
jpeg_destroy_decompress(&cinfo); jpeg_destroy_decompress(&cinfo);
if (buff != NULL) if (buff != NULL)
{ {
delete[] buff; delete[] buff;
} }
return Pixels;
} }
@ -385,8 +385,7 @@ int FJPEGTexture::CopyPixels(FBitmap *bmp, int conversion)
{ {
PalEntry pe[256]; PalEntry pe[256];
auto lump = fileSystem.OpenFileReader(Name); auto lump = fileSystem.OpenFileReader (SourceLump);
if (!lump.isOpen()) return -1; // Just leave the texture blank.
jpeg_decompress_struct cinfo; jpeg_decompress_struct cinfo;
jpeg_error_mgr jerr; jpeg_error_mgr jerr;
@ -406,7 +405,7 @@ int FJPEGTexture::CopyPixels(FBitmap *bmp, int conversion)
(cinfo.out_color_space == JCS_YCbCr && cinfo.num_components == 3) || (cinfo.out_color_space == JCS_YCbCr && cinfo.num_components == 3) ||
(cinfo.out_color_space == JCS_GRAYSCALE && cinfo.num_components == 1))) (cinfo.out_color_space == JCS_GRAYSCALE && cinfo.num_components == 1)))
{ {
Printf(TEXTCOLOR_ORANGE "Unsupported color format in %s\n", Name.GetChars()); Printf(TEXTCOLOR_ORANGE "Unsupported color format in %s\n", fileSystem.GetFileFullPath(SourceLump).GetChars());
} }
else else
{ {
@ -454,7 +453,7 @@ int FJPEGTexture::CopyPixels(FBitmap *bmp, int conversion)
} }
catch (int) catch (int)
{ {
Printf(TEXTCOLOR_ORANGE "JPEG error in %s\n", Name.GetChars()); Printf(TEXTCOLOR_ORANGE "JPEG error in %s\n", fileSystem.GetFileFullPath(SourceLump).GetChars());
} }
jpeg_destroy_decompress(&cinfo); jpeg_destroy_decompress(&cinfo);
return 0; return 0;

View File

@ -34,12 +34,11 @@
** **
*/ */
#include "basics.h"
#include "files.h" #include "files.h"
#include "filesystem.h"
#include "bitmap.h" #include "bitmap.h"
#include "imagehelpers.h" #include "imagehelpers.h"
#include "image.h" #include "image.h"
#include "filesystem.h"
//========================================================================== //==========================================================================
// //
@ -82,7 +81,7 @@ struct PCXHeader
class FPCXTexture : public FImageSource class FPCXTexture : public FImageSource
{ {
public: public:
FPCXTexture (PCXHeader &); FPCXTexture (int lumpnum, PCXHeader &);
int CopyPixels(FBitmap *bmp, int conversion) override; int CopyPixels(FBitmap *bmp, int conversion) override;
@ -92,7 +91,7 @@ protected:
void ReadPCX8bits (uint8_t *dst, FileReader & lump, PCXHeader *hdr); void ReadPCX8bits (uint8_t *dst, FileReader & lump, PCXHeader *hdr);
void ReadPCX24bits (uint8_t *dst, FileReader & lump, PCXHeader *hdr, int planes); void ReadPCX24bits (uint8_t *dst, FileReader & lump, PCXHeader *hdr, int planes);
void CreatePalettedPixels(uint8_t *destbuffer) override; TArray<uint8_t> CreatePalettedPixels(int conversion) override;
}; };
@ -102,7 +101,7 @@ protected:
// //
//========================================================================== //==========================================================================
FImageSource * PCXImage_TryCreate(FileReader & file) FImageSource * PCXImage_TryCreate(FileReader & file, int lumpnum)
{ {
PCXHeader hdr; PCXHeader hdr;
@ -131,7 +130,7 @@ FImageSource * PCXImage_TryCreate(FileReader & file)
file.Seek(0, FileReader::SeekSet); file.Seek(0, FileReader::SeekSet);
file.Read(&hdr, sizeof(hdr)); file.Read(&hdr, sizeof(hdr));
return new FPCXTexture(hdr); return new FPCXTexture(lumpnum, hdr);
} }
//========================================================================== //==========================================================================
@ -140,7 +139,8 @@ FImageSource * PCXImage_TryCreate(FileReader & file)
// //
//========================================================================== //==========================================================================
FPCXTexture::FPCXTexture(PCXHeader & hdr) FPCXTexture::FPCXTexture(int lumpnum, PCXHeader & hdr)
: FImageSource(lumpnum)
{ {
bMasked = false; bMasked = false;
Width = LittleShort(hdr.xmax) - LittleShort(hdr.xmin) + 1; Width = LittleShort(hdr.xmax) - LittleShort(hdr.xmin) + 1;
@ -344,19 +344,20 @@ void FPCXTexture::ReadPCX24bits (uint8_t *dst, FileReader & lump, PCXHeader *hdr
// //
//========================================================================== //==========================================================================
void FPCXTexture::CreatePalettedPixels(uint8_t *buffer) TArray<uint8_t> FPCXTexture::CreatePalettedPixels(int conversion)
{ {
uint8_t PaletteMap[256]; uint8_t PaletteMap[256];
PCXHeader header; PCXHeader header;
int bitcount; int bitcount;
auto lump = fileSystem.OpenFileReader(Name); auto lump = fileSystem.OpenFileReader(SourceLump);
if (!lump.isOpen()) return; // Just leave the texture blank.
lump.Read(&header, sizeof(header)); lump.Read(&header, sizeof(header));
bitcount = header.bitsPerPixel * header.numColorPlanes; bitcount = header.bitsPerPixel * header.numColorPlanes;
TArray<uint8_t> Pixels(Width*Height, true);
bool alphatex = conversion == luminance;
if (bitcount < 24) if (bitcount < 24)
{ {
if (bitcount < 8) if (bitcount < 8)
@ -365,17 +366,17 @@ void FPCXTexture::CreatePalettedPixels(uint8_t *buffer)
{ {
default: default:
case 1: case 1:
PaletteMap[0] = GPalette.GrayMap[0]; PaletteMap[0] = alphatex? 0 : GPalette.GrayMap[0];
PaletteMap[1] = GPalette.GrayMap[255]; PaletteMap[1] = alphatex? 255 : GPalette.GrayMap[255];
ReadPCX1bit (buffer, lump, &header); ReadPCX1bit (Pixels.Data(), lump, &header);
break; break;
case 4: case 4:
for (int i = 0; i < 16; i++) for (int i = 0; i < 16; i++)
{ {
PaletteMap[i] = ImageHelpers::RGBToPalettePrecise(false, header.palette[i * 3], header.palette[i * 3 + 1], header.palette[i * 3 + 2]); PaletteMap[i] = ImageHelpers::RGBToPalettePrecise(alphatex, header.palette[i * 3], header.palette[i * 3 + 1], header.palette[i * 3 + 2]);
} }
ReadPCX4bits (buffer, lump, &header); ReadPCX4bits (Pixels.Data(), lump, &header);
break; break;
} }
} }
@ -390,19 +391,20 @@ void FPCXTexture::CreatePalettedPixels(uint8_t *buffer)
uint8_t r = lump.ReadUInt8(); uint8_t r = lump.ReadUInt8();
uint8_t g = lump.ReadUInt8(); uint8_t g = lump.ReadUInt8();
uint8_t b = lump.ReadUInt8(); uint8_t b = lump.ReadUInt8();
PaletteMap[i] = ImageHelpers::RGBToPalettePrecise(false, r, g, b); PaletteMap[i] = ImageHelpers::RGBToPalettePrecise(alphatex, r, g, b);
} }
lump.Seek(sizeof(header), FileReader::SeekSet); lump.Seek(sizeof(header), FileReader::SeekSet);
ReadPCX8bits (buffer, lump, &header); ReadPCX8bits (Pixels.Data(), lump, &header);
} }
if (Width == Height) if (Width == Height)
{ {
ImageHelpers::FlipSquareBlockRemap(buffer, Width, PaletteMap); ImageHelpers::FlipSquareBlockRemap(Pixels.Data(), Width, PaletteMap);
} }
else else
{ {
TArray<uint8_t> newpix(Width*Height, true); TArray<uint8_t> newpix(Width*Height, true);
ImageHelpers::FlipNonSquareBlockRemap (newpix.Data(), buffer, Width, Height, Width, PaletteMap); ImageHelpers::FlipNonSquareBlockRemap (newpix.Data(), Pixels.Data(), Width, Height, Width, PaletteMap);
return newpix;
} }
} }
else else
@ -414,11 +416,12 @@ void FPCXTexture::CreatePalettedPixels(uint8_t *buffer)
{ {
for(int x=0; x < Width; x++) for(int x=0; x < Width; x++)
{ {
buffer[y + Height * x] = ImageHelpers::RGBToPalette(false, row[0], row[1], row[2]); Pixels[y + Height * x] = ImageHelpers::RGBToPalette(alphatex, row[0], row[1], row[2]);
row+=3; row+=3;
} }
} }
} }
return Pixels;
} }
//=========================================================================== //===========================================================================
@ -436,8 +439,7 @@ int FPCXTexture::CopyPixels(FBitmap *bmp, int conversion)
int bitcount; int bitcount;
TArray<uint8_t> Pixels; TArray<uint8_t> Pixels;
auto lump = fileSystem.OpenFileReader(Name); auto lump = fileSystem.OpenFileReader(SourceLump);
if (!lump.isOpen()) return -1; // Just leave the texture blank.
lump.Read(&header, sizeof(header)); lump.Read(&header, sizeof(header));

View File

@ -53,20 +53,22 @@
class FPNGTexture : public FImageSource class FPNGTexture : public FImageSource
{ {
public: public:
FPNGTexture (FileReader &lump, int width, int height, uint8_t bitdepth, uint8_t colortype, uint8_t interlace); FPNGTexture (FileReader &lump, int lumpnum, int width, int height, uint8_t bitdepth, uint8_t colortype, uint8_t interlace);
void CreatePalettedPixels(uint8_t* buffer) override;
int CopyPixels(FBitmap *bmp, int conversion) override; int CopyPixels(FBitmap *bmp, int conversion) override;
TArray<uint8_t> CreatePalettedPixels(int conversion) override;
protected: protected:
void ReadAlphaRemap(FileReader *lump, uint8_t *alpharemap);
uint8_t BitDepth; uint8_t BitDepth;
uint8_t ColorType; uint8_t ColorType;
uint8_t Interlace; uint8_t Interlace;
bool HaveTrans; bool HaveTrans;
uint16_t NonPaletteTrans[3]; uint16_t NonPaletteTrans[3];
uint8_t PaletteMap[256]; uint8_t *PaletteMap = nullptr;
uint32_t PaletteSize = 0; int PaletteSize = 0;
uint32_t StartOfIDAT = 0; uint32_t StartOfIDAT = 0;
uint32_t StartOfPalette = 0; uint32_t StartOfPalette = 0;
}; };
@ -78,7 +80,7 @@ protected:
// //
//========================================================================== //==========================================================================
FImageSource *PNGImage_TryCreate(FileReader & data) FImageSource *PNGImage_TryCreate(FileReader & data, int lumpnum)
{ {
union union
{ {
@ -137,7 +139,7 @@ FImageSource *PNGImage_TryCreate(FileReader & data)
} }
} }
return new FPNGTexture (data, width, height, bitdepth, colortype, interlace); return new FPNGTexture (data, lumpnum, width, height, bitdepth, colortype, interlace);
} }
//========================================================================== //==========================================================================
@ -146,13 +148,14 @@ FImageSource *PNGImage_TryCreate(FileReader & data)
// //
//========================================================================== //==========================================================================
FPNGTexture::FPNGTexture (FileReader &lump, int width, int height, FPNGTexture::FPNGTexture (FileReader &lump, int lumpnum, int width, int height,
uint8_t depth, uint8_t colortype, uint8_t interlace) uint8_t depth, uint8_t colortype, uint8_t interlace)
: BitDepth(depth), ColorType(colortype), Interlace(interlace), HaveTrans(false) : FImageSource(lumpnum),
BitDepth(depth), ColorType(colortype), Interlace(interlace), HaveTrans(false)
{ {
union union
{ {
PalEntry palette[256]; uint32_t palette[256];
uint8_t pngpal[256][3]; uint8_t pngpal[256][3];
} p; } p;
uint8_t trans[256]; uint8_t trans[256];
@ -192,12 +195,12 @@ FPNGTexture::FPNGTexture (FileReader &lump, int width, int height,
ihoty = BigLong((int)hoty); ihoty = BigLong((int)hoty);
if (ihotx < -32768 || ihotx > 32767) if (ihotx < -32768 || ihotx > 32767)
{ {
Printf ("X-Offset for PNG texture %s is bad: %d (0x%08x)\n", Name.GetChars(), ihotx, ihotx); Printf ("X-Offset for PNG texture %s is bad: %d (0x%08x)\n", fileSystem.GetFileFullName (lumpnum), ihotx, ihotx);
ihotx = 0; ihotx = 0;
} }
if (ihoty < -32768 || ihoty > 32767) if (ihoty < -32768 || ihoty > 32767)
{ {
Printf ("Y-Offset for PNG texture %s is bad: %d (0x%08x)\n", Name.GetChars(), ihoty, ihoty); Printf ("Y-Offset for PNG texture %s is bad: %d (0x%08x)\n", fileSystem.GetFileFullName (lumpnum), ihoty, ihoty);
ihoty = 0; ihoty = 0;
} }
LeftOffset = ihotx; LeftOffset = ihotx;
@ -206,7 +209,7 @@ FPNGTexture::FPNGTexture (FileReader &lump, int width, int height,
break; break;
case MAKE_ID('P','L','T','E'): case MAKE_ID('P','L','T','E'):
PaletteSize = std::min<int> (len / 3, 256); PaletteSize = MIN<int> (len / 3, 256);
StartOfPalette = (uint32_t)lump.Tell(); StartOfPalette = (uint32_t)lump.Tell();
lump.Read (p.pngpal, PaletteSize * 3); lump.Read (p.pngpal, PaletteSize * 3);
if (PaletteSize * 3 != (int)len) if (PaletteSize * 3 != (int)len)
@ -215,7 +218,7 @@ FPNGTexture::FPNGTexture (FileReader &lump, int width, int height,
} }
for (i = PaletteSize - 1; i >= 0; --i) for (i = PaletteSize - 1; i >= 0; --i)
{ {
p.palette[i] = PalEntry(p.pngpal[i][0], p.pngpal[i][1], p.pngpal[i][2]); p.palette[i] = MAKERGB(p.pngpal[i][0], p.pngpal[i][1], p.pngpal[i][2]);
} }
break; break;
@ -246,27 +249,25 @@ FPNGTexture::FPNGTexture (FileReader &lump, int width, int height,
{ {
bMasked = true; bMasked = true;
PaletteSize = 256; PaletteSize = 256;
memcpy (PaletteMap, GPalette.GrayMap+1, 256); PaletteMap = (uint8_t*)ImageArena.Alloc(PaletteSize);
PaletteMap[255] = 254; // cannot use 255. memcpy (PaletteMap, GPalette.GrayMap, 256);
PaletteMap[NonPaletteTrans[0]] = 255; PaletteMap[NonPaletteTrans[0]] = 0;
} }
else else
{ {
memcpy(PaletteMap, GPalette.GrayMap, 256); PaletteMap = GPalette.GrayMap;
} }
break; break;
case 3: // Paletted case 3: // Paletted
PaletteMap = (uint8_t*)ImageArena.Alloc(PaletteSize);
MakeRemap ((uint32_t*)GPalette.BaseColors, p.palette, PaletteMap, trans, PaletteSize);
for (i = 0; i < PaletteSize; ++i) for (i = 0; i < PaletteSize; ++i)
{ {
if (trans[i] == 0) if (trans[i] == 0)
{ {
bMasked = true; bMasked = true;
PaletteMap[i] = 255; PaletteMap[i] = 0;
}
else
{
PaletteMap[i] = ColorMatcher.Pick(p.palette[i].r, p.palette[i].g, p.palette[i].b);
} }
} }
break; break;
@ -287,13 +288,32 @@ FPNGTexture::FPNGTexture (FileReader &lump, int width, int height,
// //
//========================================================================== //==========================================================================
void FPNGTexture::CreatePalettedPixels(uint8_t *buffer) void FPNGTexture::ReadAlphaRemap(FileReader *lump, uint8_t *alpharemap)
{
auto p = lump->Tell();
lump->Seek(StartOfPalette, FileReader::SeekSet);
for (int i = 0; i < PaletteSize; i++)
{
uint8_t r = lump->ReadUInt8();
uint8_t g = lump->ReadUInt8();
uint8_t b = lump->ReadUInt8();
alpharemap[i] = PaletteMap[i] == 0 ? 0 : Luminance(r, g, b);
}
lump->Seek(p, FileReader::SeekSet);
}
//==========================================================================
//
//
//
//==========================================================================
TArray<uint8_t> FPNGTexture::CreatePalettedPixels(int conversion)
{ {
FileReader *lump; FileReader *lump;
FileReader lfr; FileReader lfr;
lfr = fileSystem.OpenFileReader(Name); lfr = fileSystem.OpenFileReader(SourceLump);
if (!lfr.isOpen()) return;
lump = &lfr; lump = &lfr;
TArray<uint8_t> Pixels(Width*Height, true); TArray<uint8_t> Pixels(Width*Height, true);
@ -308,18 +328,46 @@ void FPNGTexture::CreatePalettedPixels(uint8_t *buffer)
lump->Read(&len, 4); lump->Read(&len, 4);
lump->Read(&id, 4); lump->Read(&id, 4);
bool alphatex = conversion == luminance;
if (ColorType == 0 || ColorType == 3) /* Grayscale and paletted */ if (ColorType == 0 || ColorType == 3) /* Grayscale and paletted */
{ {
M_ReadIDAT (*lump, Pixels.Data(), Width, Height, Width, BitDepth, ColorType, Interlace, BigLong((unsigned int)len)); M_ReadIDAT (*lump, Pixels.Data(), Width, Height, Width, BitDepth, ColorType, Interlace, BigLong((unsigned int)len));
if (Width == Height) if (Width == Height)
{ {
ImageHelpers::FlipSquareBlockRemap (Pixels.Data(), Width, PaletteMap); if (conversion != luminance)
{
ImageHelpers::FlipSquareBlockRemap (Pixels.Data(), Width, PaletteMap);
}
else if (ColorType == 0)
{
ImageHelpers::FlipSquareBlock (Pixels.Data(), Width);
}
else
{
uint8_t alpharemap[256];
ReadAlphaRemap(lump, alpharemap);
ImageHelpers::FlipSquareBlockRemap(Pixels.Data(), Width, alpharemap);
}
} }
else else
{ {
TArray<uint8_t> newpix(Width*Height, true); TArray<uint8_t> newpix(Width*Height, true);
ImageHelpers::FlipNonSquareBlockRemap (newpix.Data(), Pixels.Data(), Width, Height, Width, PaletteMap); if (conversion != luminance)
{
ImageHelpers::FlipNonSquareBlockRemap (newpix.Data(), Pixels.Data(), Width, Height, Width, PaletteMap);
}
else if (ColorType == 0)
{
ImageHelpers::FlipNonSquareBlock (newpix.Data(), Pixels.Data(), Width, Height, Width);
}
else
{
uint8_t alpharemap[256];
ReadAlphaRemap(lump, alpharemap);
ImageHelpers::FlipNonSquareBlockRemap(newpix.Data(), Pixels.Data(), Width, Height, Width, alpharemap);
}
return newpix;
} }
} }
else /* RGB and/or Alpha present */ else /* RGB and/or Alpha present */
@ -346,11 +394,11 @@ void FPNGTexture::CreatePalettedPixels(uint8_t *buffer)
{ {
if (HaveTrans && in[0] == NonPaletteTrans[0] && in[1] == NonPaletteTrans[1] && in[2] == NonPaletteTrans[2]) if (HaveTrans && in[0] == NonPaletteTrans[0] && in[1] == NonPaletteTrans[1] && in[2] == NonPaletteTrans[2])
{ {
*out++ = 255; *out++ = 0;
} }
else else
{ {
*out++ = ImageHelpers::RGBToPalette(false, in[0], in[1], in[2]); *out++ = ImageHelpers::RGBToPalette(alphatex, in[0], in[1], in[2]);
} }
in += pitch; in += pitch;
} }
@ -365,7 +413,7 @@ void FPNGTexture::CreatePalettedPixels(uint8_t *buffer)
{ {
for (y = Height; y > 0; --y) for (y = Height; y > 0; --y)
{ {
*out++ = PaletteMap[in[0]]; *out++ = alphatex? ((in[0] * in[1]) / 255) : in[1] < 128 ? 0 : PaletteMap[in[0]];
in += pitch; in += pitch;
} }
in -= backstep; in -= backstep;
@ -379,7 +427,7 @@ void FPNGTexture::CreatePalettedPixels(uint8_t *buffer)
{ {
for (y = Height; y > 0; --y) for (y = Height; y > 0; --y)
{ {
*out++ = ImageHelpers::RGBToPalette(false, in[0], in[1], in[2], in[3]); *out++ = ImageHelpers::RGBToPalette(alphatex, in[0], in[1], in[2], in[3]);
in += pitch; in += pitch;
} }
in -= backstep; in -= backstep;
@ -389,6 +437,7 @@ void FPNGTexture::CreatePalettedPixels(uint8_t *buffer)
delete[] tempix; delete[] tempix;
} }
} }
return Pixels;
} }
//=========================================================================== //===========================================================================
@ -409,9 +458,7 @@ int FPNGTexture::CopyPixels(FBitmap *bmp, int conversion)
FileReader *lump; FileReader *lump;
FileReader lfr; FileReader lfr;
lfr = fileSystem.OpenFileReader(Name); lfr = fileSystem.OpenFileReader(SourceLump);
if (!lfr.isOpen()) return -1; // Just leave the texture blank.
lump = &lfr; lump = &lfr;
lump->Seek(33, FileReader::SeekSet); lump->Seek(33, FileReader::SeekSet);
@ -487,8 +534,8 @@ int FPNGTexture::CopyPixels(FBitmap *bmp, int conversion)
} }
else else
{ {
bmp->CopyPixelDataRGB(0, 0, Pixels, Width, Height, 3, pixwidth, 0, CF_RGBT, bmp->CopyPixelDataRGB(0, 0, Pixels, Width, Height, 3, pixwidth, 0, CF_RGBT, nullptr,
nullptr, NonPaletteTrans[0], NonPaletteTrans[1], NonPaletteTrans[2]); NonPaletteTrans[0], NonPaletteTrans[1], NonPaletteTrans[2]);
transpal = true; transpal = true;
} }
break; break;
@ -512,6 +559,8 @@ int FPNGTexture::CopyPixels(FBitmap *bmp, int conversion)
} }
#include "textures.h"
//========================================================================== //==========================================================================
// //
// A savegame picture // A savegame picture

View File

@ -46,10 +46,10 @@
#include "files.h" #include "files.h"
#include "filesystem.h"
#include "bitmap.h" #include "bitmap.h"
#include "imagehelpers.h" #include "imagehelpers.h"
#include "image.h" #include "image.h"
#include "filesystem.h"
//========================================================================== //==========================================================================
// //
@ -66,8 +66,8 @@ class FStbTexture : public FImageSource
{ {
public: public:
FStbTexture (int w, int h); FStbTexture (int lumpnum, int w, int h);
void CreatePalettedPixels(uint8_t *destbuffer) override; TArray<uint8_t> CreatePalettedPixels(int conversion) override;
int CopyPixels(FBitmap *bmp, int conversion) override; int CopyPixels(FBitmap *bmp, int conversion) override;
}; };
@ -83,14 +83,14 @@ static stbi_io_callbacks callbacks = {
// //
//========================================================================== //==========================================================================
FImageSource *StbImage_TryCreate(FileReader & file) FImageSource *StbImage_TryCreate(FileReader & file, int lumpnum)
{ {
int x, y, comp; int x, y, comp;
file.Seek(0, FileReader::SeekSet); file.Seek(0, FileReader::SeekSet);
int result = stbi_info_from_callbacks(&callbacks, &file, &x, &y, &comp); int result = stbi_info_from_callbacks(&callbacks, &file, &x, &y, &comp);
if (result == 1) if (result == 1)
{ {
return new FStbTexture(x, y); return new FStbTexture(lumpnum, x, y);
} }
return nullptr; return nullptr;
@ -102,7 +102,8 @@ FImageSource *StbImage_TryCreate(FileReader & file)
// //
//========================================================================== //==========================================================================
FStbTexture::FStbTexture (int w, int h) FStbTexture::FStbTexture (int lumpnum, int w, int h)
: FImageSource(lumpnum)
{ {
Width = w; Width = w;
Height = h; Height = h;
@ -116,19 +117,21 @@ FStbTexture::FStbTexture (int w, int h)
// //
//========================================================================== //==========================================================================
void FStbTexture::CreatePalettedPixels(uint8_t *buffer) TArray<uint8_t> FStbTexture::CreatePalettedPixels(int conversion)
{ {
FBitmap bitmap; FBitmap bitmap;
bitmap.Create(Width, Height); bitmap.Create(Width, Height);
CopyPixels(&bitmap, 0); CopyPixels(&bitmap, conversion);
const uint8_t *data = bitmap.GetPixels(); const uint8_t *data = bitmap.GetPixels();
uint8_t *dest_p; uint8_t *dest_p;
int dest_adv = Height; int dest_adv = Height;
int dest_rew = Width * Height - 1; int dest_rew = Width * Height - 1;
dest_p = buffer; TArray<uint8_t> Pixels(Width*Height, true);
dest_p = Pixels.Data();
bool doalpha = conversion == luminance;
// Convert the source image from row-major to column-major format and remap it // Convert the source image from row-major to column-major format and remap it
for (int y = Height; y != 0; --y) for (int y = Height; y != 0; --y)
{ {
@ -139,11 +142,12 @@ void FStbTexture::CreatePalettedPixels(uint8_t *buffer)
int r = *data++; int r = *data++;
int a = *data++; int a = *data++;
if (a < 128) *dest_p = 0; if (a < 128) *dest_p = 0;
else *dest_p = ImageHelpers::RGBToPalette(false, r, g, b); else *dest_p = ImageHelpers::RGBToPalette(doalpha, r, g, b);
dest_p += dest_adv; dest_p += dest_adv;
} }
dest_p -= dest_rew; dest_p -= dest_rew;
} }
return Pixels;
} }
//========================================================================== //==========================================================================
@ -154,8 +158,7 @@ void FStbTexture::CreatePalettedPixels(uint8_t *buffer)
int FStbTexture::CopyPixels(FBitmap *bmp, int conversion) int FStbTexture::CopyPixels(FBitmap *bmp, int conversion)
{ {
auto lump = fileSystem.OpenFileReader(Name); auto lump = fileSystem.OpenFileReader (SourceLump);
if (!lump.isOpen()) return -1; // Just leave the texture blank.
int x, y, chan; int x, y, chan;
auto image = stbi_load_from_callbacks(&callbacks, &lump, &x, &y, &chan, STBI_rgb_alpha); auto image = stbi_load_from_callbacks(&callbacks, &lump, &x, &y, &chan, STBI_rgb_alpha);
if (image) if (image)

View File

@ -34,11 +34,11 @@
*/ */
#include "files.h" #include "files.h"
#include "filesystem.h"
#include "templates.h" #include "templates.h"
#include "bitmap.h" #include "bitmap.h"
#include "image.h"
#include "filesystem.h"
#include "imagehelpers.h" #include "imagehelpers.h"
#include "image.h"
//========================================================================== //==========================================================================
@ -77,13 +77,13 @@ struct TGAHeader
class FTGATexture : public FImageSource class FTGATexture : public FImageSource
{ {
public: public:
FTGATexture (TGAHeader *); FTGATexture (int lumpnum, TGAHeader *);
int CopyPixels(FBitmap *bmp, int conversion) override; int CopyPixels(FBitmap *bmp, int conversion) override;
protected: protected:
void ReadCompressed(FileReader &lump, uint8_t * buffer, int bytesperpixel); void ReadCompressed(FileReader &lump, uint8_t * buffer, int bytesperpixel);
void CreatePalettedPixels(uint8_t *destbuffer) override; TArray<uint8_t> CreatePalettedPixels(int conversion) override;
}; };
//========================================================================== //==========================================================================
@ -92,7 +92,7 @@ protected:
// //
//========================================================================== //==========================================================================
FImageSource *TGAImage_TryCreate(FileReader & file) FImageSource *TGAImage_TryCreate(FileReader & file, int lumpnum)
{ {
TGAHeader hdr; TGAHeader hdr;
@ -117,7 +117,7 @@ FImageSource *TGAImage_TryCreate(FileReader & file)
hdr.width = LittleShort(hdr.width); hdr.width = LittleShort(hdr.width);
hdr.height = LittleShort(hdr.height); hdr.height = LittleShort(hdr.height);
return new FTGATexture(&hdr); return new FTGATexture(lumpnum, &hdr);
} }
//========================================================================== //==========================================================================
@ -126,7 +126,8 @@ FImageSource *TGAImage_TryCreate(FileReader & file)
// //
//========================================================================== //==========================================================================
FTGATexture::FTGATexture(TGAHeader * hdr) FTGATexture::FTGATexture (int lumpnum, TGAHeader * hdr)
: FImageSource(lumpnum)
{ {
Width = hdr->width; Width = hdr->width;
Height = hdr->height; Height = hdr->height;
@ -152,7 +153,7 @@ void FTGATexture::ReadCompressed(FileReader &lump, uint8_t * buffer, int bytespe
{ {
b&=~128; b&=~128;
lump.Read(data, bytesperpixel); lump.Read(data, bytesperpixel);
for (int i=std::min<int>(Size, (b+1)); i>0; i--) for (int i=MIN<int>(Size, (b+1)); i>0; i--)
{ {
buffer[0] = data[0]; buffer[0] = data[0];
if (bytesperpixel>=2) buffer[1] = data[1]; if (bytesperpixel>=2) buffer[1] = data[1];
@ -163,7 +164,7 @@ void FTGATexture::ReadCompressed(FileReader &lump, uint8_t * buffer, int bytespe
} }
else else
{ {
lump.Read(buffer, std::min<int>(Size, (b+1))*bytesperpixel); lump.Read(buffer, MIN<int>(Size, (b+1))*bytesperpixel);
buffer += (b+1)*bytesperpixel; buffer += (b+1)*bytesperpixel;
} }
Size -= b+1; Size -= b+1;
@ -176,11 +177,10 @@ void FTGATexture::ReadCompressed(FileReader &lump, uint8_t * buffer, int bytespe
// //
//========================================================================== //==========================================================================
void FTGATexture::CreatePalettedPixels(uint8_t *buffer) TArray<uint8_t> FTGATexture::CreatePalettedPixels(int conversion)
{ {
uint8_t PaletteMap[256]; uint8_t PaletteMap[256];
auto lump = fileSystem.OpenFileReader(Name); auto lump = fileSystem.OpenFileReader (SourceLump);
if (!lump.isOpen()) return;
TGAHeader hdr; TGAHeader hdr;
uint16_t w; uint16_t w;
uint8_t r,g,b,a; uint8_t r,g,b,a;
@ -229,22 +229,23 @@ void FTGATexture::CreatePalettedPixels(uint8_t *buffer)
r=g=b=a=0; r=g=b=a=0;
break; break;
} }
PaletteMap[i] = ImageHelpers::RGBToPalettePrecise(false, r, g, b, a); PaletteMap[i] = ImageHelpers::RGBToPalettePrecise(conversion == luminance, r, g, b, a);
} }
} }
int Size = Width * Height * (hdr.bpp>>3); int Size = Width * Height * (hdr.bpp>>3);
TArray<uint8_t> buffer(Size, true);
if (hdr.img_type < 4) // uncompressed if (hdr.img_type < 4) // uncompressed
{ {
lump.Read(buffer, Size); lump.Read(buffer.Data(), Size);
} }
else // compressed else // compressed
{ {
ReadCompressed(lump, buffer, hdr.bpp>>3); ReadCompressed(lump, buffer.Data(), hdr.bpp>>3);
} }
uint8_t * ptr = buffer; uint8_t * ptr = buffer.Data();
int step_x = (hdr.bpp>>3); int step_x = (hdr.bpp>>3);
int Pitch = Width * step_x; int Pitch = Width * step_x;
@ -287,7 +288,7 @@ void FTGATexture::CreatePalettedPixels(uint8_t *buffer)
for(int x=0;x<Width;x++) for(int x=0;x<Width;x++)
{ {
int v = LittleShort(*p); int v = LittleShort(*p);
Pixels[x*Height + y] = ImageHelpers::RGBToPalette(false, ((v >> 10) & 0x1f) * 8, ((v >> 5) & 0x1f) * 8, (v & 0x1f) * 8); Pixels[x*Height + y] = ImageHelpers::RGBToPalette(conversion == luminance, ((v >> 10) & 0x1f) * 8, ((v >> 5) & 0x1f) * 8, (v & 0x1f) * 8);
p+=step_x; p+=step_x;
} }
} }
@ -299,7 +300,7 @@ void FTGATexture::CreatePalettedPixels(uint8_t *buffer)
uint8_t * p = ptr + y * Pitch; uint8_t * p = ptr + y * Pitch;
for(int x=0;x<Width;x++) for(int x=0;x<Width;x++)
{ {
Pixels[x*Height + y] = ImageHelpers::RGBToPalette(false, p[2], p[1], p[0]); Pixels[x*Height + y] = ImageHelpers::RGBToPalette(conversion == luminance, p[2], p[1], p[0]);
p+=step_x; p+=step_x;
} }
} }
@ -313,7 +314,7 @@ void FTGATexture::CreatePalettedPixels(uint8_t *buffer)
uint8_t * p = ptr + y * Pitch; uint8_t * p = ptr + y * Pitch;
for(int x=0;x<Width;x++) for(int x=0;x<Width;x++)
{ {
Pixels[x*Height + y] = ImageHelpers::RGBToPalette(false, p[2], p[1], p[0]); Pixels[x*Height + y] = ImageHelpers::RGBToPalette(conversion == luminance, p[2], p[1], p[0]);
p+=step_x; p+=step_x;
} }
} }
@ -325,7 +326,7 @@ void FTGATexture::CreatePalettedPixels(uint8_t *buffer)
uint8_t * p = ptr + y * Pitch; uint8_t * p = ptr + y * Pitch;
for(int x=0;x<Width;x++) for(int x=0;x<Width;x++)
{ {
Pixels[x*Height + y] = ImageHelpers::RGBToPalette(false, p[2], p[1], p[0], p[3]); Pixels[x*Height + y] = ImageHelpers::RGBToPalette(conversion == luminance, p[2], p[1], p[0], p[3]);
p+=step_x; p+=step_x;
} }
} }
@ -339,7 +340,7 @@ void FTGATexture::CreatePalettedPixels(uint8_t *buffer)
case 3: // Grayscale case 3: // Grayscale
{ {
auto remap = GPalette.GrayMap; auto remap = ImageHelpers::GetRemap(conversion == luminance, true);
switch (hdr.bpp) switch (hdr.bpp)
{ {
case 8: case 8:
@ -374,6 +375,7 @@ void FTGATexture::CreatePalettedPixels(uint8_t *buffer)
default: default:
break; break;
} }
return Pixels;
} }
//=========================================================================== //===========================================================================
@ -385,8 +387,7 @@ void FTGATexture::CreatePalettedPixels(uint8_t *buffer)
int FTGATexture::CopyPixels(FBitmap *bmp, int conversion) int FTGATexture::CopyPixels(FBitmap *bmp, int conversion)
{ {
PalEntry pe[256]; PalEntry pe[256];
auto lump = fileSystem.OpenFileReader(Name); auto lump = fileSystem.OpenFileReader (SourceLump);
if (!lump.isOpen()) return -1;
TGAHeader hdr; TGAHeader hdr;
uint16_t w; uint16_t w;
uint8_t r,g,b,a; uint8_t r,g,b,a;
@ -441,7 +442,7 @@ int FTGATexture::CopyPixels(FBitmap *bmp, int conversion)
} }
int Size = Width * Height * (hdr.bpp>>3); int Size = Width * Height * (hdr.bpp>>3);
TArray<uint8_t> sbuffer(Size, true); TArray<uint8_t> sbuffer(Size);
if (hdr.img_type < 4) // uncompressed if (hdr.img_type < 4) // uncompressed
{ {

View File

@ -34,14 +34,160 @@
** **
*/ */
#include "memarena.h"
#include "bitmap.h" #include "bitmap.h"
#include "image.h" #include "image.h"
#include "files.h"
#include "filesystem.h" #include "filesystem.h"
#include "imagehelpers.h" #include "files.h"
#include "cmdlib.h"
#include "palettecontainer.h"
FMemArena FImageSource::ImageArena(32768);
TArray<FImageSource *>FImageSource::ImageForLump;
int FImageSource::NextID; int FImageSource::NextID;
static PrecacheInfo precacheInfo;
struct PrecacheDataPaletted
{
TArray<uint8_t> Pixels;
int RefCount;
int ImageID;
};
struct PrecacheDataRgba
{
FBitmap Pixels;
int TransInfo;
int RefCount;
int ImageID;
};
// TMap doesn't handle this kind of data well. std::map neither. The linear search is still faster, even for a few 100 entries because it doesn't have to access the heap as often..
TArray<PrecacheDataPaletted> precacheDataPaletted;
TArray<PrecacheDataRgba> precacheDataRgba;
//===========================================================================
//
// the default just returns an empty texture.
//
//===========================================================================
TArray<uint8_t> FImageSource::CreatePalettedPixels(int conversion)
{
TArray<uint8_t> Pixels(Width * Height, true);
memset(Pixels.Data(), 0, Width * Height);
return Pixels;
}
PalettedPixels FImageSource::GetCachedPalettedPixels(int conversion)
{
PalettedPixels ret;
FString name;
fileSystem.GetFileShortName(name, SourceLump);
std::pair<int, int> *info = nullptr;
auto imageID = ImageID;
// Do we have this image in the cache?
unsigned index = conversion != normal? UINT_MAX : precacheDataPaletted.FindEx([=](PrecacheDataPaletted &entry) { return entry.ImageID == imageID; });
if (index < precacheDataPaletted.Size())
{
auto cache = &precacheDataPaletted[index];
if (cache->RefCount > 1)
{
//Printf("returning reference to %s, refcount = %d\n", name.GetChars(), cache->RefCount);
ret.Pixels.Set(cache->Pixels.Data(), cache->Pixels.Size());
cache->RefCount--;
}
else if (cache->Pixels.Size() > 0)
{
//Printf("returning contents of %s, refcount = %d\n", name.GetChars(), cache->RefCount);
ret.PixelStore = std::move(cache->Pixels);
ret.Pixels.Set(ret.PixelStore.Data(), ret.PixelStore.Size());
precacheDataPaletted.Delete(index);
}
else
{
//Printf("something bad happened for %s, refcount = %d\n", name.GetChars(), cache->RefCount);
}
}
else
{
// The image wasn't cached. Now there's two possibilities:
auto info = precacheInfo.CheckKey(ImageID);
if (!info || info->second <= 1 || conversion != normal)
{
// This is either the only copy needed or some access outside the caching block. In these cases create a new one and directly return it.
//Printf("returning fresh copy of %s\n", name.GetChars());
ret.PixelStore = CreatePalettedPixels(conversion);
ret.Pixels.Set(ret.PixelStore.Data(), ret.PixelStore.Size());
}
else
{
//Printf("creating cached entry for %s, refcount = %d\n", name.GetChars(), info->second);
// This is the first time it gets accessed and needs to be placed in the cache.
PrecacheDataPaletted *pdp = &precacheDataPaletted[precacheDataPaletted.Reserve(1)];
pdp->ImageID = imageID;
pdp->RefCount = info->second - 1;
info->second = 0;
pdp->Pixels = CreatePalettedPixels(normal);
ret.Pixels.Set(pdp->Pixels.Data(), pdp->Pixels.Size());
}
}
return ret;
}
TArray<uint8_t> FImageSource::GetPalettedPixels(int conversion)
{
auto pix = GetCachedPalettedPixels(conversion);
if (pix.ownsPixels())
{
// return the pixel store of the returned data directly if this was the last reference.
auto array = std::move(pix.PixelStore);
return array;
}
else
{
// If there are pending references, make a copy.
TArray<uint8_t> array(pix.Pixels.Size(), true);
memcpy(array.Data(), pix.Pixels.Data(), array.Size());
return array;
}
}
//===========================================================================
//
// FImageSource::CopyPixels
//
// this is the generic case that can handle
// any properly implemented texture for software rendering.
// Its drawback is that it is limited to the base palette which is
// why all classes that handle different palettes should subclass this
// method
//
//===========================================================================
int FImageSource::CopyPixels(FBitmap *bmp, int conversion)
{
if (conversion == luminance) conversion = normal; // luminance images have no use as an RGB source.
PalEntry *palette = GPalette.BaseColors;
for(int i=1;i<256;i++) palette[i].a = 255; // set proper alpha values
auto ppix = CreatePalettedPixels(conversion);
bmp->CopyPixelData(0, 0, ppix.Data(), Width, Height, Height, 1, 0, palette, nullptr);
for(int i=1;i<256;i++) palette[i].a = 0;
return 0;
}
int FImageSource::CopyTranslatedPixels(FBitmap *bmp, const PalEntry *remap)
{
auto ppix = CreatePalettedPixels(false);
bmp->CopyPixelData(0, 0, ppix.Data(), Width, Height, Height, 1, 0, remap, nullptr);
return 0;
}
//========================================================================== //==========================================================================
// //
@ -49,46 +195,184 @@ int FImageSource::NextID;
// //
//========================================================================== //==========================================================================
typedef FImageSource * (*CreateFunc)(FileReader & file); FBitmap FImageSource::GetCachedBitmap(const PalEntry *remap, int conversion, int *ptrans)
{
FBitmap ret;
FString name;
int trans = -1;
fileSystem.GetFileShortName(name, SourceLump);
std::pair<int, int> *info = nullptr;
auto imageID = ImageID;
if (remap != nullptr)
{
// Remapped images are never run through the cache because they would complicate matters too much for very little gain.
// Translated images are normally sprites which normally just consist of a single image and use no composition.
// Additionally, since translation requires the base palette, the really time consuming stuff will never be subjected to it.
ret.Create(Width, Height);
trans = CopyTranslatedPixels(&ret, remap);
}
else
{
if (conversion == luminance) conversion = normal; // luminance has no meaning for true color.
// Do we have this image in the cache?
unsigned index = conversion != normal? UINT_MAX : precacheDataRgba.FindEx([=](PrecacheDataRgba &entry) { return entry.ImageID == imageID; });
if (index < precacheDataRgba.Size())
{
auto cache = &precacheDataRgba[index];
trans = cache->TransInfo;
if (cache->RefCount > 1)
{
//Printf("returning reference to %s, refcount = %d\n", name.GetChars(), cache->RefCount);
ret.Copy(cache->Pixels, false);
cache->RefCount--;
}
else if (cache->Pixels.GetPixels())
{
//Printf("returning contents of %s, refcount = %d\n", name.GetChars(), cache->RefCount);
ret = std::move(cache->Pixels);
precacheDataRgba.Delete(index);
}
else
{
// This should never happen if the function is implemented correctly
//Printf("something bad happened for %s, refcount = %d\n", name.GetChars(), cache->RefCount);
ret.Create(Width, Height);
trans = CopyPixels(&ret, normal);
}
}
else
{
// The image wasn't cached. Now there's two possibilities:
auto info = precacheInfo.CheckKey(ImageID);
if (!info || info->first <= 1 || conversion != normal)
{
// This is either the only copy needed or some access outside the caching block. In these cases create a new one and directly return it.
//Printf("returning fresh copy of %s\n", name.GetChars());
ret.Create(Width, Height);
trans = CopyPixels(&ret, conversion);
}
else
{
//Printf("creating cached entry for %s, refcount = %d\n", name.GetChars(), info->first);
// This is the first time it gets accessed and needs to be placed in the cache.
PrecacheDataRgba *pdr = &precacheDataRgba[precacheDataRgba.Reserve(1)];
pdr->ImageID = imageID;
pdr->RefCount = info->first - 1;
info->first = 0;
pdr->Pixels.Create(Width, Height);
trans = pdr->TransInfo = CopyPixels(&pdr->Pixels, normal);
ret.Copy(pdr->Pixels, false);
}
}
}
if (ptrans) *ptrans = trans;
return ret;
}
//==========================================================================
//
//
//
//==========================================================================
void FImageSource::CollectForPrecache(PrecacheInfo &info, bool requiretruecolor)
{
auto val = info.CheckKey(ImageID);
bool tc = requiretruecolor;
if (val)
{
val->first += tc;
val->second += !tc;
}
else
{
auto pair = std::make_pair(tc, !tc);
info.Insert(ImageID, pair);
}
}
void FImageSource::BeginPrecaching()
{
precacheInfo.Clear();
}
void FImageSource::EndPrecaching()
{
precacheDataPaletted.Clear();
precacheDataRgba.Clear();
}
void FImageSource::RegisterForPrecache(FImageSource *img, bool requiretruecolor)
{
img->CollectForPrecache(precacheInfo, requiretruecolor);
}
//==========================================================================
//
//
//
//==========================================================================
typedef FImageSource * (*CreateFunc)(FileReader & file, int lumpnum);
struct TexCreateInfo struct TexCreateInfo
{ {
CreateFunc TryCreate; CreateFunc TryCreate;
bool checkflat;
}; };
FImageSource *PNGImage_TryCreate(FileReader &); FImageSource *PNGImage_TryCreate(FileReader &, int lumpnum);
FImageSource *JPEGImage_TryCreate(FileReader &); FImageSource *JPEGImage_TryCreate(FileReader &, int lumpnum);
FImageSource *DDSImage_TryCreate(FileReader &); FImageSource *DDSImage_TryCreate(FileReader &, int lumpnum);
FImageSource *PCXImage_TryCreate(FileReader &); FImageSource *PCXImage_TryCreate(FileReader &, int lumpnum);
FImageSource *TGAImage_TryCreate(FileReader &); FImageSource *TGAImage_TryCreate(FileReader &, int lumpnum);
FImageSource *ArtImage_TryCreate(FileReader &); FImageSource *StbImage_TryCreate(FileReader &, int lumpnum);
FImageSource *StbImage_TryCreate(FileReader &);
// Examines the lump contents to decide what type of texture to create, // Examines the lump contents to decide what type of texture to create,
// and creates the texture. // and creates the texture.
FImageSource * FImageSource::GetImage(const char *name) FImageSource * FImageSource::GetImage(int lumpnum, bool isflat)
{ {
static TexCreateInfo CreateInfo[] = { static TexCreateInfo CreateInfo[] = {
{ PNGImage_TryCreate }, { PNGImage_TryCreate, false },
{ JPEGImage_TryCreate }, { JPEGImage_TryCreate, false },
{ DDSImage_TryCreate }, { DDSImage_TryCreate, false },
{ PCXImage_TryCreate }, { PCXImage_TryCreate, false },
{ StbImage_TryCreate }, { StbImage_TryCreate, false },
{ ArtImage_TryCreate }, { TGAImage_TryCreate, false },
{ nullptr }
}; };
auto data = fileSystem.OpenFileReader(name); if (lumpnum == -1) return nullptr;
if (!data.isOpen()) return nullptr;
for (size_t i = 0; CreateInfo[i].TryCreate; i++) unsigned size = ImageForLump.Size();
if (size <= (unsigned)lumpnum)
{ {
auto image = CreateInfo[i].TryCreate(data); // Hires textures can be added dynamically to the end of the lump array, so this must be checked each time.
if (image != nullptr) ImageForLump.Resize(lumpnum + 1);
for (; size < ImageForLump.Size(); size++) ImageForLump[size] = nullptr;
}
// An image for this lump already exists. We do not need another one.
if (ImageForLump[lumpnum] != nullptr) return ImageForLump[lumpnum];
auto data = fileSystem.OpenFileReader(lumpnum);
if (!data.isOpen())
return nullptr;
for (size_t i = 0; i < countof(CreateInfo); i++)
{
if (!CreateInfo[i].checkflat || isflat)
{ {
image->Name = name; auto image = CreateInfo[i].TryCreate(data, lumpnum);
return image; if (image != nullptr)
{
ImageForLump[lumpnum] = image;
return image;
}
} }
} }
return nullptr; return nullptr;

View File

@ -10,6 +10,16 @@
class FImageSource; class FImageSource;
using PrecacheInfo = TMap<int, std::pair<int, int>>; using PrecacheInfo = TMap<int, std::pair<int, int>>;
// Doom patch format header
struct patch_t
{
int16_t width; // bounding box size
int16_t height;
int16_t leftoffset; // pixels to the left of origin
int16_t topoffset; // pixels below the origin
uint32_t columnofs[1]; // only [width] used
};
struct PalettedPixels struct PalettedPixels
{ {
friend class FImageSource; friend class FImageSource;
@ -27,8 +37,11 @@ private:
// All it can do is provide raw image data to its users. // All it can do is provide raw image data to its users.
class FImageSource class FImageSource
{ {
friend class FBrightmapTexture;
protected: protected:
static FMemArena ImageArena;
static TArray<FImageSource *>ImageForLump;
static int NextID; static int NextID;
int SourceLump; int SourceLump;
@ -36,14 +49,17 @@ protected:
int LeftOffset = 0, TopOffset = 0; // Offsets stored in the image. int LeftOffset = 0, TopOffset = 0; // Offsets stored in the image.
bool bUseGamePalette = false; // true if this is an image without its own color set. bool bUseGamePalette = false; // true if this is an image without its own color set.
int ImageID = -1; int ImageID = -1;
FString Name;
// Internal image creation functions. All external access should go through the cache interface, // Internal image creation functions. All external access should go through the cache interface,
// so that all code can benefit from future improvements to that. // so that all code can benefit from future improvements to that.
virtual TArray<uint8_t> CreatePalettedPixels(int conversion);
virtual int CopyPixels(FBitmap *bmp, int conversion); // This will always ignore 'luminance'.
int CopyTranslatedPixels(FBitmap *bmp, const PalEntry *remap);
public: public:
virtual ~FImageSource() = default;
void CopySize(FImageSource &other) void CopySize(FImageSource &other)
{ {
Width = other.Width; Width = other.Width;
@ -53,16 +69,30 @@ public:
SourceLump = other.SourceLump; SourceLump = other.SourceLump;
} }
// Images are statically allocated and freed in bulk. None of the subclasses may hold any destructible data.
void *operator new(size_t block) { return ImageArena.Alloc(block); }
void operator delete(void *block) {}
bool bMasked = true; // Image (might) have holes (Assume true unless proven otherwise!) bool bMasked = true; // Image (might) have holes (Assume true unless proven otherwise!)
int8_t bTranslucent = -1; // Image has pixels with a non-0/1 value. (-1 means the user needs to do a real check) int8_t bTranslucent = -1; // Image has pixels with a non-0/1 value. (-1 means the user needs to do a real check)
int GetId() const { return ImageID; } int GetId() const { return ImageID; }
// 'noremap0' will only be looked at by FPatchTexture and forwarded by FMultipatchTexture. // 'noremap0' will only be looked at by FPatchTexture and forwarded by FMultipatchTexture.
static FImageSource * GetImage(const char *name);
virtual void CreatePalettedPixels(uint8_t *destbuffer) = 0; // Either returns a reference to the cache, or a newly created item. The return of this has to be considered transient. If you need to store the result, use GetPalettedPixels
virtual int CopyPixels(FBitmap* bmp, int conversion) = 0; // This will always ignore 'luminance'. PalettedPixels GetCachedPalettedPixels(int conversion);
// tries to get a buffer from the cache. If not available, create a new one. If further references are pending, create a copy.
TArray<uint8_t> GetPalettedPixels(int conversion);
// Unlile for paletted images there is no variant here that returns a persistent bitmap, because all users have to process the returned image into another format.
FBitmap GetCachedBitmap(const PalEntry *remap, int conversion, int *trans = nullptr);
static void ClearImages() { ImageArena.FreeAll(); ImageForLump.Clear(); NextID = 0; }
static FImageSource * GetImage(int lumpnum, bool checkflat);
// Conversion option // Conversion option
@ -74,6 +104,7 @@ public:
}; };
FImageSource(int sourcelump = -1) : SourceLump(sourcelump) { ImageID = ++NextID; } FImageSource(int sourcelump = -1) : SourceLump(sourcelump) { ImageID = ++NextID; }
virtual ~FImageSource() {}
int GetWidth() const int GetWidth() const
{ {
@ -110,6 +141,11 @@ public:
{ {
return bUseGamePalette; return bUseGamePalette;
} }
virtual void CollectForPrecache(PrecacheInfo &info, bool requiretruecolor);
static void BeginPrecaching();
static void EndPrecaching();
static void RegisterForPrecache(FImageSource *img, bool requiretruecolor);
}; };
//========================================================================== //==========================================================================

View File

@ -39,24 +39,49 @@
#include <stdint.h> #include <stdint.h>
#include "tarray.h" #include "tarray.h"
#include "palentry.h" #include "colormatcher.h"
#include "bitmap.h" #include "bitmap.h"
#include "palutil.h"
#include "palettecontainer.h" #include "palettecontainer.h"
#include "v_colortables.h" #include "v_colortables.h"
namespace ImageHelpers namespace ImageHelpers
{ {
extern int alphaThreshold; // Helpers for creating paletted images.
inline uint8_t *GetRemap(bool wantluminance, bool srcisgrayscale = false)
{
if (wantluminance)
{
return srcisgrayscale ? GPalette.GrayRamp.Remap : GPalette.GrayscaleMap.Remap;
}
else
{
return srcisgrayscale ? GPalette.GrayMap : GPalette.Remap;
}
}
inline uint8_t RGBToPalettePrecise(bool wantluminance, int r, int g, int b, int a = 255) inline uint8_t RGBToPalettePrecise(bool wantluminance, int r, int g, int b, int a = 255)
{ {
return BestColor((uint32_t*)GPalette.BaseColors, r, g, b); if (wantluminance)
{
return (uint8_t)Luminance(r, g, b) * a / 255;
}
else
{
return ColorMatcher.Pick(r, g, b);
}
} }
inline uint8_t RGBToPalette(bool wantluminance, int r, int g, int b, int a = 255) inline uint8_t RGBToPalette(bool wantluminance, int r, int g, int b, int a = 255)
{ {
return a < alphaThreshold? 255 : RGB256k.RGB[r >> 2][g >> 2][b >> 2]; if (wantluminance)
{
// This is the same formula the OpenGL renderer uses for grayscale textures with an alpha channel.
return (uint8_t)(Luminance(r, g, b) * a / 255);
}
else
{
return a < 128? 0 : RGB256k.RGB[r >> 2][g >> 2][b >> 2];
}
} }
inline uint8_t RGBToPalette(bool wantluminance, PalEntry pe, bool hasalpha = true) inline uint8_t RGBToPalette(bool wantluminance, PalEntry pe, bool hasalpha = true)

View File

@ -71,20 +71,7 @@ FImageTexture::FImageTexture(FImageSource *img, const char *name)
FBitmap FImageTexture::GetBgraBitmap(const PalEntry *p, int *trans) FBitmap FImageTexture::GetBgraBitmap(const PalEntry *p, int *trans)
{ {
FBitmap bmp; return mImage->GetCachedBitmap(p, FImageSource::normal, trans);
bmp.Create(Size.x, Size.y);
if (p == nullptr)
{
mImage->CopyPixels(&bmp, 0);
}
else
{
// For different base palettes the image needs to be downconverted.
TArray<uint8_t> ppix(Size.x * Size.y, true);
mImage->CreatePalettedPixels(ppix.Data());
bmp.CopyPixelData(0, 0, ppix.Data(), Size.x, Size.y, Size.y, 1, 0, p);
}
return bmp;
} }
//=========================================================================== //===========================================================================
@ -95,6 +82,7 @@ FBitmap FImageTexture::GetBgraBitmap(const PalEntry *p, int *trans)
void FImageTexture::Create8BitPixels(uint8_t* buffer) void FImageTexture::Create8BitPixels(uint8_t* buffer)
{ {
ImageHelpers::alphaThreshold = alphaThreshold; //ImageHelpers::alphaThreshold = alphaThreshold;
return mImage->CreatePalettedPixels(buffer); auto buf = mImage->GetPalettedPixels(FImageSource::normal);
memcpy(buffer, buf.Data(), buf.Size());
} }

View File

@ -56,7 +56,9 @@ FTexture *CreateBrightmapTexture(FImageSource*);
// and creates the texture. // and creates the texture.
FTexture * FTexture::CreateTexture(const char *name) FTexture * FTexture::CreateTexture(const char *name)
{ {
auto image = FImageSource::GetImage(name); int lump = fileSystem.FindFile(name);
if (lump < 0) return nullptr;
auto image = FImageSource::GetImage(lump, false);
if (image != nullptr) if (image != nullptr)
{ {
FTexture *tex = new FImageTexture(image); FTexture *tex = new FImageTexture(image);