]> granicus.if.org Git - postgis/commitdiff
Changes in raster memory management:
authorJorge Arévalo <jorge.arevalo at deimos-space.com>
Fri, 6 May 2011 12:06:29 +0000 (12:06 +0000)
committerJorge Arévalo <jorge.arevalo at deimos-space.com>
Fri, 6 May 2011 12:06:29 +0000 (12:06 +0000)
- Only calling function memory context is used, except in SRF functions, where
multi_call_memory_ctx is used.
- rt_context internals hidden. The memory management and error reporting is
performed by rtalloc/rterror family functions. They simply call rt_context
struct fields as requested. Same philosophy here than in liblwgeom.
- Now rt_context memory is statically allocated. Before this, new memory for
rt_context was allocated in every function call. And the memory was allocated
outside the calling function (in fcinfo->flinfo->fn_mcxt postgres memory
        context).

git-svn-id: http://svn.osgeo.org/postgis/trunk@7106 b70326c6-7e19-0410-871a-916f4a2858ee

raster/rt_core/rt_api.c
raster/rt_core/rt_api.h
raster/rt_pg/rt_pg.c
raster/test/core/testapi.c
raster/test/core/testwkb.c
raster/test/regress/rt_addband_expected
raster/test/regress/rt_mapalgebra_expected

index 4a3f5d980d70fd5ffa81da09a6267d8b1a402d97..845d1bece816d7abf7d2aaa6d253573e2e5b45d1 100644 (file)
@@ -37,6 +37,7 @@
 
 #define POSTGIS_RASTER_WARN_ON_TRUNCATION
 
+
 /*--- Utilities -------------------------------------------------*/
 
 static void
@@ -125,56 +126,91 @@ rt_util_clamp_to_32F(double value) {
     return (float)fmin(fmax((value), -FLT_MAX), FLT_MAX);
 }
 
+
 /*- rt_context -------------------------------------------------------*/
 
-static void
-default_error_handler(const char *fmt, ...) {
-    va_list ap;
+/* Functions definitions */
+void * init_rt_allocator(size_t size);
+void * init_rt_reallocator(void * mem, size_t size);
+void init_rt_deallocator(void * mem);
+void init_rt_errorreporter(const char * fmt, va_list ap);
+void init_rt_warnreporter(const char * fmt, va_list ap);
+void init_rt_inforeporter(const char * fmt, va_list ap);
+
+
+
+/*
+ * Default allocators
+ *
+ * We include some default allocators that use malloc/free/realloc
+ * along with stdout/stderr since this is the most common use case
+ *
+ */
+void *
+default_rt_allocator(size_t size)
+{
+    void *mem = malloc(size);
+    return mem;
+}
+
+void *
+default_rt_reallocator(void *mem, size_t size)
+{
+    void *ret = realloc(mem, size);
+    return ret;
+}
+
+void
+default_rt_deallocator(void *mem)
+{
+    free(mem);
+}
+
+
+void
+default_rt_error_handler(const char *fmt, va_list ap) {
 
     static const char *label = "ERROR: ";
     char newfmt[1024] = {0};
     snprintf(newfmt, 1024, "%s%s\n", label, fmt);
     newfmt[1023] = '\0';
 
-    va_start(ap, fmt);
-
     vprintf(newfmt, ap);
 
     va_end(ap);
 }
-
-static void
-default_warning_handler(const char *fmt, ...) {
-    va_list ap;
-
+       
+void
+default_rt_warning_handler(const char *fmt, va_list ap) {
+       
     static const char *label = "WARNING: ";
     char newfmt[1024] = {0};
     snprintf(newfmt, 1024, "%s%s\n", label, fmt);
     newfmt[1023] = '\0';
-
-    va_start(ap, fmt);
-
+       
     vprintf(newfmt, ap);
-
+       
     va_end(ap);
 }
 
-static void
-default_info_handler(const char *fmt, ...) {
-    va_list ap;
+
+void
+default_rt_info_handler(const char *fmt, va_list ap) {
 
     static const char *label = "INFO: ";
     char newfmt[1024] = {0};
     snprintf(newfmt, 1024, "%s%s\n", label, fmt);
     newfmt[1023] = '\0';
-
-    va_start(ap, fmt);
-
+       
     vprintf(newfmt, ap);
 
     va_end(ap);
 }
 
+
+/**
+ * Struct definition here
+ */
 struct rt_context_t {
     rt_allocator alloc;
     rt_reallocator realloc;
@@ -184,70 +220,200 @@ struct rt_context_t {
     rt_message_handler info;
 };
 
-rt_context
-rt_context_new(rt_allocator allocator, rt_reallocator reallocator,
-        rt_deallocator deallocator) {
-    rt_context ret;
+/* Static variable, to be used for all rt_core functions */
+static struct rt_context_t ctx_t = {
+    .alloc = init_rt_allocator,
+    .realloc = init_rt_reallocator,
+    .dealloc = init_rt_deallocator,
+    .err = init_rt_errorreporter,
+    .warn = init_rt_warnreporter,
+    .info = init_rt_inforeporter
+};
 
-    if (!allocator) allocator = malloc;
-    if (!reallocator) reallocator = realloc;
-    if (!deallocator) deallocator = free;
 
-    ret = (rt_context) allocator(sizeof (struct rt_context_t));
-    if (!ret) {
-        default_error_handler("Out of virtual memory creating an rt_context");
-        return 0;
-    }
+/**
+ * This function is normally called by rt_init_allocators when no special memory
+ * management is needed. Useful in raster core testing and in the (future)
+ * loader, when we need to use raster core functions but we don't have
+ * PostgreSQL backend behind. We must take care of memory by ourselves in those
+ * situations
+ */
+void
+rt_install_default_allocators(void)
+{
+    ctx_t.alloc = default_rt_allocator;
+    ctx_t.realloc = default_rt_reallocator;
+    ctx_t.dealloc = default_rt_deallocator;
+    ctx_t.err = default_rt_error_handler;
+    ctx_t.info = default_rt_info_handler;
+    ctx_t.warn = default_rt_warning_handler;    
+}
 
-    // Can not be used here
-    //RASTER_DEBUGF(3, "Created rt_context @ %p", ret);
 
-    ret->alloc = allocator;
-    ret->realloc = reallocator;
-    ret->dealloc = deallocator;
-    ret->err = default_error_handler;
-    ret->warn = default_warning_handler;
-    ret->info = default_info_handler;
+/**
+ * This function is called by rt_init_allocators when the PostgreSQL backend is
+ * taking care of the memory and we want to use palloc family
+ */
+void
+rt_set_handlers(rt_allocator allocator, rt_reallocator reallocator,
+        rt_deallocator deallocator, rt_message_handler error_handler, 
+        rt_message_handler info_handler, rt_message_handler warning_handler) {
 
-    assert(NULL != ret->alloc);
-    assert(NULL != ret->realloc);
-    assert(NULL != ret->dealloc);
-    assert(NULL != ret->err);
-    assert(NULL != ret->warn);
-    assert(NULL != ret->info);
-    return ret;
+    ctx_t.alloc = allocator;
+    ctx_t.realloc = reallocator;
+    ctx_t.dealloc = deallocator;
+
+    ctx_t.err = error_handler;
+    ctx_t.info = info_handler;
+    ctx_t.warn = warning_handler;
 }
 
+
+       
+/**
+ * Initialisation allocators
+ *
+ * These are used the first time any of the allocators are called to enable 
+ * executables/libraries that link into raster to be able to set up their own 
+ * allocators. This is mainly useful for older PostgreSQL versions that don't 
+ * have functions that are called upon startup.
+ **/
+void *
+init_rt_allocator(size_t size)
+{
+    rt_init_allocators();
+
+    return ctx_t.alloc(size);
+}
+       
 void
-rt_context_set_message_handlers(rt_context ctx,
-        rt_message_handler error_handler,
-        rt_message_handler warning_handler,
-        rt_message_handler info_handler) {
-    ctx->err = error_handler;
-    ctx->warn = warning_handler;
-    ctx->info = info_handler;
+init_rt_deallocator(void *mem)
+{
+    rt_init_allocators();
+       
+    ctx_t.dealloc(mem);
+}
+
+       
+void *
+init_rt_reallocator(void *mem, size_t size)
+{
+    rt_init_allocators();
 
-    assert(NULL != ctx->err);
-    assert(NULL != ctx->warn);
-    assert(NULL != ctx->info);
+    return ctx_t.realloc(mem, size);
+}
+       
+void
+init_rt_inforeporter(const char *fmt, va_list ap)
+{
+    rt_init_allocators();
+       
+    (*ctx_t.info)(fmt, ap);
 }
+       
+void
+init_rt_warnreporter(const char *fmt, va_list ap)
+{
+    rt_init_allocators();
+       
+    (*ctx_t.warn)(fmt, ap);
+}
+
+    
+void
+init_rt_errorreporter(const char *fmt, va_list ap)
+{
+    rt_init_allocators();
+
+    (*ctx_t.err)(fmt, ap);
+
+}
+
+
+
+/**
+ * Raster core memory management functions. 
+ * 
+ * They use the functions defined by the caller.
+ */ 
+void *
+rtalloc(size_t size) {
+    void * mem = ctx_t.alloc(size);
+    RASTER_DEBUGF(5, "rtalloc called: %d@%p", size, mem);
+    return mem;
+}
+
+
+void *
+rtrealloc(void * mem, size_t size) {
+    void * result = ctx_t.realloc(mem, size);
+    RASTER_DEBUGF(5, "rtrealloc called: %d@%p", size, result);
+    return result;
+}
+
+void
+rtdealloc(void * mem) {
+    ctx_t.dealloc(mem);
+    RASTER_DEBUG(5, "rtdealloc called");
+}
+
+/**
+ * Raster core error and info handlers
+ *
+ * Since variadic functions cannot pass their parameters directly, we need
+ * wrappers for these functions to convert the arguments into a va_list
+ * structure.
+ */
+void
+rterror(const char *fmt, ...) {
+    va_list ap;
+
+    va_start(ap, fmt);
+
+    /* Call the supplied function */
+    (*ctx_t.err)(fmt, ap);
+
+    va_end(ap);
+}
+
+void
+rtinfo(const char *fmt, ...) {
+    va_list ap;
+
+    va_start(ap, fmt);
+
+    /* Call the supplied function */
+    (*ctx_t.info)(fmt, ap);
+
+    va_end(ap);
+}
+
 
 void
-rt_context_destroy(rt_context ctx) {
-    RASTER_DEBUGF(3, "Destroying rt_context @ %p", ctx);
+rtwarn(const char *fmt, ...) {
+    va_list ap;
 
-    ctx->dealloc(ctx);
+    va_start(ap, fmt);
+
+    /* Call the supplied function */
+    (*ctx_t.warn)(fmt, ap);
+
+    va_end(ap);
 }
 
+
+
 int
-rt_util_display_dbl_trunc_warning(rt_context ctx,
-                                  double initialvalue,
+rt_util_display_dbl_trunc_warning(double initialvalue,
                                   int32_t checkvalint,
                                   uint32_t checkvaluint,
                                   float checkvalfloat,
                                   double checkvaldouble,
                                   rt_pixtype pixtype) {
     int result = 0;
+
+    
+
     switch (pixtype)
     {
         case PT_1BB:
@@ -260,14 +426,14 @@ rt_util_display_dbl_trunc_warning(rt_context ctx,
         case PT_32BSI:
         {
             if (fabs(checkvalint - initialvalue) >= 1) {
-                ctx->warn("Value set for %s band got clamped from %f to %d",
-                    rt_pixtype_name(ctx, pixtype),
+                rtwarn("Value set for %s band got clamped from %f to %d",
+                    rt_pixtype_name(pixtype),
                     initialvalue, checkvalint);
                 result = -1;
             }
             else if (fabs(checkvalint - initialvalue) > FLT_EPSILON) {
-                ctx->warn("Value set for %s band got truncated from %f to %d",
-                    rt_pixtype_name(ctx, pixtype),
+                rtwarn("Value set for %s band got truncated from %f to %d",
+                    rt_pixtype_name(pixtype),
                     initialvalue, checkvalint);
                 result = -1;
             }
@@ -276,14 +442,14 @@ rt_util_display_dbl_trunc_warning(rt_context ctx,
         case PT_32BUI:
         {
             if (fabs(checkvaluint - initialvalue) >= 1) {
-                ctx->warn("Value set for %s band got clamped from %f to %u",
-                    rt_pixtype_name(ctx, pixtype),
+                rtwarn("Value set for %s band got clamped from %f to %u",
+                    rt_pixtype_name(pixtype),
                     initialvalue, checkvaluint);
                 result = -1;
             }
             else if (fabs(checkvaluint - initialvalue) > FLT_EPSILON) {
-                ctx->warn("Value set for %s band got truncated from %f to %u",
-                    rt_pixtype_name(ctx, pixtype),
+                rtwarn("Value set for %s band got truncated from %f to %u",
+                    rt_pixtype_name(pixtype),
                     initialvalue, checkvaluint);
                 result = -1;
             }
@@ -294,16 +460,16 @@ rt_util_display_dbl_trunc_warning(rt_context ctx,
             /* For float, because the initial value is a double,
             there is very often a difference between the desired value and the obtained one */
             if (fabs(checkvalfloat - initialvalue) > FLT_EPSILON)
-                ctx->warn("Value set for %s band got converted from %f to %f",
-                    rt_pixtype_name(ctx, pixtype),
+                rtwarn("Value set for %s band got converted from %f to %f",
+                    rt_pixtype_name(pixtype),
                     initialvalue, checkvalfloat);
             break;
         }
         case PT_64BF:
         {
             if (fabs(checkvaldouble - initialvalue) > FLT_EPSILON)
-                ctx->warn("Value set for %s band got converted from %f to %f",
-                    rt_pixtype_name(ctx, pixtype),
+                rtwarn("Value set for %s band got converted from %f to %f",
+                    rt_pixtype_name(pixtype),
                     initialvalue, checkvaldouble);
             break;
         }
@@ -318,18 +484,19 @@ rt_util_display_dbl_trunc_warning(rt_context ctx,
 #if POSTGIS_DEBUG_LEVEL > 3
 
 static char*
-d_binary_to_hex(rt_context ctx, const uint8_t * const raw, uint32_t size, uint32_t *hexsize) {
+d_binary_to_hex(const uint8_t * const raw, uint32_t size, uint32_t *hexsize) {
     char* hex = NULL;
     uint32_t i = 0;
 
-    assert(NULL != ctx);
+    
     assert(NULL != raw);
     assert(NULL != hexsize);
+    
 
     *hexsize = size * 2; /* hex is 2 times bytes */
-    hex = (char*) ctx->alloc((*hexsize) + 1);
+    hex = (char*) rtalloc((*hexsize) + 1);
     if (!hex) {
-        ctx->err("d_binary_to_hex: Out of memory hexifying raw binary\n");
+        rterror("d_binary_to_hex: Out of memory hexifying raw binary\n");
         return NULL;
     }
     hex[*hexsize] = '\0'; /* Null-terminate */
@@ -344,18 +511,19 @@ d_binary_to_hex(rt_context ctx, const uint8_t * const raw, uint32_t size, uint32
 }
 
 static void
-d_print_binary_hex(rt_context ctx, const char* msg, const uint8_t * const raw, uint32_t size) {
+d_print_binary_hex(const char* msg, const uint8_t * const raw, uint32_t size) {
     char* hex = NULL;
     uint32_t hexsize = 0;
 
-    assert(NULL != ctx);
+    
     assert(NULL != msg);
     assert(NULL != raw);
+    
 
-    hex = d_binary_to_hex(ctx, raw, size, &hexsize);
+    hex = d_binary_to_hex(raw, size, &hexsize);
     if (NULL != hex) {
-        ctx->info("%s\t%s", msg, hex);
-        ctx->dealloc(hex);
+        rtinfo("%s\t%s", msg, hex);
+        rtdealloc(hex);
     }
 }
 
@@ -381,10 +549,10 @@ d_binptr_to_pos(const uint8_t * const ptr, const uint8_t * const end, size_t siz
 /*- rt_pixeltype -----------------------------------------------------*/
 
 int
-rt_pixtype_size(rt_context ctx, rt_pixtype pixtype) {
+rt_pixtype_size(rt_pixtype pixtype) {
     int pixbytes = -1;
-
-    assert(NULL != ctx);
+    
+    
 
     switch (pixtype) {
         case PT_1BB:
@@ -407,26 +575,28 @@ rt_pixtype_size(rt_context ctx, rt_pixtype pixtype) {
             pixbytes = 8;
             break;
         default:
-            ctx->err("rt_pixtype_size: Unknown pixeltype %d", pixtype);
+            rterror("rt_pixtype_size: Unknown pixeltype %d", pixtype);
             pixbytes = -1;
             break;
     }
 
     RASTER_DEBUGF(3, "Pixel type = %s and size = %d bytes",
-            rt_pixtype_name(ctx, pixtype), pixbytes);
+            rt_pixtype_name(pixtype), pixbytes);
 
 
     return pixbytes;
 }
 
 int
-rt_pixtype_alignment(rt_context ctx, rt_pixtype pixtype) {
-    return rt_pixtype_size(ctx, pixtype);
+rt_pixtype_alignment(rt_pixtype pixtype) {
+    
+    return rt_pixtype_size(pixtype);
 }
 
 rt_pixtype
-rt_pixtype_index_from_name(rt_context ctx, const char* pixname) {
-    assert(strlen(pixname) > 0);
+rt_pixtype_index_from_name(const char* pixname) {
+    assert(pixname && strlen(pixname) > 0);
+    
 
     if (strcmp(pixname, "1BB") == 0)
         return PT_1BB;
@@ -454,8 +624,9 @@ rt_pixtype_index_from_name(rt_context ctx, const char* pixname) {
 }
 
 const char*
-rt_pixtype_name(rt_context ctx, rt_pixtype pixtype) {
-    assert(NULL != ctx);
+rt_pixtype_name(rt_pixtype pixtype) {
+    
+    
 
     switch (pixtype) {
         case PT_1BB:
@@ -481,7 +652,7 @@ rt_pixtype_name(rt_context ctx, rt_pixtype pixtype) {
         case PT_64BF:
             return "64BF";
         default:
-            ctx->err("rt_pixtype_name: Unknown pixeltype %d", pixtype);
+            rterror("rt_pixtype_name: Unknown pixeltype %d", pixtype);
             return "Unknown";
     }
 }
@@ -512,22 +683,22 @@ struct rt_band_t {
 };
 
 rt_band
-rt_band_new_inline(rt_context ctx, uint16_t width, uint16_t height,
+rt_band_new_inline(uint16_t width, uint16_t height,
         rt_pixtype pixtype, uint32_t hasnodata, double nodataval,
         uint8_t* data) {
     rt_band band = NULL;
 
-    assert(NULL != ctx);
+    
     assert(NULL != data);
 
-    band = ctx->alloc(sizeof (struct rt_band_t));
+    band = rtalloc(sizeof (struct rt_band_t));
     if (!band) {
-        ctx->err("rt_band_new_inline: Out of memory allocating rt_band");
+        rterror("rt_band_new_inline: Out of memory allocating rt_band");
         return 0;
     }
 
     RASTER_DEBUGF(3, "Created rt_band @ %p with pixtype %s",
-            band, rt_pixtype_name(ctx, pixtype));
+            band, rt_pixtype_name(pixtype));
 
 
     band->pixtype = pixtype;
@@ -544,23 +715,23 @@ rt_band_new_inline(rt_context ctx, uint16_t width, uint16_t height,
 }
 
 rt_band
-rt_band_new_offline(rt_context ctx, uint16_t width, uint16_t height,
+rt_band_new_offline(uint16_t width, uint16_t height,
         rt_pixtype pixtype, uint32_t hasnodata, double nodataval,
         uint8_t bandNum, const char* path) {
     rt_band band = NULL;
-
-    assert(NULL != ctx);
+    
     assert(NULL != path);
 
-    band = ctx->alloc(sizeof (struct rt_band_t));
+    band = rtalloc(sizeof (struct rt_band_t));
     if (!band) {
-        ctx->err("rt_band_new_offline: Out of memory allocating rt_band");
+        rterror("rt_band_new_offline: Out of memory allocating rt_band");
         return 0;
     }
 
 
     RASTER_DEBUGF(3, "Created rt_band @ %p with pixtype %s",
-            band, rt_pixtype_name(ctx, pixtype));
+            band, rt_pixtype_name(pixtype));
 
     band->pixtype = pixtype;
     band->offline = 1;
@@ -585,29 +756,32 @@ rt_band_new_offline(rt_context ctx, uint16_t width, uint16_t height,
 }
 
 int
-rt_band_is_offline(rt_context ctx, rt_band band) {
-    assert(NULL != ctx);
+rt_band_is_offline(rt_band band) {
+    
     assert(NULL != band);
+    
 
     return band->offline;
 }
 
 void
-rt_band_destroy(rt_context ctx, rt_band band) {
+rt_band_destroy(rt_band band) {
+    
+    
     RASTER_DEBUGF(3, "Destroying rt_band @ %p", band);
 
-
     /* band->data content is externally owned */
     /* XXX jorgearevalo: not really... rt_band_from_wkb allocates memory for
      * data.mem
      */
-    ctx->dealloc(band);
+    rtdealloc(band);
 }
 
 const char*
-rt_band_get_ext_path(rt_context ctx, rt_band band) {
-    assert(NULL != ctx);
+rt_band_get_ext_path(rt_band band) {
+    
     assert(NULL != band);
+    
 
     if (!band->offline) {
         RASTER_DEBUG(3, "rt_band_get_ext_path: non-offline band doesn't have "
@@ -618,9 +792,10 @@ rt_band_get_ext_path(rt_context ctx, rt_band band) {
 }
 
 uint8_t
-rt_band_get_ext_band_num(rt_context ctx, rt_band band) {
-    assert(NULL != ctx);
+rt_band_get_ext_band_num(rt_band band) {
+    
     assert(NULL != band);
+    
 
     if (!band->offline) {
         RASTER_DEBUG(3, "rt_band_get_ext_path: non-offline band doesn't have "
@@ -631,9 +806,10 @@ rt_band_get_ext_band_num(rt_context ctx, rt_band band) {
 }
 
 void *
-rt_band_get_data(rt_context ctx, rt_band band) {
-    assert(NULL != ctx);
+rt_band_get_data(rt_band band) {
+    
     assert(NULL != band);
+    
 
     if (band->offline) {
         RASTER_DEBUG(3, "rt_band_get_data: "
@@ -644,25 +820,28 @@ rt_band_get_data(rt_context ctx, rt_band band) {
 }
 
 rt_pixtype
-rt_band_get_pixtype(rt_context ctx, rt_band band) {
-    assert(NULL != ctx);
+rt_band_get_pixtype(rt_band band) {
+    
     assert(NULL != band);
+    
 
     return band->pixtype;
 }
 
 uint16_t
-rt_band_get_width(rt_context ctx, rt_band band) {
-    assert(NULL != ctx);
+rt_band_get_width(rt_band band) {
+    
     assert(NULL != band);
+    
 
     return band->width;
 }
 
 uint16_t
-rt_band_get_height(rt_context ctx, rt_band band) {
-    assert(NULL != ctx);
+rt_band_get_height(rt_band band) {
+    
     assert(NULL != band);
+    
 
     return band->height;
 }
@@ -687,6 +866,7 @@ setBits(char* ch, double val, int bits, int bitOffset) {
     char mask = 0xFF >> (8 - bits);
     char ival = val;
 
+    
     assert(8 - bitOffset >= bits);
 
     RASTER_DEBUGF(4, "ival:%d bits:%d mask:%hhx bitoffset:%d\n",
@@ -696,7 +876,7 @@ setBits(char* ch, double val, int bits, int bitOffset) {
     ival &= mask;
 #ifdef POSTGIS_RASTER_WARN_ON_TRUNCATION
     if (ival != val) {
-        ctx->warn("Pixel value for %d-bits band got truncated"
+        rtwarn("Pixel value for %d-bits band got truncated"
                 " from %g to %hhu\n", bits, val, ival);
     }
 #endif /* POSTGIS_RASTER_WARN_ON_TRUNCATION */
@@ -725,39 +905,42 @@ setBits(char* ch, double val, int bits, int bitOffset) {
 #endif /* OPTIMIZE_SPACE */
 
 int
-rt_band_get_hasnodata_flag(rt_context ctx, rt_band band) {
-    assert(NULL != ctx);
+rt_band_get_hasnodata_flag(rt_band band) {
+    
     assert(NULL != band);
+    
 
     return band->hasnodata;
 }
 
 void
-rt_band_set_hasnodata_flag(rt_context ctx, rt_band band, int flag) {
-    assert(NULL != ctx);
+rt_band_set_hasnodata_flag(rt_band band, int flag) {
+    
     assert(NULL != band);
+    
 
     band->hasnodata = (flag) ? 1 : 0;
 }
 
 void
-rt_band_set_isnodata_flag(rt_context ctx, rt_band band, int flag) {
-    assert(NULL != ctx);
+rt_band_set_isnodata_flag(rt_band band, int flag) {
+    
     assert(NULL != band);
+    
 
     band->isnodata = (flag) ? 1 : 0;
 }
 
 int
-rt_band_get_isnodata_flag(rt_context ctx, rt_band band) {
-    assert(NULL != ctx);
+rt_band_get_isnodata_flag(rt_band band) {
+    
     assert(NULL != band);
 
     return band->isnodata;
 }
 
 int
-rt_band_set_nodata(rt_context ctx, rt_band band, double val) {
+rt_band_set_nodata(rt_band band, double val) {
     rt_pixtype pixtype = PT_END;
     //double oldnodataval = band->nodataval;
 
@@ -766,12 +949,13 @@ rt_band_set_nodata(rt_context ctx, rt_band band, double val) {
     float checkvalfloat = 0;
     double checkvaldouble = 0;
 
-    assert(NULL != ctx);
+    
+    
     assert(NULL != band);
 
     pixtype = band->pixtype;
 
-    RASTER_DEBUGF(3, "rt_band_set_nodata: setting nodata value %g with band type %s", val, rt_pixtype_name(ctx, pixtype));
+    RASTER_DEBUGF(3, "rt_band_set_nodata: setting nodata value %g with band type %s", val, rt_pixtype_name(pixtype));
 
     /* return -1 on out of range */
     switch (pixtype) {
@@ -843,7 +1027,7 @@ rt_band_set_nodata(rt_context ctx, rt_band band, double val) {
         }
         default:
             {
-            ctx->err("rt_band_set_nodata: Unknown pixeltype %d", pixtype);
+            rterror("rt_band_set_nodata: Unknown pixeltype %d", pixtype);
             band->hasnodata = 0;
             return -1;
         }
@@ -854,10 +1038,10 @@ rt_band_set_nodata(rt_context ctx, rt_band band, double val) {
 
 
     // the nodata value was just set, so this band has NODATA
-    rt_band_set_hasnodata_flag(ctx, band, 1);
+    rt_band_set_hasnodata_flag(band, 1);
 
 #ifdef POSTGIS_RASTER_WARN_ON_TRUNCATION
-    if (rt_util_display_dbl_trunc_warning(ctx, val, checkvalint, checkvaluint, checkvalfloat,
+    if (rt_util_display_dbl_trunc_warning(val, checkvalint, checkvaluint, checkvalfloat,
                                       checkvaldouble, pixtype))
         return -1;
 #endif
@@ -869,14 +1053,14 @@ rt_band_set_nodata(rt_context ctx, rt_band band, double val) {
 
     /*
     if (fabs(band->nodataval - oldnodataval) > FLT_EPSILON)
-        rt_band_check_is_nodata(ctx, band);
+        rt_band_check_is_nodata(band);
     */
 
     return 0;
 }
 
 int
-rt_band_set_pixel(rt_context ctx, rt_band band, uint16_t x, uint16_t y,
+rt_band_set_pixel(rt_band band, uint16_t x, uint16_t y,
         double val) {
     rt_pixtype pixtype = PT_END;
     unsigned char* data = NULL;
@@ -889,22 +1073,23 @@ rt_band_set_pixel(rt_context ctx, rt_band band, uint16_t x, uint16_t y,
 
     double checkval = 0;
 
-    assert(NULL != ctx);
+    
+    
     assert(NULL != band);
 
     pixtype = band->pixtype;
 
     if (x >= band->width || y >= band->height) {
-        ctx->err("rt_band_set_pixel: Coordinates out of range");
+        rterror("rt_band_set_pixel: Coordinates out of range");
         return -1;
     }
 
     if (band->offline) {
-        ctx->err("rt_band_set_pixel not implemented yet for OFFDB bands");
+        rterror("rt_band_set_pixel not implemented yet for OFFDB bands");
         return -1;
     }
 
-    data = rt_band_get_data(ctx, band);
+    data = rt_band_get_data(band);
     offset = x + (y * band->width);
 
     switch (pixtype) {
@@ -982,14 +1167,14 @@ rt_band_set_pixel(rt_context ctx, rt_band band, uint16_t x, uint16_t y,
         }
         default:
         {
-            ctx->err("rt_band_set_pixel: Unknown pixeltype %d", pixtype);
+            rterror("rt_band_set_pixel: Unknown pixeltype %d", pixtype);
             return -1;
         }
     }
 
     /* Overflow checking */
 #ifdef POSTGIS_RASTER_WARN_ON_TRUNCATION
-    if (rt_util_display_dbl_trunc_warning(ctx, val, checkvalint, checkvaluint, checkvalfloat,
+    if (rt_util_display_dbl_trunc_warning(val, checkvalint, checkvaluint, checkvalfloat,
                                       checkvaldouble, pixtype))
        return -1;
 #endif /* POSTGIS_RASTER_WARN_ON_TRUNCATION */
@@ -1006,7 +1191,7 @@ rt_band_set_pixel(rt_context ctx, rt_band band, uint16_t x, uint16_t y,
 
     /*
     else {
-        rt_band_check_is_nodata(ctx, band);
+        rt_band_check_is_nodata(band);
     }
     */
 
@@ -1015,27 +1200,28 @@ rt_band_set_pixel(rt_context ctx, rt_band band, uint16_t x, uint16_t y,
 }
 
 int
-rt_band_get_pixel(rt_context ctx, rt_band band, uint16_t x, uint16_t y, double *result) {
+rt_band_get_pixel(rt_band band, uint16_t x, uint16_t y, double *result) {
     rt_pixtype pixtype = PT_END;
     uint8_t* data = NULL;
     uint32_t offset = 0;
 
-    assert(NULL != ctx);
+    
+    
     assert(NULL != band);
 
     pixtype = band->pixtype;
 
     if (x >= band->width || y >= band->height) {
-        ctx->warn("Attempting to get pixel value with out of range raster coordinates");
+        rterror("Attempting to get pixel value with out of range raster coordinates");
         return -1;
     }
 
     if (band->offline) {
-        ctx->err("rt_band_get_pixel not implemented yet for OFFDB bands");
+        rterror("rt_band_get_pixel not implemented yet for OFFDB bands");
         return -1;
     }
 
-    data = rt_band_get_data(ctx, band);
+    data = rt_band_get_data(band);
     offset = x + (y * band->width); /* +1 for the nodata value */
 
     switch (pixtype) {
@@ -1128,15 +1314,16 @@ rt_band_get_pixel(rt_context ctx, rt_band band, uint16_t x, uint16_t y, double *
         }
         default:
         {
-            ctx->err("rt_band_get_pixel: Unknown pixeltype %d", pixtype);
+            rterror("rt_band_get_pixel: Unknown pixeltype %d", pixtype);
             return -1;
         }
     }
 }
 
 double
-rt_band_get_nodata(rt_context ctx, rt_band band) {
-    assert(NULL != ctx);
+rt_band_get_nodata(rt_band band) {
+    
+    
     assert(NULL != band);
 
     if (!band->hasnodata)
@@ -1147,10 +1334,11 @@ rt_band_get_nodata(rt_context ctx, rt_band band) {
 }
 
 double
-rt_band_get_min_value(rt_context ctx, rt_band band) {
+rt_band_get_min_value(rt_band band) {
     rt_pixtype pixtype = PT_END;
 
-    assert(NULL != ctx);
+    
+    
     assert(NULL != band);
 
     pixtype = band->pixtype;
@@ -1187,7 +1375,7 @@ rt_band_get_min_value(rt_context ctx, rt_band band) {
         }
         default:
         {
-            ctx->err("rt_band_get_min_value: Unknown pixeltype %d", pixtype);
+            rterror("rt_band_get_min_value: Unknown pixeltype %d", pixtype);
             return (double)CHAR_MIN;
         }
     }
@@ -1195,13 +1383,14 @@ rt_band_get_min_value(rt_context ctx, rt_band band) {
 
 
 int
-rt_band_check_is_nodata(rt_context ctx, rt_band band)
+rt_band_check_is_nodata(rt_band band)
 {
     int i, j;
     double pxValue = band->nodataval;
     double dEpsilon = 0.0;
 
-    assert(NULL != ctx);
+    
+    
     assert(NULL != band);
 
     /* Check if band has nodata value */
@@ -1223,7 +1412,7 @@ rt_band_check_is_nodata(rt_context ctx, rt_band band)
     {
         for(j = 0; j < band->height; j++)
         {
-            rt_band_get_pixel(ctx, band, i, j, &pxValue);
+            rt_band_get_pixel(band, i, j, &pxValue);
             dEpsilon = fabs(pxValue - band->nodataval);
             if (dEpsilon > FLT_EPSILON) {
                 band->isnodata = FALSE;
@@ -1296,15 +1485,14 @@ struct rt_raster_t {
 };
 
 rt_raster
-rt_raster_new(rt_context ctx, uint16_t width, uint16_t height) {
+rt_raster_new(uint16_t width, uint16_t height) {
     rt_raster ret = NULL;
 
-    assert(NULL != ctx);
-    assert(NULL != ctx->alloc);
-
-    ret = (rt_raster) ctx->alloc(sizeof (struct rt_raster_t));
+    
+    
+    ret = (rt_raster) rtalloc(sizeof (struct rt_raster_t));
     if (!ret) {
-        ctx->err("rt_raster_new: Out of virtual memory creating an rt_raster");
+        rterror("rt_raster_new: Out of virtual memory creating an rt_raster");
         return 0;
     }
 
@@ -1330,35 +1518,38 @@ rt_raster_new(rt_context ctx, uint16_t width, uint16_t height) {
 }
 
 void
-rt_raster_destroy(rt_context ctx, rt_raster raster) {
-    RASTER_DEBUGF(3, "Destroying rt_raster @ %p", ctx);
+rt_raster_destroy(rt_raster raster) {
+    
+    
+    RASTER_DEBUGF(3, "Destroying rt_raster @ %p", raster);
 
     if (raster->bands) {
-        ctx->dealloc(raster->bands);
+        rtdealloc(raster->bands);
     }
-    ctx->dealloc(raster);
+    rtdealloc(raster);
 }
 
 uint16_t
-rt_raster_get_width(rt_context ctx, rt_raster raster) {
-    assert(NULL != ctx);
+rt_raster_get_width(rt_raster raster) {
+    
     assert(NULL != raster);
 
     return raster->width;
 }
 
 uint16_t
-rt_raster_get_height(rt_context ctx, rt_raster raster) {
-    assert(NULL != ctx);
+rt_raster_get_height(rt_raster raster) {
+    
     assert(NULL != raster);
 
     return raster->height;
 }
 
 void
-rt_raster_set_scale(rt_context ctx, rt_raster raster,
+rt_raster_set_scale(rt_raster raster,
         double scaleX, double scaleY) {
-    assert(NULL != ctx);
+    
+    
     assert(NULL != raster);
 
     raster->scaleX = scaleX;
@@ -1366,25 +1557,28 @@ rt_raster_set_scale(rt_context ctx, rt_raster raster,
 }
 
 double
-rt_raster_get_x_scale(rt_context ctx, rt_raster raster) {
-    assert(NULL != ctx);
+rt_raster_get_x_scale(rt_raster raster) {
+    
+    
     assert(NULL != raster);
 
     return raster->scaleX;
 }
 
 double
-rt_raster_get_y_scale(rt_context ctx, rt_raster raster) {
-    assert(NULL != ctx);
+rt_raster_get_y_scale(rt_raster raster) {
+    
+    
     assert(NULL != raster);
 
     return raster->scaleY;
 }
 
 void
-rt_raster_set_skews(rt_context ctx, rt_raster raster,
+rt_raster_set_skews(rt_raster raster,
         double skewX, double skewY) {
-    assert(NULL != ctx);
+    
+    
     assert(NULL != raster);
 
     raster->skewX = skewX;
@@ -1392,24 +1586,27 @@ rt_raster_set_skews(rt_context ctx, rt_raster raster,
 }
 
 double
-rt_raster_get_x_skew(rt_context ctx, rt_raster raster) {
-    assert(NULL != ctx);
+rt_raster_get_x_skew(rt_raster raster) {
+    
+    
     assert(NULL != raster);
 
     return raster->skewX;
 }
 
 double
-rt_raster_get_y_skew(rt_context ctx, rt_raster raster) {
-    assert(NULL != ctx);
+rt_raster_get_y_skew(rt_raster raster) {
+    
+    
     assert(NULL != raster);
 
     return raster->skewY;
 }
 
 void
-rt_raster_set_offsets(rt_context ctx, rt_raster raster, double x, double y) {
-    assert(NULL != ctx);
+rt_raster_set_offsets(rt_raster raster, double x, double y) {
+    
+    
     assert(NULL != raster);
 
     raster->ipX = x;
@@ -1417,48 +1614,54 @@ rt_raster_set_offsets(rt_context ctx, rt_raster raster, double x, double y) {
 }
 
 double
-rt_raster_get_x_offset(rt_context ctx, rt_raster raster) {
-    assert(NULL != ctx);
+rt_raster_get_x_offset(rt_raster raster) {
+    
+    
     assert(NULL != raster);
 
     return raster->ipX;
 }
 
 double
-rt_raster_get_y_offset(rt_context ctx, rt_raster raster) {
-    assert(NULL != ctx);
+rt_raster_get_y_offset(rt_raster raster) {
+    
+    
     assert(NULL != raster);
 
     return raster->ipY;
 }
 
 int32_t
-rt_raster_get_srid(rt_context ctx, rt_raster raster) {
-    assert(NULL != ctx);
+rt_raster_get_srid(rt_raster raster) {
+    
+    
     assert(NULL != raster);
 
     return raster->srid;
 }
 
 void
-rt_raster_set_srid(rt_context ctx, rt_raster raster, int32_t srid) {
-    assert(NULL != ctx);
+rt_raster_set_srid(rt_raster raster, int32_t srid) {
+    
+    
     assert(NULL != raster);
 
     raster->srid = srid;
 }
 
 int
-rt_raster_get_num_bands(rt_context ctx, rt_raster raster) {
-    assert(NULL != ctx);
+rt_raster_get_num_bands(rt_raster raster) {
+    
+    
     assert(NULL != raster);
 
     return raster->numBands;
 }
 
 rt_band
-rt_raster_get_band(rt_context ctx, rt_raster raster, int n) {
-    assert(NULL != ctx);
+rt_raster_get_band(rt_raster raster, int n) {
+    
+    
     assert(NULL != raster);
 
     if (n >= raster->numBands || n < 0) return 0;
@@ -1466,19 +1669,20 @@ rt_raster_get_band(rt_context ctx, rt_raster raster, int n) {
 }
 
 int32_t
-rt_raster_add_band(rt_context ctx, rt_raster raster, rt_band band, int index) {
+rt_raster_add_band(rt_raster raster, rt_band band, int index) {
     rt_band *oldbands = NULL;
     rt_band oldband = NULL;
     rt_band tmpband = NULL;
     uint16_t i = 0;
 
-    assert(NULL != ctx);
+    
+    
     assert(NULL != raster);
 
     RASTER_DEBUGF(3, "Adding band %p to raster %p", band, raster);
 
     if (band->width != raster->width || band->height != raster->height) {
-        ctx->err("rt_raster_add_band: Can't add a %dx%d band to a %dx%d raster",
+        rterror("rt_raster_add_band: Can't add a %dx%d band to a %dx%d raster",
                 band->width, band->height, raster->width, raster->height);
         return -1;
     }
@@ -1493,12 +1697,14 @@ rt_raster_add_band(rt_context ctx, rt_raster raster, rt_band band, int index) {
 
     RASTER_DEBUGF(3, "Oldbands at %p", oldbands);
 
-    raster->bands = (rt_band*) ctx->realloc(raster->bands,
+    raster->bands = (rt_band*) rtrealloc(raster->bands,
             sizeof (rt_band)*(raster->numBands + 1)
             );
 
-    if (!raster->bands) {
-        ctx->err("rt_raster_add_band: Out of virtual memory "
+    RASTER_DEBUG(3, "Checking bands");
+
+    if (NULL == raster->bands) {
+        rterror("rt_raster_add_band: Out of virtual memory "
                 "reallocating band pointers");
         raster->bands = oldbands;
         return -1;
@@ -1526,7 +1732,7 @@ rt_raster_add_band(rt_context ctx, rt_raster raster, rt_band band, int index) {
 
 
 int32_t
-rt_raster_generate_new_band(rt_context ctx, rt_raster raster, rt_pixtype pixtype,
+rt_raster_generate_new_band(rt_raster raster, rt_pixtype pixtype,
         double initialvalue, uint32_t hasnodata, double nodatavalue, int index)
 {
     rt_band band = NULL;
@@ -1542,27 +1748,26 @@ rt_raster_generate_new_band(rt_context ctx, rt_raster raster, rt_pixtype pixtype
     double checkvaldouble = 0;
     float checkvalfloat = 0;
     int i;
-
-
-    assert(NULL != ctx);
+    
+    
     assert(NULL != raster);
 
     /* Make sure index is in a valid range */
-    oldnumbands = rt_raster_get_num_bands(ctx, raster);
+    oldnumbands = rt_raster_get_num_bands(raster);
     if (index < 0)
         index = 0;
-    else if (index > rt_raster_get_num_bands(ctx, raster) + 1)
-        index = rt_raster_get_num_bands(ctx, raster) + 1;
+    else if (index > rt_raster_get_num_bands(raster) + 1)
+        index = rt_raster_get_num_bands(raster) + 1;
 
     /* Determine size of memory block to allocate and allocate it */
-    width = rt_raster_get_width(ctx, raster);
-    height = rt_raster_get_height(ctx, raster);
+    width = rt_raster_get_width(raster);
+    height = rt_raster_get_height(raster);
     numval = width * height;
-    datasize = rt_pixtype_size(ctx, pixtype) * numval;
+    datasize = rt_pixtype_size(pixtype) * numval;
 
-    mem = (int *)ctx->alloc(datasize);
+    mem = (int *)rtalloc(datasize);
     if (!mem) {
-        ctx->err("rt_raster_generate_new_band: Could not allocate memory for band");
+        rterror("rt_raster_generate_new_band: Could not allocate memory for band");
         return -1;
     }
 
@@ -1671,8 +1876,8 @@ rt_raster_generate_new_band(rt_context ctx, rt_raster raster, rt_pixtype pixtype
             }
             default:
             {
-                ctx->err("rt_raster_generate_new_band: Unknown pixeltype %d", pixtype);
-                ctx->dealloc(mem);
+                rterror("rt_raster_generate_new_band: Unknown pixeltype %d", pixtype);
+                rtdealloc(mem);
                 return -1;
             }
         }
@@ -1680,21 +1885,21 @@ rt_raster_generate_new_band(rt_context ctx, rt_raster raster, rt_pixtype pixtype
 
 #ifdef POSTGIS_RASTER_WARN_ON_TRUNCATION
     /* Overflow checking */
-    rt_util_display_dbl_trunc_warning(ctx, initialvalue, checkvalint, checkvaluint, checkvalfloat,
+    rt_util_display_dbl_trunc_warning(initialvalue, checkvalint, checkvaluint, checkvalfloat,
                                       checkvaldouble, pixtype);
 #endif /* POSTGIS_RASTER_WARN_ON_TRUNCATION */
 
-    band = rt_band_new_inline(ctx, width, height, pixtype, hasnodata, nodatavalue, mem);
+    band = rt_band_new_inline(width, height, pixtype, hasnodata, nodatavalue, mem);
     if (! band) {
-        ctx->err("rt_raster_generate_new_band: Could not add band to raster. Aborting");
-        ctx->dealloc(mem);
+        rterror("rt_raster_generate_new_band: Could not add band to raster. Aborting");
+        rtdealloc(mem);
         return -1;
     }
-    index = rt_raster_add_band(ctx, raster, band, index);
-    numbands = rt_raster_get_num_bands(ctx, raster);
+    index = rt_raster_add_band(raster, band, index);
+    numbands = rt_raster_get_num_bands(raster);
     if (numbands == oldnumbands || index == -1) {
-        ctx->err("rt_raster_generate_new_band: Could not add band to raster. Aborting");
-        rt_band_destroy(ctx, band);
+        rterror("rt_raster_generate_new_band: Could not add band to raster. Aborting");
+        rt_band_destroy(band);
     }
 
     return index;
@@ -1702,10 +1907,11 @@ rt_raster_generate_new_band(rt_context ctx, rt_raster raster, rt_pixtype pixtype
 
 
 void
-rt_raster_cell_to_geopoint(rt_context ctx, rt_raster raster,
+rt_raster_cell_to_geopoint(rt_raster raster,
         double x, double y,
         double* x1, double* y1) {
-    assert(NULL != ctx);
+    
+    
     assert(NULL != raster);
     assert(NULL != x1);
     assert(NULL != y1);
@@ -1730,7 +1936,7 @@ struct rt_geomval_t {
 };
 
 rt_geomval
-rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
+rt_raster_dump_as_wktpolygons(rt_raster raster, int nband,
         int * pnElements) {
 
        char * pszQuery;
@@ -1759,18 +1965,19 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
     double dBandNoData = 0.0;
 
     /* Checkings */
-    assert(NULL != ctx);
+    
+    
     assert(NULL != raster);
-    assert(nband > 0 && nband <= rt_raster_get_num_bands(ctx, raster));
+    assert(nband > 0 && nband <= rt_raster_get_num_bands(raster));
 
     RASTER_DEBUG(2, "In rt_raster_dump_as_polygons");
 
     /*******************************
      * Get band
      *******************************/
-    band = rt_raster_get_band(ctx, raster, nband - 1);
+    band = rt_raster_get_band(raster, nband - 1);
     if (NULL == band) {
-        ctx->err("rt_raster_dump_as_wktpolygons: Error getting band %d from raster", nband);
+        rterror("rt_raster_dump_as_wktpolygons: Error getting band %d from raster", nband);
         return 0;
     }
 
@@ -1790,7 +1997,7 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
     memdatasource = OGR_Dr_CreateDataSource(ogr_drv, "", NULL);
 
     if (NULL == memdatasource) {
-        ctx->err("rt_raster_dump_as_wktpolygons: Couldn't create a OGR Datasource to store pols\n");
+        rterror("rt_raster_dump_as_wktpolygons: Couldn't create a OGR Datasource to store pols\n");
         return 0;
     }
 
@@ -1798,7 +2005,7 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
      * Can MEM driver create new layers?
      **/
     if (!OGR_DS_TestCapability(memdatasource, ODsCCreateLayer)) {
-        ctx->err("rt_raster_dump_as_wktpolygons: MEM driver can't create new layers, aborting\n");
+        rterror("rt_raster_dump_as_wktpolygons: MEM driver can't create new layers, aborting\n");
         /* xxx jorgearevalo: what should we do now? */
         OGRReleaseDataSource(memdatasource);
         return 0;
@@ -1817,11 +2024,11 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
      * First, create a Dataset with no bands using MEM driver
      **/
     gdal_drv = GDALGetDriverByName("MEM");
-    memdataset = GDALCreate(gdal_drv, "", rt_band_get_width(ctx, band),
-            rt_band_get_height(ctx, band), 0, GDT_Byte, NULL);
+    memdataset = GDALCreate(gdal_drv, "", rt_band_get_width(band),
+            rt_band_get_height(band), 0, GDT_Byte, NULL);
 
     if (NULL == memdataset) {
-        ctx->err("rt_raster_dump_as_wktpolygons: Couldn't create a GDALDataset to polygonize it\n");
+        rterror("rt_raster_dump_as_wktpolygons: Couldn't create a GDALDataset to polygonize it\n");
         GDALDeregisterDriver(gdal_drv);
         GDALDestroyDriver(gdal_drv);
         OGRReleaseDataSource(memdatasource);
@@ -1832,12 +2039,12 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
      * Add geotransform
      */
     double adfGeoTransform[6] = {0.0, 1.0, 0.0, 0.0, 0.0, 1.0};
-    adfGeoTransform[0] = rt_raster_get_x_offset(ctx, raster);
-    adfGeoTransform[1] = rt_raster_get_x_scale(ctx, raster);
-    adfGeoTransform[2] = rt_raster_get_x_skew(ctx, raster);
-    adfGeoTransform[3] = rt_raster_get_y_offset(ctx, raster);
-    adfGeoTransform[4] = rt_raster_get_y_skew(ctx, raster);
-    adfGeoTransform[5] = rt_raster_get_y_scale(ctx, raster);
+    adfGeoTransform[0] = rt_raster_get_x_offset(raster);
+    adfGeoTransform[1] = rt_raster_get_x_scale(raster);
+    adfGeoTransform[2] = rt_raster_get_x_skew(raster);
+    adfGeoTransform[3] = rt_raster_get_y_offset(raster);
+    adfGeoTransform[4] = rt_raster_get_y_skew(raster);
+    adfGeoTransform[5] = rt_raster_get_y_scale(raster);
     GDALSetGeoTransform(memdataset, adfGeoTransform);
 
     RASTER_DEBUG(3, "Adding GDAL MEM raster band");
@@ -1845,7 +2052,7 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
     /**
      * Now, add the raster band
      */
-    pt = rt_band_get_pixtype(ctx, band);
+    pt = rt_band_get_pixtype(band);
 
     switch (pt) {
         case PT_1BB: case PT_2BUI: case PT_4BUI: case PT_8BSI: case PT_8BUI:
@@ -1873,12 +2080,19 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
             break;
 
         default:
-            ctx->warn("Unknown pixel type for band\n");
+            rtwarn("Unknown pixel type for band\n");
             nPixelType = GDT_Unknown;
             break;
     }
 
-    void * pVoid = rt_band_get_data(ctx, band);
+    void * pVoid = rt_band_get_data(band);
+    if (NULL == pVoid) {
+        rterror("rt_raster_dump_as_wktpolygons: Couldn't get raster band data");
+        GDALDeregisterDriver(gdal_drv);
+        GDALDestroyDriver(gdal_drv);
+        OGRReleaseDataSource(memdatasource);
+        return 0;
+    }
 
     RASTER_DEBUGF(4, "Band data is at pos %p", pVoid);
 
@@ -1889,7 +2103,15 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
      * And be careful with size too: 10 characters may be insufficient
      * to store 64bits memory addresses
      */
-    pszDataPointer = (char *) ctx->alloc(20 * sizeof (char));
+    pszDataPointer = (char *)rtalloc(20 * sizeof (char));
+    if (NULL == pszDataPointer) {
+        rterror("rt_raster_dump_as_wktpolygons: Couldn't allocate memory for data");
+        GDALDeregisterDriver(gdal_drv);
+        GDALDestroyDriver(gdal_drv);
+        OGRReleaseDataSource(memdatasource);
+        return 0;
+    }
+
     sprintf(pszDataPointer, "%p", pVoid);
 
     RASTER_DEBUGF(4, "rt_raster_dump_as_polygons: szDatapointer is %p",
@@ -1911,10 +2133,11 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
      * This memory must be deallocated because we own it. The GDALRasterBand
      * destructor will not deallocate it
      **/
-    ctx->dealloc(pszDataPointer);
+    rtdealloc(pszDataPointer);
 
     if (GDALAddBand(memdataset, nPixelType, apszOptions) == CE_Failure) {
-        ctx->err("rt_raster_dump_as_wktpolygons: Couldn't transform raster band in GDALRasterBand format to polygonize it");
+        rterror("rt_raster_dump_as_wktpolygons: Couldn't transform raster band "
+                "in GDALRasterBand format to polygonize it");
         GDALClose(memdataset);
         GDALDeregisterDriver(gdal_drv);
         GDALDestroyDriver(gdal_drv);
@@ -1925,7 +2148,7 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
 
     /* Checking */
     if (GDALGetRasterCount(memdataset) != 1) {
-        ctx->err("rt_raster_dump_as_wktpolygons: Error creating GDAL MEM raster bands");
+        rterror("rt_raster_dump_as_wktpolygons: Error creating GDAL MEM raster bands");
         GDALClose(memdataset);
         GDALDeregisterDriver(gdal_drv);
         GDALDestroyDriver(gdal_drv);
@@ -1949,7 +2172,7 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
             wkbPolygon, NULL);
 
     if (NULL == hLayer) {
-        ctx->err("rt_raster_dump_as_wktpolygons: Couldn't create layer to store polygons");
+        rterror("rt_raster_dump_as_wktpolygons: Couldn't create layer to store polygons");
         GDALClose(memdataset);
         GDALDeregisterDriver(gdal_drv);
         GDALDestroyDriver(gdal_drv);
@@ -1967,7 +2190,7 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
     /* Second, create the field */
     if (OGR_L_CreateField(hLayer, hFldDfn, TRUE) !=
             OGRERR_NONE) {
-        ctx->warn("Couldn't create a field in OGR Layer. The polygons generated won't be able to store the pixel value");
+        rtwarn("Couldn't create a field in OGR Layer. The polygons generated won't be able to store the pixel value");
         iPixVal = -1;
     }
     else {
@@ -1979,7 +2202,7 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
     /* Get GDAL raster band */
     gdal_band = GDALGetRasterBand(memdataset, 1);
     if (NULL == gdal_band) {
-        ctx->err("rt_raster_dump_as_wktpolygons: Couldn't get GDAL band to polygonize");
+        rterror("rt_raster_dump_as_wktpolygons: Couldn't get GDAL band to polygonize");
         GDALClose(memdataset);
         GDALDeregisterDriver(gdal_drv);
         GDALDestroyDriver(gdal_drv);
@@ -1991,12 +2214,12 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
         return 0;
     }
 
-    iBandHasNodataValue = rt_band_get_hasnodata_flag(ctx, band);
+    iBandHasNodataValue = rt_band_get_hasnodata_flag(band);
     if (iBandHasNodataValue) {
         /* Add nodata value for band */
-        dBandNoData = rt_band_get_nodata(ctx, band);
+        dBandNoData = rt_band_get_nodata(band);
         if (GDALSetRasterNoDataValue(gdal_band, dBandNoData) != CE_None)
-            ctx->warn("Couldn't set nodata value for band.");
+            rtwarn("Couldn't set nodata value for band.");
     }
 
 
@@ -2005,25 +2228,26 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
      **/
     GDALPolygonize(gdal_band, NULL, hLayer, iPixVal, NULL, NULL, NULL);
 
-       /**
-        * Optimization: Apply a OGR SQL filter to the layer to select the
-        * features different from NODATA value.
-        *
-        * Thanks to David Zwarg.
-        **/
-       if (iBandHasNodataValue) {
-               pszQuery = (char *) ctx->alloc(50 * sizeof (char));
-               sprintf(pszQuery, "PixelValue != %f", dBandNoData );            
-               OGRErr e = OGR_L_SetAttributeFilter(hLayer, pszQuery);
-               if (e != OGRERR_NONE) {
-                       ctx->warn("Error filtering NODATA values for band. All values will be treated as data values\n");
-               }
-                       
-       }
+    /**
+     * Optimization: Apply a OGR SQL filter to the layer to select the
+     * features different from NODATA value.
+     *
+     * Thanks to David Zwarg.
+     **/
+    if (iBandHasNodataValue) {
+            pszQuery = (char *) rtalloc(50 * sizeof (char));
+            sprintf(pszQuery, "PixelValue != %f", dBandNoData );
+            OGRErr e = OGR_L_SetAttributeFilter(hLayer, pszQuery);
+            if (e != OGRERR_NONE) {
+                    rtwarn("Error filtering NODATA values for band. All values"
+                            " will be treated as data values");
+            }
+
+    }
 
-       else {
-               pszQuery = NULL;
-       }
+    else {
+            pszQuery = NULL;
+    }
 
 
 
@@ -2032,16 +2256,16 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
      * XXX jorgearevalo: GDALPolygonize does not set the coordinate system
      * on the output layer. Application code should do this when the layer
      * is created, presumably matching the raster coordinate system.
-     * XXX jorgearevalo: modify GDALPolygonize to directly emit polygons
-     * in WKT format?
+     * TODO: modify GDALPolygonize to directly emit polygons in WKT format?
      *********************************************************************/
     nFeatureCount = OGR_L_GetFeatureCount(hLayer, TRUE);
 
     /* Allocate memory for pols */
-    pols = (rt_geomval) ctx->alloc(nFeatureCount * sizeof (struct rt_geomval_t));
+    pols = (rt_geomval) rtalloc(nFeatureCount * sizeof (struct rt_geomval_t));
 
     if (NULL == pols) {
-        ctx->err("rt_raster_dump_as_wktpolygons: Couldn't allocate memory for geomval structure");
+        rterror("rt_raster_dump_as_wktpolygons: Couldn't allocate memory for "
+                "geomval structure");
         GDALClose(memdataset);
         GDALDeregisterDriver(gdal_drv);
         GDALDestroyDriver(gdal_drv);
@@ -2058,8 +2282,8 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
     if (pnElements)
         *pnElements = 0;
 
-       /* Reset feature reading to start in the first feature */
-       OGR_L_ResetReading(hLayer);
+    /* Reset feature reading to start in the first feature */
+    OGR_L_ResetReading(hLayer);
 
     for (j = 0; j < nFeatureCount; j++) {
 
@@ -2070,8 +2294,8 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
        OGR_G_ExportToWkt(hGeom, &pszSrcText);
 
        pols[j].val = dValue;
-               pols[j].srid = rt_raster_get_srid(ctx, raster);
-       pols[j].geom = (char *) ctx->alloc((1 + strlen(pszSrcText))
+               pols[j].srid = rt_raster_get_srid(raster);
+       pols[j].geom = (char *) rtalloc((1 + strlen(pszSrcText))
                     * sizeof (char));
        strcpy(pols[j].geom, pszSrcText);
 
@@ -2085,7 +2309,7 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
          * postgresql memory context to work with, and the memory created
          * for pszSrcText is created outside this context.
          **/
-         //ctx->dealloc(pszSrcText);
+         //rtdealloc(pszSrcText);
         free(pszSrcText);
         pszSrcText = NULL;
         
@@ -2111,13 +2335,14 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
 }
 
 LWPOLY*
-rt_raster_get_convex_hull(rt_context ctx, rt_raster raster) {
+rt_raster_get_convex_hull(rt_raster raster) {
     POINTARRAY **rings = NULL;
     POINTARRAY *pts = NULL;
     LWPOLY* ret = NULL;
     POINT4D p4d;
 
-    assert(NULL != ctx);
+    
+    
     assert(NULL != raster);
 
     RASTER_DEBUGF(3, "rt_raster_get_convex_hull: raster is %dx%d",
@@ -2127,41 +2352,41 @@ rt_raster_get_convex_hull(rt_context ctx, rt_raster raster) {
         return 0;
     }
 
-    rings = (POINTARRAY **) ctx->alloc(sizeof (POINTARRAY*));
+    rings = (POINTARRAY **) rtalloc(sizeof (POINTARRAY*));
     if (!rings) {
-        ctx->err("rt_raster_get_convex_hull: Out of memory [%s:%d]", __FILE__, __LINE__);
+        rterror("rt_raster_get_convex_hull: Out of memory [%s:%d]", __FILE__, __LINE__);
         return 0;
     }
     rings[0] = ptarray_construct(0, 0, 5);
     /* TODO: handle error on ptarray construction */
     /* XXX jorgearevalo: the error conditions aren't managed in ptarray_construct */
     if (!rings[0]) {
-        ctx->err("rt_raster_get_convex_hull: Out of memory [%s:%d]", __FILE__, __LINE__);
+        rterror("rt_raster_get_convex_hull: Out of memory [%s:%d]", __FILE__, __LINE__);
         return 0;
     }
     pts = rings[0];
 
     /* Upper-left corner (first and last points) */
-    rt_raster_cell_to_geopoint(ctx, raster,
+    rt_raster_cell_to_geopoint(raster,
             0, 0,
             &p4d.x, &p4d.y);
     ptarray_set_point4d(pts, 0, &p4d);
     ptarray_set_point4d(pts, 4, &p4d); /* needed for closing it? */
 
     /* Upper-right corner (we go clockwise) */
-    rt_raster_cell_to_geopoint(ctx, raster,
+    rt_raster_cell_to_geopoint(raster,
             raster->width, 0,
             &p4d.x, &p4d.y);
     ptarray_set_point4d(pts, 1, &p4d);
 
     /* Lower-right corner */
-    rt_raster_cell_to_geopoint(ctx, raster,
+    rt_raster_cell_to_geopoint(raster,
             raster->width, raster->height,
             &p4d.x, &p4d.y);
     ptarray_set_point4d(pts, 2, &p4d);
 
     /* Lower-left corner */
-    rt_raster_cell_to_geopoint(ctx, raster,
+    rt_raster_cell_to_geopoint(raster,
             0, raster->height,
             &p4d.x, &p4d.y);
     ptarray_set_point4d(pts, 3, &p4d);
@@ -2466,7 +2691,7 @@ write_float64(uint8_t** to, uint8_t littleEndian, double v)
 
 /* Read band from WKB as at start of band */
 static rt_band
-rt_band_from_wkb(rt_context ctx, uint16_t width, uint16_t height,
+rt_band_from_wkb(uint16_t width, uint16_t height,
         const uint8_t** ptr, const uint8_t* end,
         uint8_t littleEndian) {
     rt_band band = NULL;
@@ -2475,26 +2700,27 @@ rt_band_from_wkb(rt_context ctx, uint16_t width, uint16_t height,
     unsigned long sz = 0;
     uint32_t v = 0;
 
-    assert(NULL != ctx);
+    
+    
     assert(NULL != ptr);
     assert(NULL != end);
 
-    band = ctx->alloc(sizeof (struct rt_band_t));
+    band = rtalloc(sizeof (struct rt_band_t));
     if (!band) {
-        ctx->err("rt_band_from_wkb: Out of memory allocating rt_band during WKB parsing");
+        rterror("rt_band_from_wkb: Out of memory allocating rt_band during WKB parsing");
         return 0;
     }
 
     if (end - *ptr < 1) {
-        ctx->err("rt_band_from_wkb: Premature end of WKB on band reading (%s:%d)",
+        rterror("rt_band_from_wkb: Premature end of WKB on band reading (%s:%d)",
                 __FILE__, __LINE__);
         return 0;
     }
     type = read_uint8(ptr);
 
     if ((type & BANDTYPE_PIXTYPE_MASK) >= PT_END) {
-        ctx->err("rt_band_from_wkb: Invalid pixtype %d", type & BANDTYPE_PIXTYPE_MASK);
-        ctx->dealloc(band);
+        rterror("rt_band_from_wkb: Invalid pixtype %d", type & BANDTYPE_PIXTYPE_MASK);
+        rtdealloc(band);
         return 0;
     }
     assert(NULL != band);
@@ -2507,16 +2733,16 @@ rt_band_from_wkb(rt_context ctx, uint16_t width, uint16_t height,
     band->height = height;
 
     RASTER_DEBUGF(3, " Band pixtype:%s, offline:%d, hasnodata:%d",
-            rt_pixtype_name(ctx, band->pixtype),
+            rt_pixtype_name(band->pixtype),
             band->offline,
             band->hasnodata);
 
     /* Check there's enough bytes to read nodata value */
 
-    pixbytes = rt_pixtype_size(ctx, band->pixtype);
+    pixbytes = rt_pixtype_size(band->pixtype);
     if (((*ptr) + pixbytes) >= end) {
-        ctx->err("rt_band_from_wkb: Premature end of WKB on band novalue reading");
-        ctx->dealloc(band);
+        rterror("rt_band_from_wkb: Premature end of WKB on band novalue reading");
+        rtdealloc(band);
         return 0;
     }
 
@@ -2579,8 +2805,8 @@ rt_band_from_wkb(rt_context ctx, uint16_t width, uint16_t height,
         }
         default:
         {
-            ctx->err("rt_band_from_wkb: Unknown pixeltype %d", band->pixtype);
-            ctx->dealloc(band);
+            rterror("rt_band_from_wkb: Unknown pixeltype %d", band->pixtype);
+            rtdealloc(band);
             return 0;
         }
     }
@@ -2590,10 +2816,10 @@ rt_band_from_wkb(rt_context ctx, uint16_t width, uint16_t height,
 
     if (band->offline) {
         if (((*ptr) + 1) >= end) {
-            ctx->err("rt_band_from_wkb: Premature end of WKB on offline "
+            rterror("rt_band_from_wkb: Premature end of WKB on offline "
                     "band data bandNum reading (%s:%d)",
                     __FILE__, __LINE__);
-            ctx->dealloc(band);
+            rtdealloc(band);
             return 0;
         }
         band->data.offline.bandNum = read_int8(ptr);
@@ -2603,13 +2829,13 @@ rt_band_from_wkb(rt_context ctx, uint16_t width, uint16_t height,
             sz = 0;
             while ((*ptr)[sz] && &((*ptr)[sz]) < end) ++sz;
             if (&((*ptr)[sz]) >= end) {
-                ctx->err("rt_band_from_wkb: Premature end of WKB on band offline path reading");
-                ctx->dealloc(band);
+                rterror("rt_band_from_wkb: Premature end of WKB on band offline path reading");
+                rtdealloc(band);
                 return 0;
             }
 
             band->ownsData = 1;
-            band->data.offline.path = ctx->alloc(sz + 1);
+            band->data.offline.path = rtalloc(sz + 1);
 
             memcpy(band->data.offline.path, *ptr, sz);
             band->data.offline.path[sz] = '\0';
@@ -2628,16 +2854,16 @@ rt_band_from_wkb(rt_context ctx, uint16_t width, uint16_t height,
     /* This is an on-disk band */
     sz = width * height * pixbytes;
     if (((*ptr) + sz) > end) {
-        ctx->err("rt_band_from_wkb: Premature end of WKB on band data reading (%s:%d)",
+        rterror("rt_band_from_wkb: Premature end of WKB on band data reading (%s:%d)",
                 __FILE__, __LINE__);
-        ctx->dealloc(band);
+        rtdealloc(band);
         return 0;
     }
 
-    band->data.mem = ctx->alloc(sz);
+    band->data.mem = rtalloc(sz);
     if (!band->data.mem) {
-        ctx->err("rt_band_from_wkb: Out of memory during band creation in WKB parser");
-        ctx->dealloc(band);
+        rterror("rt_band_from_wkb: Out of memory during band creation in WKB parser");
+        rtdealloc(band);
         return 0;
     }
 
@@ -2656,9 +2882,9 @@ rt_band_from_wkb(rt_context ctx, uint16_t width, uint16_t height,
                 else if (pixbytes == 4) flipper = flip_endian_32;
                 else if (pixbytes == 8) flipper = flip_endian_64;
                 else {
-                    ctx->err("rt_band_from_wkb: Unexpected pix bytes %d", pixbytes);
-                    ctx->dealloc(band);
-                    ctx->dealloc(band->data.mem);
+                    rterror("rt_band_from_wkb: Unexpected pix bytes %d", pixbytes);
+                    rtdealloc(band);
+                    rtdealloc(band->data.mem);
                     return 0;
                 }
 
@@ -2685,10 +2911,10 @@ rt_band_from_wkb(rt_context ctx, uint16_t width, uint16_t height,
             for (v = 0; v < sz; ++v) {
                 val = ((uint8_t*) band->data.mem)[v];
                 if (val > maxVal) {
-                    ctx->err("rt_band_from_wkb: Invalid value %d for pixel of type %s",
-                            val, rt_pixtype_name(ctx, band->pixtype));
-                    ctx->dealloc(band->data.mem);
-                    ctx->dealloc(band);
+                    rterror("rt_band_from_wkb: Invalid value %d for pixel of type %s",
+                            val, rt_pixtype_name(band->pixtype));
+                    rtdealloc(band->data.mem);
+                    rtdealloc(band);
                     return 0;
                 }
             }
@@ -2697,7 +2923,7 @@ rt_band_from_wkb(rt_context ctx, uint16_t width, uint16_t height,
 
     /* And we should check if the band is a nodata band */
     /* TODO: No!! This is too slow */
-    //rt_band_check_is_nodata(ctx, band);
+    //rt_band_check_is_nodata(band);
 
     return band;
 }
@@ -2706,7 +2932,7 @@ rt_band_from_wkb(rt_context ctx, uint16_t width, uint16_t height,
 #define RT_WKB_HDR_SZ (sizeof(struct rt_raster_serialized_t)-4+1)
 
 rt_raster
-rt_raster_from_wkb(rt_context ctx, const uint8_t* wkb, uint32_t wkbsize) {
+rt_raster_from_wkb(const uint8_t* wkb, uint32_t wkbsize) {
     const uint8_t *ptr = wkb;
     const uint8_t *wkbend = NULL;
     rt_raster rast = NULL;
@@ -2714,12 +2940,13 @@ rt_raster_from_wkb(rt_context ctx, const uint8_t* wkb, uint32_t wkbsize) {
     uint16_t version = 0;
     uint16_t i = 0;
 
-    assert(NULL != ctx);
+    
+    
     assert(NULL != ptr);
 
     /* Check that wkbsize is >= sizeof(rt_raster_serialized) */
     if (wkbsize < RT_WKB_HDR_SZ) {
-        ctx->err("rt_raster_from_wkb: wkb size < min size (%d)",
+        rterror("rt_raster_from_wkb: wkb size < min size (%d)",
                 RT_WKB_HDR_SZ);
         return 0;
     }
@@ -2738,14 +2965,14 @@ rt_raster_from_wkb(rt_context ctx, const uint8_t* wkb, uint32_t wkbsize) {
     /* Read version of protocol */
     version = read_uint16(&ptr, endian);
     if (version != 0) {
-        ctx->err("rt_raster_from_wkb: WKB version %d unsupported", version);
+        rterror("rt_raster_from_wkb: WKB version %d unsupported", version);
         return 0;
     }
 
     /* Read other components of raster header */
-    rast = (rt_raster) ctx->alloc(sizeof (struct rt_raster_t));
+    rast = (rt_raster) rtalloc(sizeof (struct rt_raster_t));
     if (!rast) {
-        ctx->err("rt_raster_from_wkb: Out of memory allocating raster for wkb input");
+        rterror("rt_raster_from_wkb: Out of memory allocating raster for wkb input");
         return 0;
     }
     rast->numBands = read_uint16(&ptr, endian);
@@ -2784,20 +3011,20 @@ rt_raster_from_wkb(rt_context ctx, const uint8_t* wkb, uint32_t wkbsize) {
     if (!rast->numBands) {
         /* Here ptr should have been left to right after last used byte */
         if (ptr < wkbend) {
-            ctx->info("%d bytes of WKB remained unparsed", wkbend - ptr);
+            rtwarn("%d bytes of WKB remained unparsed", wkbend - ptr);
         } else if (ptr > wkbend) {
             /* Easier to get a segfault before I guess */
-            ctx->warn("We parsed %d bytes more then available!", ptr - wkbend);
+            rtwarn("We parsed %d bytes more then available!", ptr - wkbend);
         }
         rast->bands = 0;
         return rast;
     }
 
     /* Now read the bands */
-    rast->bands = (rt_band*) ctx->alloc(sizeof (rt_band) * rast->numBands);
+    rast->bands = (rt_band*) rtalloc(sizeof (rt_band) * rast->numBands);
     if (!rast->bands) {
-        ctx->err("rt_raster_from_wkb: Out of memory allocating bands for WKB raster decoding");
-        ctx->dealloc(rast);
+        rterror("rt_raster_from_wkb: Out of memory allocating bands for WKB raster decoding");
+        rtdealloc(rast);
         return 0;
     }
 
@@ -2808,11 +3035,11 @@ rt_raster_from_wkb(rt_context ctx, const uint8_t* wkb, uint32_t wkbsize) {
         RASTER_DEBUGF(3, "Parsing band %d from wkb position %d", i,
                 d_binptr_to_pos(ptr, wkbend, wkbsize));
 
-        rt_band band = rt_band_from_wkb(ctx, rast->width, rast->height,
+        rt_band band = rt_band_from_wkb(rast->width, rast->height,
                 &ptr, wkbend, endian);
         if (!band) {
-            ctx->err("rt_raster_from_wkb: Error reading WKB form of band %d", i);
-            ctx->dealloc(rast);
+            rterror("rt_raster_from_wkb: Error reading WKB form of band %d", i);
+            rtdealloc(rast);
             /* TODO: dealloc any previously allocated band too ! */
             return 0;
         }
@@ -2821,10 +3048,10 @@ rt_raster_from_wkb(rt_context ctx, const uint8_t* wkb, uint32_t wkbsize) {
 
     /* Here ptr should have been left to right after last used byte */
     if (ptr < wkbend) {
-        ctx->info("%d bytes of WKB remained unparsed", wkbend - ptr);
+        rtwarn("%d bytes of WKB remained unparsed", wkbend - ptr);
     } else if (ptr > wkbend) {
         /* Easier to get a segfault before I guess */
-        ctx->warn("We parsed %d bytes more then available!",
+        rtwarn("We parsed %d bytes more then available!",
                 ptr - wkbend);
     }
 
@@ -2833,26 +3060,27 @@ rt_raster_from_wkb(rt_context ctx, const uint8_t* wkb, uint32_t wkbsize) {
 }
 
 rt_raster
-rt_raster_from_hexwkb(rt_context ctx, const char* hexwkb,
+rt_raster_from_hexwkb(const char* hexwkb,
         uint32_t hexwkbsize) {
     uint8_t* wkb = NULL;
     uint32_t wkbsize = 0;
     uint32_t i = 0;
 
-    assert(NULL != ctx);
+    
+    
     assert(NULL != hexwkb);
 
     RASTER_DEBUGF(3, "rt_raster_from_hexwkb: input wkb: %s", hexwkb);
 
     if (hexwkbsize % 2) {
-        ctx->err("rt_raster_from_hexwkb: Raster HEXWKB input must have an even number of characters");
+        rterror("rt_raster_from_hexwkb: Raster HEXWKB input must have an even number of characters");
         return 0;
     }
     wkbsize = hexwkbsize / 2;
 
-    wkb = ctx->alloc(wkbsize);
+    wkb = rtalloc(wkbsize);
     if (!wkb) {
-        ctx->err("rt_raster_from_hexwkb: Out of memory allocating memory for decoding HEXWKB");
+        rterror("rt_raster_from_hexwkb: Out of memory allocating memory for decoding HEXWKB");
         return 0;
     }
 
@@ -2860,19 +3088,20 @@ rt_raster_from_hexwkb(rt_context ctx, const char* hexwkb,
         wkb[i] = parse_hex((char*) & (hexwkb[i * 2]));
     }
 
-    rt_raster ret = rt_raster_from_wkb(ctx, wkb, wkbsize);
+    rt_raster ret = rt_raster_from_wkb(wkb, wkbsize);
 
-    ctx->dealloc(wkb); /* as long as rt_raster_from_wkb copies memory */
+    rtdealloc(wkb); /* as long as rt_raster_from_wkb copies memory */
 
     return ret;
 }
 
 static uint32_t
-rt_raster_wkb_size(rt_context ctx, rt_raster raster) {
+rt_raster_wkb_size(rt_raster raster) {
     uint32_t size = RT_WKB_HDR_SZ;
     uint16_t i = 0;
 
-    assert(NULL != ctx);
+    
+    
     assert(NULL != raster);
 
     RASTER_DEBUGF(3, "rt_raster_wkb_size: computing size for %d bands",
@@ -2881,12 +3110,12 @@ rt_raster_wkb_size(rt_context ctx, rt_raster raster) {
     for (i = 0; i < raster->numBands; ++i) {
         rt_band band = raster->bands[i];
         rt_pixtype pixtype = band->pixtype;
-        int pixbytes = rt_pixtype_size(ctx, pixtype);
+        int pixbytes = rt_pixtype_size(pixtype);
 
         RASTER_DEBUGF(3, "rt_raster_wkb_size: adding size of band %d", i);
 
         if (pixbytes < 1) {
-            ctx->err("rt_raster_wkb_size: Corrupted band: unknown pixtype");
+            rterror("rt_raster_wkb_size: Corrupted band: unknown pixtype");
             return 0;
         }
 
@@ -2913,7 +3142,7 @@ rt_raster_wkb_size(rt_context ctx, rt_raster raster) {
 }
 
 uint8_t *
-rt_raster_to_wkb(rt_context ctx, rt_raster raster, uint32_t *wkbsize) {
+rt_raster_to_wkb(rt_raster raster, uint32_t *wkbsize) {
 #if POSTGIS_DEBUG_LEVEL > 0
     const uint8_t *wkbend = NULL;
 #endif
@@ -2922,19 +3151,20 @@ rt_raster_to_wkb(rt_context ctx, rt_raster raster, uint32_t *wkbsize) {
     uint16_t i = 0;
     uint8_t littleEndian = isMachineLittleEndian();
 
-    assert(NULL != ctx);
+    
+    
     assert(NULL != raster);
     assert(NULL != wkbsize);
 
     RASTER_DEBUG(2, "rt_raster_to_wkb: about to call rt_raster_wkb_size");
 
-    *wkbsize = rt_raster_wkb_size(ctx, raster);
+    *wkbsize = rt_raster_wkb_size(raster);
 
     RASTER_DEBUGF(3, "rt_raster_to_wkb: found size: %d", *wkbsize);
 
-    wkb = (uint8_t*) ctx->alloc(*wkbsize);
+    wkb = (uint8_t*) rtalloc(*wkbsize);
     if (!wkb) {
-        ctx->err("rt_raster_to_wkb: Out of memory allocating WKB for raster");
+        rterror("rt_raster_to_wkb: Out of memory allocating WKB for raster");
         return 0;
     }
 
@@ -2964,14 +3194,14 @@ rt_raster_to_wkb(rt_context ctx, rt_raster raster, uint32_t *wkbsize) {
     for (i = 0; i < raster->numBands; ++i) {
         rt_band band = raster->bands[i];
         rt_pixtype pixtype = band->pixtype;
-        int pixbytes = rt_pixtype_size(ctx, pixtype);
+        int pixbytes = rt_pixtype_size(pixtype);
 
         RASTER_DEBUGF(3, "Writing WKB for band %d", i);
         RASTER_DEBUGF(3, "Writing band pixel type to wkb position %d",
                 d_binptr_to_pos(ptr, wkbend, *wkbsize));
 
         if (pixbytes < 1) {
-            ctx->err("rt_raster_to_wkb: Corrupted band: unknown pixtype");
+            rterror("rt_raster_to_wkb: Corrupted band: unknown pixtype");
             return 0;
         }
 
@@ -3044,7 +3274,7 @@ rt_raster_to_wkb(rt_context ctx, rt_raster raster, uint32_t *wkbsize) {
                 break;
             }
             default:
-                ctx->err("rt_raster_to_wkb: Fatal error caused by unknown pixel type. Aborting.");
+                rterror("rt_raster_to_wkb: Fatal error caused by unknown pixel type. Aborting.");
                 abort(); /* shoudn't happen */
                 return 0;
         }
@@ -3088,27 +3318,28 @@ rt_raster_to_wkb(rt_context ctx, rt_raster raster, uint32_t *wkbsize) {
 }
 
 char *
-rt_raster_to_hexwkb(rt_context ctx, rt_raster raster, uint32_t *hexwkbsize) {
+rt_raster_to_hexwkb(rt_raster raster, uint32_t *hexwkbsize) {
     uint8_t *wkb = NULL;
     char* hexwkb = NULL;
     uint32_t i = 0;
     uint32_t wkbsize = 0;
 
-    assert(NULL != ctx);
+    
+    
     assert(NULL != raster);
     assert(NULL != hexwkbsize);
 
     RASTER_DEBUG(2, "rt_raster_to_hexwkb: calling rt_raster_to_wkb");
 
-    wkb = rt_raster_to_wkb(ctx, raster, &wkbsize);
+    wkb = rt_raster_to_wkb(raster, &wkbsize);
 
     RASTER_DEBUG(3, "rt_raster_to_hexwkb: rt_raster_to_wkb returned");
 
     *hexwkbsize = wkbsize * 2; /* hex is 2 times bytes */
-    hexwkb = (char*) ctx->alloc((*hexwkbsize) + 1);
+    hexwkb = (char*) rtalloc((*hexwkbsize) + 1);
     if (!hexwkb) {
-        ctx->dealloc(wkb);
-        ctx->err("rt_raster_to_hexwkb: Out of memory hexifying raster WKB");
+        rtdealloc(wkb);
+        rterror("rt_raster_to_hexwkb: Out of memory hexifying raster WKB");
         return 0;
     }
     hexwkb[*hexwkbsize] = '\0'; /* Null-terminate */
@@ -3117,7 +3348,7 @@ rt_raster_to_hexwkb(rt_context ctx, rt_raster raster, uint32_t *hexwkbsize) {
         deparse_hex(wkb[i], &(hexwkb[2 * i]));
     }
 
-    ctx->dealloc(wkb); /* we don't need this anymore */
+    rtdealloc(wkb); /* we don't need this anymore */
 
     RASTER_DEBUGF(3, "rt_raster_to_hexwkb: output wkb: %s", hexwkb);
 
@@ -3127,11 +3358,12 @@ rt_raster_to_hexwkb(rt_context ctx, rt_raster raster, uint32_t *hexwkbsize) {
 /*--------- Serializer/Deserializer --------------------------------------*/
 
 static uint32_t
-rt_raster_serialized_size(rt_context ctx, rt_raster raster) {
+rt_raster_serialized_size(rt_raster raster) {
     uint32_t size = sizeof (struct rt_raster_serialized_t);
     uint16_t i = 0;
 
-    assert(NULL != ctx);
+    
+    
     assert(NULL != raster);
 
     RASTER_DEBUGF(3, "Serialized size with just header:%d - now adding size of %d bands",
@@ -3140,10 +3372,10 @@ rt_raster_serialized_size(rt_context ctx, rt_raster raster) {
     for (i = 0; i < raster->numBands; ++i) {
         rt_band band = raster->bands[i];
         rt_pixtype pixtype = band->pixtype;
-        int pixbytes = rt_pixtype_size(ctx, pixtype);
+        int pixbytes = rt_pixtype_size(pixtype);
 
         if (pixbytes < 1) {
-            ctx->err("rt_raster_serialized_size: Corrupted band: unknown pixtype");
+            rterror("rt_raster_serialized_size: Corrupted band: unknown pixtype");
             return 0;
         }
 
@@ -3180,19 +3412,18 @@ rt_raster_serialized_size(rt_context ctx, rt_raster raster) {
 }
 
 void*
-rt_raster_serialize(rt_context ctx, rt_raster raster) {
-    uint32_t size = rt_raster_serialized_size(ctx, raster);
+rt_raster_serialize(rt_raster raster) {
+    uint32_t size = rt_raster_serialized_size(raster);
     uint8_t* ret = NULL;
     uint8_t* ptr = NULL;
     uint16_t i = 0;
 
-    assert(NULL != ctx);
-    assert(NULL != ctx->alloc);
+    
     assert(NULL != raster);
 
-    ret = (uint8_t*) ctx->alloc(size);
+    ret = (uint8_t*) rtalloc(size);
     if (!ret) {
-        ctx->err("rt_raster_serialize: Out of memory allocating %d bytes for serializing a raster",
+        rterror("rt_raster_serialize: Out of memory allocating %d bytes for serializing a raster",
                 size);
         return 0;
     }
@@ -3223,7 +3454,7 @@ rt_raster_serialize(rt_context ctx, rt_raster raster) {
 
 #if POSTGIS_DEBUG_LEVEL > 2
     uint8_t* dbg_ptr = ptr;
-    d_print_binary_hex(ctx, "HEADER", dbg_ptr, size);
+    d_print_binary_hex("HEADER", dbg_ptr, size);
 #endif
 
     ptr += sizeof (struct rt_raster_serialized_t);
@@ -3234,9 +3465,9 @@ rt_raster_serialize(rt_context ctx, rt_raster raster) {
         assert(NULL != band);
 
         rt_pixtype pixtype = band->pixtype;
-        int pixbytes = rt_pixtype_size(ctx, pixtype);
+        int pixbytes = rt_pixtype_size(pixtype);
         if (pixbytes < 1) {
-            ctx->err("rt_raster_serialize: Corrupted band: unknown pixtype");
+            rterror("rt_raster_serialize: Corrupted band: unknown pixtype");
             return 0;
         }
 
@@ -3254,7 +3485,7 @@ rt_raster_serialize(rt_context ctx, rt_raster raster) {
         }
 
 #if POSTGIS_DEBUG_LEVEL > 2
-        d_print_binary_hex(ctx, "PIXTYPE", dbg_ptr, size);
+        d_print_binary_hex("PIXTYPE", dbg_ptr, size);
 #endif
 
         ptr += 1;
@@ -3266,7 +3497,7 @@ rt_raster_serialize(rt_context ctx, rt_raster raster) {
         }
 
 #if POSTGIS_DEBUG_LEVEL > 2
-        d_print_binary_hex(ctx, "PADDING", dbg_ptr, size);
+        d_print_binary_hex("PADDING", dbg_ptr, size);
 #endif
 
         /* Consistency checking (ptr is pixbytes-aligned) */
@@ -3321,7 +3552,7 @@ rt_raster_serialize(rt_context ctx, rt_raster raster) {
                 break;
             }
             default:
-                ctx->err("rt_raster_serialize: Fatal error caused by unknown pixel type. Aborting.");
+                rterror("rt_raster_serialize: Fatal error caused by unknown pixel type. Aborting.");
                 abort(); /* shouldn't happen */
                 return 0;
         }
@@ -3330,7 +3561,7 @@ rt_raster_serialize(rt_context ctx, rt_raster raster) {
         assert(!((uintptr_t) ptr % pixbytes));
 
 #if POSTGIS_DEBUG_LEVEL > 2
-        d_print_binary_hex(ctx, "nodata", dbg_ptr, size);
+        d_print_binary_hex("nodata", dbg_ptr, size);
 #endif
 
         if (band->offline) {
@@ -3349,7 +3580,7 @@ rt_raster_serialize(rt_context ctx, rt_raster raster) {
         }
 
 #if POSTGIS_DEBUG_LEVEL > 2
-        d_print_binary_hex(ctx, "BAND", dbg_ptr, size);
+        d_print_binary_hex("BAND", dbg_ptr, size);
 #endif
 
         /* Pad up to 8-bytes boundary */
@@ -3367,21 +3598,22 @@ rt_raster_serialize(rt_context ctx, rt_raster raster) {
     } /* for-loop over bands */
 
 #if POSTGIS_DEBUG_LEVEL > 2
-    d_print_binary_hex(ctx, "SERIALIZED RASTER", dbg_ptr, size);
+    d_print_binary_hex("SERIALIZED RASTER", dbg_ptr, size);
 #endif
 
     return ret;
 }
 
 rt_raster
-rt_raster_deserialize(rt_context ctx, void* serialized) {
+rt_raster_deserialize(void* serialized) {
     rt_raster rast = NULL;
     const uint8_t *ptr = NULL;
     const uint8_t *beg = NULL;
     uint16_t i = 0;
     uint8_t littleEndian = isMachineLittleEndian();
 
-    assert(NULL != ctx);
+    
+    
     assert(NULL != serialized);
 
     RASTER_DEBUG(2, "rt_raster_deserialize: Entering...");
@@ -3394,9 +3626,9 @@ rt_raster_deserialize(rt_context ctx, void* serialized) {
     /* Allocate memory for deserialized raster header */
 
     RASTER_DEBUG(3, "rt_raster_deserialize: Allocationg memory for deserialized raster header");
-    rast = (rt_raster) ctx->alloc(sizeof (struct rt_raster_t));
+    rast = (rt_raster) rtalloc(sizeof (struct rt_raster_t));
     if (!rast) {
-        ctx->err("rt_raster_deserialize: Out of memory allocating raster for deserialization");
+        rterror("rt_raster_deserialize: Out of memory allocating raster for deserialization");
         return 0;
     }
 
@@ -3412,7 +3644,7 @@ rt_raster_deserialize(rt_context ctx, void* serialized) {
 
     RASTER_DEBUG(3, "rt_raster_deserialize: Allocating memory for bands");
     /* Allocate registry of raster bands */
-    rast->bands = ctx->alloc(rast->numBands * sizeof (rt_band));
+    rast->bands = rtalloc(rast->numBands * sizeof (rt_band));
 
     RASTER_DEBUGF(3, "rt_raster_deserialize: %d bands", rast->numBands);
 
@@ -3426,9 +3658,9 @@ rt_raster_deserialize(rt_context ctx, void* serialized) {
         uint8_t type = 0;
         int pixbytes = 0;
 
-        band = ctx->alloc(sizeof (struct rt_band_t));
+        band = rtalloc(sizeof (struct rt_band_t));
         if (!band) {
-            ctx->err("rt_raster_deserialize: Out of memory allocating rt_band during deserialization");
+            rterror("rt_raster_deserialize: Out of memory allocating rt_band during deserialization");
             return 0;
         }
 
@@ -3438,7 +3670,7 @@ rt_raster_deserialize(rt_context ctx, void* serialized) {
         ptr++;
         band->pixtype = type & BANDTYPE_PIXTYPE_MASK;
 
-        RASTER_DEBUGF(3, "rt_raster_deserialize: band %d with pixel type %s", i, rt_pixtype_name(ctx, band->pixtype));
+        RASTER_DEBUGF(3, "rt_raster_deserialize: band %d with pixel type %s", i, rt_pixtype_name(band->pixtype));
 
         band->offline = BANDTYPE_IS_OFFDB(type) ? 1 : 0;
         band->hasnodata = BANDTYPE_HAS_NODATA(type) ? 1 : 0;
@@ -3448,7 +3680,7 @@ rt_raster_deserialize(rt_context ctx, void* serialized) {
         band->ownsData = 0;
 
         /* Advance by data padding */
-        pixbytes = rt_pixtype_size(ctx, band->pixtype);
+        pixbytes = rt_pixtype_size(band->pixtype);
         ptr += pixbytes - 1;
 
         /* Read nodata value */
@@ -3510,9 +3742,9 @@ rt_raster_deserialize(rt_context ctx, void* serialized) {
             }
             default:
             {
-                ctx->err("rt_raster_deserialize: Unknown pixeltype %d", band->pixtype);
-                ctx->dealloc(band);
-                ctx->dealloc(rast);
+                rterror("rt_raster_deserialize: Unknown pixeltype %d", band->pixtype);
+                rtdealloc(band);
+                rtdealloc(rast);
                 return 0;
             }
         }
@@ -3556,65 +3788,66 @@ rt_raster_deserialize(rt_context ctx, void* serialized) {
     return rast;
 }
 
-int rt_raster_is_empty(rt_context ctx, rt_raster raster) {
-    assert(NULL != ctx);
-
+int rt_raster_is_empty(rt_raster raster) {
+    
+    
     return (NULL == raster || raster->height <= 0 || raster->width <= 0);
 }
 
-int rt_raster_has_no_band(rt_context ctx, rt_raster raster, int nband) {
-    assert(NULL != ctx);
-
+int rt_raster_has_no_band(rt_raster raster, int nband) {
+    
+    
     return (NULL == raster || raster->numBands < nband);
 }
 
-int32_t rt_raster_copy_band(rt_context ctx, rt_raster torast,
+int32_t rt_raster_copy_band(rt_raster torast,
         rt_raster fromrast, int fromindex, int toindex)
 {
     rt_band newband = NULL;
-    assert(NULL != ctx);
+    
+    
     assert(NULL != torast);
     assert(NULL != fromrast);
 
     /* Check raster dimensions */
     if (torast->height != fromrast->height || torast->width != fromrast->width)
     {
-        ctx->err("rt_raster_copy_band: Attempting to add a band with different width or height");
+        rtwarn("rt_raster_copy_band: Attempting to add a band with different width or height");
         return -1;
     }
 
     /* Check bands limits */
     if (fromrast->numBands < 1)
     {
-        ctx->warn("rt_raster_copy_band: Second raster has no band");
+        rtwarn("rt_raster_copy_band: Second raster has no band");
         return -1;
     }
     else if (fromindex < 0)
     {
-        ctx->warn("rt_raster_copy_band: Band index for second raster < 0. Defaulted to 1");
+        rtwarn("rt_raster_copy_band: Band index for second raster < 0. Defaulted to 1");
         fromindex = 0;
     }
     else if (fromindex >= fromrast->numBands)
     {
-        ctx->warn("rt_raster_copy_band: Band index for second raster > number of bands, truncated from %u to %u", fromindex - 1, fromrast->numBands);
+        rtwarn("rt_raster_copy_band: Band index for second raster > number of bands, truncated from %u to %u", fromindex - 1, fromrast->numBands);
         fromindex = fromrast->numBands - 1;
     }
 
     if (toindex < 0)
     {
-        ctx->warn("rt_raster_copy_band: Band index for first raster < 0. Defaulted to 1");
+        rtwarn("rt_raster_copy_band: Band index for first raster < 0. Defaulted to 1");
         toindex = 0;
     }
     else if (toindex > torast->numBands)
     {
-        ctx->warn("rt_raster_copy_band: Band index for first raster > number of bands, truncated from %u to %u", toindex - 1, torast->numBands);
+        rtwarn("rt_raster_copy_band: Band index for first raster > number of bands, truncated from %u to %u", toindex - 1, torast->numBands);
         toindex = torast->numBands;
     }
 
     /* Get band from source raster */
-    newband = rt_raster_get_band(ctx, fromrast, fromindex);
+    newband = rt_raster_get_band(fromrast, fromindex);
 
     /* Add band to the second raster */
-    return rt_raster_add_band(ctx, torast, newband, toindex);
+    return rt_raster_add_band(torast, newband, toindex);
 }
 
index 13e8a50103e8df23ded161a9d4abb3abb39f4a01..6a30db9fa703ae4b2421e9d2f21182ca11202784 100644 (file)
@@ -82,8 +82,6 @@
 #endif
 #endif
 
-
-
 #include <stdlib.h> /* For size_t */
 #include <stdint.h> /* For C99 int types */
 
 #include "ogr_api.h"
 #include "../../postgis_config.h"
 
+/**
+ * @file rt_api.h
+ *
+ * This library is the generic raster handling section of PostGIS. The raster
+ * objects, constructors, destructors, and a set of spatial processing functions
+ * are implemented here.
+ * 
+ * The library is designed for use in non-PostGIS applications if necessary. The
+ * units tests at test/core (and the future loader/dumper programs) are examples
+ * of non-PostGIS applications using rt_core.
+ *
+ * Programs using this library should set up the default memory managers and error
+ * handlers by implementing an rt_init_allocators() function, which can be as
+ * a wrapper around the rt_install_default_allocators() function if you want
+ * no special handling for memory management and error reporting.
+ *
+ **/
+
 
-typedef struct rt_context_t* rt_context;
+/**
+ * Types definitions
+ */
 typedef struct rt_raster_t* rt_raster;
 typedef struct rt_band_t* rt_band;
 typedef struct rt_geomval_t* rt_geomval;
-
-
-/*- rt_context -------------------------------------------------------*/
-
+/**
+* Global functions for memory/logging handlers.
+*/
 typedef void* (*rt_allocator)(size_t size);
 typedef void* (*rt_reallocator)(void *mem, size_t size);
 typedef void  (*rt_deallocator)(void *mem);
-typedef void  (*rt_message_handler)(const char* string, ...);
+typedef void  (*rt_message_handler)(const char* string, va_list ap);
+
+/****************************************************************************
+ * Functions that must be implemented for the raster core function's caller
+ * (for example: rt_pg functions, test functions, future loader/exporter)
+ ****************************************************************************/
+
+/**
+ * Supply the memory management and error handling functions you want your
+ * application to use
+ */
+extern void rt_init_allocators(void);
+
+/*********************************************************************/
+
+
+/*******************************************************************
+ * Functions that may be used by the raster core function's caller
+ * (for example: rt_pg functions, test functions, future loader/exporter)
+ *******************************************************************/
+/**
+ * Apply the default memory management (malloc() and free()) and error handlers.
+ * Called inside rt_init_allocators() generally.
+ */
+extern void rt_install_default_allocators(void);
+
+
+/**
+ * Wrappers used for managing memory. They simply call the functions defined by 
+ * the caller
+ **/
+extern void* rtalloc(size_t size);
+extern void* rtrealloc(void* mem, size_t size);
+extern void rtdealloc(void* mem);
+
+/******************************************************************/
+
+
+/**
+ * Wrappers used for reporting errors and info.
+ **/
+void rterror(const char *fmt, ...);
+void rtinfo(const char *fmt, ...);
+void rtwarn(const char *fmt, ...);
+
+
+/**
+* The default memory/logging handlers installed by lwgeom_install_default_allocators()
+*/
+void * default_rt_allocator(size_t size);
+void * default_rt_reallocator(void * mem, size_t size);
+void default_rt_deallocator(void * mem);
+void default_rt_error_handler(const char * fmt, va_list ap);
+void default_rt_warning_handler(const char * fmt, va_list ap);
+void default_rt_info_handler(const char * fmt, va_list ap);
 
 
 /* Debugging macros */
@@ -117,14 +189,14 @@ typedef void  (*rt_message_handler)(const char* string, ...);
 #define RASTER_DEBUG(level, msg) \
     do { \
         if (POSTGIS_DEBUG_LEVEL >= level) \
-            ctx->warn("[%s:%s:%d] " msg, __FILE__, __func__, __LINE__); \
+            rtinfo("[%s:%s:%d] " msg, __FILE__, __func__, __LINE__); \
     } while (0);
 
 /* Display a formatted message at NOTICE level (like printf, with variadic arguments) */
 #define RASTER_DEBUGF(level, msg, ...) \
     do { \
         if (POSTGIS_DEBUG_LEVEL >= level) \
-        ctx->warn("[%s:%s:%d] " msg, __FILE__, __func__, __LINE__, __VA_ARGS__); \
+            rtinfo("[%s:%s:%d] " msg, __FILE__, __func__, __LINE__, __VA_ARGS__); \
     } while (0);
 
 #else
@@ -139,26 +211,14 @@ typedef void  (*rt_message_handler)(const char* string, ...);
 
 #endif
 
+
 /*- memory context -------------------------------------------------------*/
 
-/* Initialize a context object
- * @param allocator memory allocator to use, 0 to use malloc
- * @param reallocator memory reallocator to use, 0 to use realloc
- * @param deallocator memory deallocator to use, 0 to use free
- * @return an opaque rt_context, or 0 on failure (out of memory)
- */
-rt_context rt_context_new(rt_allocator allocator,
-                          rt_reallocator reallocator,
-                          rt_deallocator deallocator);
+void rt_set_handlers(rt_allocator allocator, rt_reallocator reallocator,
+        rt_deallocator deallocator, rt_message_handler error_handler, 
+        rt_message_handler info_handler, rt_message_handler warning_handler);
 
-/* Destroy context */
-void rt_context_destroy(rt_context ctx);
 
-/* Set message handlers */
-void rt_context_set_message_handlers(rt_context ctx,
-                                    rt_message_handler error_handler,
-                                    rt_message_handler warning_handler,
-                                    rt_message_handler info_handler);
 
 /*- rt_pixtype --------------------------------------------------------*/
 
@@ -181,27 +241,26 @@ typedef enum {
 /**
  * Return size in bytes of a value in the given pixtype
  */
-int rt_pixtype_size(rt_context ctx, rt_pixtype pixtype);
+int rt_pixtype_size(rt_pixtype pixtype);
 
 /**
  * Return alignment requirements for data in the given pixel type.
  * Fast access to pixel values of this type must be aligned to as
  * many bytes as returned by this function.
  */
-int rt_pixtype_alignment(rt_context ctx, rt_pixtype pixtype);
+int rt_pixtype_alignment(rt_pixtype pixtype);
 
 /* Return human-readable name of pixel type */
-const char* rt_pixtype_name(rt_context ctx, rt_pixtype pixtype);
+const char* rt_pixtype_name(rt_pixtype pixtype);
 
 /* Return pixel type index from human-readable name */
-rt_pixtype rt_pixtype_index_from_name(rt_context ctx, const char* pixname);
+rt_pixtype rt_pixtype_index_from_name(const char* pixname);
 
 /*- rt_band ----------------------------------------------------------*/
 
 /**
  * Create an in-buffer rt_band with no data
  *
- * @param ctx       : context, for thread safety
  * @param width     : number of pixel columns
  * @param height    : number of pixel rows
  * @param pixtype   : pixel type for the band
@@ -219,15 +278,13 @@ rt_pixtype rt_pixtype_index_from_name(rt_context ctx, const char* pixname);
  *
  * @return an rt_band, or 0 on failure
  */
-rt_band rt_band_new_inline(rt_context ctx,
-                           uint16_t width, uint16_t height,
+rt_band rt_band_new_inline(uint16_t width, uint16_t height,
                            rt_pixtype pixtype, uint32_t hasnodata,
                            double nodataval, uint8_t* data);
 
 /**
  * Create an on-disk rt_band
  *
- * @param ctx       : context, for thread safety
  * @param width     : number of pixel columns
  * @param height    : number of pixel rows
  * @param pixtype   : pixel type for the band
@@ -243,8 +300,7 @@ rt_band rt_band_new_inline(rt_context ctx,
  *
  * @return an rt_band, or 0 on failure
  */
-rt_band rt_band_new_offline(rt_context ctx,
-                            uint16_t width, uint16_t height,
+rt_band rt_band_new_offline(uint16_t width, uint16_t height,
                             rt_pixtype pixtype, uint32_t hasnodata,
                             double nodataval, uint8_t bandNum, const char* path);
 
@@ -252,79 +308,73 @@ rt_band rt_band_new_offline(rt_context ctx,
  * Return non-zero if the given band data is on
  * the filesystem.
  *
- * @param ctx : context, for thread safety
  * @param band : the band
  *
  * @return non-zero if the given band data is on
  *         the filesystem.
  */
-int rt_band_is_offline(rt_context ctx, rt_band band);
+int rt_band_is_offline(rt_band band);
 
 /**
  * Return bands' external path (only valid when rt_band_is_offline
  * returns non-zero).
  */
-const char* rt_band_get_ext_path(rt_context ctx, rt_band band);
+const char* rt_band_get_ext_path(rt_band band);
 
 /**
  * Return bands' external band number (only valid when
  * rt_band_is_offline returns non-zero).
  */
-uint8_t rt_band_get_ext_band_num(rt_context ctx, rt_band band);
+uint8_t rt_band_get_ext_band_num(rt_band band);
 
 
 /* Get pixeltype of this band */
-rt_pixtype rt_band_get_pixtype(rt_context ctx, rt_band band);
+rt_pixtype rt_band_get_pixtype(rt_band band);
 
 /* Get width of this band */
-uint16_t rt_band_get_width(rt_context ctx, rt_band band);
+uint16_t rt_band_get_width(rt_band band);
 
 /* Get height of this band */
-uint16_t rt_band_get_height(rt_context ctx, rt_band band);
+uint16_t rt_band_get_height(rt_band band);
 
 /* Get pointer to inline raster band data
  * @@deprecate ?
  */
-void* rt_band_get_data(rt_context ctx, rt_band band);
+void* rt_band_get_data(rt_band band);
 
 /* Destroy a raster band */
-void rt_band_destroy(rt_context ctx, rt_band band);
+void rt_band_destroy(rt_band band);
 
 /**
  * Get hasnodata flag value
- * @param ctx : context, for thread safety
  * @param band : the band on which to check the hasnodata flag
  * @return the hasnodata flag.
  */
-int rt_band_get_hasnodata_flag(rt_context ctx, rt_band band);
+int rt_band_get_hasnodata_flag(rt_band band);
 
 /**
  * Set hasnodata flag value
- * @param ctx : context, for thread safety
  * @param band : the band on which to set the hasnodata flag
  * @param flag : the new hasnodata flag value. Must be 1 or 0.
  */
-void rt_band_set_hasnodata_flag(rt_context ctx, rt_band band, int flag);
+void rt_band_set_hasnodata_flag(rt_band band, int flag);
 
 /**
  * Set isnodata flag value
- * @param ctx : context, for thread safety
  * @param band : the band on which to set the isnodata flag
  * @param flag : the new isnodata flag value. Must be 1 or 0
  */
-void rt_band_set_isnodata_flag(rt_context ctx, rt_band band, int flag);
+void rt_band_set_isnodata_flag(rt_band band, int flag);
 
 /**
  * Get hasnodata flag value
- * @param ctx : context, for thread safety
  * @param band : the band on which to check the isnodata flag
  * @return the hasnodata flag.
  */
-int rt_band_get_isnodata_flag(rt_context ctx, rt_band band);
+int rt_band_get_isnodata_flag(rt_band band);
 
 /**
  * Set nodata value
- * @param ctx : context, for thread safety
  * @param band : the band to set nodata value to
  * @param val : the nodata value, must be in the range
  *              of values supported by this band's pixeltype
@@ -334,19 +384,17 @@ int rt_band_get_isnodata_flag(rt_context ctx, rt_band band);
  * @return 0 on success, -1 on error (value out of valid range).
  *
  */
-int rt_band_set_nodata(rt_context ctx, rt_band band, double val);
+int rt_band_set_nodata(rt_band band, double val);
 
 /**
  * Get nodata value
- * @param ctx : context, for thread safety
  * @param band : the band to set nodata value to
  * @return nodata value
  */
-double rt_band_get_nodata(rt_context ctx, rt_band band);
+double rt_band_get_nodata(rt_band band);
 
 /**
  * Set pixel value
- * @param ctx : context, for thread safety
  * @param band : the band to set nodata value to
  * @param x : x ordinate
  * @param y : x ordinate
@@ -357,46 +405,42 @@ double rt_band_get_nodata(rt_context ctx, rt_band band);
  *
  * @return 0 on success, -1 on error (value out of valid range).
  */
-int rt_band_set_pixel(rt_context ctx, rt_band band,
+int rt_band_set_pixel(rt_band band,
                       uint16_t x, uint16_t y, double val);
 
 /**
  * Get pixel value
  *
- * @param ctx : context, for thread safety
  * @param band : the band to set nodata value to
  * @param x : x ordinate
  * @param y : x ordinate
  * @param *result: result if there is a value
  * @return the pixel value, as a double.
  */
-int rt_band_get_pixel(rt_context ctx, rt_band band,
+int rt_band_get_pixel(rt_band band,
                          uint16_t x, uint16_t y, double *result );
 
 
 /**
  * Returns the minimal possible value for the band according to the pixel type.
- * @param ctx: context, for thread safety
  * @param band: the band to get info from
  * @return the minimal possible value for the band.
  */
-double rt_band_get_min_value(rt_context ctx, rt_band band);
+double rt_band_get_min_value(rt_band band);
 
 /**
  * Returns TRUE if the band is only nodata values
- * @param ctx: context, for thread safety
  * @param band: the band to get info from
  * @return TRUE if the band is only nodata values, FALSE otherwise
  */
-int rt_band_is_nodata(rt_context ctx, rt_band band);
+int rt_band_is_nodata(rt_band band);
 
 /**
  * Returns TRUE if the band is only nodata values
- * @param ctx: context, for thread safety
  * @param band: the band to get info from
  * @return TRUE if the band is only nodata values, FALSE otherwise
  */
-int rt_band_check_is_nodata(rt_context ctx, rt_band band);
+int rt_band_check_is_nodata(rt_band band);
 
 
 
@@ -408,18 +452,16 @@ int rt_band_check_is_nodata(rt_context ctx, rt_band band);
  * Transform will be set to identity.
  * Will contain no bands.
  *
- * @param ctx : context, for thread safety
  * @param width : number of pixel columns
  * @param height : number of pixel rows
  *
  * @return an rt_raster or 0 if out of memory
  */
-rt_raster rt_raster_new(rt_context ctx, uint16_t width, uint16_t height);
+rt_raster rt_raster_new(uint16_t width, uint16_t height);
 
 /**
  * Construct an rt_raster from a binary WKB representation
  *
- * @param ctx : context, for thread safety
  * @param wkb : an octet stream
  * @param wkbsize : size (in bytes) of the wkb octet stream
  *
@@ -427,13 +469,12 @@ rt_raster rt_raster_new(rt_context ctx, uint16_t width, uint16_t height);
  *         malformed WKB).
  *
  */
-rt_raster rt_raster_from_wkb(rt_context ctx, const uint8_t* wkb,
+rt_raster rt_raster_from_wkb(const uint8_t* wkb,
                              uint32_t wkbsize);
 
 /**
  * Construct an rt_raster from a text HEXWKB representation
  *
- * @param ctx : context, for thread safety
  * @param hexwkb : an hex-encoded stream
  * @param hexwkbsize : size (in bytes) of the hexwkb stream
  *
@@ -441,28 +482,26 @@ rt_raster rt_raster_from_wkb(rt_context ctx, const uint8_t* wkb,
  *         malformed WKB).
  *
  */
-rt_raster rt_raster_from_hexwkb(rt_context ctx, const char* hexwkb,
+rt_raster rt_raster_from_hexwkb(const char* hexwkb,
                              uint32_t hexwkbsize);
 
 /**
  * Return this raster in WKB form
  *
- * @param ctx : context, for thread safety
  * @param raster : the raster
  * @param wkbsize : will be set to the size of returned wkb form
  */
-uint8_t *rt_raster_to_wkb(rt_context ctx, rt_raster raster,
+uint8_t *rt_raster_to_wkb(rt_raster raster,
                                     uint32_t *wkbsize);
 
 /**
  * Return this raster in HEXWKB form (null-terminated hex)
  *
- * @param ctx : context, for thread safety
  * @param raster : the raster
  * @param hexwkbsize : will be set to the size of returned wkb form,
  *                     not including the null termination
  */
-char *rt_raster_to_hexwkb(rt_context ctx, rt_raster raster,
+char *rt_raster_to_hexwkb(rt_raster raster,
                                     uint32_t *hexwkbsize);
 
 /**
@@ -473,27 +512,26 @@ char *rt_raster_to_hexwkb(rt_context ctx, rt_raster raster,
  * the one associated with the pointers pointing
  * at them).
  *
- * @param ctx : context, for thread safety
+
  * @param raster : the raster to destroy
  */
-void rt_raster_destroy(rt_context ctx, rt_raster raster);
+void rt_raster_destroy(rt_raster raster);
 
 /* Get number of bands */
-int rt_raster_get_num_bands(rt_context ctx, rt_raster raster);
+int rt_raster_get_num_bands(rt_raster raster);
 
 /* Return Nth band, or 0 if unavailable */
-rt_band rt_raster_get_band(rt_context ctx, rt_raster raster, int bandNum);
+rt_band rt_raster_get_band(rt_raster raster, int bandNum);
 
 /* Get number of rows */
-uint16_t rt_raster_get_width(rt_context ctx, rt_raster raster);
+uint16_t rt_raster_get_width(rt_raster raster);
 
 /* Get number of columns */
-uint16_t rt_raster_get_height(rt_context ctx, rt_raster raster);
+uint16_t rt_raster_get_height(rt_raster raster);
 
 /**
  * Add band data to a raster.
  *
- * @param ctx : context, for thread safety
  * @param raster : the raster to add a band to
  * @param band : the band to add, ownership left to caller.
  *               Band dimensions are required to match with raster ones.
@@ -501,14 +539,13 @@ uint16_t rt_raster_get_height(rt_context ctx, rt_raster raster);
  *
  * @return identifier (position) for the just-added raster, or -1 on error
  */
-int32_t rt_raster_add_band(rt_context ctx, rt_raster raster, rt_band band, int index);
+int32_t rt_raster_add_band(rt_raster raster, rt_band band, int index);
 
 
 
 /**
  * Generate a new band data and add it to a raster.
  *
- * @param ctx : context, for thread safety
  * @param raster : the raster to add a band to
  * @param pixtype: the pixel type for the new band
  * @param initialvalue: initial value for pixels
@@ -518,135 +555,123 @@ int32_t rt_raster_add_band(rt_context ctx, rt_raster raster, rt_band band, int i
  *
  * @return identifier (position) for the just-added raster, or -1 on error
  */
-int32_t rt_raster_generate_new_band(rt_context ctx, rt_raster raster, rt_pixtype pixtype,
+int32_t rt_raster_generate_new_band(rt_raster raster, rt_pixtype pixtype,
         double initialvalue, uint32_t hasnodata, double nodatavalue, int index);
 
 /**
  * Set scale in projection units
  *
- * @param ctx : context, for thread safety
  * @param raster : the raster to set georeference of
  * @param scaleX : scale X in projection units
  * @param scaleY : scale Y height in projection units
  *
  * NOTE: doesn't recompute offsets
  */
-void rt_raster_set_scale(rt_context ctx, rt_raster raster,
+void rt_raster_set_scale(rt_raster raster,
                                double scaleX, double scaleY);
 
 /**
  * Get scale X in projection units
  *
- * @param ctx : context, for thread safety
  * @param raster : the raster to get georeference of
  *
  * @return scale X in projection units
  */
-double rt_raster_get_x_scale(rt_context ctx, rt_raster raster);
+double rt_raster_get_x_scale(rt_raster raster);
 
 /**
  * Get scale Y in projection units
  *
- * @param ctx : context, for thread safety
  * @param raster : the raster to get georeference of
  *
  * @return scale Y in projection units
  */
-double rt_raster_get_y_scale(rt_context ctx, rt_raster raster);
+double rt_raster_get_y_scale(rt_raster raster);
 
 /**
  * Set insertion points in projection units
  *
- * @param ctx : context, for thread safety
  * @param raster : the raster to set georeference of
  * @param x : x ordinate of the upper-left corner of upper-left pixel,
  *            in projection units
  * @param y : y ordinate of the upper-left corner of upper-left pixel,
  *            in projection units
  */
-void rt_raster_set_offsets(rt_context ctx, rt_raster raster,
+void rt_raster_set_offsets(rt_raster raster,
                            double x, double y);
 
 /**
  * Get raster x offset, in projection units
  *
- * @param ctx : context, for thread safety
  * @param raster : the raster to get georeference of
  *
  * @return  x ordinate of the upper-left corner of upper-left pixel,
  *          in projection units
  */
-double rt_raster_get_x_offset(rt_context ctx, rt_raster raster);
+double rt_raster_get_x_offset(rt_raster raster);
 
 /**
  * Get raster y offset, in projection units
  *
- * @param ctx : context, for thread safety
  * @param raster : the raster to get georeference of
  *
  * @return  y ordinate of the upper-left corner of upper-left pixel,
  *          in projection units
  */
-double rt_raster_get_y_offset(rt_context ctx, rt_raster raster);
+double rt_raster_get_y_offset(rt_raster raster);
 
 /**
  * Set skews about the X and Y axis
  *
- * @param ctx : context, for thread safety
  * @param raster : the raster to set georeference of
  * @param skewX : skew about the x axis
  * @param skewY : skew about the y axis
  */
-void rt_raster_set_skews(rt_context ctx, rt_raster raster,
+void rt_raster_set_skews(rt_raster raster,
                              double skewX, double skewY);
 
 /**
  * Get skew about the X axis
  *
- * @param ctx : context, for thread safety
  * @param raster : the raster to set georeference of
  * @return skew about the Y axis
  */
-double rt_raster_get_x_skew(rt_context ctx, rt_raster raster);
+double rt_raster_get_x_skew(rt_raster raster);
 
 /**
  * Get skew about the Y axis
  *
- * @param ctx : context, for thread safety
  * @param raster : the raster to set georeference of
  * @return skew about the Y axis
  */
-double rt_raster_get_y_skew(rt_context ctx, rt_raster raster);
+double rt_raster_get_y_skew(rt_raster raster);
 
 /**
  * Set raster's SRID
  *
- * @param ctx : context, for thread safety
  * @param raster : the raster to set SRID of
  * @param srid : the SRID to set for the raster
  */
-void rt_raster_set_srid(rt_context ctx, rt_raster raster, int32_t srid);
+void rt_raster_set_srid(rt_raster raster, int32_t srid);
 
 /**
  * Get raster's SRID
- * @param ctx : context, for thread safety
  * @param raster : the raster to set SRID of
  *
  * @return the raster's SRID
  */
-int32_t rt_raster_get_srid(rt_context ctx, rt_raster raster);
+int32_t rt_raster_get_srid(rt_raster raster);
 
 /**
  * Convert an x,y raster point to an x1,y1 point on map
  *
- * @param ctx : context for thread safety
  * @param raster : the raster to get info from
  * @param x : the pixel's column
  * @param y : the pixel's row
  * @param x1 : output parameter, X ordinate of the geographical point
  * @param y1 : output parameter, Y ordinate of the geographical point
  */
-void rt_raster_cell_to_geopoint(rt_context ctx, rt_raster raster,
+void rt_raster_cell_to_geopoint(rt_raster raster,
                                 double x, double y,
                                 double* x1, double* y1);
 
@@ -657,13 +682,12 @@ void rt_raster_cell_to_geopoint(rt_context ctx, rt_raster raster,
  * ring polygon bearing the raster's rotation
  * and using projection coordinates
  *
- * @param ctx : context for thread safety
  * @param raster : the raster to get info from
  *
  * @return the convex hull, or NULL on error.
  *
  */
-LWPOLY* rt_raster_get_convex_hull(rt_context ctx, rt_raster raster);
+LWPOLY* rt_raster_get_convex_hull(rt_raster raster);
 
 
 /**
@@ -674,7 +698,6 @@ LWPOLY* rt_raster_get_convex_hull(rt_context ctx, rt_raster raster);
  * representation of a geometry (one for each group of pixel sharing
  * the same value) and the value associated with this geometry.
  *
- * @param ctx: context for thread safety.
  * @param raster: the raster to get info from.
  * @param nband: the band to polygonize. From 1 to rt_raster_get_num_bands
  *
@@ -684,7 +707,7 @@ LWPOLY* rt_raster_get_convex_hull(rt_context ctx, rt_raster raster);
  * future, and the function returns real geometries)
  */
 rt_geomval
-rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
+rt_raster_dump_as_wktpolygons(rt_raster raster, int nband,
         int * pnElements);
 
 
@@ -694,7 +717,7 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
  * Serialized form is documented in doc/RFC1-SerializedFormat.
  *
  */
-void* rt_raster_serialize(rt_context ctx, rt_raster raster);
+void* rt_raster_serialize(rt_raster raster);
 
 /**
  * Return a raster from a serialized form.
@@ -704,41 +727,47 @@ void* rt_raster_serialize(rt_context ctx, rt_raster raster);
  * NOTE: the raster will contain pointer to the serialized
  *       form, which must be kept alive.
  */
-rt_raster rt_raster_deserialize(rt_context ctx, void* serialized);
+rt_raster rt_raster_deserialize(void* serialized);
 
 
 /**
  * Return TRUE if the raster is empty. i.e. is NULL, width = 0 or height = 0
- * @param ctx: context, for thread safety
  * @param raster: the raster to get info from
  * @return TRUE if the raster is empty, FALSE otherwise
  */
-int rt_raster_is_empty(rt_context ctx, rt_raster raster);
+int rt_raster_is_empty(rt_raster raster);
 
 /**
  * Return TRUE if the raster do not have a band of this number.
- * @param ctx: context, for thread safety
  * @param raster: the raster to get info from
  * @param nband: the band number.
  * @return TRUE if the raster do not have a band of this number, FALSE otherwise
  */
-int rt_raster_has_no_band(rt_context ctx, rt_raster raster, int nband);
+int rt_raster_has_no_band(rt_raster raster, int nband);
 
 
 /**
  * Copy one band from one raster to another
- * @param ctx: context, for thread safety
  * @param torast: raster to copy band to
  * @param fromrast: raster to copy band from
  * @param fromindex: index of band in source raster
  * @param toindex: index of new band in destination raster
  * @return The band index of the second raster where the new band is copied.
  */
-int32_t rt_raster_copy_band(rt_context ctx, rt_raster torast,
+int32_t rt_raster_copy_band(rt_raster torast,
         rt_raster fromrast, int fromindex, int toindex);
 
 /*- utilities -------------------------------------------------------*/
 
+/*
+ * rt_core memory functions
+ */
+extern void *rtalloc(size_t size);
+extern void *rtrealloc(void *mem, size_t size);
+extern void rtdealloc(void *mem);
+
+
+
 /* Set of functions to clamp double to int of different size
  */
 
@@ -777,8 +806,7 @@ float
 rt_util_clamp_to_32F(double value);
 
 int
-rt_util_display_dbl_trunc_warning(rt_context ctx,
-                                  double initialvalue,
+rt_util_display_dbl_trunc_warning(double initialvalue,
                                   int32_t checkvalint,
                                   uint32_t checkvaluint,
                                   float checkvalfloat,
index 4824acdd8265116744036147903c12f08355917d..d37cfc8ca6338b2703ff04e206de23f2a5f81c0c 100644 (file)
@@ -59,10 +59,6 @@ PG_MODULE_MAGIC;
 #endif
 
 /* Internal funcs */
-static rt_context get_rt_context(FunctionCallInfoData *fcinfo);
-static void *rt_pgalloc(size_t size);
-static void *rt_pgrealloc(void *mem, size_t size);
-static void rt_pgfree(void *mem);
 static char * replace(const char *str, const char *oldstr, const char *newstr,
         int *count);
 static char *strtoupper(char *str);
@@ -77,23 +73,49 @@ static char *strtoupper(char *str);
  * only when:
  *
  * -something wrong happen with memory, 
- * -a function got an invalid argument ('3BUI' as pixel type) so that no row can be processed
+ * -a function got an invalid argument ('3BUI' as pixel type) so that no row can
+ * be processed
  *
  * Send a NOTICE like:
  *
  * elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
  *
- * when arguments (e.g. x, y, band) are NULL or out of range so that some or most rows can be processed anyway
+ * when arguments (e.g. x, y, band) are NULL or out of range so that some or
+ * most rows can be processed anyway
  *
  * in this case, 
- * for SET functions or function normally returning a modified raster, return the original raster
+ * for SET functions or function normally returning a modified raster, return
+ * the original raster
  * for GET functions, return NULL
- * try to deduce a valid parameter value if it makes sence (e.g. out of range index for addband)
+ * try to deduce a valid parameter value if it makes sence (e.g. out of range
+ * index for addband)
  *
  * Do not put the name of the faulty function for NOTICEs, only with ERRORs.
  *
  ****************************************************************/
 
+/******************************************************************************
+ * Some notes on memory management...
+ *
+ * Every time a SQL function is called, PostgreSQL creates a  new memory context.
+ * So, all the memory allocated with palloc/repalloc in that context is
+ * automatically free'd at the end of the function. If you want some data to
+ * live between function calls, you have 2 options:
+ *
+ * - Use fcinfo->flinfo->fn_mcxt contex to store the data (by pointing the
+ *   data you want to keep with fcinfo->flinfo->fn_extra)
+ * - Use SRF funcapi, and storing the data at multi_call_memory_ctx (by pointing
+ *   the data you want to keep with funcctx->user_fctx. funcctx is created by
+ *   funcctx = SPI_FIRSTCALL_INIT()). Recommended way in functions returning rows,
+ *   like RASTER_dumpAsWKTPolygons (see section 34.9.9 at
+ *   http://www.postgresql.org/docs/8.4/static/xfunc-c.html).
+ *
+ * But raster code follows the same philosophy than the rest of PostGIS: keep
+ * memory as clean as possible. So, we free all allocated memory.
+ *
+ * TODO: In case of functions returning NULL, we should free the memory too.
+ *****************************************************************************/
+
 /* Prototypes */
 
 /* Utility functions */
@@ -159,7 +181,9 @@ Datum RASTER_copyband(PG_FUNCTION_ARGS);
 Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS);
 
 
-/* Replace function taken from http://ubuntuforums.org/showthread.php?s=aa6f015109fd7e4c7e30d2fd8b717497&t=141670&page=3 */
+/* Replace function taken from 
+ * http://ubuntuforums.org/showthread.php?s=aa6f015109fd7e4c7e30d2fd8b717497&t=141670&page=3
+ */
 /* ---------------------------------------------------------------------------
   Name       : replace - Search & replace a substring by another one.
   Creation   : Thierry Husson, Sept 2010
@@ -192,21 +216,7 @@ replace(const char *str, const char *oldstr, const char *newstr, int *count)
 
    length = strlen(str) + found * (newlen - oldlen);
 
-   /**
-    * From: http://www.postgresql.org/docs/8.4/static/spi-memory.html
-    * "(...) if your procedure needs to return an object in allocated memory
-    * (such as a value of a pass-by-reference data type), you cannot allocate
-    * that memory using palloc, at least not while you are connected to SPI.
-    * If you try, the object will be deallocated by SPI_finish, and your
-    * procedure will not work reliably. To solve this problem, use SPI_palloc to
-    * allocate memory for your return object. SPI_palloc allocates memory in the
-    * "upper executor context", that is, the memory context that was current
-    * when SPI_connect was called, which is precisely the right context for a
-    * value returned from your procedure."
-    *
-    * So, we use SPI_palloc for allocating memory.
-    */
-   if ( (result = (char *)SPI_palloc(length+1)) == NULL) {
+   if ( (result = (char *)palloc(length+1)) == NULL) {
       fprintf(stderr, "Not enough memory\n");
       found = -1;
    } else {
@@ -241,111 +251,6 @@ strtoupper(char * str)
 
 }
 
-
-static void *
-rt_pgalloc(size_t size)
-{
-    void* ret;
-    POSTGIS_RT_DEBUGF(5, "rt_pgalloc(%ld) called", size);
-    ret = palloc(size);
-    return ret;
-}
-
-static void *
-rt_pgrealloc(void *mem, size_t size)
-{
-    void* ret;
-
-    POSTGIS_RT_DEBUGF(5, "rt_pgrealloc(%p, %ld) called", mem, size);
-
-    if ( mem )
-    {
-
-        POSTGIS_RT_DEBUGF(5, "rt_pgrealloc calling repalloc(%p, %ld) called", mem, size);
-        ret = repalloc(mem, size);
-    }
-    else
-    {
-        POSTGIS_RT_DEBUGF(5, "rt_pgrealloc calling palloc(%ld)", size);
-        ret = palloc(size);
-    }
-    return ret;
-}
-
-static void
-rt_pgfree(void *mem)
-{
-    POSTGIS_RT_DEBUGF(5, "rt_pgfree(%p) calling pfree", mem);
-    pfree(mem);
-}
-
-static void
-rt_pgerr(const char *fmt, ...)
-{
-    va_list ap;
-    char msg[1024];
-
-    va_start(ap, fmt);
-
-    vsnprintf(msg, 1023, fmt, ap);
-
-    elog(ERROR, "%s", msg);
-
-    va_end(ap);
-}
-
-static void
-rt_pgwarn(const char *fmt, ...)
-{
-    va_list ap;
-    char msg[1024];
-
-    va_start(ap, fmt);
-
-    vsnprintf(msg, 1023, fmt, ap);
-
-    elog(WARNING, "%s", msg);
-
-    va_end(ap);
-}
-
-static void
-rt_pginfo(const char *fmt, ...)
-{
-    va_list ap;
-    char msg[1024];
-
-    va_start(ap, fmt);
-
-    vsnprintf(msg, 1023, fmt, ap);
-
-    elog(INFO, "%s", msg);
-
-    va_end(ap);
-}
-
-
-static rt_context
-get_rt_context(FunctionCallInfoData *fcinfo)
-{
-    rt_context ctx = 0;
-    MemoryContext old_context;
-
-    if ( ctx ) return ctx;
-
-    /* We switch memory context info so the rt_context
-     * is not freed by the end of function call
-     * TODO: check _which_ context we should be using
-     * for a whole-plugin-lifetime persistence */
-    old_context = MemoryContextSwitchTo(fcinfo->flinfo->fn_mcxt);
-    ctx = rt_context_new(rt_pgalloc, rt_pgrealloc, rt_pgfree);
-    MemoryContextSwitchTo(old_context);
-
-    rt_context_set_message_handlers(ctx, rt_pgerr, rt_pgwarn, rt_pginfo);
-
-    return ctx;
-}
-
 PG_FUNCTION_INFO_V1(RASTER_lib_version);
 Datum RASTER_lib_version(PG_FUNCTION_ARGS)
 {
@@ -378,13 +283,14 @@ Datum RASTER_in(PG_FUNCTION_ARGS)
     rt_raster raster;
     char *hexwkb = PG_GETARG_CSTRING(0);
     void *result = NULL;
-    rt_context ctx = get_rt_context(fcinfo);
 
-    raster = rt_raster_from_hexwkb(ctx, hexwkb, strlen(hexwkb));
-    result = rt_raster_serialize(ctx, raster);
+    raster = rt_raster_from_hexwkb(hexwkb, strlen(hexwkb));
+    result = rt_raster_serialize(raster);
 
     SET_VARSIZE(result, ((rt_pgraster*)result)->size);
 
+    rt_raster_destroy(raster);
+
     if ( NULL != result )
         PG_RETURN_POINTER(result);
     else
@@ -401,24 +307,28 @@ Datum RASTER_out(PG_FUNCTION_ARGS)
     rt_raster raster = NULL;
     uint32_t hexwkbsize = 0;
     char *hexwkb = NULL;
-    rt_context ctx = NULL;
     
     pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
-    ctx = get_rt_context(fcinfo);
 
-    raster = rt_raster_deserialize(ctx, pgraster);
+    raster = rt_raster_deserialize(pgraster);
     if ( ! raster ) {
         elog(ERROR, "RASTER_out: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
-    hexwkb = rt_raster_to_hexwkb(ctx, raster, &hexwkbsize);
+    hexwkb = rt_raster_to_hexwkb(raster, &hexwkbsize);
     if ( ! hexwkb )
     {
         elog(ERROR, "RASTER_out: Could not HEX-WKBize raster");
         PG_RETURN_NULL();
     }
 
+    /* Free the raster objects used */
+    rt_raster_destroy(raster);
+
+    /* Call free_if_copy on the "varlena" structures originally get as args */
+    PG_FREE_IF_COPY(pgraster, 0);
+
     PG_RETURN_CSTRING(hexwkb);
 }
 
@@ -430,32 +340,37 @@ Datum RASTER_to_bytea(PG_FUNCTION_ARGS)
 {
     rt_pgraster *pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
     rt_raster raster = NULL;
-    rt_context ctx = get_rt_context(fcinfo);
     uint8_t *wkb = NULL;
     uint32_t wkb_size = 0;
     bytea *result = NULL;
     int result_size = 0;
 
-    raster = rt_raster_deserialize(ctx, pgraster);
+    /* Get raster object */
+    raster = rt_raster_deserialize(pgraster);
     if ( ! raster ) {
         elog(ERROR, "RASTER_to_bytea: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
-    /* Uses context allocator */
-    wkb = rt_raster_to_wkb(ctx, raster, &wkb_size);
+    /* Parse raster to wkb object */
+    wkb = rt_raster_to_wkb(raster, &wkb_size);
     if ( ! wkb ) {
         elog(ERROR, "RASTER_to_bytea: Could not allocate and generate WKB data");
         PG_RETURN_NULL();
     }
 
-    /* TODO: Replace all palloc/pfree, malloc/free, etc. with rt_context_t->{alloc/dealloc}
-     * FIXME: ATM, impossible as rt_context_t is incomplete type for rt_pg layer. */
+    /* Create varlena object */
     result_size = wkb_size + VARHDRSZ;
     result = (bytea *)palloc(result_size);
     SET_VARSIZE(result, result_size);
     memcpy(VARDATA(result), wkb, VARSIZE(result) - VARHDRSZ);
-    pfree(wkb);
+
+    /* Free raster objects used */
+    rt_raster_destroy(raster);
+    rtdealloc(wkb);
+
+     /* Call free_if_copy on the "varlena" structures originally get as args */
+    PG_FREE_IF_COPY(pgraster, 0);
 
     PG_RETURN_POINTER(result);
 }
@@ -468,32 +383,37 @@ Datum RASTER_to_binary(PG_FUNCTION_ARGS)
 {
     rt_pgraster *pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
     rt_raster raster = NULL;
-    rt_context ctx = get_rt_context(fcinfo);
     uint8_t *wkb = NULL;
     uint32_t wkb_size = 0;
     char *result = NULL;
     int result_size = 0;
 
-    raster = rt_raster_deserialize(ctx, pgraster);
+    /* Get raster object */
+    raster = rt_raster_deserialize(pgraster);
     if ( ! raster ) {
         elog(ERROR, "RASTER_to_binary: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
-    /* Uses context allocator */
-    wkb = rt_raster_to_wkb(ctx, raster, &wkb_size);
+    /* Parse raster to wkb object */
+    wkb = rt_raster_to_wkb(raster, &wkb_size);
     if ( ! wkb ) {
         elog(ERROR, "RASTER_to_binary: Could not allocate and generate WKB data");
         PG_RETURN_NULL();
     }
 
-    /* TODO: Replace all palloc/pfree, malloc/free, etc. with rt_context_t->{alloc/dealloc}
-     * FIXME: ATM, impossible as rt_context_t is incomplete type for rt_pg layer. */
+    /* Create varlena object */
     result_size = wkb_size + VARHDRSZ;
     result = (char *)palloc(result_size);
     SET_VARSIZE(result, result_size);
     memcpy(VARDATA(result), wkb, VARSIZE(result) - VARHDRSZ);
-    pfree(wkb);
+
+    /* Free raster objects used */
+    rt_raster_destroy(raster);
+    rtdealloc(wkb);
+
+     /* Call free_if_copy on the "varlena" structures originally get as args */
+    PG_FREE_IF_COPY(pgraster, 0);
 
     PG_RETURN_POINTER(result);
 }
@@ -506,18 +426,17 @@ Datum RASTER_convex_hull(PG_FUNCTION_ARGS)
 {
     rt_pgraster *pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
     rt_raster raster;
-    rt_context ctx = get_rt_context(fcinfo);
     LWPOLY* convexhull;
     uchar* pglwgeom;
 
     { /* TODO: can be optimized to only detoast the header! */
-        raster = rt_raster_deserialize(ctx, pgraster);
+        raster = rt_raster_deserialize(pgraster);
         if ( ! raster ) {
             elog(ERROR, "RASTER_convex_hull: Could not deserialize raster");
             PG_RETURN_NULL();
         }
 
-        convexhull = rt_raster_get_convex_hull(ctx, raster);
+        convexhull = rt_raster_get_convex_hull(raster);
         if ( ! convexhull ) {
             elog(ERROR, "RASTER_convex_hull: Could not get raster's convex hull");
             PG_RETURN_NULL();
@@ -538,11 +457,13 @@ Datum RASTER_convex_hull(PG_FUNCTION_ARGS)
         SET_VARSIZE(pglwgeom, VARHDRSZ+sz);
 #endif
     }
+    
+    /* Free raster and lwgeom memory */
+    rt_raster_destroy(raster);
+    lwfree(convexhull);
 
-    /* PG_FREE_IF_COPY(pgraster, 0); */
-    /* lwfree(convexhull) */
-    /* ... more deallocs ... */
-
+    /* Free input varlena object */
+    PG_FREE_IF_COPY(pgraster, 0);
 
     PG_RETURN_POINTER(pglwgeom);
 }
@@ -561,9 +482,8 @@ struct rt_geomval_t {
 PG_FUNCTION_INFO_V1(RASTER_dumpAsWKTPolygons);
 Datum RASTER_dumpAsWKTPolygons(PG_FUNCTION_ARGS)
 {
-    rt_pgraster *pgraster;
-    rt_raster raster;
-    rt_context ctx;
+    rt_pgraster *pgraster = NULL;
+    rt_raster raster = NULL;
     FuncCallContext *funcctx;
     TupleDesc tupdesc;
     AttInMetadata *attinmeta;
@@ -573,7 +493,7 @@ Datum RASTER_dumpAsWKTPolygons(PG_FUNCTION_ARGS)
     int call_cntr;
     int max_calls;
     int nElements;
-    MemoryContext   oldcontext;
+    MemoryContext oldcontext;
 
 
     /* stuff done only on the first call of the function */
@@ -587,17 +507,9 @@ Datum RASTER_dumpAsWKTPolygons(PG_FUNCTION_ARGS)
         /* switch to memory context appropriate for multiple function calls */
         oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
 
-        /**
-         * Create a new context. We don't call get_rt_context, because this
-         * function changes the memory context to the one pointed by
-         * fcinfo->flinfo->fn_mcxt, and we want to keep the current one
-         */
-        ctx = rt_context_new(rt_pgalloc, rt_pgrealloc, rt_pgfree);
-        rt_context_set_message_handlers(ctx, rt_pgerr, rt_pgwarn, rt_pginfo);
-
         /* Get input arguments */
         pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
-        raster = rt_raster_deserialize(ctx, pgraster);
+        raster = rt_raster_deserialize(pgraster);
         if ( ! raster )
         {
             ereport(ERROR,
@@ -618,7 +530,7 @@ Datum RASTER_dumpAsWKTPolygons(PG_FUNCTION_ARGS)
         /**
          * Dump raster
          */
-        geomval = rt_raster_dump_as_wktpolygons(ctx, raster, nband, &nElements);
+        geomval = rt_raster_dump_as_wktpolygons(raster, nband, &nElements);
         if (NULL == geomval)
         {
             ereport(ERROR,
@@ -628,12 +540,7 @@ Datum RASTER_dumpAsWKTPolygons(PG_FUNCTION_ARGS)
         }
 
         POSTGIS_RT_DEBUGF(3, "raster dump, %d elements returned", nElements);
-
-        /**
-         * Not needed to check geomval. It was allocated by the new
-         * rt_context, that uses palloc. It never returns NULL
-         */
-
+        
         /* Store needed information */
         funcctx->user_fctx = geomval;
 
@@ -667,7 +574,6 @@ Datum RASTER_dumpAsWKTPolygons(PG_FUNCTION_ARGS)
 
     if (call_cntr < max_calls)    /* do when there is more left to send */
     {
-
         char       **values;
         HeapTuple    tuple;
         Datum        result;
@@ -698,12 +604,9 @@ Datum RASTER_dumpAsWKTPolygons(PG_FUNCTION_ARGS)
 
         /**
          * Free resources.
-         * TODO: Do we have to use the same context we used
-         * for creating them?
          */
         pfree(geomval2[call_cntr].geom);
 
-
         /* build a tuple */
         tuple = BuildTupleFromCStrings(attinmeta, values);
 
@@ -722,6 +625,8 @@ Datum RASTER_dumpAsWKTPolygons(PG_FUNCTION_ARGS)
     else    /* do when there is no more left */
     {
         pfree(geomval2);
+        //rt_raster_destroy(raster);
+        //PG_FREE_IF_COPY(pgraster, 0);
         SRF_RETURN_DONE(funcctx);
     }
 
@@ -742,7 +647,6 @@ Datum RASTER_makeEmpty(PG_FUNCTION_ARGS)
     int32_t srid;
     rt_pgraster *pgraster;
     rt_raster raster;
-    rt_context ctx;
 
     if ( PG_NARGS() < 9 )
     {
@@ -799,22 +703,23 @@ Datum RASTER_makeEmpty(PG_FUNCTION_ARGS)
                   width, height, ipx, ipy, scalex, scaley,
                   skewx, skewy, srid);
 
-    ctx = get_rt_context(fcinfo);
-
-    raster = rt_raster_new(ctx, width, height);
+    raster = rt_raster_new(width, height);
     if ( ! raster ) {
         PG_RETURN_NULL(); /* error was supposedly printed already */
     }
 
-    rt_raster_set_scale(ctx, raster, scalex, scaley);
-    rt_raster_set_offsets(ctx, raster, ipx, ipy);
-    rt_raster_set_skews(ctx, raster, skewx, skewy);
-    rt_raster_set_srid(ctx, raster, srid);
+    rt_raster_set_scale(raster, scalex, scaley);
+    rt_raster_set_offsets(raster, ipx, ipy);
+    rt_raster_set_skews(raster, skewx, skewy);
+    rt_raster_set_srid(raster, srid);
 
-    pgraster = rt_raster_serialize(ctx, raster);
+    pgraster = rt_raster_serialize(raster);
     if ( ! pgraster ) PG_RETURN_NULL();
 
     SET_VARSIZE(pgraster, pgraster->size);
+
+    rt_raster_destroy(raster);
+
     PG_RETURN_POINTER(pgraster);
 }
 
@@ -826,17 +731,20 @@ Datum RASTER_getSRID(PG_FUNCTION_ARGS)
 {
     rt_pgraster *pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
     rt_raster raster;
-    rt_context ctx = get_rt_context(fcinfo);
     int32_t srid;
 
     /* TODO: can be optimized to only detoast the header! */
-    raster = rt_raster_deserialize(ctx, pgraster);
+    raster = rt_raster_deserialize(pgraster);
     if ( ! raster ) {
         elog(ERROR, "RASTER_getSRID: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
-    srid = rt_raster_get_srid(ctx, raster);
+    srid = rt_raster_get_srid(raster);
+
+    rt_raster_destroy(raster);
+    PG_FREE_IF_COPY(pgraster, 0);
+
     PG_RETURN_INT32(srid);
 }
 
@@ -848,22 +756,26 @@ Datum RASTER_setSRID(PG_FUNCTION_ARGS)
 {
     rt_pgraster *pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
     rt_raster raster;
-    rt_context ctx = get_rt_context(fcinfo);
     int32_t newSRID = PG_GETARG_INT32(1);
 
     /* TODO: can be optimized to only detoast the header! */
-    raster = rt_raster_deserialize(ctx, pgraster);
+    raster = rt_raster_deserialize(pgraster);
     if ( ! raster ) {
         elog(ERROR, "RASTER_setSRID: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
-    rt_raster_set_srid(ctx, raster, newSRID);
+    rt_raster_set_srid(raster, newSRID);
+
+    //PG_FREE_IF_COPY(pgraster, 0);
 
-    pgraster = rt_raster_serialize(ctx, raster);
+    pgraster = rt_raster_serialize(raster);
     if ( ! pgraster ) PG_RETURN_NULL();
 
     SET_VARSIZE(pgraster, pgraster->size);
+
+    rt_raster_destroy(raster);
+
     PG_RETURN_POINTER(pgraster);
 }
 
@@ -875,17 +787,20 @@ Datum RASTER_getWidth(PG_FUNCTION_ARGS)
 {
     rt_pgraster *pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
     rt_raster raster;
-    rt_context ctx = get_rt_context(fcinfo);
     uint16_t width;
 
     /* TODO: can be optimized to only detoast the header! */
-    raster = rt_raster_deserialize(ctx, pgraster);
+    raster = rt_raster_deserialize(pgraster);
     if ( ! raster ) {
         elog(ERROR, "RASTER_getWidth: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
-    width = rt_raster_get_width(ctx, raster);
+    width = rt_raster_get_width(raster);
+
+    rt_raster_destroy(raster);
+    PG_FREE_IF_COPY(pgraster, 0);
+
     PG_RETURN_INT32(width);
 }
 
@@ -897,17 +812,20 @@ Datum RASTER_getHeight(PG_FUNCTION_ARGS)
 {
     rt_pgraster *pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
     rt_raster raster;
-    rt_context ctx = get_rt_context(fcinfo);
     uint16_t height;
 
     /* TODO: can be optimized to only detoast the header! */
-    raster = rt_raster_deserialize(ctx, pgraster);
+    raster = rt_raster_deserialize(pgraster);
     if ( ! raster ) {
         elog(ERROR, "RASTER_getHeight: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
-    height = rt_raster_get_height(ctx, raster);
+    height = rt_raster_get_height(raster);
+
+    rt_raster_destroy(raster);
+    PG_FREE_IF_COPY(pgraster, 0);
+
     PG_RETURN_INT32(height);
 }
 
@@ -919,17 +837,20 @@ Datum RASTER_getNumBands(PG_FUNCTION_ARGS)
 {
     rt_pgraster *pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
     rt_raster raster;
-    rt_context ctx = get_rt_context(fcinfo);
     int32_t num_bands;
 
     /* TODO: can be optimized to only detoast the header! */
-    raster = rt_raster_deserialize(ctx, pgraster);
+    raster = rt_raster_deserialize(pgraster);
     if ( ! raster ) {
         elog(ERROR, "RASTER_getNumBands: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
-    num_bands = rt_raster_get_num_bands(ctx, raster);
+    num_bands = rt_raster_get_num_bands(raster);
+
+    rt_raster_destroy(raster);
+    PG_FREE_IF_COPY(pgraster, 0);
+    
     PG_RETURN_INT32(num_bands);
 }
 
@@ -941,17 +862,20 @@ Datum RASTER_getXScale(PG_FUNCTION_ARGS)
 {
     rt_pgraster *pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
     rt_raster raster;
-    rt_context ctx = get_rt_context(fcinfo);
     double xsize;
 
     /* TODO: can be optimized to only detoast the header! */
-    raster = rt_raster_deserialize(ctx, pgraster);
+    raster = rt_raster_deserialize(pgraster);
     if ( ! raster ) {
         elog(ERROR, "RASTER_getXScale: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
-    xsize = rt_raster_get_x_scale(ctx, raster);
+    xsize = rt_raster_get_x_scale(raster);
+
+    rt_raster_destroy(raster);
+    PG_FREE_IF_COPY(pgraster, 0);
+    
     PG_RETURN_FLOAT8(xsize);
 }
 
@@ -963,17 +887,21 @@ Datum RASTER_getYScale(PG_FUNCTION_ARGS)
 {
     rt_pgraster *pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
     rt_raster raster;
-    rt_context ctx = get_rt_context(fcinfo);
     double ysize;
 
     /* TODO: can be optimized to only detoast the header! */
-    raster = rt_raster_deserialize(ctx, pgraster);
+    raster = rt_raster_deserialize(pgraster);
     if ( ! raster ) {
         elog(ERROR, "RASTER_getYScale: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
-    ysize = rt_raster_get_y_scale(ctx, raster);
+    ysize = rt_raster_get_y_scale(raster);
+
+    rt_raster_destroy(raster);
+
+    PG_FREE_IF_COPY(pgraster, 0);
+    
     PG_RETURN_FLOAT8(ysize);
 }
 
@@ -985,21 +913,25 @@ Datum RASTER_setScale(PG_FUNCTION_ARGS)
 {
     rt_pgraster *pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
     rt_raster raster;
-    rt_context ctx = get_rt_context(fcinfo);
     double size = PG_GETARG_FLOAT8(1);
 
-    raster = rt_raster_deserialize(ctx, pgraster);
+    raster = rt_raster_deserialize(pgraster);
     if (! raster ) {
         elog(ERROR, "RASTER_setScale: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
-    rt_raster_set_scale(ctx, raster, size, size);
+    rt_raster_set_scale(raster, size, size);
 
-    pgraster = rt_raster_serialize(ctx, raster);
+    //PG_FREE_IF_COPY(pgraster, 0);
+
+    pgraster = rt_raster_serialize(raster);
     if ( ! pgraster ) PG_RETURN_NULL();
 
     SET_VARSIZE(pgraster, pgraster->size);
+
+    rt_raster_destroy(raster);
+    
     PG_RETURN_POINTER(pgraster);
 }
 
@@ -1011,22 +943,26 @@ Datum RASTER_setScaleXY(PG_FUNCTION_ARGS)
 {
     rt_pgraster *pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
     rt_raster raster;
-    rt_context ctx = get_rt_context(fcinfo);
     double xscale = PG_GETARG_FLOAT8(1);
     double yscale = PG_GETARG_FLOAT8(2);
 
-    raster = rt_raster_deserialize(ctx, pgraster);
+    raster = rt_raster_deserialize(pgraster);
     if (! raster ) {
         elog(ERROR, "RASTER_setScaleXY: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
-    rt_raster_set_scale(ctx, raster, xscale, yscale);
+    rt_raster_set_scale(raster, xscale, yscale);
 
-    pgraster = rt_raster_serialize(ctx, raster);
+    //PG_FREE_IF_COPY(pgraster, 0);
+
+    pgraster = rt_raster_serialize(raster);
     if ( ! pgraster ) PG_RETURN_NULL();
 
     SET_VARSIZE(pgraster, pgraster->size);
+
+    rt_raster_destroy(raster);
+
     PG_RETURN_POINTER(pgraster);
 }
 
@@ -1038,17 +974,21 @@ Datum RASTER_getXSkew(PG_FUNCTION_ARGS)
 {
     rt_pgraster *pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
     rt_raster raster;
-    rt_context ctx = get_rt_context(fcinfo);
     double xskew;
 
     /* TODO: can be optimized to only detoast the header! */
-    raster = rt_raster_deserialize(ctx, pgraster);
+    raster = rt_raster_deserialize(pgraster);
     if ( ! raster ) {
         elog(ERROR, "RASTER_getXSkew: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
-    xskew = rt_raster_get_x_skew(ctx, raster);
+    xskew = rt_raster_get_x_skew(raster);
+
+    rt_raster_destroy(raster);
+
+    PG_FREE_IF_COPY(pgraster, 0);
+    
     PG_RETURN_FLOAT8(xskew);
 }
 
@@ -1060,17 +1000,21 @@ Datum RASTER_getYSkew(PG_FUNCTION_ARGS)
 {
     rt_pgraster *pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
     rt_raster raster;
-    rt_context ctx = get_rt_context(fcinfo);
     double yskew;
 
     /* TODO: can be optimized to only detoast the header! */
-    raster = rt_raster_deserialize(ctx, pgraster);
+    raster = rt_raster_deserialize(pgraster);
     if ( ! raster ) {
         elog(ERROR, "RASTER_getYSkew: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
-    yskew = rt_raster_get_y_skew(ctx, raster);
+    yskew = rt_raster_get_y_skew(raster);
+
+    rt_raster_destroy(raster);
+
+    PG_FREE_IF_COPY(pgraster, 0);
+    
     PG_RETURN_FLOAT8(yskew);
 }
 
@@ -1082,21 +1026,25 @@ Datum RASTER_setSkew(PG_FUNCTION_ARGS)
 {
     rt_pgraster *pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
     rt_raster raster;
-    rt_context ctx = get_rt_context(fcinfo);
     double skew = PG_GETARG_FLOAT8(1);
 
-    raster = rt_raster_deserialize(ctx, pgraster);
+    raster = rt_raster_deserialize(pgraster);
     if (! raster ) {
         elog(ERROR, "RASTER_setSkew: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
-    rt_raster_set_skews(ctx, raster, skew, skew);
+    rt_raster_set_skews(raster, skew, skew);
 
-    pgraster = rt_raster_serialize(ctx, raster);
+    //PG_FREE_IF_COPY(raster, 0);
+
+    pgraster = rt_raster_serialize(raster);
     if ( ! pgraster ) PG_RETURN_NULL();
 
     SET_VARSIZE(pgraster, pgraster->size);
+
+    rt_raster_destroy(raster);
+    
     PG_RETURN_POINTER(pgraster);
 }
 
@@ -1108,22 +1056,26 @@ Datum RASTER_setSkewXY(PG_FUNCTION_ARGS)
 {
     rt_pgraster *pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
     rt_raster raster;
-    rt_context ctx = get_rt_context(fcinfo);
     double xskew = PG_GETARG_FLOAT8(1);
     double yskew = PG_GETARG_FLOAT8(2);
 
-    raster = rt_raster_deserialize(ctx, pgraster);
+    raster = rt_raster_deserialize(pgraster);
     if (! raster ) {
         elog(ERROR, "RASTER_setSkewXY: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
-    rt_raster_set_skews(ctx, raster, xskew, yskew);
+    rt_raster_set_skews(raster, xskew, yskew);
+
+    //PG_FREE_IF_COPY(pgraster, 0);
 
-    pgraster = rt_raster_serialize(ctx, raster);
+    pgraster = rt_raster_serialize(raster);
     if ( ! pgraster ) PG_RETURN_NULL();
 
     SET_VARSIZE(pgraster, pgraster->size);
+
+    rt_raster_destroy(raster);
+    
     PG_RETURN_POINTER(pgraster);
 }
 
@@ -1135,17 +1087,20 @@ Datum RASTER_getXUpperLeft(PG_FUNCTION_ARGS)
 {
     rt_pgraster *pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
     rt_raster raster;
-    rt_context ctx = get_rt_context(fcinfo);
     double xul;
 
     /* TODO: can be optimized to only detoast the header! */
-    raster = rt_raster_deserialize(ctx, pgraster);
+    raster = rt_raster_deserialize(pgraster);
     if ( ! raster ) {
         elog(ERROR, "RASTER_getXUpperLeft: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
-    xul = rt_raster_get_x_offset(ctx, raster);
+    xul = rt_raster_get_x_offset(raster);
+
+    rt_raster_destroy(raster);    
+    PG_FREE_IF_COPY(pgraster, 0);
+    
     PG_RETURN_FLOAT8(xul);
 }
 
@@ -1157,17 +1112,20 @@ Datum RASTER_getYUpperLeft(PG_FUNCTION_ARGS)
 {
     rt_pgraster *pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
     rt_raster raster;
-    rt_context ctx = get_rt_context(fcinfo);
     double yul;
 
     /* TODO: can be optimized to only detoast the header! */
-    raster = rt_raster_deserialize(ctx, pgraster);
+    raster = rt_raster_deserialize(pgraster);
     if ( ! raster ) {
         elog(ERROR, "RASTER_getYUpperLeft: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
-    yul = rt_raster_get_y_offset(ctx, raster);
+    yul = rt_raster_get_y_offset(raster);
+
+    rt_raster_destroy(raster);
+    PG_FREE_IF_COPY(pgraster, 0);
+
     PG_RETURN_FLOAT8(yul);
 }
 
@@ -1179,22 +1137,26 @@ Datum RASTER_setUpperLeftXY(PG_FUNCTION_ARGS)
 {
     rt_pgraster *pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
     rt_raster raster;
-    rt_context ctx = get_rt_context(fcinfo);
     double xoffset = PG_GETARG_FLOAT8(1);
     double yoffset = PG_GETARG_FLOAT8(2);
 
-    raster = rt_raster_deserialize(ctx, pgraster);
+    raster = rt_raster_deserialize(pgraster);
     if (! raster ) {
         elog(ERROR, "RASTER_setUpperLeftXY: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
-    rt_raster_set_offsets(ctx, raster, xoffset, yoffset);
+    rt_raster_set_offsets(raster, xoffset, yoffset);
 
-    pgraster = rt_raster_serialize(ctx, raster);
+    //PG_FREE_IF_COPY(pgraster, 0);
+
+    pgraster = rt_raster_serialize(raster);
     if ( ! pgraster ) PG_RETURN_NULL();
 
     SET_VARSIZE(pgraster, pgraster->size);
+
+    rt_raster_destroy(raster);
+
     PG_RETURN_POINTER(pgraster);
 }
 
@@ -1208,7 +1170,6 @@ Datum RASTER_getBandPixelType(PG_FUNCTION_ARGS)
     rt_pgraster *pgraster = NULL;
     rt_raster raster = NULL;
     rt_band band = NULL;
-    rt_context ctx = NULL;
     rt_pixtype pixtype;
     int32_t bandindex;
 
@@ -1218,26 +1179,28 @@ Datum RASTER_getBandPixelType(PG_FUNCTION_ARGS)
         elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
         PG_RETURN_NULL();
     }
-    assert(0 <= (bandindex - 1));
 
     /* Deserialize raster */
     pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
-    ctx = get_rt_context(fcinfo);
 
-    raster = rt_raster_deserialize(ctx, pgraster);
+    raster = rt_raster_deserialize(pgraster);
     if ( ! raster ) {
         elog(ERROR, "RASTER_getBandPixelType: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
     /* Fetch requested band and its pixel type */
-    band = rt_raster_get_band(ctx, raster, bandindex - 1);
+    band = rt_raster_get_band(raster, bandindex - 1);
     if ( ! band ) {
         elog(NOTICE, "Could not find raster band of index %d when getting pixel type. Returning NULL", bandindex);
         PG_RETURN_NULL();
     }
 
-    pixtype = rt_band_get_pixtype(ctx, band);
+    pixtype = rt_band_get_pixtype(band);
+
+    rt_raster_destroy(raster);
+    PG_FREE_IF_COPY(pgraster, 0);
+
     PG_RETURN_INT32(pixtype);
 }
 
@@ -1252,7 +1215,6 @@ Datum RASTER_getBandPixelTypeName(PG_FUNCTION_ARGS)
     rt_pgraster *pgraster = NULL;
     rt_raster raster = NULL;
     rt_band band = NULL;
-    rt_context ctx = NULL;
     rt_pixtype pixtype;
     int32_t bandindex;
     const size_t name_size = 8; /* size of type name */
@@ -1266,26 +1228,24 @@ Datum RASTER_getBandPixelTypeName(PG_FUNCTION_ARGS)
         elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
         PG_RETURN_NULL();
     }
-    assert(0 <= (bandindex - 1));
 
     /* Deserialize raster */
     pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
-    ctx = get_rt_context(fcinfo);
 
-    raster = rt_raster_deserialize(ctx, pgraster);
+    raster = rt_raster_deserialize(pgraster);
     if ( ! raster ) {
         elog(ERROR, "RASTER_getBandPixelTypeName: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
     /* Fetch requested band and its pixel type */
-    band = rt_raster_get_band(ctx, raster, bandindex - 1);
+    band = rt_raster_get_band(raster, bandindex - 1);
     if ( ! band ) {
         elog(NOTICE, "Could not find raster band of index %d when getting pixel type name. Returning NULL", bandindex);
         PG_RETURN_NULL();
     }
 
-    pixtype = rt_band_get_pixtype(ctx, band);
+    pixtype = rt_band_get_pixtype(band);
 
     result = palloc(VARHDRSZ + name_size);
     /* We don't need to check for NULL pointer, because if out of memory, palloc
@@ -1340,6 +1300,9 @@ Datum RASTER_getBandPixelTypeName(PG_FUNCTION_ARGS)
     size = VARHDRSZ + strlen(ptr);
     SET_VARSIZE(result, size);
 
+    rt_raster_destroy(raster);
+    PG_FREE_IF_COPY(pgraster, 0);
+
     PG_RETURN_TEXT_P(result);
 }
 
@@ -1353,9 +1316,8 @@ Datum RASTER_getBandNoDataValue(PG_FUNCTION_ARGS)
     rt_pgraster *pgraster = NULL;
     rt_raster raster = NULL;
     rt_band band = NULL;
-    rt_context ctx = NULL;
-    double nodata;
     int32_t bandindex;
+    double nodata;
 
     /* Index is 1-based */
     bandindex = PG_GETARG_INT32(1);
@@ -1363,31 +1325,33 @@ Datum RASTER_getBandNoDataValue(PG_FUNCTION_ARGS)
         elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
         PG_RETURN_NULL();
     }
-    assert(0 <= (bandindex - 1));
 
     /* Deserialize raster */
     pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
-    ctx = get_rt_context(fcinfo);
 
-    raster = rt_raster_deserialize(ctx, pgraster);
+    raster = rt_raster_deserialize(pgraster);
     if ( ! raster ) {
         elog(ERROR, "RASTER_getBandNoDataValue: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
     /* Fetch requested band and its nodata value */
-    band = rt_raster_get_band(ctx, raster, bandindex - 1);
+    band = rt_raster_get_band(raster, bandindex - 1);
     if ( ! band ) {
         elog(NOTICE, "Could not find raster band of index %d when getting band nodata value. Returning NULL", bandindex);
         PG_RETURN_NULL();
     }
 
-    if ( ! rt_band_get_hasnodata_flag(ctx, band) ) {
+    if ( ! rt_band_get_hasnodata_flag(band) ) {
         // Raster does not have a nodata value set so we return NULL
         PG_RETURN_NULL();
     }
 
-    nodata = rt_band_get_nodata(ctx, band);
+    nodata = rt_band_get_nodata(band);
+
+    rt_raster_destroy(raster);
+    PG_FREE_IF_COPY(pgraster, 0);
+    
     PG_RETURN_FLOAT4(nodata);
 }
 
@@ -1401,7 +1365,6 @@ Datum RASTER_setBandNoDataValue(PG_FUNCTION_ARGS)
     rt_pgraster *pgraster = NULL;
     rt_raster raster = NULL;
     rt_band band = NULL;
-    rt_context ctx = NULL;
     double nodata;
     int32_t bandindex;
     bool forcechecking = FALSE;
@@ -1423,9 +1386,8 @@ Datum RASTER_setBandNoDataValue(PG_FUNCTION_ARGS)
         PG_RETURN_NULL();
     }
     pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
-    ctx = get_rt_context(fcinfo);
 
-    raster = rt_raster_deserialize(ctx, pgraster);
+    raster = rt_raster_deserialize(pgraster);
     if (! raster) {
         elog(ERROR, "RASTER_setBandNoDataValue: Could not deserialize raster");
         PG_RETURN_NULL();
@@ -1434,7 +1396,7 @@ Datum RASTER_setBandNoDataValue(PG_FUNCTION_ARGS)
     if (! skipset) {
         assert(0 <= (bandindex - 1));
          /* Fetch requested band */
-        band = rt_raster_get_band(ctx, raster, bandindex - 1);
+        band = rt_raster_get_band(raster, bandindex - 1);
         if (! band) {
             elog(NOTICE, "Could not find raster band of index %d when setting pixel value. Nodata value not set. Returning original raster", bandindex);
         }
@@ -1444,7 +1406,7 @@ Datum RASTER_setBandNoDataValue(PG_FUNCTION_ARGS)
 
             if (PG_ARGISNULL(2)) {
                 /* Set the hasnodata flag to FALSE */
-                rt_band_set_hasnodata_flag(ctx, band, FALSE);
+                rt_band_set_hasnodata_flag(band, FALSE);
 
                 POSTGIS_RT_DEBUGF(3, "Raster band %d does not have a nodata value",
                         bandindex);
@@ -1455,22 +1417,27 @@ Datum RASTER_setBandNoDataValue(PG_FUNCTION_ARGS)
                 nodata = PG_GETARG_FLOAT8(2);
 
                 /* Set the band's nodata value */
-                rt_band_set_nodata(ctx, band, nodata);
+                rt_band_set_nodata(band, nodata);
 
                 /* Set the hasnodata flag to TRUE */
-                rt_band_set_hasnodata_flag(ctx, band, TRUE);
+                rt_band_set_hasnodata_flag(band, TRUE);
 
                 /* Recheck all pixels if requested */
                 if (forcechecking)
-                   rt_band_check_is_nodata(ctx, band);
+                   rt_band_check_is_nodata(band);
             }
         }
     }
-    
-    pgraster = rt_raster_serialize(ctx, raster);
+
+    //PG_FREE_IF_COPY(pgraster, 0);
+
+    pgraster = rt_raster_serialize(raster);
     if (! pgraster) PG_RETURN_NULL();
 
     SET_VARSIZE(pgraster, pgraster->size);
+
+    rt_raster_destroy(raster);
+
     PG_RETURN_POINTER(pgraster);
 }
 
@@ -1480,13 +1447,11 @@ Datum RASTER_setBandIsNoData(PG_FUNCTION_ARGS)
     rt_pgraster *pgraster = NULL;
     rt_raster raster = NULL;
     rt_band band = NULL;
-    rt_context ctx = NULL;
     int32_t bandindex;
 
     pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
-    ctx = get_rt_context(fcinfo);
 
-    raster = rt_raster_deserialize(ctx, pgraster);
+    raster = rt_raster_deserialize(pgraster);
     if ( ! raster ) {
         elog(ERROR, "RASTER_setBandIsNoData: Could not deserialize raster");
         PG_RETURN_NULL();
@@ -1501,23 +1466,26 @@ Datum RASTER_setBandIsNoData(PG_FUNCTION_ARGS)
     if (bandindex < 1)
         elog(NOTICE, "Invalid band index (must use 1-based). Isnodata flag not set. Returning original raster");
     else {
-        assert(0 <= (bandindex - 1));
 
         /* Fetch requested band */
-        band = rt_raster_get_band(ctx, raster, bandindex - 1);
+        band = rt_raster_get_band(raster, bandindex - 1);
 
         if ( ! band )
             elog(NOTICE, "Could not find raster band of index %d. Isnodata flag not set. Returning original raster", bandindex);
         else
             /* Set the band's nodata value */
-            rt_band_set_isnodata_flag(ctx, band, 1);
+            rt_band_set_isnodata_flag(band, 1);
     }
 
+    //PG_FREE_IF_COPY(pgraster, 0);
     /* Serialize raster again */
-    pgraster = rt_raster_serialize(ctx, raster);
+    pgraster = rt_raster_serialize(raster);
     if ( ! pgraster ) PG_RETURN_NULL();
 
     SET_VARSIZE(pgraster, pgraster->size);
+
+    rt_raster_destroy(raster);
+
     PG_RETURN_POINTER(pgraster);
 }
 
@@ -1527,9 +1495,9 @@ Datum RASTER_bandIsNoData(PG_FUNCTION_ARGS)
     rt_pgraster *pgraster = NULL;
     rt_raster raster = NULL;
     rt_band band = NULL;
-    rt_context ctx = NULL;
     int32_t bandindex;
     bool forcechecking = FALSE;
+    bool bandisnodata = FALSE;
 
     /* Index is 1-based */
     bandindex = PG_GETARG_INT32(1);
@@ -1537,20 +1505,18 @@ Datum RASTER_bandIsNoData(PG_FUNCTION_ARGS)
         elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
         PG_RETURN_NULL();
     }
-    assert(0 <= (bandindex - 1));
 
     /* Deserialize raster */
     pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
-    ctx = get_rt_context(fcinfo);
 
-    raster = rt_raster_deserialize(ctx, pgraster);
+    raster = rt_raster_deserialize(pgraster);
     if ( ! raster ) {
         elog(ERROR, "RASTER_bandIsNoData: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
     /* Fetch requested band and its nodata value */
-    band = rt_raster_get_band(ctx, raster, bandindex - 1);
+    band = rt_raster_get_band(raster, bandindex - 1);
     if ( ! band ) {
         elog(NOTICE, "Could not find raster band of index %d when determining if band is nodata. Returning NULL", bandindex);
         PG_RETURN_NULL();
@@ -1558,10 +1524,13 @@ Datum RASTER_bandIsNoData(PG_FUNCTION_ARGS)
 
     forcechecking = PG_GETARG_BOOL(2);
 
-    if (forcechecking)
-        PG_RETURN_BOOL(rt_band_check_is_nodata(ctx, band));
-    else
-        PG_RETURN_BOOL(rt_band_get_isnodata_flag(ctx, band));
+    bandisnodata = (forcechecking) ?
+        rt_band_check_is_nodata(band) : rt_band_get_isnodata_flag(band);
+
+    rt_raster_destroy(raster);
+    PG_FREE_IF_COPY(pgraster, 0);
+
+    PG_RETURN_BOOL(bandisnodata);
 }
 
 
@@ -1575,7 +1544,6 @@ Datum RASTER_getBandPath(PG_FUNCTION_ARGS)
     rt_pgraster *pgraster = NULL;
     rt_raster raster = NULL;
     rt_band band = NULL;
-    rt_context ctx = NULL;
     int32_t bandindex;
     const char *bandpath;
     text *result;
@@ -1586,26 +1554,24 @@ Datum RASTER_getBandPath(PG_FUNCTION_ARGS)
         elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
         PG_RETURN_NULL();
     }
-    assert(0 <= (bandindex - 1));
 
     /* Deserialize raster */
     pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
-    ctx = get_rt_context(fcinfo);
 
-    raster = rt_raster_deserialize(ctx, pgraster);
+    raster = rt_raster_deserialize(pgraster);
     if ( ! raster ) {
         elog(ERROR, "RASTER_getBandPath: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
     /* Fetch requested band and its nodata value */
-    band = rt_raster_get_band(ctx, raster, bandindex - 1);
+    band = rt_raster_get_band(raster, bandindex - 1);
     if ( ! band ) {
         elog(NOTICE, "Could not find raster band of index %d when getting band path. Returning NULL", bandindex);
         PG_RETURN_NULL();
     }
 
-    bandpath = rt_band_get_ext_path(ctx, band);
+    bandpath = rt_band_get_ext_path(band);
     if ( ! bandpath )
     {
         PG_RETURN_NULL();
@@ -1616,6 +1582,10 @@ Datum RASTER_getBandPath(PG_FUNCTION_ARGS)
     SET_VARSIZE(result, VARHDRSZ + strlen(bandpath) + 1);
 
     strcpy((char *) VARDATA(result), bandpath);
+
+    rt_raster_destroy(raster);
+    PG_FREE_IF_COPY(pgraster, 0);
+
     PG_RETURN_TEXT_P(result);
 }
 
@@ -1633,7 +1603,6 @@ Datum RASTER_getPixelValue(PG_FUNCTION_ARGS)
     rt_pgraster *pgraster = NULL;
     rt_raster raster = NULL;
     rt_band band = NULL;
-    rt_context ctx = NULL;
     double pixvalue = 0;
     int32_t bandindex = 0;
     int32_t x = 0;
@@ -1647,7 +1616,6 @@ Datum RASTER_getPixelValue(PG_FUNCTION_ARGS)
         elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
         PG_RETURN_NULL();
     }
-    assert(0 <= (bandindex - 1));
 
     x = PG_GETARG_INT32(2);
 
@@ -1659,33 +1627,33 @@ Datum RASTER_getPixelValue(PG_FUNCTION_ARGS)
 
     /* Deserialize raster */
     pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
-    ctx = get_rt_context(fcinfo);
 
-    raster = rt_raster_deserialize(ctx, pgraster);
+    raster = rt_raster_deserialize(pgraster);
     if (!raster) {
         elog(ERROR, "RASTER_getPixelValue: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
     /* Fetch Nth band using 0-based internal index */
-    band = rt_raster_get_band(ctx, raster, bandindex - 1);
+    band = rt_raster_get_band(raster, bandindex - 1);
     if (! band) {
-        elog(NOTICE, "Could not find raster band of index %d when getting pixel value. Returning NULL", bandindex);
+        elog(NOTICE, "Could not find raster band of index %d when getting pixel "
+                "value. Returning NULL", bandindex);
         PG_RETURN_NULL();
     }
     /* Fetch pixel using 0-based coordinates */
-    result = rt_band_get_pixel(ctx, band, x - 1, y - 1, &pixvalue);
-    
-    /* If the result is -1 or the value is nodata and we take nodata into account then return nodata = NULL */
-    if (result == -1 || (hasnodata && rt_band_get_hasnodata_flag(ctx, band) && pixvalue == rt_band_get_nodata(ctx, band))) {
-        rt_raster_destroy(ctx, raster);
-        rt_context_destroy(ctx);        
+    result = rt_band_get_pixel(band, x - 1, y - 1, &pixvalue);
+
+    /* If the result is -1 or the value is nodata and we take nodata into account
+     * then return nodata = NULL */
+    if (result == -1 || (hasnodata && rt_band_get_hasnodata_flag(band) &&
+            pixvalue == rt_band_get_nodata(band))) {
         PG_RETURN_NULL();
     }
 
-    rt_raster_destroy(ctx, raster);
-    rt_context_destroy(ctx);        
-
+    rt_raster_destroy(raster);
+    PG_FREE_IF_COPY(pgraster, 0);
     PG_RETURN_FLOAT8(pixvalue);
 }
 
@@ -1698,7 +1666,6 @@ Datum RASTER_setPixelValue(PG_FUNCTION_ARGS)
     rt_pgraster *pgraster = NULL;
     rt_raster raster = NULL;
     rt_band band = NULL;
-    rt_context ctx = NULL;
     double pixvalue = 0;
     int32_t bandindex = 0;
     int32_t x = 0;
@@ -1715,20 +1682,23 @@ Datum RASTER_setPixelValue(PG_FUNCTION_ARGS)
     else
         bandindex = PG_GETARG_INT32(1);
     if ( bandindex < 1 ) {
-        elog(NOTICE, "Invalid band index (must use 1-based). Value not set. Returning original raster");
+        elog(NOTICE, "Invalid band index (must use 1-based). Value not set. "
+                "Returning original raster");
         skipset = TRUE;
     }
 
     /* Validate pixel coordinates are not null */
     if (PG_ARGISNULL(2)) {
-        elog(NOTICE, "X coordinate can not be NULL when getting pixel value. Value not set. Returning original raster");
+        elog(NOTICE, "X coordinate can not be NULL when getting pixel value. "
+                "Value not set. Returning original raster");
         skipset = TRUE;
     }
     else
         x = PG_GETARG_INT32(2);
 
     if (PG_ARGISNULL(3)) {
-        elog(NOTICE, "Y coordinate can not be NULL when getting pixel value. Value not set. Returning original raster");
+        elog(NOTICE, "Y coordinate can not be NULL when getting pixel value. "
+                "Value not set. Returning original raster");
         skipset = TRUE;
     }
     else 
@@ -1738,42 +1708,50 @@ Datum RASTER_setPixelValue(PG_FUNCTION_ARGS)
 
     /* Deserialize raster */
     pgraster = (rt_pgraster *)PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0));
-    ctx = get_rt_context(fcinfo);
 
-    raster = rt_raster_deserialize(ctx, pgraster);
+    raster = rt_raster_deserialize(pgraster);
     if ( ! raster ) {
         elog(ERROR, "RASTER_setPixelValue: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
     if (! skipset) {
-        assert(0 <= (bandindex - 1));
         /* Fetch requested band */
-        band = rt_raster_get_band(ctx, raster, bandindex - 1);
+        band = rt_raster_get_band(raster, bandindex - 1);
         if ( ! band ) {
-            elog(NOTICE, "Could not find raster band of index %d when setting pixel value. Value not set. Returning original raster", bandindex);
+            elog(NOTICE, "Could not find raster band of index %d when setting "
+                    "pixel value. Value not set. Returning original raster",
+                    bandindex);
         }
         else {
             /* Set the pixel value */
             if (PG_ARGISNULL(4)) {
-                if (!rt_band_get_hasnodata_flag(ctx, band)) {
-                    elog(NOTICE, "Raster do not have a nodata value defined. Set band nodata value first. Nodata value not set. Returning original raster");
+                if (!rt_band_get_hasnodata_flag(band)) {
+                    elog(NOTICE, "Raster do not have a nodata value defined. "
+                            "Set band nodata value first. Nodata value not set. "
+                            "Returning original raster");
                 }
                 else {
-                    pixvalue = rt_band_get_nodata(ctx, band);
-                    rt_band_set_pixel(ctx, band, x - 1, y - 1, pixvalue);
+                    pixvalue = rt_band_get_nodata(band);
+                    rt_band_set_pixel(band, x - 1, y - 1, pixvalue);
                 }
             }
             else {
                 pixvalue = PG_GETARG_FLOAT8(4);
-                rt_band_set_pixel(ctx, band, x - 1, y - 1, pixvalue);
+                rt_band_set_pixel(band, x - 1, y - 1, pixvalue);
             }
         }
     }
-    pgraster = rt_raster_serialize(ctx, raster);
+
+    //PG_FREE_IF_COPY(pgraster, 0);
+
+    pgraster = rt_raster_serialize(raster);
     if ( ! pgraster ) PG_RETURN_NULL();
 
     SET_VARSIZE(pgraster, pgraster->size);
+
+    rt_raster_destroy(raster);
+
     PG_RETURN_POINTER(pgraster);
 }
 
@@ -1785,7 +1763,6 @@ Datum RASTER_addband(PG_FUNCTION_ARGS)
 {
     rt_pgraster *pgraster = NULL;
     rt_raster raster = NULL;
-    rt_context ctx = NULL;
 
     int bandindex = 0;
     double initialvalue = 0;
@@ -1821,7 +1798,6 @@ Datum RASTER_addband(PG_FUNCTION_ARGS)
         PG_RETURN_NULL();
     }
     pgraster = (rt_pgraster *)PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0));
-    ctx = get_rt_context(fcinfo);
 
     /* Get the pixel type in text form */
     if (PG_ARGISNULL(2)) {
@@ -1832,54 +1808,64 @@ Datum RASTER_addband(PG_FUNCTION_ARGS)
     pixeltypename = PG_GETARG_TEXT_P(2);
     new_pixeltypename = (char *) palloc(VARSIZE(pixeltypename) + 1 - VARHDRSZ);
     SET_VARSIZE(new_pixeltypename, VARSIZE(pixeltypename));
-    memcpy(new_pixeltypename, VARDATA(pixeltypename), VARSIZE(pixeltypename) - VARHDRSZ);
+    memcpy(new_pixeltypename, VARDATA(pixeltypename),
+        VARSIZE(pixeltypename) - VARHDRSZ);
     new_pixeltypename[VARSIZE(pixeltypename) - VARHDRSZ] = 0; /* null terminate */
 
     /* Get the pixel type index */
-    pixtype = rt_pixtype_index_from_name(ctx, new_pixeltypename);
+    pixtype = rt_pixtype_index_from_name(new_pixeltypename);
     if ( pixtype == PT_END ) {
         elog(ERROR, "RASTER_addband: Invalid pixel type: %s", new_pixeltypename);
         PG_RETURN_NULL();
     }
-    assert(-1 < pixtype && pixtype < PT_END);
 
-    raster = rt_raster_deserialize(ctx, pgraster);
+    raster = rt_raster_deserialize(pgraster);
     if ( ! raster ) {
         elog(ERROR, "RASTER_addband: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
     /* Make sure index (1 based) is in a valid range */
-    oldnumbands = rt_raster_get_num_bands(ctx, raster);
+    oldnumbands = rt_raster_get_num_bands(raster);
     if (PG_ARGISNULL(1))
         bandindex = oldnumbands + 1;
     else
     {
         bandindex = PG_GETARG_INT32(1);
         if (bandindex < 1) {
-            elog(NOTICE, "Invalid band index (must use 1-based). Band not added. Returning original raster");
+            elog(NOTICE, "Invalid band index (must use 1-based). Band not added. "
+                    "Returning original raster");
             skipaddband = TRUE;
         }
         if (bandindex > oldnumbands + 1) {
-            elog(NOTICE, "RASTER_addband: Band index number exceed possible values, truncated to number of band (%u) + 1", oldnumbands);
+            elog(NOTICE, "RASTER_addband: Band index number exceed possible "
+                    "values, truncated to number of band (%u) + 1", oldnumbands);
             bandindex = oldnumbands + 1;
         }
     }
 
     if (!skipaddband) {
-        bandindex = rt_raster_generate_new_band(ctx, raster, pixtype, initialvalue,
-                                                hasnodata, nodatavalue, bandindex - 1);
+        bandindex = rt_raster_generate_new_band(raster, pixtype, initialvalue,
+                                                hasnodata, nodatavalue,
+                bandindex - 1);
 
-        numbands = rt_raster_get_num_bands(ctx, raster);
+        numbands = rt_raster_get_num_bands(raster);
         if (numbands == oldnumbands || bandindex == -1) {
-            elog(ERROR, "RASTER_addband: Could not add band to raster. Returning NULL");
+            elog(ERROR, "RASTER_addband: Could not add band to raster. "
+                    "Returning NULL");
             PG_RETURN_NULL();
         }
     }
-    pgraster = rt_raster_serialize(ctx, raster);
+
+    PG_FREE_IF_COPY(pgraster, 0);
+
+    pgraster = rt_raster_serialize(raster);
     if (!pgraster) PG_RETURN_NULL();
 
     SET_VARSIZE(pgraster, pgraster->size);
+
+    rt_raster_destroy(raster);
+
     PG_RETURN_POINTER(pgraster);
 }
 
@@ -1898,7 +1884,6 @@ Datum RASTER_copyband(PG_FUNCTION_ARGS)
     int oldtorastnumbands = 0;
     int newtorastnumbands = 0;
     int newbandindex = 0;
-    rt_context ctx = NULL;
 
     /* Deserialize torast */
     if (PG_ARGISNULL(0)) {
@@ -1906,9 +1891,8 @@ Datum RASTER_copyband(PG_FUNCTION_ARGS)
         PG_RETURN_NULL();
     }
     pgraster = (rt_pgraster *)PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0));
-    ctx = get_rt_context(fcinfo);
 
-    torast = rt_raster_deserialize(ctx, pgraster);
+    torast = rt_raster_deserialize(pgraster);
     if ( ! torast ) {
         elog(ERROR, "RASTER_copyband: Could not deserialize first raster");
         PG_RETURN_NULL();
@@ -1917,15 +1901,14 @@ Datum RASTER_copyband(PG_FUNCTION_ARGS)
     /* Deserialize fromrast */
     if (!PG_ARGISNULL(1)) {
         pgraster = (rt_pgraster *)PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(1));
-        ctx = get_rt_context(fcinfo);
 
-        fromrast = rt_raster_deserialize(ctx, pgraster);
+        fromrast = rt_raster_deserialize(pgraster);
         if ( ! fromrast ) {
             elog(ERROR, "RASTER_copyband: Could not deserialize second raster");
             PG_RETURN_NULL();
         }
 
-        oldtorastnumbands = rt_raster_get_num_bands(ctx, torast);
+        oldtorastnumbands = rt_raster_get_num_bands(torast);
 
         if (PG_ARGISNULL(2))
             fromband = 1;
@@ -1938,18 +1921,27 @@ Datum RASTER_copyband(PG_FUNCTION_ARGS)
             toindex = PG_GETARG_INT32(3);
 
         /* Copy band fromrast torast */
-        newbandindex = rt_raster_copy_band(ctx, torast, fromrast, fromband - 1, toindex - 1);
+        newbandindex = rt_raster_copy_band(torast, fromrast, fromband - 1,
+            toindex - 1);
 
-        newtorastnumbands = rt_raster_get_num_bands(ctx, torast);
+        newtorastnumbands = rt_raster_get_num_bands(torast);
         if (newtorastnumbands == oldtorastnumbands || newbandindex == -1) {
-            elog(NOTICE, "RASTER_copyband: Could not add band to raster. Returning original raster.");
+            elog(NOTICE, "RASTER_copyband: Could not add band to raster. "
+                    "Returning original raster.");
         }
     }
+
+    PG_FREE_IF_COPY(pgraster, 0);
+
     /* Serialize and return torast */
-    pgraster = rt_raster_serialize(ctx, torast);
+    pgraster = rt_raster_serialize(torast);
     if (!pgraster) PG_RETURN_NULL();
 
     SET_VARSIZE(pgraster, pgraster->size);
+
+    rt_raster_destroy(fromrast);
+    rt_raster_destroy(torast);
+
     PG_RETURN_POINTER(pgraster);
 }
 
@@ -1962,12 +1954,11 @@ Datum RASTER_isEmpty(PG_FUNCTION_ARGS)
 {
     rt_pgraster *pgraster = NULL;
     rt_raster raster = NULL;
-    rt_context ctx = NULL;
+    bool isempty = FALSE;
 
     /* Deserialize raster */
     pgraster = (rt_pgraster *)PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0));
-    ctx = get_rt_context(fcinfo);
-    raster = rt_raster_deserialize(ctx, pgraster);
+    raster = rt_raster_deserialize(pgraster);
     if ( ! raster )
     {
         ereport(ERROR,
@@ -1976,7 +1967,12 @@ Datum RASTER_isEmpty(PG_FUNCTION_ARGS)
         PG_RETURN_NULL();
     }
 
-    PG_RETURN_BOOL(rt_raster_is_empty(ctx, raster));
+    isempty = rt_raster_is_empty(raster);
+
+    rt_raster_destroy(raster);
+    PG_FREE_IF_COPY(pgraster, 0);
+
+    PG_RETURN_BOOL(isempty);
 }
 
 /**
@@ -1988,12 +1984,11 @@ Datum RASTER_hasNoBand(PG_FUNCTION_ARGS)
     rt_pgraster *pgraster = NULL;
     rt_raster raster = NULL;
     int bandindex = 0;
-    rt_context ctx = NULL;
+    bool hasnoband = FALSE;
 
     /* Deserialize raster */
     pgraster = (rt_pgraster *)PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0));
-    ctx = get_rt_context(fcinfo);
-    raster = rt_raster_deserialize(ctx, pgraster);
+    raster = rt_raster_deserialize(pgraster);
     if ( ! raster )
     {
         ereport(ERROR,
@@ -2004,8 +1999,12 @@ Datum RASTER_hasNoBand(PG_FUNCTION_ARGS)
 
     /* Get band number */
     bandindex = PG_GETARG_INT32(1);
+    hasnoband = rt_raster_has_no_band(raster, bandindex);
+
+    rt_raster_destroy(raster);
+    PG_FREE_IF_COPY(pgraster, 0);
 
-    PG_RETURN_BOOL(rt_raster_has_no_band(ctx, raster, bandindex));
+    PG_RETURN_BOOL(hasnoband);
 }
 
 PG_FUNCTION_INFO_V1(RASTER_mapAlgebra);
@@ -2014,7 +2013,6 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
     rt_pgraster *pgraster = NULL;
     rt_raster raster = NULL;
     rt_raster newrast = NULL;
-    rt_context ctx = NULL;
     rt_band band = NULL;
     rt_band newband = NULL;
     int x, y, nband, width, height;
@@ -2038,12 +2036,12 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
     SPITupleTable * tuptable = NULL;
     HeapTuple tuple;
     char * strFromText = NULL;
+    bool freemem = FALSE;
 
     POSTGIS_RT_DEBUG(2, "RASTER_mapAlgebra: Starting...");
 
     /* Check raster */
-    if (PG_ARGISNULL(0)) 
-    {
+    if (PG_ARGISNULL(0)) {
         elog(NOTICE, "Raster is NULL. Returning NULL");
         PG_RETURN_NULL();
     }
@@ -2051,158 +2049,143 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
 
     /* Deserialize raster */
     pgraster = (rt_pgraster *)PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0));
-    ctx = get_rt_context(fcinfo);
-    if (NULL == ctx)
-    {
-        elog(ERROR, "RASTER_mapAlgebra: Could not get memory context. Returning"
-                " NULL");
-        PG_RETURN_NULL();
-    }
-    raster = rt_raster_deserialize(ctx, pgraster);
-    if (NULL == raster)
-    {
+    raster = rt_raster_deserialize(pgraster);
+    if (NULL == raster) {
         elog(ERROR, "RASTER_mapAlgebra: Could not deserialize raster");
-        rt_context_destroy(ctx);
         PG_RETURN_NULL();
     }
 
-    POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebra: Getting arguments...");
+    /* We don't need this */
+    //PG_FREE_IF_COPY(pgraster, 0);
 
-    /* Get the rest of the arguments */
+       POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebra: Getting arguments...");
 
-    if (PG_ARGISNULL(1))
-        nband = 1;
-    else
-        nband = PG_GETARG_INT32(1);
+       if (PG_ARGISNULL(1))
+               nband = 1;
+       else
+               nband = PG_GETARG_INT32(1);
 
-    if (nband < 1)
-        nband = 1;
+       if (nband < 1)
+               nband = 1;
 
-    POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebra: Creating new raster...");
 
+    POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebra: Creating new empty raster...");
+    
     /**
      * Create a new empty raster with having the same georeference as the
      * provided raster
      **/
-    width = rt_raster_get_width(ctx, raster);
-    height = rt_raster_get_height(ctx, raster);
+    width = rt_raster_get_width(raster);
+    height = rt_raster_get_height(raster);
 
-    newrast = rt_raster_new(ctx, width, height);
+    newrast = rt_raster_new(width, height);
 
-    if ( NULL == newrast ) 
-    {
+    if ( NULL == newrast ) {
         elog(ERROR, "RASTER_mapAlgebra: Could not create a new raster. "
                 "Returning NULL");
-        rt_raster_destroy(ctx, raster);
-        rt_context_destroy(ctx);
         PG_RETURN_NULL();
     }
 
-    rt_raster_set_scale(ctx, newrast,
-            rt_raster_get_x_scale(ctx, raster),
-            rt_raster_get_y_scale(ctx, raster));
-
-    rt_raster_set_offsets(ctx, newrast,
-            rt_raster_get_x_offset(ctx, raster),
-            rt_raster_get_y_offset(ctx, raster));
+    rt_raster_set_scale(newrast,
+            rt_raster_get_x_scale(raster),
+            rt_raster_get_y_scale(raster));
 
-    rt_raster_set_skews(ctx, newrast,
-            rt_raster_get_x_skew(ctx, raster),
-            rt_raster_get_y_skew(ctx, raster));
+    rt_raster_set_offsets(newrast,
+            rt_raster_get_x_offset(raster),
+            rt_raster_get_y_offset(raster));
 
-    rt_raster_set_srid(ctx, newrast, rt_raster_get_srid(ctx, raster));
+    rt_raster_set_skews(newrast,
+            rt_raster_get_x_skew(raster),
+            rt_raster_get_y_skew(raster));
 
+    rt_raster_set_srid(newrast, rt_raster_get_srid(raster));
 
 
     /**
      * If this new raster is empty (width = 0 OR height = 0) then there is
      * nothing to compute and we return it right now
      **/
-    if (rt_raster_is_empty(ctx, newrast)) 
+    if (rt_raster_is_empty(newrast))
     {
         elog(NOTICE, "Raster is empty. Returning an empty raster");
-        rt_raster_destroy(ctx, raster);
-        
-        pgraster = rt_raster_serialize(ctx, newrast);
+        rt_raster_destroy(raster);
+
+        pgraster = rt_raster_serialize(newrast);
         if (NULL == pgraster) {
-            rt_raster_destroy(ctx, newrast);
-            rt_context_destroy(ctx);
+
             elog(ERROR, "RASTER_mapAlgebra: Could not serialize raster. "
                     "Returning NULL");
             PG_RETURN_NULL();
         }
 
         SET_VARSIZE(pgraster, pgraster->size);
-        rt_raster_destroy(ctx, newrast);
+        rt_raster_destroy(newrast);
 
-        rt_context_destroy(ctx);
         PG_RETURN_POINTER(pgraster);
     }
 
-    POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebra: Getting raster band %d...", nband);
 
+    POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebra: Getting raster band %d...", nband);
 
     /**
      * Check if the raster has the required band. Otherwise, return a raster
      * without band
      **/
-    if (rt_raster_has_no_band(ctx, raster, nband)) 
-    {
+    if (rt_raster_has_no_band(raster, nband)) {
         elog(NOTICE, "Raster do not have the required band. Returning a raster "
                 "without a band");
-        rt_raster_destroy(ctx, raster);
+        rt_raster_destroy(raster);
 
-        pgraster = rt_raster_serialize(ctx, newrast);
-        if (NULL == pgraster) {
-            rt_raster_destroy(ctx, newrast);
-            rt_context_destroy(ctx);
+        pgraster = rt_raster_serialize(newrast);
+        if (NULL == pgraster) {        
             elog(ERROR, "RASTER_mapAlgebra: Could not serialize raster. "
                     "Returning NULL");
             PG_RETURN_NULL();
         }
 
         SET_VARSIZE(pgraster, pgraster->size);
-        rt_raster_destroy(ctx, newrast);
-        rt_context_destroy(ctx);
+        rt_raster_destroy(newrast);
         
         PG_RETURN_POINTER(pgraster);
     }
 
     /* Get the raster band */
-    band = rt_raster_get_band(ctx, raster, nband - 1);
-    if ( NULL == band )
-    {
+    band = rt_raster_get_band(raster, nband - 1);
+    if ( NULL == band ) {
         elog(NOTICE, "Could not get the required band. Returning a raster "
                 "without a band");
-        rt_raster_destroy(ctx, raster);
+        rt_raster_destroy(raster);
 
-        pgraster = rt_raster_serialize(ctx, newrast);
+        pgraster = rt_raster_serialize(newrast);
         if (NULL == pgraster) {
-            rt_raster_destroy(ctx,newrast);
-            rt_context_destroy(ctx);
             elog(ERROR, "RASTER_mapAlgebra: Could not serialize raster. "
                     "Returning NULL");
             PG_RETURN_NULL();
         }
 
         SET_VARSIZE(pgraster, pgraster->size);
-        rt_raster_destroy(ctx, newrast);
-        rt_context_destroy(ctx);
+        
+        rt_raster_destroy(newrast);
         
         PG_RETURN_POINTER(pgraster); 
     }
-
-    /* Check for nodata value */
-    if (rt_band_get_hasnodata_flag(ctx, band)) {
-        newnodatavalue = rt_band_get_nodata(ctx, band);
+    
+     /*
+     * Get NODATA value
+     */
+    POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebra: Getting NODATA value for band...");
+    
+    if (rt_band_get_hasnodata_flag(band)) {
+        newnodatavalue = rt_band_get_nodata(band);
     }
 
     else {
-        newnodatavalue = rt_band_get_min_value(ctx, band);
+        newnodatavalue = rt_band_get_min_value(band);
     }
 
-    POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebra: nodata value for band: %f...",
-            newnodatavalue);
+    POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebra: NODATA value for band: = %f",
+        newnodatavalue);
+
     /**
      * We set the initial value of the future band to nodata value. If nodata
      * value is null, then the raster will be initialized to
@@ -2210,43 +2193,34 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
      **/
     newinitialvalue = newnodatavalue;
 
-    POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebra: Setting pixeltype...");
-
     /**
      * Set the new pixeltype
      **/
-    if (PG_ARGISNULL(4))
-    {
-        newpixeltype = rt_band_get_pixtype(ctx, band);
+    POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebra: Setting pixeltype...");
+
+    if (PG_ARGISNULL(4)) {
+        newpixeltype = rt_band_get_pixtype(band);
     }
 
-    else
-    {
+    else {
         strFromText = text_to_cstring(PG_GETARG_TEXT_P(4));
-        newpixeltype = rt_pixtype_index_from_name(ctx,strFromText);
+        newpixeltype = rt_pixtype_index_from_name(strFromText);
         lwfree(strFromText);
         if (newpixeltype == PT_END)
-            newpixeltype = rt_band_get_pixtype(ctx, band);
+            newpixeltype = rt_band_get_pixtype(band);
     }
 
-    if (newpixeltype == PT_END)
-    {
+    if (newpixeltype == PT_END) {
         elog(ERROR, "RASTER_mapAlgebra: Invalid pixeltype. Returning NULL");
-        
-        rt_raster_destroy(ctx, raster);
-        rt_raster_destroy(ctx, newrast);
-        rt_context_destroy(ctx);
-        
         PG_RETURN_NULL();
     }
 
-    /* Connect with SPI manager */
-    SPI_connect();
+    POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebra: Pixeltype set to %s",
+        rt_pixtype_name(newpixeltype));
 
-    /* Construct expression for raster values */
 
-    if (!PG_ARGISNULL(2))
-    {
+       /* Construct expression for raster values */
+    if (!PG_ARGISNULL(2)) {
         expression = text_to_cstring(PG_GETARG_TEXT_P(2));
         len = strlen("SELECT ") + strlen(expression);
         initexpr = (char *)palloc(len + 1);
@@ -2258,15 +2232,19 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
 
         POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebra: Expression is %s", initexpr);
 
+        /* We don't need this memory */
+        //lwfree(expression);
+        //expression = NULL;
     }
 
+   
+
     /**
      * Optimization: If a nodatavalueexpr is provided, recompute the initial
      * value. Then, we can initialize the raster with this value and skip the
      * computation of nodata values one by one in the main computing loop
      **/
-    if (!PG_ARGISNULL(3))
-    {
+    if (!PG_ARGISNULL(3)) {
         nodatavalueexpr = text_to_cstring(PG_GETARG_TEXT_P(3));
         len = strlen("SELECT ") + strlen(nodatavalueexpr);
         initndvexpr = (char *)palloc(len + 1);
@@ -2274,39 +2252,47 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
         strncpy(initndvexpr + strlen("SELECT "), strtoupper(nodatavalueexpr),
                 strlen(nodatavalueexpr));
         initndvexpr[len] = '\0';
-        sprintf(strnewnodatavalue, "%f", newnodatavalue);
 
-        newexpr = replace(initndvexpr, "RAST", strnewnodatavalue, &count);
+        //lwfree(nodatavalueexpr);
+        //nodatavalueexpr = NULL;
 
-        POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebra: initndvexpr = %s", initndvexpr);
+        /* Replace RAST, if present, for NODATA value, to eval the expression */
+        if (strstr(initndvexpr, "RAST")) {
+            sprintf(strnewnodatavalue, "%f", newnodatavalue);
 
-        /**
-         * Execute the expression for nodata value and store the result as new
-         * initial value
-         **/
-        ret = SPI_execute(newexpr, FALSE, 0);
+            newexpr = replace(initndvexpr, "RAST", strnewnodatavalue, &count);
+            freemem = TRUE;
+        }
 
-        if (ret != SPI_OK_SELECT || SPI_tuptable == NULL || SPI_processed != 1)
-        {
-            elog(ERROR, "RASTER_mapAlgebra: Invalid construction for nodata "
-                    "expression. Aborting");
+        /* If newexpr reduces to a constant, simply eval it */
+        else {
+            newexpr = initndvexpr;
+        }
+            
+        POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebra: initndvexpr = %s", newexpr);
 
-            /* Free memory allocated out of the current context */
-            if (expression)
-                lwfree(expression);
-            if (nodatavalueexpr)
-                lwfree(nodatavalueexpr);
+        /* Eval the NODATA expression to get new NODATA. Connect with SPI manager
+        * NOTE: This creates a NEW memory context and makes it current.
+        */
+        SPI_connect();
 
-            rt_raster_destroy(ctx, raster);
-            rt_raster_destroy(ctx, newrast);
+        /**
+        * Execute the expression for nodata value and store the result as new
+        * initial value
+        **/
+        ret = SPI_execute(newexpr, FALSE, 0);
 
-            rt_context_destroy(ctx);
+        if (ret != SPI_OK_SELECT || SPI_tuptable == NULL ||
+                SPI_processed != 1) {
+            elog(ERROR, "RASTER_mapAlgebra: Invalid construction for nodata "
+                "expression. Aborting");
 
             if (SPI_tuptable)
                 SPI_freetuptable(tuptable);
-            
-            /* TODO: make postgres to crash when dealing with BIG rasters */
-            SPI_finish();
+
+            /* Disconnect from SPI manager */
+            SPI_finish();        
+
             PG_RETURN_NULL();
         }
 
@@ -2318,33 +2304,37 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
 
         SPI_freetuptable(tuptable);
 
-        POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebra: new initial value = %f",
-                newinitialvalue);
+        /* Close the connection to SPI manager.
+         * NOTE: This restores the previous memory context
+         */
+        SPI_finish();
 
-    }
+        if (freemem)
+            pfree(newexpr);
 
+        POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebra: new initial value = %f",
+            newinitialvalue);
+        
+    }
 
+   
+    
     /**
      * Optimization: If the raster is only filled with nodata values return
      * right now a raster filled with the nodatavalueexpr
      * TODO: Call rt_band_check_isnodata instead?
      **/
-    if (rt_band_get_isnodata_flag(ctx, band))
-    {
+    if (rt_band_get_isnodata_flag(band)) {
 
         POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebra: Band is a nodata band, returning "
                 "a raster filled with nodata");
 
-        ret = rt_raster_generate_new_band(ctx, newrast, newpixeltype,
+        ret = rt_raster_generate_new_band(newrast, newpixeltype,
                 newinitialvalue, TRUE, newnodatavalue, 0);
 
         /* Serialize created raster */
-        pgraster = rt_raster_serialize(ctx, newrast);
-        if (NULL == pgraster)
-        {
-            rt_raster_destroy(ctx, newrast);
-            rt_raster_destroy(ctx, raster);
-            rt_context_destroy(ctx);
+        pgraster = rt_raster_serialize(newrast);
+        if (NULL == pgraster) {
             elog(ERROR, "RASTER_mapAlgebra: Could not serialize raster. "
                     "Returning NULL");
             PG_RETURN_NULL();
@@ -2352,17 +2342,13 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
 
         SET_VARSIZE(pgraster, pgraster->size);
 
-        /* Free memory allocated out of the current context */
-        if (expression)
-            lwfree(expression);
-        if (nodatavalueexpr)
-            lwfree(nodatavalueexpr);
-        rt_raster_destroy(ctx, raster);
-        rt_raster_destroy(ctx, newrast);
-
-        /* Disconnect function from SPI manager */
-        /* TODO: make postgres to crash when dealing with BIG rasters */
-        SPI_finish();
+        /* Free memory */
+        if (initndvexpr)
+            pfree(initndvexpr);
+        if (initexpr)
+            pfree(initexpr);
+        rt_raster_destroy(raster);
+        rt_raster_destroy(newrast);
 
         PG_RETURN_POINTER(pgraster);
     }
@@ -2374,48 +2360,36 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
      * raster
      **/
     if (initexpr != NULL && !strcmp(initexpr, "SELECT RAST") &&
-            (nodatavalueexpr == NULL || !strcmp(initndvexpr, "SELECT RAST"))) {
+                   (nodatavalueexpr  == NULL || !strcmp(initndvexpr, "SELECT RAST"))) {
+            //(initndvexpr == NULL || !strcmp(initndvexpr, "SELECT RAST"))) {
 
-        POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebra: Expression resumes to RAST. Returning "
-               "raster with band %d from original raster", nband);
+        POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebra: Expression resumes to RAST. "
+                "Returning raster with band %d from original raster", nband);
 
         POSTGIS_RT_DEBUGF(4, "RASTER_mapAlgebra: New raster has %d bands",
-                rt_raster_get_num_bands(ctx, newrast));
+                rt_raster_get_num_bands(newrast));
 
-        rt_raster_copy_band(ctx, raster, newrast, nband - 1, 0);
+        rt_raster_copy_band(raster, newrast, nband - 1, 0);
 
         POSTGIS_RT_DEBUGF(4, "RASTER_mapAlgebra: New raster now has %d bands",
-                rt_raster_get_num_bands(ctx, newrast));
+                rt_raster_get_num_bands(newrast));
 
         /* Serialize created raster */
-        pgraster = rt_raster_serialize(ctx, newrast);
-        if (NULL == pgraster)
-        {
-            /* Free memory allocated out of the current context */
-            if (expression)
-                lwfree(expression);
-            if (nodatavalueexpr)
-                lwfree(nodatavalueexpr);
-            rt_raster_destroy(ctx, raster);
-            rt_raster_destroy(ctx, newrast);
-            rt_context_destroy(ctx);
-            
+        pgraster = rt_raster_serialize(newrast);
+        if (NULL == pgraster) {
+            elog(ERROR, "RASTER_mapAlgebra: Could not serialize raster. "
+                    "Returning NULL");            
             PG_RETURN_NULL();
         }
 
         SET_VARSIZE(pgraster, pgraster->size);
 
-        /* Free memory allocated out of the current context */
-        if (expression)
-            lwfree(expression);
-        if (nodatavalueexpr)
-            lwfree(nodatavalueexpr);
-        rt_raster_destroy(ctx, raster);
-        rt_raster_destroy(ctx, newrast);
-        rt_context_destroy(ctx);
-
-        /* Disconnect function from SPI manager */        
-        SPI_finish();
+        if (initndvexpr)
+            pfree(initndvexpr);
+        if (initexpr)
+            pfree(initexpr);
+        rt_raster_destroy(raster);
+        rt_raster_destroy(newrast);
 
         PG_RETURN_POINTER(pgraster);
     }
@@ -2424,29 +2398,21 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
      * Optimization: If expression resume to a constant (it does not contain
      * RAST)
      **/
-    if (initexpr != NULL && strstr(initexpr, "RAST") == NULL)
-    {
+    if (initexpr != NULL && strstr(initexpr, "RAST") == NULL) {
+
+        SPI_connect();
+
         /* Execute the expresion into newval */
         ret = SPI_execute(initexpr, FALSE, 0);
 
-        if (ret != SPI_OK_SELECT || SPI_tuptable == NULL || SPI_processed != 1)
-        {
+        if (ret != SPI_OK_SELECT || SPI_tuptable == NULL || SPI_processed != 1) {
             elog(ERROR, "RASTER_mapAlgebra: Invalid construction for expression."
                     " Aborting");
 
             /* Free memory allocated out of the current context */
-            if (expression)
-                lwfree(expression);
-            if (nodatavalueexpr)
-                lwfree(nodatavalueexpr);
-            rt_raster_destroy(ctx, raster);
-            rt_raster_destroy(ctx,newrast);
-            rt_context_destroy(ctx);
-
             if (SPI_tuptable)
                 SPI_freetuptable(tuptable);
-
-            /* TODO: make postgres to crash when dealing with BIG rasters */
+            
             SPI_finish();
             PG_RETURN_NULL();
         }
@@ -2462,12 +2428,15 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
         POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebra: New raster value = %f",
                 newval);
 
+        SPI_finish();
+
         skipcomputation = 1;
 
         /**
          * Compute the new value, set it and we will return after creating the
          * new raster
          **/
+        //if (initndvexpr == NULL) {
         if (nodatavalueexpr == NULL) {
             newinitialvalue = newval;
             skipcomputation = 2;
@@ -2483,94 +2452,74 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
      * Create the raster receiving all the computed values. Initialize it to the
      * new initial value
      **/
-    ret = rt_raster_generate_new_band(ctx, newrast, newpixeltype,
+    ret = rt_raster_generate_new_band(newrast, newpixeltype,
             newinitialvalue, TRUE, newnodatavalue, 0);
 
     /**
      * Optimization: If expression is NULL, or all the pixels could be set in
      * one step, return the initialized raster now
      **/
+    //if (initexpr == NULL || skipcomputation == 2) {
     if (expression == NULL || skipcomputation == 2) {
+
         /* Serialize created raster */
-        pgraster = rt_raster_serialize(ctx, newrast);
-        if (NULL == pgraster)
-        {
-            /* Free memory allocated out of the current context */
-            if (expression)
-                lwfree(expression);
-            if (nodatavalueexpr)
-                lwfree(nodatavalueexpr);
-            rt_raster_destroy(ctx, raster);
-            rt_raster_destroy(ctx, newrast);
-            rt_context_destroy(ctx);
+        pgraster = rt_raster_serialize(newrast);
+        if (NULL == pgraster) {
+            elog(ERROR, "RASTER_mapAlgebra: Could not serialize raster. "
+                    "Returning NULL");
             
             PG_RETURN_NULL();
         }
 
         SET_VARSIZE(pgraster, pgraster->size);
 
-        /* Free memory allocated out of the current context */
-        if (expression)
-            lwfree(expression);
-        if (nodatavalueexpr)
-            lwfree(nodatavalueexpr);
-        rt_raster_destroy(ctx, raster);
-        rt_context_destroy(ctx);
-
-        /* Disconnect function from SPI manager */
-        /* TODO: make postgres to crash when dealing with BIG rasters */
-        SPI_finish();
+        /* Free memory */
+        if (initndvexpr)
+            pfree(initndvexpr);
+        if (initexpr)
+            pfree(initexpr);
+        rt_raster_destroy(raster);
+        rt_raster_destroy(newrast);
 
         PG_RETURN_POINTER(pgraster);
     }
 
+    RASTER_DEBUG(3, "RASTER_mapAlgebra: Creating new raster band...");
+
     /* Get the new raster band */
-    newband = rt_raster_get_band(ctx, newrast, 0);
-    if ( ! newband )
-    {
+    newband = rt_raster_get_band(newrast, 0);
+    if ( NULL == newband ) {
         elog(NOTICE, "Could not modify band for new raster. Returning new "
                 "raster with the original band");
 
         /* Serialize created raster */
-        pgraster = rt_raster_serialize(ctx, newrast);
-        if (!pgraster) 
-        {
-            /* Free memory allocated out of the current context */
-            if (expression)
-                lwfree(expression);
-            if (nodatavalueexpr)
-                lwfree(nodatavalueexpr);
-            rt_raster_destroy(ctx, raster);
-            rt_raster_destroy(ctx, newrast);
-            rt_context_destroy(ctx);
+        pgraster = rt_raster_serialize(newrast);
+        if (NULL == pgraster) {
+            elog(ERROR, "RASTER_mapAlgebra: Could not serialize raster. "
+                    "Returning NULL");            
             
             PG_RETURN_NULL();
         }
 
         SET_VARSIZE(pgraster, pgraster->size);
 
-        /* Free memory allocated out of the current context */
-        if (expression)
-            lwfree(expression);
-        if (nodatavalueexpr)
-            lwfree(nodatavalueexpr);
-        rt_raster_destroy(ctx, raster);
-        rt_raster_destroy(ctx, newrast);
-        rt_context_destroy(ctx);
-
-        /* Disconnect function from SPI manager */
-        SPI_finish();
+        if (initndvexpr)
+            pfree(initndvexpr);
+        if (initexpr)
+            pfree(initexpr);
+        rt_raster_destroy(raster);
+        rt_raster_destroy(newrast);
+
 
         PG_RETURN_POINTER(pgraster);
     }
 
-
     POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebra: Main computing loop (%d x %d)",
             width, height);
 
     for (x = 0; x < width; x++) {
         for(y = 0; y < height; y++) {
-            ret = rt_band_get_pixel(ctx, band, x, y, &r);
+            ret = rt_band_get_pixel(band, x, y, &r);
 
             /**
              * We compute a value only for the withdata value pixel since the
@@ -2587,27 +2536,19 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
                                 "r = %s, newexpr = %s",
                                 x, y, strnewval, newexpr);
 
+                        SPI_connect();
+
                         ret = SPI_execute(newexpr, FALSE, 0);
 
                         if (ret != SPI_OK_SELECT || SPI_tuptable == NULL || 
-                                SPI_processed != 1)
-                        {
+                                SPI_processed != 1) {
                             elog(ERROR, "RASTER_mapAlgebra: Invalid construction"
                                     " for expression. Aborting");
 
-                            /* Free memory allocated out of the current context */
-                            if (expression)
-                                lwfree(expression);
-                            if (nodatavalueexpr)
-                                lwfree(nodatavalueexpr);
-                            rt_raster_destroy(ctx, raster);
-                            rt_raster_destroy(ctx, newrast);
-                            rt_context_destroy(ctx);
-
+                            pfree(newexpr);
                             if (SPI_tuptable)
                                 SPI_freetuptable(tuptable);
-
-                            /* TODO: make postgres to crash when dealing with BIG rasters */
+                            
                             SPI_finish();
                             PG_RETURN_NULL();
                         }
@@ -2619,6 +2560,10 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
                         newval = atof(SPI_getvalue(tuple, tupdesc, 1));
 
                         SPI_freetuptable(tuptable);
+
+                        SPI_finish();
+
+                        pfree(newexpr);
                     }
 
                     else
@@ -2629,7 +2574,7 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
                 }
 
 
-                rt_band_set_pixel(ctx, newband, x, y, newval);
+                rt_band_set_pixel(newband, x, y, newval);
             }
 
         }
@@ -2639,18 +2584,17 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
 
     POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebra: raster modified, serializing it.");
     /* Serialize created raster */
-    pgraster = rt_raster_serialize(ctx, newrast);
-    if (NULL == pgraster)
-    {
+
+    pgraster = rt_raster_serialize(newrast);
+    if (NULL == pgraster) {
         /* Free memory allocated out of the current context */
-        if (expression)
-            lwfree(expression);
-        if (nodatavalueexpr)
-            lwfree(nodatavalueexpr);
-        rt_raster_destroy(ctx, raster);
-        rt_raster_destroy(ctx, newrast);
-        rt_context_destroy(ctx);
-        
+        if (initndvexpr)
+            pfree(initndvexpr);
+        if (initexpr)
+            pfree(initexpr);
+        rt_raster_destroy(raster);
+        rt_raster_destroy(newrast);
+
         PG_RETURN_NULL();
     }
 
@@ -2658,18 +2602,14 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
 
     POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebra: raster serialized");
 
-    /* Free memory allocated out of the current context */
-    if (expression)
-        lwfree(expression);
-    if (nodatavalueexpr)
-        lwfree(nodatavalueexpr);
-    rt_raster_destroy(ctx, raster);
-    rt_raster_destroy(ctx, newrast);
-    rt_context_destroy(ctx);
-
-    /* Disconnect function from SPI manager */
-    SPI_finish();
-    
+    /* Free memory */
+    if (initndvexpr)
+        pfree(initndvexpr);
+    if (initexpr)
+        pfree(initexpr);
+
+    rt_raster_destroy(raster);
+    rt_raster_destroy(newrast);
 
     POSTGIS_RT_DEBUG(4, "RASTER_mapAlgebra: returning raster");
 
@@ -2686,6 +2626,8 @@ rt_pg_alloc(size_t size)
 {
     void * result;
 
+    POSTGIS_RT_DEBUGF(5, "rt_pgalloc(%ld) called", size);
+
     result = palloc(size);
 
     return result;
@@ -2696,7 +2638,13 @@ rt_pg_realloc(void *mem, size_t size)
 {
     void * result;
 
-    result = repalloc(mem, size);
+    POSTGIS_RT_DEBUGF(5, "rt_pg_realloc(%ld) called", size);
+
+    if (mem)
+        result = repalloc(mem, size);
+
+    else
+        result = palloc(size);
 
     return result;
 }
@@ -2704,6 +2652,7 @@ rt_pg_realloc(void *mem, size_t size)
 static void
 rt_pg_free(void *ptr)
 {
+    POSTGIS_RT_DEBUG(5, "rt_pfree called");
     pfree(ptr);
 }
 
@@ -2747,11 +2696,16 @@ lwgeom_init_allocators(void)
     lwalloc_var = rt_pg_alloc;
     lwrealloc_var = rt_pg_realloc;
     lwfree_var = rt_pg_free;
-
     lwerror_var = rt_pg_error;
     lwnotice_var = rt_pg_notice;
 }
 
-/* ---------------------------------------------------------------- */
-/*  Memory allocation / error reporting hooks                       */
-/* ---------------------------------------------------------------- */
+
+void
+rt_init_allocators(void)
+{
+    /* raster callback - install raster handlers */
+    rt_set_handlers(rt_pg_alloc, rt_pg_realloc, rt_pg_free, rt_pg_error,
+            rt_pg_notice, rt_pg_notice);
+}
+
index ffedd3e3faf63a103aae664630af3958a64ea854..26bbfe3abd6b8dcd67138e3379aafb0bca808a64 100644 (file)
@@ -7,48 +7,48 @@
 #include "rt_api.h"
 #include "check.h"
 
-static rt_band addBand(rt_context ctx, rt_raster raster, rt_pixtype pixtype, int hasnodata, double nodataval);
-static void deepRelease(rt_context ctx, rt_raster raster);
-static void testBand1BB(rt_context ctx, rt_band band);
-static rt_raster fillRasterToPolygonize(rt_context ctx, int hasnodata, double nodatavalue);
+static rt_band addBand(rt_raster raster, rt_pixtype pixtype, int hasnodata, double nodataval);
+static void deepRelease(rt_raster raster);
+static void testBand1BB(rt_band band);
+static rt_raster fillRasterToPolygonize(int hasnodata, double nodatavalue);
 
 static rt_band
-addBand(rt_context ctx, rt_raster raster, rt_pixtype pixtype, int hasnodata, double nodataval)
+addBand(rt_raster raster, rt_pixtype pixtype, int hasnodata, double nodataval)
 {
     void* mem;
     int32_t bandNum;
     size_t datasize;
-    uint16_t width = rt_raster_get_width(ctx, raster);
-    uint16_t height = rt_raster_get_height(ctx, raster);
+    uint16_t width = rt_raster_get_width(raster);
+    uint16_t height = rt_raster_get_height(raster);
 
-    datasize = rt_pixtype_size(ctx, pixtype)*width*height;
+    datasize = rt_pixtype_size(pixtype)*width*height;
     mem = malloc(datasize);
 
-    rt_band band = rt_band_new_inline(ctx, width, height,
+    rt_band band = rt_band_new_inline(width, height,
                                       pixtype, hasnodata, nodataval, mem);
     assert(band);
-    bandNum = rt_raster_add_band(ctx, raster, band, 100);
+    bandNum = rt_raster_add_band(raster, band, 100);
     assert(bandNum>=0);
     return band;
 }
 
 static void
-deepRelease(rt_context ctx, rt_raster raster)
+deepRelease(rt_raster raster)
 {
-    uint16_t i, nbands=rt_raster_get_num_bands(ctx, raster);
+    uint16_t i, nbands=rt_raster_get_num_bands(raster);
     for (i=0; i<nbands; ++i)
     {
-        rt_band band = rt_raster_get_band(ctx, raster, i);
-        void* mem = rt_band_get_data(ctx, band);
+        rt_band band = rt_raster_get_band(raster, i);
+        void* mem = rt_band_get_data(band);
         if ( mem ) free(mem);
-        rt_band_destroy(ctx, band);
+        rt_band_destroy(band);
     }
-    rt_raster_destroy(ctx, raster);
+    rt_raster_destroy(raster);
 }
 
 
 static rt_raster
-fillRasterToPolygonize(rt_context ctx, int hasnodata, double nodatavalue)
+fillRasterToPolygonize(int hasnodata, double nodatavalue)
 {
     /* Create raster */
 
@@ -68,138 +68,138 @@ fillRasterToPolygonize(rt_context ctx, int hasnodata, double nodatavalue)
     uint16_t height = 5;
     */
 
-    rt_raster raster = rt_raster_new(ctx, width, height);
+    rt_raster raster = rt_raster_new(width, height);
     
     /* Fill raster. Option 1: simple raster */
     /*
-    rt_band band = addBand(ctx, raster, PT_32BSI, 0, 0);
+    rt_band band = addBand(raster, PT_32BSI, 0, 0);
 
-    rt_band_set_pixel(ctx, band, 0, 0, 1);
-    rt_band_set_pixel(ctx, band, 0, 1, 1);
-    rt_band_set_pixel(ctx, band, 1, 0, 1);
-    rt_band_set_pixel(ctx, band, 1, 1, 1);
+    rt_band_set_pixel(band, 0, 0, 1);
+    rt_band_set_pixel(band, 0, 1, 1);
+    rt_band_set_pixel(band, 1, 0, 1);
+    rt_band_set_pixel(band, 1, 1, 1);
     */
     
 
     /* Fill raster. Option 2: 9x9, 1 band */    
-    rt_band band = addBand(ctx, raster, PT_32BUI, hasnodata, nodatavalue);
+    rt_band band = addBand(raster, PT_32BUI, hasnodata, nodatavalue);
     
     {
         int x, y;
-        for (x = 0; x < rt_band_get_width(ctx, band); ++x)
-             for (y = 0; y < rt_band_get_height(ctx, band); ++y)
-                       rt_band_set_pixel(ctx, band, x, y, 0);
+        for (x = 0; x < rt_band_get_width(band); ++x)
+             for (y = 0; y < rt_band_get_height(band); ++y)
+                       rt_band_set_pixel(band, x, y, 0);
        }
     /**/
-    rt_band_set_pixel(ctx, band, 3, 1, 1);
-    rt_band_set_pixel(ctx, band, 4, 1, 1);
-    rt_band_set_pixel(ctx, band, 5, 1, 2);
-    rt_band_set_pixel(ctx, band, 2, 2, 1);
-    rt_band_set_pixel(ctx, band, 3, 2, 1);
-    rt_band_set_pixel(ctx, band, 4, 2, 1);
-    rt_band_set_pixel(ctx, band, 5, 2, 2);
-    rt_band_set_pixel(ctx, band, 6, 2, 2);
-    rt_band_set_pixel(ctx, band, 1, 3, 1);
-    rt_band_set_pixel(ctx, band, 2, 3, 1);
-    rt_band_set_pixel(ctx, band, 6, 3, 2);
-    rt_band_set_pixel(ctx, band, 7, 3, 2);
-    rt_band_set_pixel(ctx, band, 1, 4, 1);
-    rt_band_set_pixel(ctx, band, 2, 4, 1);
-    rt_band_set_pixel(ctx, band, 6, 4, 2);
-    rt_band_set_pixel(ctx, band, 7, 4, 2);
-    rt_band_set_pixel(ctx, band, 1, 5, 1);
-    rt_band_set_pixel(ctx, band, 2, 5, 1);
-    rt_band_set_pixel(ctx, band, 6, 5, 2);
-    rt_band_set_pixel(ctx, band, 7, 5, 2);
-    rt_band_set_pixel(ctx, band, 2, 6, 1);
-    rt_band_set_pixel(ctx, band, 3, 6, 1);
-    rt_band_set_pixel(ctx, band, 4, 6, 1);
-    rt_band_set_pixel(ctx, band, 5, 6, 2);
-    rt_band_set_pixel(ctx, band, 6, 6, 2);
-    rt_band_set_pixel(ctx, band, 3, 7, 1);
-    rt_band_set_pixel(ctx, band, 4, 7, 1);
-    rt_band_set_pixel(ctx, band, 5, 7, 2);
+    rt_band_set_pixel(band, 3, 1, 1);
+    rt_band_set_pixel(band, 4, 1, 1);
+    rt_band_set_pixel(band, 5, 1, 2);
+    rt_band_set_pixel(band, 2, 2, 1);
+    rt_band_set_pixel(band, 3, 2, 1);
+    rt_band_set_pixel(band, 4, 2, 1);
+    rt_band_set_pixel(band, 5, 2, 2);
+    rt_band_set_pixel(band, 6, 2, 2);
+    rt_band_set_pixel(band, 1, 3, 1);
+    rt_band_set_pixel(band, 2, 3, 1);
+    rt_band_set_pixel(band, 6, 3, 2);
+    rt_band_set_pixel(band, 7, 3, 2);
+    rt_band_set_pixel(band, 1, 4, 1);
+    rt_band_set_pixel(band, 2, 4, 1);
+    rt_band_set_pixel(band, 6, 4, 2);
+    rt_band_set_pixel(band, 7, 4, 2);
+    rt_band_set_pixel(band, 1, 5, 1);
+    rt_band_set_pixel(band, 2, 5, 1);
+    rt_band_set_pixel(band, 6, 5, 2);
+    rt_band_set_pixel(band, 7, 5, 2);
+    rt_band_set_pixel(band, 2, 6, 1);
+    rt_band_set_pixel(band, 3, 6, 1);
+    rt_band_set_pixel(band, 4, 6, 1);
+    rt_band_set_pixel(band, 5, 6, 2);
+    rt_band_set_pixel(band, 6, 6, 2);
+    rt_band_set_pixel(band, 3, 7, 1);
+    rt_band_set_pixel(band, 4, 7, 1);
+    rt_band_set_pixel(band, 5, 7, 2);
     
 
 
     /* Fill raster. Option 3: 5x5, 1 band */
     /*
-    rt_band band = addBand(ctx, raster, PT_8BUI, 1, 255);
+    rt_band band = addBand(raster, PT_8BUI, 1, 255);
     
-    rt_band_set_pixel(ctx, band, 0, 0, 253);
-    rt_band_set_pixel(ctx, band, 1, 0, 254);
-    rt_band_set_pixel(ctx, band, 2, 0, 253);
-    rt_band_set_pixel(ctx, band, 3, 0, 254);
-    rt_band_set_pixel(ctx, band, 4, 0, 254);
-    rt_band_set_pixel(ctx, band, 0, 1, 253);
-    rt_band_set_pixel(ctx, band, 1, 1, 254);
-    rt_band_set_pixel(ctx, band, 2, 1, 254);
-    rt_band_set_pixel(ctx, band, 3, 1, 253);
-    rt_band_set_pixel(ctx, band, 4, 1, 249);
-    rt_band_set_pixel(ctx, band, 0, 2, 250);
-    rt_band_set_pixel(ctx, band, 1, 2, 254);
-    rt_band_set_pixel(ctx, band, 2, 2, 254);
-    rt_band_set_pixel(ctx, band, 3, 2, 252);
-    rt_band_set_pixel(ctx, band, 4, 2, 249);
-    rt_band_set_pixel(ctx, band, 0, 3, 251);
-    rt_band_set_pixel(ctx, band, 1, 3, 253);
-    rt_band_set_pixel(ctx, band, 2, 3, 254);
-    rt_band_set_pixel(ctx, band, 3, 3, 254);
-    rt_band_set_pixel(ctx, band, 4, 3, 253);
-    rt_band_set_pixel(ctx, band, 0, 4, 252);
-    rt_band_set_pixel(ctx, band, 1, 4, 250);
-    rt_band_set_pixel(ctx, band, 2, 4, 254);
-    rt_band_set_pixel(ctx, band, 3, 4, 254);
-    rt_band_set_pixel(ctx, band, 4, 4, 254);
+    rt_band_set_pixel(band, 0, 0, 253);
+    rt_band_set_pixel(band, 1, 0, 254);
+    rt_band_set_pixel(band, 2, 0, 253);
+    rt_band_set_pixel(band, 3, 0, 254);
+    rt_band_set_pixel(band, 4, 0, 254);
+    rt_band_set_pixel(band, 0, 1, 253);
+    rt_band_set_pixel(band, 1, 1, 254);
+    rt_band_set_pixel(band, 2, 1, 254);
+    rt_band_set_pixel(band, 3, 1, 253);
+    rt_band_set_pixel(band, 4, 1, 249);
+    rt_band_set_pixel(band, 0, 2, 250);
+    rt_band_set_pixel(band, 1, 2, 254);
+    rt_band_set_pixel(band, 2, 2, 254);
+    rt_band_set_pixel(band, 3, 2, 252);
+    rt_band_set_pixel(band, 4, 2, 249);
+    rt_band_set_pixel(band, 0, 3, 251);
+    rt_band_set_pixel(band, 1, 3, 253);
+    rt_band_set_pixel(band, 2, 3, 254);
+    rt_band_set_pixel(band, 3, 3, 254);
+    rt_band_set_pixel(band, 4, 3, 253);
+    rt_band_set_pixel(band, 0, 4, 252);
+    rt_band_set_pixel(band, 1, 4, 250);
+    rt_band_set_pixel(band, 2, 4, 254);
+    rt_band_set_pixel(band, 3, 4, 254);
+    rt_band_set_pixel(band, 4, 4, 254);
     */
      
-    rt_raster_add_band(ctx, raster, band, 100);
+    rt_raster_add_band(raster, band, 100);
     
     return raster;
 }
 
-static void testBand1BB(rt_context ctx, rt_band band)
+static void testBand1BB(rt_band band)
 {
     int failure;
     double val = 0;
 
-    failure = rt_band_set_nodata(ctx, band, 1);
+    failure = rt_band_set_nodata(band, 1);
     CHECK(!failure);
-    //printf("1BB nodata is %g\n", rt_band_get_nodata(ctx, band));
-    CHECK_EQUALS(rt_band_get_nodata(ctx, band), 1);
+    //printf("1BB nodata is %g\n", rt_band_get_nodata(band));
+    CHECK_EQUALS(rt_band_get_nodata(band), 1);
 
-    failure = rt_band_set_nodata(ctx, band, 0);
+    failure = rt_band_set_nodata(band, 0);
     CHECK(!failure);
-    //printf("1BB nodata is %g\n", rt_band_get_nodata(ctx, band));
-    CHECK_EQUALS(rt_band_get_nodata(ctx, band), 0);
+    //printf("1BB nodata is %g\n", rt_band_get_nodata(band));
+    CHECK_EQUALS(rt_band_get_nodata(band), 0);
 
-    failure = rt_band_set_nodata(ctx, band, 2);
+    failure = rt_band_set_nodata(band, 2);
     CHECK(failure); /* out of range value */
 
-    failure = rt_band_set_nodata(ctx, band, 3);
+    failure = rt_band_set_nodata(band, 3);
     CHECK(failure); /* out of range value */
 
-    failure = rt_band_set_pixel(ctx, band, 0, 0, 2);
+    failure = rt_band_set_pixel(band, 0, 0, 2);
     CHECK(failure); /* out of range value */
 
-    failure = rt_band_set_pixel(ctx, band, 0, 0, 3);
+    failure = rt_band_set_pixel(band, 0, 0, 3);
     CHECK(failure); /* out of range value */
 
     {
         int x, y;
-        for (x=0; x<rt_band_get_width(ctx, band); ++x)
+        for (x=0; x<rt_band_get_width(band); ++x)
         {
-            for (y=0; y<rt_band_get_height(ctx, band); ++y)
+            for (y=0; y<rt_band_get_height(band); ++y)
             {
-                failure = rt_band_set_pixel(ctx, band, x, y, 1);
+                failure = rt_band_set_pixel(band, x, y, 1);
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS(val, 1);
 
-                failure = rt_band_set_pixel(ctx, band, x, y, 0);
+                failure = rt_band_set_pixel(band, x, y, 0);
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS(val, 0);
 
@@ -209,65 +209,65 @@ static void testBand1BB(rt_context ctx, rt_band band)
 
 }
 
-static void testBand2BUI(rt_context ctx, rt_band band)
+static void testBand2BUI(rt_band band)
 {
     double val;
     int failure;
 
-    failure = rt_band_set_nodata(ctx, band, 1);
-    val = rt_band_get_nodata(ctx, band);
+    failure = rt_band_set_nodata(band, 1);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 1);
     CHECK(!failure);
 
-    failure = rt_band_set_nodata(ctx, band, 0);
-    val = rt_band_get_nodata(ctx, band);
+    failure = rt_band_set_nodata(band, 0);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 0);
     CHECK(!failure);
 
-    failure = rt_band_set_nodata(ctx, band, 2);
-    val = rt_band_get_nodata(ctx, band);
+    failure = rt_band_set_nodata(band, 2);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 2);
     CHECK(!failure);
 
-    failure = rt_band_set_nodata(ctx, band, 3);
-    val = rt_band_get_nodata(ctx, band);
+    failure = rt_band_set_nodata(band, 3);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 3);
     CHECK(!failure);
 
-    failure = rt_band_set_nodata(ctx, band, 4); /* invalid: out of range */
+    failure = rt_band_set_nodata(band, 4); /* invalid: out of range */
     CHECK(failure);
 
-    failure = rt_band_set_nodata(ctx, band, 5); /* invalid: out of range */
+    failure = rt_band_set_nodata(band, 5); /* invalid: out of range */
     CHECK(failure);
 
-    failure = rt_band_set_pixel(ctx, band, 0, 0, 4); /* out of range */
+    failure = rt_band_set_pixel(band, 0, 0, 4); /* out of range */
     CHECK(failure);
 
-    failure = rt_band_set_pixel(ctx, band, 0, 0, 5); /* out of range */
+    failure = rt_band_set_pixel(band, 0, 0, 5); /* out of range */
     CHECK(failure);
 
 
     {
         int x, y;
-        for (x=0; x<rt_band_get_width(ctx, band); ++x)
+        for (x=0; x<rt_band_get_width(band); ++x)
         {
-            for (y=0; y<rt_band_get_height(ctx, band); ++y)
+            for (y=0; y<rt_band_get_height(band); ++y)
             {
-                failure = rt_band_set_pixel(ctx, band, x, y, 1);
+                failure = rt_band_set_pixel(band, x, y, 1);
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS(val, 1);
 
-                failure = rt_band_set_pixel(ctx, band, x, y, 2);
+                failure = rt_band_set_pixel(band, x, y, 2);
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS(val, 2);
 
-                failure = rt_band_set_pixel(ctx, band, x, y, 3);
+                failure = rt_band_set_pixel(band, x, y, 3);
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS(val, 3);
             }
@@ -276,79 +276,79 @@ static void testBand2BUI(rt_context ctx, rt_band band)
 
 }
 
-static void testBand4BUI(rt_context ctx, rt_band band)
+static void testBand4BUI(rt_band band)
 {
     double val;
     int failure;
 
-    failure = rt_band_set_nodata(ctx, band, 1);
+    failure = rt_band_set_nodata(band, 1);
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 1);
 
-    failure = rt_band_set_nodata(ctx, band, 0);
-    val = rt_band_get_nodata(ctx, band);
+    failure = rt_band_set_nodata(band, 0);
+    val = rt_band_get_nodata(band);
     CHECK(!failure);
     CHECK_EQUALS(val, 0);
 
-    failure = rt_band_set_nodata(ctx, band, 2);
-    val = rt_band_get_nodata(ctx, band);
+    failure = rt_band_set_nodata(band, 2);
+    val = rt_band_get_nodata(band);
     CHECK(!failure);
     CHECK_EQUALS(val, 2);
 
-    failure = rt_band_set_nodata(ctx, band, 4); 
-    val = rt_band_get_nodata(ctx, band);
+    failure = rt_band_set_nodata(band, 4); 
+    val = rt_band_get_nodata(band);
     CHECK(!failure);
     CHECK_EQUALS(val, 4);
 
-    failure = rt_band_set_nodata(ctx, band, 8); 
-    val = rt_band_get_nodata(ctx, band);
+    failure = rt_band_set_nodata(band, 8); 
+    val = rt_band_get_nodata(band);
     CHECK(!failure);
     CHECK_EQUALS(val, 8);
 
-    failure = rt_band_set_nodata(ctx, band, 15); 
-    val = rt_band_get_nodata(ctx, band);
+    failure = rt_band_set_nodata(band, 15); 
+    val = rt_band_get_nodata(band);
     CHECK(!failure);
     CHECK_EQUALS(val, 15);
 
-    failure = rt_band_set_nodata(ctx, band, 16);  /* out of value range */
+    failure = rt_band_set_nodata(band, 16);  /* out of value range */
     CHECK(failure);
 
-    failure = rt_band_set_nodata(ctx, band, 17);  /* out of value range */
+    failure = rt_band_set_nodata(band, 17);  /* out of value range */
     CHECK(failure);
 
-    failure = rt_band_set_pixel(ctx, band, 0, 0, 35); /* out of value range */
+    failure = rt_band_set_pixel(band, 0, 0, 35); /* out of value range */
     CHECK(failure);
 
 
     {
         int x, y;
         
-        for (x=0; x<rt_band_get_width(ctx, band); ++x)
+        for (x=0; x<rt_band_get_width(band); ++x)
         {
-            for (y=0; y<rt_band_get_height(ctx, band); ++y)
+            for (y=0; y<rt_band_get_height(band); ++y)
             {
-                failure = rt_band_set_pixel(ctx, band, x, y, 1);
+                failure = rt_band_set_pixel(band, x, y, 1);
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS(val, 1);
 
-                failure = rt_band_set_pixel(ctx, band, x, y, 3);
+                failure = rt_band_set_pixel(band, x, y, 3);
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS(val, 3);
 
-                failure = rt_band_set_pixel(ctx, band, x, y, 7);
+                failure = rt_band_set_pixel(band, x, y, 7);
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS(val, 7);
 
-                failure = rt_band_set_pixel(ctx, band, x, y, 15);
+                failure = rt_band_set_pixel(band, x, y, 15);
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS(val, 15);
             }
@@ -357,78 +357,78 @@ static void testBand4BUI(rt_context ctx, rt_band band)
 
 }
 
-static void testBand8BUI(rt_context ctx, rt_band band)
+static void testBand8BUI(rt_band band)
 {
     double val;
     int failure; 
 
-    failure = rt_band_set_nodata(ctx, band, 1);
+    failure = rt_band_set_nodata(band, 1);
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 1);
 
-    failure = rt_band_set_nodata(ctx, band, 0);
+    failure = rt_band_set_nodata(band, 0);
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 0);
 
-    failure = rt_band_set_nodata(ctx, band, 2);
+    failure = rt_band_set_nodata(band, 2);
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 2);
 
-    failure = rt_band_set_nodata(ctx, band, 4); 
+    failure = rt_band_set_nodata(band, 4); 
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 4);
 
-    failure = rt_band_set_nodata(ctx, band, 8); 
+    failure = rt_band_set_nodata(band, 8); 
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 8);
 
-    failure = rt_band_set_nodata(ctx, band, 15); 
+    failure = rt_band_set_nodata(band, 15); 
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 15);
 
-    failure = rt_band_set_nodata(ctx, band, 31);  
+    failure = rt_band_set_nodata(band, 31);  
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 31);
 
-    failure = rt_band_set_nodata(ctx, band, 255);  
+    failure = rt_band_set_nodata(band, 255);  
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 255);
 
-    failure = rt_band_set_nodata(ctx, band, 256); /* out of value range */
+    failure = rt_band_set_nodata(band, 256); /* out of value range */
     CHECK(failure);
 
-    failure = rt_band_set_pixel(ctx, band, 0, 0, 256); /* out of value range */
+    failure = rt_band_set_pixel(band, 0, 0, 256); /* out of value range */
     CHECK(failure);
 
     {
         int x, y;
-        for (x=0; x<rt_band_get_width(ctx, band); ++x)
+        for (x=0; x<rt_band_get_width(band); ++x)
         {
-            for (y=0; y<rt_band_get_height(ctx, band); ++y)
+            for (y=0; y<rt_band_get_height(band); ++y)
             {
-                failure = rt_band_set_pixel(ctx, band, x, y, 31);  
+                failure = rt_band_set_pixel(band, x, y, 31);  
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS(val, 31);
 
-                failure = rt_band_set_pixel(ctx, band, x, y, 255);  
+                failure = rt_band_set_pixel(band, x, y, 255);  
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS(val, 255);
 
-                failure = rt_band_set_pixel(ctx, band, x, y, 1);
+                failure = rt_band_set_pixel(band, x, y, 1);
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS(val, 1);
             }
@@ -436,99 +436,99 @@ static void testBand8BUI(rt_context ctx, rt_band band)
     }
 }
 
-static void testBand8BSI(rt_context ctx, rt_band band)
+static void testBand8BSI(rt_band band)
 {
     double val;
     int failure;
 
-    failure = rt_band_set_nodata(ctx, band, 1);
+    failure = rt_band_set_nodata(band, 1);
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 1);
 
-    failure = rt_band_set_nodata(ctx, band, 0);
+    failure = rt_band_set_nodata(band, 0);
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 0);
 
-    failure = rt_band_set_nodata(ctx, band, 2);
+    failure = rt_band_set_nodata(band, 2);
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 2);
 
-    failure = rt_band_set_nodata(ctx, band, 4); 
+    failure = rt_band_set_nodata(band, 4); 
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 4);
 
-    failure = rt_band_set_nodata(ctx, band, 8); 
+    failure = rt_band_set_nodata(band, 8); 
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 8);
 
-    failure = rt_band_set_nodata(ctx, band, 15); 
-    val = rt_band_get_nodata(ctx, band);
+    failure = rt_band_set_nodata(band, 15); 
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 15);
 
-    failure = rt_band_set_nodata(ctx, band, 31);  
+    failure = rt_band_set_nodata(band, 31);  
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 31);
 
-    failure = rt_band_set_nodata(ctx, band, -127);  
+    failure = rt_band_set_nodata(band, -127);  
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, -127);
 
-    failure = rt_band_set_nodata(ctx, band, 127);  
+    failure = rt_band_set_nodata(band, 127);  
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 127);
 
     /* out of range (-127..127) */
-    failure = rt_band_set_nodata(ctx, band, -129);  
+    failure = rt_band_set_nodata(band, -129);  
     CHECK(failure);
 
     /* out of range (-127..127) */
-    failure = rt_band_set_nodata(ctx, band, 129);  
+    failure = rt_band_set_nodata(band, 129);  
     CHECK(failure);
 
     /* out of range (-127..127) */
-    failure = rt_band_set_pixel(ctx, band, 0, 0, -129);  
+    failure = rt_band_set_pixel(band, 0, 0, -129);  
     CHECK(failure);
 
     /* out of range (-127..127) */
-    failure = rt_band_set_pixel(ctx, band, 0, 0, 129);  
+    failure = rt_band_set_pixel(band, 0, 0, 129);  
     CHECK(failure);
 
 
     {
         int x, y;
-        for (x=0; x<rt_band_get_width(ctx, band); ++x)
+        for (x=0; x<rt_band_get_width(band); ++x)
         {
-            for (y=0; y<rt_band_get_height(ctx, band); ++y)
+            for (y=0; y<rt_band_get_height(band); ++y)
             {
-                failure = rt_band_set_pixel(ctx, band, x, y, 31);  
+                failure = rt_band_set_pixel(band, x, y, 31);  
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS(val, 31);
 
-                failure = rt_band_set_pixel(ctx, band, x, y, 1);
+                failure = rt_band_set_pixel(band, x, y, 1);
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS(val, 1);
 
-                failure = rt_band_set_pixel(ctx, band, x, y, -127);
+                failure = rt_band_set_pixel(band, x, y, -127);
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS(val, -127);
 
-                failure = rt_band_set_pixel(ctx, band, x, y, 127);
+                failure = rt_band_set_pixel(band, x, y, 127);
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS(val, 127);
 
@@ -537,63 +537,63 @@ static void testBand8BSI(rt_context ctx, rt_band band)
     }
 }
 
-static void testBand16BUI(rt_context ctx, rt_band band)
+static void testBand16BUI(rt_band band)
 {
     double val;
     int failure;
 
-    failure = rt_band_set_nodata(ctx, band, 1);
+    failure = rt_band_set_nodata(band, 1);
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 1);
 
-    failure = rt_band_set_nodata(ctx, band, 0);
+    failure = rt_band_set_nodata(band, 0);
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 0);
 
-    failure = rt_band_set_nodata(ctx, band, 31);  
+    failure = rt_band_set_nodata(band, 31);  
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 31);
 
-    failure = rt_band_set_nodata(ctx, band, 255);  
+    failure = rt_band_set_nodata(band, 255);  
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 255);
 
-    failure = rt_band_set_nodata(ctx, band, 65535);   
+    failure = rt_band_set_nodata(band, 65535);   
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     //printf("set 65535 on %s band gets %g back\n", pixtypeName, val);
     CHECK_EQUALS(val, 65535);
 
-    failure = rt_band_set_nodata(ctx, band, 65536); /* out of range */
+    failure = rt_band_set_nodata(band, 65536); /* out of range */
     CHECK(failure);
 
     /* out of value range */
-    failure = rt_band_set_pixel(ctx, band, 0, 0, 65536);
+    failure = rt_band_set_pixel(band, 0, 0, 65536);
     CHECK(failure);
 
     /* out of dimensions range */
-    failure = rt_band_set_pixel(ctx, band, rt_band_get_width(ctx, band), 0, 0);
+    failure = rt_band_set_pixel(band, rt_band_get_width(band), 0, 0);
     CHECK(failure);
 
     {
         int x, y;
-        for (x=0; x<rt_band_get_width(ctx, band); ++x)
+        for (x=0; x<rt_band_get_width(band); ++x)
         {
-            for (y=0; y<rt_band_get_height(ctx, band); ++y)
+            for (y=0; y<rt_band_get_height(band); ++y)
             {
-                failure = rt_band_set_pixel(ctx, band, x, y, 255);  
+                failure = rt_band_set_pixel(band, x, y, 255);  
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS(val, 255);
 
-                failure = rt_band_set_pixel(ctx, band, x, y, 65535);   
+                failure = rt_band_set_pixel(band, x, y, 65535);   
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS(val, 65535);
             }
@@ -601,84 +601,84 @@ static void testBand16BUI(rt_context ctx, rt_band band)
     }
 }
 
-static void testBand16BSI(rt_context ctx, rt_band band)
+static void testBand16BSI(rt_band band)
 {
     double val;
     int failure;
 
-    failure = rt_band_set_nodata(ctx, band, 1);
+    failure = rt_band_set_nodata(band, 1);
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 1);
 
-    failure = rt_band_set_nodata(ctx, band, 0);
+    failure = rt_band_set_nodata(band, 0);
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 0);
 
-    failure = rt_band_set_nodata(ctx, band, 31);  
+    failure = rt_band_set_nodata(band, 31);  
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 31);
 
-    failure = rt_band_set_nodata(ctx, band, 255);  
+    failure = rt_band_set_nodata(band, 255);  
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 255);
 
-    failure = rt_band_set_nodata(ctx, band, -32767);   
+    failure = rt_band_set_nodata(band, -32767);   
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     //printf("set 65535 on %s band gets %g back\n", pixtypeName, val);
     CHECK_EQUALS(val, -32767);
 
-    failure = rt_band_set_nodata(ctx, band, 32767);   
+    failure = rt_band_set_nodata(band, 32767);   
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     //printf("set 65535 on %s band gets %g back\n", pixtypeName, val);
     CHECK_EQUALS(val, 32767);
 
     /* out of range (-32767..32767) */
-    failure = rt_band_set_nodata(ctx, band, -32769);
+    failure = rt_band_set_nodata(band, -32769);
     CHECK(failure);
 
     /* out of range (-32767..32767) */
-    failure = rt_band_set_nodata(ctx, band, 32769);
+    failure = rt_band_set_nodata(band, 32769);
     CHECK(failure);
 
     /* out of range (-32767..32767) */
-    failure = rt_band_set_pixel(ctx, band, 0, 0, -32769); 
+    failure = rt_band_set_pixel(band, 0, 0, -32769); 
     CHECK(failure);
 
     /* out of range (-32767..32767) */
-    failure = rt_band_set_pixel(ctx, band, 0, 0, 32769); 
+    failure = rt_band_set_pixel(band, 0, 0, 32769); 
     CHECK(failure);
 
     /* out of dimensions range */
-    failure = rt_band_set_pixel(ctx, band, rt_band_get_width(ctx, band), 0, 0); 
+    failure = rt_band_set_pixel(band, rt_band_get_width(band), 0, 0); 
     CHECK(failure);
 
     {
         int x, y;
-        for (x=0; x<rt_band_get_width(ctx, band); ++x)
+        for (x=0; x<rt_band_get_width(band); ++x)
         {
-            for (y=0; y<rt_band_get_height(ctx, band); ++y)
+            for (y=0; y<rt_band_get_height(band); ++y)
             {
-                failure = rt_band_set_pixel(ctx, band, x, y, 255);  
+                failure = rt_band_set_pixel(band, x, y, 255);  
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS(val, 255);
 
-                failure = rt_band_set_pixel(ctx, band, x, y, -32767);   
+                failure = rt_band_set_pixel(band, x, y, -32767);   
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS(val, -32767);
 
-                failure = rt_band_set_pixel(ctx, band, x, y, 32767);   
+                failure = rt_band_set_pixel(band, x, y, 32767);   
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS(val, 32767);
             }
@@ -686,71 +686,71 @@ static void testBand16BSI(rt_context ctx, rt_band band)
     }
 }
 
-static void testBand32BUI(rt_context ctx, rt_band band)
+static void testBand32BUI(rt_band band)
 {
     double val;
     int failure;
 
-    failure = rt_band_set_nodata(ctx, band, 1);
+    failure = rt_band_set_nodata(band, 1);
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 1);
 
-    failure = rt_band_set_nodata(ctx, band, 0);
+    failure = rt_band_set_nodata(band, 0);
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 0);
 
-    failure = rt_band_set_nodata(ctx, band, 65535);   
+    failure = rt_band_set_nodata(band, 65535);   
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 65535);
 
-    failure = rt_band_set_nodata(ctx, band, 4294967295UL);
+    failure = rt_band_set_nodata(band, 4294967295UL);
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 4294967295UL);
 
     /* out of range */
-    failure = rt_band_set_nodata(ctx, band, 4294967296ULL);
+    failure = rt_band_set_nodata(band, 4294967296ULL);
     CHECK(failure);
 
     /* out of value range */
-    failure = rt_band_set_pixel(ctx, band, 0, 0, 4294967296ULL);
+    failure = rt_band_set_pixel(band, 0, 0, 4294967296ULL);
     CHECK(failure);
 
     /* out of dimensions range */
-    failure = rt_band_set_pixel(ctx, band, rt_band_get_width(ctx, band),
+    failure = rt_band_set_pixel(band, rt_band_get_width(band),
                                 0, 4294967296ULL);
     CHECK(failure);
 
     {
         int x, y;
-        for (x=0; x<rt_band_get_width(ctx, band); ++x)
+        for (x=0; x<rt_band_get_width(band); ++x)
         {
-            for (y=0; y<rt_band_get_height(ctx, band); ++y)
+            for (y=0; y<rt_band_get_height(band); ++y)
             {
-                failure = rt_band_set_pixel(ctx, band, x, y, 1);
+                failure = rt_band_set_pixel(band, x, y, 1);
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS(val, 1);
 
-                failure = rt_band_set_pixel(ctx, band, x, y, 0);
+                failure = rt_band_set_pixel(band, x, y, 0);
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS(val, 0);
 
-                failure = rt_band_set_pixel(ctx, band, x, y, 65535);   
+                failure = rt_band_set_pixel(band, x, y, 65535);   
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS(val, 65535);
 
-                failure = rt_band_set_pixel(ctx, band, x, y, 4294967295UL); 
+                failure = rt_band_set_pixel(band, x, y, 4294967295UL); 
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS(val, 4294967295UL);
             }
@@ -758,72 +758,72 @@ static void testBand32BUI(rt_context ctx, rt_band band)
     }
 }
 
-static void testBand32BSI(rt_context ctx, rt_band band)
+static void testBand32BSI(rt_band band)
 {
     double val;
     int failure;
 
-    failure = rt_band_set_nodata(ctx, band, 1);
+    failure = rt_band_set_nodata(band, 1);
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 1);
 
-    failure = rt_band_set_nodata(ctx, band, 0);
+    failure = rt_band_set_nodata(band, 0);
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 0);
 
-    failure = rt_band_set_nodata(ctx, band, 65535);   
+    failure = rt_band_set_nodata(band, 65535);   
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 65535);
 
-    failure = rt_band_set_nodata(ctx, band, 2147483647);
+    failure = rt_band_set_nodata(band, 2147483647);
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     /*printf("32BSI pix is %ld\n", (long int)val);*/
     CHECK_EQUALS(val, 2147483647);
 
-    failure = rt_band_set_nodata(ctx, band, 2147483648UL);
+    failure = rt_band_set_nodata(band, 2147483648UL);
     /* out of range */
     CHECK(failure);
 
     /* out of value range */
-    failure = rt_band_set_pixel(ctx, band, 0, 0, 2147483648UL);  
+    failure = rt_band_set_pixel(band, 0, 0, 2147483648UL);  
     CHECK(failure);
 
     /* out of dimensions range */
-    failure = rt_band_set_pixel(ctx, band, rt_band_get_width(ctx, band), 0, 0);
+    failure = rt_band_set_pixel(band, rt_band_get_width(band), 0, 0);
     CHECK(failure);
 
 
     {
         int x, y;
-        for (x=0; x<rt_band_get_width(ctx, band); ++x)
+        for (x=0; x<rt_band_get_width(band); ++x)
         {
-            for (y=0; y<rt_band_get_height(ctx, band); ++y)
+            for (y=0; y<rt_band_get_height(band); ++y)
             {
-                failure = rt_band_set_pixel(ctx, band, x, y, 1);
+                failure = rt_band_set_pixel(band, x, y, 1);
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS(val, 1);
 
-                failure = rt_band_set_pixel(ctx, band, x, y, 0);
+                failure = rt_band_set_pixel(band, x, y, 0);
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS(val, 0);
 
-                failure = rt_band_set_pixel(ctx, band, x, y, 65535);   
+                failure = rt_band_set_pixel(band, x, y, 65535);   
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS(val, 65535);
 
-                failure = rt_band_set_pixel(ctx, band, x, y, 2147483647); 
+                failure = rt_band_set_pixel(band, x, y, 2147483647); 
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS(val, 2147483647);
             }
@@ -831,59 +831,59 @@ static void testBand32BSI(rt_context ctx, rt_band band)
     }
 }
 
-static void testBand32BF(rt_context ctx, rt_band band)
+static void testBand32BF(rt_band band)
 {
     double val;
     int failure;
 
-    failure = rt_band_set_nodata(ctx, band, 1);
+    failure = rt_band_set_nodata(band, 1);
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 1);
 
-    failure = rt_band_set_nodata(ctx, band, 0);
+    failure = rt_band_set_nodata(band, 0);
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 0);
 
-    failure = rt_band_set_nodata(ctx, band, 65535.5);
+    failure = rt_band_set_nodata(band, 65535.5);
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     //printf("set 65535.56 on %s band gets %g back\n", pixtypeName, val);
     CHECK_EQUALS_DOUBLE(val, 65535.5);
 
-    failure = rt_band_set_nodata(ctx, band, 0.006); 
+    failure = rt_band_set_nodata(band, 0.006); 
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS_DOUBLE(val, 0.0060000000521540); /* XXX: Alternatively, use CHECK_EQUALS_DOUBLE_EX */
 
     {
         int x, y;
-        for (x=0; x<rt_band_get_width(ctx, band); ++x)
+        for (x=0; x<rt_band_get_width(band); ++x)
         {
-            for (y=0; y<rt_band_get_height(ctx, band); ++y)
+            for (y=0; y<rt_band_get_height(band); ++y)
             {
-                failure = rt_band_set_pixel(ctx, band, x, y, 1);
+                failure = rt_band_set_pixel(band, x, y, 1);
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS(val, 1);
 
-                failure = rt_band_set_pixel(ctx, band, x, y, 0);
+                failure = rt_band_set_pixel(band, x, y, 0);
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS(val, 0);
 
-                failure = rt_band_set_pixel(ctx, band, x, y, 65535.5);
+                failure = rt_band_set_pixel(band, x, y, 65535.5);
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS_DOUBLE(val, 65535.5);
 
-                failure = rt_band_set_pixel(ctx, band, x, y, 0.006); 
+                failure = rt_band_set_pixel(band, x, y, 0.006); 
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS_DOUBLE(val, 0.0060000000521540);
 
@@ -892,58 +892,58 @@ static void testBand32BF(rt_context ctx, rt_band band)
     }
 }
 
-static void testBand64BF(rt_context ctx, rt_band band)
+static void testBand64BF(rt_band band)
 {
     double val;
     int failure;
 
-    failure = rt_band_set_nodata(ctx, band, 1);
+    failure = rt_band_set_nodata(band, 1);
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 1);
 
-    failure = rt_band_set_nodata(ctx, band, 0);
+    failure = rt_band_set_nodata(band, 0);
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 0);
 
-    failure = rt_band_set_nodata(ctx, band, 65535.56);   
+    failure = rt_band_set_nodata(band, 65535.56);   
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 65535.56);
 
-    failure = rt_band_set_nodata(ctx, band, 0.006); 
+    failure = rt_band_set_nodata(band, 0.006); 
     CHECK(!failure);
-    val = rt_band_get_nodata(ctx, band);
+    val = rt_band_get_nodata(band);
     CHECK_EQUALS(val, 0.006);
 
     {
         int x, y;
-        for (x=0; x<rt_band_get_width(ctx, band); ++x)
+        for (x=0; x<rt_band_get_width(band); ++x)
         {
-            for (y=0; y<rt_band_get_height(ctx, band); ++y)
+            for (y=0; y<rt_band_get_height(band); ++y)
             {
-                failure = rt_band_set_pixel(ctx, band, x, y, 1);
+                failure = rt_band_set_pixel(band, x, y, 1);
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS(val, 1);
 
-                failure = rt_band_set_pixel(ctx, band, x, y, 0);
+                failure = rt_band_set_pixel(band, x, y, 0);
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS(val, 0);
 
-                failure = rt_band_set_pixel(ctx, band, x, y, 65535.56);   
+                failure = rt_band_set_pixel(band, x, y, 65535.56);   
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS(val, 65535.56);
 
-                failure = rt_band_set_pixel(ctx, band, x, y, 0.006); 
+                failure = rt_band_set_pixel(band, x, y, 0.006); 
                 CHECK(!failure);
-                failure = rt_band_get_pixel(ctx, band, x, y, &val);
+                failure = rt_band_get_pixel(band, x, y, &val);
                 CHECK(!failure);
                 CHECK_EQUALS(val, 0.006);
 
@@ -952,119 +952,115 @@ static void testBand64BF(rt_context ctx, rt_band band)
     }
 }
 
-static void testBandHasNoData(rt_context ctx, rt_band band)
+static void testBandHasNoData(rt_band band)
 {
     int flag;
 
-    flag = rt_band_get_hasnodata_flag(ctx, band);
+    flag = rt_band_get_hasnodata_flag(band);
     CHECK_EQUALS(flag, 1);
 
-    rt_band_set_hasnodata_flag(ctx, band, 0);
-    flag = rt_band_get_hasnodata_flag(ctx, band);
+    rt_band_set_hasnodata_flag(band, 0);
+    flag = rt_band_get_hasnodata_flag(band);
     CHECK_EQUALS(flag, 0);
 
-    rt_band_set_hasnodata_flag(ctx, band, 10);
-    flag = rt_band_get_hasnodata_flag(ctx, band);
+    rt_band_set_hasnodata_flag(band, 10);
+    flag = rt_band_get_hasnodata_flag(band);
     CHECK_EQUALS(flag, 1);
 
-    rt_band_set_hasnodata_flag(ctx, band, -10);
-    flag = rt_band_get_hasnodata_flag(ctx, band);
+    rt_band_set_hasnodata_flag(band, -10);
+    flag = rt_band_get_hasnodata_flag(band);
     CHECK_EQUALS(flag, 1);
 }
 
 int
 main()
 {
-    /* will use default allocators and message handlers */
-    rt_context ctx;
-    
     rt_raster raster;
     rt_band band_1BB, band_2BUI, band_4BUI,
             band_8BSI, band_8BUI, band_16BSI, band_16BUI,
             band_32BSI, band_32BUI, band_32BF,
             band_64BF;
 
-    ctx = rt_context_new(0, 0, 0);
-    raster = rt_raster_new(ctx, 256, 256);
+    raster = rt_raster_new(256, 256);
     assert(raster); /* or we're out of virtual memory */
        
        printf("Checking empty and hasnoband functions...\n");
        { /* Check isEmpty and hasnoband */
-               CHECK(!rt_raster_is_empty(ctx, raster));
+               CHECK(!rt_raster_is_empty(raster));
                
                /* Create a dummy empty raster to test the opposite
                 * to the previous sentence
                 */
-               rt_raster emptyraster = rt_raster_new(ctx, 0, 0);
-               CHECK(rt_raster_is_empty(ctx, emptyraster));
-               rt_raster_destroy(ctx, emptyraster);
+               rt_raster emptyraster = rt_raster_new(0, 0);
+               CHECK(rt_raster_is_empty(emptyraster));
+               rt_raster_destroy(emptyraster);
                
                /* Once we add a band to this raster, we'll check the opposite */
-               CHECK(rt_raster_has_no_band(ctx, raster, 1));
+               CHECK(rt_raster_has_no_band(raster, 1));
        }
 
        
        printf("Checking raster properties...\n");
     { /* Check scale */
         float scale;
-        scale = rt_raster_get_x_scale(ctx, raster);
+        scale = rt_raster_get_x_scale(raster);
         CHECK_EQUALS(scale, 1);
-        scale = rt_raster_get_y_scale(ctx, raster);
+        scale = rt_raster_get_y_scale(raster);
         CHECK_EQUALS(scale, 1);
     }
 
     { /* Check offsets */
         float off;
 
-        off = rt_raster_get_x_offset(ctx, raster);
+        off = rt_raster_get_x_offset(raster);
         CHECK_EQUALS(off, 0.5);
 
-        off = rt_raster_get_y_offset(ctx, raster);
+        off = rt_raster_get_y_offset(raster);
         CHECK_EQUALS(off, 0.5);
 
-        rt_raster_set_offsets(ctx, raster, 30, 70);
+        rt_raster_set_offsets(raster, 30, 70);
 
-        off = rt_raster_get_x_offset(ctx, raster);
+        off = rt_raster_get_x_offset(raster);
         CHECK_EQUALS(off, 30);
 
-        off = rt_raster_get_y_offset(ctx, raster);
+        off = rt_raster_get_y_offset(raster);
         CHECK_EQUALS(off, 70);
 
-        rt_raster_set_offsets(ctx, raster, 0.5, 0.5);
+        rt_raster_set_offsets(raster, 0.5, 0.5);
     }
 
     { /* Check skew */
         float off;
 
-        off = rt_raster_get_x_skew(ctx, raster);
+        off = rt_raster_get_x_skew(raster);
         CHECK_EQUALS(off, 0);
 
-        off = rt_raster_get_y_skew(ctx, raster);
+        off = rt_raster_get_y_skew(raster);
         CHECK_EQUALS(off, 0);
 
-        rt_raster_set_skews(ctx, raster, 4, 5);
+        rt_raster_set_skews(raster, 4, 5);
 
-        off = rt_raster_get_x_skew(ctx, raster);
+        off = rt_raster_get_x_skew(raster);
         CHECK_EQUALS(off, 4);
 
-        off = rt_raster_get_y_skew(ctx, raster);
+        off = rt_raster_get_y_skew(raster);
         CHECK_EQUALS(off, 5);
 
-        rt_raster_set_skews(ctx, raster, 0, 0);
+        rt_raster_set_skews(raster, 0, 0);
     }
 
     { /* Check SRID */
         int32_t srid;
-        srid = rt_raster_get_srid(ctx, raster);
+        srid = rt_raster_get_srid(raster);
         CHECK_EQUALS(srid, -1);
 
-        rt_raster_set_srid(ctx, raster, 65546);
-        srid = rt_raster_get_srid(ctx, raster);
+        rt_raster_set_srid(raster, 65546);
+        srid = rt_raster_get_srid(raster);
         CHECK_EQUALS(srid, 65546);
     }
 
     printf("Raster starts with %d bands\n",
-        rt_raster_get_num_bands(ctx, raster));
+        rt_raster_get_num_bands(raster));
 
     { /* Check convex hull, based on offset, scale and rotation */
         LWPOLY *convexhull;
@@ -1072,10 +1068,10 @@ main()
         POINT4D pt;
 
         /* will rotate the raster to see difference with the envelope */
-        rt_raster_set_skews(ctx, raster, 4, 5);
+        rt_raster_set_skews(raster, 4, 5);
 
-        convexhull = rt_raster_get_convex_hull(ctx, raster);
-        CHECK_EQUALS(convexhull->srid, rt_raster_get_srid(ctx, raster));
+        convexhull = rt_raster_get_convex_hull(raster);
+        CHECK_EQUALS(convexhull->srid, rt_raster_get_srid(raster));
         CHECK_EQUALS(convexhull->nrings, 1);
         ring = convexhull->rings[0];
         CHECK(ring);
@@ -1108,17 +1104,17 @@ main()
 
         lwpoly_free(convexhull);
 
-        rt_raster_set_skews(ctx, raster, 0, 0);
+        rt_raster_set_skews(raster, 0, 0);
     }
 
     {   /* Check ST_AsPolygon */
         printf("Testing polygonize function\n");
 
                /* First test: NODATA value = -1 */
-        rt_raster rt = fillRasterToPolygonize(ctx, 1, -1.0);
+        rt_raster rt = fillRasterToPolygonize(1, -1.0);
 
                /* We can check rt_raster_has_no_band here too */
-               CHECK(!rt_raster_has_no_band(ctx, rt, 1));
+               CHECK(!rt_raster_has_no_band(rt, 1));
 
         /**
          * Need to define again, to access the struct fields
@@ -1132,7 +1128,7 @@ main()
         typedef struct rt_geomval_t* rt_geomval;
         int nPols = 0;
         
-        rt_geomval gv = (rt_geomval) rt_raster_dump_as_wktpolygons(ctx, rt, 1, &nPols);
+        rt_geomval gv = (rt_geomval) rt_raster_dump_as_wktpolygons(rt, 1, &nPols);
 
         CHECK_EQUALS_DOUBLE(gv[0].val, 1.0);
         CHECK(!strcmp(gv[0].geom, "POLYGON ((3 1,3 2,2 2,2 3,1 3,1 6,2 6,2 7,3 7,3 8,5 8,5 6,3 6,3 3,4 3,5 3,5 1,3 1))"));
@@ -1147,18 +1143,18 @@ main()
                CHECK(!strcmp(gv[3].geom, "POLYGON ((0 0,0 9,9 9,9 0,0 0),(6 7,6 8,3 8,3 7,2 7,2 6,1 6,1 3,2 3,2 2,3 2,3 1,6 1,6 2,7 2,7 3,8 3,8 6,7 6,7 7,6 7))"));
                
         
-        rt_raster_destroy(ctx, rt);
+        rt_raster_destroy(rt);
 
        
                /* Second test: NODATA value = 1 */
-        rt = fillRasterToPolygonize(ctx, 1, 1.0);
+        rt = fillRasterToPolygonize(1, 1.0);
                                
                /* We can check rt_raster_has_no_band here too */
-               CHECK(!rt_raster_has_no_band(ctx, rt, 1));
+               CHECK(!rt_raster_has_no_band(rt, 1));
 
         nPols = 0;
         
-        gv = (rt_geomval) rt_raster_dump_as_wktpolygons(ctx, rt, 1, &nPols);
+        gv = (rt_geomval) rt_raster_dump_as_wktpolygons(rt, 1, &nPols);
 
 
                CHECK_EQUALS_DOUBLE(gv[0].val, 0.0);
@@ -1169,17 +1165,17 @@ main()
 
                CHECK_EQUALS_DOUBLE(gv[2].val, 0.0);
                CHECK(!strcmp(gv[2].geom, "POLYGON ((0 0,0 9,9 9,9 0,0 0),(6 7,6 8,3 8,3 7,2 7,2 6,1 6,1 3,2 3,2 2,3 2,3 1,6 1,6 2,7 2,7 3,8 3,8 6,7 6,7 7,6 7))"));
-        rt_raster_destroy(ctx, rt);
+        rt_raster_destroy(rt);
  
                /* Third test: NODATA value = 2 */
-        rt = fillRasterToPolygonize(ctx, 1, 2.0);
+        rt = fillRasterToPolygonize(1, 2.0);
                                
                /* We can check rt_raster_has_no_band here too */
-               CHECK(!rt_raster_has_no_band(ctx, rt, 1));
+               CHECK(!rt_raster_has_no_band(rt, 1));
 
         nPols = 0;
         
-        gv = (rt_geomval) rt_raster_dump_as_wktpolygons(ctx, rt, 1, &nPols);
+        gv = (rt_geomval) rt_raster_dump_as_wktpolygons(rt, 1, &nPols);
 
         CHECK_EQUALS_DOUBLE(gv[0].val, 1.0);
         CHECK(!strcmp(gv[0].geom, "POLYGON ((3 1,3 2,2 2,2 3,1 3,1 6,2 6,2 7,3 7,3 8,5 8,5 6,3 6,3 3,4 3,5 3,5 1,3 1))"));
@@ -1189,18 +1185,18 @@ main()
 
                CHECK_EQUALS_DOUBLE(gv[2].val, 0.0);
                CHECK(!strcmp(gv[2].geom, "POLYGON ((0 0,0 9,9 9,9 0,0 0),(6 7,6 8,3 8,3 7,2 7,2 6,1 6,1 3,2 3,2 2,3 2,3 1,6 1,6 2,7 2,7 3,8 3,8 6,7 6,7 7,6 7))"));
-        rt_raster_destroy(ctx, rt);
+        rt_raster_destroy(rt);
  
 
                /* Fourth test: NODATA value = 0 */
-        rt = fillRasterToPolygonize(ctx, 1, 0.0);
+        rt = fillRasterToPolygonize(1, 0.0);
                                
                /* We can check rt_raster_has_no_band here too */
-               CHECK(!rt_raster_has_no_band(ctx, rt, 1));
+               CHECK(!rt_raster_has_no_band(rt, 1));
 
         nPols = 0;
         
-        gv = (rt_geomval) rt_raster_dump_as_wktpolygons(ctx, rt, 1, &nPols);
+        gv = (rt_geomval) rt_raster_dump_as_wktpolygons(rt, 1, &nPols);
                
         CHECK_EQUALS_DOUBLE(gv[0].val, 1.0);
         CHECK(!strcmp(gv[0].geom, "POLYGON ((3 1,3 2,2 2,2 3,1 3,1 6,2 6,2 7,3 7,3 8,5 8,5 6,3 6,3 3,4 3,5 3,5 1,3 1))"));
@@ -1208,17 +1204,17 @@ main()
                CHECK_EQUALS_DOUBLE(gv[1].val, 2.0);
         CHECK(!strcmp(gv[1].geom, "POLYGON ((5 1,5 3,6 3,6 6,5 6,5 8,6 8,6 7,7 7,7 6,8 6,8 3,7 3,7 2,6 2,6 1,5 1))"));   
 
-               rt_raster_destroy(ctx, rt);
+               rt_raster_destroy(rt);
  
                /* Last test: There is no NODATA value (all values are valid) */
-        rt = fillRasterToPolygonize(ctx, 0, 1.0);
+        rt = fillRasterToPolygonize(0, 1.0);
                
                /* We can check rt_raster_has_no_band here too */
-               CHECK(!rt_raster_has_no_band(ctx, rt, 1));
+               CHECK(!rt_raster_has_no_band(rt, 1));
 
         nPols = 0;
         
-        gv = (rt_geomval) rt_raster_dump_as_wktpolygons(ctx, rt, 1, &nPols);
+        gv = (rt_geomval) rt_raster_dump_as_wktpolygons(rt, 1, &nPols);
 
         CHECK_EQUALS_DOUBLE(gv[0].val, 1.0);
         CHECK(!strcmp(gv[0].geom, "POLYGON ((3 1,3 2,2 2,2 3,1 3,1 6,2 6,2 7,3 7,3 8,5 8,5 6,3 6,3 3,4 3,5 3,5 1,3 1))"));
@@ -1231,59 +1227,58 @@ main()
 
                CHECK_EQUALS_DOUBLE(gv[3].val, 0.0);
                CHECK(!strcmp(gv[3].geom, "POLYGON ((0 0,0 9,9 9,9 0,0 0),(6 7,6 8,3 8,3 7,2 7,2 6,1 6,1 3,2 3,2 2,3 2,3 1,6 1,6 2,7 2,7 3,8 3,8 6,7 6,7 7,6 7))"));
-               rt_raster_destroy(ctx, rt);
+               rt_raster_destroy(rt);
 
     }
 
     printf("Testing 1BB band\n");
-    band_1BB = addBand(ctx, raster, PT_1BB, 0, 0);
-    testBand1BB(ctx, band_1BB);
+    band_1BB = addBand(raster, PT_1BB, 0, 0);
+    testBand1BB(band_1BB);
 
     printf("Testing 2BB band\n");
-    band_2BUI = addBand(ctx, raster, PT_2BUI, 0, 0);
-    testBand2BUI(ctx, band_2BUI);
+    band_2BUI = addBand(raster, PT_2BUI, 0, 0);
+    testBand2BUI(band_2BUI);
 
     printf("Testing 4BUI band\n");
-    band_4BUI = addBand(ctx, raster, PT_4BUI, 0, 0);
-    testBand4BUI(ctx, band_4BUI);
+    band_4BUI = addBand(raster, PT_4BUI, 0, 0);
+    testBand4BUI(band_4BUI);
 
     printf("Testing 8BUI band\n");
-    band_8BUI = addBand(ctx, raster, PT_8BUI, 0, 0);
-    testBand8BUI(ctx, band_8BUI);
+    band_8BUI = addBand(raster, PT_8BUI, 0, 0);
+    testBand8BUI(band_8BUI);
 
     printf("Testing 8BSI band\n");
-    band_8BSI = addBand(ctx, raster, PT_8BSI, 0, 0);
-    testBand8BSI(ctx, band_8BSI);
+    band_8BSI = addBand(raster, PT_8BSI, 0, 0);
+    testBand8BSI(band_8BSI);
 
     printf("Testing 16BSI band\n");
-    band_16BSI = addBand(ctx, raster, PT_16BSI, 0, 0);
-    testBand16BSI(ctx, band_16BSI);
+    band_16BSI = addBand(raster, PT_16BSI, 0, 0);
+    testBand16BSI(band_16BSI);
 
     printf("Testing 16BUI band\n");
-    band_16BUI = addBand(ctx, raster, PT_16BUI, 0, 0);
-    testBand16BUI(ctx, band_16BUI);
+    band_16BUI = addBand(raster, PT_16BUI, 0, 0);
+    testBand16BUI(band_16BUI);
 
     printf("Testing 32BUI band\n");
-    band_32BUI = addBand(ctx, raster, PT_32BUI, 0, 0);
-    testBand32BUI(ctx, band_32BUI);
+    band_32BUI = addBand(raster, PT_32BUI, 0, 0);
+    testBand32BUI(band_32BUI);
 
     printf("Testing 32BSI band\n");
-    band_32BSI = addBand(ctx, raster, PT_32BSI, 0, 0);
-    testBand32BSI(ctx, band_32BSI);
+    band_32BSI = addBand(raster, PT_32BSI, 0, 0);
+    testBand32BSI(band_32BSI);
 
     printf("Testing 32BF band\n");
-    band_32BF = addBand(ctx, raster, PT_32BF, 0, 0);
-    testBand32BF(ctx, band_32BF);
+    band_32BF = addBand(raster, PT_32BF, 0, 0);
+    testBand32BF(band_32BF);
 
     printf("Testing 64BF band\n");
-    band_64BF = addBand(ctx, raster, PT_64BF, 0, 0);
-    testBand64BF(ctx, band_64BF);
+    band_64BF = addBand(raster, PT_64BF, 0, 0);
+    testBand64BF(band_64BF);
 
     printf("Testing band hasnodata flag\n");
-    testBandHasNoData(ctx, band_64BF);
+    testBandHasNoData(band_64BF);
 
-    deepRelease(ctx, raster);
-    rt_context_destroy(ctx);
+    deepRelease(raster);
 
     return EXIT_SUCCESS;
 }
@@ -1294,3 +1289,9 @@ lwgeom_init_allocators(void)
 {
     lwgeom_install_default_allocators();
 }
+
+
+void rt_init_allocators(void)
+{
+    rt_install_default_allocators();
+}
index 30e7ad7b6a6f9e22ad846a23ab4f513d4c49fa87..42a1468262df0d81871022a478a926dc984da375 100644 (file)
@@ -36,15 +36,12 @@ int
 main()
 {
     /* will use default allocators and message handlers */
-    rt_context ctx = NULL;
     rt_raster raster = NULL;
     const char *hexwkb = NULL;
     const char *out = NULL;
     uint32_t len = 0;
     int i = 0;
 
-    ctx = rt_context_new(NULL, NULL, NULL);
-
     /* ------------------------------------------------------ */
     /* No bands, 7x8 - little endian                          */
     /* ------------------------------------------------------ */
@@ -64,20 +61,20 @@ main()
 "0800"             /* height (uint16 8) */
     ;
 
-    raster = rt_raster_from_hexwkb(ctx, hexwkb, strlen(hexwkb));
+    raster = rt_raster_from_hexwkb(hexwkb, strlen(hexwkb));
     CHECK(raster);
-    CHECK_EQUALS(rt_raster_get_num_bands(ctx, raster), 0);
-    CHECK_EQUALS(rt_raster_get_x_scale(ctx, raster), 1);
-    CHECK_EQUALS(rt_raster_get_y_scale(ctx, raster), 2);
-    CHECK_EQUALS(rt_raster_get_x_offset(ctx, raster), 3);
-    CHECK_EQUALS(rt_raster_get_y_offset(ctx, raster), 4);
-    CHECK_EQUALS(rt_raster_get_x_skew(ctx, raster), 5);
-    CHECK_EQUALS(rt_raster_get_y_skew(ctx, raster), 6);
-    CHECK_EQUALS(rt_raster_get_srid(ctx, raster), 10);
-    CHECK_EQUALS(rt_raster_get_width(ctx, raster), 7);
-    CHECK_EQUALS(rt_raster_get_height(ctx, raster), 8);
-
-    out  = rt_raster_to_hexwkb(ctx, raster, &len);
+    CHECK_EQUALS(rt_raster_get_num_bands(raster), 0);
+    CHECK_EQUALS(rt_raster_get_x_scale(raster), 1);
+    CHECK_EQUALS(rt_raster_get_y_scale(raster), 2);
+    CHECK_EQUALS(rt_raster_get_x_offset(raster), 3);
+    CHECK_EQUALS(rt_raster_get_y_offset(raster), 4);
+    CHECK_EQUALS(rt_raster_get_x_skew(raster), 5);
+    CHECK_EQUALS(rt_raster_get_y_skew(raster), 6);
+    CHECK_EQUALS(rt_raster_get_srid(raster), 10);
+    CHECK_EQUALS(rt_raster_get_width(raster), 7);
+    CHECK_EQUALS(rt_raster_get_height(raster), 8);
+
+    out  = rt_raster_to_hexwkb(raster, &len);
 /*
     printf(" in hexwkb len: %d\n", strlen(hexwkb));
     printf("out hexwkb len: %d\n", len);
@@ -94,14 +91,14 @@ main()
         void *serialized;
         rt_raster rast2;
 
-        serialized = rt_raster_serialize(ctx, raster);
-        rast2 = rt_raster_deserialize(ctx, serialized);
+        serialized = rt_raster_serialize(raster);
+        rast2 = rt_raster_deserialize(serialized);
 
-        rt_raster_destroy(ctx, rast2);
+        rt_raster_destroy(rast2);
         free(serialized);
     }
 
-    rt_raster_destroy(ctx, raster);
+    rt_raster_destroy(raster);
 
     /* ------------------------------------------------------ */
     /* No bands, 7x8 - big endian                             */
@@ -122,20 +119,20 @@ main()
 "0008"             /* height (uint16 8) */
     ;
 
-    raster = rt_raster_from_hexwkb(ctx, hexwkb, strlen(hexwkb));
+    raster = rt_raster_from_hexwkb(hexwkb, strlen(hexwkb));
     CHECK(raster);
-    CHECK_EQUALS(rt_raster_get_num_bands(ctx, raster), 0);
-    CHECK_EQUALS(rt_raster_get_x_scale(ctx, raster), 1);
-    CHECK_EQUALS(rt_raster_get_y_scale(ctx, raster), 2);
-    CHECK_EQUALS(rt_raster_get_x_offset(ctx, raster), 3);
-    CHECK_EQUALS(rt_raster_get_y_offset(ctx, raster), 4);
-    CHECK_EQUALS(rt_raster_get_x_skew(ctx, raster), 5);
-    CHECK_EQUALS(rt_raster_get_y_skew(ctx, raster), 6);
-    CHECK_EQUALS(rt_raster_get_srid(ctx, raster), 10);
-    CHECK_EQUALS(rt_raster_get_width(ctx, raster), 7);
-    CHECK_EQUALS(rt_raster_get_height(ctx, raster), 8);
-
-    out  = rt_raster_to_hexwkb(ctx, raster, &len);
+    CHECK_EQUALS(rt_raster_get_num_bands(raster), 0);
+    CHECK_EQUALS(rt_raster_get_x_scale(raster), 1);
+    CHECK_EQUALS(rt_raster_get_y_scale(raster), 2);
+    CHECK_EQUALS(rt_raster_get_x_offset(raster), 3);
+    CHECK_EQUALS(rt_raster_get_y_offset(raster), 4);
+    CHECK_EQUALS(rt_raster_get_x_skew(raster), 5);
+    CHECK_EQUALS(rt_raster_get_y_skew(raster), 6);
+    CHECK_EQUALS(rt_raster_get_srid(raster), 10);
+    CHECK_EQUALS(rt_raster_get_width(raster), 7);
+    CHECK_EQUALS(rt_raster_get_height(raster), 8);
+
+    out  = rt_raster_to_hexwkb(raster, &len);
     printf(" in hexwkb len: %u\n", strlen(hexwkb));
     printf("out hexwkb len: %u\n", len);
     printf(" in hexwkb: %s\n", hexwkb);
@@ -145,7 +142,7 @@ main()
     CHECK( ! strcmp(hexwkb, out) );
 */
 
-    rt_raster_destroy(ctx, raster);
+    rt_raster_destroy(raster);
     free((/*no const*/ void*)out);
 
     /* ------------------------------------------------------ */
@@ -170,33 +167,33 @@ main()
 "01"               /* pix(0,0) == 1 */
     ;
 
-    raster = rt_raster_from_hexwkb(ctx, hexwkb, strlen(hexwkb));
+    raster = rt_raster_from_hexwkb(hexwkb, strlen(hexwkb));
     CHECK(raster);
-    CHECK_EQUALS(rt_raster_get_num_bands(ctx, raster), 1);
-    CHECK_EQUALS(rt_raster_get_x_scale(ctx, raster), 1);
-    CHECK_EQUALS(rt_raster_get_y_scale(ctx, raster), 2);
-    CHECK_EQUALS(rt_raster_get_x_offset(ctx, raster), 3);
-    CHECK_EQUALS(rt_raster_get_y_offset(ctx, raster), 4);
-    CHECK_EQUALS(rt_raster_get_x_skew(ctx, raster), 5);
-    CHECK_EQUALS(rt_raster_get_y_skew(ctx, raster), 6);
-    CHECK_EQUALS(rt_raster_get_srid(ctx, raster), 10);
-    CHECK_EQUALS(rt_raster_get_width(ctx, raster), 1);
-    CHECK_EQUALS(rt_raster_get_height(ctx, raster), 1);
+    CHECK_EQUALS(rt_raster_get_num_bands(raster), 1);
+    CHECK_EQUALS(rt_raster_get_x_scale(raster), 1);
+    CHECK_EQUALS(rt_raster_get_y_scale(raster), 2);
+    CHECK_EQUALS(rt_raster_get_x_offset(raster), 3);
+    CHECK_EQUALS(rt_raster_get_y_offset(raster), 4);
+    CHECK_EQUALS(rt_raster_get_x_skew(raster), 5);
+    CHECK_EQUALS(rt_raster_get_y_skew(raster), 6);
+    CHECK_EQUALS(rt_raster_get_srid(raster), 10);
+    CHECK_EQUALS(rt_raster_get_width(raster), 1);
+    CHECK_EQUALS(rt_raster_get_height(raster), 1);
     {
         double val;
         int failure;
-        rt_band band = rt_raster_get_band(ctx, raster, 0);
+        rt_band band = rt_raster_get_band(raster, 0);
         CHECK(band);
-        CHECK_EQUALS(rt_band_get_pixtype(ctx, band), PT_1BB);
-        CHECK(!rt_band_is_offline(ctx, band));
-        CHECK(rt_band_get_hasnodata_flag(ctx, band));
-        CHECK_EQUALS(rt_band_get_nodata(ctx, band), 0);
-        failure = rt_band_get_pixel(ctx, band, 0, 0, &val);
+        CHECK_EQUALS(rt_band_get_pixtype(band), PT_1BB);
+        CHECK(!rt_band_is_offline(band));
+        CHECK(rt_band_get_hasnodata_flag(band));
+        CHECK_EQUALS(rt_band_get_nodata(band), 0);
+        failure = rt_band_get_pixel(band, 0, 0, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, 1);
     }
 
-    out  = rt_raster_to_hexwkb(ctx, raster, &len);
+    out  = rt_raster_to_hexwkb(raster, &len);
     printf(" in hexwkb len: %u\n", strlen(hexwkb));
     printf("out hexwkb len: %u\n", len);
     CHECK_EQUALS(len, strlen(hexwkb));
@@ -204,7 +201,7 @@ main()
     CHECK( ! strcmp(hexwkb, out) );
 */
 
-    rt_raster_destroy(ctx, raster);
+    rt_raster_destroy(raster);
     free((/*no const*/ void*)out);
 
     /* ------------------------------------------------------ */
@@ -234,55 +231,55 @@ main()
 "02"               /* pix(2,1) == 2 */
     ;
 
-    raster = rt_raster_from_hexwkb(ctx, hexwkb, strlen(hexwkb));
+    raster = rt_raster_from_hexwkb(hexwkb, strlen(hexwkb));
     CHECK(raster);
-    CHECK_EQUALS(rt_raster_get_num_bands(ctx, raster), 1);
-    CHECK_EQUALS(rt_raster_get_x_scale(ctx, raster), 1);
-    CHECK_EQUALS(rt_raster_get_y_scale(ctx, raster), 2);
-    CHECK_EQUALS(rt_raster_get_x_offset(ctx, raster), 3);
-    CHECK_EQUALS(rt_raster_get_y_offset(ctx, raster), 4);
-    CHECK_EQUALS(rt_raster_get_x_skew(ctx, raster), 5);
-    CHECK_EQUALS(rt_raster_get_y_skew(ctx, raster), 6);
-    CHECK_EQUALS(rt_raster_get_srid(ctx, raster), 10);
-    CHECK_EQUALS(rt_raster_get_width(ctx, raster), 3);
-    CHECK_EQUALS(rt_raster_get_height(ctx, raster), 2);
+    CHECK_EQUALS(rt_raster_get_num_bands(raster), 1);
+    CHECK_EQUALS(rt_raster_get_x_scale(raster), 1);
+    CHECK_EQUALS(rt_raster_get_y_scale(raster), 2);
+    CHECK_EQUALS(rt_raster_get_x_offset(raster), 3);
+    CHECK_EQUALS(rt_raster_get_y_offset(raster), 4);
+    CHECK_EQUALS(rt_raster_get_x_skew(raster), 5);
+    CHECK_EQUALS(rt_raster_get_y_skew(raster), 6);
+    CHECK_EQUALS(rt_raster_get_srid(raster), 10);
+    CHECK_EQUALS(rt_raster_get_width(raster), 3);
+    CHECK_EQUALS(rt_raster_get_height(raster), 2);
     {
         double val;
         int failure;
         
-        rt_band band = rt_raster_get_band(ctx, raster, 0);
+        rt_band band = rt_raster_get_band(raster, 0);
         CHECK(band);
-        CHECK_EQUALS(rt_band_get_pixtype(ctx, band), PT_8BSI);
-        CHECK(!rt_band_is_offline(ctx, band));
-        CHECK(rt_band_get_hasnodata_flag(ctx,band));
-        CHECK_EQUALS(rt_band_get_nodata(ctx, band), -1);
+        CHECK_EQUALS(rt_band_get_pixtype(band), PT_8BSI);
+        CHECK(!rt_band_is_offline(band));
+        CHECK(rt_band_get_hasnodata_flag(band));
+        CHECK_EQUALS(rt_band_get_nodata(band), -1);
 
-        failure = rt_band_get_pixel(ctx, band, 0, 0, &val);
+        failure = rt_band_get_pixel(band, 0, 0, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, -1);
 
-        failure = rt_band_get_pixel(ctx, band, 1, 0, &val);
+        failure = rt_band_get_pixel(band, 1, 0, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, 0);
 
-        failure = rt_band_get_pixel(ctx, band, 2, 0, &val);
+        failure = rt_band_get_pixel(band, 2, 0, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, 1);
 
-        failure = rt_band_get_pixel(ctx, band, 0, 1, &val);
+        failure = rt_band_get_pixel(band, 0, 1, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, 127);
 
-        failure = rt_band_get_pixel(ctx, band, 1, 1, &val);
+        failure = rt_band_get_pixel(band, 1, 1, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, 10);
 
-        failure = rt_band_get_pixel(ctx, band, 2, 1, &val);
+        failure = rt_band_get_pixel(band, 2, 1, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, 2);
     }
 
-    out  = rt_raster_to_hexwkb(ctx, raster, &len);
+    out  = rt_raster_to_hexwkb(raster, &len);
     printf(" in hexwkb len: %u\n", strlen(hexwkb));
     printf("out hexwkb len: %u\n", len);
     CHECK_EQUALS(len, strlen(hexwkb));
@@ -296,15 +293,15 @@ main()
         void *serialized;
         rt_raster rast2;
 
-        serialized = rt_raster_serialize(ctx, raster);
-        rast2 = rt_raster_deserialize(ctx, serialized);
+        serialized = rt_raster_serialize(raster);
+        rast2 = rt_raster_deserialize(serialized);
 
-        rt_raster_destroy(ctx, rast2);
+        rt_raster_destroy(rast2);
         free(serialized);
     
     }
 
-    rt_raster_destroy(ctx, raster);
+    rt_raster_destroy(raster);
 
     /* ------------------------------------------------------ */
     /* 3x2, little endian, band0(16BSI)                       */
@@ -333,54 +330,54 @@ main()
 "0200"               /* pix(2,1) == 2 */
     ;
 
-    raster = rt_raster_from_hexwkb(ctx, hexwkb, strlen(hexwkb));
+    raster = rt_raster_from_hexwkb(hexwkb, strlen(hexwkb));
     CHECK(raster);
-    CHECK_EQUALS(rt_raster_get_num_bands(ctx, raster), 1);
-    CHECK_EQUALS(rt_raster_get_x_scale(ctx, raster), 1);
-    CHECK_EQUALS(rt_raster_get_y_scale(ctx, raster), 2);
-    CHECK_EQUALS(rt_raster_get_x_offset(ctx, raster), 3);
-    CHECK_EQUALS(rt_raster_get_y_offset(ctx, raster), 4);
-    CHECK_EQUALS(rt_raster_get_x_skew(ctx, raster), 5);
-    CHECK_EQUALS(rt_raster_get_y_skew(ctx, raster), 6);
-    CHECK_EQUALS(rt_raster_get_srid(ctx, raster), 10);
-    CHECK_EQUALS(rt_raster_get_width(ctx, raster), 3);
-    CHECK_EQUALS(rt_raster_get_height(ctx, raster), 2);
+    CHECK_EQUALS(rt_raster_get_num_bands(raster), 1);
+    CHECK_EQUALS(rt_raster_get_x_scale(raster), 1);
+    CHECK_EQUALS(rt_raster_get_y_scale(raster), 2);
+    CHECK_EQUALS(rt_raster_get_x_offset(raster), 3);
+    CHECK_EQUALS(rt_raster_get_y_offset(raster), 4);
+    CHECK_EQUALS(rt_raster_get_x_skew(raster), 5);
+    CHECK_EQUALS(rt_raster_get_y_skew(raster), 6);
+    CHECK_EQUALS(rt_raster_get_srid(raster), 10);
+    CHECK_EQUALS(rt_raster_get_width(raster), 3);
+    CHECK_EQUALS(rt_raster_get_height(raster), 2);
     {
         double val;
         int failure;
-        rt_band band = rt_raster_get_band(ctx, raster, 0);
+        rt_band band = rt_raster_get_band(raster, 0);
         CHECK(band);
-        CHECK_EQUALS(rt_band_get_pixtype(ctx, band), PT_16BSI);
-        CHECK(!rt_band_is_offline(ctx, band));
-        CHECK(!rt_band_get_hasnodata_flag(ctx, band));
-        CHECK_EQUALS(rt_band_get_nodata(ctx, band), -1);
+        CHECK_EQUALS(rt_band_get_pixtype(band), PT_16BSI);
+        CHECK(!rt_band_is_offline(band));
+        CHECK(!rt_band_get_hasnodata_flag(band));
+        CHECK_EQUALS(rt_band_get_nodata(band), -1);
         
-        failure = rt_band_get_pixel(ctx, band, 0, 0, &val);
+        failure = rt_band_get_pixel(band, 0, 0, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, -1);
 
-        failure = rt_band_get_pixel(ctx, band, 1, 0, &val);
+        failure = rt_band_get_pixel(band, 1, 0, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, 0);
         
-        failure = rt_band_get_pixel(ctx, band, 2, 0, &val);
+        failure = rt_band_get_pixel(band, 2, 0, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, -16);
         
-        failure = rt_band_get_pixel(ctx, band, 0, 1, &val);
+        failure = rt_band_get_pixel(band, 0, 1, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, 127);
         
-        failure = rt_band_get_pixel(ctx, band, 1, 1, &val);
+        failure = rt_band_get_pixel(band, 1, 1, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, 10);
         
-        failure = rt_band_get_pixel(ctx, band, 2, 1, &val);
+        failure = rt_band_get_pixel(band, 2, 1, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, 2);
     }
 
-    out  = rt_raster_to_hexwkb(ctx, raster, &len);
+    out  = rt_raster_to_hexwkb(raster, &len);
     printf(" in hexwkb len: %u\n", strlen(hexwkb));
     printf("out hexwkb len: %u\n", len);
     CHECK_EQUALS(len, strlen(hexwkb));
@@ -388,7 +385,7 @@ main()
     CHECK( ! strcmp(hexwkb, out) );
 */
 
-    rt_raster_destroy(ctx, raster);
+    rt_raster_destroy(raster);
     free((/*no const*/ void*)out);
 
     /* ------------------------------------------------------ */
@@ -418,54 +415,54 @@ main()
 "0002"               /* pix(2,1) == 2 */
     ;
 
-    raster = rt_raster_from_hexwkb(ctx, hexwkb, strlen(hexwkb));
+    raster = rt_raster_from_hexwkb(hexwkb, strlen(hexwkb));
     CHECK(raster);
-    CHECK_EQUALS(rt_raster_get_num_bands(ctx, raster), 1);
-    CHECK_EQUALS(rt_raster_get_x_scale(ctx, raster), 1);
-    CHECK_EQUALS(rt_raster_get_y_scale(ctx, raster), 2);
-    CHECK_EQUALS(rt_raster_get_x_offset(ctx, raster), 3);
-    CHECK_EQUALS(rt_raster_get_y_offset(ctx, raster), 4);
-    CHECK_EQUALS(rt_raster_get_x_skew(ctx, raster), 5);
-    CHECK_EQUALS(rt_raster_get_y_skew(ctx, raster), 6);
-    CHECK_EQUALS(rt_raster_get_srid(ctx, raster), 10);
-    CHECK_EQUALS(rt_raster_get_width(ctx, raster), 3);
-    CHECK_EQUALS(rt_raster_get_height(ctx, raster), 2);
+    CHECK_EQUALS(rt_raster_get_num_bands(raster), 1);
+    CHECK_EQUALS(rt_raster_get_x_scale(raster), 1);
+    CHECK_EQUALS(rt_raster_get_y_scale(raster), 2);
+    CHECK_EQUALS(rt_raster_get_x_offset(raster), 3);
+    CHECK_EQUALS(rt_raster_get_y_offset(raster), 4);
+    CHECK_EQUALS(rt_raster_get_x_skew(raster), 5);
+    CHECK_EQUALS(rt_raster_get_y_skew(raster), 6);
+    CHECK_EQUALS(rt_raster_get_srid(raster), 10);
+    CHECK_EQUALS(rt_raster_get_width(raster), 3);
+    CHECK_EQUALS(rt_raster_get_height(raster), 2);
     {
         double val;
         int failure;
-        rt_band band = rt_raster_get_band(ctx, raster, 0);
+        rt_band band = rt_raster_get_band(raster, 0);
         CHECK(band);
-        CHECK_EQUALS(rt_band_get_pixtype(ctx, band), PT_16BSI);
-        CHECK(!rt_band_is_offline(ctx, band));
-        CHECK(!rt_band_get_hasnodata_flag(ctx,band));
-        CHECK_EQUALS(rt_band_get_nodata(ctx, band), -1);
+        CHECK_EQUALS(rt_band_get_pixtype(band), PT_16BSI);
+        CHECK(!rt_band_is_offline(band));
+        CHECK(!rt_band_get_hasnodata_flag(band));
+        CHECK_EQUALS(rt_band_get_nodata(band), -1);
 
-        failure = rt_band_get_pixel(ctx, band, 0, 0, &val);
+        failure = rt_band_get_pixel(band, 0, 0, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, -1);
 
-        failure = rt_band_get_pixel(ctx, band, 1, 0, &val);
+        failure = rt_band_get_pixel(band, 1, 0, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, 0);
         
-        failure = rt_band_get_pixel(ctx, band, 2, 0, &val);
+        failure = rt_band_get_pixel(band, 2, 0, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, -16);
         
-        failure = rt_band_get_pixel(ctx, band, 0, 1, &val);
+        failure = rt_band_get_pixel(band, 0, 1, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, 127);
         
-        failure = rt_band_get_pixel(ctx, band, 1, 1, &val);
+        failure = rt_band_get_pixel(band, 1, 1, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, 10);
         
-        failure = rt_band_get_pixel(ctx, band, 2, 1, &val);
+        failure = rt_band_get_pixel(band, 2, 1, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, 2);
     }
 
-    out  = rt_raster_to_hexwkb(ctx, raster, &len);
+    out  = rt_raster_to_hexwkb(raster, &len);
     printf(" in hexwkb len: %u\n", strlen(hexwkb));
     printf("out hexwkb len: %u\n", len);
     CHECK_EQUALS(len, strlen(hexwkb));
@@ -473,7 +470,7 @@ main()
     CHECK( ! strcmp(hexwkb, out) );
 */
 
-    rt_raster_destroy(ctx, raster);
+    rt_raster_destroy(raster);
     free((/*no const*/ void*)out);
 
     /* ------------------------------------------------------ */
@@ -500,32 +497,32 @@ main()
 "2F746D702F742E74696600" 
     ;
 
-    raster = rt_raster_from_hexwkb(ctx, hexwkb, strlen(hexwkb));
+    raster = rt_raster_from_hexwkb(hexwkb, strlen(hexwkb));
     CHECK(raster);
-    CHECK_EQUALS(rt_raster_get_num_bands(ctx, raster), 1);
-    CHECK_EQUALS(rt_raster_get_x_scale(ctx, raster), 1);
-    CHECK_EQUALS(rt_raster_get_y_scale(ctx, raster), 2);
-    CHECK_EQUALS(rt_raster_get_x_offset(ctx, raster), 3);
-    CHECK_EQUALS(rt_raster_get_y_offset(ctx, raster), 4);
-    CHECK_EQUALS(rt_raster_get_x_skew(ctx, raster), 5);
-    CHECK_EQUALS(rt_raster_get_y_skew(ctx, raster), 6);
-    CHECK_EQUALS(rt_raster_get_srid(ctx, raster), 10);
-    CHECK_EQUALS(rt_raster_get_width(ctx, raster), 3);
-    CHECK_EQUALS(rt_raster_get_height(ctx, raster), 2);
+    CHECK_EQUALS(rt_raster_get_num_bands(raster), 1);
+    CHECK_EQUALS(rt_raster_get_x_scale(raster), 1);
+    CHECK_EQUALS(rt_raster_get_y_scale(raster), 2);
+    CHECK_EQUALS(rt_raster_get_x_offset(raster), 3);
+    CHECK_EQUALS(rt_raster_get_y_offset(raster), 4);
+    CHECK_EQUALS(rt_raster_get_x_skew(raster), 5);
+    CHECK_EQUALS(rt_raster_get_y_skew(raster), 6);
+    CHECK_EQUALS(rt_raster_get_srid(raster), 10);
+    CHECK_EQUALS(rt_raster_get_width(raster), 3);
+    CHECK_EQUALS(rt_raster_get_height(raster), 2);
     {
-        rt_band band = rt_raster_get_band(ctx, raster, 0);
+        rt_band band = rt_raster_get_band(raster, 0);
         CHECK(band);
-        CHECK_EQUALS(rt_band_get_pixtype(ctx, band), PT_16BSI);
-        CHECK(rt_band_is_offline(ctx, band));
-        CHECK(rt_band_get_hasnodata_flag(ctx,band));
-        CHECK_EQUALS(rt_band_get_nodata(ctx, band), -1);
-        printf("ext band path: %s\n", rt_band_get_ext_path(ctx, band));
-        printf("ext band  num: %u\n", rt_band_get_ext_band_num(ctx, band));
-        CHECK( ! strcmp(rt_band_get_ext_path(ctx, band), "/tmp/t.tif"));
-        CHECK_EQUALS(rt_band_get_ext_band_num(ctx, band), 3);
+        CHECK_EQUALS(rt_band_get_pixtype(band), PT_16BSI);
+        CHECK(rt_band_is_offline(band));
+        CHECK(rt_band_get_hasnodata_flag(band));
+        CHECK_EQUALS(rt_band_get_nodata(band), -1);
+        printf("ext band path: %s\n", rt_band_get_ext_path(band));
+        printf("ext band  num: %u\n", rt_band_get_ext_band_num(band));
+        CHECK( ! strcmp(rt_band_get_ext_path(band), "/tmp/t.tif"));
+        CHECK_EQUALS(rt_band_get_ext_band_num(band), 3);
     }
 
-    out  = rt_raster_to_hexwkb(ctx, raster, &len);
+    out  = rt_raster_to_hexwkb(raster, &len);
     printf(" in hexwkb len: %u\n", strlen(hexwkb));
     printf("out hexwkb len: %u\n", len);
     CHECK_EQUALS(len, strlen(hexwkb));
@@ -533,7 +530,7 @@ main()
     CHECK( ! strcmp(hexwkb, out) );
 */
 
-    rt_raster_destroy(ctx, raster);
+    rt_raster_destroy(raster);
     free((/*no const*/ void*)out);
 
     /* ------------------------------------------------------ */
@@ -560,42 +557,42 @@ main()
 "AF01"             /* pix(2,0) == 431 */
     ;
 
-    raster = rt_raster_from_hexwkb(ctx, hexwkb, strlen(hexwkb));
+    raster = rt_raster_from_hexwkb(hexwkb, strlen(hexwkb));
     CHECK(raster);
-    CHECK_EQUALS(rt_raster_get_num_bands(ctx, raster), 1);
-    CHECK_EQUALS(rt_raster_get_x_scale(ctx, raster), 90);
-    CHECK_EQUALS(rt_raster_get_y_scale(ctx, raster), -90);
-    CHECK_EQUALS(rt_raster_get_x_offset(ctx, raster), 969870.0);
-    CHECK_EQUALS(rt_raster_get_y_offset(ctx, raster), 642930.0);
-    CHECK_EQUALS(rt_raster_get_x_skew(ctx, raster), 0);
-    CHECK_EQUALS(rt_raster_get_y_skew(ctx, raster), 0);
-    CHECK_EQUALS(rt_raster_get_srid(ctx, raster), -1);
-    CHECK_EQUALS(rt_raster_get_width(ctx, raster), 3);
-    CHECK_EQUALS(rt_raster_get_height(ctx, raster), 1);
+    CHECK_EQUALS(rt_raster_get_num_bands(raster), 1);
+    CHECK_EQUALS(rt_raster_get_x_scale(raster), 90);
+    CHECK_EQUALS(rt_raster_get_y_scale(raster), -90);
+    CHECK_EQUALS(rt_raster_get_x_offset(raster), 969870.0);
+    CHECK_EQUALS(rt_raster_get_y_offset(raster), 642930.0);
+    CHECK_EQUALS(rt_raster_get_x_skew(raster), 0);
+    CHECK_EQUALS(rt_raster_get_y_skew(raster), 0);
+    CHECK_EQUALS(rt_raster_get_srid(raster), -1);
+    CHECK_EQUALS(rt_raster_get_width(raster), 3);
+    CHECK_EQUALS(rt_raster_get_height(raster), 1);
     {
         double val;
         int failure;
-        rt_band band = rt_raster_get_band(ctx, raster, 0);
+        rt_band band = rt_raster_get_band(raster, 0);
         CHECK(band);
-        CHECK_EQUALS(rt_band_get_pixtype(ctx, band), PT_16BSI);
-        CHECK(!rt_band_is_offline(ctx, band));
-        CHECK(rt_band_get_hasnodata_flag(ctx,band));
-        CHECK_EQUALS(rt_band_get_nodata(ctx, band), 1);
+        CHECK_EQUALS(rt_band_get_pixtype(band), PT_16BSI);
+        CHECK(!rt_band_is_offline(band));
+        CHECK(rt_band_get_hasnodata_flag(band));
+        CHECK_EQUALS(rt_band_get_nodata(band), 1);
 
-        failure = rt_band_get_pixel(ctx, band, 0, 0, &val);
+        failure = rt_band_get_pixel(band, 0, 0, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, 1);
 
-        failure = rt_band_get_pixel(ctx, band, 1, 0, &val);
+        failure = rt_band_get_pixel(band, 1, 0, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, 436);
         
-        failure = rt_band_get_pixel(ctx, band, 2, 0, &val);
+        failure = rt_band_get_pixel(band, 2, 0, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, 431);
     }
 
-    out  = rt_raster_to_hexwkb(ctx, raster, &len);
+    out  = rt_raster_to_hexwkb(raster, &len);
 /*
     printf(" in hexwkb len: %d\n", strlen(hexwkb));
     printf("out hexwkb len: %d\n", len);
@@ -612,14 +609,14 @@ main()
         void *serialized;
         rt_raster rast2;
 
-        serialized = rt_raster_serialize(ctx, raster);
-        rast2 = rt_raster_deserialize(ctx, serialized);
+        serialized = rt_raster_serialize(raster);
+        rast2 = rt_raster_deserialize(serialized);
 
-        rt_raster_destroy(ctx, rast2);
+        rt_raster_destroy(rast2);
         free(serialized);
     }
 
-    rt_raster_destroy(ctx, raster);
+    rt_raster_destroy(raster);
 
     /* ------------------------------------------------------ */
     /* 5x5, little endian, 3 x band 8BUI (RGB),               */
@@ -654,46 +651,46 @@ main()
 "46566487A1506CA2E3FA5A6CAFFBFE4D566DA4CB3E454C5665" /* 3rd band pixels */
 ;
 
-    raster = rt_raster_from_hexwkb(ctx, hexwkb, strlen(hexwkb));
+    raster = rt_raster_from_hexwkb(hexwkb, strlen(hexwkb));
     CHECK(raster);
-    CHECK_EQUALS(rt_raster_get_num_bands(ctx, raster), 3);
-    CHECK_EQUALS_DOUBLE(rt_raster_get_x_scale(ctx, raster), 0.05);
-    CHECK_EQUALS_DOUBLE(rt_raster_get_y_scale(ctx, raster), -0.05);
-    CHECK_EQUALS_DOUBLE(rt_raster_get_x_offset(ctx, raster), 3427927.75);
-    CHECK_EQUALS_DOUBLE(rt_raster_get_y_offset(ctx, raster), 5793244.00);
-    CHECK_EQUALS_DOUBLE(rt_raster_get_x_skew(ctx, raster), 0.0);
-    CHECK_EQUALS_DOUBLE(rt_raster_get_y_skew(ctx, raster), 0.0);
-    CHECK_EQUALS(rt_raster_get_srid(ctx, raster), -1);
-    CHECK_EQUALS(rt_raster_get_width(ctx, raster), 5);
-    CHECK_EQUALS(rt_raster_get_height(ctx, raster), 5);
+    CHECK_EQUALS(rt_raster_get_num_bands(raster), 3);
+    CHECK_EQUALS_DOUBLE(rt_raster_get_x_scale(raster), 0.05);
+    CHECK_EQUALS_DOUBLE(rt_raster_get_y_scale(raster), -0.05);
+    CHECK_EQUALS_DOUBLE(rt_raster_get_x_offset(raster), 3427927.75);
+    CHECK_EQUALS_DOUBLE(rt_raster_get_y_offset(raster), 5793244.00);
+    CHECK_EQUALS_DOUBLE(rt_raster_get_x_skew(raster), 0.0);
+    CHECK_EQUALS_DOUBLE(rt_raster_get_y_skew(raster), 0.0);
+    CHECK_EQUALS(rt_raster_get_srid(raster), -1);
+    CHECK_EQUALS(rt_raster_get_width(raster), 5);
+    CHECK_EQUALS(rt_raster_get_height(raster), 5);
     {
         /* Test 1st band */
         double val;
         int failure;
-        rt_band band = rt_raster_get_band(ctx, raster, 0);
+        rt_band band = rt_raster_get_band(raster, 0);
         CHECK(band);
-        CHECK_EQUALS(rt_band_get_pixtype(ctx, band), PT_8BUI);
-        CHECK(!rt_band_is_offline(ctx, band));
-        CHECK(rt_band_get_hasnodata_flag(ctx,band));
-        CHECK_EQUALS(rt_band_get_nodata(ctx, band), 0);
+        CHECK_EQUALS(rt_band_get_pixtype(band), PT_8BUI);
+        CHECK(!rt_band_is_offline(band));
+        CHECK(rt_band_get_hasnodata_flag(band));
+        CHECK_EQUALS(rt_band_get_nodata(band), 0);
 
-        failure = rt_band_get_pixel(ctx, band, 0, 0, &val);
+        failure = rt_band_get_pixel(band, 0, 0, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, 253);
 
-        failure = rt_band_get_pixel(ctx, band, 1, 0, &val);
+        failure = rt_band_get_pixel(band, 1, 0, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, 254);
         
-        failure = rt_band_get_pixel(ctx, band, 2, 0, &val);
+        failure = rt_band_get_pixel(band, 2, 0, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, 253);
         
-        failure = rt_band_get_pixel(ctx, band, 3, 0, &val);
+        failure = rt_band_get_pixel(band, 3, 0, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, 254);
         
-        failure = rt_band_get_pixel(ctx, band, 4, 0, &val);
+        failure = rt_band_get_pixel(band, 4, 0, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, 254);
     }
@@ -702,30 +699,30 @@ main()
         /* Test 2nd band */
         double val;
         int failure;
-        rt_band band = rt_raster_get_band(ctx, raster, 1);
+        rt_band band = rt_raster_get_band(raster, 1);
         CHECK(band);
-        CHECK_EQUALS(rt_band_get_pixtype(ctx, band), PT_8BUI);
-        CHECK(!rt_band_is_offline(ctx, band));
-        CHECK(rt_band_get_hasnodata_flag(ctx,band));
-        CHECK_EQUALS(rt_band_get_nodata(ctx, band), 0);
+        CHECK_EQUALS(rt_band_get_pixtype(band), PT_8BUI);
+        CHECK(!rt_band_is_offline(band));
+        CHECK(rt_band_get_hasnodata_flag(band));
+        CHECK_EQUALS(rt_band_get_nodata(band), 0);
 
-        failure = rt_band_get_pixel(ctx, band, 0, 0, &val);
+        failure = rt_band_get_pixel(band, 0, 0, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, 78);
 
-        failure = rt_band_get_pixel(ctx, band, 1, 0, &val);
+        failure = rt_band_get_pixel(band, 1, 0, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, 98);
         
-        failure = rt_band_get_pixel(ctx, band, 2, 0, &val);
+        failure = rt_band_get_pixel(band, 2, 0, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, 122);
         
-        failure = rt_band_get_pixel(ctx, band, 3, 0, &val);
+        failure = rt_band_get_pixel(band, 3, 0, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, 173);
         
-        failure = rt_band_get_pixel(ctx, band, 4, 0, &val);
+        failure = rt_band_get_pixel(band, 4, 0, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, 209);
     }
@@ -734,35 +731,35 @@ main()
         /* Test 3rd band */
         double val;
         int failure;
-        rt_band band = rt_raster_get_band(ctx, raster, 2);
+        rt_band band = rt_raster_get_band(raster, 2);
         CHECK(band);
-        CHECK_EQUALS(rt_band_get_pixtype(ctx, band), PT_8BUI);
-        CHECK(!rt_band_is_offline(ctx, band));
-        CHECK(rt_band_get_hasnodata_flag(ctx,band));
-        CHECK_EQUALS(rt_band_get_nodata(ctx, band), 0);
+        CHECK_EQUALS(rt_band_get_pixtype(band), PT_8BUI);
+        CHECK(!rt_band_is_offline(band));
+        CHECK(rt_band_get_hasnodata_flag(band));
+        CHECK_EQUALS(rt_band_get_nodata(band), 0);
 
-        failure = rt_band_get_pixel(ctx, band, 0, 0, &val);
+        failure = rt_band_get_pixel(band, 0, 0, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, 70);
 
-        failure = rt_band_get_pixel(ctx, band, 1, 0, &val);
+        failure = rt_band_get_pixel(band, 1, 0, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, 86);
         
-        failure = rt_band_get_pixel(ctx, band, 2, 0, &val);
+        failure = rt_band_get_pixel(band, 2, 0, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, 100);
         
-        failure = rt_band_get_pixel(ctx, band, 3, 0, &val);
+        failure = rt_band_get_pixel(band, 3, 0, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, 135);
         
-        failure = rt_band_get_pixel(ctx, band, 4, 0, &val);
+        failure = rt_band_get_pixel(band, 4, 0, &val);
         CHECK(!failure);
         CHECK_EQUALS(val, 161);
     }
 
-    out  = rt_raster_to_hexwkb(ctx, raster, &len);
+    out  = rt_raster_to_hexwkb(raster, &len);
     printf(" in hexwkb len: %u\n", strlen(hexwkb));
     printf("out hexwkb len: %u\n", len);
     CHECK_EQUALS(len, strlen(hexwkb));
@@ -775,13 +772,13 @@ main()
         void *serialized;
         rt_raster rast2;
 
-        serialized = rt_raster_serialize(ctx, raster);
-        rast2 = rt_raster_deserialize(ctx, serialized);
+        serialized = rt_raster_serialize(raster);
+        rast2 = rt_raster_deserialize(serialized);
 
-        rt_raster_destroy(ctx, rast2);
+        rt_raster_destroy(rast2);
         free(serialized);
     }
-    rt_raster_destroy(ctx, raster);
+    rt_raster_destroy(raster);
     
     } /* for-loop running car5 tests */
 
@@ -807,3 +804,7 @@ lwgeom_init_allocators(void)
     lwgeom_install_default_allocators();
 }
 
+void rt_init_allocators(void)
+{
+    rt_install_default_allocators();
+}
\ No newline at end of file
index 53ad9b8915ee1fb6137788e1a4ee41a6c30e9bd1..aadb39e8b5a5a9a363d7a987635fea86208b85cc 100644 (file)
-WARNING:  Value set for 1BB band got clamped from -1.000000 to 0
+NOTICE:  Value set for 1BB band got clamped from -1.000000 to 0
 0
 0
 1
-WARNING:  Value set for 1BB band got clamped from 2.000000 to 1
+NOTICE:  Value set for 1BB band got clamped from 2.000000 to 1
 1
-WARNING:  Value set for 1BB band got clamped from 21.460000 to 1
+NOTICE:  Value set for 1BB band got clamped from 21.460000 to 1
 1
-WARNING:  Value set for 2BUI band got clamped from -1.000000 to 0
+NOTICE:  Value set for 2BUI band got clamped from -1.000000 to 0
 0
 0
 3
-WARNING:  Value set for 2BUI band got clamped from 4.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 4.000000 to 3
 3
-WARNING:  Value set for 2BUI band got clamped from 21.460000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 21.460000 to 3
 3
-WARNING:  Value set for 4BUI band got clamped from -1.000000 to 0
+NOTICE:  Value set for 4BUI band got clamped from -1.000000 to 0
 0
 0
 15
-WARNING:  Value set for 4BUI band got clamped from 16.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 16.000000 to 15
 15
-WARNING:  Value set for 4BUI band got clamped from 21.460000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 21.460000 to 15
 15
-WARNING:  Value set for 8BSI band got clamped from -129.000000 to -128
+NOTICE:  Value set for 8BSI band got clamped from -129.000000 to -128
 -128
 -128
 0
 127
-WARNING:  Value set for 8BSI band got clamped from 128.000000 to 127
+NOTICE:  Value set for 8BSI band got clamped from 128.000000 to 127
 127
-WARNING:  Value set for 8BSI band got truncated from 21.460000 to 21
+NOTICE:  Value set for 8BSI band got truncated from 21.460000 to 21
 21
-WARNING:  Value set for 8BSI band got clamped from 210.460000 to 127
+NOTICE:  Value set for 8BSI band got clamped from 210.460000 to 127
 127
-WARNING:  Value set for 8BUI band got clamped from -1.000000 to 0
+NOTICE:  Value set for 8BUI band got clamped from -1.000000 to 0
 0
 0
 255
-WARNING:  Value set for 8BUI band got clamped from 256.000000 to 255
+NOTICE:  Value set for 8BUI band got clamped from 256.000000 to 255
 255
-WARNING:  Value set for 8BUI band got truncated from 21.460000 to 21
+NOTICE:  Value set for 8BUI band got truncated from 21.460000 to 21
 21
-WARNING:  Value set for 8BUI band got clamped from 410.460000 to 255
+NOTICE:  Value set for 8BUI band got clamped from 410.460000 to 255
 255
-WARNING:  Value set for 8BUI band got truncated from 256.000000 to 255
+NOTICE:  Value set for 8BUI band got truncated from 256.000000 to 255
 255
-WARNING:  Value set for 16BSI band got clamped from -32769.000000 to -32768
+NOTICE:  Value set for 16BSI band got clamped from -32769.000000 to -32768
 -32768
 -32768
 0
 32767
-WARNING:  Value set for 16BSI band got clamped from 32768.000000 to 32767
+NOTICE:  Value set for 16BSI band got clamped from 32768.000000 to 32767
 32767
-WARNING:  Value set for 16BSI band got truncated from 21.460000 to 21
+NOTICE:  Value set for 16BSI band got truncated from 21.460000 to 21
 21
-WARNING:  Value set for 16BSI band got clamped from 210000.460000 to 32767
+NOTICE:  Value set for 16BSI band got clamped from 210000.460000 to 32767
 32767
-WARNING:  Value set for 16BUI band got clamped from -1.000000 to 0
+NOTICE:  Value set for 16BUI band got clamped from -1.000000 to 0
 0
 0
 65535
-WARNING:  Value set for 16BUI band got clamped from 65537.000000 to 65535
+NOTICE:  Value set for 16BUI band got clamped from 65537.000000 to 65535
 65535
-WARNING:  Value set for 16BUI band got truncated from 21.460000 to 21
+NOTICE:  Value set for 16BUI band got truncated from 21.460000 to 21
 21
-WARNING:  Value set for 16BUI band got clamped from 210000.464564 to 65535
+NOTICE:  Value set for 16BUI band got clamped from 210000.464564 to 65535
 65535
-WARNING:  Value set for 32BSI band got clamped from -2147483649.000000 to -2147483648
+NOTICE:  Value set for 32BSI band got clamped from -2147483649.000000 to -2147483648
 -2147483648
 -2147483648
 0
 2147483647
-WARNING:  Value set for 32BSI band got clamped from 2147483648.000000 to 2147483647
+NOTICE:  Value set for 32BSI band got clamped from 2147483648.000000 to 2147483647
 2147483647
-WARNING:  Value set for 32BSI band got truncated from 21.460000 to 21
+NOTICE:  Value set for 32BSI band got truncated from 21.460000 to 21
 21
-WARNING:  Value set for 32BSI band got truncated from 210000.464564 to 210000
+NOTICE:  Value set for 32BSI band got truncated from 210000.464564 to 210000
 210000
-WARNING:  Value set for 32BUI band got clamped from -1.000000 to 0
+NOTICE:  Value set for 32BUI band got clamped from -1.000000 to 0
 0
 0
 4294967295
-WARNING:  Value set for 32BUI band got clamped from 4294967296.000000 to 4294967295
+NOTICE:  Value set for 32BUI band got clamped from 4294967296.000000 to 4294967295
 4294967295
-WARNING:  Value set for 32BUI band got clamped from 214294967296.000000 to 4294967295
+NOTICE:  Value set for 32BUI band got clamped from 214294967296.000000 to 4294967295
 4294967295
-WARNING:  Value set for 32BUI band got truncated from 21.460000 to 21
+NOTICE:  Value set for 32BUI band got truncated from 21.460000 to 21
 21
-WARNING:  Value set for 32BUI band got clamped from 4294967296.000000 to 4294967295
+NOTICE:  Value set for 32BUI band got clamped from 4294967296.000000 to 4294967295
 4294967295
 0
-WARNING:  Value set for 32BF band got converted from 4294967000.000000 to 4294967040.000000
+NOTICE:  Value set for 32BF band got converted from 4294967000.000000 to 4294967040.000000
 4294967040
-WARNING:  Value set for 32BF band got converted from 4294967000.000000 to 4294967040.000000
+NOTICE:  Value set for 32BF band got converted from 4294967000.000000 to 4294967040.000000
 4.29497e+9
-WARNING:  Value set for 32BF band got converted from 4294967295.000000 to 4294967296.000000
+NOTICE:  Value set for 32BF band got converted from 4294967295.000000 to 4294967296.000000
 4294967296
-WARNING:  Value set for 32BF band got converted from 4294967295.000000 to 4294967296.000000
+NOTICE:  Value set for 32BF band got converted from 4294967295.000000 to 4294967296.000000
 4.29497e+9
 4294967296
 4.29497e+9
-WARNING:  Value set for 32BF band got converted from 21.460000 to 21.459999
+NOTICE:  Value set for 32BF band got converted from 21.460000 to 21.459999
 21.4599990844727
-WARNING:  Value set for 32BF band got converted from 21.460000 to 21.459999
+NOTICE:  Value set for 32BF band got converted from 21.460000 to 21.459999
 21.46
-WARNING:  Value set for 32BF band got converted from 21003.100000 to 21003.099609
+NOTICE:  Value set for 32BF band got converted from 21003.100000 to 21003.099609
 21003.099609375
-WARNING:  Value set for 32BF band got converted from 21003.100000 to 21003.099609
+NOTICE:  Value set for 32BF band got converted from 21003.100000 to 21003.099609
 21003.1
-WARNING:  Value set for 32BF band got converted from 123.456000 to 123.456001
+NOTICE:  Value set for 32BF band got converted from 123.456000 to 123.456001
 123.456001281738
-WARNING:  Value set for 32BF band got converted from 123.456000 to 123.456001
+NOTICE:  Value set for 32BF band got converted from 123.456000 to 123.456001
 123.456
-WARNING:  Value set for 32BF band got converted from 1234.567000 to 1234.567017
+NOTICE:  Value set for 32BF band got converted from 1234.567000 to 1234.567017
 1234.56701660156
-WARNING:  Value set for 32BF band got converted from 1234.567000 to 1234.567017
+NOTICE:  Value set for 32BF band got converted from 1234.567000 to 1234.567017
 1234.57
-WARNING:  Value set for 32BF band got converted from 210000.464564 to 210000.468750
+NOTICE:  Value set for 32BF band got converted from 210000.464564 to 210000.468750
 210000.46875
-WARNING:  Value set for 32BF band got converted from 210000.464564 to 210000.468750
+NOTICE:  Value set for 32BF band got converted from 210000.464564 to 210000.468750
 210000
 -1
 0
@@ -129,12 +129,12 @@ WARNING:  Value set for 32BF band got converted from 210000.464564 to 210000.468
 1234.567
 210000.464564365
 1234.46456436475
-WARNING:  rt_raster_copy_band: Second raster has no band
+NOTICE:  rt_raster_copy_band: Second raster has no band
 NOTICE:  RASTER_copyband: Could not add band to raster. Returning original raster.
 1234.5678
-WARNING:  rt_raster_copy_band: Second raster has no band
+NOTICE:  rt_raster_copy_band: Second raster has no band
 NOTICE:  RASTER_copyband: Could not add band to raster. Returning original raster.
 1234.5678
-WARNING:  rt_raster_copy_band: Second raster has no band
+NOTICE:  rt_raster_copy_band: Second raster has no band
 NOTICE:  RASTER_copyband: Could not add band to raster. Returning original raster.
 1234.5678
index 09f225f99aa353e55733c3b9c6708ad547c82785..42c2a8fdaa22e8d93e424d3212a2f418b8e2130d 100644 (file)
@@ -6,212 +6,212 @@ NOTICE:  Raster do not have the required band. Returning a raster without a band
 t
 |19
 |1
-WARNING:  rt_raster_copy_band: Second raster has no band
+NOTICE:  rt_raster_copy_band: Second raster has no band
 NOTICE:  Could not find raster band of index 1 when getting pixel value. Returning NULL
 |
-WARNING:  rt_raster_copy_band: Second raster has no band
+NOTICE:  rt_raster_copy_band: Second raster has no band
 NOTICE:  Could not find raster band of index 1 when getting pixel value. Returning NULL
 |
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
-WARNING:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
+NOTICE:  Value set for 4BUI band got clamped from 120.000000 to 15
 100|
 100|120
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
-WARNING:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
+NOTICE:  Value set for 2BUI band got clamped from 121.000000 to 3
 101|