Changeset: 08904140b193 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=08904140b193
Modified Files:
        sql/backends/monet5/vaults/geotiff/geotiff.c
        sql/backends/monet5/vaults/geotiff/geotiff.h
Branch: sciql
Log Message:

Updated the GeoTIFF data vault code to take into account that CREATE ARRAY does
not materialise the array any more.

Next to loading the pixel intensities, the implementation of rs.import now also
needs to create BATs with values for the x,y dimensions.


diffs (truncated from 424 to 300 lines):

diff --git a/sql/backends/monet5/vaults/geotiff/geotiff.c 
b/sql/backends/monet5/vaults/geotiff/geotiff.c
--- a/sql/backends/monet5/vaults/geotiff/geotiff.c
+++ b/sql/backends/monet5/vaults/geotiff/geotiff.c
@@ -23,6 +23,7 @@
 #include "sql.h"
 #include "sql_scenario.h"
 #include "mal_exception.h"
+#include "array.h"
 #include <xtiffio.h>  /* for TIFF */
 
 
@@ -32,14 +33,13 @@
  * adapted accordingly.
  */
 
+/* CURRENT_TIMESTAMP() ?*/
 #define INSFILE "INSERT INTO rs.files(fileid,location,status,lastmodified) \
         VALUES(%d, '%s', %d, CURRENT_TIMESTAMP());"
 #define INSCAT "INSERT INTO rs.catalog(imageid,fileid,width,length,bps) \
         VALUES(%d, %d, %d, %d, %d);"
-#define CRTIMAGE "CREATE ARRAY %s (x int dimension[%d], \
-       y int dimension[%d], v %s);"
-
-/* CURRENT_TIMESTAMP() ?*/
+#define CRT_GREYSCALE_IMAGE "CREATE ARRAY %s (x INT DIMENSION[%d], \
+       y INT DIMENSION[%d], intensity %s);"
 
 str
 GTIFFtest(int *wid, int *len, str *fname)
@@ -59,7 +59,6 @@ GTIFFtest(int *wid, int *len, str *fname
 }
 
 /* attach a single geotiff file given its name, fill in geotiff catalog tables 
*/
-
 str
 GTIFFattach(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
 {
@@ -109,7 +108,6 @@ GTIFFattach(Client cntxt, MalBlkPtr mb, 
        if ( ( msg = SQLstatementIntern(cntxt,&s,"geotiff.attach",TRUE,FALSE)) 
!= MAL_SUCCEED)
                goto finish;
 
-
        /* add row in the rs.catalog catalog table */
        col = mvc_bind_column(m, cat, "imageid");
        imid = store_funcs.count_col(col, 1) + 1;
@@ -120,91 +118,150 @@ GTIFFattach(Client cntxt, MalBlkPtr mb, 
        snprintf(buf, BUFSIZ, INSCAT, (int)imid, (int)fid, wid, len, bps);
        msg = SQLstatementIntern(cntxt,&s,"geotiff.attach",TRUE,FALSE);
 
- finish:
+finish:
        /* if (msg != MAL_SUCCEED){
-               snprintf(buf, BUFSIZ,"ROLLBACK;");
-               SQLstatementIntern(cntxt,&s,"geotiff.attach",TRUE,FALSE));
-       }*/
+          snprintf(buf, BUFSIZ,"ROLLBACK;");
+          SQLstatementIntern(cntxt,&s,"geotiff.attach",TRUE,FALSE));
+          }*/
        XTIFFClose(tif);
        return msg;
 }
 
+#define LOAD_GREY_CLEANUP() \
+{\
+       if (tif) XTIFFClose(tif); \
+       if (linebuf) GDKfree(linebuf); \
+       if (resX) BBPunfix(resX->batCacheid); \
+       if (resY) BBPunfix(resX->batCacheid); \
+       /* FIXME: is this correct, if we haven't doen any BBPkeepref yet */ \
+       if (resI) BBPunfix(resX->batCacheid); \
+}
+
 str
-GTIFFloadImage(bat *result, str *fname)
+GTIFFloadGreyscaleImage(bat *x, bat *y, bat *intensity, str *fname)
 {
        TIFF *tif = (TIFF*)0;
-       int  wid = 0, len = 0;
+       int  bid = 0, strt = 0, step = 1, rep1 = 1, wid = 0, len = 0;
        BUN pixels = BUN_NONE;
        sht photoint, bps;
        tsize_t i, j;
        void *linebuf = NULL;
-       sht *data_sht = NULL;
-       int *data_int = NULL;
-       BAT *res;
-
+       BAT *resX = NULL, *resY = NULL, *resI = NULL;
 
        tif = XTIFFOpen(*fname, "r");
        if (!tif)
                return createException(MAL, "geotiff.loadimage", "Missing 
GEOTIFF file %s\n", *fname);
 
-       TIFFGetField(tif,TIFFTAG_PHOTOMETRIC, &photoint);
-       if ( photoint > 1 ){
+       TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &wid);
+       TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &len);
+       TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &bps);
+       TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photoint);
+       if (photoint > 1){
                XTIFFClose(tif);
                return createException(MAL, "geotiff.loadimage", "Currently 
only support of greyscale images.\n");
        }
-
-       TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &wid);
-       TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &len);
-       TIFFGetField(tif,TIFFTAG_BITSPERSAMPLE, &bps);
-
        pixels = (BUN)wid * (BUN)len;
 
-       /* allocate res BAT */
+       /* Manually compute values for the X-dimension, since we know that its
+        * range is [strt:step:wid] and each of its value must be repeated 'len'
+        * times with 1 #repeats */
+       ARRAYseries_int(&bid, &strt, &step, &wid, &len, &rep1);
+       resX = BATdescriptor(bid);
+       if (resX == NULL) {
+               XTIFFClose(tif);
+               return createException(MAL, "geotiff.loadimage", "Failed to 
load the X-dimension of %s", *fname);
+       }
+       if (BATcount(resX) != pixels) {
+               XTIFFClose(tif);
+               BBPunfix(resX->batCacheid);
+               return createException(MAL, "geotiff.loadimage", "X-dimension 
has invalid number of pixels: " BUNFMT " (!= " BUNFMT ")", BATcount(resX), 
pixels);
+       }
+
+       /* Manually compute values for the Y-dimension, since we know that its
+        * range is [strt:step:len] and each of its value must be repeated 1 
times
+        * with 'wid' #repeats */
+       ARRAYseries_int(&bid, &strt, &step, &len, &rep1, &wid);
+       resY = BATdescriptor(bid);
+       if (resY == NULL) {
+               XTIFFClose(tif);
+               return createException(MAL, "geotiff.loadimage", "Failed to 
load the Y-dimension of %s", *fname);
+       }
+       if (BATcount(resY) != pixels) {
+               XTIFFClose(tif);
+               BBPunfix(resX->batCacheid);
+               BBPunfix(resY->batCacheid);
+               return createException(MAL, "geotiff.loadimage", "Y-dimension 
has invalid number of pixels: " BUNFMT " (!= " BUNFMT ")", BATcount(resY), 
pixels);
+       }
+
+       /* Read the pixel intensities from the GeoTIFF file and fill in the BAT 
*/
        switch (bps/8){ /* sacrifice some storage to avoid values become signed 
*/
        case sizeof(bte):
-               res = BATnew(TYPE_void, TYPE_sht, pixels);
+       {
+               sht *data_sht = NULL;
+
+               resI = BATnew(TYPE_void, TYPE_sht, pixels);
+               if (resI == NULL) {
+                       LOAD_GREY_CLEANUP();
+                       return createException(MAL, "geotiff.loadimage", 
MAL_MALLOC_FAIL);
+               }
+
                linebuf = GDKmalloc(wid); /* buffer for one line of image */
-               data_sht = (sht *) Tloc(res, BUNfirst(res));
-               /* read data */
+               if (linebuf == NULL) {
+                       LOAD_GREY_CLEANUP();
+                       return createException(MAL, "geotiff.loadimage", 
MAL_MALLOC_FAIL);
+               }
+
+               data_sht = (sht *)Tloc(resI, BUNfirst(resI));
                for( i = 0; i < len; i++){
                        if (TIFFReadScanline(tif, linebuf, i, 0) != -1) {
-                               for (j = 0; j < wid; j++)
+                               for (j = 0; j < wid; j++) 
                                        data_sht[j*len+i] = ((unsigned 
char*)linebuf)[j];
                        }
                }
                break;
+       }
        case sizeof(sht):
-               res = BATnew(TYPE_void, TYPE_int, pixels);
-               linebuf = GDKmalloc(wid * 2); /* buffer for one line of image */
-               data_int = (int *) Tloc(res, BUNfirst(res));
-               /* read data */
+       {
+               int *data_int = NULL;
+
+               resI = BATnew(TYPE_void, TYPE_int, pixels);
+               if (resI == NULL) {
+                       LOAD_GREY_CLEANUP();
+                       return createException(MAL, "geotiff.loadimage", 
MAL_MALLOC_FAIL);
+               }
+
+               linebuf = GDKmalloc(wid*2); /* buffer for one line of image */
+               if (linebuf == NULL) {
+                       LOAD_GREY_CLEANUP();
+                       return createException(MAL, "geotiff.loadimage", 
MAL_MALLOC_FAIL);
+               }
+
+               data_int = (int *)Tloc(resI, BUNfirst(resI));
                for( i = 0; i < len; i++){
                        if (TIFFReadScanline(tif, linebuf, i, 0) != -1) {
-                               for (j = 0; j < wid; j++)
-                                       data_int[j*len+i] = ((unsigned short 
*)linebuf)[j];
+                               for (j = 0; j < wid; j++) 
+                                       data_int[j*len+i] = ((unsigned 
short*)linebuf)[j];
                        }
                }
                break;
+       }
        default:
-               XTIFFClose(tif);
+               LOAD_GREY_CLEANUP();
                return createException(MAL, "geotiff.loadimage", "Unexpected 
BitsPerSample: %d not in {8,16}", bps);
        }
-       if ( res == NULL ) {
-               XTIFFClose(tif);
-               return createException(MAL, "geotiff.loadimage", 
MAL_MALLOC_FAIL);
-       }
-
-       /* set result BAT properties */
-       BATsetcount(res, pixels);
-       BATseqbase(res, 0);
-       BATkey(res, TRUE);
-       res->T->nonil = TRUE;
-       res->T->nil = FALSE;
-       res->tsorted = FALSE;
-
-       BBPkeepref(*result = res->batCacheid);
        XTIFFClose(tif);
        GDKfree(linebuf);
+       BATsetcount(resI, pixels);
+       BATseqbase(resI, 0);
+       BATkey(resI, TRUE);
+       resI->T->nonil = TRUE;
+       resI->T->nil = FALSE;
+       resI->tsorted = FALSE;
+       BBPkeepref(resI->batCacheid);
+
+       *x = resX->batCacheid;
+       *y = resY->batCacheid;
+       *intensity = resI->batCacheid;
        return MAL_SUCCEED;
 }
 
@@ -215,87 +272,134 @@ GTIFFimportImage(Client cntxt, MalBlkPtr
 {
        mvc *m = NULL;
        sql_schema *sch = NULL;
-       sql_table *fls = NULL, *cat = NULL, *arr = NULL;
-       sql_column *col;
+       sql_table *fls = NULL, *cat = NULL, *img = NULL;
+       sql_column *col = NULL;
        oid irid = oid_nil, frid = oid_nil;
        str msg = MAL_SUCCEED, fname = NULL;
        int imageid = *(int*)getArgReference(stk, pci, 1);
        int  wid = 0, len = 0, fid;
        sht bps;
        char aname[20], buf[BUFSIZ], *s = buf;
-       BAT *b;
        bte stat = 0;
-       bat res;
+       bat x, y, intensity;
 
        msg = getSQLContext(cntxt, mb, &m, NULL);
        if (msg)
                return msg;
 
        sch = mvc_bind_schema(m, "rs");
-       if ( !sch )
+       if (sch == NULL)
                return createException(MAL, "geotiff.import", "Schema rs 
missing\n");
 
        fls = mvc_bind_table(m, sch, "files");
        cat = mvc_bind_table(m, sch, "catalog");
-       if (fls == NULL || cat == NULL )
-               return createException(MAL, "geotiff.import", "Catalog table 
missing\n");
+       if (fls == NULL || cat == NULL)
+               return createException(MAL, "geotiff.import", "Catalog tables 
\"files\" or \"catalog\" missing\n");
 
        /* get fileid */
        col = mvc_bind_column(m, cat, "imageid");
+       if (col == NULL)
+               return createException(MAL, "geotiff.import", "Could not find 
\"catalog\".\"imageid\"\n");
        irid = table_funcs.column_find_row(m->session->tr, col, (void 
*)&imageid, NULL);
-       if (irid == oid_nil) {
-               msg = createException(MAL, "geotiff.import", "Image %d not in 
the catalog\n", imageid);
-               return msg;
-       }
+       if (irid == oid_nil) 
+               return createException(MAL, "geotiff.import", "Image %d not in 
the GeoTIFF \"catalog\"\n", imageid);
 
        col = mvc_bind_column(m, cat, "fileid");
+       if (col == NULL)
+               return createException(MAL, "geotiff.import", "Could not find 
\"catalog\".\"fileid\"\n");
        fid = *(int*)table_funcs.column_find_value(m->session->tr, col, irid);
-
+       if (fid < 0) 
+               return createException(MAL, "geotiff.import", "File ID of image 
%d not in the GeoTIFF \"files\"\n", imageid);
        /* check status of the image file */
-       col = mvc_bind_column(m, fls, "fileid");
        frid = table_funcs.column_find_row(m->session->tr, col, (void *)&fid, 
NULL);
        col = mvc_bind_column(m, fls, "status");
+       if (col == NULL)
+               return createException(MAL, "geotiff.import", "Could not find 
\"files\".\"status\"\n");
        stat = *(bte *)table_funcs.column_find_value(m->session->tr, col, frid);
_______________________________________________
checkin-list mailing list
[email protected]
http://mail.monetdb.org/mailman/listinfo/checkin-list

Reply via email to