39 uint16_t width, uint16_t height,
40 const uint8_t** ptr,
const uint8_t* end,
54 rterror(
"rt_band_from_wkb: Out of memory allocating rt_band during WKB parsing");
60 rterror(
"rt_band_from_wkb: Premature end of WKB on band reading (%s:%d)",
78 band->height = height;
80 RASTER_DEBUGF(3,
" Band pixtype:%s, offline:%d, hasnodata:%d",
88 if (((*ptr) + pixbytes) >= end) {
89 rterror(
"rt_band_from_wkb: Premature end of WKB on band novalue reading");
95 switch (
band->pixtype) {
141 rterror(
"rt_band_from_wkb: Unknown pixeltype %d",
band->pixtype);
147 RASTER_DEBUGF(3,
" Nodata value: %g, pixbytes: %d, ptr @ %p, end @ %p",
148 band->nodataval, pixbytes, *ptr, end);
151 if (((*ptr) + 1) >= end) {
152 rterror(
"rt_band_from_wkb: Premature end of WKB on offline "
153 "band data bandNum reading (%s:%d)",
161 band->data.offline.mem = NULL;
166 while ((*ptr)[sz] && &((*ptr)[sz]) < end) ++sz;
167 if (&((*ptr)[sz]) >= end) {
168 rterror(
"rt_band_from_wkb: Premature end of WKB on band offline path reading");
177 if (
band->data.offline.path == NULL) {
178 rterror(
"rt_band_from_wkb: Out of memory allocating for offline path of band");
183 memcpy(
band->data.offline.path, *ptr, sz);
184 band->data.offline.path[sz] =
'\0';
187 band->data.offline.path, sz);
200 sz = (size_t)width * height * pixbytes;
201 if (((*ptr) + sz) > end) {
202 rterror(
"rt_band_from_wkb: Premature end of WKB on band data reading (%s:%d)",
209 if (!
band->data.mem) {
210 rterror(
"rt_band_from_wkb: Out of memory during band creation in WKB parser");
216 memcpy(
band->data.mem, *ptr, sz);
223 void (*flipper)(uint8_t*) = 0;
224 uint8_t *flipme = NULL;
228 else if (pixbytes == 4)
230 else if (pixbytes == 8)
233 rterror(
"rt_band_from_wkb: Unexpected pix bytes %d", pixbytes);
238 flipme =
band->data.mem;
239 sz = (size_t)width * height;
240 for (v = 0; v < sz; ++v) {
255 sz = (size_t)width*height;
256 for (v = 0; v < sz; ++v) {
257 val = ((uint8_t*)
band->data.mem)[v];
259 rterror(
"rt_band_from_wkb: Invalid value %d for pixel of type %s",
275 #define RT_WKB_HDR_SZ (sizeof(struct rt_raster_serialized_t)-4+1)
279 const uint8_t *ptr = wkb;
280 const uint8_t *wkbend = NULL;
283 uint16_t version = 0;
291 rterror(
"rt_raster_from_wkb: wkb size (%" PRIu32
292 ") < min size (%zu)",
296 wkbend = wkb + wkbsize;
298 RASTER_DEBUGF(3,
"Parsing header from wkb position %zu (expected 0)",
299 d_binptr_to_pos(ptr, wkbend, wkbsize));
310 rterror(
"rt_raster_from_wkb: WKB version %d unsupported", version);
317 rterror(
"rt_raster_from_wkb: Out of memory allocating raster for wkb input");
333 assert(ptr <= wkbend);
347 RASTER_DEBUGF(3,
"Parsing raster header finished at wkb position %zu (expected 61)",
348 d_binptr_to_pos(ptr, wkbend, wkbsize));
353 if (!
rast->numBands) {
356 rtwarn(
"%zu bytes of WKB remained unparsed", wkbend - ptr);
358 else if (ptr > wkbend) {
360 rtwarn(
"We parsed %zu bytes more then available!", ptr - wkbend);
370 rterror(
"rt_raster_from_wkb: Out of memory allocating bands for WKB raster decoding");
377 assert(ptr <= wkbend);
379 for (i = 0; i <
rast->numBands; ++i) {
381 d_binptr_to_pos(ptr, wkbend, wkbsize));
384 &ptr, wkbend, endian);
386 rterror(
"rt_raster_from_wkb: Error reading WKB form of band %d", i);
398 rtwarn(
"%zu bytes of WKB remained unparsed", wkbend - ptr);
400 else if (ptr > wkbend) {
402 rtwarn(
"We parsed %zu bytes more then available!", ptr - wkbend);
412 uint32_t wkbsize = 0;
415 assert(NULL != hexwkb);
420 if (hexwkbsize % 2) {
421 rterror(
"rt_raster_from_hexwkb: Raster HEXWKB input must have an even number of characters");
424 wkbsize = hexwkbsize / 2;
428 rterror(
"rt_raster_from_hexwkb: Out of memory allocating memory for decoding HEXWKB");
433 for (i = 0; i < wkbsize; ++i) {
434 wkb[i] =
parse_hex((
char*) & (hexwkb[i * 2]));
450 RASTER_DEBUGF(3,
"rt_raster_wkb_size: computing size for %d bands",
453 for (i = 0; i <
raster->numBands; ++i) {
458 RASTER_DEBUGF(3,
"rt_raster_wkb_size: adding size of band %d", i);
461 rterror(
"rt_raster_wkb_size: Corrupted band: unknown pixtype");
471 if (!outasin &&
band->offline) {
476 size += strlen(
band->data.offline.path) + 1;
499 #if POSTGIS_DEBUG_LEVEL > 0
500 const uint8_t *wkbend = NULL;
509 assert(NULL != wkbsize);
511 RASTER_DEBUG(2,
"rt_raster_to_wkb: about to call rt_raster_wkb_size");
514 RASTER_DEBUGF(3,
"rt_raster_to_wkb: found size: %d", *wkbsize);
516 wkb = (uint8_t*)
rtalloc(*wkbsize);
518 rterror(
"rt_raster_to_wkb: Out of memory allocating WKB for raster");
524 #if POSTGIS_DEBUG_LEVEL > 2
525 wkbend = ptr + (*wkbsize);
527 RASTER_DEBUGF(3,
"Writing raster header to wkb on position %zu (expected 0)",
528 d_binptr_to_pos(ptr, wkbend, *wkbsize));
541 RASTER_DEBUGF(3,
"Writing bands header to wkb position %zu (expected 61)",
542 d_binptr_to_pos(ptr, wkbend, *wkbsize));
545 for (i = 0; i <
raster->numBands; ++i) {
551 RASTER_DEBUGF(3,
"Writing band pixel type to wkb position %zu",
552 d_binptr_to_pos(ptr, wkbend, *wkbsize));
555 rterror(
"rt_raster_to_wkb: Corrupted band: unknown pixtype");
561 *ptr =
band->pixtype;
571 memset(ptr,
'\0', pixbytes - 1);
575 assert(!(((uint64_t) ptr) % pixbytes));
579 d_binptr_to_pos(ptr, wkbend, *wkbsize));
587 uint8_t v =
band->nodataval;
593 int8_t v =
band->nodataval;
599 int16_t v =
band->nodataval;
605 uint16_t v =
band->nodataval;
611 int32_t v =
band->nodataval;
617 uint32_t v =
band->nodataval;
623 float v =
band->nodataval;
629 memcpy(ptr, &
band->nodataval, 8);
634 rterror(
"rt_raster_to_wkb: Fatal error caused by unknown pixel type. Aborting.");
643 assert(!((uint64_t) ptr % pixbytes));
646 if (!outasin &&
band->offline) {
648 *ptr =
band->data.offline.bandNum;
652 strcpy((
char*) ptr,
band->data.offline.path);
653 ptr += strlen(
band->data.offline.path) + 1;
657 uint32_t datasize =
raster->width *
raster->height * pixbytes;
658 RASTER_DEBUGF(4,
"rt_raster_to_wkb: Copying %d bytes", datasize);
668 while ((uint64_t) ptr % 8) {
674 assert(!((uint64_t) ptr % pixbytes));
685 uint32_t wkbsize = 0;
688 assert(NULL != hexwkbsize);
690 RASTER_DEBUG(2,
"rt_raster_to_hexwkb: calling rt_raster_to_wkb");
694 RASTER_DEBUG(3,
"rt_raster_to_hexwkb: rt_raster_to_wkb returned");
696 *hexwkbsize = wkbsize * 2;
697 hexwkb = (
char*)
rtalloc((*hexwkbsize) + 1);
699 rterror(
"rt_raster_to_hexwkb: Out of memory hexifying raster WKB");
706 const char hexchar[]=
"0123456789ABCDEF";
709 *optr++ = hexchar[v>>4];
710 *optr++ = hexchar[v & 0x0F];
716 RASTER_DEBUGF(3,
"rt_raster_to_hexwkb: output wkb: %s", hexwkb);
uint8_t parse_hex(char *str)
Convert a single hex digit into the corresponding char.
int32_t clamp_srid(int32_t srid)
Return a valid SRID from an arbitrary integer Raises a notice if what comes out is different from wha...
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.
#define RASTER_DEBUG(level, msg)
#define RASTER_DEBUGF(level, msg,...)
void void void rtwarn(const char *fmt,...) __attribute__((format(printf
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
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)