52 return (int8_t)fmin(fmax((value), SCHAR_MIN), SCHAR_MAX);
57 return (uint8_t)fmin(fmax((value), 0), UCHAR_MAX);
62 return (int16_t)fmin(fmax((value), SHRT_MIN), SHRT_MAX);
67 return (uint16_t)fmin(fmax((value), 0), USHRT_MAX);
72 return (int32_t)fmin(fmax((value), INT_MIN), INT_MAX);
77 return (uint32_t)fmin(fmax((value), 0), UINT_MAX);
84 return (
float)fmin(fmax((value), -FLT_MAX), FLT_MAX);
114 uint32_t sign = (v.u >> 16) & 0x8000U;
115 uint32_t mantissa = v.u & 0x7fffffU;
116 int32_t exponent = ((int32_t)((v.u >> 23) & 0xffU)) - 127 + 15;
121 return (uint16_t)sign;
123 mantissa = (mantissa | 0x800000U) >> (uint32_t)(1 - exponent);
124 return (uint16_t)(sign | ((mantissa + 0x1000U) >> 13));
128 return (uint16_t)(sign | 0x7c00U | (mantissa ? 0x200U : 0));
130 return (uint16_t)(sign | ((uint32_t)exponent << 10) | ((mantissa + 0x1000U) >> 13));
141 uint32_t sign = (uint32_t)(value & 0x8000U) << 16;
142 uint32_t exponent = (value >> 10) & 0x1fU;
143 uint32_t mantissa = value & 0x3ffU;
154 while ((mantissa & 0x400U) == 0)
160 v.u = sign | ((exponent + 127 - 15) << 23) | (mantissa << 13);
163 else if (exponent == 31)
165 v.u = sign | 0x7f800000U | (mantissa << 13);
169 v.u = sign | ((exponent + 127 - 15) << 23) | (mantissa << 13);
184 assert(algname != NULL && strlen(algname) > 0);
186 if (strcmp(algname,
"NEARESTNEIGHBOUR") == 0)
187 return GRA_NearestNeighbour;
188 else if (strcmp(algname,
"NEARESTNEIGHBOR") == 0)
189 return GRA_NearestNeighbour;
190 else if (strcmp(algname,
"BILINEAR") == 0)
192 else if (strcmp(algname,
"CUBICSPLINE") == 0)
193 return GRA_CubicSpline;
194 else if (strcmp(algname,
"CUBIC") == 0)
196 else if (strcmp(algname,
"LANCZOS") == 0)
198 else if (strcmp(algname,
"MAX") == 0)
200 else if (strcmp(algname,
"MIN") == 0)
202 return GRA_NearestNeighbour;
221#if POSTGIS_GDAL_VERSION >= 30700
232#if POSTGIS_GDAL_VERSION >= 31100 && defined(GDT_Float16)
259#if POSTGIS_GDAL_VERSION >= 30700
275#if POSTGIS_GDAL_VERSION >= 31100 && defined(GDT_Float16)
291 if (NULL == request || !strlen(request))
292 return GDALVersionInfo(
"RELEASE_NAME");
294 return GDALVersionInfo(request);
302 assert(name != NULL && strlen(name) > 0);
304 if (strcmp(name,
"UNION") == 0)
306 else if (strcmp(name,
"FIRST") == 0)
308 else if (strcmp(name,
"SECOND") == 0)
310 else if (strcmp(name,
"LAST") == 0)
312 else if (strcmp(name,
"CUSTOM") == 0)
323 OGRSpatialReferenceH hsrs;
328 hsrs = OSRNewSpatialReference(NULL);
329 if (OSRSetFromUserInput(hsrs, srs) == OGRERR_NONE) {
331 OSRExportToProj4(hsrs, &rtn);
333 OSRExportToWkt(hsrs, &rtn);
336 rterror(
"rt_util_gdal_convert_sr: Could not process the provided srs: %s", srs);
340 OSRDestroySpatialReference(hsrs);
342 rterror(
"rt_util_gdal_convert_sr: Could not process the provided srs: %s", srs);
354 OGRSpatialReferenceH hsrs;
355 OGRErr rtn = OGRERR_NONE;
359 hsrs = OSRNewSpatialReference(NULL);
360 rtn = OSRSetFromUserInput(hsrs, srs);
361 OSRDestroySpatialReference(hsrs);
363 if (rtn == OGRERR_NONE)
381 const char *srs = NULL;
383 assert(authname != NULL);
384 assert(authcode != NULL);
389 srs = GDALGetProjectionRef(hds);
390 if (srs != NULL && srs[0] !=
'\0') {
391 OGRSpatialReferenceH hSRS = OSRNewSpatialReference(NULL);
393 if (OSRSetFromUserInput(hSRS, srs) == OGRERR_NONE) {
394 const char* pszAuthorityName = OSRGetAuthorityName(hSRS, NULL);
395 const char* pszAuthorityCode = OSRGetAuthorityCode(hSRS, NULL);
397 if (pszAuthorityName != NULL && pszAuthorityCode != NULL) {
398 size_t authorityName_len = strlen(pszAuthorityName) +1;
399 size_t authorityCode_len = strlen(pszAuthorityCode) + 1;
400 *authname =
rtalloc(
sizeof(
char) * authorityName_len);
401 *authcode =
rtalloc(
sizeof(
char) * authorityCode_len);
403 if (*authname == NULL || *authcode == NULL) {
404 rterror(
"rt_util_gdal_sr_auth_info: Could not allocate memory for auth name and code");
405 if (*authname != NULL)
rtdealloc(*authname);
406 if (*authcode != NULL)
rtdealloc(*authcode);
407 OSRDestroySpatialReference(hSRS);
411 strncpy(*authname, pszAuthorityName, authorityName_len);
412 strncpy(*authcode, pszAuthorityCode, authorityCode_len);
416 OSRDestroySpatialReference(hSRS);
445 static int registered = 0;
447 if (registered && !force_register_all) {
448 RASTER_DEBUG(3,
"Already called once... not calling GDALAllRegister");
464 int count = GDALGetDriverCount();
466 GDALDriverH hdrv = NULL;
468 if (drv == NULL || !strlen(drv) || count < 1)
471 for (i = 0; i < count; i++) {
472 hdrv = GDALGetDriver(i);
473 if (hdrv == NULL)
continue;
475 if (strcmp(drv, GDALGetDriverShortName(hdrv)) == 0)
493 GDALAccess fn_access,
496 char *vsi_options_str =
rtoptions(
"gdal_vsi_options");
499 if (vsi_options_str && strlen(vsi_options_str) > 0) {
502 rtinfo(
"postgis.gdal_vsi_options is set");
503 memset(olist, 0,
sizeof(olist));
508 for (i = 0; i < sz; i += 2)
510 char *key = olist[i];
511 char *val = olist[i+1];
514 if (strcmp(key,
"gdal_skip") == 0) {
515 rtwarn(
"Unable to set GDAL_SKIP config option");
518 rtinfo(
"CPLSetConfigOption(%s)", key);
519 CPLSetConfigOption(key, val);
524 unsigned int open_flags;
529 rterror(
"rt_util_gdal_open: Cannot open file. All GDAL drivers disabled");
536 (strstr(fn,
"/vsi") != NULL) &&
537 (strstr(fn,
"/vsimem") == NULL) &&
545 open_flags = GDAL_OF_RASTER
546 | GDAL_OF_VERBOSE_ERROR
547 | (fn_access == GA_Update ? GDAL_OF_UPDATE : 0)
548 | (shared ? GDAL_OF_SHARED : 0);
550 return GDALOpenEx(fn ,
565 ext->
MinX = env.MinX;
566 ext->
MaxX = env.MaxX;
567 ext->
MinY = env.MinY;
568 ext->
MaxY = env.MaxY;
581 env->MinX = ext.
MinX;
582 env->MaxX = ext.
MaxX;
583 env->MinY = ext.
MinY;
584 env->MaxY = ext.
MaxY;
598 rterror(
"rt_util_envelope_to_lwpoly: Out of memory building envelope's geometry");
603 rterror(
"rt_util_envelope_to_lwpoly: Out of memory building envelope's geometry ring");
632 rterror(
"rt_util_envelope_to_lwpoly: Could not build envelope's geometry");
643 if (gt1 == NULL || gt2 == NULL)
646 for (k = 0; k < 6; k++) {
670 for (i = 1; i < 3; i++) {
687 rc = (maxc - rgb[0]) / diff;
688 gc = (maxc - rgb[1]) / diff;
689 bc = (maxc - rgb[2]) / diff;
693 else if (
DBL_EQ(rgb[1], maxc))
698 h = modf((h / 6.0), &junk);
727 i = floor(hsv[0] * 6.);
728 f = (hsv[0] * 6.0) - i;
729 p = v * (1. - hsv[1]);
730 q = v * (1. - hsv[1] * f);
731 t = v * (1. - hsv[1] * (1. - f));
780 int32_t checkvalint, uint32_t checkvaluint,
781 float checkvalfloat,
double checkvaldouble,
795 if (fabs(checkvalint - initialvalue) >= 1) {
796#if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
797 rtwarn(
"Value set for %s band got clamped from %f to %d",
799 initialvalue, checkvalint
804 else if (checkvalint != initialvalue)
806#if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
807 rtwarn(
"Value set for %s band got truncated from %f to %d",
809 initialvalue, checkvalint
817 if (fabs(checkvaluint - initialvalue) >= 1)
819#if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
820 rtwarn(
"Value set for %s band got clamped from %f to %u",
827 else if (checkvaluint != initialvalue)
829#if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
830 rtwarn(
"Value set for %s band got truncated from %f to %u",
832 initialvalue, checkvaluint
845 if (
FLT_NEQ(checkvalfloat, initialvalue)) {
846#if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
847 rtwarn(
"Value set for %s band got converted from %f to %f",
849 initialvalue, checkvalfloat
857 if (
FLT_NEQ(checkvaldouble, initialvalue)) {
858#if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
859 rtwarn(
"Value set for %s band got converted from %f to %f",
861 initialvalue, checkvaldouble
char result[OUT_DOUBLE_BUFFER_SIZE]
LWPOLY * lwpoly_construct(int32_t srid, GBOX *bbox, uint32_t nrings, POINTARRAY **points)
#define SRID_UNKNOWN
Unknown SRID value.
void ptarray_set_point4d(POINTARRAY *pa, uint32_t n, const POINT4D *p4d)
POINTARRAY * ptarray_construct(char hasz, char hasm, uint32_t npoints)
Construct an empty pointarray, allocating storage and setting the npoints, but not filling in any inf...
void rterror(const char *fmt,...) __attribute__((format(printf
Wrappers used for reporting errors and info.
void * rtalloc(size_t size)
Wrappers used for managing memory.
#define RASTER_DEBUG(level, msg)
void void rtinfo(const char *fmt,...) __attribute__((format(printf
void void void rtwarn(const char *fmt,...) __attribute__((format(printf
#define POSTGIS_RT_4BUIMAX
const char * rt_pixtype_name(rt_pixtype pixtype)
void void void char * rtoptions(const char *varname)
Wrappers used for options.
rt_errorstate
Enum definitions.
#define POSTGIS_RT_16F_MAX
#define POSTGIS_RT_2BUIMAX
#define POSTGIS_RT_1BBMAX
void rtdealloc(void *mem)
This library is the generic raster handling section of PostGIS.
size_t option_list_length(char **olist)
Returns the total number of keys and values in the list.
void option_list_parse(char *input, char **olist)
option_list is a null-terminated list of strings, where every odd string is a key and every even stri...
rt_pixtype rt_util_gdal_datatype_to_pixtype(GDALDataType gdt)
Convert GDALDataType to rt_pixtype.
rt_errorstate rt_util_rgb_to_hsv(double rgb[3], double hsv[3])
int rt_util_gdal_register_all(int force_register_all)
int rt_util_gdal_configured(void)
int8_t rt_util_clamp_to_8BSI(double value)
uint8_t rt_util_clamp_to_1BB(double value)
float rt_util_clamp_to_16F(double value)
int32_t rt_util_clamp_to_32BSI(double value)
uint16_t rt_util_float_to_float16(float value)
float rt_util_float16_to_float(uint16_t value)
char * rt_util_gdal_convert_sr(const char *srs, int proj4)
rt_extenttype rt_util_extent_type(const char *name)
int rt_util_dbl_trunc_warning(double initialvalue, int32_t checkvalint, uint32_t checkvaluint, float checkvalfloat, double checkvaldouble, rt_pixtype pixtype)
GDALDataType rt_util_pixtype_to_gdal_datatype(rt_pixtype pt)
Convert rt_pixtype to GDALDataType.
uint8_t rt_util_clamp_to_2BUI(double value)
const char * rt_util_gdal_version(const char *request)
uint8_t rt_util_clamp_to_8BUI(double value)
GDALDatasetH rt_util_gdal_open(const char *fn, GDALAccess fn_access, int shared)
int rt_util_gdal_supported_sr(const char *srs)
int16_t rt_util_clamp_to_16BSI(double value)
GDALResampleAlg rt_util_gdal_resample_alg(const char *algname)
Convert cstring name to GDAL Resample Algorithm.
int rt_util_gdal_driver_registered(const char *drv)
uint8_t rt_util_clamp_to_4BUI(double value)
rt_errorstate rt_util_hsv_to_rgb(double hsv[3], double rgb[3])
void rt_util_to_ogr_envelope(rt_envelope ext, OGREnvelope *env)
LWPOLY * rt_util_envelope_to_lwpoly(rt_envelope env)
rt_errorstate rt_util_gdal_sr_auth_info(GDALDatasetH hds, char **authname, char **authcode)
Get auth name and code.
uint16_t rt_util_clamp_to_16BUI(double value)
uint32_t rt_util_clamp_to_32BUI(double value)
char * gdal_enabled_drivers
int rt_util_same_geotransform_matrix(double *gt1, double *gt2)
float rt_util_clamp_to_32F(double value)
void rt_util_from_ogr_envelope(OGREnvelope env, rt_envelope *ext)