Returns new band with values reclassified.
54 {
56 uint32_t width = 0;
57 uint32_t height = 0;
58 int numval = 0;
59 int memsize = 0;
60 void *mem = NULL;
61 uint32_t src_hasnodata = 0;
62 double src_nodataval = 0.0;
63 int isnodata = 0;
64
65 int rtn;
68 int i;
69 double or = 0;
70 double ov = 0;
71 double nr = 0;
72 double nv = 0;
73 int do_nv = 0;
75
76 assert(NULL != srcband);
77 assert(NULL != exprset && exprcount > 0);
80
81
83 if (src_hasnodata)
85
86
89 numval = width * height;
92 if (!mem) {
93 rterror(
"rt_band_reclass: Could not allocate memory for band");
94 return 0;
95 }
96
97
98 if (!hasnodata) {
99 memset(mem, 0, memsize);
100 }
101
102 else {
103 int32_t checkvalint = 0;
104 uint32_t checkvaluint = 0;
105 double checkvaldouble = 0;
106 float checkvalfloat = 0;
107
108 switch (pixtype) {
110 {
111 uint8_t *ptr = mem;
113 for (i = 0; i < numval; i++)
114 ptr[i] = clamped_initval;
115 checkvalint = ptr[0];
116 break;
117 }
119 {
120 uint8_t *ptr = mem;
122 for (i = 0; i < numval; i++)
123 ptr[i] = clamped_initval;
124 checkvalint = ptr[0];
125 break;
126 }
128 {
129 uint8_t *ptr = mem;
131 for (i = 0; i < numval; i++)
132 ptr[i] = clamped_initval;
133 checkvalint = ptr[0];
134 break;
135 }
137 {
138 int8_t *ptr = mem;
140 for (i = 0; i < numval; i++)
141 ptr[i] = clamped_initval;
142 checkvalint = ptr[0];
143 break;
144 }
146 {
147 uint8_t *ptr = mem;
149 for (i = 0; i < numval; i++)
150 ptr[i] = clamped_initval;
151 checkvalint = ptr[0];
152 break;
153 }
155 {
156 int16_t *ptr = mem;
158 for (i = 0; i < numval; i++)
159 ptr[i] = clamped_initval;
160 checkvalint = ptr[0];
161 break;
162 }
164 {
165 uint16_t *ptr = mem;
167 for (i = 0; i < numval; i++)
168 ptr[i] = clamped_initval;
169 checkvalint = ptr[0];
170 break;
171 }
173 {
174 int32_t *ptr = mem;
176 for (i = 0; i < numval; i++)
177 ptr[i] = clamped_initval;
178 checkvalint = ptr[0];
179 break;
180 }
182 {
183 uint32_t *ptr = mem;
185 for (i = 0; i < numval; i++)
186 ptr[i] = clamped_initval;
187 checkvaluint = ptr[0];
188 break;
189 }
191 {
192 float *ptr = mem;
194 for (i = 0; i < numval; i++)
195 ptr[i] = clamped_initval;
196 checkvalfloat = ptr[0];
197 break;
198 }
200 {
201 double *ptr = mem;
202 for (i = 0; i < numval; i++)
203 ptr[i] = nodataval;
204 checkvaldouble = ptr[0];
205 break;
206 }
207 default:
208 {
209 rterror(
"rt_band_reclass: Unknown pixeltype %d", pixtype);
211 return 0;
212 }
213 }
214
215
217 nodataval,
218 checkvalint, checkvaluint,
219 checkvalfloat, checkvaldouble,
220 pixtype
221 );
222 }
223 RASTER_DEBUGF(3,
"rt_band_reclass: width = %d height = %d", width, height);
224
226 if (!band) {
227 rterror(
"rt_band_reclass: Could not create new band");
229 return 0;
230 }
233
234 for (x = 0;
x < width;
x++) {
235 for (y = 0;
y < height;
y++) {
237
238
241 continue;
242 }
243 RASTER_DEBUGF(4,
"(x, y, ov, isnodata) = (%d, %d, %f, %d)", x, y, ov, isnodata);
244
245 do {
246 do_nv = 0;
247
248
249 if (hasnodata && isnodata) {
250 do_nv = 1;
251 break;
252 }
253
254 for (i = 0; i < exprcount; i++) {
255 expr = exprset[i];
256
257
258 if (
261 ) {
262 do_nv = 1;
263 break;
264 }
265
266
267 if ((
271 )) || (
275 )) || (
277 )) {
278
279 if ((
283 )) || (
285 ov < expr->src.max ||
287 )) || (
288 ov < expr->src.max
289 )) {
290 do_nv = 1;
291 break;
292 }
293 }
294 }
295 }
296 while (0);
297
298
299 if (!do_nv) continue;
301
302
303
304
305
306
307
308
309 if (hasnodata && isnodata) {
310 nv = nodataval;
311 }
312
313
314
315
318 }
319 else {
322 nv = (((ov - expr->
src.
min) * nr) / or) + expr->
dst.
min;
323
324
328 else if (nv < expr->dst.max)
330 }
331
332 else {
333 if (nv < expr->dst.min)
335 else if (nv > expr->
dst.
max)
337 }
338 }
339
340
341 switch (pixtype) {
351 nv = round(nv);
352 break;
353 default:
354 break;
355 }
356
357 RASTER_DEBUGF(4,
"(%d, %d) ov: %f or: %f - %f nr: %f - %f nv: %f"
358 , x
359 , y
360 , ov
361 , (NULL != expr) ? expr->
src.
min : 0
362 , (NULL != expr) ? expr->src.max : 0
363 , (NULL != expr) ? expr->dst.min : 0
364 , (NULL != expr) ? expr->dst.max : 0
365 , nv
366 );
368 rterror(
"rt_band_reclass: Could not assign value to new band");
371 return 0;
372 }
373
374 expr = NULL;
375 }
376 }
377
379}
rt_band rt_band_new_inline(uint16_t width, uint16_t height, rt_pixtype pixtype, uint32_t hasnodata, double nodataval, uint8_t *data)
Create an in-db rt_band with no data.
void rt_band_set_ownsdata_flag(rt_band band, int flag)
void rterror(const char *fmt,...)
Wrappers used for reporting errors and info.
void * rtalloc(size_t size)
Wrappers used for managing memory.
uint16_t rt_band_get_width(rt_band band)
Return width of this band.
#define RASTER_DEBUGF(level, msg,...)
int8_t rt_util_clamp_to_8BSI(double value)
uint8_t rt_util_clamp_to_1BB(double value)
int32_t rt_util_clamp_to_32BSI(double value)
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
rt_errorstate rt_band_get_pixel(rt_band band, int x, int y, double *value, int *nodata)
Get pixel value.
int rt_util_dbl_trunc_warning(double initialvalue, int32_t checkvalint, uint32_t checkvaluint, float checkvalfloat, double checkvaldouble, rt_pixtype pixtype)
uint8_t rt_util_clamp_to_2BUI(double value)
uint8_t rt_util_clamp_to_8BUI(double value)
rt_errorstate rt_band_set_pixel(rt_band band, int x, int y, double val, int *converted)
Set single pixel's value.
void rt_band_destroy(rt_band band)
Destroy a raster band.
int16_t rt_util_clamp_to_16BSI(double value)
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
uint8_t rt_util_clamp_to_4BUI(double value)
void rtdealloc(void *mem)
uint16_t rt_util_clamp_to_16BUI(double value)
uint32_t rt_util_clamp_to_32BUI(double value)
float rt_util_clamp_to_32F(double value)
uint16_t rt_band_get_height(rt_band band)
Return height of this band.
int rt_pixtype_size(rt_pixtype pixtype)
Return size in bytes of a value in the given pixtype.
struct rt_reclassexpr_t::rt_reclassrange src
struct rt_reclassexpr_t::rt_reclassrange dst