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)",
76 band->height = height;
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");
93 switch (
band->pixtype) {
139 rterror(
"rt_band_from_wkb: Unknown pixeltype %d",
band->pixtype);
145 RASTER_DEBUGF(3,
" Nodata value: %g, pixbytes: %d, ptr @ %p, end @ %p",
146 band->nodataval, pixbytes, *ptr, end);
149 if (((*ptr) + 1) >= end) {
150 rterror(
"rt_band_from_wkb: Premature end of WKB on offline "
151 "band data bandNum reading (%s:%d)",
159 band->data.offline.mem = NULL;
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");
175 if (
band->data.offline.path == NULL) {
176 rterror(
"rt_band_from_wkb: Out of memory allocating for offline path of band");
181 memcpy(
band->data.offline.path, *ptr, sz);
182 band->data.offline.path[sz] =
'\0';
185 band->data.offline.path, sz);
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)",
207 if (!
band->data.mem) {
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);
236 flipme =
band->data.mem;
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));
350 if (!
rast->numBands) {
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]));
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) {
473 size += strlen(
band->data.offline.path) + 1;
496 #if POSTGIS_DEBUG_LEVEL > 0
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");
558 *ptr =
band->pixtype;
568 memset(ptr,
'\0', pixbytes - 1);
572 assert(!(((uint64_t) ptr) % pixbytes));
576 d_binptr_to_pos(ptr, wkbend, *wkbsize));
590 int8_t v =
band->nodataval;
596 int16_t v =
band->nodataval;
602 uint16_t v =
band->nodataval;
608 int32_t v =
band->nodataval;
620 float v =
band->nodataval;
626 memcpy(ptr, &
band->nodataval, 8);
631 rterror(
"rt_raster_to_wkb: Fatal error caused by unknown pixel type. Aborting.");
640 assert(!((uint64_t) ptr % pixbytes));
643 if (!outasin &&
band->offline) {
645 *ptr =
band->data.offline.bandNum;
649 strcpy((
char*) ptr,
band->data.offline.path);
650 ptr += strlen(
band->data.offline.path) + 1;
655 RASTER_DEBUGF(4,
"rt_raster_to_wkb: Copying %d bytes", datasize);
665 while ((uint64_t) ptr % 8) {
671 assert(!((uint64_t) ptr % pixbytes));
685 assert(NULL != hexwkbsize);
687 RASTER_DEBUG(2,
"rt_raster_to_hexwkb: calling rt_raster_to_wkb");
691 RASTER_DEBUG(3,
"rt_raster_to_hexwkb: rt_raster_to_wkb returned");
693 *hexwkbsize = wkbsize * 2;
694 hexwkb = (
char*)
rtalloc((*hexwkbsize) + 1);
696 rterror(
"rt_raster_to_hexwkb: Out of memory hexifying raster WKB");
703 const char hexchar[]=
"0123456789ABCDEF";
706 *optr++ = hexchar[v>>4];
707 *optr++ = hexchar[v & 0x0F];
713 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...
uint8_t parse_hex(char *str)
Convert a single hex digit into the corresponding char.
void rterror(const char *fmt,...)
Wrappers used for reporting errors and info.
void * rtalloc(size_t size)
Wrappers used for managing memory.
#define RASTER_DEBUG(level, msg)
#define RASTER_DEBUGF(level, msg,...)
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
void rtwarn(const char *fmt,...)
void rt_band_destroy(rt_band band)
Destroy a raster band.
const char * rt_pixtype_name(rt_pixtype pixtype)
void rtdealloc(void *mem)
void * rt_band_get_data(rt_band band)
Get pointer to raster band data.
struct rt_raster_t * rt_raster
Types definitions.
int rt_pixtype_size(rt_pixtype pixtype)
Return size in bytes of a value in the given pixtype.
This library is the generic raster handling section of PostGIS.
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
uint8_t isMachineLittleEndian(void)
void flip_endian_32(uint8_t *d)
void flip_endian_16(uint8_t *d)
uint8_t read_uint8(const uint8_t **from)
void write_uint16(uint8_t **to, uint8_t littleEndian, uint16_t v)
double read_float64(const uint8_t **from, uint8_t littleEndian)
float read_float32(const uint8_t **from, uint8_t littleEndian)
void flip_endian_64(uint8_t *d)
int8_t read_int8(const uint8_t **from)
int16_t read_int16(const uint8_t **from, uint8_t littleEndian)
int32_t read_int32(const uint8_t **from, uint8_t littleEndian)
uint32_t read_uint32(const uint8_t **from, uint8_t littleEndian)
uint16_t read_uint16(const uint8_t **from, uint8_t littleEndian)
#define BANDTYPE_FLAG_HASNODATA
#define BANDTYPE_FLAG_OFFDB
#define BANDTYPE_FLAG_ISNODATA
#define BANDTYPE_HAS_NODATA(x)
#define BANDTYPE_IS_OFFDB(x)
#define BANDTYPE_IS_NODATA(x)
#define BANDTYPE_PIXTYPE_MASK
#define CHECK_BINPTR_POSITION(ptr, end, size, pos)
static rt_band rt_band_from_wkb(uint16_t width, uint16_t height, const uint8_t **ptr, const uint8_t *end, uint8_t littleEndian)
rt_raster rt_raster_from_wkb(const uint8_t *wkb, uint32_t wkbsize)
Construct an rt_raster from a binary WKB representation.
char * rt_raster_to_hexwkb(rt_raster raster, int outasin, uint32_t *hexwkbsize)
Return this raster in HEXWKB form (null-terminated hex)
uint8_t * rt_raster_to_wkb(rt_raster raster, int outasin, uint32_t *wkbsize)
Return this raster in WKB form.
rt_raster rt_raster_from_hexwkb(const char *hexwkb, uint32_t hexwkbsize)
Construct an rt_raster from a text HEXWKB representation.
static uint32_t rt_raster_wkb_size(rt_raster raster, int outasin)