]> granicus.if.org Git - postgis/commitdiff
Simplify code underlying ST_AddBand(raster, raster[])
authorBborie Park <bkpark at ucdavis.edu>
Mon, 6 Aug 2012 15:02:50 +0000 (15:02 +0000)
committerBborie Park <bkpark at ucdavis.edu>
Mon, 6 Aug 2012 15:02:50 +0000 (15:02 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@10167 b70326c6-7e19-0410-871a-916f4a2858ee

raster/rt_pg/rt_pg.c

index be740b5faf00d22b8710125e51413e734909404e..837c08b341b59f21d0ebc88ceb42d26ff34b9afe 100644 (file)
@@ -4329,33 +4329,38 @@ Datum RASTER_addBandRasterArray(PG_FUNCTION_ARGS)
        deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
                &nulls, &n);
 
-       /* quick check that the source band number is valid for all rasters */
+       /* decrement srcnband and dstnband by 1, now 0-based */
+       srcnband--;
+       dstnband--;
+       POSTGIS_RT_DEBUGF(4, "0-based nband (src, dst) = (%d, %d)", srcnband, dstnband);
+
+       /* time to copy bands */
        for (i = 0; i < n; i++) {
                if (nulls[i]) continue;
                src = NULL;
 
-               pgsrc = (rt_pgraster *) PG_DETOAST_DATUM_SLICE(e[i], 0, sizeof(struct rt_raster_serialized_t));
-               src = rt_raster_deserialize(pgsrc, TRUE);
+               pgsrc = (rt_pgraster *) PG_DETOAST_DATUM(e[i]);
+               src = rt_raster_deserialize(pgsrc, FALSE);
                if (src == NULL) {
                        elog(ERROR, "RASTER_addBandRasterArray: Could not deserialize source raster at index %d", i + 1);
                        pfree(nulls);
                        pfree(e);
-                       if (raster != NULL) {
+                       if (raster != NULL)
                                rt_raster_destroy(raster);
+                       if (pgraster != NULL)
                                PG_FREE_IF_COPY(pgraster, 0);
-                       }
                        PG_RETURN_NULL();
                }
 
                srcnumbands = rt_raster_get_num_bands(src);
-               rt_raster_destroy(src);
                POSTGIS_RT_DEBUGF(4, "source raster %d has %d bands", i + 1, srcnumbands);
 
                /* band index isn't valid */
-               if (srcnband > srcnumbands) {
+               if (srcnband > srcnumbands - 1) {
                        elog(NOTICE, "Invalid band index for source raster at index %d.  Returning original raster", i + 1);
                        pfree(nulls);
                        pfree(e);
+                       rt_raster_destroy(src);
                        if (raster != NULL) {
                                rt_raster_destroy(raster);
                                PG_RETURN_POINTER(pgraster);
@@ -4363,30 +4368,6 @@ Datum RASTER_addBandRasterArray(PG_FUNCTION_ARGS)
                        else
                                PG_RETURN_NULL();
                }
-       }
-
-       /* decrement srcnband and dstnband by 1, now 0-based */
-       srcnband--;
-       dstnband--;
-       POSTGIS_RT_DEBUGF(4, "0-based nband (src, dst) = (%d, %d)", srcnband, dstnband);
-
-       /* still here, time to copy bands */
-       for (i = 0; i < n; i++) {
-               if (nulls[i]) continue;
-               src = NULL;
-
-               pgsrc = (rt_pgraster *) PG_DETOAST_DATUM(e[i]);
-               src = rt_raster_deserialize(pgsrc, FALSE);
-               if (src == NULL) {
-                       elog(ERROR, "RASTER_addBandRasterArray: Could not deserialize source raster at index %d", i + 1);
-                       pfree(nulls);
-                       pfree(e);
-                       if (raster != NULL)
-                               rt_raster_destroy(raster);
-                       if (pgraster != NULL)
-                               PG_FREE_IF_COPY(pgraster, 0);
-                       PG_RETURN_NULL();
-               }
 
                /* destination raster is empty, new raster */
                if (raster == NULL) {