Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions autotest/cpp/test_gdal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6575,4 +6575,28 @@ TEST_F(test_gdal, GDALRasterBand_HasConflictingMaskSources)
}
}

TEST_F(test_gdal, GDALLoadEsriCLRAsRAT)
{
auto poRAT = GDALLoadEsriCLRAsRAT(GDRIVERS_DATA_DIR "ehdr/int16_rat.clr");
ASSERT_TRUE(poRAT != nullptr);

ASSERT_EQ(poRAT->GetColumnCount(), 4);
ASSERT_EQ(poRAT->GetRowCount(), 25);

ASSERT_STREQ(poRAT->GetNameOfCol(0), "Value");
ASSERT_STREQ(poRAT->GetNameOfCol(1), "Red");
ASSERT_STREQ(poRAT->GetNameOfCol(2), "Green");
ASSERT_STREQ(poRAT->GetNameOfCol(3), "Blue");

ASSERT_EQ(poRAT->GetValueAsInt(0, 0), -500);
ASSERT_EQ(poRAT->GetValueAsInt(0, 1), 127);
ASSERT_EQ(poRAT->GetValueAsInt(0, 2), 40);
ASSERT_EQ(poRAT->GetValueAsInt(0, 3), 65);

ASSERT_EQ(poRAT->GetValueAsInt(24, 0), 2000);
ASSERT_EQ(poRAT->GetValueAsInt(24, 1), 145);
ASSERT_EQ(poRAT->GetValueAsInt(24, 2), 97);
ASSERT_EQ(poRAT->GetValueAsInt(24, 3), 47);
}

} // namespace
86 changes: 27 additions & 59 deletions frmts/raw/ehdrdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1532,54 +1532,26 @@ GDALDataset *EHdrDataset::Open(GDALOpenInfo *poOpenInfo, bool bFileSizeCheck)

// Only read the .clr for byte, int16 or uint16 bands.
if (nItemSize <= 2)
fp = VSIFOpenL(osCLRFilename.c_str(), "r");
else
fp = nullptr;

if (fp != nullptr)
{
std::shared_ptr<GDALRasterAttributeTable> poRat(
new GDALDefaultRasterAttributeTable());
poRat->CreateColumn("Value", GFT_Integer, GFU_Generic);
poRat->CreateColumn("Red", GFT_Integer, GFU_Red);
poRat->CreateColumn("Green", GFT_Integer, GFU_Green);
poRat->CreateColumn("Blue", GFT_Integer, GFU_Blue);

poDS->m_poColorTable.reset(new GDALColorTable());

bool bHasFoundNonCTValues = false;
int nRatRow = 0;

while (true)
auto poRAT = GDALLoadEsriCLRAsRAT(osCLRFilename.c_str());
if (poRAT && poRAT->GetColumnCount() == 4)
{
pszLine = CPLReadLineL(fp);
if (!pszLine)
break;

if (*pszLine == '#' || *pszLine == '!')
continue;
poDS->m_poColorTable = std::make_unique<GDALColorTable>();

char **papszValues =
CSLTokenizeString2(pszLine, "\t ", CSLT_HONOURSTRINGS);
bool bHasFoundNonCTValues = false;

if (CSLCount(papszValues) >= 4)
for (int iRow = 0; iRow < poRAT->GetRowCount(); ++iRow)
{
const int nIndex = atoi(papszValues[0]);
poRat->SetValue(nRatRow, 0, nIndex);
poRat->SetValue(nRatRow, 1, atoi(papszValues[1]));
poRat->SetValue(nRatRow, 2, atoi(papszValues[2]));
poRat->SetValue(nRatRow, 3, atoi(papszValues[3]));
nRatRow++;

const int nIndex = poRAT->GetValueAsInt(iRow, 0);
if (nIndex >= 0 && nIndex < 65536)
{
const GDALColorEntry oEntry = {
static_cast<short>(
std::clamp(atoi(papszValues[1]), 0, 255)), // Red
static_cast<short>(
std::clamp(atoi(papszValues[2]), 0, 255)), // Green
static_cast<short>(
std::clamp(atoi(papszValues[3]), 0, 255)), // Blue
static_cast<short>(std::clamp(
poRAT->GetValueAsInt(iRow, 1), 0, 255)), // Red
static_cast<short>(std::clamp(
poRAT->GetValueAsInt(iRow, 2), 0, 255)), // Green
static_cast<short>(std::clamp(
poRAT->GetValueAsInt(iRow, 3), 0, 255)), // Blue
255};

poDS->m_poColorTable->SetColorEntry(nIndex, &oEntry);
Expand All @@ -1591,32 +1563,28 @@ GDALDataset *EHdrDataset::Open(GDALOpenInfo *poOpenInfo, bool bFileSizeCheck)
// http://www.ngdc.noaa.gov/mgg/topo/elev/esri/clr/
// But, there's no way of representing them with GDAL color
// table model.
if (!bHasFoundNonCTValues)
CPLDebug("EHdr", "Ignoring color index : %d", nIndex);
CPLDebug("EHdr", "Ignoring color index : %d", nIndex);
bHasFoundNonCTValues = true;
break;
}
}

CSLDestroy(papszValues);
}

CPL_IGNORE_RET_VAL(VSIFCloseL(fp));
if (bHasFoundNonCTValues)
{
poDS->m_poRAT.reset(poRAT.release());
}

if (bHasFoundNonCTValues)
{
poDS->m_poRAT.swap(poRat);
}
for (int i = 1; i <= poDS->nBands; i++)
{
EHdrRasterBand *poBand =
cpl::down_cast<EHdrRasterBand *>(poDS->GetRasterBand(i));
poBand->m_poColorTable = poDS->m_poColorTable;
poBand->m_poRAT = poDS->m_poRAT;
poBand->SetColorInterpretation(GCI_PaletteIndex);
}

for (int i = 1; i <= poDS->nBands; i++)
{
EHdrRasterBand *poBand =
cpl::down_cast<EHdrRasterBand *>(poDS->GetRasterBand(i));
poBand->m_poColorTable = poDS->m_poColorTable;
poBand->m_poRAT = poDS->m_poRAT;
poBand->SetColorInterpretation(GCI_PaletteIndex);
poDS->bCLRDirty = false;
}

poDS->bCLRDirty = false;
}

// Read statistics (.STX).
Expand Down
53 changes: 53 additions & 0 deletions gcore/gdal_rat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "cpl_error.h"
#include "cpl_string.h"
#include "cpl_vsi.h"
#include "cpl_vsi_virtual.h"

#ifdef __clang__
#pragma clang diagnostic push
Expand Down Expand Up @@ -3421,3 +3422,55 @@ void CPL_STDCALL GDALRATRemoveStatistics(GDALRasterAttributeTableH hRAT)

GDALRasterAttributeTable::FromHandle(hRAT)->RemoveStatistics();
}

/************************************************************************/
/* GDALLoadEsriCLRAsRAT() */
/************************************************************************/

/**
* \brief Load a Esri .clr as a RAT.
*
* @param pszFilename .clr filename
*
* @return a new RAT, or nullptr in case of error.
*
* @since GDAL 3.14
*/
std::unique_ptr<GDALRasterAttributeTable>
GDALLoadEsriCLRAsRAT(const char *pszFilename)
{
auto fp = VSIFilesystemHandler::OpenStatic(pszFilename, "rb");
if (!fp)
return nullptr;

auto poRAT = std::make_unique<GDALDefaultRasterAttributeTable>();
poRAT->CreateColumn("Value", GFT_Integer, GFU_Generic);
poRAT->CreateColumn("Red", GFT_Integer, GFU_Red);
poRAT->CreateColumn("Green", GFT_Integer, GFU_Green);
poRAT->CreateColumn("Blue", GFT_Integer, GFU_Blue);

int nRatRow = 0;

constexpr int MAX_LINE_SIZE = 1000; // Arbitary
while (const char *pszLine =
CPLReadLine2L(fp.get(), MAX_LINE_SIZE, nullptr))
{
if (*pszLine == '#' || *pszLine == '!')
continue;

const CPLStringList aosTokens(
CSLTokenizeString2(pszLine, "\t ", CSLT_HONOURSTRINGS));

if (aosTokens.size() >= 4)
{
const int nIndex = atoi(aosTokens[0]);
poRAT->SetValue(nRatRow, 0, nIndex);
poRAT->SetValue(nRatRow, 1, atoi(aosTokens[1]));
poRAT->SetValue(nRatRow, 2, atoi(aosTokens[2]));
poRAT->SetValue(nRatRow, 3, atoi(aosTokens[3]));
nRatRow++;
}
}

return poRAT;
}
3 changes: 3 additions & 0 deletions gcore/gdal_rat.h
Original file line number Diff line number Diff line change
Expand Up @@ -586,4 +586,7 @@ class CPL_DLL GDALDefaultRasterAttributeTable : public GDALRasterAttributeTable
std::unique_ptr<GDALRasterAttributeTable>
CPL_DLL GDALLoadVATDBF(const char *pszFilename);

std::unique_ptr<GDALRasterAttributeTable>
CPL_DLL GDALLoadEsriCLRAsRAT(const char *pszFilename);

#endif /* ndef GDAL_RAT_H_INCLUDED */
Loading