LCOV - code coverage report
Current view: top level - frmts/miramon - miramon_palettes.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 205 270 75.9 %
Date: 2026-03-05 10:33:42 Functions: 8 8 100.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  MiraMonRaster 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             : #include "miramon_rel.h"
      17             : #include "miramon_palettes.h"
      18             : 
      19             : #include "../miramon_common/mm_gdal_functions.h"  // For MMCheck_REL_FILE()
      20             : 
      21         119 : MMRPalettes::MMRPalettes(MMRRel &fRel, int nIBand) : m_pfRel(&fRel)
      22             : {
      23             :     // Is the palette a constant color? Then, which color is it?
      24         119 :     MMRBand *poBand = m_pfRel->GetBand(nIBand - 1);
      25         119 :     if (poBand == nullptr)
      26          75 :         return;
      27             : 
      28         119 :     m_osBandSection = poBand->GetBandSection();
      29             : 
      30         119 :     if (EQUAL(poBand->GetColor_Const(), "1"))
      31             :     {
      32           3 :         m_bIsConstantColor = true;
      33           3 :         if (!poBand->ValidConstantColorRGB())
      34           0 :             return;  // The constant color indicated is wrong
      35             : 
      36           3 :         SetConstantColorRGB(poBand->GetConstantColorRGB());
      37           3 :         m_nRealNPaletteColors = 1;
      38           3 :         m_bIsValid = true;
      39           3 :         m_ColorScaling = ColorTreatment::DIRECT_ASSIGNATION;
      40           3 :         SetIsCategorical(true);
      41           3 :         return;
      42             :     }
      43             : 
      44             :     // Is this an authomatic palette or has a color table (dbf, pal,...)?
      45         167 :     if (EQUAL(poBand->GetColor_Paleta(), "<Automatic>") ||
      46          51 :         poBand->GetColor_Paleta().empty())
      47          69 :         m_bIsAutomatic = true;
      48             : 
      49             :     // Treatment of the color variable
      50         116 :     if (poBand->GetColor_TractamentVariable().empty())
      51             :     {
      52          32 :         if (poBand->IsCategorical())
      53          26 :             SetIsCategorical(true);
      54             :         else
      55           6 :             SetIsCategorical(false);
      56             :     }
      57             :     else
      58             :     {
      59          84 :         if (EQUAL(poBand->GetColor_TractamentVariable(), "Categoric"))
      60          51 :             SetIsCategorical(true);
      61             :         else
      62          33 :             SetIsCategorical(false);
      63             :     }
      64             : 
      65         116 :     if (UpdateColorInfo() == CE_Failure)
      66           0 :         return;
      67             : 
      68         116 :     if (m_bIsAutomatic)
      69             :     {
      70             :         // How many "colors" are involved?
      71          69 :         if (!poBand->GetColor_N_SimbolsALaTaula().empty())
      72             :         {
      73             :             GIntBig nBigVal =
      74           0 :                 CPLAtoGIntBig(poBand->GetColor_N_SimbolsALaTaula());
      75           0 :             if (nBigVal >= INT_MAX)
      76           0 :                 return;
      77           0 :             m_nRealNPaletteColors = m_nNPaletteColors =
      78             :                 static_cast<int>(nBigVal);
      79           0 :             if (m_nNPaletteColors <= 0 || m_nNPaletteColors >= 256)
      80             :             {
      81           0 :                 CPLError(CE_Failure, CPLE_AssertionFailed,
      82             :                          "Invalid number of colors "
      83             :                          "(Color_N_SimbolsALaTaula) in \"%s\".",
      84           0 :                          m_pfRel->GetRELName().c_str());
      85           0 :                 return;
      86             :             }
      87             :         }
      88             :         else
      89             :         {
      90          69 :             if (IsCategorical())
      91             :             {
      92             :                 // Predefined color table: m_ThematicPalette
      93          45 :                 if (CE_None != GetPaletteColors_Automatic())
      94           0 :                     return;
      95             :             }
      96             :             else  // No palette associated
      97          24 :                 return;
      98             :         }
      99          45 :         m_bIsValid = true;
     100          45 :         return;
     101             :     }
     102             : 
     103             :     // If color is no automatic, from where we got this?
     104          47 :     CPLString osExtension = CPLGetExtensionSafe(poBand->GetColor_Paleta());
     105          47 :     if (osExtension.tolower() == "dbf")
     106             :     {
     107          44 :         if (CE_None != GetPaletteColors_DBF(poBand->GetColor_Paleta()))
     108           3 :             return;
     109             : 
     110          41 :         m_bIsValid = true;
     111             :     }
     112           4 :     else if (osExtension.tolower() == "pal" || osExtension.tolower() == "p25" ||
     113           1 :              osExtension.tolower() == "p65")
     114             :     {
     115           3 :         if (CE_None != GetPaletteColors_PAL_P25_P65(poBand->GetColor_Paleta()))
     116           0 :             return;
     117             : 
     118           3 :         m_bIsValid = true;
     119             :     }
     120             :     else
     121           0 :         return;
     122             : 
     123          44 :     m_nRealNPaletteColors = m_nNPaletteColors;
     124          44 :     if (HasNodata())
     125             :     {
     126          11 :         if (m_nNPaletteColors < 1)
     127           0 :             return;
     128          11 :         m_nNPaletteColors--;
     129             :     }
     130             :     else
     131             :     {
     132             :         // If palette doesn't have nodata let's set some index
     133          33 :         m_nNoDataPaletteIndex = m_nRealNPaletteColors;
     134             :     }
     135             : }
     136             : 
     137         119 : MMRPalettes::~MMRPalettes()
     138             : {
     139         119 : }
     140             : 
     141        1335 : void MMRPalettes::AssignColorFromDBF(struct MM_DATA_BASE_XP &oColorTable,
     142             :                                      char *pzsRecord, char *pszField,
     143             :                                      MM_EXT_DBF_N_FIELDS &nRIndex,
     144             :                                      MM_EXT_DBF_N_FIELDS &nGIndex,
     145             :                                      MM_EXT_DBF_N_FIELDS &nBIndex,
     146             :                                      int nIPaletteIndex)
     147             : {
     148             :     // RED
     149        1335 :     memcpy(pszField, pzsRecord + oColorTable.pField[nRIndex].AccumulatedBytes,
     150        1335 :            oColorTable.pField[nRIndex].BytesPerField);
     151        1335 :     pszField[oColorTable.pField[nRIndex].BytesPerField] = '\0';
     152        1335 :     m_aadfPaletteColors[0][nIPaletteIndex] = CPLAtof(pszField);
     153             : 
     154             :     // GREEN
     155        1335 :     memcpy(pszField, pzsRecord + oColorTable.pField[nGIndex].AccumulatedBytes,
     156        1335 :            oColorTable.pField[nGIndex].BytesPerField);
     157        1335 :     pszField[oColorTable.pField[nGIndex].BytesPerField] = '\0';
     158        1335 :     m_aadfPaletteColors[1][nIPaletteIndex] = CPLAtof(pszField);
     159             : 
     160             :     // BLUE
     161        1335 :     memcpy(pszField, pzsRecord + oColorTable.pField[nBIndex].AccumulatedBytes,
     162        1335 :            oColorTable.pField[nBIndex].BytesPerField);
     163        1335 :     pszField[oColorTable.pField[nBIndex].BytesPerField] = '\0';
     164        1335 :     m_aadfPaletteColors[2][nIPaletteIndex] = CPLAtof(pszField);
     165             : 
     166             :     // ALPHA
     167        1335 :     if (m_aadfPaletteColors[0][nIPaletteIndex] == -1 &&
     168        1359 :         m_aadfPaletteColors[1][nIPaletteIndex] == -1 &&
     169          24 :         m_aadfPaletteColors[2][nIPaletteIndex] == -1)
     170             :     {
     171             :         // Transparent (white or whatever color)
     172          24 :         m_aadfPaletteColors[0][nIPaletteIndex] = m_sNoDataColorRGB.c1;
     173          24 :         m_aadfPaletteColors[1][nIPaletteIndex] = m_sNoDataColorRGB.c2;
     174          24 :         m_aadfPaletteColors[2][nIPaletteIndex] = m_sNoDataColorRGB.c3;
     175          24 :         m_aadfPaletteColors[3][nIPaletteIndex] = m_sNoDataColorRGB.c4;
     176             :     }
     177             :     else
     178        1311 :         m_aadfPaletteColors[3][nIPaletteIndex] = 255;
     179        1335 : }
     180             : 
     181          43 : CPLErr MMRPalettes::GetPaletteColors_DBF_Indexes(
     182             :     struct MM_DATA_BASE_XP &oColorTable, MM_EXT_DBF_N_FIELDS &nClauSimbol,
     183             :     MM_EXT_DBF_N_FIELDS &nRIndex, MM_EXT_DBF_N_FIELDS &nGIndex,
     184             :     MM_EXT_DBF_N_FIELDS &nBIndex)
     185             : {
     186          43 :     nClauSimbol = oColorTable.nFields;
     187          43 :     nRIndex = oColorTable.nFields;
     188          43 :     nGIndex = oColorTable.nFields;
     189          43 :     nBIndex = oColorTable.nFields;
     190             : 
     191         221 :     for (MM_EXT_DBF_N_FIELDS nIField = 0; nIField < oColorTable.nFields;
     192             :          nIField++)
     193             :     {
     194         178 :         if (EQUAL(oColorTable.pField[nIField].FieldName, "CLAUSIMBOL"))
     195          42 :             nClauSimbol = nIField;
     196         136 :         else if (EQUAL(oColorTable.pField[nIField].FieldName, "R_COLOR"))
     197          43 :             nRIndex = nIField;
     198          93 :         else if (EQUAL(oColorTable.pField[nIField].FieldName, "G_COLOR"))
     199          43 :             nGIndex = nIField;
     200          50 :         else if (EQUAL(oColorTable.pField[nIField].FieldName, "B_COLOR"))
     201          43 :             nBIndex = nIField;
     202             :     }
     203             : 
     204          43 :     if (nClauSimbol == oColorTable.nFields || nRIndex == oColorTable.nFields ||
     205          42 :         nGIndex == oColorTable.nFields || nBIndex == oColorTable.nFields)
     206           1 :         return CE_Failure;
     207             : 
     208          42 :     return CE_None;
     209             : }
     210             : 
     211             : // Colors in a PAL, P25 or P65 format files
     212             : // Updates nNPaletteColors
     213          45 : CPLErr MMRPalettes::GetPaletteColors_Automatic()
     214             : {
     215          45 :     m_nRealNPaletteColors = m_nNPaletteColors =
     216          45 :         static_cast<int>(m_ThematicPalette.size());
     217             : 
     218         225 :     for (int iColumn = 0; iColumn < 4; iColumn++)
     219             :     {
     220             :         try
     221             :         {
     222         180 :             m_aadfPaletteColors[iColumn].resize(m_nNPaletteColors, 0);
     223             :         }
     224           0 :         catch (std::bad_alloc &e)
     225             :         {
     226           0 :             CPLError(CE_Failure, CPLE_AppDefined, "%s", e.what());
     227           0 :             return CE_Failure;
     228             :         }
     229             :     }
     230             : 
     231       11565 :     for (int nIndex = 0; nIndex < m_nRealNPaletteColors; nIndex++)
     232             :     {
     233             :         // Index of the color
     234             : 
     235             :         // RED
     236       11520 :         m_aadfPaletteColors[0][nIndex] = m_ThematicPalette[nIndex].c1;
     237             : 
     238             :         // GREEN
     239       11520 :         m_aadfPaletteColors[1][nIndex] = m_ThematicPalette[nIndex].c2;
     240             : 
     241             :         // BLUE
     242       11520 :         m_aadfPaletteColors[2][nIndex] = m_ThematicPalette[nIndex].c3;
     243             : 
     244             :         // ALPHA
     245       11520 :         m_aadfPaletteColors[3][nIndex] = m_ThematicPalette[nIndex].c4;
     246             :     }
     247             : 
     248          45 :     return CE_None;
     249             : }
     250             : 
     251             : // Updates nNPaletteColors
     252          44 : CPLErr MMRPalettes::GetPaletteColors_DBF(const CPLString &os_Color_Paleta_DBF)
     253             : 
     254             : {
     255             :     // Getting the full path name of the DBF
     256          88 :     CPLString osAux = CPLGetPathSafe(m_pfRel->GetRELNameChar());
     257             :     CPLString osColorTableFileName =
     258          88 :         CPLFormFilenameSafe(osAux.c_str(), os_Color_Paleta_DBF.c_str(), "");
     259             : 
     260             :     // Reading the DBF file
     261             :     struct MM_DATA_BASE_XP oColorTable;
     262          44 :     memset(&oColorTable, 0, sizeof(oColorTable));
     263             : 
     264          44 :     if (MM_ReadExtendedDBFHeaderFromFile(osColorTableFileName.c_str(),
     265             :                                          &oColorTable,
     266          88 :                                          m_pfRel->GetRELNameChar()))
     267             :     {
     268           1 :         CPLError(CE_Failure, CPLE_AssertionFailed,
     269             :                  "Invalid color table:"
     270             :                  "\"%s\".",
     271             :                  osColorTableFileName.c_str());
     272             : 
     273           1 :         return CE_Failure;
     274             :     }
     275             : 
     276             :     // Getting indices of fields that determine the colors.
     277             :     MM_EXT_DBF_N_FIELDS nClauSimbol, nRIndex, nGIndex, nBIndex;
     278          43 :     if (CE_Failure == GetPaletteColors_DBF_Indexes(oColorTable, nClauSimbol,
     279             :                                                    nRIndex, nGIndex, nBIndex))
     280             :     {
     281           1 :         CPLError(CE_Failure, CPLE_AssertionFailed,
     282             :                  "Invalid color table:"
     283             :                  "\"%s\".",
     284             :                  osColorTableFileName.c_str());
     285             : 
     286           1 :         VSIFCloseL(oColorTable.pfDataBase);
     287           1 :         MM_ReleaseMainFields(&oColorTable);
     288           1 :         return CE_Failure;
     289             :     }
     290             : 
     291             :     // Checking the structure to be correct
     292          42 :     if (oColorTable.pField[nClauSimbol].BytesPerField == 0 ||
     293          42 :         oColorTable.pField[nRIndex].BytesPerField == 0 ||
     294          42 :         oColorTable.pField[nGIndex].BytesPerField == 0 ||
     295          42 :         oColorTable.pField[nBIndex].BytesPerField == 0 ||
     296          42 :         oColorTable.pField[nClauSimbol].FieldType != 'N' ||
     297          41 :         oColorTable.pField[nRIndex].FieldType != 'N' ||
     298          41 :         oColorTable.pField[nGIndex].FieldType != 'N' ||
     299          41 :         oColorTable.pField[nBIndex].FieldType != 'N')
     300             :     {
     301           1 :         CPLError(CE_Failure, CPLE_AssertionFailed,
     302             :                  "Invalid color table:"
     303             :                  "\"%s\".",
     304             :                  osColorTableFileName.c_str());
     305             : 
     306           1 :         VSIFCloseL(oColorTable.pfDataBase);
     307           1 :         MM_ReleaseMainFields(&oColorTable);
     308           1 :         return CE_Failure;
     309             :     }
     310             : 
     311             :     // Guessing or reading the number of colors of the palette.
     312          41 :     MM_ACCUMULATED_BYTES_TYPE_DBF nBufferSize = oColorTable.BytesPerRecord + 1;
     313          41 :     char *pzsRecord = static_cast<char *>(VSI_CALLOC_VERBOSE(1, nBufferSize));
     314          41 :     if (!pzsRecord)
     315             :     {
     316           0 :         CPLError(CE_Failure, CPLE_OutOfMemory,
     317             :                  "Out of memory allocating working buffer");
     318           0 :         VSIFCloseL(oColorTable.pfDataBase);
     319           0 :         MM_ReleaseMainFields(&oColorTable);
     320           0 :         return CE_Failure;
     321             :     }
     322          41 :     char *pszField = static_cast<char *>(VSI_CALLOC_VERBOSE(1, nBufferSize));
     323             : 
     324          41 :     if (!pszField)
     325             :     {
     326           0 :         CPLError(CE_Failure, CPLE_OutOfMemory,
     327             :                  "Out of memory allocating working buffer");
     328           0 :         VSIFree(pzsRecord);
     329           0 :         VSIFCloseL(oColorTable.pfDataBase);
     330           0 :         MM_ReleaseMainFields(&oColorTable);
     331           0 :         return CE_Failure;
     332             :     }
     333             : 
     334          41 :     m_nNPaletteColors = static_cast<int>(oColorTable.nRecords);  // Safe cast
     335             : 
     336             :     // Checking the size of the palette.
     337          41 :     if (m_nNPaletteColors < 0 || m_nNPaletteColors > 65536)
     338             :     {
     339           0 :         m_nNPaletteColors = 0;
     340           0 :         CPLError(CE_Failure, CPLE_AppDefined,
     341             :                  "Invalid number of colors: %d "
     342             :                  "in color table \"%s\".",
     343             :                  m_nNPaletteColors, osColorTableFileName.c_str());
     344             : 
     345           0 :         VSIFree(pszField);
     346           0 :         VSIFree(pzsRecord);
     347           0 :         VSIFCloseL(oColorTable.pfDataBase);
     348           0 :         MM_ReleaseMainFields(&oColorTable);
     349           0 :         return CE_Failure;
     350             :     }
     351             : 
     352             :     // Getting the memory to allocate the color values
     353         205 :     for (int iColumn = 0; iColumn < 4; iColumn++)
     354             :     {
     355             :         try
     356             :         {
     357         164 :             m_aadfPaletteColors[iColumn].resize(m_nNPaletteColors, 0);
     358             :         }
     359           0 :         catch (std::bad_alloc &e)
     360             :         {
     361           0 :             CPLError(CE_Failure, CPLE_AppDefined, "%s", e.what());
     362           0 :             VSIFree(pszField);
     363           0 :             VSIFree(pzsRecord);
     364           0 :             VSIFCloseL(oColorTable.pfDataBase);
     365           0 :             MM_ReleaseMainFields(&oColorTable);
     366           0 :             return CE_Failure;
     367             :         }
     368             :     }
     369             : 
     370          41 :     VSIFSeekL(oColorTable.pfDataBase,
     371          41 :               static_cast<vsi_l_offset>(oColorTable.FirstRecordOffset),
     372             :               SEEK_SET);
     373             :     /*
     374             :         Each record's CLAUSIMBOL field doesn't match a pixel value present in the raster,
     375             :         and it's used only for discovering nodata value (blanc value).
     376             :         The list of values is used to map every value in a color using:
     377             :             - Direct assignation: mode used in categorical modes but possible in continuous.
     378             :             - Linear scaling
     379             :             - Logarithmic scaling
     380             :         */
     381        1376 :     for (int nIPaletteColors = 0; nIPaletteColors < m_nNPaletteColors;
     382             :          nIPaletteColors++)
     383             :     {
     384        2670 :         if (oColorTable.BytesPerRecord !=
     385        1335 :             VSIFReadL(pzsRecord, sizeof(unsigned char),
     386        1335 :                       oColorTable.BytesPerRecord, oColorTable.pfDataBase))
     387             :         {
     388           0 :             CPLError(CE_Failure, CPLE_AppDefined, "Invalid color table: \"%s\"",
     389             :                      osColorTableFileName.c_str());
     390           0 :             VSIFree(pszField);
     391           0 :             VSIFree(pzsRecord);
     392           0 :             VSIFCloseL(oColorTable.pfDataBase);
     393           0 :             MM_ReleaseMainFields(&oColorTable);
     394           0 :             return CE_Failure;
     395             :         }
     396             : 
     397             :         // Nodata identification
     398        1335 :         memcpy(pszField,
     399        1335 :                pzsRecord + oColorTable.pField[nClauSimbol].AccumulatedBytes,
     400        1335 :                oColorTable.pField[nClauSimbol].BytesPerField);
     401        1335 :         pszField[oColorTable.pField[nClauSimbol].BytesPerField] = '\0';
     402        2670 :         CPLString osField = pszField;
     403        1335 :         osField.replaceAll(" ", "");
     404        1335 :         if (osField.empty())  // Nodata value
     405             :         {
     406          11 :             m_bHasNodata = true;
     407          11 :             m_nNoDataPaletteIndex = nIPaletteColors;
     408             :         }
     409             : 
     410        1335 :         AssignColorFromDBF(oColorTable, pzsRecord, pszField, nRIndex, nGIndex,
     411             :                            nBIndex, nIPaletteColors);
     412             :     }
     413             : 
     414          41 :     VSIFree(pszField);
     415          41 :     VSIFree(pzsRecord);
     416          41 :     VSIFCloseL(oColorTable.pfDataBase);
     417          41 :     MM_ReleaseMainFields(&oColorTable);
     418             : 
     419          41 :     return CE_None;
     420             : }
     421             : 
     422             : // Colors in a PAL, P25 or P65 format files
     423             : // Updates nNPaletteColors
     424             : CPLErr
     425           3 : MMRPalettes::GetPaletteColors_PAL_P25_P65(const CPLString &os_Color_Paleta_DBF)
     426             : 
     427             : {
     428           6 :     CPLString osAux = CPLGetPathSafe(m_pfRel->GetRELNameChar());
     429             :     CPLString osColorTableFileName =
     430           6 :         CPLFormFilenameSafe(osAux.c_str(), os_Color_Paleta_DBF.c_str(), "");
     431             : 
     432             :     // This kind of palette has not NoData color.
     433             :     //bHasNodata = false;
     434             : 
     435           6 :     CPLString osExtension = CPLGetExtensionSafe(os_Color_Paleta_DBF);
     436           3 :     int nNReadPaletteColors = 0;
     437           3 :     m_nNPaletteColors = 0;
     438             : 
     439           3 :     if (osExtension.tolower() == "pal")
     440           1 :         m_nNPaletteColors = 64;
     441           2 :     else if (osExtension.tolower() == "p25")
     442           1 :         m_nNPaletteColors = 256;
     443           1 :     else if (osExtension.tolower() == "p65")
     444           1 :         m_nNPaletteColors = 65536;
     445             :     else
     446           0 :         return CE_None;
     447             : 
     448          15 :     for (int iColumn = 0; iColumn < 4; iColumn++)
     449             :     {
     450             :         try
     451             :         {
     452          12 :             m_aadfPaletteColors[iColumn].resize(m_nNPaletteColors, 0);
     453             :         }
     454           0 :         catch (std::bad_alloc &e)
     455             :         {
     456           0 :             CPLError(CE_Failure, CPLE_AppDefined, "%s", e.what());
     457           0 :             return CE_Failure;
     458             :         }
     459             :     }
     460             : 
     461           3 :     VSILFILE *fpColorTable = VSIFOpenL(osColorTableFileName, "rt");
     462           3 :     if (!fpColorTable)
     463             :     {
     464           0 :         CPLError(CE_Failure, CPLE_AppDefined, "Invalid color table: \"%s\"",
     465             :                  osColorTableFileName.c_str());
     466           0 :         return CE_Failure;
     467             :     }
     468             : 
     469           3 :     nNReadPaletteColors = 0;
     470             :     const char *pszLine;
     471          51 :     while ((pszLine = CPLReadLineL(fpColorTable)) != nullptr)
     472             :     {
     473             :         // Ignore empty lines
     474          48 :         if (pszLine[0] == '\0')
     475           0 :             continue;
     476             : 
     477          48 :         const CPLStringList aosTokens(CSLTokenizeString2(pszLine, " \t", 0));
     478          48 :         if (aosTokens.size() != 4)
     479             :         {
     480           0 :             VSIFCloseL(fpColorTable);
     481           0 :             CPLError(CE_Failure, CPLE_AppDefined, "Invalid color table: \"%s\"",
     482             :                      osColorTableFileName.c_str());
     483           0 :             return CE_Failure;
     484             :         }
     485             : 
     486             :         // Index of the color
     487             :         // papszTokens[0] is ignored;
     488             : 
     489             :         // RED
     490          48 :         m_aadfPaletteColors[0][nNReadPaletteColors] = CPLAtof(aosTokens[1]);
     491             : 
     492             :         // GREEN
     493          48 :         m_aadfPaletteColors[1][nNReadPaletteColors] = CPLAtof(aosTokens[2]);
     494             : 
     495             :         // BLUE
     496          48 :         m_aadfPaletteColors[2][nNReadPaletteColors] = CPLAtof(aosTokens[3]);
     497             : 
     498             :         // ALPHA
     499          48 :         m_aadfPaletteColors[3][nNReadPaletteColors] = 255.0;
     500          48 :         nNReadPaletteColors++;
     501             :     }
     502             : 
     503             :     // Filling the rest of colors.
     504           3 :     for (int nIColorIndex = nNReadPaletteColors;
     505       65811 :          nIColorIndex < m_nNPaletteColors; nIColorIndex++)
     506             :     {
     507       65808 :         m_aadfPaletteColors[0][nNReadPaletteColors] = m_sDefaultColorRGB.c1;
     508       65808 :         m_aadfPaletteColors[1][nNReadPaletteColors] = m_sDefaultColorRGB.c2;
     509       65808 :         m_aadfPaletteColors[2][nNReadPaletteColors] = m_sDefaultColorRGB.c3;
     510       65808 :         m_aadfPaletteColors[3][nNReadPaletteColors] = m_sDefaultColorRGB.c4;
     511       65808 :         nNReadPaletteColors++;
     512             :     }
     513             : 
     514           3 :     if (nNReadPaletteColors != m_nNPaletteColors)
     515             :     {
     516           0 :         VSIFCloseL(fpColorTable);
     517           0 :         CPLError(CE_Failure, CPLE_AppDefined, "Invalid color table: \"%s\"",
     518             :                  osColorTableFileName.c_str());
     519           0 :         return CE_Failure;
     520             :     }
     521             : 
     522           3 :     VSIFCloseL(fpColorTable);
     523             : 
     524           3 :     return CE_None;
     525             : }
     526             : 
     527         116 : CPLErr MMRPalettes::UpdateColorInfo()
     528             : {
     529         232 :     CPLString os_Color_EscalatColor;
     530         116 :     if (m_pfRel->GetMetadataValue(SECTION_COLOR_TEXT, m_osBandSection,
     531             :                                   "Color_EscalatColor",
     532         128 :                                   os_Color_EscalatColor) &&
     533          12 :         !os_Color_EscalatColor.empty())
     534             :     {
     535          12 :         if (os_Color_EscalatColor.compare("AssigDirecta") == 0)
     536           7 :             m_ColorScaling = ColorTreatment::DIRECT_ASSIGNATION;
     537           5 :         else if (os_Color_EscalatColor.compare("DespOrigen") == 0)
     538           0 :             m_ColorScaling = ColorTreatment::ORIGIN_DISPLACEMENT;
     539           5 :         else if (os_Color_EscalatColor.compare("lineal") == 0)
     540           5 :             m_ColorScaling = ColorTreatment::LINEAR_SCALING;
     541           0 :         else if (os_Color_EscalatColor.compare("log_10") == 0)
     542           0 :             m_ColorScaling = ColorTreatment::LOG_10_SCALING;
     543           0 :         else if (os_Color_EscalatColor.compare("IntervalsUsuari") == 0)
     544           0 :             m_ColorScaling = ColorTreatment::USER_INTERVALS;
     545             :     }
     546             :     else
     547             :     {
     548         104 :         if (IsCategorical())
     549          77 :             m_ColorScaling = ColorTreatment::DIRECT_ASSIGNATION;
     550             :         else
     551          27 :             m_ColorScaling = ColorTreatment::LINEAR_SCALING;
     552             :     }
     553             : 
     554         116 :     if (m_ColorScaling == ColorTreatment::DEFAULT_SCALING)
     555           0 :         return CE_Failure;
     556         116 :     return CE_None;
     557             : }

Generated by: LCOV version 1.14