Check-in [25c2d2d92f]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
SHA1 Hash:25c2d2d92fbf0b5f612a0db7046191a2a3bdaa84
Date: 2010-03-15 16:23:15
User: a.furieri@lqt.it
Comment:fixing png_checj_sig / png_sig_cmp
Tags And Properties
Changes
hide diffs side-by-side diffs patch

Changes to lib/rasterlite_png.c

   119    119       int i, j;
   120    120       volatile int palette_allocated = FALSE;
   121    121       memset (sig, 0, sizeof (sig));
   122    122       if (xgdGetBuf (sig, 8, infile) < 8)
   123    123         {
   124    124   	  return NULL;
   125    125         }
   126         -    if (!png_check_sig (sig, 8))
          126  +    if (!png_sig_cmp (sig, 0, 8))
   127    127         {
   128    128   	  return NULL;
   129    129         }
   130    130   #ifndef PNG_SETJMP_NOT_SUPPORTED
   131    131       png_ptr =
   132    132   	png_create_read_struct (PNG_LIBPNG_VER_STRING, &xgdPngJmpbufStruct,
   133    133   				xgdPngErrorHandler, NULL);

Changes to src/rasterlite_grid.c

   129    129   	return 0;
   130    130       if (pc1->min > pc2->min)
   131    131   	return 1;
   132    132       if (pc1->min < pc2->min)
   133    133   	return -1;
   134    134       if (pc1->min > pc2->max)
   135    135   	return 1;
   136         -    if (pc1->min < pc2->max)
   137         -	return -1;
          136  +    return -1;
   138    137   }
   139    138   
   140    139   static int
   141    140   match_color (struct colorTable *color_table, int colors, double value,
   142    141   	     unsigned char *red, unsigned char *green, unsigned char *blue)
   143    142   {
   144    143   /* mapping a value into the corresponding color */
................................................................................
   275    274       int j;
   276    275       int n;
   277    276       double x;
   278    277       double y;
   279    278       double aspect;
   280    279       double slope;
   281    280       double cang;
   282         -    int gray;
          281  +    unsigned char gray;
   283    282       float afWin[9];
   284    283       const double degreesToRadians = M_PI / 180.0;
   285    284       const double altRadians = p->altitude * degreesToRadians;
   286    285       const double azRadians = p->azimuth * degreesToRadians;
   287    286       double red;
   288    287       double green;
   289    288       double blue;
................................................................................
   328    327   		      break;
   329    328   		  }
   330    329   	    }
   331    330   
   332    331   	  if (bContainsNull)
   333    332   	    {
   334    333   		/* We have nulls so write nullValue and move on */
   335         -		r = p->no_red;
   336         -		g = p->no_green;
   337         -		b = p->no_blue;
          334  +		if (p->mono_color)
          335  +		  {
          336  +		      *p_raster++ = p->no_red;
          337  +		      *p_raster++ = p->no_green;
          338  +		      *p_raster++ = p->no_blue;
          339  +		  }
          340  +		else
          341  +		    *p_raster++ = 0;
   338    342   	    }
   339    343   	  else
   340    344   	    {
   341    345   		/* We have a valid 3x3 window. */
   342    346   
   343    347   		/* ---------------------------------------
   344    348   		 * Compute Hillshade
................................................................................
   386    390   		      if (green > 255.0)
   387    391   			  green = 255.0;
   388    392   		      if (blue > 255.0)
   389    393   			  blue = 255.0;
   390    394   		      r = (unsigned char) red;
   391    395   		      g = (unsigned char) green;
   392    396   		      b = (unsigned) blue;
          397  +		      *p_raster++ = r;
          398  +		      *p_raster++ = g;
          399  +		      *p_raster++ = b;
   393    400   		  }
   394    401   		else
   395    402   		  {
   396    403   		      /* plain gray-scale */
   397         -		      gray = (int) cang;
   398         -		      r = (unsigned char) gray;
   399         -		      g = (unsigned char) gray;
   400         -		      b = (unsigned) gray;
          404  +		      gray = (unsigned char) cang;
          405  +		      *p_raster++ = gray;
   401    406   		  }
   402    407   	    }
   403         -	  *p_raster++ = r;
   404         -	  *p_raster++ = g;
   405         -	  *p_raster++ = b;
   406    408         }
   407    409   }
   408    410   
   409    411   static int
   410    412   triple_shaded_relief_color (struct colorTable *color_table, int colors,
   411    413   			    struct triple_scan *p, unsigned char *raster,
   412    414   			    int row)
   413    415   {
   414    416   /* creating a shaded relief color scanline */
   415         -    int color;
   416    417       int j;
   417    418       int n;
   418    419       double x;
   419    420       double y;
   420    421       double aspect;
   421    422       double slope;
   422    423       double cang;
................................................................................
  1147   1148   		  }
  1148   1149   	    }
  1149   1150   	  ptr += sizeof (float);
  1150   1151         }
  1151   1152       return 1;
  1152   1153   }
  1153   1154   
  1154         -static void
  1155         -export_geoTiff_float (struct colorTable *color_table, int colors,
  1156         -		      const char *proj4text, const char *grid_path,
  1157         -		      const char *tiff_path, unsigned char no_red,
  1158         -		      unsigned char no_green, unsigned char no_blue,
  1159         -		      int verbose)
         1155  +static int
         1156  +parse_grid_header_float (const char *grid_path, int *ext_byteorder,
         1157  +			 int *ext_ncols, int *ext_nrows, double *ext_xllcorner,
         1158  +			 double *ext_yllcorner, double *ext_cellsize,
         1159  +			 double *ext_nodata)
  1160   1160   {
  1161         -/* exporting a FLOAT GRID as GeoTIFF */
  1162         -    TIFF *tiff = NULL;
  1163         -    GTIF *gtif = NULL;
  1164         -    int byteorder = BYTE_ORDER_NONE;
  1165         -    int c;
         1161  +/* parsing a Grid Header: FLOAT */
  1166   1162       int row = 0;
  1167   1163       char buf[1024];
  1168   1164       char path[1024];
  1169   1165       char *ptr = buf;
  1170   1166       int err = 0;
         1167  +    FILE *grid;
         1168  +    int c;
         1169  +    int byteorder = BYTE_ORDER_NONE;
  1171   1170       int ncols = -1;
  1172   1171       int nrows = -1;
  1173   1172       double xllcorner = 0.0;
  1174   1173       double yllcorner = 0.0;
  1175   1174       double cellsize = 0.0;
  1176   1175       double nodata = 0.0;
  1177         -    double tiepoint[6];
  1178         -    double pixsize[3];
  1179         -    unsigned char *raster = NULL;
  1180         -    unsigned char *flt_buf = NULL;
  1181         -    size_t rd;
  1182         -    FILE *grid;
  1183         -
  1184   1176   /* parsing the Grid Header .hdr */
  1185   1177       sprintf (path, "%s.hdr", grid_path);
  1186   1178       grid = fopen (path, "rb");
  1187   1179       if (!grid)
  1188   1180         {
  1189   1181   	  printf ("Open error: %s\n", path);
  1190         -	  return;
         1182  +	  return 0;
  1191   1183         }
  1192   1184       while ((c = getc (grid)) != EOF)
  1193   1185         {
  1194   1186   	  if (c == '\r')
  1195   1187   	    {
  1196   1188   		/* ignoring Return chars */
  1197   1189   		continue;
................................................................................
  1241   1233       if (err)
  1242   1234         {
  1243   1235   	  /* there was some error */
  1244   1236   	  printf ("Invalid FLOAT Grid Header format in: %s\n", path);
  1245   1237   	  goto stop;
  1246   1238         }
  1247   1239       fclose (grid);
         1240  +    *ext_byteorder = byteorder;
         1241  +    *ext_ncols = ncols;
         1242  +    *ext_nrows = xllcorner;
         1243  +    *ext_xllcorner = yllcorner;
         1244  +    *ext_yllcorner = yllcorner;
         1245  +    *ext_cellsize = cellsize;
         1246  +    *ext_nodata = nodata;
         1247  +    return 1;
         1248  +  stop:
         1249  +    fclose (grid);
         1250  +    return 0;
         1251  +}
         1252  +
         1253  +static void
         1254  +export_geoTiff_float (struct colorTable *color_table, int colors,
         1255  +		      const char *proj4text, const char *grid_path,
         1256  +		      const char *tiff_path, unsigned char no_red,
         1257  +		      unsigned char no_green, unsigned char no_blue,
         1258  +		      int verbose)
         1259  +{
         1260  +/* exporting a FLOAT GRID as GeoTIFF */
         1261  +    TIFF *tiff = NULL;
         1262  +    GTIF *gtif = NULL;
         1263  +    int byteorder = BYTE_ORDER_NONE;
         1264  +    int row = 0;
         1265  +    char path[1024];
         1266  +    int ncols = -1;
         1267  +    int nrows = -1;
         1268  +    double xllcorner = 0.0;
         1269  +    double yllcorner = 0.0;
         1270  +    double cellsize = 0.0;
         1271  +    double nodata = 0.0;
         1272  +    double tiepoint[6];
         1273  +    double pixsize[3];
         1274  +    unsigned char *raster = NULL;
         1275  +    unsigned char *flt_buf = NULL;
         1276  +    size_t rd;
         1277  +    FILE *grid;
         1278  +
         1279  +    if (!parse_grid_header_float
         1280  +	(grid_path, &byteorder, &ncols, &nrows, &xllcorner, &yllcorner,
         1281  +	 &cellsize, &nodata))
         1282  +      {
         1283  +	  printf ("Unable to read the Grid Header\n");
         1284  +	  return;
         1285  +      }
  1248   1286   
  1249   1287   /* parsing the Grid Cells .flt */
  1250   1288       sprintf (path, "%s.flt", grid_path);
  1251   1289       grid = fopen (path, "rb");
  1252   1290       if (!grid)
  1253   1291         {
  1254   1292   	  printf ("Open error: %s\n", path);
................................................................................
  1496   1534   	XTIFFClose (tiff);
  1497   1535       if (raster)
  1498   1536   	free (raster);
  1499   1537       fclose (asc);
  1500   1538   }
  1501   1539   
  1502   1540   static int
  1503         -fetch_float_scanline_shaded (int row, unsigned char *floats, int columns,
         1541  +fetch_float_scanline_shaded (unsigned char *floats, int columns,
  1504   1542   			     int byteorder, struct triple_scan *triplet)
  1505   1543   {
  1506   1544   /* feeding a TIFF scanline from a FLOAT Grid */
  1507   1545       int cell = 0;
  1508   1546       double value;
  1509   1547       unsigned char *ptr = floats;
  1510   1548       int endian_arch = check_endian_arch ();
................................................................................
  1527   1565   			     double z_factor, double scale_factor,
  1528   1566   			     double azimuth, double altitude, int verbose)
  1529   1567   {
  1530   1568   /* exporting a FLOAT GRID as GeoTIFF */
  1531   1569       TIFF *tiff = NULL;
  1532   1570       GTIF *gtif = NULL;
  1533   1571       int byteorder = BYTE_ORDER_NONE;
  1534         -    int c;
  1535   1572       int row = 0;
  1536         -    char buf[1024];
  1537   1573       char path[1024];
  1538         -    char *ptr = buf;
  1539         -    int err = 0;
  1540   1574       int ncols = -1;
  1541   1575       int nrows = -1;
  1542   1576       double xllcorner = 0.0;
  1543   1577       double yllcorner = 0.0;
  1544   1578       double cellsize = 0.0;
  1545   1579       double nodata = 0.0;
  1546   1580       double tiepoint[6];
................................................................................
  1547   1581       double pixsize[3];
  1548   1582       struct triple_scan *triplet = NULL;
  1549   1583       unsigned char *raster = NULL;
  1550   1584       unsigned char *flt_buf = NULL;
  1551   1585       size_t rd;
  1552   1586       FILE *grid;
  1553   1587   
  1554         -/* parsing the Grid Header .hdr */
  1555         -    sprintf (path, "%s.hdr", grid_path);
  1556         -    grid = fopen (path, "rb");
  1557         -    if (!grid)
         1588  +    if (!parse_grid_header_float
         1589  +	(grid_path, &byteorder, &ncols, &nrows, &xllcorner, &yllcorner,
         1590  +	 &cellsize, &nodata))
  1558   1591         {
  1559         -	  printf ("Open error: %s\n", path);
         1592  +	  printf ("Unable to read the Grid Header\n");
  1560   1593   	  return;
  1561   1594         }
  1562         -    while ((c = getc (grid)) != EOF)
  1563         -      {
  1564         -	  if (c == '\r')
  1565         -	    {
  1566         -		/* ignoring Return chars */
  1567         -		continue;
  1568         -	    }
  1569         -	  if (c == '\n')
  1570         -	    {
  1571         -		*ptr = '\0';
  1572         -		switch (row)
  1573         -		  {
  1574         -		  case 0:
  1575         -		      if (!parseIntHeader (buf, "ncols ", &ncols))
  1576         -			  err = 1;
  1577         -		      break;
  1578         -		  case 1:
  1579         -		      if (!parseIntHeader (buf, "nrows ", &nrows))
  1580         -			  err = 1;
  1581         -		      break;
  1582         -		  case 2:
  1583         -		      if (!parseDblHeader (buf, "xllcorner ", &xllcorner))
  1584         -			  err = 1;
  1585         -		      break;
  1586         -		  case 3:
  1587         -		      if (!parseDblHeader (buf, "yllcorner ", &yllcorner))
  1588         -			  err = 1;
  1589         -		      break;
  1590         -		  case 4:
  1591         -		      if (!parseDblHeader (buf, "cellsize ", &cellsize))
  1592         -			  err = 1;
  1593         -		      break;
  1594         -		  case 5:
  1595         -		      if (!parseDblHeader (buf, "NODATA_value ", &nodata))
  1596         -			  err = 1;
  1597         -		      break;
  1598         -		  case 6:
  1599         -		      if (!parseOrderHeader (buf, "byteorder ", &byteorder))
  1600         -			  err = 1;
  1601         -		      break;
  1602         -		  };
  1603         -		ptr = buf;
  1604         -		row++;
  1605         -		if (row == 7)
  1606         -		    break;
  1607         -		continue;
  1608         -	    }
  1609         -	  *ptr++ = c;
  1610         -      }
  1611         -    if (err)
  1612         -      {
  1613         -	  /* there was some error */
  1614         -	  printf ("Invalid FLOAT Grid Header format in: %s\n", path);
  1615         -	  goto stop;
  1616         -      }
  1617         -    fclose (grid);
  1618   1595   
  1619   1596   /* resizing CellSize */
  1620   1597       cellsize = (cellsize * (double) ncols) / (double) (ncols - 2);
  1621   1598   
  1622   1599   /* parsing the Grid Cells .flt */
  1623   1600       sprintf (path, "%s.flt", grid_path);
  1624   1601       grid = fopen (path, "rb");
................................................................................
  1644   1621   /* writing the TIFF Tags */
  1645   1622       TIFFSetField (tiff, TIFFTAG_IMAGEWIDTH, ncols - 2);
  1646   1623       TIFFSetField (tiff, TIFFTAG_IMAGELENGTH, nrows - 2);
  1647   1624       TIFFSetField (tiff, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
  1648   1625       TIFFSetField (tiff, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
  1649   1626       TIFFSetField (tiff, TIFFTAG_ROWSPERSTRIP, 1);
  1650   1627       TIFFSetField (tiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
  1651         -    TIFFSetField (tiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
  1652   1628       TIFFSetField (tiff, TIFFTAG_BITSPERSAMPLE, 8);
  1653         -    TIFFSetField (tiff, TIFFTAG_SAMPLESPERPIXEL, 3);
         1629  +    if (!color_table && !mono_color)
         1630  +      {
         1631  +	  TIFFSetField (tiff, TIFFTAG_SAMPLESPERPIXEL, 1);
         1632  +	  TIFFSetField (tiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
         1633  +      }
         1634  +    else
         1635  +      {
         1636  +	  TIFFSetField (tiff, TIFFTAG_SAMPLESPERPIXEL, 3);
         1637  +	  TIFFSetField (tiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
         1638  +      }
  1654   1639   
  1655   1640   /* writing the GeoTIFF Tags */
  1656   1641       pixsize[0] = cellsize;
  1657   1642       pixsize[1] = cellsize;
  1658   1643       pixsize[2] = 0.0;
  1659   1644       TIFFSetField (tiff, GTIFF_PIXELSCALE, 3, pixsize);
  1660   1645       tiepoint[0] = 0.0;
................................................................................
  1662   1647       tiepoint[2] = 0.0;
  1663   1648       tiepoint[3] = xllcorner;
  1664   1649       tiepoint[4] = yllcorner + (cellsize * nrows);
  1665   1650       tiepoint[5] = 0.0;
  1666   1651       TIFFSetField (tiff, GTIFF_TIEPOINTS, 6, tiepoint);
  1667   1652       GTIFSetFromProj4 (gtif, proj4text);
  1668   1653       GTIFWriteKeys (gtif);
  1669         -    raster = malloc (ncols * 3);
         1654  +    if (!color_table && !mono_color)
         1655  +	raster = malloc (ncols);
         1656  +    else
         1657  +	raster = malloc (ncols * 3);
  1670   1658       flt_buf = malloc (sizeof (float) * ncols);
  1671   1659   
  1672   1660   /* initializing the TripleRow object */
  1673   1661       triplet = triple_alloc (ncols);
  1674   1662       triple_set_shaded_relief_params (triplet, z_factor, scale_factor, altitude,
  1675   1663   				     azimuth);
  1676   1664       triple_set_monochrome_params (triplet, mono_color, mono_red, mono_green,
................................................................................
  1687   1675   	  rd = fread (flt_buf, 1, ncols * sizeof (float), grid);
  1688   1676   	  if (rd != (sizeof (float) * ncols))
  1689   1677   	    {
  1690   1678   		printf ("*** Grid read error ***\n");
  1691   1679   		printf ("An invalid GeoTIFF was generated ... aborting ...\n");
  1692   1680   		goto stop;
  1693   1681   	    }
  1694         -	  if (fetch_float_scanline_shaded
  1695         -	      (row + 1, flt_buf, ncols, byteorder, triplet))
         1682  +	  if (fetch_float_scanline_shaded (flt_buf, ncols, byteorder, triplet))
  1696   1683   	    {
  1697   1684   		if (triple_is_valid (triplet))
  1698   1685   		  {
  1699   1686   		      int ret = 1;
  1700   1687   		      if (!color_table)
  1701   1688   			  triple_shaded_relief (triplet, raster);
  1702   1689   		      else
................................................................................
  1877   1864   /* writing the TIFF Tags */
  1878   1865       TIFFSetField (tiff, TIFFTAG_IMAGEWIDTH, ncols - 2);
  1879   1866       TIFFSetField (tiff, TIFFTAG_IMAGELENGTH, nrows - 2);
  1880   1867       TIFFSetField (tiff, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
  1881   1868       TIFFSetField (tiff, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
  1882   1869       TIFFSetField (tiff, TIFFTAG_ROWSPERSTRIP, 1);
  1883   1870       TIFFSetField (tiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
  1884         -    TIFFSetField (tiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
  1885   1871       TIFFSetField (tiff, TIFFTAG_BITSPERSAMPLE, 8);
  1886         -    TIFFSetField (tiff, TIFFTAG_SAMPLESPERPIXEL, 3);
         1872  +    if (!color_table && !mono_color)
         1873  +      {
         1874  +	  TIFFSetField (tiff, TIFFTAG_SAMPLESPERPIXEL, 1);
         1875  +	  TIFFSetField (tiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
         1876  +      }
         1877  +    else
         1878  +      {
         1879  +	  TIFFSetField (tiff, TIFFTAG_SAMPLESPERPIXEL, 3);
         1880  +	  TIFFSetField (tiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
         1881  +      }
  1887   1882   
  1888   1883   /* writing the GeoTIFF Tags */
  1889   1884       pixsize[0] = cellsize;
  1890   1885       pixsize[1] = cellsize;
  1891   1886       pixsize[2] = 0.0;
  1892   1887       TIFFSetField (tiff, GTIFF_PIXELSCALE, 3, pixsize);
  1893   1888       tiepoint[0] = 0.0;
................................................................................
  1895   1890       tiepoint[2] = 0.0;
  1896   1891       tiepoint[3] = xllcorner;
  1897   1892       tiepoint[4] = yllcorner + (cellsize * nrows);
  1898   1893       tiepoint[5] = 0.0;
  1899   1894       TIFFSetField (tiff, GTIFF_TIEPOINTS, 6, tiepoint);
  1900   1895       GTIFSetFromProj4 (gtif, proj4text);
  1901   1896       GTIFWriteKeys (gtif);
  1902         -    raster = malloc (ncols * 3);
         1897  +    if (!color_table && !mono_color)
         1898  +	raster = malloc (ncols);
         1899  +    else
         1900  +	raster = malloc (ncols * 3);
  1903   1901   
  1904   1902   /* initializing the TripleRow object */
  1905   1903       triplet = triple_alloc (ncols);
  1906   1904       triple_set_shaded_relief_params (triplet, z_factor, scale_factor, altitude,
  1907   1905   				     azimuth);
  1908   1906       triple_set_monochrome_params (triplet, mono_color, mono_red, mono_green,
  1909   1907   				  mono_blue);
................................................................................
  2288   2286       switch (grid_type)
  2289   2287         {
  2290   2288         case ASCII_GRID:
  2291   2289   	  printf ("Grid Format: ASCII\n");
  2292   2290   	  break;
  2293   2291         case FLOAT_GRID:
  2294   2292   	  printf ("Grid Format: FLOAT\n");
  2295         -	  break;
  2296   2293         default:
  2297   2294   	  printf ("Grid Format: UNKNOWN\n");
  2298   2295   	  break;
  2299   2296         }
  2300   2297       if (shaded_relief)
  2301   2298         {
  2302   2299   	  printf ("\n           Shaded Relief arguments:\n");