LCOV - code coverage report
Current view: top level - frmts/miramon - miramon_palettes.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 206 275 74.9 %
Date: 2026-05-08 18:52:02 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          41 :     if (oColorTable.BytesPerRecord == UINT32_MAX)
     312             :     {
     313           0 :         CPLError(CE_Failure, CPLE_AssertionFailed,
     314             :                  "Invalid color table:"
     315             :                  "\"%s\".",
     316             :                  osColorTableFileName.c_str());
     317             : 
     318           0 :         VSIFCloseL(oColorTable.pfDataBase);
     319           0 :         MM_ReleaseMainFields(&oColorTable);
     320           0 :         return CE_Failure;
     321             :     }
     322             : 
     323             :     // Guessing or reading the number of colors of the palette.
     324          41 :     MM_ACCUMULATED_BYTES_TYPE_DBF nBufferSize = oColorTable.BytesPerRecord + 1;
     325          41 :     char *pzsRecord = static_cast<char *>(VSI_CALLOC_VERBOSE(1, nBufferSize));
     326          41 :     if (!pzsRecord)
     327             :     {
     328           0 :         CPLError(CE_Failure, CPLE_OutOfMemory,
     329             :                  "Out of memory allocating working buffer");
     330           0 :         VSIFCloseL(oColorTable.pfDataBase);
     331           0 :         MM_ReleaseMainFields(&oColorTable);
     332           0 :         return CE_Failure;
     333             :     }
     334          41 :     char *pszField = static_cast<char *>(VSI_CALLOC_VERBOSE(1, nBufferSize));
     335             : 
     336          41 :     if (!pszField)
     337             :     {
     338           0 :         CPLError(CE_Failure, CPLE_OutOfMemory,
     339             :                  "Out of memory allocating working buffer");
     340           0 :         VSIFree(pzsRecord);
     341           0 :         VSIFCloseL(oColorTable.pfDataBase);
     342           0 :         MM_ReleaseMainFields(&oColorTable);
     343           0 :         return CE_Failure;
     344             :     }
     345             : 
     346          41 :     m_nNPaletteColors = static_cast<int>(oColorTable.nRecords);  // Safe cast
     347             : 
     348             :     // Checking the size of the palette.
     349          41 :     if (m_nNPaletteColors < 0 || m_nNPaletteColors > 65536)
     350             :     {
     351           0 :         m_nNPaletteColors = 0;
     352           0 :         CPLError(CE_Failure, CPLE_AppDefined,
     353             :                  "Invalid number of colors: %d "
     354             :                  "in color table \"%s\".",
     355             :                  m_nNPaletteColors, osColorTableFileName.c_str());
     356             : 
     357           0 :         VSIFree(pszField);
     358           0 :         VSIFree(pzsRecord);
     359           0 :         VSIFCloseL(oColorTable.pfDataBase);
     360           0 :         MM_ReleaseMainFields(&oColorTable);
     361           0 :         return CE_Failure;
     362             :     }
     363             : 
     364             :     // Getting the memory to allocate the color values
     365         205 :     for (int iColumn = 0; iColumn < 4; iColumn++)
     366             :     {
     367             :         try
     368             :         {
     369         164 :             m_aadfPaletteColors[iColumn].resize(m_nNPaletteColors, 0);
     370             :         }
     371           0 :         catch (std::bad_alloc &e)
     372             :         {
     373           0 :             CPLError(CE_Failure, CPLE_AppDefined, "%s", e.what());
     374           0 :             VSIFree(pszField);
     375           0 :             VSIFree(pzsRecord);
     376           0 :             VSIFCloseL(oColorTable.pfDataBase);
     377           0 :             MM_ReleaseMainFields(&oColorTable);
     378           0 :             return CE_Failure;
     379             :         }
     380             :     }
     381             : 
     382          41 :     VSIFSeekL(oColorTable.pfDataBase,
     383          41 :               static_cast<vsi_l_offset>(oColorTable.FirstRecordOffset),
     384             :               SEEK_SET);
     385             :     /*
     386             :         Each record's CLAUSIMBOL field doesn't match a pixel value present in the raster,
     387             :         and it's used only for discovering nodata value (blanc value).
     388             :         The list of values is used to map every value in a color using:
     389             :             - Direct assignation: mode used in categorical modes but possible in continuous.
     390             :             - Linear scaling
     391             :             - Logarithmic scaling
     392             :         */
     393        1376 :     for (int nIPaletteColors = 0; nIPaletteColors < m_nNPaletteColors;
     394             :          nIPaletteColors++)
     395             :     {
     396        2670 :         if (oColorTable.BytesPerRecord !=
     397        1335 :             VSIFReadL(pzsRecord, sizeof(unsigned char),
     398        1335 :                       oColorTable.BytesPerRecord, oColorTable.pfDataBase))
     399             :         {
     400           0 :             CPLError(CE_Failure, CPLE_AppDefined, "Invalid color table: \"%s\"",
     401             :                      osColorTableFileName.c_str());
     402           0 :             VSIFree(pszField);
     403           0 :             VSIFree(pzsRecord);
     404           0 :             VSIFCloseL(oColorTable.pfDataBase);
     405           0 :             MM_ReleaseMainFields(&oColorTable);
     406           0 :             return CE_Failure;
     407             :         }
     408             : 
     409             :         // Nodata identification
     410        1335 :         memcpy(pszField,
     411        1335 :                pzsRecord + oColorTable.pField[nClauSimbol].AccumulatedBytes,
     412        1335 :                oColorTable.pField[nClauSimbol].BytesPerField);
     413        1335 :         pszField[oColorTable.pField[nClauSimbol].BytesPerField] = '\0';
     414        2670 :         CPLString osField = pszField;
     415        1335 :         osField.replaceAll(" ", "");
     416        1335 :         if (osField.empty())  // Nodata value
     417             :         {
     418          11 :             m_bHasNodata = true;
     419          11 :             m_nNoDataPaletteIndex = nIPaletteColors;
     420             :         }
     421             : 
     422        1335 :         AssignColorFromDBF(oColorTable, pzsRecord, pszField, nRIndex, nGIndex,
     423             :                            nBIndex, nIPaletteColors);
     424             :     }
     425             : 
     426          41 :     VSIFree(pszField);
     427          41 :     VSIFree(pzsRecord);
     428          41 :     VSIFCloseL(oColorTable.pfDataBase);
     429          41 :     MM_ReleaseMainFields(&oColorTable);
     430             : 
     431          41 :     return CE_None;
     432             : }
     433             : 
     434             : // Colors in a PAL, P25 or P65 format files
     435             : // Updates nNPaletteColors
     436             : CPLErr
     437           3 : MMRPalettes::GetPaletteColors_PAL_P25_P65(const CPLString &os_Color_Paleta_DBF)
     438             : 
     439             : {
     440           6 :     CPLString osAux = CPLGetPathSafe(m_pfRel->GetRELNameChar());
     441             :     CPLString osColorTableFileName =
     442           6 :         CPLFormFilenameSafe(osAux.c_str(), os_Color_Paleta_DBF.c_str(), "");
     443             : 
     444             :     // This kind of palette has not NoData color.
     445             :     //bHasNodata = false;
     446             : 
     447           6 :     CPLString osExtension = CPLGetExtensionSafe(os_Color_Paleta_DBF);
     448           3 :     int nNReadPaletteColors = 0;
     449           3 :     m_nNPaletteColors = 0;
     450             : 
     451           3 :     if (osExtension.tolower() == "pal")
     452           1 :         m_nNPaletteColors = 64;
     453           2 :     else if (osExtension.tolower() == "p25")
     454           1 :         m_nNPaletteColors = 256;
     455           1 :     else if (osExtension.tolower() == "p65")
     456           1 :         m_nNPaletteColors = 65536;
     457             :     else
     458           0 :         return CE_None;
     459             : 
     460          15 :     for (int iColumn = 0; iColumn < 4; iColumn++)
     461             :     {
     462             :         try
     463             :         {
     464          12 :             m_aadfPaletteColors[iColumn].resize(m_nNPaletteColors, 0);
     465             :         }
     466           0 :         catch (std::bad_alloc &e)
     467             :         {
     468           0 :             CPLError(CE_Failure, CPLE_AppDefined, "%s", e.what());
     469           0 :             return CE_Failure;
     470             :         }
     471             :     }
     472             : 
     473           3 :     VSILFILE *fpColorTable = VSIFOpenL(osColorTableFileName, "rt");
     474           3 :     if (!fpColorTable)
     475             :     {
     476           0 :         CPLError(CE_Failure, CPLE_AppDefined, "Invalid color table: \"%s\"",
     477             :                  osColorTableFileName.c_str());
     478           0 :         return CE_Failure;
     479             :     }
     480             : 
     481           3 :     nNReadPaletteColors = 0;
     482             :     const char *pszLine;
     483          51 :     while ((pszLine = CPLReadLineL(fpColorTable)) != nullptr)
     484             :     {
     485             :         // Ignore empty lines
     486          48 :         if (pszLine[0] == '\0')
     487           0 :             continue;
     488             : 
     489          48 :         const CPLStringList aosTokens(CSLTokenizeString2(pszLine, " \t", 0));
     490          48 :         if (aosTokens.size() != 4)
     491             :         {
     492           0 :             VSIFCloseL(fpColorTable);
     493           0 :             CPLError(CE_Failure, CPLE_AppDefined, "Invalid color table: \"%s\"",
     494             :                      osColorTableFileName.c_str());
     495           0 :             return CE_Failure;
     496             :         }
     497             : 
     498             :         // Index of the color
     499             :         // papszTokens[0] is ignored;
     500             : 
     501             :         // RED
     502          48 :         m_aadfPaletteColors[0][nNReadPaletteColors] = CPLAtof(aosTokens[1]);
     503             : 
     504             :         // GREEN
     505          48 :         m_aadfPaletteColors[1][nNReadPaletteColors] = CPLAtof(aosTokens[2]);
     506             : 
     507             :         // BLUE
     508          48 :         m_aadfPaletteColors[2][nNReadPaletteColors] = CPLAtof(aosTokens[3]);
     509             : 
     510             :         // ALPHA
     511          48 :         m_aadfPaletteColors[3][nNReadPaletteColors] = 255.0;
     512          48 :         nNReadPaletteColors++;
     513             :     }
     514             : 
     515             :     // Filling the rest of colors.
     516           3 :     for (int nIColorIndex = nNReadPaletteColors;
     517       65811 :          nIColorIndex < m_nNPaletteColors; nIColorIndex++)
     518             :     {
     519       65808 :         m_aadfPaletteColors[0][nNReadPaletteColors] = m_sDefaultColorRGB.c1;
     520       65808 :         m_aadfPaletteColors[1][nNReadPaletteColors] = m_sDefaultColorRGB.c2;
     521       65808 :         m_aadfPaletteColors[2][nNReadPaletteColors] = m_sDefaultColorRGB.c3;
     522       65808 :         m_aadfPaletteColors[3][nNReadPaletteColors] = m_sDefaultColorRGB.c4;
     523       65808 :         nNReadPaletteColors++;
     524             :     }
     525             : 
     526           3 :     if (nNReadPaletteColors != m_nNPaletteColors)
     527             :     {
     528           0 :         VSIFCloseL(fpColorTable);
     529           0 :         CPLError(CE_Failure, CPLE_AppDefined, "Invalid color table: \"%s\"",
     530             :                  osColorTableFileName.c_str());
     531           0 :         return CE_Failure;
     532             :     }
     533             : 
     534           3 :     VSIFCloseL(fpColorTable);
     535             : 
     536           3 :     return CE_None;
     537             : }
     538             : 
     539         116 : CPLErr MMRPalettes::UpdateColorInfo()
     540             : {
     541         232 :     CPLString os_Color_EscalatColor;
     542         116 :     if (m_pfRel->GetMetadataValue(SECTION_COLOR_TEXT, m_osBandSection,
     543             :                                   "Color_EscalatColor",
     544         128 :                                   os_Color_EscalatColor) &&
     545          12 :         !os_Color_EscalatColor.empty())
     546             :     {
     547          12 :         if (os_Color_EscalatColor.compare("AssigDirecta") == 0)
     548           7 :             m_ColorScaling = ColorTreatment::DIRECT_ASSIGNATION;
     549           5 :         else if (os_Color_EscalatColor.compare("DespOrigen") == 0)
     550           0 :             m_ColorScaling = ColorTreatment::ORIGIN_DISPLACEMENT;
     551           5 :         else if (os_Color_EscalatColor.compare("lineal") == 0)
     552           5 :             m_ColorScaling = ColorTreatment::LINEAR_SCALING;
     553           0 :         else if (os_Color_EscalatColor.compare("log_10") == 0)
     554           0 :             m_ColorScaling = ColorTreatment::LOG_10_SCALING;
     555           0 :         else if (os_Color_EscalatColor.compare("IntervalsUsuari") == 0)
     556           0 :             m_ColorScaling = ColorTreatment::USER_INTERVALS;
     557             :     }
     558             :     else
     559             :     {
     560         104 :         if (IsCategorical())
     561          77 :             m_ColorScaling = ColorTreatment::DIRECT_ASSIGNATION;
     562             :         else
     563          27 :             m_ColorScaling = ColorTreatment::LINEAR_SCALING;
     564             :     }
     565             : 
     566         116 :     if (m_ColorScaling == ColorTreatment::DEFAULT_SCALING)
     567           0 :         return CE_Failure;
     568         116 :     return CE_None;
     569             : }

Generated by: LCOV version 1.14