[postgis-commits] svn - r3642 - spike/wktraster/rt_core
postgis-commits at postgis.refractions.net
postgis-commits at postgis.refractions.net
Wed Feb 4 18:46:56 PST 2009
Author: strk
Date: 2009-02-04 18:46:56 -0800 (Wed, 04 Feb 2009)
New Revision: 3642
Modified:
spike/wktraster/rt_core/rt_api.c
spike/wktraster/rt_core/rt_api.h
Log:
Change set_pixe/set_nodata signatures to return an error when passed an out-of-range value
Modified: spike/wktraster/rt_core/rt_api.c
===================================================================
--- spike/wktraster/rt_core/rt_api.c 2009-02-04 18:13:44 UTC (rev 3641)
+++ spike/wktraster/rt_core/rt_api.c 2009-02-05 02:46:56 UTC (rev 3642)
@@ -433,31 +433,22 @@
}
#endif /* OPTIMIZE_SPACE */
-void
+int
rt_band_set_nodata(rt_context ctx, rt_band band, double val)
{
rt_pixtype pixtype = band->pixtype;
- /* truncate as needed */
+ /* return -1 on out of range */
switch (pixtype)
{
default:
ctx->err("Unknown pixeltype %d", pixtype);
- break;
+ return -1;
case PT_1BB:
{
uint8_t v = val;
v &= 1;
band->nodataval = v;
-#ifdef RT_WARN_ON_TRUNCATION
- if ( band->nodataval != val )
- {
- printf("Nodata value for %s band got truncated"
- " from %g to %g\n",
- rt_pixtype_name(ctx, pixtype),
- val, band->nodataval);
- }
-#endif
break;
}
case PT_2BUI:
@@ -465,15 +456,6 @@
uint8_t v = val;
v &= 3;
band->nodataval = v;
-#ifdef RT_WARN_ON_TRUNCATION
- if ( band->nodataval != val )
- {
- printf("Nodata value for %s band got truncated"
- " from %g to %g\n",
- rt_pixtype_name(ctx, pixtype),
- val, band->nodataval);
- }
-#endif
break;
}
case PT_4BUI:
@@ -481,30 +463,13 @@
uint8_t v = val;
v &= 15;
band->nodataval = v;
-#ifdef RT_WARN_ON_TRUNCATION
- if ( band->nodataval != val )
- {
- printf("Nodata value for %s band got truncated"
- " from %g to %g\n",
- rt_pixtype_name(ctx, pixtype),
- val, band->nodataval);
- }
-#endif
+ return -1;
break;
}
case PT_8BSI:
{
int8_t v = val;
band->nodataval = v;
-#ifdef RT_WARN_ON_TRUNCATION
- if ( band->nodataval != val )
- {
- printf("Nodata value for %s band got truncated"
- " from %g to %g\n",
- rt_pixtype_name(ctx, pixtype),
- val, band->nodataval);
- }
-#endif
break;
}
@@ -512,15 +477,6 @@
{
uint8_t v = val;
band->nodataval = v;
-#ifdef RT_WARN_ON_TRUNCATION
- if ( band->nodataval != val )
- {
- printf("Nodata value for %s band got truncated"
- " from %g to %g\n",
- rt_pixtype_name(ctx, pixtype),
- val, band->nodataval);
- }
-#endif
break;
}
@@ -528,15 +484,6 @@
{
int16_t v = val;
band->nodataval = v;
-#ifdef RT_WARN_ON_TRUNCATION
- if ( band->nodataval != val )
- {
- printf("Nodata value for %s band got truncated"
- " from %g to %g\n",
- rt_pixtype_name(ctx, pixtype),
- val, band->nodataval);
- }
-#endif
break;
}
@@ -544,15 +491,6 @@
{
uint16_t v = val;
band->nodataval = v;
-#ifdef RT_WARN_ON_TRUNCATION
- if ( band->nodataval != val )
- {
- printf("Nodata value for %s band got truncated"
- " from %g to %g\n",
- rt_pixtype_name(ctx, pixtype),
- val, band->nodataval);
- }
-#endif
break;
}
@@ -560,15 +498,6 @@
{
int32_t v = val;
band->nodataval = v;
-#ifdef RT_WARN_ON_TRUNCATION
- if ( band->nodataval != val )
- {
- printf("Nodata value for %s band got truncated"
- " from %g to %g\n",
- rt_pixtype_name(ctx, pixtype),
- val, band->nodataval);
- }
-#endif
break;
}
@@ -576,35 +505,18 @@
{
uint32_t v = val;
band->nodataval = v;
-#ifdef RT_WARN_ON_TRUNCATION
- if ( band->nodataval != val )
- {
- printf("Nodata value for %s band got truncated"
- " from %g to %g\n",
- rt_pixtype_name(ctx, pixtype),
- val, band->nodataval);
- }
-#endif
break;
}
case PT_16BF:
ctx->err("16BF pixel type not supported yet");
+ return -1;
break;
case PT_32BF:
{
float v = val;
band->nodataval = v;
-#ifdef RT_WARN_ON_TRUNCATION
- if ( fabs(band->nodataval-val) > 0.0001 )
- {
- printf("Nodata value for %s band got truncated"
- " from %g to %g\n",
- rt_pixtype_name(ctx, pixtype),
- val, band->nodataval);
- }
-#endif
break;
}
@@ -612,6 +524,19 @@
band->nodataval = val;
break;
}
+
+ if ( fabs(band->nodataval-val) > 0.0001 )
+ {
+#ifdef RT_WARN_ON_TRUNCATION
+ ctx->warn("Nodata value for %s band got truncated"
+ " from %g to %g\n",
+ rt_pixtype_name(ctx, pixtype),
+ val, band->nodataval);
+#endif
+ return -1;
+ }
+
+ return 0;
}
int
@@ -621,17 +546,18 @@
rt_pixtype pixtype = band->pixtype;
unsigned char* data;
uint32_t offset;
+ double checkval;
if ( x >= band->width || y >= band->height )
{
ctx->err("Coordinates ouf of range");
- return 0;
+ return -1;
}
if ( band->offline )
{
ctx->err("rt_band_set_pixel not implemented for OFFDB bands");
- return 0;
+ return -1;
}
data = rt_band_get_data(ctx, band);
@@ -642,109 +568,42 @@
default:
{
ctx->err("Unknown pixeltype %d", pixtype);
- return 0;
+ return -1;
}
case PT_1BB:
{
-#ifdef OPTIMIZE_SPACE
- int byteOffset = offset/8;
- int bitOffset = offset%8;
- data += byteOffset;
-
- /* Bit to set is bitOffset into data */
- setBits(data, val, 1, bitOffset);
-
-#else
data[offset] = (int)val&0x01;
-
-#ifdef RT_WARN_ON_TRUNCATION
- /* Consistency checking */
- if ( data[offset] != val ) {
- ctx->warn("Pixel value for 8BSI or 8BUI band got truncated"
- " from %g to %hhu", val, *data);
- }
-#endif /* RT_WARN_ON_TRUNCATION */
-#endif
- return 1;
+ checkval = data[offset];
+ break;
}
case PT_2BUI:
{
-#ifdef OPTIMIZE_SPACE
- int byteOffset = offset/4;
- int bitOffset = offset%4;
- data += byteOffset;
-
- /* Bits to set start at bitOffset into data */
- setBits(data, val, 2, bitOffset);
-
-#else
data[offset] = (int)val&0x03;
-
-#ifdef RT_WARN_ON_TRUNCATION
- /* Consistency checking */
- if ( data[offset] != val ) {
- ctx->warn("Pixel value for 8BSI or 8BUI band got truncated"
- " from %g to %hhu", val, *data);
- }
-#endif /* RT_WARN_ON_TRUNCATION */
-#endif
- return 1;
+ checkval = data[offset];
+ break;
}
case PT_4BUI:
{
-#ifdef OPTIMIZE_SPACE
- int byteOffset = offset/2;
- int bitOffset = offset%2;
- data += byteOffset;
-
- /* Bits to set start at bitOffset into data */
- setBits(data, val, 2, bitOffset);
-
-#else
data[offset] = (int)val&0x0F;
-
-#ifdef RT_WARN_ON_TRUNCATION
- /* Consistency checking */
- if ( data[offset] != val ) {
- ctx->warn("Pixel value for 8BSI or 8BUI band got truncated"
- " from %g to %hhu", val, *data);
- }
-#endif /* RT_WARN_ON_TRUNCATION */
-#endif
- return 1;
+ checkval = data[offset];
+ break;
}
case PT_8BSI:
{
data[offset] = (int8_t)val;
-
-#ifdef RT_WARN_ON_TRUNCATION
- /* Consistency checking */
- if ( data[offset] != val ) {
- ctx->warn("Pixel value for 8BSI band got truncated"
- " from %g to %hhu", val, *data);
- }
-#endif /* RT_WARN_ON_TRUNCATION */
-
- return 1;
+ checkval = data[offset];
+ break;
}
case PT_8BUI:
{
data[offset] = val;
-
-#ifdef RT_WARN_ON_TRUNCATION
- /* Consistency checking */
- if ( data[offset] != val ) {
- ctx->warn("Pixel value for 8BUI band got truncated"
- " from %g to %hhu", val, *data);
- }
-#endif /* RT_WARN_ON_TRUNCATION */
-
- return 1;
+ checkval = data[offset];
+ break;
}
case PT_16BSI:
@@ -752,22 +611,14 @@
{
uint16_t *ptr = (uint16_t*)data; /* we assume correct alignment */
ptr[offset] = val;
-
-#ifdef RT_WARN_ON_TRUNCATION
- /* Consistency checking */
- if ( ptr[offset] != val ) {
- ctx->warn("Pixel value for 16BSI or 16BUI band got truncated"
- " from %g to %hu", val, *ptr);
- }
-#endif /* RT_WARN_ON_TRUNCATION */
-
- return 1;
+ checkval = ptr[offset];
+ break;
}
case PT_16BF:
{
ctx->err("16BF pixel type not supported yet");
- return 0;
+ return -1;
}
case PT_32BSI:
@@ -775,43 +626,40 @@
{
uint32_t *ptr = (uint32_t*)data; /* we assume correct alignment */
ptr[offset] = val;
-
-#ifdef RT_WARN_ON_TRUNCATION
- /* Consistency checking */
- if ( ptr[offset] != val ) {
- ctx->warn("Pixel value for 32BSI or 32BUI band got truncated"
- " from %g to %u", val, *ptr);
- }
-#endif /* RT_WARN_ON_TRUNCATION */
-
- return 1;
+ checkval = ptr[offset];
+ break;
}
case PT_32BF:
{
float *ptr = (float*)data; /* we assume correct alignment */
ptr[offset] = val;
-
-#ifdef RT_WARN_ON_TRUNCATION
- /* Consistency checking */
- if ( fabs(ptr[offset]-val) > 0.0001 )
- {
- ctx->warn("Pixel value for 32BF band got truncated"
- " from %g to %u", val, *ptr);
- }
-#endif /* RT_WARN_ON_TRUNCATION */
-
- return 1;
+ checkval = ptr[offset];
+ break;
}
case PT_64BF:
{
double *ptr = (double*)data; /* we assume correct alignment */
ptr[offset] = val;
-
- return 1;
+ checkval = ptr[offset];
+ break;
}
}
+
+ /* Overflow checking */
+ if ( fabs(checkval-val) > 0.0001 )
+ {
+#ifdef RT_WARN_ON_TRUNCATION
+ ctx->warn("Pixel value for %s band got truncated"
+ " from %g to %u",
+ rt_pixtype_name(band->pixtype),
+ val, checkval);
+#endif /* RT_WARN_ON_TRUNCATION */
+ return -1;
+ }
+
+ return 0;
}
double
Modified: spike/wktraster/rt_core/rt_api.h
===================================================================
--- spike/wktraster/rt_core/rt_api.h 2009-02-04 18:13:44 UTC (rev 3641)
+++ spike/wktraster/rt_core/rt_api.h 2009-02-05 02:46:56 UTC (rev 3642)
@@ -189,10 +189,15 @@
/* Set nodata value
* @param ctx : context, for thread safety
* @param band : the band to set nodata value to
- * @param val : the nodata value, will be appropriately
- * truncated to fit for the pixeltype.
+ * @param val : the nodata value, must be in the range
+ * of values supported by this band's pixeltype
+ * or an error will be printed and non-zero
+ * returned.
+ *
+ * @return 0 on success, -1 on error (value out of valid range).
+ *
*/
-void rt_band_set_nodata(rt_context ctx, rt_band band, double val);
+int rt_band_set_nodata(rt_context ctx, rt_band band, double val);
/* Get nodata value
* @param ctx : context, for thread safety
@@ -206,10 +211,12 @@
* @param band : the band to set nodata value to
* @param x : x ordinate
* @param y : x ordinate
- * @param val : the pixel value, will be appropriately
- * truncated to fit for the pixeltype.
- * @return 1 on success, 0 on error.
- * Truncation won't be considered an error.
+ * @param val : the pixel value, must be in the range
+ * of values supported by this band's pixeltype
+ * or an error will be printed and non-zero
+ * returned.
+ *
+ * @return 0 on success, -1 on error (value out of valid range).
*/
int rt_band_set_pixel(rt_context ctx, rt_band band,
uint16_t x, uint16_t y, double val);
More information about the postgis-commits
mailing list