forked from postgis/postgis
/
rt_api.c
11749 lines (9941 loc) · 289 KB
/
rt_api.c
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* $Id$
*
* WKTRaster - Raster Types for PostGIS
* http://trac.osgeo.org/postgis/wiki/WKTRaster
*
* Copyright (C) 2011-2012 Regents of the University of California
* <bkpark@ucdavis.edu>
* Copyright (C) 2010-2011 Jorge Arevalo <jorge.arevalo@deimos-space.com>
* Copyright (C) 2010-2011 David Zwarg <dzwarg@azavea.com>
* Copyright (C) 2009-2011 Pierre Racine <pierre.racine@sbf.ulaval.ca>
* Copyright (C) 2009-2011 Mateusz Loskot <mateusz@loskot.net>
* Copyright (C) 2008-2009 Sandro Santilli <strk@keybit.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <math.h>
#include <stdio.h> /* for printf (default message handler) */
#include <stdarg.h> /* for va_list, va_start etc */
#include <string.h> /* for memcpy and strlen */
#include <assert.h>
#include <time.h> /* for time */
#include "rt_api.h"
#include "gdal_vrt.h"
/******************************************************************************
* Some rules for *.(c|h) files in rt_core
*
* All functions in rt_core that receive a band index parameter
* must be 0-based
*****************************************************************************/
/*--- Utilities -------------------------------------------------*/
static void
swap_char(uint8_t *a, uint8_t *b) {
uint8_t c = 0;
assert(NULL != a && NULL != b);
c = *a;
*a = *b;
*b = c;
}
static void
flip_endian_16(uint8_t *d) {
assert(NULL != d);
swap_char(d, d + 1);
}
static void
flip_endian_32(uint8_t *d) {
assert(NULL != d);
swap_char(d, d + 3);
swap_char(d + 1, d + 2);
}
static void
flip_endian_64(uint8_t *d) {
assert(NULL != d);
swap_char(d + 7, d);
swap_char(d + 6, d + 1);
swap_char(d + 5, d + 2);
swap_char(d + 4, d + 3);
}
uint8_t
rt_util_clamp_to_1BB(double value) {
return (uint8_t)fmin(fmax((value), 0), POSTGIS_RT_1BBMAX);
}
uint8_t
rt_util_clamp_to_2BUI(double value) {
return (uint8_t)fmin(fmax((value), 0), POSTGIS_RT_2BUIMAX);
}
uint8_t
rt_util_clamp_to_4BUI(double value) {
return (uint8_t)fmin(fmax((value), 0), POSTGIS_RT_4BUIMAX);
}
int8_t
rt_util_clamp_to_8BSI(double value) {
return (int8_t)fmin(fmax((value), SCHAR_MIN), SCHAR_MAX);
}
uint8_t
rt_util_clamp_to_8BUI(double value) {
return (uint8_t)fmin(fmax((value), 0), UCHAR_MAX);
}
int16_t
rt_util_clamp_to_16BSI(double value) {
return (int16_t)fmin(fmax((value), SHRT_MIN), SHRT_MAX);
}
uint16_t
rt_util_clamp_to_16BUI(double value) {
return (uint16_t)fmin(fmax((value), 0), USHRT_MAX);
}
int32_t
rt_util_clamp_to_32BSI(double value) {
return (int32_t)fmin(fmax((value), INT_MIN), INT_MAX);
}
uint32_t
rt_util_clamp_to_32BUI(double value) {
return (uint32_t)fmin(fmax((value), 0), UINT_MAX);
}
float
rt_util_clamp_to_32F(double value) {
return (float)fmin(fmax((value), -FLT_MAX), FLT_MAX);
}
/* quicksort */
#define SWAP(x, y) { double t; t = x; x = y; y = t; }
#define ORDER(x, y) if (x > y) SWAP(x, y)
static double pivot(double *left, double *right) {
double l, m, r, *p;
l = *left;
m = *(left + (right - left) / 2);
r = *right;
/* order */
ORDER(l, m);
ORDER(l, r);
ORDER(m, r);
/* pivot is higher of two values */
if (l < m) return m;
if (m < r) return r;
/* find pivot that isn't left */
for (p = left + 1; p <= right; ++p) {
if (*p != *left)
return (*p < *left) ? *left : *p;
}
/* all values are same */
return -1;
}
static double *partition(double *left, double *right, double pivot) {
while (left <= right) {
while (*left < pivot) ++left;
while (*right >= pivot) --right;
if (left < right) {
SWAP(*left, *right);
++left;
--right;
}
}
return left;
}
static void quicksort(double *left, double *right) {
double p = pivot(left, right);
double *pos;
if (p != -1) {
pos = partition(left, right, p);
quicksort(left, pos - 1);
quicksort(pos, right);
}
}
/**
* Convert cstring name to GDAL Resample Algorithm
*
* @param algname: cstring name to convert
*
* @return valid GDAL resampling algorithm
*/
GDALResampleAlg
rt_util_gdal_resample_alg(const char *algname) {
if (!algname || !strlen(algname)) return GRA_NearestNeighbour;
if (strcmp(algname, "NEARESTNEIGHBOUR") == 0)
return GRA_NearestNeighbour;
else if (strcmp(algname, "NEARESTNEIGHBOR") == 0)
return GRA_NearestNeighbour;
else if (strcmp(algname, "BILINEAR") == 0)
return GRA_Bilinear;
else if (strcmp(algname, "CUBICSPLINE") == 0)
return GRA_CubicSpline;
else if (strcmp(algname, "CUBIC") == 0)
return GRA_Cubic;
else if (strcmp(algname, "LANCZOS") == 0)
return GRA_Lanczos;
return GRA_NearestNeighbour;
}
/**
* Convert rt_pixtype to GDALDataType
*
* @param pt: pixeltype to convert
*
* @return valid GDALDataType
*/
GDALDataType
rt_util_pixtype_to_gdal_datatype(rt_pixtype pt) {
switch (pt) {
case PT_1BB:
case PT_2BUI:
case PT_4BUI:
case PT_8BUI:
return GDT_Byte;
case PT_8BSI:
case PT_16BSI:
return GDT_Int16;
case PT_16BUI:
return GDT_UInt16;
case PT_32BSI:
return GDT_Int32;
case PT_32BUI:
return GDT_UInt32;
case PT_32BF:
return GDT_Float32;
case PT_64BF:
return GDT_Float64;
default:
return GDT_Unknown;
}
return GDT_Unknown;
}
/**
* Convert GDALDataType to rt_pixtype
*
* @param gdt: GDAL datatype to convert
*
* @return valid rt_pixtype
*/
rt_pixtype
rt_util_gdal_datatype_to_pixtype(GDALDataType gdt) {
switch (gdt) {
case GDT_Byte:
return PT_8BUI;
case GDT_UInt16:
return PT_16BUI;
case GDT_Int16:
return PT_16BSI;
case GDT_UInt32:
return PT_32BUI;
case GDT_Int32:
return PT_32BSI;
case GDT_Float32:
return PT_32BF;
case GDT_Float64:
return PT_64BF;
default:
return PT_END;
}
return PT_END;
}
/*
get GDAL runtime version information
*/
const char*
rt_util_gdal_version(const char *request) {
if (NULL == request || !strlen(request))
return GDALVersionInfo("RELEASE_NAME");
else
return GDALVersionInfo(request);
}
/*
computed extent type
*/
rt_extenttype
rt_util_extent_type(const char *name) {
if (strcmp(name, "UNION") == 0)
return ET_UNION;
else if (strcmp(name, "FIRST") == 0)
return ET_FIRST;
else if (strcmp(name, "SECOND") == 0)
return ET_SECOND;
else
return ET_INTERSECTION;
}
char*
rt_util_gdal_convert_sr(const char *srs, int proj4) {
OGRSpatialReferenceH hsrs;
char *rtn = NULL;
hsrs = OSRNewSpatialReference(NULL);
if (OSRSetFromUserInput(hsrs, srs) == OGRERR_NONE) {
if (proj4)
OSRExportToProj4(hsrs, &rtn);
else
OSRExportToWkt(hsrs, &rtn);
}
else {
rterror("rt_util_gdal_convert_sr: Could not process the provided srs: %s", srs);
return NULL;
}
OSRDestroySpatialReference(hsrs);
if (rtn == NULL) {
rterror("rt_util_gdal_convert_sr: Could not process the provided srs: %s", srs);
return NULL;
}
return rtn;
}
int
rt_util_gdal_supported_sr(const char *srs) {
OGRSpatialReferenceH hsrs;
OGRErr rtn = OGRERR_NONE;
hsrs = OSRNewSpatialReference(NULL);
rtn = OSRSetFromUserInput(hsrs, srs);
OSRDestroySpatialReference(hsrs);
if (rtn == OGRERR_NONE)
return 1;
else
return 0;
}
int
rt_util_gdal_driver_registered(const char *drv) {
int count = GDALGetDriverCount();
int i = 0;
GDALDriverH hdrv = NULL;
if (drv == NULL || !strlen(drv) || count < 1)
return 0;
for (i = 0; i < count; i++) {
hdrv = GDALGetDriver(i);
if (hdrv == NULL) continue;
if (strcmp(drv, GDALGetDriverShortName(hdrv)) == 0)
return 1;
}
return 0;
}
void
rt_util_from_ogr_envelope(
OGREnvelope env,
rt_envelope *ext
) {
assert(ext != NULL);
ext->MinX = env.MinX;
ext->MaxX = env.MaxX;
ext->MinY = env.MinY;
ext->MaxY = env.MaxY;
ext->UpperLeftX = env.MinX;
ext->UpperLeftY = env.MaxY;
}
void
rt_util_to_ogr_envelope(
rt_envelope ext,
OGREnvelope *env
) {
assert(env != NULL);
env->MinX = ext.MinX;
env->MaxX = ext.MaxX;
env->MinY = ext.MinY;
env->MaxY = ext.MaxY;
}
LWPOLY *
rt_util_envelope_to_lwpoly(
rt_envelope env
) {
LWPOLY *npoly = NULL;
POINTARRAY **rings = NULL;
POINTARRAY *pts = NULL;
POINT4D p4d;
rings = (POINTARRAY **) rtalloc(sizeof (POINTARRAY*));
if (!rings) {
rterror("rt_util_envelope_to_lwpoly: Out of memory building envelope's geometry");
return NULL;
}
rings[0] = ptarray_construct(0, 0, 5);
if (!rings[0]) {
rterror("rt_util_envelope_to_lwpoly: Out of memory building envelope's geometry ring");
return NULL;
}
pts = rings[0];
/* Upper-left corner (first and last points) */
p4d.x = env.MinX;
p4d.y = env.MaxY;
ptarray_set_point4d(pts, 0, &p4d);
ptarray_set_point4d(pts, 4, &p4d);
/* Upper-right corner (we go clockwise) */
p4d.x = env.MaxX;
p4d.y = env.MaxY;
ptarray_set_point4d(pts, 1, &p4d);
/* Lower-right corner */
p4d.x = env.MaxX;
p4d.y = env.MinY;
ptarray_set_point4d(pts, 2, &p4d);
/* Lower-left corner */
p4d.x = env.MinX;
p4d.y = env.MinY;
ptarray_set_point4d(pts, 3, &p4d);
npoly = lwpoly_construct(SRID_UNKNOWN, 0, 1, rings);
if (npoly == NULL) {
rterror("rt_util_envelope_to_lwpoly: Unable to build envelope's geometry");
return NULL;
}
return npoly;
}
/*- rt_context -------------------------------------------------------*/
/* Functions definitions */
void * init_rt_allocator(size_t size);
void * init_rt_reallocator(void * mem, size_t size);
void init_rt_deallocator(void * mem);
void init_rt_errorreporter(const char * fmt, va_list ap);
void init_rt_warnreporter(const char * fmt, va_list ap);
void init_rt_inforeporter(const char * fmt, va_list ap);
/*
* Default allocators
*
* We include some default allocators that use malloc/free/realloc
* along with stdout/stderr since this is the most common use case
*
*/
void *
default_rt_allocator(size_t size)
{
void *mem = malloc(size);
return mem;
}
void *
default_rt_reallocator(void *mem, size_t size)
{
void *ret = realloc(mem, size);
return ret;
}
void
default_rt_deallocator(void *mem)
{
free(mem);
}
void
default_rt_error_handler(const char *fmt, va_list ap) {
static const char *label = "ERROR: ";
char newfmt[1024] = {0};
snprintf(newfmt, 1024, "%s%s\n", label, fmt);
newfmt[1023] = '\0';
vprintf(newfmt, ap);
va_end(ap);
}
void
default_rt_warning_handler(const char *fmt, va_list ap) {
static const char *label = "WARNING: ";
char newfmt[1024] = {0};
snprintf(newfmt, 1024, "%s%s\n", label, fmt);
newfmt[1023] = '\0';
vprintf(newfmt, ap);
va_end(ap);
}
void
default_rt_info_handler(const char *fmt, va_list ap) {
static const char *label = "INFO: ";
char newfmt[1024] = {0};
snprintf(newfmt, 1024, "%s%s\n", label, fmt);
newfmt[1023] = '\0';
vprintf(newfmt, ap);
va_end(ap);
}
/**
* Struct definition here
*/
struct rt_context_t {
rt_allocator alloc;
rt_reallocator realloc;
rt_deallocator dealloc;
rt_message_handler err;
rt_message_handler warn;
rt_message_handler info;
};
/* Static variable, to be used for all rt_core functions */
static struct rt_context_t ctx_t = {
.alloc = init_rt_allocator,
.realloc = init_rt_reallocator,
.dealloc = init_rt_deallocator,
.err = init_rt_errorreporter,
.warn = init_rt_warnreporter,
.info = init_rt_inforeporter
};
/**
* This function is normally called by rt_init_allocators when no special memory
* management is needed. Useful in raster core testing and in the (future)
* loader, when we need to use raster core functions but we don't have
* PostgreSQL backend behind. We must take care of memory by ourselves in those
* situations
*/
void
rt_install_default_allocators(void)
{
ctx_t.alloc = default_rt_allocator;
ctx_t.realloc = default_rt_reallocator;
ctx_t.dealloc = default_rt_deallocator;
ctx_t.err = default_rt_error_handler;
ctx_t.info = default_rt_info_handler;
ctx_t.warn = default_rt_warning_handler;
}
/**
* This function is called by rt_init_allocators when the PostgreSQL backend is
* taking care of the memory and we want to use palloc family
*/
void
rt_set_handlers(rt_allocator allocator, rt_reallocator reallocator,
rt_deallocator deallocator, rt_message_handler error_handler,
rt_message_handler info_handler, rt_message_handler warning_handler) {
ctx_t.alloc = allocator;
ctx_t.realloc = reallocator;
ctx_t.dealloc = deallocator;
ctx_t.err = error_handler;
ctx_t.info = info_handler;
ctx_t.warn = warning_handler;
}
/**
* Initialisation allocators
*
* These are used the first time any of the allocators are called to enable
* executables/libraries that link into raster to be able to set up their own
* allocators. This is mainly useful for older PostgreSQL versions that don't
* have functions that are called upon startup.
**/
void *
init_rt_allocator(size_t size)
{
rt_init_allocators();
return ctx_t.alloc(size);
}
void
init_rt_deallocator(void *mem)
{
rt_init_allocators();
ctx_t.dealloc(mem);
}
void *
init_rt_reallocator(void *mem, size_t size)
{
rt_init_allocators();
return ctx_t.realloc(mem, size);
}
void
init_rt_inforeporter(const char *fmt, va_list ap)
{
rt_init_allocators();
(*ctx_t.info)(fmt, ap);
}
void
init_rt_warnreporter(const char *fmt, va_list ap)
{
rt_init_allocators();
(*ctx_t.warn)(fmt, ap);
}
void
init_rt_errorreporter(const char *fmt, va_list ap)
{
rt_init_allocators();
(*ctx_t.err)(fmt, ap);
}
/**
* Raster core memory management functions.
*
* They use the functions defined by the caller.
*/
void *
rtalloc(size_t size) {
void * mem = ctx_t.alloc(size);
RASTER_DEBUGF(5, "rtalloc called: %d@%p", size, mem);
return mem;
}
void *
rtrealloc(void * mem, size_t size) {
void * result = ctx_t.realloc(mem, size);
RASTER_DEBUGF(5, "rtrealloc called: %d@%p", size, result);
return result;
}
void
rtdealloc(void * mem) {
ctx_t.dealloc(mem);
RASTER_DEBUG(5, "rtdealloc called");
}
/**
* Raster core error and info handlers
*
* Since variadic functions cannot pass their parameters directly, we need
* wrappers for these functions to convert the arguments into a va_list
* structure.
*/
void
rterror(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
/* Call the supplied function */
(*ctx_t.err)(fmt, ap);
va_end(ap);
}
void
rtinfo(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
/* Call the supplied function */
(*ctx_t.info)(fmt, ap);
va_end(ap);
}
void
rtwarn(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
/* Call the supplied function */
(*ctx_t.warn)(fmt, ap);
va_end(ap);
}
int
rt_util_dbl_trunc_warning(
double initialvalue,
int32_t checkvalint, uint32_t checkvaluint,
float checkvalfloat, double checkvaldouble,
rt_pixtype pixtype
) {
int result = 0;
switch (pixtype) {
case PT_1BB:
case PT_2BUI:
case PT_4BUI:
case PT_8BSI:
case PT_8BUI:
case PT_16BSI:
case PT_16BUI:
case PT_32BSI: {
if (fabs(checkvalint - initialvalue) >= 1) {
#if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
rtwarn("Value set for %s band got clamped from %f to %d",
rt_pixtype_name(pixtype),
initialvalue, checkvalint
);
#endif
result = 1;
}
else if (FLT_NEQ(checkvalint, initialvalue)) {
#if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
rtwarn("Value set for %s band got truncated from %f to %d",
rt_pixtype_name(pixtype),
initialvalue, checkvalint
);
#endif
result = 1;
}
break;
}
case PT_32BUI: {
if (fabs(checkvaluint - initialvalue) >= 1) {
#if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
rtwarn("Value set for %s band got clamped from %f to %u",
rt_pixtype_name(pixtype),
initialvalue, checkvaluint
);
#endif
result = 1;
}
else if (FLT_NEQ(checkvaluint, initialvalue)) {
#if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
rtwarn("Value set for %s band got truncated from %f to %u",
rt_pixtype_name(pixtype),
initialvalue, checkvaluint
);
#endif
result = 1;
}
break;
}
case PT_32BF: {
/*
For float, because the initial value is a double,
there is very often a difference between the desired value and the obtained one
*/
if (FLT_NEQ(checkvalfloat, initialvalue)) {
#if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
rtwarn("Value set for %s band got converted from %f to %f",
rt_pixtype_name(pixtype),
initialvalue, checkvalfloat
);
#endif
result = 1;
}
break;
}
case PT_64BF: {
if (FLT_NEQ(checkvaldouble, initialvalue)) {
#if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
rtwarn("Value set for %s band got converted from %f to %f",
rt_pixtype_name(pixtype),
initialvalue, checkvaldouble
);
#endif
result = 1;
}
break;
}
case PT_END:
break;
}
return result;
}
/*--- Debug and Testing Utilities --------------------------------------------*/
#if POSTGIS_DEBUG_LEVEL > 2
static char*
d_binary_to_hex(const uint8_t * const raw, uint32_t size, uint32_t *hexsize) {
char* hex = NULL;
uint32_t i = 0;
assert(NULL != raw);
assert(NULL != hexsize);
*hexsize = size * 2; /* hex is 2 times bytes */
hex = (char*) rtalloc((*hexsize) + 1);
if (!hex) {
rterror("d_binary_to_hex: Out of memory hexifying raw binary");
return NULL;
}
hex[*hexsize] = '\0'; /* Null-terminate */
for (i = 0; i < size; ++i) {
deparse_hex(raw[i], &(hex[2 * i]));
}
assert(NULL != hex);
assert(0 == strlen(hex) % 2);
return hex;
}
static void
d_print_binary_hex(const char* msg, const uint8_t * const raw, uint32_t size) {
char* hex = NULL;
uint32_t hexsize = 0;
assert(NULL != msg);
assert(NULL != raw);
hex = d_binary_to_hex(raw, size, &hexsize);
if (NULL != hex) {
rtinfo("%s\t%s", msg, hex);
rtdealloc(hex);
}
}
static size_t
d_binptr_to_pos(const uint8_t * const ptr, const uint8_t * const end, size_t size) {
assert(NULL != ptr && NULL != end);
return (size - (end - ptr));
}
#define CHECK_BINPTR_POSITION(ptr, end, size, pos) \
{ if (pos != d_binptr_to_pos(ptr, end, size)) { \
fprintf(stderr, "Check of binary stream pointer position failed on line %d\n", __LINE__); \
fprintf(stderr, "\tactual = %lu, expected = %lu\n", (long unsigned)d_binptr_to_pos(ptr, end, size), (long unsigned)pos); \
} }
#else
#define CHECK_BINPTR_POSITION(ptr, end, size, pos) ((void)0);
#endif /* ifndef POSTGIS_DEBUG_LEVEL > 3 */
/*- rt_pixeltype -----------------------------------------------------*/
int
rt_pixtype_size(rt_pixtype pixtype) {
int pixbytes = -1;
switch (pixtype) {
case PT_1BB:
case PT_2BUI:
case PT_4BUI:
case PT_8BSI:
case PT_8BUI:
pixbytes = 1;
break;
case PT_16BSI:
case PT_16BUI:
pixbytes = 2;
break;
case PT_32BSI:
case PT_32BUI:
case PT_32BF:
pixbytes = 4;
break;
case PT_64BF:
pixbytes = 8;
break;
default:
rterror("rt_pixtype_size: Unknown pixeltype %d", pixtype);
pixbytes = -1;
break;
}
RASTER_DEBUGF(3, "Pixel type = %s and size = %d bytes",
rt_pixtype_name(pixtype), pixbytes);
return pixbytes;
}
int
rt_pixtype_alignment(rt_pixtype pixtype) {
return rt_pixtype_size(pixtype);
}
rt_pixtype
rt_pixtype_index_from_name(const char* pixname) {
assert(pixname && strlen(pixname) > 0);
if (strcmp(pixname, "1BB") == 0)
return PT_1BB;
if (strcmp(pixname, "2BUI") == 0)
return PT_2BUI;
if (strcmp(pixname, "4BUI") == 0)
return PT_4BUI;
if (strcmp(pixname, "8BSI") == 0)
return PT_8BSI;
if (strcmp(pixname, "8BUI") == 0)
return PT_8BUI;
if (strcmp(pixname, "16BSI") == 0)
return PT_16BSI;
if (strcmp(pixname, "16BUI") == 0)
return PT_16BUI;
if (strcmp(pixname, "32BSI") == 0)
return PT_32BSI;
if (strcmp(pixname, "32BUI") == 0)
return PT_32BUI;
if (strcmp(pixname, "32BF") == 0)
return PT_32BF;
if (strcmp(pixname, "64BF") == 0)
return PT_64BF;
return PT_END;
}
const char*
rt_pixtype_name(rt_pixtype pixtype) {
switch (pixtype) {
case PT_1BB:
return "1BB";
case PT_2BUI:
return "2BUI";
case PT_4BUI:
return "4BUI";
case PT_8BSI:
return "8BSI";
case PT_8BUI:
return "8BUI";
case PT_16BSI:
return "16BSI";
case PT_16BUI:
return "16BUI";
case PT_32BSI:
return "32BSI";
case PT_32BUI:
return "32BUI";
case PT_32BF:
return "32BF";
case PT_64BF:
return "64BF";
default:
rterror("rt_pixtype_name: Unknown pixeltype %d", pixtype);
return "Unknown";
}
}
/**
* Return minimum value possible for pixel type
*
* @param pixtype: the pixel type to get minimum possible value for
*
* @return the minimum possible value for the pixel type.
*/
double