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

◆ rt_band_get_pixel()

rt_errorstate rt_band_get_pixel ( rt_band  band,
int  x,
int  y,
double *  value,
int *  nodata 
)

Get pixel value.

If band's isnodata flag is TRUE, value returned will be the band's NODATA value

Parameters
band: the band to set nodata value to
x: x ordinate (0-based)
y: x ordinate (0-based)
*value: pixel value
*nodata: 0 if pixel is not NODATA
Returns
0 on success, -1 on error (value out of valid range).

Definition at line 1551 of file rt_band.c.

1556 {
1557 rt_pixtype pixtype = PT_END;
1558 uint8_t* data = NULL;
1559 uint32_t offset = 0;
1560
1561 assert(NULL != band);
1562 assert(NULL != value);
1563
1564 /* set nodata to 0 */
1565 if (nodata != NULL)
1566 *nodata = 0;
1567
1568 if (
1569 x < 0 || x >= band->width ||
1570 y < 0 || y >= band->height
1571 ) {
1572 rtwarn("Attempting to get pixel value with out of range raster coordinates: (%d, %d)", x, y);
1573 return ES_ERROR;
1574 }
1575
1576 /* band is NODATA */
1577 if (band->isnodata) {
1578 RASTER_DEBUG(3, "Band's isnodata flag is TRUE. Returning NODATA value");
1579 *value = band->nodataval;
1580 if (nodata != NULL) *nodata = 1;
1581 return ES_NONE;
1582 }
1583
1584 data = rt_band_get_data(band);
1585 if (data == NULL) {
1586 rterror("rt_band_get_pixel: Cannot get band data");
1587 return ES_ERROR;
1588 }
1589
1590 /* +1 for the nodata value */
1591 offset = x + (y * band->width);
1592
1593 pixtype = band->pixtype;
1594
1595 switch (pixtype) {
1596 case PT_1BB:
1597#ifdef OPTIMIZE_SPACE
1598 {
1599 int byteOffset = offset / 8;
1600 int bitOffset = offset % 8;
1601 data += byteOffset;
1602
1603 /* Bit to set is bitOffset into data */
1604 *value = getBits(data, val, 1, bitOffset);
1605 break;
1606 }
1607#endif
1608 case PT_2BUI:
1609#ifdef OPTIMIZE_SPACE
1610 {
1611 int byteOffset = offset / 4;
1612 int bitOffset = offset % 4;
1613 data += byteOffset;
1614
1615 /* Bits to set start at bitOffset into data */
1616 *value = getBits(data, val, 2, bitOffset);
1617 break;
1618 }
1619#endif
1620 case PT_4BUI:
1621#ifdef OPTIMIZE_SPACE
1622 {
1623 int byteOffset = offset / 2;
1624 int bitOffset = offset % 2;
1625 data += byteOffset;
1626
1627 /* Bits to set start at bitOffset into data */
1628 *value = getBits(data, val, 2, bitOffset);
1629 break;
1630 }
1631#endif
1632 case PT_8BSI: {
1633 int8_t val = (int8_t)data[offset];
1634 *value = val;
1635 break;
1636 }
1637 case PT_8BUI: {
1638 uint8_t val = data[offset];
1639 *value = val;
1640 break;
1641 }
1642 case PT_16BSI: {
1643 int16_t *ptr = (int16_t*) data; /* we assume correct alignment */
1644 *value = ptr[offset];
1645 break;
1646 }
1647 case PT_16BUI: {
1648 uint16_t *ptr = (uint16_t*) data; /* we assume correct alignment */
1649 *value = ptr[offset];
1650 break;
1651 }
1652 case PT_32BSI: {
1653 int32_t *ptr = (int32_t*) data; /* we assume correct alignment */
1654 *value = ptr[offset];
1655 break;
1656 }
1657 case PT_32BUI: {
1658 uint32_t *ptr = (uint32_t *)data; /* we assume correct alignment */
1659 *value = ptr[offset];
1660 break;
1661 }
1662 case PT_16BF: {
1663 uint16_t *ptr = (uint16_t *)data; /* we assume correct alignment */
1664 *value = rt_util_float16_to_float(ptr[offset]);
1665 break;
1666 }
1667 case PT_32BF: {
1668 float *ptr = (float *)data; /* we assume correct alignment */
1669 *value = ptr[offset];
1670 break;
1671 }
1672 case PT_64BF: {
1673 double *ptr = (double*) data; /* we assume correct alignment */
1674 *value = ptr[offset];
1675 break;
1676 }
1677 default: {
1678 rterror("rt_band_get_pixel: Unknown pixeltype %d", pixtype);
1679 return ES_ERROR;
1680 }
1681 }
1682
1683 /* set NODATA flag */
1684 if (band->hasnodata && nodata != NULL) {
1685 if (rt_band_clamped_value_is_nodata(band, *value))
1686 *nodata = 1;
1687 }
1688
1689 return ES_NONE;
1690}
void rterror(const char *fmt,...) __attribute__((format(printf
Wrappers used for reporting errors and info.
#define RASTER_DEBUG(level, msg)
Definition librtcore.h:304
void void void rtwarn(const char *fmt,...) __attribute__((format(printf
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
@ ES_NONE
Definition librtcore.h:182
@ ES_ERROR
Definition librtcore.h:183
int value
Definition genraster.py:62
void * rt_band_get_data(rt_band band)
Get pointer to raster band data.
Definition rt_band.c:559
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, 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_get_data(), rt_util_float16_to_float(), rterror(), and rtwarn().

Referenced by _rti_raster_get_band_perimeter(), RASTER_clip(), RASTER_dumpValues(), RASTER_getPixelCentroids(), RASTER_getPixelPolygons(), RASTER_getPixelValue(), RASTER_mapAlgebra2(), RASTER_mapAlgebraExpr(), RASTER_mapAlgebraFct(), RASTER_mapAlgebraFctNgb(), RASTER_nearestValue(), RASTER_neighborhood(), RASTER_setPixelValuesArray(), RASTER_setPixelValuesGeomval(), rt_band_check_is_nodata(), rt_band_get_nearest_pixel(), rt_band_get_pixel_bilinear(), rt_band_get_pixel_of_value(), rt_band_get_pixel_resample(), rt_band_get_quantiles_stream(), rt_band_get_summary_stats(), rt_band_get_value_count(), rt_band_reclass(), rt_band_reclass_exact(), rt_band_set_pixel_line(), rt_raster_gdal_rasterize(), rt_raster_intersects(), rt_raster_intersects_algorithm(), rt_raster_iterator(), rt_raster_to_gdal_mem(), test_band_metadata(), 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_gdal_to_raster(), test_gdal_warp(), test_pixel_set_to_array(), test_raster_colormap(), and test_raster_wkb().

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