38 #if POSTGIS_DEBUG_LEVEL > 2 47 assert(NULL != hexsize);
51 hex = (
char*)
rtalloc((*hexsize) + 1);
53 rterror(
"d_binary_to_hex: Out of memory hexifying raw binary");
58 for (i = 0; i < size; ++i) {
63 assert(0 == strlen(hex) % 2);
68 d_print_binary_hex(
const char* msg,
const uint8_t *
const raw,
uint32_t size) {
77 hex = d_binary_to_hex(raw, size, &hexsize);
79 rtinfo(
"%s\t%s", msg, hex);
85 d_binptr_to_pos(
const uint8_t *
const ptr,
const uint8_t *
const end,
size_t size) {
86 assert(NULL != ptr && NULL != end);
88 return (size - (end - ptr));
110 setBits(
char* ch,
double val,
int bits,
int bitOffset) {
111 char mask = 0xFF >> (8 - bits);
116 assert(8 - bitOffset >= bits);
119 ival, bits, mask, bitOffset);
123 #if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0 125 rtwarn(
"Pixel value for %d-bits band got truncated" 126 " from %g to %hhu", bits, val, ival);
135 ival <<= (8 - bitOffset - bits);
141 *ch &= ~(mask << (8 - bits - bitOffset));
157 assert(NULL != a && NULL != b);
191 static int endian_check_int = 1;
193 return *((
uint8_t *) & endian_check_int);
198 assert(NULL != from);
214 assert(NULL != from);
232 assert(NULL != from);
239 ret = (*from)[0] << 8 |
251 (*to)[0] = v & 0x00FF;
254 (*to)[1] = v & 0x00FF;
262 assert(NULL != from);
290 assert(NULL != from);
293 ret = (
uint32_t) ((*from)[0] & 0xff) |
294 (
uint32_t) ((*from)[1] & 0xff) << 8 |
295 (
uint32_t) ((*from)[2] & 0xff) << 16 |
296 (
uint32_t) ((*from)[3] & 0xff) << 24;
299 ret = (
uint32_t) ((*from)[3] & 0xff) |
300 (
uint32_t) ((*from)[2] & 0xff) << 8 |
301 (
uint32_t) ((*from)[1] & 0xff) << 16 |
302 (
uint32_t) ((*from)[0] & 0xff) << 24;
334 assert(NULL != from);
396 assert(NULL != from);
399 ret.i = (uint64_t) ((*from)[0] & 0xff) |
400 (uint64_t) ((*from)[1] & 0xff) << 8 |
401 (uint64_t) ((*from)[2] & 0xff) << 16 |
402 (uint64_t) ((*from)[3] & 0xff) << 24 |
403 (uint64_t) ((*from)[4] & 0xff) << 32 |
404 (uint64_t) ((*from)[5] & 0xff) << 40 |
405 (uint64_t) ((*from)[6] & 0xff) << 48 |
406 (uint64_t) ((*from)[7] & 0xff) << 56;
409 ret.i = (uint64_t) ((*from)[7] & 0xff) |
410 (uint64_t) ((*from)[6] & 0xff) << 8 |
411 (uint64_t) ((*from)[5] & 0xff) << 16 |
412 (uint64_t) ((*from)[4] & 0xff) << 24 |
413 (uint64_t) ((*from)[3] & 0xff) << 32 |
414 (uint64_t) ((*from)[2] & 0xff) << 40 |
415 (uint64_t) ((*from)[1] & 0xff) << 48 |
416 (uint64_t) ((*from)[0] & 0xff) << 56;
466 assert(NULL != raster);
468 RASTER_DEBUGF(3,
"Serialized size with just header:%d - now adding size of %d bands",
471 for (i = 0; i < raster->
numBands; ++i) {
477 rterror(
"rt_raster_serialized_size: Corrupted band: unknown pixtype");
506 size += 8 - (size % 8);
527 assert(NULL != raster);
532 rterror(
"rt_raster_serialize: Out of memory allocating %d bytes for serializing a raster", size);
535 memset(ret,
'-', size);
542 RASTER_DEBUGF(3,
"serialized size:%lu", (
long unsigned) size);
557 RASTER_DEBUG(3,
"Start hex dump of raster being serialized using 0x2D to mark non-written bytes");
559 #if POSTGIS_DEBUG_LEVEL > 2 561 d_print_binary_hex(
"HEADER", dbg_ptr, size);
567 for (i = 0; i < raster->
numBands; ++i) {
569 assert(NULL != band);
574 rterror(
"rt_raster_serialize: Corrupted band: unknown pixtype");
582 #ifdef POSTGIS_RASTER_DISABLE_OFFLINE 583 rterror(
"rt_raster_serialize: offdb raster support disabled at compile-time");
596 #if POSTGIS_DEBUG_LEVEL > 2 597 d_print_binary_hex(
"PIXTYPE", dbg_ptr, size);
604 memset(ptr,
'\0', pixbytes - 1);
608 #if POSTGIS_DEBUG_LEVEL > 2 609 d_print_binary_hex(
"PADDING", dbg_ptr, size);
613 assert(!((ptr - ret) % pixbytes));
658 rterror(
"rt_raster_serialize: Fatal error caused by unknown pixel type. Aborting.");
664 assert(!((ptr - ret) % pixbytes));
666 #if POSTGIS_DEBUG_LEVEL > 2 667 d_print_binary_hex(
"nodata", dbg_ptr, size);
682 memcpy(ptr, band->
data.
mem, datasize);
686 #if POSTGIS_DEBUG_LEVEL > 2 687 d_print_binary_hex(
"BAND", dbg_ptr, size);
691 while ((uintptr_t) ptr % 8) {
699 assert(!((ptr - ret) % pixbytes));
702 #if POSTGIS_DEBUG_LEVEL > 2 703 d_print_binary_hex(
"SERIALIZED RASTER", dbg_ptr, size);
725 assert(NULL != serialized);
735 RASTER_DEBUG(3,
"rt_raster_deserialize: Allocating memory for deserialized raster header");
738 rterror(
"rt_raster_deserialize: Out of memory allocating raster for deserialization");
743 RASTER_DEBUG(3,
"rt_raster_deserialize: Deserialize raster header");
746 if (0 == rast->
numBands || header_only) {
751 beg = (
const uint8_t*) serialized;
754 RASTER_DEBUG(3,
"rt_raster_deserialize: Allocating memory for bands");
756 if (rast->
bands == NULL) {
757 rterror(
"rt_raster_deserialize: Out of memory allocating bands");
769 for (i = 0; i < rast->
numBands; ++i) {
776 rterror(
"rt_raster_deserialize: Out of memory allocating rt_band during deserialization");
849 rterror(
"rt_raster_deserialize: Unknown pixeltype %d", band->
pixtype);
860 assert(!((ptr - beg) % pixbytes));
870 pathlen = strlen((
char*) ptr);
873 rterror(
"rt_raster_deserialize: Could not allocate memory for offline band path");
893 #if POSTGIS_DEBUG_LEVEL > 0 896 while (0 != ((ptr - beg) % 8)) {
900 RASTER_DEBUGF(3,
"rt_raster_deserialize: skip %d bytes of 8-bytes boundary padding", ptr - padbeg);
903 assert(!((ptr - beg) % pixbytes));
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
#define BANDTYPE_FLAG_HASNODATA
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
void deparse_hex(uint8_t str, char *result)
Convert a char into a human readable hex digit.
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_raster_serialize(rt_raster raster)
Return this raster in serialized form.
void rt_band_destroy(rt_band band)
Destroy a raster band.
void flip_endian_16(uint8_t *d)
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)
static uint32_t rt_raster_serialized_size(rt_raster raster)
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)
void rtinfo(const char *fmt,...)
#define BANDTYPE_PIXTYPE_MASK
#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)
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)
int32_t read_int32(const uint8_t **from, uint8_t littleEndian)
void swap_char(uint8_t *a, uint8_t *b)
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