diff --git a/src/textures/formats/automaptexture.cpp b/src/textures/formats/automaptexture.cpp index 4eb4ec353..3c3763678 100644 --- a/src/textures/formats/automaptexture.cpp +++ b/src/textures/formats/automaptexture.cpp @@ -39,6 +39,7 @@ #include "files.h" #include "w_wad.h" #include "textures/textures.h" +#include "imagehelpers.h" //========================================================================== // @@ -96,7 +97,7 @@ TArray FAutomapTexture::Get8BitPixels(bool alphatex) TArray Pixels(Width * Height, true); - const uint8_t *remap = GetRemap(alphatex); + const uint8_t *remap = ImageHelpers::GetRemap(alphatex); for (x = 0; x < Width; ++x) { for (y = 0; y < Height; ++y) diff --git a/src/textures/formats/ddstexture.cpp b/src/textures/formats/ddstexture.cpp index d13733ccf..cbcb1b562 100644 --- a/src/textures/formats/ddstexture.cpp +++ b/src/textures/formats/ddstexture.cpp @@ -53,6 +53,7 @@ #include "w_wad.h" #include "bitmap.h" #include "v_video.h" +#include "imagehelpers.h" // Since we want this to compile under Linux too, we need to define this // stuff ourselves instead of including a DirectX header. @@ -470,7 +471,7 @@ void FDDSTexture::ReadRGB (FileReader &lump, uint8_t *buffer, int pixelmode) uint32_t g = (c & GMask) << GShiftL; g |= g >> GShiftR; uint32_t b = (c & BMask) << BShiftL; b |= b >> BShiftR; uint32_t a = (c & AMask) << AShiftL; a |= a >> AShiftR; - *pixelp = RGBToPalette(pixelmode == PIX_Alphatex, r >> 24, g >> 24, b >> 24, a >> 24); + *pixelp = ImageHelpers::RGBToPalette(pixelmode == PIX_Alphatex, r >> 24, g >> 24, b >> 24, a >> 24); } else { @@ -556,7 +557,7 @@ void FDDSTexture::DecompressDXT1 (FileReader &lump, uint8_t *buffer, int pixelmo // Pick colors from the palette for each of the four colors. if (pixelmode != PIX_ARGB) for (i = 3; i >= 0; --i) { - palcol[i] = RGBToPalette(pixelmode == PIX_Alphatex, color[i]); + palcol[i] = ImageHelpers::RGBToPalette(pixelmode == PIX_Alphatex, color[i]); } // Now decode this 4x4 block to the pixel buffer. for (y = 0; y < 4; ++y) @@ -636,7 +637,7 @@ void FDDSTexture::DecompressDXT3 (FileReader &lump, bool premultiplied, uint8_t // Pick colors from the palette for each of the four colors. if (pixelmode != PIX_ARGB) for (i = 3; i >= 0; --i) { - palcol[i] = RGBToPalette(pixelmode == PIX_Alphatex, color[i], false); + palcol[i] = ImageHelpers::RGBToPalette(pixelmode == PIX_Alphatex, color[i], false); } // Now decode this 4x4 block to the pixel buffer. @@ -748,7 +749,7 @@ void FDDSTexture::DecompressDXT5 (FileReader &lump, bool premultiplied, uint8_t // Pick colors from the palette for each of the four colors. if (pixelmode != PIX_ARGB) for (i = 3; i >= 0; --i) { - palcol[i] = RGBToPalette(pixelmode == PIX_Alphatex, color[i], false); + palcol[i] = ImageHelpers::RGBToPalette(pixelmode == PIX_Alphatex, color[i], false); } // Now decode this 4x4 block to the pixel buffer. for (y = 0; y < 4; ++y) diff --git a/src/textures/formats/flattexture.cpp b/src/textures/formats/flattexture.cpp index 7f1dfbd52..44a814d71 100644 --- a/src/textures/formats/flattexture.cpp +++ b/src/textures/formats/flattexture.cpp @@ -37,6 +37,7 @@ #include "files.h" #include "w_wad.h" #include "textures/textures.h" +#include "imagehelpers.h" //========================================================================== // @@ -110,7 +111,7 @@ TArray FFlatTexture::Get8BitPixels(bool alphatex) { memset (Pixels.Data() + numread, 0xBB, Width*Height - numread); } - FTexture::FlipSquareBlockRemap(Pixels.Data(), Width, Height, GetRemap(alphatex)); + ImageHelpers::FlipSquareBlockRemap(Pixels.Data(), Width, ImageHelpers::GetRemap(alphatex)); return Pixels; } diff --git a/src/textures/formats/imgztexture.cpp b/src/textures/formats/imgztexture.cpp index 1486fdedc..7523d2080 100644 --- a/src/textures/formats/imgztexture.cpp +++ b/src/textures/formats/imgztexture.cpp @@ -38,6 +38,7 @@ #include "w_wad.h" #include "v_video.h" #include "bitmap.h" +#include "imagehelpers.h" bool checkIMGZPalette(FileReader &file); @@ -134,7 +135,7 @@ TArray FIMGZTexture::Get8BitPixels(bool alphatex) TArray Pixels(Width*Height, true); dest_p = Pixels.Data(); - const uint8_t *remap = GetRemap(alphatex, isalpha); + const uint8_t *remap = ImageHelpers::GetRemap(alphatex, isalpha); // Convert the source image from row-major to column-major format and remap it if (!imgz->Compression) diff --git a/src/textures/formats/jpegtexture.cpp b/src/textures/formats/jpegtexture.cpp index fd8a7beb7..647fc139c 100644 --- a/src/textures/formats/jpegtexture.cpp +++ b/src/textures/formats/jpegtexture.cpp @@ -45,6 +45,7 @@ extern "C" #include "v_text.h" #include "bitmap.h" #include "v_video.h" +#include "imagehelpers.h" struct FLumpSourceMgr : public jpeg_source_mgr @@ -316,7 +317,7 @@ TArray FJPEGTexture::Get8BitPixels(bool doalpha) case JCS_RGB: for (int x = Width; x > 0; --x) { - *out = RGBToPalette(doalpha, in[0], in[1], in[2]); + *out = ImageHelpers::RGBToPalette(doalpha, in[0], in[1], in[2]); out += Height; in += 3; } @@ -324,7 +325,7 @@ TArray FJPEGTexture::Get8BitPixels(bool doalpha) case JCS_GRAYSCALE: { - auto remap = GetRemap(doalpha, true); + auto remap = ImageHelpers::GetRemap(doalpha, true); for (int x = Width; x > 0; --x) { *out = remap[in[0]]; @@ -342,7 +343,7 @@ TArray FJPEGTexture::Get8BitPixels(bool doalpha) int r = in[3] - (((256 - in[0])*in[3]) >> 8); int g = in[3] - (((256 - in[1])*in[3]) >> 8); int b = in[3] - (((256 - in[2])*in[3]) >> 8); - *out = RGBToPalette(doalpha, r, g, b); + *out = ImageHelpers::RGBToPalette(doalpha, r, g, b); out += Height; in += 4; } @@ -356,7 +357,7 @@ TArray FJPEGTexture::Get8BitPixels(bool doalpha) 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 b = clamp((int)(Y + 1.77200 * (Cb - 0x80)), 0, 255); - *out = RGBToPalette(doalpha, r, g, b); + *out = ImageHelpers::RGBToPalette(doalpha, r, g, b); out += Height; in += 4; } diff --git a/src/textures/formats/multipatchtexture.cpp b/src/textures/formats/multipatchtexture.cpp index 1def77b5b..f8e7078d8 100644 --- a/src/textures/formats/multipatchtexture.cpp +++ b/src/textures/formats/multipatchtexture.cpp @@ -49,6 +49,7 @@ #include "v_video.h" #include "v_text.h" #include "cmdlib.h" +#include "imagehelpers.h" // On the Alpha, accessing the shorts directly if they aren't aligned on a // 4-byte boundary causes unaligned access warnings. Why it does this at @@ -450,7 +451,7 @@ TArray FMultiPatchTexture::MakeTexture (bool alphatex) { if (*out == 0 && in[3] != 0) { - *out = RGBToPalette(alphatex, in[2], in[1], in[0]); + *out = ImageHelpers::RGBToPalette(alphatex, in[2], in[1], in[0]); } out += Height; in += 4; diff --git a/src/textures/formats/patchtexture.cpp b/src/textures/formats/patchtexture.cpp index 5748bbce4..25492916b 100644 --- a/src/textures/formats/patchtexture.cpp +++ b/src/textures/formats/patchtexture.cpp @@ -39,6 +39,7 @@ #include "v_palette.h" #include "v_video.h" #include "bitmap.h" +#include "imagehelpers.h" // posts are runs of non masked source pixels @@ -179,7 +180,7 @@ TArray FPatchTexture::Get8BitPixels(bool alphatex) maxcol = (const column_t *)((const uint8_t *)patch + Wads.LumpLength (SourceLump) - 3); - remap = GetRemap(alphatex, isalpha); + remap = ImageHelpers::GetRemap(alphatex, isalpha); // Special case for skies if (bNoRemap0 && remap == GPalette.Remap) { diff --git a/src/textures/formats/pcxtexture.cpp b/src/textures/formats/pcxtexture.cpp index fc9863f24..14531c5c6 100644 --- a/src/textures/formats/pcxtexture.cpp +++ b/src/textures/formats/pcxtexture.cpp @@ -39,6 +39,7 @@ #include "w_wad.h" #include "bitmap.h" #include "v_video.h" +#include "imagehelpers.h" //========================================================================== // @@ -391,15 +392,15 @@ TArray FPCXTexture::Get8BitPixels(bool alphatex) { default: case 1: - PaletteMap[0] = alphatex? 0 : GrayMap[0]; - PaletteMap[1] = alphatex? 255 : GrayMap[255]; + PaletteMap[0] = alphatex? 0 : ImageHelpers::GrayMap[0]; + PaletteMap[1] = alphatex? 255 : ImageHelpers::GrayMap[255]; ReadPCX1bit (Pixels.Data(), lump, &header); break; case 4: for (int i = 0; i < 16; i++) { - PaletteMap[i] = RGBToPalettePrecise(alphatex, 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 (Pixels.Data(), lump, &header); break; @@ -416,19 +417,19 @@ TArray FPCXTexture::Get8BitPixels(bool alphatex) uint8_t r = lump.ReadUInt8(); uint8_t g = lump.ReadUInt8(); uint8_t b = lump.ReadUInt8(); - PaletteMap[i] = RGBToPalettePrecise(alphatex, r, g, b); + PaletteMap[i] = ImageHelpers::RGBToPalettePrecise(alphatex, r, g, b); } lump.Seek(sizeof(header), FileReader::SeekSet); ReadPCX8bits (Pixels.Data(), lump, &header); } if (Width == Height) { - FlipSquareBlockRemap(Pixels.Data(), Width, Height, PaletteMap); + ImageHelpers::FlipSquareBlockRemap(Pixels.Data(), Width, PaletteMap); } else { TArray newpix(Width*Height, true); - FlipNonSquareBlockRemap (newpix.Data(), Pixels.Data(), Width, Height, Width, PaletteMap); + ImageHelpers::FlipNonSquareBlockRemap (newpix.Data(), Pixels.Data(), Width, Height, Width, PaletteMap); return newpix; } } @@ -441,7 +442,7 @@ TArray FPCXTexture::Get8BitPixels(bool alphatex) { for(int x=0; x < Width; x++) { - Pixels[y + Height * x] = RGBToPalette(alphatex, row[0], row[1], row[2]); + Pixels[y + Height * x] = ImageHelpers::RGBToPalette(alphatex, row[0], row[1], row[2]); row+=3; } } diff --git a/src/textures/formats/pngtexture.cpp b/src/textures/formats/pngtexture.cpp index 0d1cd661c..75ae9f5d7 100644 --- a/src/textures/formats/pngtexture.cpp +++ b/src/textures/formats/pngtexture.cpp @@ -39,6 +39,7 @@ #include "templates.h" #include "m_png.h" #include "bitmap.h" +#include "imagehelpers.h" //========================================================================== // @@ -293,12 +294,12 @@ FPNGTexture::FPNGTexture (FileReader &lump, int lumpnum, const FString &filename bMasked = true; PaletteSize = 256; PaletteMap = new uint8_t[256]; - memcpy (PaletteMap, GrayMap, 256); + memcpy (PaletteMap, ImageHelpers::GrayMap, 256); PaletteMap[NonPaletteTrans[0]] = 0; } else { - PaletteMap = GrayMap; + PaletteMap = ImageHelpers::GrayMap; } break; @@ -335,7 +336,7 @@ FPNGTexture::FPNGTexture (FileReader &lump, int lumpnum, const FString &filename FPNGTexture::~FPNGTexture () { - if (PaletteMap != nullptr && PaletteMap != FTexture::GrayMap) + if (PaletteMap != nullptr && PaletteMap != ImageHelpers::GrayMap) { delete[] PaletteMap; PaletteMap = nullptr; @@ -424,17 +425,17 @@ TArray FPNGTexture::Get8BitPixels(bool alphatex) { if (!alphatex) { - FTexture::FlipSquareBlockRemap (Pixels.Data(), Width, Height, PaletteMap); + ImageHelpers::FlipSquareBlockRemap (Pixels.Data(), Width, PaletteMap); } else if (ColorType == 0) { - FTexture::FlipSquareBlock (Pixels.Data(), Width, Height); + ImageHelpers::FlipSquareBlock (Pixels.Data(), Width); } else { uint8_t alpharemap[256]; ReadAlphaRemap(lump, alpharemap); - FTexture::FlipSquareBlockRemap(Pixels.Data(), Width, Height, alpharemap); + ImageHelpers::FlipSquareBlockRemap(Pixels.Data(), Width, alpharemap); } } else @@ -442,17 +443,17 @@ TArray FPNGTexture::Get8BitPixels(bool alphatex) TArray newpix(Width*Height, true); if (!alphatex) { - FTexture::FlipNonSquareBlockRemap (newpix.Data(), Pixels.Data(), Width, Height, Width, PaletteMap); + ImageHelpers::FlipNonSquareBlockRemap (newpix.Data(), Pixels.Data(), Width, Height, Width, PaletteMap); } else if (ColorType == 0) { - FTexture::FlipNonSquareBlock (newpix.Data(), Pixels.Data(), Width, Height, Width); + ImageHelpers::FlipNonSquareBlock (newpix.Data(), Pixels.Data(), Width, Height, Width); } else { uint8_t alpharemap[256]; ReadAlphaRemap(lump, alpharemap); - FTexture::FlipNonSquareBlockRemap(newpix.Data(), Pixels.Data(), Width, Height, Width, alpharemap); + ImageHelpers::FlipNonSquareBlockRemap(newpix.Data(), Pixels.Data(), Width, Height, Width, alpharemap); } return newpix; } @@ -485,7 +486,7 @@ TArray FPNGTexture::Get8BitPixels(bool alphatex) } else { - *out++ = RGBToPalette(alphatex, in[0], in[1], in[2]); + *out++ = ImageHelpers::RGBToPalette(alphatex, in[0], in[1], in[2]); } in += pitch; } @@ -514,7 +515,7 @@ TArray FPNGTexture::Get8BitPixels(bool alphatex) { for (y = Height; y > 0; --y) { - *out++ = RGBToPalette(alphatex, in[0], in[1], in[2], in[3]); + *out++ = ImageHelpers::RGBToPalette(alphatex, in[0], in[1], in[2], in[3]); in += pitch; } in -= backstep; diff --git a/src/textures/formats/rawpagetexture.cpp b/src/textures/formats/rawpagetexture.cpp index 901f23a41..72ccecf26 100644 --- a/src/textures/formats/rawpagetexture.cpp +++ b/src/textures/formats/rawpagetexture.cpp @@ -39,6 +39,7 @@ #include "gi.h" #include "bitmap.h" #include "textures/textures.h" +#include "imagehelpers.h" //========================================================================== @@ -184,7 +185,7 @@ TArray FRawPageTexture::Get8BitPixels(bool alphatex) TArray Pixels(Width*Height, true); dest_p = Pixels.Data(); - const uint8_t *remap = GetRemap(alphatex); + const uint8_t *remap = ImageHelpers::GetRemap(alphatex); // This does not handle the custom palette. // User maps are encouraged to use a real image format when replacing E2END and the original could never be used anywhere else. diff --git a/src/textures/formats/shadertexture.cpp b/src/textures/formats/shadertexture.cpp index 9b5eddce1..e68fdf7e5 100644 --- a/src/textures/formats/shadertexture.cpp +++ b/src/textures/formats/shadertexture.cpp @@ -39,6 +39,7 @@ #include "menu/menu.h" #include "w_wad.h" #include "bitmap.h" +#include "imagehelpers.h" class FBarShader : public FWorldTexture @@ -113,7 +114,7 @@ public: // even if it makes little sense. for (int i = 0; i < 512; i++) { - Pix[i] = GrayMap[Pixels[i]]; + Pix[i] = ImageHelpers::GrayMap[Pixels[i]]; } } return Pix; diff --git a/src/textures/formats/tgatexture.cpp b/src/textures/formats/tgatexture.cpp index 111e3e473..12b420e19 100644 --- a/src/textures/formats/tgatexture.cpp +++ b/src/textures/formats/tgatexture.cpp @@ -39,6 +39,7 @@ #include "templates.h" #include "bitmap.h" #include "v_video.h" +#include "imagehelpers.h" //========================================================================== @@ -244,7 +245,7 @@ TArray FTGATexture::Get8BitPixels(bool alphatex) r=g=b=a=0; break; } - PaletteMap[i] = RGBToPalettePrecise(alphatex, r, g, b, a); + PaletteMap[i] = ImageHelpers::RGBToPalettePrecise(alphatex, r, g, b, a); } } @@ -303,7 +304,7 @@ TArray FTGATexture::Get8BitPixels(bool alphatex) for(int x=0;x> 10) & 0x1f) * 8, ((v >> 5) & 0x1f) * 8, (v & 0x1f) * 8); + Pixels[x*Height + y] = ImageHelpers::RGBToPalette(alphatex, ((v >> 10) & 0x1f) * 8, ((v >> 5) & 0x1f) * 8, (v & 0x1f) * 8); p+=step_x; } } @@ -315,7 +316,7 @@ TArray FTGATexture::Get8BitPixels(bool alphatex) uint8_t * p = ptr + y * Pitch; for(int x=0;x FTGATexture::Get8BitPixels(bool alphatex) uint8_t * p = ptr + y * Pitch; for(int x=0;x FTGATexture::Get8BitPixels(bool alphatex) uint8_t * p = ptr + y * Pitch; for(int x=0;x FTGATexture::Get8BitPixels(bool alphatex) case 3: // Grayscale { - auto remap = GetRemap(alphatex, true); + auto remap = ImageHelpers::GetRemap(alphatex, true); switch (hdr.bpp) { case 8: diff --git a/src/textures/imagehelpers.h b/src/textures/imagehelpers.h new file mode 100644 index 000000000..16b5e1876 --- /dev/null +++ b/src/textures/imagehelpers.h @@ -0,0 +1,154 @@ +#pragma once + +/* + ** imagehelpers.h + ** Utilities for image conversion - mostly 8 bit paletted baggage + ** + **--------------------------------------------------------------------------- + ** Copyright 2004-2007 Randy Heit + ** Copyright 2006-2018 Christoph Oelckers + ** 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 +#include "tarray.h" +#include "colormatcher.h" +#include "v_palette.h" +#include "textures/bitmap.h" +#include "r_data/renderstyle.h" +#include "r_data/r_translate.h" + +namespace ImageHelpers +{ + extern uint8_t GrayMap[256]; + + // Helpers for creating paletted images. + inline uint8_t *GetRemap(bool wantluminance, bool srcisgrayscale = false) + { + if (wantluminance) + { + return translationtables[TRANSLATION_Standard][srcisgrayscale ? STD_Gray : STD_Grayscale]->Remap; + } + else + { + return srcisgrayscale ? GrayMap : GPalette.Remap; + } + } + + inline uint8_t RGBToPalettePrecise(bool wantluminance, int r, int g, int b, int a = 255) + { + 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) + { + 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) + { + return RGBToPalette(wantluminance, pe.r, pe.g, pe.b, hasalpha? pe.a : 255); + } + + //========================================================================== + // + // Converts a texture between row-major and column-major format + // by flipping it about the X=Y axis. + // + //========================================================================== + + template + void FlipSquareBlock (T *block, int x) + { + for (int i = 0; i < x; ++i) + { + uint8_t *corner = block + x*i + i; + int count = x - i; + for (int j = 0; j < count; j++) + { + std::swap(corner[j], corner[j*x]); + } + } + } + + inline void FlipSquareBlockRemap (uint8_t *block, int x, const uint8_t *remap) + { + for (int i = 0; i < x; ++i) + { + uint8_t *corner = block + x*i + i; + int count = x - i; + for (int j = 0; j < count; j++) + { + auto t = remap[corner[j]]; + corner[j] = remap[corner[j*x]]; + corner[j*x] = t; + } + } + } + + template + void FlipNonSquareBlock (T *dst, const T *src, int x, int y, int srcpitch) + { + for (int i = 0; i < x; ++i) + { + for (int j = 0; j < y; ++j) + { + dst[i*y+j] = src[i+j*srcpitch]; + } + } + } + + inline void FlipNonSquareBlockRemap (uint8_t *dst, const uint8_t *src, int x, int y, int srcpitch, const uint8_t *remap) + { + for (int i = 0; i < x; ++i) + { + for (int j = 0; j < y; ++j) + { + dst[i*y+j] = remap[src[i+j*srcpitch]]; + } + } + } + +} diff --git a/src/textures/texture.cpp b/src/textures/texture.cpp index 87aa95c19..06e73a6d8 100644 --- a/src/textures/texture.cpp +++ b/src/textures/texture.cpp @@ -48,6 +48,7 @@ #include "hwrenderer/textures/hw_material.h" #include "hwrenderer/textures/hw_ihwtexture.h" #include "swrenderer/textures/r_swtexture.h" +#include "imagehelpers.h" FTexture *CreateBrightmapTexture(FTexture*); @@ -66,13 +67,13 @@ CUSTOM_CVAR(Int, r_spriteadjust, 2, CVAR_ARCHIVE | CVAR_GLOBALCONFIG) // //========================================================================== -uint8_t FTexture::GrayMap[256]; +uint8_t ImageHelpers::GrayMap[256]; void FTexture::InitGrayMap() { for (int i = 0; i < 256; ++i) { - GrayMap[i] = ColorMatcher.Pick(i, i, i); + ImageHelpers::GrayMap[i] = ColorMatcher.Pick(i, i, i); } } @@ -297,128 +298,6 @@ void FTexture::CopyToBlock (uint8_t *dest, int dwidth, int dheight, int xpos, in } } -//========================================================================== -// -// Converts a texture between row-major and column-major format -// by flipping it about the X=Y axis. -// -//========================================================================== - -void FTexture::FlipSquareBlock (uint8_t *block, int x, int y) -{ - int i, j; - - if (x != y) return; - - for (i = 0; i < x; ++i) - { - uint8_t *corner = block + x*i + i; - int count = x - i; - if (count & 1) - { - count--; - swapvalues (corner[count], corner[count*x]); - } - for (j = 0; j < count; j += 2) - { - swapvalues (corner[j], corner[j*x]); - swapvalues (corner[j+1], corner[(j+1)*x]); - } - } -} - -void FTexture::FlipSquareBlockBgra(uint32_t *block, int x, int y) -{ - int i, j; - - if (x != y) return; - - for (i = 0; i < x; ++i) - { - uint32_t *corner = block + x*i + i; - int count = x - i; - if (count & 1) - { - count--; - swapvalues(corner[count], corner[count*x]); - } - for (j = 0; j < count; j += 2) - { - swapvalues(corner[j], corner[j*x]); - swapvalues(corner[j + 1], corner[(j + 1)*x]); - } - } -} - -void FTexture::FlipSquareBlockRemap (uint8_t *block, int x, int y, const uint8_t *remap) -{ - int i, j; - uint8_t t; - - if (x != y) return; - - for (i = 0; i < x; ++i) - { - uint8_t *corner = block + x*i + i; - int count = x - i; - if (count & 1) - { - count--; - t = remap[corner[count]]; - corner[count] = remap[corner[count*x]]; - corner[count*x] = t; - } - for (j = 0; j < count; j += 2) - { - t = remap[corner[j]]; - corner[j] = remap[corner[j*x]]; - corner[j*x] = t; - t = remap[corner[j+1]]; - corner[j+1] = remap[corner[(j+1)*x]]; - corner[(j+1)*x] = t; - } - } -} - -void FTexture::FlipNonSquareBlock (uint8_t *dst, const uint8_t *src, int x, int y, int srcpitch) -{ - int i, j; - - for (i = 0; i < x; ++i) - { - for (j = 0; j < y; ++j) - { - dst[i*y+j] = src[i+j*srcpitch]; - } - } -} - -void FTexture::FlipNonSquareBlockBgra(uint32_t *dst, const uint32_t *src, int x, int y, int srcpitch) -{ - int i, j; - - for (i = 0; i < x; ++i) - { - for (j = 0; j < y; ++j) - { - dst[i*y + j] = src[i + j*srcpitch]; - } - } -} - -void FTexture::FlipNonSquareBlockRemap (uint8_t *dst, const uint8_t *src, int x, int y, int srcpitch, const uint8_t *remap) -{ - int i, j; - - for (i = 0; i < x; ++i) - { - for (j = 0; j < y; ++j) - { - dst[i*y+j] = remap[src[i+j*srcpitch]]; - } - } -} - //=========================================================================== // // FTexture::CopyPixels diff --git a/src/textures/textures.h b/src/textures/textures.h index ec609e641..3541f432f 100644 --- a/src/textures/textures.h +++ b/src/textures/textures.h @@ -304,12 +304,14 @@ public: /*virtual*/ FBitmap GetBgraBitmap(PalEntry *remap, int *trans = nullptr); public: + /* static void FlipSquareBlock (uint8_t *block, int x, int y); static void FlipSquareBlockBgra (uint32_t *block, int x, int y); static void FlipSquareBlockRemap (uint8_t *block, int x, int y, const uint8_t *remap); static void FlipNonSquareBlock (uint8_t *blockto, const uint8_t *blockfrom, int x, int y, int srcpitch); static void FlipNonSquareBlockBgra (uint32_t *blockto, const uint32_t *blockfrom, int x, int y, int srcpitch); static void FlipNonSquareBlockRemap (uint8_t *blockto, const uint8_t *blockfrom, int x, int y, int srcpitch, const uint8_t *remap); + */ static bool SmoothEdges(unsigned char * buffer,int w, int h); static PalEntry averageColor(const uint32_t *data, int size, int maxout); @@ -459,53 +461,6 @@ protected: protected: uint16_t Width, Height; int16_t _LeftOffset[2], _TopOffset[2]; - static uint8_t GrayMap[256]; - uint8_t *GetRemap(bool wantluminance, bool srcisgrayscale = false) - { - if (wantluminance) - { - return translationtables[TRANSLATION_Standard][srcisgrayscale ? STD_Gray : STD_Grayscale]->Remap; - } - else - { - return srcisgrayscale ? GrayMap : GPalette.Remap; - } - } - - uint8_t RGBToPalettePrecise(bool wantluminance, int r, int g, int b, int a = 255) - { - if (wantluminance) - { - return (uint8_t)Luminance(r, g, b) * a / 255; - } - else - { - return ColorMatcher.Pick(r, g, b); - } - } - - uint8_t RGBToPalette(bool wantluminance, int r, int g, int b, int a = 255) - { - 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]; - } - } - - uint8_t RGBToPalette(bool wantluminance, PalEntry pe, bool hasalpha = true) - { - return RGBToPalette(wantluminance, pe.r, pe.g, pe.b, hasalpha? pe.a : 255); - } - - uint8_t RGBToPalette(FRenderStyle style, int r, int g, int b, int a = 255) - { - return RGBToPalette(!!(style.Flags & STYLEF_RedIsAlpha), r, g, b, a); - } FTexture (const char *name = NULL, int lumpnum = -1); @@ -635,13 +590,6 @@ public: void DeleteAll(); void SpriteAdjustChanged(); - // Replaces one texture with another. The new texture will be assigned - // the same name, slot, and use type as the texture it is replacing. - // The old texture will no longer be managed. Set free true if you want - // the old texture to be deleted or set it false if you want it to - // be left alone in memory. You will still need to delete it at some - // point, because the texture manager no longer knows about it. - // This function can be used for such things as warping textures. void ReplaceTexture (FTextureID picnum, FTexture *newtexture, bool free); int NumTextures () const { return (int)Textures.Size(); }