-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathRasterBandHelper.cpp
More file actions
200 lines (170 loc) · 5.36 KB
/
Copy pathRasterBandHelper.cpp
File metadata and controls
200 lines (170 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "stdafx.h"
#include "RasterBandHelper.h"
// ***************************************************************
// Cast()
// ***************************************************************
CGdalRasterBand* RasterBandHelper::Cast(IGdalRasterBand* band)
{
return (CGdalRasterBand*)band;
}
// ***************************************************************
// ColorTableToColorScheme()
// ***************************************************************
// Retrieve the color table, assuming that there is a color entry
// for each integer value between min and max.
bool RasterBandHelper::ColorTableToColorScheme(GDALRasterBand* _poBand, IGridColorScheme** newscheme)
{
if (!_poBand)
{
return false;
}
if (_poBand->GetRasterDataType() == GDT_Float32)
{
return false;
}
GDALColorTable * poCT = NULL;
GDALColorInterp cInterp;
const GDALColorEntry * poCE;
double tmp = 0;
bool colorTableExists = false;
bool colorEntryExists = false;
int bGotMin = false;
int bGotMax = false;
int iMin = static_cast<int>(GDALGetRasterMinimum(_poBand, &bGotMin));
int iMax = static_cast<int>(GDALGetRasterMaximum(_poBand, &bGotMax));
double range = GDALGetRasterMaximum(_poBand, &bGotMax) - GDALGetRasterMinimum(_poBand, &bGotMin);
double shift = 0 - GDALGetRasterMinimum(_poBand, &bGotMin);
poCT = _poBand->GetColorTable();
if (poCT != NULL)
colorTableExists = true;
if (!colorTableExists)
return false;
CoCreateInstance(CLSID_GridColorScheme, NULL, CLSCTX_INPROC_SERVER, IID_IGridColorScheme, (void**)newscheme);
(*newscheme)->put_AmbientIntensity(0.7);
(*newscheme)->put_LightSourceIntensity(0.7);
(*newscheme)->SetLightSource(0, 1);
(*newscheme)->put_LightSourceIntensity(0.7);
(*newscheme)->put_NoDataColor(0);
cInterp = _poBand->GetColorInterpretation();
OLE_COLOR lastColor = RGB(0, 0, 0);
int boundA = iMin;
int boundB = iMin;
for (int i = iMin; i < iMax; i++)
{
poCE = GDALGetColorEntry(poCT, i);
colorEntryExists = poCE != NULL;
OLE_COLOR newColor = RGB(0, 0, 0);
if (colorEntryExists)
{
if (cInterp == GCI_GrayIndex || cInterp == GCI_AlphaBand)
{
newColor = RGB((unsigned char)poCE->c4, (unsigned char)poCE->c4, (unsigned char)poCE->c4);
}
else
{
newColor = RGB((unsigned char)poCE->c1, (unsigned char)poCE->c2, (unsigned char)poCE->c3);
}
}
else
{
newColor = RGB((unsigned char)((tmp + shift) * 255 / range), (unsigned char)((tmp + shift) * 255 / range), (unsigned char)((tmp + shift) * 255 / range));
}
if (newColor != lastColor && i != iMin)
{
// Put a break for the last chunk of color bands
IGridColorBreak * newbreak;
CoCreateInstance(CLSID_GridColorBreak, NULL, CLSCTX_INPROC_SERVER, IID_IGridColorBreak, (void**)&newbreak);
newbreak->put_LowValue(boundA);
newbreak->put_HighValue(boundB);
newbreak->put_LowColor(lastColor);
newbreak->put_HighColor(lastColor);
newbreak->put_ColoringType(ColoringType::Random);
(*newscheme)->InsertBreak(newbreak);
newbreak->Release();
// Set the new bounds and the new "last" color
lastColor = newColor;
boundA = i;
boundB = i;
}
else if (newColor == lastColor)
{
boundB = i;
}
}
// Write the last one
IGridColorBreak * newbreak;
CoCreateInstance(CLSID_GridColorBreak, NULL, CLSCTX_INPROC_SERVER, IID_IGridColorBreak, (void**)&newbreak);
newbreak->put_LowValue(boundA);
newbreak->put_HighValue(boundB);
newbreak->put_LowColor(lastColor);
newbreak->put_HighColor(lastColor);
(*newscheme)->InsertBreak(newbreak);
newbreak->Release();
return true;
}
// ***************************************************************
// AllocateBuffer()
// ***************************************************************
bool RasterBandHelper::AllocateBuffer(GDALDataType dataType, int width, int height, void** buffer)
{
if (width <= 1 || height != 1)
{
CallbackHelper::ErrorMsg("GdalRasterBand::AllocateBuffer: Width and height must be greater than zero");
return false;
}
dataType = SimplifyDataType(dataType);
switch (dataType)
{
case GDT_Float64:
*buffer = new double[width * height];
break;
case GDT_Float32:
*buffer = new float[width * height];
break;
case GDT_Int32:
*buffer = new int[width * height];
break;
default:
return false;
}
return true;
}
// ***************************************************************
// GetSimpleDataType()
// ***************************************************************
GDALDataType RasterBandHelper::GetSimpleDataType(GDALRasterBand* band)
{
if (!band) return GDT_Unknown;
GDALDataType dataType = band->GetRasterDataType();
return SimplifyDataType(dataType);
}
// ***************************************************************
// GetSimpleDataType()
// ***************************************************************
GDALDataType RasterBandHelper::SimplifyDataType(GDALDataType dataType)
{
switch (dataType)
{
case GDT_Unknown:
return GDT_Unknown;
case GDT_CFloat64:
case GDT_Float64:
return GDT_Float64;
case GDT_CFloat32:
case GDT_Float32:
return GDT_Float32;
default:
return GDT_Int32;
}
}
// ***************************************************************
// GetOverviewCount()
// ***************************************************************
int RasterBandHelper::GetOverviewCount(GDALRasterBand* band)
{
if (!band) return 0;
m_globalSettings.SetGdalUtf8(true);
int count = band->GetOverviewCount();
m_globalSettings.SetGdalUtf8(false);
return count;
}