forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgdal_misc.cpp
5567 lines (4889 loc) · 194 KB
/
gdal_misc.cpp
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
/******************************************************************************
*
* Project: GDAL Core
* Purpose: Free standing functions for GDAL.
* Author: Frank Warmerdam, [email protected]
*
******************************************************************************
* Copyright (c) 1999, Frank Warmerdam
* Copyright (c) 2007-2013, Even Rouault <even dot rouault at spatialys.com>
*
* SPDX-License-Identifier: MIT
****************************************************************************/
#include "cpl_port.h"
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fcntl.h>
#include <algorithm>
#include <iostream>
#include <limits>
#include <string>
#include "cpl_conv.h"
#include "cpl_error.h"
#include "cpl_json.h"
#include "cpl_minixml.h"
#include "cpl_multiproc.h"
#include "cpl_string.h"
#include "cpl_vsi.h"
#ifdef EMBED_RESOURCE_FILES
#include "embedded_resources.h"
#endif
#include "gdal_version_full/gdal_version.h"
#include "gdal.h"
#include "gdal_mdreader.h"
#include "gdal_priv.h"
#include "gdal_priv_templates.hpp"
#include "ogr_core.h"
#include "ogr_spatialref.h"
#include "ogr_geos.h"
#include "proj.h"
#ifdef HAVE_CURL
#include "cpl_curl_priv.h"
#endif
static int GetMinBitsForPair(const bool pabSigned[], const bool pabFloating[],
const int panBits[])
{
if (pabFloating[0] != pabFloating[1])
{
const int nNotFloatingTypeIndex = pabFloating[0] ? 1 : 0;
const int nFloatingTypeIndex = pabFloating[0] ? 0 : 1;
return std::max(panBits[nFloatingTypeIndex],
2 * panBits[nNotFloatingTypeIndex]);
}
if (pabSigned[0] != pabSigned[1])
{
const int nUnsignedTypeIndex = pabSigned[0] ? 1 : 0;
const int nSignedTypeIndex = pabSigned[0] ? 0 : 1;
return std::max(panBits[nSignedTypeIndex],
2 * panBits[nUnsignedTypeIndex]);
}
return std::max(panBits[0], panBits[1]);
}
static int GetDataTypeElementSizeBits(GDALDataType eDataType)
{
switch (eDataType)
{
case GDT_Byte:
case GDT_Int8:
return 8;
case GDT_UInt16:
case GDT_Int16:
case GDT_CInt16:
return 16;
case GDT_UInt32:
case GDT_Int32:
case GDT_Float32:
case GDT_CInt32:
case GDT_CFloat32:
return 32;
case GDT_Float64:
case GDT_CFloat64:
case GDT_UInt64:
case GDT_Int64:
return 64;
case GDT_Unknown:
case GDT_TypeCount:
break;
}
return 0;
}
/************************************************************************/
/* GDALDataTypeUnion() */
/************************************************************************/
/**
* \brief Return the smallest data type that can fully express both input data
* types.
*
* @param eType1 first data type.
* @param eType2 second data type.
*
* @return a data type able to express eType1 and eType2.
*/
GDALDataType CPL_STDCALL GDALDataTypeUnion(GDALDataType eType1,
GDALDataType eType2)
{
if (eType1 == GDT_Unknown)
return eType2;
if (eType2 == GDT_Unknown)
return eType1;
const int panBits[] = {GetDataTypeElementSizeBits(eType1),
GetDataTypeElementSizeBits(eType2)};
if (panBits[0] == 0 || panBits[1] == 0)
return GDT_Unknown;
const bool pabSigned[] = {CPL_TO_BOOL(GDALDataTypeIsSigned(eType1)),
CPL_TO_BOOL(GDALDataTypeIsSigned(eType2))};
const bool bSigned = pabSigned[0] || pabSigned[1];
const bool pabFloating[] = {CPL_TO_BOOL(GDALDataTypeIsFloating(eType1)),
CPL_TO_BOOL(GDALDataTypeIsFloating(eType2))};
const bool bFloating = pabFloating[0] || pabFloating[1];
const bool bComplex = CPL_TO_BOOL(GDALDataTypeIsComplex(eType1)) ||
CPL_TO_BOOL(GDALDataTypeIsComplex(eType2));
const int nBits = GetMinBitsForPair(pabSigned, pabFloating, panBits);
return GDALFindDataType(nBits, bSigned, bFloating, bComplex);
}
/************************************************************************/
/* GDALDataTypeUnionWithValue() */
/************************************************************************/
/**
* \brief Union a data type with the one found for a value
*
* @param eDT the first data type
* @param dfValue the value for which to find a data type and union with eDT
* @param bComplex if the value is complex
*
* @return a data type able to express eDT and dfValue.
* @since GDAL 2.3
*/
GDALDataType CPL_STDCALL GDALDataTypeUnionWithValue(GDALDataType eDT,
double dfValue,
int bComplex)
{
if (!bComplex && !GDALDataTypeIsComplex(eDT))
{
switch (eDT)
{
case GDT_Byte:
{
if (GDALIsValueExactAs<uint8_t>(dfValue))
return eDT;
break;
}
case GDT_Int8:
{
if (GDALIsValueExactAs<int8_t>(dfValue))
return eDT;
break;
}
case GDT_UInt16:
{
if (GDALIsValueExactAs<uint16_t>(dfValue))
return eDT;
break;
}
case GDT_Int16:
{
if (GDALIsValueExactAs<int16_t>(dfValue))
return eDT;
break;
}
case GDT_UInt32:
{
if (GDALIsValueExactAs<uint32_t>(dfValue))
return eDT;
break;
}
case GDT_Int32:
{
if (GDALIsValueExactAs<int32_t>(dfValue))
return eDT;
break;
}
case GDT_UInt64:
{
if (GDALIsValueExactAs<uint64_t>(dfValue))
return eDT;
break;
}
case GDT_Int64:
{
if (GDALIsValueExactAs<int64_t>(dfValue))
return eDT;
break;
}
case GDT_Float32:
{
if (GDALIsValueExactAs<float>(dfValue))
return eDT;
break;
}
case GDT_Float64:
{
return eDT;
}
case GDT_Unknown:
case GDT_CInt16:
case GDT_CInt32:
case GDT_CFloat32:
case GDT_CFloat64:
case GDT_TypeCount:
break;
}
}
const GDALDataType eDT2 = GDALFindDataTypeForValue(dfValue, bComplex);
return GDALDataTypeUnion(eDT, eDT2);
}
/************************************************************************/
/* GetMinBitsForValue() */
/************************************************************************/
static int GetMinBitsForValue(double dValue)
{
if (round(dValue) == dValue)
{
if (dValue <= std::numeric_limits<GByte>::max() &&
dValue >= std::numeric_limits<GByte>::min())
return 8;
if (dValue <= std::numeric_limits<GInt8>::max() &&
dValue >= std::numeric_limits<GInt8>::min())
return 8;
if (dValue <= std::numeric_limits<GInt16>::max() &&
dValue >= std::numeric_limits<GInt16>::min())
return 16;
if (dValue <= std::numeric_limits<GUInt16>::max() &&
dValue >= std::numeric_limits<GUInt16>::min())
return 16;
if (dValue <= std::numeric_limits<GInt32>::max() &&
dValue >= std::numeric_limits<GInt32>::min())
return 32;
if (dValue <= std::numeric_limits<GUInt32>::max() &&
dValue >= std::numeric_limits<GUInt32>::min())
return 32;
if (dValue <= static_cast<double>(
std::numeric_limits<std::uint64_t>::max()) &&
dValue >=
static_cast<double>(std::numeric_limits<std::uint64_t>::min()))
return 64;
}
else if (static_cast<float>(dValue) == dValue)
{
return 32;
}
return 64;
}
/************************************************************************/
/* GDALFindDataType() */
/************************************************************************/
/**
* \brief Finds the smallest data type able to support the given
* requirements
*
* @param nBits number of bits necessary
* @param bSigned if negative values are necessary
* @param bFloating if non-integer values necessary
* @param bComplex if complex values are necessary
*
* @return a best fit GDALDataType for supporting the requirements
* @since GDAL 2.3
*/
GDALDataType CPL_STDCALL GDALFindDataType(int nBits, int bSigned, int bFloating,
int bComplex)
{
if (bComplex)
{
nBits = std::max(nBits, !bSigned ? 32 : 16);
} // we don't have complex unsigned data types, so for a complex uint16,
// promote to complex int32
if (bFloating)
{
nBits = std::max(nBits, 32);
}
if (nBits <= 8)
{
return bSigned ? GDT_Int8 : GDT_Byte;
}
if (nBits <= 16)
{
if (bComplex)
return GDT_CInt16;
if (bSigned)
return GDT_Int16;
return GDT_UInt16;
}
if (nBits <= 32)
{
if (bFloating)
{
if (bComplex)
return GDT_CFloat32;
return GDT_Float32;
}
if (bComplex)
return GDT_CInt32;
if (bSigned)
return GDT_Int32;
return GDT_UInt32;
}
if (nBits == 64 && !bFloating && !bComplex)
return bSigned ? GDT_Int64 : GDT_UInt64;
if (bComplex)
return GDT_CFloat64;
return GDT_Float64;
}
/************************************************************************/
/* GDALFindDataTypeForValue() */
/************************************************************************/
/**
* \brief Finds the smallest data type able to support the provided value
*
* @param dValue value to support
* @param bComplex is the value complex
*
* @return a best fit GDALDataType for supporting the value
* @since GDAL 2.3
*/
GDALDataType CPL_STDCALL GDALFindDataTypeForValue(double dValue, int bComplex)
{
const bool bFloating =
round(dValue) != dValue ||
dValue >
static_cast<double>(std::numeric_limits<std::uint64_t>::max()) ||
dValue <
static_cast<double>(std::numeric_limits<std::int64_t>::lowest());
const bool bSigned = bFloating || dValue < 0;
const int nBits = GetMinBitsForValue(dValue);
return GDALFindDataType(nBits, bSigned, bFloating, bComplex);
}
/************************************************************************/
/* GDALGetDataTypeSizeBytes() */
/************************************************************************/
/**
* \brief Get data type size in <b>bytes</b>.
*
* Returns the size of a GDT_* type in bytes. In contrast,
* GDALGetDataTypeSize() returns the size in <b>bits</b>.
*
* @param eDataType type, such as GDT_Byte.
* @return the number of bytes or zero if it is not recognised.
*/
int CPL_STDCALL GDALGetDataTypeSizeBytes(GDALDataType eDataType)
{
switch (eDataType)
{
case GDT_Byte:
case GDT_Int8:
return 1;
case GDT_UInt16:
case GDT_Int16:
return 2;
case GDT_UInt32:
case GDT_Int32:
case GDT_Float32:
case GDT_CInt16:
return 4;
case GDT_Float64:
case GDT_CInt32:
case GDT_CFloat32:
case GDT_UInt64:
case GDT_Int64:
return 8;
case GDT_CFloat64:
return 16;
case GDT_Unknown:
case GDT_TypeCount:
break;
}
return 0;
}
/************************************************************************/
/* GDALGetDataTypeSizeBits() */
/************************************************************************/
/**
* \brief Get data type size in <b>bits</b>.
*
* Returns the size of a GDT_* type in bits, <b>not bytes</b>! Use
* GDALGetDataTypeSizeBytes() for bytes.
*
* @param eDataType type, such as GDT_Byte.
* @return the number of bits or zero if it is not recognised.
*/
int CPL_STDCALL GDALGetDataTypeSizeBits(GDALDataType eDataType)
{
return GDALGetDataTypeSizeBytes(eDataType) * 8;
}
/************************************************************************/
/* GDALGetDataTypeSize() */
/************************************************************************/
/**
* \brief Get data type size in bits. <b>Deprecated</b>.
*
* Returns the size of a GDT_* type in bits, <b>not bytes</b>!
*
* Use GDALGetDataTypeSizeBytes() for bytes.
* Use GDALGetDataTypeSizeBits() for bits.
*
* @param eDataType type, such as GDT_Byte.
* @return the number of bits or zero if it is not recognised.
*/
int CPL_STDCALL GDALGetDataTypeSize(GDALDataType eDataType)
{
return GDALGetDataTypeSizeBytes(eDataType) * 8;
}
/************************************************************************/
/* GDALDataTypeIsComplex() */
/************************************************************************/
/**
* \brief Is data type complex?
*
* @return TRUE if the passed type is complex (one of GDT_CInt16, GDT_CInt32,
* GDT_CFloat32 or GDT_CFloat64), that is it consists of a real and imaginary
* component.
*/
int CPL_STDCALL GDALDataTypeIsComplex(GDALDataType eDataType)
{
switch (eDataType)
{
case GDT_CInt16:
case GDT_CInt32:
case GDT_CFloat32:
case GDT_CFloat64:
return TRUE;
case GDT_Byte:
case GDT_Int8:
case GDT_Int16:
case GDT_UInt16:
case GDT_Int32:
case GDT_UInt32:
case GDT_Int64:
case GDT_UInt64:
case GDT_Float32:
case GDT_Float64:
return FALSE;
case GDT_Unknown:
case GDT_TypeCount:
break;
}
return FALSE;
}
/************************************************************************/
/* GDALDataTypeIsFloating() */
/************************************************************************/
/**
* \brief Is data type floating? (might be complex)
*
* @return TRUE if the passed type is floating (one of GDT_Float32, GDT_Float64,
* GDT_CFloat32, GDT_CFloat64)
* @since GDAL 2.3
*/
int CPL_STDCALL GDALDataTypeIsFloating(GDALDataType eDataType)
{
switch (eDataType)
{
case GDT_Float32:
case GDT_Float64:
case GDT_CFloat32:
case GDT_CFloat64:
return TRUE;
case GDT_Byte:
case GDT_Int8:
case GDT_Int16:
case GDT_UInt16:
case GDT_Int32:
case GDT_UInt32:
case GDT_Int64:
case GDT_UInt64:
case GDT_CInt16:
case GDT_CInt32:
return FALSE;
case GDT_Unknown:
case GDT_TypeCount:
break;
}
return FALSE;
}
/************************************************************************/
/* GDALDataTypeIsInteger() */
/************************************************************************/
/**
* \brief Is data type integer? (might be complex)
*
* @return TRUE if the passed type is integer (one of GDT_Byte, GDT_Int16,
* GDT_UInt16, GDT_Int32, GDT_UInt32, GDT_CInt16, GDT_CInt32).
* @since GDAL 2.3
*/
int CPL_STDCALL GDALDataTypeIsInteger(GDALDataType eDataType)
{
switch (eDataType)
{
case GDT_Byte:
case GDT_Int8:
case GDT_Int16:
case GDT_UInt16:
case GDT_Int32:
case GDT_UInt32:
case GDT_CInt16:
case GDT_CInt32:
case GDT_UInt64:
case GDT_Int64:
return TRUE;
case GDT_Float32:
case GDT_Float64:
case GDT_CFloat32:
case GDT_CFloat64:
return FALSE;
case GDT_Unknown:
case GDT_TypeCount:
break;
}
return FALSE;
}
/************************************************************************/
/* GDALDataTypeIsSigned() */
/************************************************************************/
/**
* \brief Is data type signed?
*
* @return TRUE if the passed type is signed.
* @since GDAL 2.3
*/
int CPL_STDCALL GDALDataTypeIsSigned(GDALDataType eDataType)
{
switch (eDataType)
{
case GDT_Byte:
case GDT_UInt16:
case GDT_UInt32:
case GDT_UInt64:
return FALSE;
case GDT_Int8:
case GDT_Int16:
case GDT_Int32:
case GDT_Int64:
case GDT_Float32:
case GDT_Float64:
case GDT_CInt16:
case GDT_CInt32:
case GDT_CFloat32:
case GDT_CFloat64:
return TRUE;
case GDT_Unknown:
case GDT_TypeCount:
break;
}
return FALSE;
}
/************************************************************************/
/* GDALDataTypeIsConversionLossy() */
/************************************************************************/
/**
* \brief Is conversion from eTypeFrom to eTypeTo potentially lossy
*
* @param eTypeFrom input datatype
* @param eTypeTo output datatype
* @return TRUE if conversion from eTypeFrom to eTypeTo potentially lossy.
* @since GDAL 2.3
*/
int CPL_STDCALL GDALDataTypeIsConversionLossy(GDALDataType eTypeFrom,
GDALDataType eTypeTo)
{
// E.g cfloat32 -> float32
if (GDALDataTypeIsComplex(eTypeFrom) && !GDALDataTypeIsComplex(eTypeTo))
return TRUE;
eTypeFrom = GDALGetNonComplexDataType(eTypeFrom);
eTypeTo = GDALGetNonComplexDataType(eTypeTo);
if (GDALDataTypeIsInteger(eTypeTo))
{
// E.g. float32 -> int32
if (GDALDataTypeIsFloating(eTypeFrom))
return TRUE;
// E.g. Int16 to UInt16
const int bIsFromSigned = GDALDataTypeIsSigned(eTypeFrom);
const int bIsToSigned = GDALDataTypeIsSigned(eTypeTo);
if (bIsFromSigned && !bIsToSigned)
return TRUE;
// E.g UInt32 to UInt16
const int nFromSize = GDALGetDataTypeSize(eTypeFrom);
const int nToSize = GDALGetDataTypeSize(eTypeTo);
if (nFromSize > nToSize)
return TRUE;
// E.g UInt16 to Int16
if (nFromSize == nToSize && !bIsFromSigned && bIsToSigned)
return TRUE;
return FALSE;
}
if (eTypeTo == GDT_Float32 &&
(eTypeFrom == GDT_Int32 || eTypeFrom == GDT_UInt32 ||
eTypeFrom == GDT_Int64 || eTypeFrom == GDT_UInt64 ||
eTypeFrom == GDT_Float64))
{
return TRUE;
}
if (eTypeTo == GDT_Float64 &&
(eTypeFrom == GDT_Int64 || eTypeFrom == GDT_UInt64))
{
return TRUE;
}
return FALSE;
}
/************************************************************************/
/* GDALGetDataTypeName() */
/************************************************************************/
/**
* \brief Get name of data type.
*
* Returns a symbolic name for the data type. This is essentially the
* the enumerated item name with the GDT_ prefix removed. So GDT_Byte returns
* "Byte". The returned strings are static strings and should not be modified
* or freed by the application. These strings are useful for reporting
* datatypes in debug statements, errors and other user output.
*
* @param eDataType type to get name of.
* @return string corresponding to existing data type
* or NULL pointer if invalid type given.
*/
const char *CPL_STDCALL GDALGetDataTypeName(GDALDataType eDataType)
{
switch (eDataType)
{
case GDT_Unknown:
return "Unknown";
case GDT_Byte:
return "Byte";
case GDT_Int8:
return "Int8";
case GDT_UInt16:
return "UInt16";
case GDT_Int16:
return "Int16";
case GDT_UInt32:
return "UInt32";
case GDT_Int32:
return "Int32";
case GDT_UInt64:
return "UInt64";
case GDT_Int64:
return "Int64";
case GDT_Float32:
return "Float32";
case GDT_Float64:
return "Float64";
case GDT_CInt16:
return "CInt16";
case GDT_CInt32:
return "CInt32";
case GDT_CFloat32:
return "CFloat32";
case GDT_CFloat64:
return "CFloat64";
case GDT_TypeCount:
break;
}
return nullptr;
}
/************************************************************************/
/* GDALGetDataTypeByName() */
/************************************************************************/
/**
* \brief Get data type by symbolic name.
*
* Returns a data type corresponding to the given symbolic name. This
* function is opposite to the GDALGetDataTypeName().
*
* @param pszName string containing the symbolic name of the type.
*
* @return GDAL data type.
*/
GDALDataType CPL_STDCALL GDALGetDataTypeByName(const char *pszName)
{
VALIDATE_POINTER1(pszName, "GDALGetDataTypeByName", GDT_Unknown);
for (int iType = 1; iType < GDT_TypeCount; iType++)
{
const auto eType = static_cast<GDALDataType>(iType);
if (GDALGetDataTypeName(eType) != nullptr &&
EQUAL(GDALGetDataTypeName(eType), pszName))
{
return eType;
}
}
return GDT_Unknown;
}
/************************************************************************/
/* GDALAdjustValueToDataType() */
/************************************************************************/
template <class T>
static inline void ClampAndRound(double &dfValue, bool &bClamped,
bool &bRounded)
{
// TODO(schwehr): Rework this template. ::min() versus ::lowest.
if (dfValue < static_cast<double>(std::numeric_limits<T>::min()))
{
bClamped = true;
dfValue = static_cast<double>(std::numeric_limits<T>::min());
}
else if (dfValue > static_cast<double>(std::numeric_limits<T>::max()))
{
bClamped = true;
dfValue = static_cast<double>(std::numeric_limits<T>::max());
}
else if (dfValue != static_cast<double>(static_cast<T>(dfValue)))
{
bRounded = true;
dfValue = static_cast<double>(static_cast<T>(floor(dfValue + 0.5)));
}
}
/**
* \brief Adjust a value to the output data type
*
* Adjustment consist in clamping to minimum/maximum values of the data type
* and rounding for integral types.
*
* @param eDT target data type.
* @param dfValue value to adjust.
* @param pbClamped pointer to a integer(boolean) to indicate if clamping has
* been made, or NULL
* @param pbRounded pointer to a integer(boolean) to indicate if rounding has
* been made, or NULL
*
* @return adjusted value
* @since GDAL 2.1
*/
double GDALAdjustValueToDataType(GDALDataType eDT, double dfValue,
int *pbClamped, int *pbRounded)
{
bool bClamped = false;
bool bRounded = false;
switch (eDT)
{
case GDT_Byte:
ClampAndRound<GByte>(dfValue, bClamped, bRounded);
break;
case GDT_Int8:
ClampAndRound<GInt8>(dfValue, bClamped, bRounded);
break;
case GDT_Int16:
ClampAndRound<GInt16>(dfValue, bClamped, bRounded);
break;
case GDT_UInt16:
ClampAndRound<GUInt16>(dfValue, bClamped, bRounded);
break;
case GDT_Int32:
ClampAndRound<GInt32>(dfValue, bClamped, bRounded);
break;
case GDT_UInt32:
ClampAndRound<GUInt32>(dfValue, bClamped, bRounded);
break;
case GDT_Int64:
ClampAndRound<std::int64_t>(dfValue, bClamped, bRounded);
break;
case GDT_UInt64:
ClampAndRound<std::uint64_t>(dfValue, bClamped, bRounded);
break;
case GDT_Float32:
{
if (!std::isfinite(dfValue))
break;
// TODO(schwehr): ::min() versus ::lowest.
// Use ClampAndRound after it has been fixed.
if (dfValue < -std::numeric_limits<float>::max())
{
bClamped = TRUE;
dfValue =
static_cast<double>(-std::numeric_limits<float>::max());
}
else if (dfValue > std::numeric_limits<float>::max())
{
bClamped = TRUE;
dfValue =
static_cast<double>(std::numeric_limits<float>::max());
}
else
{
// Intentionally loose precision.
// TODO(schwehr): Is the double cast really necessary?
// If so, why? What will fail?
dfValue = static_cast<double>(static_cast<float>(dfValue));
}
break;
}
case GDT_Float64:
case GDT_CInt16:
case GDT_CInt32:
case GDT_CFloat32:
case GDT_CFloat64:
case GDT_Unknown:
case GDT_TypeCount:
break;
}
if (pbClamped)
*pbClamped = bClamped;
if (pbRounded)
*pbRounded = bRounded;
return dfValue;
}
/************************************************************************/
/* GDALIsValueExactAs() */
/************************************************************************/
/**
* \brief Check whether the provided value can be exactly represented in a
* data type.
*
* Only implemented for non-complex data types
*
* @param dfValue value to check.
* @param eDT target data type.
*
* @return true if the provided value can be exactly represented in the
* data type.
* @since GDAL 3.10
*/
bool GDALIsValueExactAs(double dfValue, GDALDataType eDT)
{
switch (eDT)
{
case GDT_Byte:
return GDALIsValueExactAs<uint8_t>(dfValue);
case GDT_Int8:
return GDALIsValueExactAs<int8_t>(dfValue);
case GDT_UInt16:
return GDALIsValueExactAs<uint16_t>(dfValue);
case GDT_Int16:
return GDALIsValueExactAs<int16_t>(dfValue);
case GDT_UInt32:
return GDALIsValueExactAs<uint32_t>(dfValue);
case GDT_Int32:
return GDALIsValueExactAs<int32_t>(dfValue);
case GDT_UInt64:
return GDALIsValueExactAs<uint64_t>(dfValue);
case GDT_Int64:
return GDALIsValueExactAs<int64_t>(dfValue);
case GDT_Float32:
return GDALIsValueExactAs<float>(dfValue);
case GDT_Float64:
return true;
case GDT_Unknown:
case GDT_CInt16:
case GDT_CInt32:
case GDT_CFloat32:
case GDT_CFloat64:
case GDT_TypeCount:
break;
}
return true;
}
/************************************************************************/
/* GDALGetNonComplexDataType() */
/************************************************************************/
/**
* \brief Return the base data type for the specified input.
*
* If the input data type is complex this function returns the base type
* i.e. the data type of the real and imaginary parts (non-complex).
* If the input data type is already non-complex, then it is returned
* unchanged.