From cf15ebc966492288094a1b33cb7352c6e6590289 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Tue, 20 Aug 2019 20:38:29 +0200 Subject: [PATCH] - inlined the entire ColorMatcher. --- src/CMakeLists.txt | 1 - src/doomtype.h | 83 +--------------------------------- src/{ => utility}/basictypes.h | 0 src/utility/colormatcher.cpp | 79 -------------------------------- src/utility/colormatcher.h | 29 +++++++++--- src/utility/palentry.h | 82 +++++++++++++++++++++++++++++++++ 6 files changed, 106 insertions(+), 168 deletions(-) rename src/{ => utility}/basictypes.h (100%) delete mode 100644 src/utility/colormatcher.cpp create mode 100644 src/utility/palentry.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d31ed54462..f6401eda38 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1308,7 +1308,6 @@ set (PCH_SOURCES utility/stats.cpp utility/atterm.cpp utility/cmdlib.cpp - utility/colormatcher.cpp utility/configfile.cpp utility/i_module.cpp utility/i_time.cpp diff --git a/src/doomtype.h b/src/doomtype.h index b361bf77c4..bc5e25da5b 100644 --- a/src/doomtype.h +++ b/src/doomtype.h @@ -10,10 +10,6 @@ #ifndef __DOOMTYPE__ #define __DOOMTYPE__ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - #ifdef _MSC_VER // VC++ does not define PATH_MAX, but the Windows headers do define MAX_PATH. // However, we want to avoid including the Windows headers in most of the @@ -104,84 +100,7 @@ enum DMSG_SPAMMY, // for those who want to see everything, regardless of its usefulness. }; -struct PalEntry -{ - PalEntry() = default; - PalEntry (uint32_t argb) { d = argb; } - operator uint32_t () const { return d; } - void SetRGB(PalEntry other) - { - d = other.d & 0xffffff; - } - PalEntry Modulate(PalEntry other) const - { - if (isWhite()) - { - return other; - } - else if (other.isWhite()) - { - return *this; - } - else - { - other.r = (r * other.r) / 255; - other.g = (g * other.g) / 255; - other.b = (b * other.b) / 255; - return other; - } - } - int Luminance() const - { - return (r * 77 + g * 143 + b * 37) >> 8; - } - - void Decolorize() // this for 'nocoloredspritelighting' and not the same as desaturation. The normal formula results in a value that's too dark. - { - int v = (r + g + b); - r = g = b = ((255*3) + v + v) / 9; - } - bool isBlack() const - { - return (d & 0xffffff) == 0; - } - bool isWhite() const - { - return (d & 0xffffff) == 0xffffff; - } - PalEntry &operator= (const PalEntry &other) = default; - PalEntry &operator= (uint32_t other) { d = other; return *this; } - PalEntry InverseColor() const { PalEntry nc; nc.a = a; nc.r = 255 - r; nc.g = 255 - g; nc.b = 255 - b; return nc; } -#ifdef __BIG_ENDIAN__ - PalEntry (uint8_t ir, uint8_t ig, uint8_t ib) : a(0), r(ir), g(ig), b(ib) {} - PalEntry (uint8_t ia, uint8_t ir, uint8_t ig, uint8_t ib) : a(ia), r(ir), g(ig), b(ib) {} - union - { - struct - { - uint8_t a,r,g,b; - }; - uint32_t d; - }; -#else - PalEntry (uint8_t ir, uint8_t ig, uint8_t ib) : b(ib), g(ig), r(ir), a(0) {} - PalEntry (uint8_t ia, uint8_t ir, uint8_t ig, uint8_t ib) : b(ib), g(ig), r(ir), a(ia) {} - union - { - struct - { - uint8_t b,g,r,a; - }; - uint32_t d; - }; -#endif -}; - -inline int Luminance(int r, int g, int b) -{ - return (r * 77 + g * 143 + b * 37) >> 8; -} - +#include "palentry.h" enum class ETextureType : uint8_t { diff --git a/src/basictypes.h b/src/utility/basictypes.h similarity index 100% rename from src/basictypes.h rename to src/utility/basictypes.h diff --git a/src/utility/colormatcher.cpp b/src/utility/colormatcher.cpp deleted file mode 100644 index aa98e54d0d..0000000000 --- a/src/utility/colormatcher.cpp +++ /dev/null @@ -1,79 +0,0 @@ -/* -** colormatcher.cpp -** My attempt at a fast color matching system -** -**--------------------------------------------------------------------------- -** Copyright 1998-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. -**--------------------------------------------------------------------------- -** -** Once upon a time, this tried to be a fast closest color finding system. -** It was, but the results were not as good as I would like, so I didn't -** actually use it. But I did keep the code around in case I ever felt like -** revisiting the problem. I never did, so now it's relegated to the mists -** of SVN history, and this is just a thin wrapper around BestColor(). -** -*/ - -#include - -#include "doomtype.h" -#include "colormatcher.h" -#include "v_palette.h" - -FColorMatcher::FColorMatcher () -{ - Pal = NULL; -} - -FColorMatcher::FColorMatcher (const uint32_t *palette) -{ - SetPalette (palette); -} - -FColorMatcher::FColorMatcher (const FColorMatcher &other) -{ - *this = other; -} - -FColorMatcher &FColorMatcher::operator= (const FColorMatcher &other) -{ - Pal = other.Pal; - return *this; -} - -void FColorMatcher::SetPalette (const uint32_t *palette) -{ - Pal = (const PalEntry *)palette; -} - -uint8_t FColorMatcher::Pick (int r, int g, int b) -{ - if (Pal == NULL) - return 1; - - return (uint8_t)BestColor ((uint32_t *)Pal, r, g, b); -} diff --git a/src/utility/colormatcher.h b/src/utility/colormatcher.h index e605fe0553..4097479b92 100644 --- a/src/utility/colormatcher.h +++ b/src/utility/colormatcher.h @@ -29,26 +29,43 @@ ** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. **--------------------------------------------------------------------------- ** +** Once upon a time, this tried to be a fast closest color finding system. +** It was, but the results were not as good as I would like, so I didn't +** actually use it. But I did keep the code around in case I ever felt like +** revisiting the problem. I never did, so now it's relegated to the mists +** of SVN history, and this is just a thin wrapper around BestColor(). +** */ #ifndef __COLORMATCHER_H__ #define __COLORMATCHER_H__ +#include + +int BestColor (const uint32_t *pal_in, int r, int g, int b, int first, int num); + class FColorMatcher { public: - FColorMatcher (); - FColorMatcher (const uint32_t *palette); - FColorMatcher (const FColorMatcher &other); + FColorMatcher () = default; + FColorMatcher (const uint32_t *palette) { Pal = reinterpret_cast(palette); } + FColorMatcher (const FColorMatcher &other) = default; - void SetPalette (const uint32_t *palette); - uint8_t Pick (int r, int g, int b); + void SetPalette (const uint32_t *palette) { Pal = reinterpret_cast(palette); } + uint8_t Pick (int r, int g, int b) + { + if (Pal == nullptr) + return 1; + + return (uint8_t)BestColor ((uint32_t *)Pal, r, g, b, 1, 255); + } + uint8_t Pick (PalEntry pe) { return Pick(pe.r, pe.g, pe.b); } - FColorMatcher &operator= (const FColorMatcher &other); + FColorMatcher &operator= (const FColorMatcher &other) = default; private: const PalEntry *Pal; diff --git a/src/utility/palentry.h b/src/utility/palentry.h new file mode 100644 index 0000000000..8f4bfa6d8b --- /dev/null +++ b/src/utility/palentry.h @@ -0,0 +1,82 @@ +#pragma once + +#include + +struct PalEntry +{ + PalEntry() = default; + PalEntry (uint32_t argb) { d = argb; } + operator uint32_t () const { return d; } + void SetRGB(PalEntry other) + { + d = other.d & 0xffffff; + } + PalEntry Modulate(PalEntry other) const + { + if (isWhite()) + { + return other; + } + else if (other.isWhite()) + { + return *this; + } + else + { + other.r = (r * other.r) / 255; + other.g = (g * other.g) / 255; + other.b = (b * other.b) / 255; + return other; + } + } + int Luminance() const + { + return (r * 77 + g * 143 + b * 37) >> 8; + } + + void Decolorize() // this for 'nocoloredspritelighting' and not the same as desaturation. The normal formula results in a value that's too dark. + { + int v = (r + g + b); + r = g = b = ((255*3) + v + v) / 9; + } + bool isBlack() const + { + return (d & 0xffffff) == 0; + } + bool isWhite() const + { + return (d & 0xffffff) == 0xffffff; + } + PalEntry &operator= (const PalEntry &other) = default; + PalEntry &operator= (uint32_t other) { d = other; return *this; } + PalEntry InverseColor() const { PalEntry nc; nc.a = a; nc.r = 255 - r; nc.g = 255 - g; nc.b = 255 - b; return nc; } +#ifdef __BIG_ENDIAN__ + PalEntry (uint8_t ir, uint8_t ig, uint8_t ib) : a(0), r(ir), g(ig), b(ib) {} + PalEntry (uint8_t ia, uint8_t ir, uint8_t ig, uint8_t ib) : a(ia), r(ir), g(ig), b(ib) {} + union + { + struct + { + uint8_t a,r,g,b; + }; + uint32_t d; + }; +#else + PalEntry (uint8_t ir, uint8_t ig, uint8_t ib) : b(ib), g(ig), r(ir), a(0) {} + PalEntry (uint8_t ia, uint8_t ir, uint8_t ig, uint8_t ib) : b(ib), g(ig), r(ir), a(ia) {} + union + { + struct + { + uint8_t b,g,r,a; + }; + uint32_t d; + }; +#endif +}; + +inline int Luminance(int r, int g, int b) +{ + return (r * 77 + g * 143 + b * 37) >> 8; +} +