Index: libraries/thuban/gdalwarp.cpp
===================================================================
RCS file: /thubanrepository/thuban/libraries/thuban/gdalwarp.cpp,v
retrieving revision 1.2
diff -u -r1.2 gdalwarp.cpp
--- libraries/thuban/gdalwarp.cpp	21 Jan 2005 14:01:25 -0000	1.2
+++ libraries/thuban/gdalwarp.cpp	24 Jan 2005 22:55:16 -0000
@@ -125,6 +125,8 @@
 static double	       dfMinX=0.0, dfMinY=0.0, dfMaxX=0.0, dfMaxY=0.0;
 static double	       dfXRes=0.0, dfYRes=0.0;
 static int             nForcePixels=0, nForceLines=0;
+static int             hasMask = 0;
+static int             maskRGB[3] = {0,0,0};
 
 static PyMethodDef gdalwarp_methods[] = {
     { "ProjectRasterFile", ProjectRasterFile, METH_VARARGS },
@@ -207,7 +209,6 @@
 {
     CPLErr ret = CE_None;
 
-    GDALColorEntry color;
     GDALColorTable *pal = NULL;
 
     ds->FlushCache();
@@ -229,9 +230,10 @@
     memset(*imgbuf, 170, *imglen);
 
     //
-    // if there are three bands assume for now that they are RGB bands
+    // if there are three or more bands assume that the first three
+    // are for RGB, unless told otherwise 
     //
-    if (rasterCount == 3)
+    if (rasterCount >= 3)
     {
         for (int i=1; i <= rasterCount; i++)
         {
@@ -240,10 +242,11 @@
 
             switch (band->GetColorInterpretation())
             {
+                case GCI_Undefined: offs = i-1; break;
                 case GCI_RedBand:   offs = 1; break;
                 case GCI_GreenBand: offs = 2; break;
                 case GCI_BlueBand:  offs = 3; break;
-                default:            offs = i-1; break;
+                default:            offs = -1; break;
             }
 
             //
@@ -251,12 +254,15 @@
             // so we first copy over all Red values, then all Green
             // values, and then all Blue values
             //
-            ret = band->RasterIO(GF_Read, 0, 0, 
-                                 nRasterXSize, nRasterYSize,
-                                 *imgbuf+offs, nRasterXSize, nRasterYSize, 
-                                 GDT_Byte, 3, 0);
-                    
-            if (ret == CE_Failure) break;
+
+            if (0 <= offs && offs < 3)
+            {
+                ret = band->RasterIO(GF_Read, 0, 0, 
+                                     nRasterXSize, nRasterYSize,
+                                     *imgbuf+offs, nRasterXSize, nRasterYSize, 
+                                     GDT_Byte, 3, 0);
+                if (ret == CE_Failure) break;
+            }
         }
     }
     else if (rasterCount == 1)
@@ -275,8 +281,11 @@
                 // FIXME: what happens if pal is NULL? ignore?
                 
                 // test this once to see if conversion is supported. 
-                if (pal && pal->GetColorEntryAsRGB(0, &color))
+                if (pal != NULL)
                 {
+                    GDALPaletteInterp pal_interp 
+                        = pal->GetPaletteInterpretation();
+
                     //
                     // copy over all the palette indices and then
                     // loop through the buffer replacing the values
@@ -285,7 +294,7 @@
                     ret = band->RasterIO(GF_Read, 0, 0, 
                                          nRasterXSize, nRasterYSize,
                                          *imgbuf, nRasterXSize, nRasterYSize, 
-                                         GDT_Byte, 3, 0);
+                                         GDT_UInt16, 3, 0);
                             
                     if (ret == CE_Failure) break;
 
@@ -293,19 +302,32 @@
                          data != (*imgbuf+*imglen);
                          data += 3)
                     {
-                        if (*data == NO_DATA_VAL)
+
+                        unsigned short int val = *((unsigned short int *)data);
+
+                        if (hasMask && val == maskRGB[0])
                         {
-                            *(data + 0) = 3;
-                            *(data + 1) = 1;
-                            *(data + 2) = 4;
+                            *(data + 0) = maskRGB[0];
+                            *(data + 1) = maskRGB[1];
+                            *(data + 2) = maskRGB[2];
                         }
                         else
                         {
-                            pal->GetColorEntryAsRGB(*data, &color);
+                            const GDALColorEntry *color 
+                                = pal->GetColorEntry(val);
 
-                            *(data + 0) = color.c1;
-                            *(data + 1) = color.c2;
-                            *(data + 2) = color.c3;
+                            if (pal_interp == GPI_Gray)
+                            {
+                                *(data + 0) = color->c1;
+                                *(data + 1) = color->c1;
+                                *(data + 2) = color->c1;
+                            }
+                            else
+                            {
+                                *(data + 0) = color->c1;
+                                *(data + 1) = color->c2;
+                                *(data + 2) = color->c3;
+                            }
                         }
                     }
                 }
@@ -330,11 +352,11 @@
                      data != (*imgbuf+*imglen);
                      data += 3)
                 {
-                    if (*data == NO_DATA_VAL)
+                    if (hasMask && *data == maskRGB[0])
                     {
-                        *(data + 0) = 3;
-                        *(data + 1) = 1;
-                        *(data + 2) = 4;
+                        *(data + 0) = maskRGB[0];
+                        *(data + 1) = maskRGB[1];
+                        *(data + 2) = maskRGB[2];
                     }
                     else
                     {
@@ -388,11 +410,13 @@
     PyObject * extents;
     PyObject * resolution;
     PyObject * imageRes;
+    PyObject * mask;
 
-    if (!PyArg_ParseTuple(args, "OOOOOO", &filename, &srcImageArgs, 
+    if (!PyArg_ParseTuple(args, "OOOOOOO", &filename, &srcImageArgs, 
                 &dstImageArgs, 
                 &extents, 
-                &resolution, &imageRes))
+                &resolution, &imageRes,
+                &mask))
     {
         return NULL;
     }
@@ -413,6 +437,15 @@
     nForcePixels = ( int )PyInt_AsLong( PyTuple_GetItem( imageRes, 0 ) );
     nForceLines  = ( int )PyInt_AsLong( PyTuple_GetItem( imageRes, 1 ) );
 
+    hasMask = mask != Py_None;
+
+    if (hasMask)
+    {
+        maskRGB[0]  = ( int )PyInt_AsLong( PyTuple_GetItem( mask, 0 ) );
+        maskRGB[1]  = ( int )PyInt_AsLong( PyTuple_GetItem( mask, 1 ) );
+        maskRGB[2]  = ( int )PyInt_AsLong( PyTuple_GetItem( mask, 2 ) );
+    }
+
     bCreateOutput = TRUE;
 
     GDALAllRegister();
@@ -452,8 +485,19 @@
     hDstDS = GDALWarpCreateOutput( hSrcDS, pszDstFilename, pszFormat, 
                                    pszSourceSRS, pszTargetSRS, nOrder,
                                    papszCreateOptions );
-    papszWarpOptions = CSLSetNameValue( papszWarpOptions, 
-                                        "INIT", NO_DATA_VAL_STR );
+
+
+    if (hasMask)
+    {
+        char str[30];
+        snprintf(str, 29, "%i,%i,%i", maskRGB[0], maskRGB[1], maskRGB[2]);
+        papszWarpOptions = CSLSetNameValue( papszWarpOptions, "INIT", str );
+    }
+    else
+    {
+        papszWarpOptions = CSLSetNameValue( papszWarpOptions, "INIT", "" );
+    }
+
     CSLDestroy( papszCreateOptions );
     papszCreateOptions = NULL;
 
