LCOV - code coverage report
Current view: top level - source/graphics - Color.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 55 62 88.7 %
Date: 2026-12-03 01:19:39 Functions: 4 5 80.0 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "Color.h"
      21             : 
      22             : #include "graphics/SColor.h"
      23             : #include "lib/sysdep/compiler.h"
      24             : #include "maths/MathUtil.h"
      25             : #include "ps/CLogger.h"
      26             : #include "ps/CStr.h"
      27             : 
      28             : #include <SDL_cpuinfo.h>
      29             : #include <sstream>
      30             : 
      31             : #if COMPILER_HAS_SSE
      32             : #include <xmmintrin.h>
      33             : #endif
      34             : 
      35           0 : static SColor4ub ConvertRGBColorTo4ubFallback(const RGBColor& src)
      36             : {
      37           0 :     SColor4ub result;
      38           0 :     result.R = Clamp(static_cast<int>(src.X * 255), 0, 255);
      39           0 :     result.G = Clamp(static_cast<int>(src.Y * 255), 0, 255);
      40           0 :     result.B = Clamp(static_cast<int>(src.Z * 255), 0, 255);
      41           0 :     result.A = 255;
      42           0 :     return result;
      43             : }
      44             : 
      45             : // on IA32, this is replaced by an SSE assembly version in ia32.cpp
      46             : SColor4ub (*ConvertRGBColorTo4ub)(const RGBColor& src) = ConvertRGBColorTo4ubFallback;
      47             : 
      48             : 
      49             : // Assembler-optimized function for color conversion
      50             : #if COMPILER_HAS_SSE
      51           5 : static SColor4ub ConvertRGBColorTo4ubSSE(const RGBColor& src)
      52             : {
      53           5 :     const __m128 zero = _mm_setzero_ps();
      54           5 :     const __m128 _255 = _mm_set_ss(255.0f);
      55           5 :     __m128 r = _mm_load_ss(&src.X);
      56           5 :     __m128 g = _mm_load_ss(&src.Y);
      57           5 :     __m128 b = _mm_load_ss(&src.Z);
      58             : 
      59             :     // C = min(255, 255*max(C, 0)) ( == Clamp(255*C, 0, 255) )
      60           5 :     r = _mm_max_ss(r, zero);
      61           5 :     g = _mm_max_ss(g, zero);
      62           5 :     b = _mm_max_ss(b, zero);
      63             : 
      64           5 :     r = _mm_mul_ss(r, _255);
      65           5 :     g = _mm_mul_ss(g, _255);
      66           5 :     b = _mm_mul_ss(b, _255);
      67             : 
      68           5 :     r = _mm_min_ss(r, _255);
      69           5 :     g = _mm_min_ss(g, _255);
      70           5 :     b = _mm_min_ss(b, _255);
      71             : 
      72             :     // convert to integer and combine channels using bit logic
      73           5 :     int ri = _mm_cvtss_si32(r);
      74           5 :     int gi = _mm_cvtss_si32(g);
      75           5 :     int bi = _mm_cvtss_si32(b);
      76             : 
      77           5 :     return SColor4ub(ri, gi, bi, 0xFF);
      78             : }
      79             : #endif
      80             : 
      81          13 : void ColorActivateFastImpl()
      82             : {
      83             : #if COMPILER_HAS_SSE
      84          13 :     if (SDL_HasSSE())
      85             :     {
      86          13 :         ConvertRGBColorTo4ub = ConvertRGBColorTo4ubSSE;
      87          13 :         return;
      88             :     }
      89             : #elif defined(ARCH_X86_64)
      90             :     debug_printf("No SSE available. Slow fallback routines will be used.\n");
      91             : #endif
      92             : }
      93             : 
      94             : /**
      95             :  * Important: This function does not modify the value if parsing fails.
      96             :  */
      97          20 : bool CColor::ParseString(const CStr8& value, int defaultAlpha)
      98             : {
      99          20 :     const size_t NUM_VALS = 4;
     100          20 :     int values[NUM_VALS] = { 0, 0, 0, defaultAlpha };
     101          20 :     std::stringstream stream;
     102          20 :     stream.str(value);
     103             :     // Parse each value
     104             :     size_t i;
     105          58 :     for (i = 0; i < NUM_VALS; ++i)
     106             :     {
     107          52 :         if (stream.eof())
     108             :             break;
     109             : 
     110          48 :         stream >> values[i];
     111          48 :         if ((stream.rdstate() & std::stringstream::failbit) != 0)
     112             :         {
     113          12 :             LOGWARNING("Unable to parse CColor parameters. Your input: '{}'", value);
     114           6 :             return false;
     115             :         }
     116          42 :         if (values[i] < 0 || values[i] > 255)
     117             :         {
     118           8 :             LOGWARNING("Invalid value (<0 or >255) when parsing CColor parameters. Your input: '{}'",
     119             :                 value);
     120           4 :             return false;
     121             :         }
     122             :     }
     123             : 
     124          10 :     if (i < 3)
     125             :     {
     126           4 :         LOGWARNING("Not enough parameters when parsing as CColor. Your input: '{}'", value);
     127           2 :         return false;
     128             :     }
     129           8 :     if (!stream.eof())
     130             :     {
     131           4 :         LOGWARNING("Too many parameters when parsing as CColor. Your input: '{}'", value);
     132           2 :         return false;
     133             :     }
     134             : 
     135           6 :     r = values[0] / 255.f;
     136           6 :     g = values[1] / 255.f;
     137           6 :     b = values[2] / 255.f;
     138           6 :     a = values[3] / 255.f;
     139             : 
     140           6 :     return true;
     141          20 : }
     142             : 
     143           6 : bool CColor::operator==(const CColor& color) const
     144             : {
     145           6 :     return
     146          12 :         r == color.r &&
     147           6 :         g == color.g &&
     148          12 :         b == color.b &&
     149           6 :         a == color.a;
     150             : }

Generated by: LCOV version 1.16