37 uint16_t width, uint16_t height,
52 rterror(
"rt_band_from_wkb: Out of memory allocating rt_band during WKB parsing");
58 rterror(
"rt_band_from_wkb: Premature end of WKB on band reading (%s:%d)",
66 rterror(
"rt_band_from_wkb: Invalid pixtype %d", type & BANDTYPE_PIXTYPE_MASK);
78 RASTER_DEBUGF(3,
" Band pixtype:%s, offline:%d, hasnodata:%d",
86 if (((*ptr) + pixbytes) >= end) {
87 rterror(
"rt_band_from_wkb: Premature end of WKB on band novalue reading");
145 RASTER_DEBUGF(3,
" Nodata value: %g, pixbytes: %d, ptr @ %p, end @ %p",
149 if (((*ptr) + 1) >= end) {
150 rterror(
"rt_band_from_wkb: Premature end of WKB on offline " 151 "band data bandNum reading (%s:%d)",
164 while ((*ptr)[sz] && &((*ptr)[sz]) < end) ++sz;
165 if (&((*ptr)[sz]) >= end) {
166 rterror(
"rt_band_from_wkb: Premature end of WKB on band offline path reading");
176 rterror(
"rt_band_from_wkb: Out of memory allocating for offline path of band");
198 sz = width * height * pixbytes;
199 if (((*ptr) + sz) > end) {
200 rterror(
"rt_band_from_wkb: Premature end of WKB on band data reading (%s:%d)",
208 rterror(
"rt_band_from_wkb: Out of memory during band creation in WKB parser");
214 memcpy(band->
data.
mem, *ptr, sz);
226 else if (pixbytes == 4)
228 else if (pixbytes == 8)
231 rterror(
"rt_band_from_wkb: Unexpected pix bytes %d", pixbytes);
238 for (v = 0; v < sz; ++v) {
254 for (v = 0; v < sz; ++v) {
257 rterror(
"rt_band_from_wkb: Invalid value %d for pixel of type %s",
273 #define RT_WKB_HDR_SZ (sizeof(struct rt_raster_serialized_t)-4+1) 281 uint16_t version = 0;
289 rterror(
"rt_raster_from_wkb: wkb size (%d) < min size (%d)",
293 wkbend = wkb + wkbsize;
295 RASTER_DEBUGF(3,
"Parsing header from wkb position %d (expected 0)",
296 d_binptr_to_pos(ptr, wkbend, wkbsize));
307 rterror(
"rt_raster_from_wkb: WKB version %d unsupported", version);
314 rterror(
"rt_raster_from_wkb: Out of memory allocating raster for wkb input");
330 assert(ptr <= wkbend);
344 RASTER_DEBUGF(3,
"Parsing raster header finished at wkb position %d (expected 61)",
345 d_binptr_to_pos(ptr, wkbend, wkbsize));
353 rtwarn(
"%d bytes of WKB remained unparsed", wkbend - ptr);
355 else if (ptr > wkbend) {
357 rtwarn(
"We parsed %d bytes more then available!", ptr - wkbend);
367 rterror(
"rt_raster_from_wkb: Out of memory allocating bands for WKB raster decoding");
374 assert(ptr <= wkbend);
376 for (i = 0; i < rast->
numBands; ++i) {
378 d_binptr_to_pos(ptr, wkbend, wkbsize));
381 &ptr, wkbend, endian);
383 rterror(
"rt_raster_from_wkb: Error reading WKB form of band %d", i);
395 rtwarn(
"%d bytes of WKB remained unparsed", wkbend - ptr);
397 else if (ptr > wkbend) {
399 rtwarn(
"We parsed %d bytes more then available!", ptr - wkbend);
412 assert(NULL != hexwkb);
417 if (hexwkbsize % 2) {
418 rterror(
"rt_raster_from_hexwkb: Raster HEXWKB input must have an even number of characters");
421 wkbsize = hexwkbsize / 2;
425 rterror(
"rt_raster_from_hexwkb: Out of memory allocating memory for decoding HEXWKB");
430 for (i = 0; i < wkbsize; ++i) {
431 wkb[i] =
parse_hex((
char*) & (hexwkb[i * 2]));
445 assert(NULL != raster);
447 RASTER_DEBUGF(3,
"rt_raster_wkb_size: computing size for %d bands",
450 for (i = 0; i < raster->
numBands; ++i) {
455 RASTER_DEBUGF(3,
"rt_raster_wkb_size: adding size of band %d", i);
458 rterror(
"rt_raster_wkb_size: Corrupted band: unknown pixtype");
468 if (!outasin && band->
offline) {
496 #if POSTGIS_DEBUG_LEVEL > 0 505 assert(NULL != raster);
506 assert(NULL != wkbsize);
508 RASTER_DEBUG(2,
"rt_raster_to_wkb: about to call rt_raster_wkb_size");
511 RASTER_DEBUGF(3,
"rt_raster_to_wkb: found size: %d", *wkbsize);
515 rterror(
"rt_raster_to_wkb: Out of memory allocating WKB for raster");
521 #if POSTGIS_DEBUG_LEVEL > 2 522 wkbend = ptr + (*wkbsize);
524 RASTER_DEBUGF(3,
"Writing raster header to wkb on position %d (expected 0)",
525 d_binptr_to_pos(ptr, wkbend, *wkbsize));
538 RASTER_DEBUGF(3,
"Writing bands header to wkb position %d (expected 61)",
539 d_binptr_to_pos(ptr, wkbend, *wkbsize));
542 for (i = 0; i < raster->
numBands; ++i) {
548 RASTER_DEBUGF(3,
"Writing band pixel type to wkb position %d",
549 d_binptr_to_pos(ptr, wkbend, *wkbsize));
552 rterror(
"rt_raster_to_wkb: Corrupted band: unknown pixtype");
568 memset(ptr,
'\0', pixbytes - 1);
572 assert(!(((uint64_t) ptr) % pixbytes));
576 d_binptr_to_pos(ptr, wkbend, *wkbsize));
621 rterror(
"rt_raster_to_wkb: Fatal error caused by unknown pixel type. Aborting.");
630 assert(!((uint64_t) ptr % pixbytes));
633 if (!outasin && band->
offline) {
645 RASTER_DEBUGF(4,
"rt_raster_to_wkb: Copying %d bytes", datasize);
655 while ((uint64_t) ptr % 8) {
661 assert(!((uint64_t) ptr % pixbytes));
674 assert(NULL != raster);
675 assert(NULL != hexwkbsize);
677 RASTER_DEBUG(2,
"rt_raster_to_hexwkb: calling rt_raster_to_wkb");
681 RASTER_DEBUG(3,
"rt_raster_to_hexwkb: rt_raster_to_wkb returned");
683 *hexwkbsize = wkbsize * 2;
684 hexwkb = (
char*)
rtalloc((*hexwkbsize) + 1);
686 rterror(
"rt_raster_to_hexwkb: Out of memory hexifying raster WKB");
693 const char hexchar[]=
"0123456789ABCDEF";
696 *optr++ = hexchar[v>>4];
697 *optr++ = hexchar[v & 0x0F];
703 RASTER_DEBUGF(3,
"rt_raster_to_hexwkb: output wkb: %s", hexwkb);
int clamp_srid(int srid)
Return a valid SRID from an arbitrary integer Raises a notice if what comes out is different from wha...
static rt_band rt_band_from_wkb(uint16_t width, uint16_t height, const uint8_t **ptr, const uint8_t *end, uint8_t littleEndian)
#define BANDTYPE_FLAG_HASNODATA
static uint32_t rt_raster_wkb_size(rt_raster raster, int outasin)
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
struct rt_raster_t * rt_raster
Types definitions.
void rterror(const char *fmt,...)
Wrappers used for reporting errors and info.
void * rtalloc(size_t size)
Wrappers used for managing memory.
void rt_band_destroy(rt_band band)
Destroy a raster band.
void flip_endian_16(uint8_t *d)
#define CHECK_BINPTR_POSITION(ptr, end, size, pos)
int16_t read_int16(const uint8_t **from, uint8_t littleEndian)
void write_uint16(uint8_t **to, uint8_t littleEndian, uint16_t v)
uint32_t read_uint32(const uint8_t **from, uint8_t littleEndian)
uint8_t * rt_raster_to_wkb(rt_raster raster, int outasin, uint32_t *wkbsize)
Return this raster in WKB form.
uint16_t read_uint16(const uint8_t **from, uint8_t littleEndian)
void rtwarn(const char *fmt,...)
#define BANDTYPE_FLAG_OFFDB
#define BANDTYPE_IS_OFFDB(x)
void flip_endian_32(uint8_t *d)
rt_raster rt_raster_from_hexwkb(const char *hexwkb, uint32_t hexwkbsize)
Construct an rt_raster from a text HEXWKB representation.
#define BANDTYPE_PIXTYPE_MASK
void * rt_band_get_data(rt_band band)
Get pointer to raster band data.
#define RASTER_DEBUGF(level, msg,...)
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
uint8_t isMachineLittleEndian(void)
int rt_pixtype_size(rt_pixtype pixtype)
Return size in bytes of a value in the given pixtype.
uint8_t read_uint8(const uint8_t **from)
uint8_t parse_hex(char *str)
Convert a single hex digit into the corresponding char.
rt_raster rt_raster_from_wkb(const uint8_t *wkb, uint32_t wkbsize)
Construct an rt_raster from a binary WKB representation.
int8_t read_int8(const uint8_t **from)
void rtdealloc(void *mem)
#define RASTER_DEBUG(level, msg)
This library is the generic raster handling section of PostGIS.
const char * rt_pixtype_name(rt_pixtype pixtype)
char * rt_raster_to_hexwkb(rt_raster raster, int outasin, uint32_t *hexwkbsize)
Return this raster in HEXWKB form (null-terminated hex)
int32_t read_int32(const uint8_t **from, uint8_t littleEndian)
double read_float64(const uint8_t **from, uint8_t littleEndian)
#define BANDTYPE_HAS_NODATA(x)
float read_float32(const uint8_t **from, uint8_t littleEndian)
#define BANDTYPE_IS_NODATA(x)
void flip_endian_64(uint8_t *d)
#define BANDTYPE_FLAG_ISNODATA