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

◆ rt_band_get_pixel_line()

rt_errorstate rt_band_get_pixel_line ( rt_band  band,
int  x,
int  y,
uint16_t  len,
void **  vals,
uint16_t *  nvals 
)

Get values of multiple pixels.

Unlike rt_band_get_pixel, values in vals are of the band's pixel type so cannot be assumed to be double. Function uses memcpy.

It is important to be careful when using this function as the number of values being fetched may exceed a pixel "row". Remember that the band values are stored in a stream (1-D array) regardless of what the raster's width and height might be. So, getting a number of values may cross multiple pixel "rows".

Parameters
band: the band to get pixel value from
x: pixel column (0-based)
y: pixel row (0-based)
len: the number of pixels to get
**vals: the pixel values
*nvals: the number of pixel values being returned
Returns
ES_NONE on success, ES_ERROR on error

Definition at line 1312 of file rt_band.c.

1317 {
1318 uint8_t *_vals = NULL;
1319 int pixsize = 0;
1320 uint8_t *data = NULL;
1321 uint32_t offset = 0;
1322 uint16_t _nvals = 0;
1323 int maxlen = 0;
1324 uint8_t *ptr = NULL;
1325
1326 assert(NULL != band);
1327 assert(vals != NULL && nvals != NULL);
1328
1329 /* initialize to no values */
1330 *nvals = 0;
1331
1332 if (
1333 x < 0 || x >= band->width ||
1334 y < 0 || y >= band->height
1335 ) {
1336 rtwarn("Attempting to get pixel values with out of range raster coordinates: (%d, %d)", x, y);
1337 return ES_ERROR;
1338 }
1339
1340 if (len < 1)
1341 return ES_NONE;
1342
1343 data = rt_band_get_data(band);
1344 if (data == NULL) {
1345 rterror("rt_band_get_pixel_line: Cannot get band data");
1346 return ES_ERROR;
1347 }
1348
1349 /* +1 for the nodata value */
1350 offset = x + (y * band->width);
1351 RASTER_DEBUGF(4, "offset = %d", offset);
1352
1353 pixsize = rt_pixtype_size(band->pixtype);
1354 RASTER_DEBUGF(4, "pixsize = %d", pixsize);
1355
1356 /* cap _nvals so that it doesn't overflow */
1357 _nvals = len;
1358 maxlen = band->width * band->height;
1359
1360 if (((int) (offset + _nvals)) > maxlen) {
1361 _nvals = maxlen - offset;
1362 rtwarn("Limiting returning number values to %d", _nvals);
1363 }
1364 RASTER_DEBUGF(4, "_nvals = %d", _nvals);
1365
1366 ptr = data + ((size_t)offset * pixsize);
1367
1368 _vals = rtalloc((size_t)_nvals * pixsize);
1369 if (_vals == NULL) {
1370 rterror("rt_band_get_pixel_line: Could not allocate memory for pixel values");
1371 return ES_ERROR;
1372 }
1373
1374 /* copy pixels */
1375 memcpy(_vals, ptr, (size_t)_nvals * pixsize);
1376
1377 *vals = _vals;
1378 *nvals = _nvals;
1379
1380 return ES_NONE;
1381}
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.
Definition rt_context.c:191
#define RASTER_DEBUGF(level, msg,...)
Definition librtcore.h:308
void void void rtwarn(const char *fmt,...) __attribute__((format(printf
@ ES_NONE
Definition librtcore.h:182
@ ES_ERROR
Definition librtcore.h:183
int rt_pixtype_size(rt_pixtype pixtype)
Return size in bytes of a value in the given pixtype.
Definition rt_pixel.c:40
void * rt_band_get_data(rt_band band)
Get pointer to raster band data.
Definition rt_band.c:559

References ES_ERROR, ES_NONE, RASTER_DEBUGF, rt_band_get_data(), rt_pixtype_size(), rtalloc(), rterror(), and rtwarn().

Referenced by RASTER_tile(), RASTER_union_transfn(), and test_band_get_pixel_line().

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