@@ -1532,54 +1532,26 @@ GDALDataset *EHdrDataset::Open(GDALOpenInfo *poOpenInfo, bool bFileSizeCheck)
15321532
15331533 // Only read the .clr for byte, int16 or uint16 bands.
15341534 if (nItemSize <= 2 )
1535- fp = VSIFOpenL (osCLRFilename.c_str (), " r" );
1536- else
1537- fp = nullptr ;
1538-
1539- if (fp != nullptr )
15401535 {
1541- std::shared_ptr<GDALRasterAttributeTable> poRat (
1542- new GDALDefaultRasterAttributeTable ());
1543- poRat->CreateColumn (" Value" , GFT_Integer, GFU_Generic);
1544- poRat->CreateColumn (" Red" , GFT_Integer, GFU_Red);
1545- poRat->CreateColumn (" Green" , GFT_Integer, GFU_Green);
1546- poRat->CreateColumn (" Blue" , GFT_Integer, GFU_Blue);
1547-
1548- poDS->m_poColorTable .reset (new GDALColorTable ());
1549-
1550- bool bHasFoundNonCTValues = false ;
1551- int nRatRow = 0 ;
1552-
1553- while (true )
1536+ auto poRAT = GDALLoadEsriCLRAsRAT (osCLRFilename.c_str ());
1537+ if (poRAT && poRAT->GetColumnCount () == 4 )
15541538 {
1555- pszLine = CPLReadLineL (fp);
1556- if (!pszLine)
1557- break ;
1558-
1559- if (*pszLine == ' #' || *pszLine == ' !' )
1560- continue ;
1539+ poDS->m_poColorTable = std::make_unique<GDALColorTable>();
15611540
1562- char **papszValues =
1563- CSLTokenizeString2 (pszLine, " \t " , CSLT_HONOURSTRINGS );
1541+ bool bHasFoundNonCTValues = false ;
15641542
1565- if ( CSLCount (papszValues) >= 4 )
1543+ for ( int iRow = 0 ; iRow < poRAT-> GetRowCount (); ++iRow )
15661544 {
1567- const int nIndex = atoi (papszValues[0 ]);
1568- poRat->SetValue (nRatRow, 0 , nIndex);
1569- poRat->SetValue (nRatRow, 1 , atoi (papszValues[1 ]));
1570- poRat->SetValue (nRatRow, 2 , atoi (papszValues[2 ]));
1571- poRat->SetValue (nRatRow, 3 , atoi (papszValues[3 ]));
1572- nRatRow++;
1573-
1545+ const int nIndex = poRAT->GetValueAsInt (iRow, 0 );
15741546 if (nIndex >= 0 && nIndex < 65536 )
15751547 {
15761548 const GDALColorEntry oEntry = {
1577- static_cast <short >(
1578- std::clamp ( atoi (papszValues[ 1 ] ), 0 , 255 )), // Red
1579- static_cast <short >(
1580- std::clamp ( atoi (papszValues[ 2 ] ), 0 , 255 )), // Green
1581- static_cast <short >(
1582- std::clamp ( atoi (papszValues[ 3 ] ), 0 , 255 )), // Blue
1549+ static_cast <short >(std::clamp (
1550+ poRAT-> GetValueAsInt (iRow, 1 ), 0 , 255 )), // Red
1551+ static_cast <short >(std::clamp (
1552+ poRAT-> GetValueAsInt (iRow, 2 ), 0 , 255 )), // Green
1553+ static_cast <short >(std::clamp (
1554+ poRAT-> GetValueAsInt (iRow, 3 ), 0 , 255 )), // Blue
15831555 255 };
15841556
15851557 poDS->m_poColorTable ->SetColorEntry (nIndex, &oEntry);
@@ -1591,32 +1563,28 @@ GDALDataset *EHdrDataset::Open(GDALOpenInfo *poOpenInfo, bool bFileSizeCheck)
15911563 // http://www.ngdc.noaa.gov/mgg/topo/elev/esri/clr/
15921564 // But, there's no way of representing them with GDAL color
15931565 // table model.
1594- if (!bHasFoundNonCTValues)
1595- CPLDebug (" EHdr" , " Ignoring color index : %d" , nIndex);
1566+ CPLDebug (" EHdr" , " Ignoring color index : %d" , nIndex);
15961567 bHasFoundNonCTValues = true ;
1568+ break ;
15971569 }
15981570 }
15991571
1600- CSLDestroy (papszValues);
1601- }
1602-
1603- CPL_IGNORE_RET_VAL ( VSIFCloseL (fp));
1572+ if (bHasFoundNonCTValues)
1573+ {
1574+ poDS-> m_poRAT . reset (poRAT. release ());
1575+ }
16041576
1605- if (bHasFoundNonCTValues)
1606- {
1607- poDS->m_poRAT .swap (poRat);
1608- }
1577+ for (int i = 1 ; i <= poDS->nBands ; i++)
1578+ {
1579+ EHdrRasterBand *poBand =
1580+ cpl::down_cast<EHdrRasterBand *>(poDS->GetRasterBand (i));
1581+ poBand->m_poColorTable = poDS->m_poColorTable ;
1582+ poBand->m_poRAT = poDS->m_poRAT ;
1583+ poBand->SetColorInterpretation (GCI_PaletteIndex);
1584+ }
16091585
1610- for (int i = 1 ; i <= poDS->nBands ; i++)
1611- {
1612- EHdrRasterBand *poBand =
1613- cpl::down_cast<EHdrRasterBand *>(poDS->GetRasterBand (i));
1614- poBand->m_poColorTable = poDS->m_poColorTable ;
1615- poBand->m_poRAT = poDS->m_poRAT ;
1616- poBand->SetColorInterpretation (GCI_PaletteIndex);
1586+ poDS->bCLRDirty = false ;
16171587 }
1618-
1619- poDS->bCLRDirty = false ;
16201588 }
16211589
16221590 // Read statistics (.STX).
0 commit comments