PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ rt_band_set_pixel()

rt_errorstate rt_band_set_pixel ( rt_band  band,
int  x,
int  y,
double  val,
int *  converted 
)

Set single pixel's value.

Parameters
band: the band to set value to
x: pixel column (0-based)
y: pixel row (0-based)
val: the pixel value
converted: (optional) non-zero if value truncated/clamped/converted
Returns
ES_NONE on success, ES_ERROR on error
Parameters
band: the band to set value to
x: x ordinate (0-based)
y: y ordinate (0-based)
val: the pixel value
converted: (optional) non-zero if value truncated/clamped/converted
Returns
ES_NONE on success, ES_ERROR on error

Definition at line 1140 of file rt_band.c.

1145 {
1146 rt_pixtype pixtype = PT_END;
1147 uint8_t *data = NULL;
1148 uint32_t offset = 0;
1149
1150 int32_t checkvalint = 0;
1151 uint32_t checkvaluint = 0;
1152 float checkvalfloat = 0;
1153 double checkvaldouble = 0;
1154
1155 assert(NULL != band);
1156
1157 if (converted != NULL)
1158 *converted = 0;
1159
1160 if (band->offline) {
1161 rterror("rt_band_set_pixel not implemented yet for OFFDB bands");
1162 return ES_ERROR;
1163 }
1164
1165 pixtype = band->pixtype;
1166
1167 if (
1168 x < 0 || x >= band->width ||
1169 y < 0 || y >= band->height
1170 ) {
1171 rterror("rt_band_set_pixel: Coordinates out of range");
1172 return ES_ERROR;
1173 }
1174
1175 /* check that clamped value isn't clamped NODATA */
1176 if (band->hasnodata && pixtype != PT_64BF) {
1177 double newval;
1178 int corrected;
1179
1180 rt_band_corrected_clamped_value(band, val, &newval, &corrected);
1181
1182 if (corrected) {
1183#if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
1184 rtwarn("Value for pixel %d x %d has been corrected as clamped value becomes NODATA", x, y);
1185#endif
1186 val = newval;
1187
1188 if (converted != NULL)
1189 *converted = 1;
1190 }
1191 }
1192
1193 data = rt_band_get_data(band);
1194 offset = x + (y * band->width);
1195
1196 switch (pixtype)
1197 {
1198 case PT_1BB: {
1199 data[offset] = rt_util_clamp_to_1BB(val);
1200 checkvalint = data[offset];
1201 break;
1202 }
1203 case PT_2BUI: {
1204 data[offset] = rt_util_clamp_to_2BUI(val);
1205 checkvalint = data[offset];
1206 break;
1207 }
1208 case PT_4BUI: {
1209 data[offset] = rt_util_clamp_to_4BUI(val);
1210 checkvalint = data[offset];
1211 break;
1212 }
1213 case PT_8BSI: {
1214 data[offset] = (uint8_t)rt_util_clamp_to_8BSI(val);
1215 checkvalint = (int8_t) data[offset];
1216 break;
1217 }
1218 case PT_8BUI: {
1219 data[offset] = rt_util_clamp_to_8BUI(val);
1220 checkvalint = data[offset];
1221 break;
1222 }
1223 case PT_16BSI: {
1224 int16_t *ptr = (int16_t*) data; /* we assume correct alignment */
1225 ptr[offset] = rt_util_clamp_to_16BSI(val);
1226 checkvalint = (int16_t) ptr[offset];
1227 break;
1228 }
1229 case PT_16BUI: {
1230 uint16_t *ptr = (uint16_t*) data; /* we assume correct alignment */
1231 ptr[offset] = rt_util_clamp_to_16BUI(val);
1232 checkvalint = ptr[offset];
1233 break;
1234 }
1235 case PT_32BSI: {
1236 int32_t *ptr = (int32_t*) data; /* we assume correct alignment */
1237 ptr[offset] = rt_util_clamp_to_32BSI(val);
1238 checkvalint = (int32_t) ptr[offset];
1239 break;
1240 }
1241 case PT_32BUI: {
1242 uint32_t *ptr = (uint32_t *)data; /* we assume correct alignment */
1243 ptr[offset] = rt_util_clamp_to_32BUI(val);
1244 checkvaluint = ptr[offset];
1245 break;
1246 }
1247 case PT_16BF: {
1248 uint16_t *ptr = (uint16_t *)data; /* we assume correct alignment */
1249 float clamped = rt_util_clamp_to_16F(val);
1250 /* Pack via explicit converter to mirror GDAL's Float16 representation. */
1251 ptr[offset] = rt_util_float_to_float16(clamped);
1252 checkvalfloat = rt_util_float16_to_float(ptr[offset]);
1253 break;
1254 }
1255 case PT_32BF: {
1256 float *ptr = (float *)data; /* we assume correct alignment */
1257 ptr[offset] = rt_util_clamp_to_32F(val);
1258 checkvalfloat = ptr[offset];
1259 break;
1260 }
1261 case PT_64BF: {
1262 double *ptr = (double*) data; /* we assume correct alignment */
1263 ptr[offset] = val;
1264 checkvaldouble = ptr[offset];
1265 break;
1266 }
1267 default: {
1268 rterror("rt_band_set_pixel: Unknown pixeltype %d", pixtype);
1269 return ES_ERROR;
1270 }
1271 }
1272
1273 /* If the stored value is not NODATA, reset the isnodata flag */
1274 if (!rt_band_clamped_value_is_nodata(band, val)) {
1275 RASTER_DEBUG(3, "Band has a value that is not NODATA. Setting isnodata to FALSE");
1276 band->isnodata = FALSE;
1277 }
1278
1279 /* Overflow checking */
1281 val,
1282 checkvalint, checkvaluint,
1283 checkvalfloat, checkvaldouble,
1284 pixtype
1285 ) && converted != NULL) {
1286 *converted = 1;
1287 }
1288
1289 return ES_NONE;
1290}
#define FALSE
Definition dbfopen.c:72
void rterror(const char *fmt,...) __attribute__((format(printf
Wrappers used for reporting errors and info.
#define RASTER_DEBUG(level, msg)
Definition librtcore.h:304
int8_t rt_util_clamp_to_8BSI(double value)
Definition rt_util.c:51
uint8_t rt_util_clamp_to_1BB(double value)
Definition rt_util.c:36
float rt_util_clamp_to_16F(double value)
Definition rt_util.c:88
void void void rtwarn(const char *fmt,...) __attribute__((format(printf
int32_t rt_util_clamp_to_32BSI(double value)
Definition rt_util.c:71
uint16_t rt_util_float_to_float16(float value)
Definition rt_util.c:101
float rt_util_float16_to_float(uint16_t value)
Definition rt_util.c:134
rt_pixtype
Definition librtcore.h:188
@ PT_32BUI
Definition librtcore.h:197
@ PT_16BF
Definition librtcore.h:198
@ PT_2BUI
Definition librtcore.h:190
@ PT_32BSI
Definition librtcore.h:196
@ PT_END
Definition librtcore.h:201
@ PT_4BUI
Definition librtcore.h:191
@ PT_32BF
Definition librtcore.h:199
@ PT_1BB
Definition librtcore.h:189
@ PT_16BUI
Definition librtcore.h:195
@ PT_8BSI
Definition librtcore.h:192
@ PT_16BSI
Definition librtcore.h:194
@ PT_64BF
Definition librtcore.h:200
@ PT_8BUI
Definition librtcore.h:193
int rt_util_dbl_trunc_warning(double initialvalue, int32_t checkvalint, uint32_t checkvaluint, float checkvalfloat, double checkvaldouble, rt_pixtype pixtype)
Definition rt_util.c:778
uint8_t rt_util_clamp_to_2BUI(double value)
Definition rt_util.c:41
uint8_t rt_util_clamp_to_8BUI(double value)
Definition rt_util.c:56
@ ES_NONE
Definition librtcore.h:182
@ ES_ERROR
Definition librtcore.h:183
int16_t rt_util_clamp_to_16BSI(double value)
Definition rt_util.c:61
uint8_t rt_util_clamp_to_4BUI(double value)
Definition rt_util.c:46
uint16_t rt_util_clamp_to_16BUI(double value)
Definition rt_util.c:66
uint32_t rt_util_clamp_to_32BUI(double value)
Definition rt_util.c:76
float rt_util_clamp_to_32F(double value)
Definition rt_util.c:81
void * rt_band_get_data(rt_band band)
Get pointer to raster band data.
Definition rt_band.c:559
rt_errorstate rt_band_corrected_clamped_value(rt_band band, double val, double *newval, int *corrected)
Correct value when clamped value is equal to clamped NODATA value.
Definition rt_band.c:2171
int rt_band_clamped_value_is_nodata(rt_band band, double val)
Compare clamped value to band's clamped NODATA value.
Definition rt_band.c:2135

References ES_ERROR, ES_NONE, FALSE, PT_16BF, PT_16BSI, PT_16BUI, PT_1BB, PT_2BUI, PT_32BF, PT_32BSI, PT_32BUI, PT_4BUI, PT_64BF, PT_8BSI, PT_8BUI, PT_END, RASTER_DEBUG, rt_band_clamped_value_is_nodata(), rt_band_corrected_clamped_value(), rt_band_get_data(), rt_util_clamp_to_16BSI(), rt_util_clamp_to_16BUI(), rt_util_clamp_to_16F(), rt_util_clamp_to_1BB(), rt_util_clamp_to_2BUI(), rt_util_clamp_to_32BSI(), rt_util_clamp_to_32BUI(), rt_util_clamp_to_32F(), rt_util_clamp_to_4BUI(), rt_util_clamp_to_8BSI(), rt_util_clamp_to_8BUI(), rt_util_dbl_trunc_warning(), rt_util_float16_to_float(), rt_util_float_to_float16(), rterror(), and rtwarn().

Referenced by fillRasterToPolygonize(), RASTER_clip(), RASTER_mapAlgebra2(), RASTER_mapAlgebraExpr(), RASTER_mapAlgebraFct(), RASTER_mapAlgebraFctNgb(), RASTER_setPixelValue(), RASTER_setPixelValuesArray(), RASTER_setPixelValuesGeomval(), rt_band_reclass(), rt_band_reclass_exact(), rt_raster_gdal_rasterize(), rt_raster_iterator(), test_band_get_nearest_pixel(), test_band_get_pixel_line(), test_band_get_pixel_of_value(), test_band_pixtype_16BSI(), test_band_pixtype_16BUI(), test_band_pixtype_1BB(), test_band_pixtype_2BUI(), test_band_pixtype_32BF(), test_band_pixtype_32BSI(), test_band_pixtype_32BUI(), test_band_pixtype_4BUI(), test_band_pixtype_64BF(), test_band_pixtype_8BSI(), test_band_pixtype_8BUI(), test_band_reclass(), test_band_stats(), test_band_value_count(), test_gdal_to_raster(), test_gdal_warp(), test_pixel_set_to_array(), test_raster_colormap(), test_raster_fully_within_distance(), test_raster_geos_contains(), test_raster_geos_contains_properly(), test_raster_geos_covered_by(), test_raster_geos_covers(), test_raster_geos_overlaps(), test_raster_geos_touches(), test_raster_get_pixel_bilinear(), test_raster_intersects(), test_raster_iterator(), test_raster_perimeter(), test_raster_pixel_as_polygon(), test_raster_surface(), test_raster_to_gdal(), and test_raster_within_distance().

Here is the call graph for this function:
Here is the caller graph for this function: