LCOV - code coverage report
Current view: top level - frmts/miramon - miramon_palettes.h (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 34 37 91.9 %
Date: 2025-11-22 03:30:40 Functions: 15 16 93.8 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  MiraMon Raster Driver
       4             :  * Purpose:  Implements MMRPalettes class: handles access to a DBF file
       5             :  *           containing color information, which is then converted into
       6             :  *           either a color table or an attribute table, depending on the
       7             :  *           context.
       8             :  * Author:   Abel Pau
       9             :  *
      10             :  ******************************************************************************
      11             :  * Copyright (c) 2025, Xavier Pons
      12             :  *
      13             :  * SPDX-License-Identifier: MIT
      14             :  ****************************************************************************/
      15             : 
      16             : #ifndef MMRPALETTES_H_INCLUDED
      17             : #define MMRPALETTES_H_INCLUDED
      18             : 
      19             : #include <array>
      20             : 
      21             : #include "../miramon_common/mm_gdal_constants.h"  // For MM_EXT_DBF_N_FIELDS
      22             : 
      23             : class MMRRel;
      24             : 
      25             : enum class ColorTreatment
      26             : {
      27             :     DEFAULT_SCALING = 0,
      28             :     DIRECT_ASSIGNATION = 1,
      29             :     ORIGIN_DISPLACEMENT = 2,
      30             :     LINEAR_SCALING = 3,
      31             :     LOG_10_SCALING = 4,
      32             :     USER_INTERVALS = 5
      33             : };
      34             : 
      35             : /* ==================================================================== */
      36             : /*                            MMRPalettes                             */
      37             : /* ==================================================================== */
      38             : 
      39             : class MMRPalettes
      40             : {
      41             :   public:
      42             :     MMRPalettes(MMRRel &fRel, const CPLString &osBandSectionIn);
      43             :     MMRPalettes(const MMRPalettes &) =
      44             :         delete;  // I don't want to construct a MMRPalettes from another MMRBand (effc++)
      45             :     MMRPalettes &operator=(const MMRPalettes &) =
      46             :         delete;  // I don't want to assign a MMRPalettes to another MMRBand (effc++)
      47             :     ~MMRPalettes();
      48             : 
      49         111 :     bool IsValid() const
      50             :     {
      51         111 :         return m_bIsValid;
      52             :     }
      53             : 
      54         186 :     bool IsCategorical() const
      55             :     {
      56         186 :         return m_bIsCategorical;
      57             :     }
      58             : 
      59          64 :     void SetIsCategorical(bool bIsCategoricalIn)
      60             :     {
      61          64 :         m_bIsCategorical = bIsCategoricalIn;
      62          64 :     }
      63             : 
      64             :     bool IsAutomatic() const
      65             :     {
      66             :         return m_bIsAutomatic;
      67             :     }
      68             : 
      69         100 :     ColorTreatment GetColorScaling() const
      70             :     {
      71         100 :         return m_ColorScaling;
      72             :     }
      73             : 
      74           0 :     void SetColorScaling(enum ColorTreatment colorScaling)
      75             :     {
      76           0 :         m_ColorScaling = colorScaling;
      77           0 :     }
      78             : 
      79          53 :     bool IsConstantColor() const
      80             :     {
      81          53 :         return m_bIsConstantColor;
      82             :     }
      83             : 
      84     1050170 :     GDALColorEntry GetDefaultColorRGB() const
      85             :     {
      86     1050170 :         return m_sDefaultColorRGB;
      87             :     }
      88             : 
      89         768 :     GDALColorEntry GetConstantColorRGB() const
      90             :     {
      91         768 :         return m_sConstantColorRGB;
      92             :     }
      93             : 
      94             :     void SetConstantColorRGB(GDALColorEntry sConstantColorRGBIn)
      95             :     {
      96             :         m_sConstantColorRGB = sConstantColorRGBIn;
      97             :     }
      98             : 
      99           2 :     void SetConstantColorRGB(short c1, short c2, short c3)
     100             :     {
     101           2 :         m_sConstantColorRGB.c1 = c1;
     102           2 :         m_sConstantColorRGB.c2 = c2;
     103           2 :         m_sConstantColorRGB.c3 = c3;
     104           2 :     }
     105             : 
     106             :     void SetConstantColorRGB(short c1, short c2, short c3, short c4)
     107             :     {
     108             :         m_sConstantColorRGB.c1 = c1;
     109             :         m_sConstantColorRGB.c2 = c2;
     110             :         m_sConstantColorRGB.c3 = c3;
     111             :         m_sConstantColorRGB.c4 = c4;
     112             :     }
     113             : 
     114         536 :     bool HasNodata() const
     115             :     {
     116         536 :         return m_bHasNodata;
     117             :     }
     118             : 
     119             :     void SetHasNodata(bool bHasNodataIn)
     120             :     {
     121             :         m_bHasNodata = bHasNodataIn;
     122             :     }
     123             : 
     124          23 :     int GetNoDataPaletteIndex() const
     125             :     {
     126          23 :         return m_nNoDataPaletteIndex;
     127             :     }
     128             : 
     129             :     void SetNoDataPaletteIndex(bool nNoDataPaletteIndexIn)
     130             :     {
     131             :         m_nNoDataPaletteIndex = nNoDataPaletteIndexIn;
     132             :     }
     133             : 
     134           3 :     GDALColorEntry GetNoDataDefaultColor() const
     135             :     {
     136           3 :         return m_sNoDataColorRGB;
     137             :     }
     138             : 
     139      299044 :     double GetPaletteColorsValue(int nIComponent, int nIColor) const
     140             :     {
     141      299044 :         return m_aadfPaletteColors[nIComponent][nIColor];
     142             :     }
     143             : 
     144          83 :     int GetSizeOfPaletteColors() const
     145             :     {
     146          83 :         return static_cast<int>(m_aadfPaletteColors[0].size());
     147             :     }
     148             : 
     149       66089 :     int GetNumberOfColors() const
     150             :     {
     151       66089 :         return m_nNPaletteColors;
     152             :     }
     153             : 
     154             :     // Real means with no nodata.
     155           7 :     int GetNumberOfColorsIncludingNodata() const
     156             :     {
     157           7 :         return m_nRealNPaletteColors;
     158             :     }
     159             : 
     160             :     CPLErr UpdateColorInfo();
     161             : 
     162             :   private:
     163             :     static CPLErr GetPaletteColors_DBF_Indexes(
     164             :         struct MM_DATA_BASE_XP &oColorTable, MM_EXT_DBF_N_FIELDS &nClauSimbol,
     165             :         MM_EXT_DBF_N_FIELDS &nRIndex, MM_EXT_DBF_N_FIELDS &nGIndex,
     166             :         MM_EXT_DBF_N_FIELDS &nBIndex);
     167             :     CPLErr GetPaletteColors_Automatic();
     168             :     CPLErr GetPaletteColors_DBF(const CPLString &os_Color_Paleta_DBF);
     169             :     CPLErr GetPaletteColors_PAL_P25_P65(const CPLString &os_Color_Paleta_DBF);
     170             :     void AssignColorFromDBF(struct MM_DATA_BASE_XP &oColorTable,
     171             :                             char *pzsRecord, char *pszField,
     172             :                             MM_EXT_DBF_N_FIELDS &nRIndex,
     173             :                             MM_EXT_DBF_N_FIELDS &nGIndex,
     174             :                             MM_EXT_DBF_N_FIELDS &nBIndex, int nIPaletteIndex);
     175             :     CPLErr UpdateConstantColor();
     176             : 
     177             :     std::array<std::vector<double>, 4> m_aadfPaletteColors{};
     178             :     bool m_bIsCategorical = false;
     179             : 
     180             :     // Palette info
     181             :     GDALColorEntry m_sDefaultColorRGB = {0, 0, 0, 127};
     182             : 
     183             :     bool m_bHasNodata = false;
     184             :     // index in the DBF that gives nodata color
     185             :     int m_nNoDataPaletteIndex = 0;
     186             :     // Default color for nodata
     187             :     GDALColorEntry m_sNoDataColorRGB = {0, 0, 0, 0};
     188             : 
     189             :     bool m_bIsAutomatic = false;
     190             :     ColorTreatment m_ColorScaling = ColorTreatment::DEFAULT_SCALING;
     191             : 
     192             :     bool m_bIsConstantColor = false;
     193             :     GDALColorEntry m_sConstantColorRGB = {0, 0, 0, 0};
     194             : 
     195             :     int m_nNPaletteColors = 0;
     196             :     int m_nRealNPaletteColors = 0;  // Without nodata
     197             : 
     198             :     MMRRel *m_pfRel = nullptr;  // Rel where metadata is read from
     199             :     CPLString m_osBandSection;
     200             : 
     201             :     bool m_bIsValid =
     202             :         false;  // Determines if the created object is valid or not.
     203             : 
     204             :     // To be used in a categorical raster with Automatic palette associated
     205             :     const std::vector<GDALColorEntry> m_ThematicPalette = {
     206             :         {0, 0, 255, 255},     {0, 255, 255, 255},   {0, 255, 0, 255},
     207             :         {255, 255, 0, 255},   {255, 0, 0, 255},     {255, 0, 255, 255},
     208             :         {191, 191, 191, 255}, {0, 128, 255, 255},   {128, 0, 255, 255},
     209             :         {0, 255, 128, 255},   {128, 255, 0, 255},   {255, 128, 0, 255},
     210             :         {255, 0, 128, 255},   {128, 255, 255, 255}, {128, 128, 255, 255},
     211             :         {128, 255, 128, 255}, {255, 128, 255, 255}, {255, 128, 128, 255},
     212             :         {255, 255, 128, 255}, {128, 128, 128, 255}, {0, 0, 128, 255},
     213             :         {0, 128, 128, 255},   {0, 128, 0, 255},     {128, 128, 0, 255},
     214             :         {128, 0, 0, 255},     {128, 0, 128, 255},   {64, 64, 64, 255},
     215             :         {0, 0, 191, 255},     {128, 128, 191, 255}, {0, 191, 191, 255},
     216             :         {0, 191, 0, 255},     {191, 191, 0, 255},   {191, 0, 0, 255},
     217             :         {191, 0, 191, 255},   {0, 128, 191, 255},   {128, 0, 191, 255},
     218             :         {128, 191, 191, 255}, {0, 191, 128, 255},   {128, 191, 0, 255},
     219             :         {128, 191, 128, 255}, {191, 191, 128, 255}, {191, 128, 0, 255},
     220             :         {191, 128, 128, 255}, {191, 0, 128, 255},   {191, 128, 191, 255},
     221             :         {0, 0, 64, 255},      {0, 64, 64, 255},     {0, 64, 0, 255},
     222             :         {64, 64, 0, 255},     {64, 0, 0, 255},      {64, 0, 64, 255},
     223             :         {0, 128, 64, 255},    {128, 0, 64, 255},    {0, 64, 128, 255},
     224             :         {128, 64, 0, 255},    {64, 128, 0, 255},    {64, 0, 128, 255},
     225             :         {128, 64, 64, 255},   {128, 128, 64, 255},  {128, 64, 128, 255},
     226             :         {64, 128, 64, 255},   {64, 128, 128, 255},  {64, 64, 128, 255},
     227             :         {0, 191, 64, 255},    {191, 0, 64, 255},    {0, 64, 191, 255},
     228             :         {191, 64, 0, 255},    {64, 191, 0, 255},    {64, 0, 191, 255},
     229             :         {191, 64, 64, 255},   {191, 191, 64, 255},  {191, 64, 191, 255},
     230             :         {64, 191, 64, 255},   {64, 191, 191, 255},  {64, 64, 191, 255},
     231             :         {15, 177, 228, 255},  {184, 91, 96, 255},   {105, 246, 240, 255},
     232             :         {139, 224, 27, 255},  {113, 111, 125, 255}, {188, 184, 147, 255},
     233             :         {125, 225, 235, 255}, {78, 166, 108, 255},  {185, 87, 250, 255},
     234             :         {171, 224, 154, 255}, {60, 25, 133, 255},   {227, 239, 158, 255},
     235             :         {140, 139, 108, 255}, {101, 195, 115, 255}, {67, 245, 217, 255},
     236             :         {150, 123, 223, 255}, {71, 86, 92, 255},    {206, 18, 20, 255},
     237             :         {255, 99, 85, 255},   {233, 235, 42, 255},  {254, 235, 235, 255},
     238             :         {18, 82, 160, 255},   {43, 82, 250, 255},   {33, 5, 223, 255},
     239             :         {132, 212, 136, 255}, {166, 250, 155, 255}, {95, 116, 2, 255},
     240             :         {249, 5, 22, 255},    {5, 221, 152, 255},   {56, 5, 194, 255},
     241             :         {6, 243, 169, 255},   {29, 149, 23, 255},   {87, 85, 251, 255},
     242             :         {128, 200, 197, 255}, {73, 120, 48, 255},   {211, 29, 1, 255},
     243             :         {97, 13, 26, 255},    {201, 31, 248, 255},  {163, 224, 32, 255},
     244             :         {46, 82, 238, 255},   {212, 53, 216, 255},  {101, 255, 186, 255},
     245             :         {205, 131, 99, 255},  {49, 191, 141, 255},  {23, 115, 53, 255},
     246             :         {11, 97, 56, 255},    {108, 208, 111, 255}, {181, 80, 251, 255},
     247             :         {53, 14, 0, 255},     {205, 190, 17, 255},  {79, 221, 250, 255},
     248             :         {40, 182, 251, 255},  {227, 91, 248, 255},  {119, 235, 88, 255},
     249             :         {93, 224, 88, 255},   {149, 185, 129, 255}, {245, 143, 30, 255},
     250             :         {23, 219, 5, 255},    {211, 59, 65, 255},   {31, 125, 29, 255},
     251             :         {49, 251, 93, 255},   {78, 112, 183, 255},  {142, 195, 201, 255},
     252             :         {206, 74, 49, 255},   {45, 221, 241, 255},  {61, 28, 13, 255},
     253             :         {139, 41, 68, 255},   {178, 130, 74, 255},  {140, 229, 251, 255},
     254             :         {119, 165, 107, 255}, {53, 175, 23, 255},   {100, 38, 228, 255},
     255             :         {111, 88, 65, 255},   {196, 157, 233, 255}, {131, 162, 134, 255},
     256             :         {58, 171, 196, 255},  {115, 116, 93, 255},  {159, 232, 239, 255},
     257             :         {217, 200, 153, 255}, {171, 59, 69, 255},   {73, 206, 236, 255},
     258             :         {11, 171, 170, 255},  {101, 142, 165, 255}, {156, 147, 175, 255},
     259             :         {156, 199, 79, 255},  {212, 47, 90, 255},   {65, 2, 123, 255},
     260             :         {120, 20, 65, 255},   {153, 51, 45, 255},   {248, 171, 167, 255},
     261             :         {59, 143, 51, 255},   {137, 68, 226, 255},  {161, 30, 43, 255},
     262             :         {96, 97, 26, 255},    {155, 184, 199, 255}, {105, 53, 146, 255},
     263             :         {49, 131, 17, 255},   {109, 139, 71, 255},  {139, 39, 226, 255},
     264             :         {230, 90, 151, 255},  {232, 237, 215, 255}, {127, 242, 248, 255},
     265             :         {202, 181, 215, 255}, {52, 220, 166, 255},  {29, 144, 124, 255},
     266             :         {125, 237, 13, 255},  {190, 115, 135, 255}, {192, 57, 127, 255},
     267             :         {57, 33, 111, 255},   {62, 87, 175, 255},   {46, 73, 248, 255},
     268             :         {101, 179, 212, 255}, {186, 243, 111, 255}, {123, 165, 115, 255},
     269             :         {92, 86, 217, 255},   {6, 18, 182, 255},    {4, 204, 57, 255},
     270             :         {152, 11, 205, 255},  {239, 127, 56, 255},  {15, 45, 141, 255},
     271             :         {2, 0, 222, 255},     {101, 253, 206, 255}, {45, 37, 74, 255},
     272             :         {152, 30, 232, 255},  {22, 10, 16, 255},    {229, 249, 42, 255},
     273             :         {80, 69, 96, 255},    {240, 49, 187, 255},  {81, 81, 239, 255},
     274             :         {54, 178, 244, 255},  {100, 159, 34, 255},  {73, 43, 105, 255},
     275             :         {217, 177, 211, 255}, {57, 102, 188, 255},  {132, 48, 72, 255},
     276             :         {34, 19, 46, 255},    {240, 210, 212, 255}, {187, 139, 121, 255},
     277             :         {70, 51, 9, 255},     {123, 149, 2, 255},   {11, 70, 191, 255},
     278             :         {39, 193, 154, 255},  {243, 67, 63, 255},   {212, 126, 180, 255},
     279             :         {153, 246, 241, 255}, {28, 231, 8, 255},    {36, 31, 157, 255},
     280             :         {123, 28, 124, 255},  {182, 87, 95, 255},   {150, 227, 203, 255},
     281             :         {141, 181, 102, 255}, {37, 60, 149, 255},   {241, 106, 178, 255},
     282             :         {110, 7, 30, 255},    {124, 194, 194, 255}, {194, 243, 216, 255},
     283             :         {248, 36, 43, 255},   {10, 134, 25, 255},   {30, 106, 213, 255},
     284             :         {80, 213, 173, 255},  {6, 128, 32, 255},    {117, 148, 210, 255},
     285             :         {19, 181, 45, 255},   {126, 217, 120, 255}, {105, 49, 187, 255},
     286             :         {158, 62, 93, 255},   {248, 36, 30, 255},   {23, 188, 200, 255},
     287             :         {251, 123, 1, 255},   {60, 169, 20, 255},   {91, 186, 69, 255},
     288             :         {95, 33, 90, 255},    {245, 203, 159, 255}, {153, 152, 163, 255},
     289             :         {247, 103, 177, 255}, {229, 43, 38, 255},   {210, 183, 17, 255},
     290             :         {197, 29, 172, 255},  {53, 248, 147, 255},  {195, 29, 185, 255},
     291             :         {38, 142, 215, 255}};
     292             : };
     293             : 
     294             : #endif  // MMRPALETTES_H_INCLUDED

Generated by: LCOV version 1.14