Check-in Differences
Not logged in

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

Difference From 294601b232f6563c To caa41a260e3342c1

2010-03-15
16:23
fixing png_checj_sig / png_sig_cmp check-in: 25c2d2d92f user: a.furieri@lqt.it tags: trunk
2010-01-13
15:15
implementing bsearch for rasterlite_grid tool check-in: caa41a260e user: a.furieri@lqt.it tags: trunk
2010-01-09
11:16
implementing Shaded Relief for Grids check-in: d4879cedec user: a.furieri@lqt.it tags: trunk
2009-12-07
15:41
supporting RGBA, ARGB, BGR and BGRA raw images check-in: 294601b232 user: a.furieri@lqt.it tags: trunk
2009-11-19
11:03
fixing makefile and warnings for MSVC check-in: bf94dc55e3 user: a.furieri@lqt.it tags: trunk

Changes to src/rasterlite_grid.c.

    53     53   #define ARG_GRID_PATH		1
    54     54   #define ARG_COLOR_PATH		2
    55     55   #define ARG_TIFF_PATH		3
    56     56   #define ARG_GRID_TYPE		4
    57     57   #define ARG_EPSG_CODE		5
    58     58   #define ARG_NODATA_COLOR	6
    59     59   #define ARG_PROJ4TEXT		7
           60  +#define ARG_MONO_COLOR		9
           61  +#define ARG_Z_FACTOR		10
           62  +#define ARG_SCALE			11
           63  +#define ARG_AZIMUTH			12
           64  +#define ARG_ALTITUDE		13
    60     65   
    61     66   #define ASCII_GRID	100
    62     67   #define FLOAT_GRID	101
    63     68   
    64     69   #define BYTE_ORDER_NONE	0
    65     70   #define BYTE_ORDER_BIG	1
    66     71   #define BYTE_ORDER_LITTLE	2
    67     72   
    68     73   struct colorTable
    69     74   {
           75  +/* a color table value range */
    70     76       double min;
    71     77       double max;
    72     78       unsigned char red;
    73     79       unsigned char green;
    74     80       unsigned char blue;
    75     81   };
           82  +
           83  +struct triple_scan
           84  +{
           85  +/* the 3-scanlines object for Shaded Relief */
           86  +    int ncols;
           87  +    double z_factor;
           88  +    double scale_factor;
           89  +    double altitude;
           90  +    double azimuth;
           91  +    int mono_color;
           92  +    unsigned char mono_red;
           93  +    unsigned char mono_green;
           94  +    unsigned char mono_blue;
           95  +    float no_data_value;
           96  +    unsigned char no_red;
           97  +    unsigned char no_green;
           98  +    unsigned char no_blue;
           99  +    float *row1;
          100  +    float *row2;
          101  +    float *row3;
          102  +    float *current_row;
          103  +};
          104  +
          105  +static int
          106  +cmp_color_qsort (const void *p1, const void *p2)
          107  +{
          108  +/* compares two colorTable entries [for QSORT] */
          109  +    struct colorTable *pc1 = (struct colorTable *) p1;
          110  +    struct colorTable *pc2 = (struct colorTable *) p2;
          111  +    if (pc1->min > pc2->min)
          112  +	return 1;
          113  +    if (pc1->min < pc2->min)
          114  +	return -1;
          115  +    if (pc1->max > pc2->max)
          116  +	return 1;
          117  +    if (pc1->max < pc2->max)
          118  +	return -1;
          119  +    return 0;
          120  +}
          121  +
          122  +static int
          123  +cmp_color_bsearch (const void *p1, const void *p2)
          124  +{
          125  +/* compares two colorTable entries [for BSEARCH] */
          126  +    struct colorTable *pc1 = (struct colorTable *) p1;
          127  +    struct colorTable *pc2 = (struct colorTable *) p2;
          128  +    if (pc1->min >= pc2->min && pc1->min <= pc2->max)
          129  +	return 0;
          130  +    if (pc1->min > pc2->min)
          131  +	return 1;
          132  +    if (pc1->min < pc2->min)
          133  +	return -1;
          134  +    if (pc1->min > pc2->max)
          135  +	return 1;
          136  +    if (pc1->min < pc2->max)
          137  +	return -1;
          138  +}
          139  +
          140  +static int
          141  +match_color (struct colorTable *color_table, int colors, double value,
          142  +	     unsigned char *red, unsigned char *green, unsigned char *blue)
          143  +{
          144  +/* mapping a value into the corresponding color */
          145  +    struct colorTable *ret;
          146  +    struct colorTable src;
          147  +    src.min = value;
          148  +    ret =
          149  +	bsearch (&src, color_table, colors, sizeof (struct colorTable),
          150  +		 cmp_color_bsearch);
          151  +    if (ret)
          152  +      {
          153  +	  *red = ret->red;
          154  +	  *green = ret->green;
          155  +	  *blue = ret->blue;
          156  +	  return 1;
          157  +      }
          158  +    return 0;
          159  +}
          160  +
          161  +static struct triple_scan *
          162  +triple_alloc (int ncols)
          163  +{
          164  +/* allocating the 3-scanlines object */
          165  +    struct triple_scan *p = malloc (sizeof (struct triple_scan));
          166  +    p->ncols = ncols;
          167  +    p->row1 = malloc (sizeof (float) * ncols);
          168  +    p->row2 = malloc (sizeof (float) * ncols);
          169  +    p->row3 = malloc (sizeof (float) * ncols);
          170  +    p->current_row = p->row1;
          171  +    p->mono_color = 0;
          172  +    p->mono_red = 0;
          173  +    p->mono_green = 0;
          174  +    p->mono_blue = 0;
          175  +    p->no_data_value = 0.0;
          176  +    p->no_red = 0;
          177  +    p->no_green = 0;
          178  +    p->no_blue = 0;
          179  +    p->z_factor = 1.0;
          180  +    p->scale_factor = 1.0;
          181  +    p->altitude = 45.0;
          182  +    p->azimuth = 315.0;
          183  +    return p;
          184  +}
          185  +
          186  +static void
          187  +triple_free (struct triple_scan *p)
          188  +{
          189  +/* freeing the 3-scanlines object */
          190  +    free (p->row1);
          191  +    free (p->row2);
          192  +    free (p->row3);
          193  +    free (p);
          194  +}
          195  +
          196  +static void
          197  +triple_set_no_data (struct triple_scan *p, float no_data_value,
          198  +		    unsigned char no_red, unsigned char no_green,
          199  +		    unsigned char no_blue)
          200  +{
          201  +/* setting NODATA params */
          202  +    p->no_data_value = no_data_value;
          203  +    p->no_red = no_red;
          204  +    p->no_green = no_green;
          205  +    p->no_blue = no_blue;
          206  +}
          207  +
          208  +static void
          209  +triple_set_monochrome_params (struct triple_scan *p, int mono_color,
          210  +			      unsigned char mono_red, unsigned char mono_green,
          211  +			      unsigned char mono_blue)
          212  +{
          213  +/* setting ShadedRelief params */
          214  +    p->mono_color = mono_color;
          215  +    p->mono_red = mono_red;
          216  +    p->mono_green = mono_green;
          217  +    p->mono_blue = mono_blue;
          218  +}
          219  +
          220  +static void
          221  +triple_set_shaded_relief_params (struct triple_scan *p, double z, double scale,
          222  +				 double alt, double az)
          223  +{
          224  +/* setting ShadedRelief params */
          225  +    p->z_factor = z;
          226  +    p->scale_factor = scale;
          227  +    p->altitude = alt;
          228  +    p->azimuth = az;
          229  +}
          230  +
          231  +static void
          232  +triple_rotate (struct triple_scan *p)
          233  +{
          234  +/* rotating the 3-scanlines */
          235  +    if (p->current_row == p->row1)
          236  +      {
          237  +	  p->current_row = p->row2;
          238  +	  goto zero_init;
          239  +      }
          240  +    if (p->current_row == p->row2)
          241  +      {
          242  +	  p->current_row = p->row3;
          243  +	  goto zero_init;
          244  +      }
          245  +    p->current_row = p->row1;
          246  +    p->row1 = p->row2;
          247  +    p->row2 = p->row3;
          248  +    p->row3 = p->current_row;
          249  +  zero_init:
          250  +    memset (p->current_row, 0, sizeof (float) * p->ncols);
          251  +}
          252  +
          253  +static void
          254  +triple_store_cell (struct triple_scan *p, int icol, float value)
          255  +{
          256  +/* storing a cell value into 3-scanlines */
          257  +    if (icol < 0 || icol >= p->ncols)
          258  +	return;
          259  +    *(p->current_row + icol) = value;
          260  +}
          261  +
          262  +static int
          263  +triple_is_valid (struct triple_scan *p)
          264  +{
          265  +/* checking for validitity */
          266  +    if (p->current_row == p->row3)
          267  +	return 1;
          268  +    return 0;
          269  +}
          270  +
          271  +static void
          272  +triple_shaded_relief (struct triple_scan *p, unsigned char *raster)
          273  +{
          274  +/* creating a shaded relief scanline */
          275  +    int j;
          276  +    int n;
          277  +    double x;
          278  +    double y;
          279  +    double aspect;
          280  +    double slope;
          281  +    double cang;
          282  +    int gray;
          283  +    float afWin[9];
          284  +    const double degreesToRadians = M_PI / 180.0;
          285  +    const double altRadians = p->altitude * degreesToRadians;
          286  +    const double azRadians = p->azimuth * degreesToRadians;
          287  +    double red;
          288  +    double green;
          289  +    double blue;
          290  +    double alpha;
          291  +    int bContainsNull;
          292  +    unsigned char *p_raster = raster;
          293  +    unsigned char r;
          294  +    unsigned char g;
          295  +    unsigned char b;
          296  +
          297  +/*
          298  +/ Move a 3x3 pafWindow over each cell 
          299  +/ (where the cell in question is #4)
          300  +/ 
          301  +/      0 1 2
          302  +/      3 4 5
          303  +/      6 7 8
          304  +*/
          305  +    for (j = 0; j < p->ncols; j++)
          306  +      {
          307  +	  /* Exclude the edges */
          308  +	  if (j == 0 || j == p->ncols - 1)
          309  +	      continue;
          310  +
          311  +	  bContainsNull = 0;
          312  +
          313  +	  afWin[0] = p->row1[j - 1];
          314  +	  afWin[1] = p->row1[j];
          315  +	  afWin[2] = p->row1[j + 1];
          316  +	  afWin[3] = p->row2[j - 1];
          317  +	  afWin[4] = p->row2[j];
          318  +	  afWin[5] = p->row2[j + 1];
          319  +	  afWin[6] = p->row3[j - 1];
          320  +	  afWin[7] = p->row3[j];
          321  +	  afWin[8] = p->row3[j + 1];
          322  +
          323  +	  for (n = 0; n <= 8; n++)
          324  +	    {
          325  +		if (afWin[n] == p->no_data_value)
          326  +		  {
          327  +		      bContainsNull = 1;
          328  +		      break;
          329  +		  }
          330  +	    }
          331  +
          332  +	  if (bContainsNull)
          333  +	    {
          334  +		/* We have nulls so write nullValue and move on */
          335  +		r = p->no_red;
          336  +		g = p->no_green;
          337  +		b = p->no_blue;
          338  +	    }
          339  +	  else
          340  +	    {
          341  +		/* We have a valid 3x3 window. */
          342  +
          343  +		/* ---------------------------------------
          344  +		 * Compute Hillshade
          345  +		 */
          346  +
          347  +		/* First Slope ... */
          348  +		x = p->z_factor * ((afWin[0] + afWin[3] + afWin[3] + afWin[6]) -
          349  +				   (afWin[2] + afWin[5] + afWin[5] +
          350  +				    afWin[8])) / (8.0 * p->scale_factor);
          351  +
          352  +		y = p->z_factor * ((afWin[6] + afWin[7] + afWin[7] + afWin[8]) -
          353  +				   (afWin[0] + afWin[1] + afWin[1] +
          354  +				    afWin[2])) / (8.0 * p->scale_factor);
          355  +
          356  +		slope = M_PI / 2 - atan (sqrt (x * x + y * y));
          357  +
          358  +		/* ... then aspect... */
          359  +		aspect = atan2 (x, y);
          360  +
          361  +		/* ... then the shade value */
          362  +		cang = sin (altRadians) * sin (slope) +
          363  +		    cos (altRadians) * cos (slope) *
          364  +		    cos (azRadians - M_PI / 2 - aspect);
          365  +
          366  +		if (cang <= 0.0)
          367  +		    cang = 1.0;
          368  +		else
          369  +		    cang = 1.0 + (254.0 * cang);
          370  +
          371  +		if (p->mono_color)
          372  +		  {
          373  +		      /* using the monochrome base color + ALPHA */
          374  +		      alpha = cang / 255.0;
          375  +		      red = (double) (p->mono_red) * alpha;
          376  +		      green = (double) (p->mono_green) * alpha;
          377  +		      blue = (double) (p->mono_blue) * alpha;
          378  +		      if (red < 0.0)
          379  +			  red = 0.0;
          380  +		      if (green < 0.0)
          381  +			  green = 0.0;
          382  +		      if (blue < 0.0)
          383  +			  blue = 0.0;
          384  +		      if (red > 255.0)
          385  +			  red = 255.0;
          386  +		      if (green > 255.0)
          387  +			  green = 255.0;
          388  +		      if (blue > 255.0)
          389  +			  blue = 255.0;
          390  +		      r = (unsigned char) red;
          391  +		      g = (unsigned char) green;
          392  +		      b = (unsigned) blue;
          393  +		  }
          394  +		else
          395  +		  {
          396  +		      /* plain gray-scale */
          397  +		      gray = (int) cang;
          398  +		      r = (unsigned char) gray;
          399  +		      g = (unsigned char) gray;
          400  +		      b = (unsigned) gray;
          401  +		  }
          402  +	    }
          403  +	  *p_raster++ = r;
          404  +	  *p_raster++ = g;
          405  +	  *p_raster++ = b;
          406  +      }
          407  +}
          408  +
          409  +static int
          410  +triple_shaded_relief_color (struct colorTable *color_table, int colors,
          411  +			    struct triple_scan *p, unsigned char *raster,
          412  +			    int row)
          413  +{
          414  +/* creating a shaded relief color scanline */
          415  +    int color;
          416  +    int j;
          417  +    int n;
          418  +    double x;
          419  +    double y;
          420  +    double aspect;
          421  +    double slope;
          422  +    double cang;
          423  +    float afWin[9];
          424  +    const double degreesToRadians = M_PI / 180.0;
          425  +    const double altRadians = p->altitude * degreesToRadians;
          426  +    const double azRadians = p->azimuth * degreesToRadians;
          427  +    double red;
          428  +    double green;
          429  +    double blue;
          430  +    double alpha;
          431  +    double thisValue;
          432  +    int bContainsNull;
          433  +    unsigned char *p_raster = raster;
          434  +    unsigned char r;
          435  +    unsigned char g;
          436  +    unsigned char b;
          437  +
          438  +/*
          439  +/ Move a 3x3 pafWindow over each cell 
          440  +/ (where the cell in question is #4)
          441  +/ 
          442  +/      0 1 2
          443  +/      3 4 5
          444  +/      6 7 8
          445  +*/
          446  +    for (j = 0; j < p->ncols; j++)
          447  +      {
          448  +	  /* Exclude the edges */
          449  +	  if (j == 0 || j == p->ncols - 1)
          450  +	      continue;
          451  +
          452  +	  bContainsNull = 0;
          453  +
          454  +	  afWin[0] = p->row1[j - 1];
          455  +	  afWin[1] = p->row1[j];
          456  +	  afWin[2] = p->row1[j + 1];
          457  +	  afWin[3] = p->row2[j - 1];
          458  +	  afWin[4] = p->row2[j];
          459  +	  afWin[5] = p->row2[j + 1];
          460  +	  afWin[6] = p->row3[j - 1];
          461  +	  afWin[7] = p->row3[j];
          462  +	  afWin[8] = p->row3[j + 1];
          463  +
          464  +	  thisValue = afWin[4];
          465  +
          466  +	  for (n = 0; n <= 8; n++)
          467  +	    {
          468  +		if (afWin[n] == p->no_data_value)
          469  +		  {
          470  +		      bContainsNull = 1;
          471  +		      break;
          472  +		  }
          473  +	    }
          474  +
          475  +	  if (bContainsNull)
          476  +	    {
          477  +		/* We have nulls so write nullValue and move on */
          478  +		r = p->no_red;
          479  +		g = p->no_green;
          480  +		b = p->no_blue;
          481  +	    }
          482  +	  else
          483  +	    {
          484  +		/* We have a valid 3x3 window. */
          485  +
          486  +		/* ---------------------------------------
          487  +		 * Compute Hillshade
          488  +		 */
          489  +
          490  +		/* First Slope ... */
          491  +		x = p->z_factor * ((afWin[0] + afWin[3] + afWin[3] + afWin[6]) -
          492  +				   (afWin[2] + afWin[5] + afWin[5] +
          493  +				    afWin[8])) / (8.0 * p->scale_factor);
          494  +
          495  +		y = p->z_factor * ((afWin[6] + afWin[7] + afWin[7] + afWin[8]) -
          496  +				   (afWin[0] + afWin[1] + afWin[1] +
          497  +				    afWin[2])) / (8.0 * p->scale_factor);
          498  +
          499  +		slope = M_PI / 2 - atan (sqrt (x * x + y * y));
          500  +
          501  +		/* ... then aspect... */
          502  +		aspect = atan2 (x, y);
          503  +
          504  +		/* ... then the shade value */
          505  +		cang = sin (altRadians) * sin (slope) +
          506  +		    cos (altRadians) * cos (slope) *
          507  +		    cos (azRadians - M_PI / 2 - aspect);
          508  +
          509  +		if (cang <= 0.0)
          510  +		    cang = 1.0;
          511  +		else
          512  +		    cang = 1.0 + (254.0 * cang);
          513  +
          514  +		/* merging the color + ALPHA */
          515  +		if (!match_color (color_table, colors, thisValue, &r, &g, &b))
          516  +		  {
          517  +		      printf
          518  +			  ("Grid row %d: unmatched value %1.8f; color not found\n",
          519  +			   row, thisValue);
          520  +		      return 0;
          521  +		  }
          522  +		alpha = cang / 255.0;
          523  +		red = (double) r *alpha;
          524  +		green = (double) g *alpha;
          525  +		blue = (double) b *alpha;
          526  +		if (red < 0.0)
          527  +		    red = 0.0;
          528  +		if (green < 0.0)
          529  +		    green = 0.0;
          530  +		if (blue < 0.0)
          531  +		    blue = 0.0;
          532  +		if (red > 255.0)
          533  +		    red = 255.0;
          534  +		if (green > 255.0)
          535  +		    green = 255.0;
          536  +		if (blue > 255.0)
          537  +		    blue = 255.0;
          538  +		r = (unsigned char) red;
          539  +		g = (unsigned char) green;
          540  +		b = (unsigned) blue;
          541  +	    }
          542  +	  *p_raster++ = r;
          543  +	  *p_raster++ = g;
          544  +	  *p_raster++ = b;
          545  +      }
          546  +    return 1;
          547  +}
    76    548   
    77    549   static int
    78    550   parse_hex (const char hi, const char lo)
    79    551   {
    80    552   /* parsing an hexedecimal value XX */
    81    553       int hex;
    82    554       switch (hi)
................................................................................
   350    822   	    }
   351    823   	  return 1;
   352    824         }
   353    825       return 0;
   354    826   }
   355    827   
   356    828   static struct colorTable *
   357         -parse_color_table (const char *path)
          829  +parse_color_table (const char *path, int *count)
   358    830   {
   359    831   /* parsing the Color Table */
   360    832       double min;
   361    833       double max;
   362    834       unsigned char red;
   363    835       unsigned char green;
   364    836       unsigned char blue;
................................................................................
   400    872   		ptr = buf;
   401    873   		continue;
   402    874   	    }
   403    875   	  *ptr++ = c;
   404    876         }
   405    877       if (lines == 0 || error > 0)
   406    878   	return NULL;
   407         -    table = malloc (sizeof (struct colorTable) * (lines + 1));
   408         -/* marking the last (stop) table item */
   409         -    (table + lines)->min = DBL_MAX;
   410         -    (table + lines)->max = DBL_MAX;
   411         -    (table + lines)->red = 0;
   412         -    (table + lines)->green = 0;
   413         -    (table + lines)->blue = 0;
          879  +    table = malloc (sizeof (struct colorTable) * (lines));
   414    880   /* closing and reopening the Color Table file */
   415    881       fclose (in);
   416    882       in = fopen (path, "rb");
   417    883       if (!in)
   418    884         {
   419    885   	  printf ("Re-Open error: %s\n", path);
   420    886   	  return NULL;
................................................................................
   457    923         {
   458    924   	  /* unexected error */
   459    925   	  fprintf (stderr, "ColorTable: unexpected read error\n");
   460    926   	  free (table);
   461    927   	  table = NULL;
   462    928         }
   463    929       fclose (in);
          930  +/* sorting the color table */
          931  +    *count = lines;
          932  +    qsort (table, lines, sizeof (struct colorTable), cmp_color_qsort);
   464    933       return table;
   465    934   }
   466    935   
   467    936   static int
   468    937   parseIntHeader (const char *row, const char *tag, int *value)
   469    938   {
   470    939   /* parsing an Integer value Header */
................................................................................
   504    973   	  *value = BYTE_ORDER_BIG;
   505    974   	  return 1;
   506    975         }
   507    976       return 0;
   508    977   }
   509    978   
   510    979   static int
   511         -match_color (struct colorTable *color_table, double value, unsigned char *red,
   512         -	     unsigned char *green, unsigned char *blue)
   513         -{
   514         -/* mapping a value into the corresponding color */
   515         -    struct colorTable *p = color_table;
   516         -    while (p)
   517         -      {
   518         -	  if (p->min == DBL_MAX)
   519         -	      return 0;
   520         -	  if (value >= p->min && value <= p->max)
   521         -	    {
   522         -		*red = p->red;
   523         -		*green = p->green;
   524         -		*blue = p->blue;
   525         -		return 1;
   526         -	    }
   527         -	  p++;
   528         -      }
   529         -    return 0;
   530         -}
   531         -
   532         -static int
   533         -fetch_scanline (int row, struct colorTable *color_table, FILE * asc,
          980  +fetch_scanline (int row, struct colorTable *color_table, int colors, FILE * asc,
   534    981   		unsigned char *raster, int columns, double nodata,
   535    982   		unsigned char no_red, unsigned char no_green,
   536    983   		unsigned char no_blue)
   537    984   {
   538    985   /* feeding a TIFF scanline from an ASCII Grid */
   539    986       int c;
   540    987       int cell = 0;
................................................................................
   565   1012   		  {
   566   1013   		      *p_raster++ = no_red;
   567   1014   		      *p_raster++ = no_green;
   568   1015   		      *p_raster++ = no_blue;
   569   1016   		  }
   570   1017   		else
   571   1018   		  {
   572         -		      if (match_color (color_table, value, &red, &green, &blue))
         1019  +		      if (match_color
         1020  +			  (color_table, colors, value, &red, &green, &blue))
   573   1021   			{
   574   1022   			    *p_raster++ = red;
   575   1023   			    *p_raster++ = green;
   576   1024   			    *p_raster++ = blue;
   577   1025   			}
   578   1026   		      else
   579   1027   			{
................................................................................
   653   1101       convert.int_value = 1;
   654   1102       if (convert.byte[0] == 0)
   655   1103   	return 0;
   656   1104       return 1;
   657   1105   }
   658   1106   
   659   1107   static int
   660         -fetch_float_scanline (int row, struct colorTable *color_table,
         1108  +fetch_float_scanline (int row, struct colorTable *color_table, int colors,
   661   1109   		      unsigned char *floats, unsigned char *raster, int columns,
   662   1110   		      double nodata, unsigned char no_red,
   663   1111   		      unsigned char no_green, unsigned char no_blue,
   664   1112   		      int byteorder)
   665   1113   {
   666   1114   /* feeding a TIFF scanline from a FLOAT Grid */
   667   1115       int cell = 0;
................................................................................
   679   1127   	    {
   680   1128   		*p_raster++ = no_red;
   681   1129   		*p_raster++ = no_green;
   682   1130   		*p_raster++ = no_blue;
   683   1131   	    }
   684   1132   	  else
   685   1133   	    {
   686         -		if (match_color (color_table, value, &red, &green, &blue))
         1134  +		if (match_color
         1135  +		    (color_table, colors, value, &red, &green, &blue))
   687   1136   		  {
   688   1137   		      *p_raster++ = red;
   689   1138   		      *p_raster++ = green;
   690   1139   		      *p_raster++ = blue;
   691   1140   		  }
   692   1141   		else
   693   1142   		  {
................................................................................
   699   1148   	    }
   700   1149   	  ptr += sizeof (float);
   701   1150         }
   702   1151       return 1;
   703   1152   }
   704   1153   
   705   1154   static void
   706         -export_geoTiff_float (struct colorTable *color_table, const char *proj4text,
   707         -		      const char *grid_path, const char *tiff_path,
   708         -		      unsigned char no_red, unsigned char no_green,
   709         -		      unsigned char no_blue, int verbose)
         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)
   710   1160   {
   711   1161   /* exporting a FLOAT GRID as GeoTIFF */
   712   1162       TIFF *tiff = NULL;
   713   1163       GTIF *gtif = NULL;
   714   1164       int byteorder = BYTE_ORDER_NONE;
   715   1165       int c;
   716   1166       int row = 0;
................................................................................
   856   1306   	  if (rd != (sizeof (float) * ncols))
   857   1307   	    {
   858   1308   		printf ("*** Grid read error ***\n");
   859   1309   		printf ("An invalid GeoTIFF was generated ... aborting ...\n");
   860   1310   		goto stop;
   861   1311   	    }
   862   1312   	  if (fetch_float_scanline
   863         -	      (row + 1, color_table, flt_buf, raster, ncols, nodata, no_red,
   864         -	       no_green, no_blue, byteorder))
         1313  +	      (row + 1, color_table, colors, flt_buf, raster, ncols, nodata,
         1314  +	       no_red, no_green, no_blue, byteorder))
   865   1315   	    {
   866   1316   		if (TIFFWriteScanline (tiff, raster, row, 0) < 0)
   867   1317   		  {
   868   1318   		      printf ("\tTIFF write error @ row=%d\n", row);
   869   1319   		      printf
   870   1320   			  ("An invalid GeoTIFF was generated ... aborting ...\n");
   871   1321   		      goto stop;
................................................................................
   888   1338   	free (raster);
   889   1339       if (flt_buf)
   890   1340   	free (flt_buf);
   891   1341       fclose (grid);
   892   1342   }
   893   1343   
   894   1344   static void
   895         -export_geoTiff_ascii (struct colorTable *color_table, const char *proj4text,
   896         -		      const char *grid_path, const char *tiff_path,
   897         -		      unsigned char no_red, unsigned char no_green,
   898         -		      unsigned char no_blue, int verbose)
         1345  +export_geoTiff_ascii (struct colorTable *color_table, int colors,
         1346  +		      const char *proj4text, const char *grid_path,
         1347  +		      const char *tiff_path, unsigned char no_red,
         1348  +		      unsigned char no_green, unsigned char no_blue,
         1349  +		      int verbose)
   899   1350   {
   900   1351   /* exporting an ASCII GRID as GeoTIFF */
   901   1352       TIFF *tiff = NULL;
   902   1353       GTIF *gtif = NULL;
   903   1354       int c;
   904   1355       int row = 0;
   905   1356       char buf[1024];
................................................................................
  1015   1466         {
  1016   1467   	  if (verbose)
  1017   1468   	    {
  1018   1469   		fprintf (stderr, "writing scanline %d of %d\n", row + 1, nrows);
  1019   1470   		fflush (stderr);
  1020   1471   	    }
  1021   1472   	  if (fetch_scanline
  1022         -	      (row + 1, color_table, asc, raster, ncols, nodata, no_red,
         1473  +	      (row + 1, color_table, colors, asc, raster, ncols, nodata, no_red,
  1023   1474   	       no_green, no_blue))
  1024   1475   	    {
  1025   1476   		if (TIFFWriteScanline (tiff, raster, row, 0) < 0)
  1026   1477   		  {
  1027   1478   		      printf ("\tTIFF write error @ row=%d\n", row);
  1028   1479   		      printf
  1029   1480   			  ("An invalid GeoTIFF was generated ... aborting ...\n");
................................................................................
  1043   1494   	GTIFFree (gtif);
  1044   1495       if (tiff)
  1045   1496   	XTIFFClose (tiff);
  1046   1497       if (raster)
  1047   1498   	free (raster);
  1048   1499       fclose (asc);
  1049   1500   }
         1501  +
         1502  +static int
         1503  +fetch_float_scanline_shaded (int row, unsigned char *floats, int columns,
         1504  +			     int byteorder, struct triple_scan *triplet)
         1505  +{
         1506  +/* feeding a TIFF scanline from a FLOAT Grid */
         1507  +    int cell = 0;
         1508  +    double value;
         1509  +    unsigned char *ptr = floats;
         1510  +    int endian_arch = check_endian_arch ();
         1511  +    for (cell = 0; cell < columns; cell++)
         1512  +      {
         1513  +	  value = fetch_float (ptr, byteorder, endian_arch);
         1514  +	  triple_store_cell (triplet, cell, value);
         1515  +	  ptr += sizeof (float);
         1516  +      }
         1517  +    return 1;
         1518  +}
         1519  +
         1520  +static void
         1521  +export_geoTiff_shaded_float (struct colorTable *color_table, int colors,
         1522  +			     const char *proj4text, const char *grid_path,
         1523  +			     const char *tiff_path, unsigned char no_red,
         1524  +			     unsigned char no_green, unsigned char no_blue,
         1525  +			     int mono_color, unsigned char mono_red,
         1526  +			     unsigned char mono_green, unsigned char mono_blue,
         1527  +			     double z_factor, double scale_factor,
         1528  +			     double azimuth, double altitude, int verbose)
         1529  +{
         1530  +/* exporting a FLOAT GRID as GeoTIFF */
         1531  +    TIFF *tiff = NULL;
         1532  +    GTIF *gtif = NULL;
         1533  +    int byteorder = BYTE_ORDER_NONE;
         1534  +    int c;
         1535  +    int row = 0;
         1536  +    char buf[1024];
         1537  +    char path[1024];
         1538  +    char *ptr = buf;
         1539  +    int err = 0;
         1540  +    int ncols = -1;
         1541  +    int nrows = -1;
         1542  +    double xllcorner = 0.0;
         1543  +    double yllcorner = 0.0;
         1544  +    double cellsize = 0.0;
         1545  +    double nodata = 0.0;
         1546  +    double tiepoint[6];
         1547  +    double pixsize[3];
         1548  +    struct triple_scan *triplet = NULL;
         1549  +    unsigned char *raster = NULL;
         1550  +    unsigned char *flt_buf = NULL;
         1551  +    size_t rd;
         1552  +    FILE *grid;
         1553  +
         1554  +/* parsing the Grid Header .hdr */
         1555  +    sprintf (path, "%s.hdr", grid_path);
         1556  +    grid = fopen (path, "rb");
         1557  +    if (!grid)
         1558  +      {
         1559  +	  printf ("Open error: %s\n", path);
         1560  +	  return;
         1561  +      }
         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  +
         1619  +/* resizing CellSize */
         1620  +    cellsize = (cellsize * (double) ncols) / (double) (ncols - 2);
         1621  +
         1622  +/* parsing the Grid Cells .flt */
         1623  +    sprintf (path, "%s.flt", grid_path);
         1624  +    grid = fopen (path, "rb");
         1625  +    if (!grid)
         1626  +      {
         1627  +	  printf ("Open error: %s\n", path);
         1628  +	  return;
         1629  +      }
         1630  +/* creating the GeoTIFF file */
         1631  +    tiff = XTIFFOpen (tiff_path, "w");
         1632  +    if (!tiff)
         1633  +      {
         1634  +	  printf ("\tCould not open TIFF image '%s'\n", tiff_path);
         1635  +	  goto stop;
         1636  +      }
         1637  +    gtif = GTIFNew (tiff);
         1638  +    if (!gtif)
         1639  +      {
         1640  +	  printf ("\tCould not open GeoTIFF image '%s'\n", tiff_path);
         1641  +	  goto stop;
         1642  +      }
         1643  +
         1644  +/* writing the TIFF Tags */
         1645  +    TIFFSetField (tiff, TIFFTAG_IMAGEWIDTH, ncols - 2);
         1646  +    TIFFSetField (tiff, TIFFTAG_IMAGELENGTH, nrows - 2);
         1647  +    TIFFSetField (tiff, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
         1648  +    TIFFSetField (tiff, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
         1649  +    TIFFSetField (tiff, TIFFTAG_ROWSPERSTRIP, 1);
         1650  +    TIFFSetField (tiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
         1651  +    TIFFSetField (tiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
         1652  +    TIFFSetField (tiff, TIFFTAG_BITSPERSAMPLE, 8);
         1653  +    TIFFSetField (tiff, TIFFTAG_SAMPLESPERPIXEL, 3);
         1654  +
         1655  +/* writing the GeoTIFF Tags */
         1656  +    pixsize[0] = cellsize;
         1657  +    pixsize[1] = cellsize;
         1658  +    pixsize[2] = 0.0;
         1659  +    TIFFSetField (tiff, GTIFF_PIXELSCALE, 3, pixsize);
         1660  +    tiepoint[0] = 0.0;
         1661  +    tiepoint[1] = 0.0;
         1662  +    tiepoint[2] = 0.0;
         1663  +    tiepoint[3] = xllcorner;
         1664  +    tiepoint[4] = yllcorner + (cellsize * nrows);
         1665  +    tiepoint[5] = 0.0;
         1666  +    TIFFSetField (tiff, GTIFF_TIEPOINTS, 6, tiepoint);
         1667  +    GTIFSetFromProj4 (gtif, proj4text);
         1668  +    GTIFWriteKeys (gtif);
         1669  +    raster = malloc (ncols * 3);
         1670  +    flt_buf = malloc (sizeof (float) * ncols);
         1671  +
         1672  +/* initializing the TripleRow object */
         1673  +    triplet = triple_alloc (ncols);
         1674  +    triple_set_shaded_relief_params (triplet, z_factor, scale_factor, altitude,
         1675  +				     azimuth);
         1676  +    triple_set_monochrome_params (triplet, mono_color, mono_red, mono_green,
         1677  +				  mono_blue);
         1678  +    triple_set_no_data (triplet, nodata, no_red, no_green, no_blue);
         1679  +
         1680  +    for (row = 0; row < nrows; row++)
         1681  +      {
         1682  +	  if (verbose)
         1683  +	    {
         1684  +		fprintf (stderr, "writing scanline %d of %d\n", row + 1, nrows);
         1685  +		fflush (stderr);
         1686  +	    }
         1687  +	  rd = fread (flt_buf, 1, ncols * sizeof (float), grid);
         1688  +	  if (rd != (sizeof (float) * ncols))
         1689  +	    {
         1690  +		printf ("*** Grid read error ***\n");
         1691  +		printf ("An invalid GeoTIFF was generated ... aborting ...\n");
         1692  +		goto stop;
         1693  +	    }
         1694  +	  if (fetch_float_scanline_shaded
         1695  +	      (row + 1, flt_buf, ncols, byteorder, triplet))
         1696  +	    {
         1697  +		if (triple_is_valid (triplet))
         1698  +		  {
         1699  +		      int ret = 1;
         1700  +		      if (!color_table)
         1701  +			  triple_shaded_relief (triplet, raster);
         1702  +		      else
         1703  +			  ret =
         1704  +			      triple_shaded_relief_color (color_table, colors,
         1705  +							  triplet, raster,
         1706  +							  row + 1);
         1707  +		      if (!ret)
         1708  +			  goto stop;
         1709  +		      if (TIFFWriteScanline (tiff, raster, row - 2, 0) < 0)
         1710  +			{
         1711  +			    printf ("\tTIFF write error @ row=%d\n", row);
         1712  +			    printf
         1713  +				("An invalid GeoTIFF was generated ... aborting ...\n");
         1714  +			    goto stop;
         1715  +			}
         1716  +		  }
         1717  +		triple_rotate (triplet);
         1718  +	    }
         1719  +	  else
         1720  +	    {
         1721  +		printf ("*** Grid read error ***\n");
         1722  +		printf ("An invalid GeoTIFF was generated ... aborting ...\n");
         1723  +		goto stop;
         1724  +	    }
         1725  +      }
         1726  +
         1727  +  stop:
         1728  +    if (gtif)
         1729  +	GTIFFree (gtif);
         1730  +    if (tiff)
         1731  +	XTIFFClose (tiff);
         1732  +    if (raster)
         1733  +	free (raster);
         1734  +    if (flt_buf)
         1735  +	free (flt_buf);
         1736  +    fclose (grid);
         1737  +    if (triplet)
         1738  +	triple_free (triplet);
         1739  +}
         1740  +
         1741  +static int
         1742  +fetch_scanline_shaded (int row, FILE * asc, int columns,
         1743  +		       struct triple_scan *triplet)
         1744  +{
         1745  +/* feeding a TIFF scanline from an ASCII Grid */
         1746  +    int c;
         1747  +    int cell = 0;
         1748  +    char buf[1024];
         1749  +    char *ptr = buf;
         1750  +    double value;
         1751  +    while ((c = getc (asc)) != EOF)
         1752  +      {
         1753  +	  if (c == '\n')
         1754  +	      break;
         1755  +	  *ptr++ = c;
         1756  +	  if (c == ' ')
         1757  +	    {
         1758  +		/* value delimiter */
         1759  +		*ptr = '\0';
         1760  +		cell++;
         1761  +		if (cell > columns)
         1762  +		  {
         1763  +		      printf ("Grid row %d: exceding column\n", row);
         1764  +		      return 0;
         1765  +		  }
         1766  +		value = atof (buf);
         1767  +		triple_store_cell (triplet, cell, (float) value);
         1768  +		ptr = buf;
         1769  +	    }
         1770  +      }
         1771  +    return 1;
         1772  +}
         1773  +
         1774  +static void
         1775  +export_geoTiff_shaded_ascii (struct colorTable *color_table, int colors,
         1776  +			     const char *proj4text, const char *grid_path,
         1777  +			     const char *tiff_path, unsigned char no_red,
         1778  +			     unsigned char no_green, unsigned char no_blue,
         1779  +			     int mono_color, unsigned char mono_red,
         1780  +			     unsigned char mono_green, unsigned char mono_blue,
         1781  +			     double z_factor, double scale_factor,
         1782  +			     double azimuth, double altitude, int verbose)
         1783  +{
         1784  +/* exporting an ASCII GRID as GeoTIFF */
         1785  +    TIFF *tiff = NULL;
         1786  +    GTIF *gtif = NULL;
         1787  +    int c;
         1788  +    int row = 0;
         1789  +    char buf[1024];
         1790  +    char *ptr = buf;
         1791  +    int err = 0;
         1792  +    int ncols = -1;
         1793  +    int nrows = -1;
         1794  +    double xllcorner = 0.0;
         1795  +    double yllcorner = 0.0;
         1796  +    double cellsize = 0.0;
         1797  +    double nodata = 0.0;
         1798  +    double tiepoint[6];
         1799  +    double pixsize[3];
         1800  +    unsigned char *raster = NULL;
         1801  +    struct triple_scan *triplet = NULL;
         1802  +    FILE *asc = fopen (grid_path, "rb");
         1803  +    if (!asc)
         1804  +      {
         1805  +	  printf ("Open error: %s\n", grid_path);
         1806  +	  return;
         1807  +      }
         1808  +    while ((c = getc (asc)) != EOF)
         1809  +      {
         1810  +	  if (c == '\r')
         1811  +	    {
         1812  +		/* ignoring Return chars */
         1813  +		continue;
         1814  +	    }
         1815  +	  if (c == '\n')
         1816  +	    {
         1817  +		*ptr = '\0';
         1818  +		switch (row)
         1819  +		  {
         1820  +		  case 0:
         1821  +		      if (!parseIntHeader (buf, "ncols ", &ncols))
         1822  +			  err = 1;
         1823  +		      break;
         1824  +		  case 1:
         1825  +		      if (!parseIntHeader (buf, "nrows ", &nrows))
         1826  +			  err = 1;
         1827  +		      break;
         1828  +		  case 2:
         1829  +		      if (!parseDblHeader (buf, "xllcorner ", &xllcorner))
         1830  +			  err = 1;
         1831  +		      break;
         1832  +		  case 3:
         1833  +		      if (!parseDblHeader (buf, "yllcorner ", &yllcorner))
         1834  +			  err = 1;
         1835  +		      break;
         1836  +		  case 4:
         1837  +		      if (!parseDblHeader (buf, "cellsize ", &cellsize))
         1838  +			  err = 1;
         1839  +		      break;
         1840  +		  case 5:
         1841  +		      if (!parseDblHeader (buf, "NODATA_value ", &nodata))
         1842  +			  err = 1;
         1843  +		      break;
         1844  +		  };
         1845  +		ptr = buf;
         1846  +		row++;
         1847  +		if (row == 6)
         1848  +		    break;
         1849  +		continue;
         1850  +	    }
         1851  +	  *ptr++ = c;
         1852  +      }
         1853  +    if (err)
         1854  +      {
         1855  +	  /* there was some error */
         1856  +	  printf ("Invalid ASCII Grid format in: %s\n", grid_path);
         1857  +	  goto stop;
         1858  +      }
         1859  +
         1860  +/* resizing CellSize */
         1861  +    cellsize = (cellsize * (double) ncols) / (double) (ncols - 2);
         1862  +
         1863  +/* creating the GeoTIFF file */
         1864  +    tiff = XTIFFOpen (tiff_path, "w");
         1865  +    if (!tiff)
         1866  +      {
         1867  +	  printf ("\tCould not open TIFF image '%s'\n", tiff_path);
         1868  +	  goto stop;
         1869  +      }
         1870  +    gtif = GTIFNew (tiff);
         1871  +    if (!gtif)
         1872  +      {
         1873  +	  printf ("\tCould not open GeoTIFF image '%s'\n", tiff_path);
         1874  +	  goto stop;
         1875  +      }
         1876  +
         1877  +/* writing the TIFF Tags */
         1878  +    TIFFSetField (tiff, TIFFTAG_IMAGEWIDTH, ncols - 2);
         1879  +    TIFFSetField (tiff, TIFFTAG_IMAGELENGTH, nrows - 2);
         1880  +    TIFFSetField (tiff, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
         1881  +    TIFFSetField (tiff, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
         1882  +    TIFFSetField (tiff, TIFFTAG_ROWSPERSTRIP, 1);
         1883  +    TIFFSetField (tiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
         1884  +    TIFFSetField (tiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
         1885  +    TIFFSetField (tiff, TIFFTAG_BITSPERSAMPLE, 8);
         1886  +    TIFFSetField (tiff, TIFFTAG_SAMPLESPERPIXEL, 3);
         1887  +
         1888  +/* writing the GeoTIFF Tags */
         1889  +    pixsize[0] = cellsize;
         1890  +    pixsize[1] = cellsize;
         1891  +    pixsize[2] = 0.0;
         1892  +    TIFFSetField (tiff, GTIFF_PIXELSCALE, 3, pixsize);
         1893  +    tiepoint[0] = 0.0;
         1894  +    tiepoint[1] = 0.0;
         1895  +    tiepoint[2] = 0.0;
         1896  +    tiepoint[3] = xllcorner;
         1897  +    tiepoint[4] = yllcorner + (cellsize * nrows);
         1898  +    tiepoint[5] = 0.0;
         1899  +    TIFFSetField (tiff, GTIFF_TIEPOINTS, 6, tiepoint);
         1900  +    GTIFSetFromProj4 (gtif, proj4text);
         1901  +    GTIFWriteKeys (gtif);
         1902  +    raster = malloc (ncols * 3);
         1903  +
         1904  +/* initializing the TripleRow object */
         1905  +    triplet = triple_alloc (ncols);
         1906  +    triple_set_shaded_relief_params (triplet, z_factor, scale_factor, altitude,
         1907  +				     azimuth);
         1908  +    triple_set_monochrome_params (triplet, mono_color, mono_red, mono_green,
         1909  +				  mono_blue);
         1910  +    triple_set_no_data (triplet, nodata, no_red, no_green, no_blue);
         1911  +
         1912  +    for (row = 0; row < nrows; row++)
         1913  +      {
         1914  +	  if (verbose)
         1915  +	    {
         1916  +		fprintf (stderr, "writing scanline %d of %d\n", row + 1, nrows);
         1917  +		fflush (stderr);
         1918  +	    }
         1919  +	  if (fetch_scanline_shaded (row + 1, asc, ncols, triplet))
         1920  +	    {
         1921  +		if (triple_is_valid (triplet))
         1922  +		  {
         1923  +		      int ret = 1;
         1924  +		      if (!color_table)
         1925  +			  triple_shaded_relief (triplet, raster);
         1926  +		      else
         1927  +			  ret =
         1928  +			      triple_shaded_relief_color (color_table, colors,
         1929  +							  triplet, raster,
         1930  +							  row + 1);
         1931  +		      if (!ret)
         1932  +			  goto stop;
         1933  +		      if (TIFFWriteScanline (tiff, raster, row - 2, 0) < 0)
         1934  +			{
         1935  +			    printf ("\tTIFF write error @ row=%d\n", row);
         1936  +			    printf
         1937  +				("An invalid GeoTIFF was generated ... aborting ...\n");
         1938  +			    goto stop;
         1939  +			}
         1940  +		  }
         1941  +		triple_rotate (triplet);
         1942  +	    }
         1943  +	  else
         1944  +	    {
         1945  +		printf ("*** Grid read error ***\n");
         1946  +		printf ("An invalid GeoTIFF was generated ... aborting ...\n");
         1947  +		goto stop;
         1948  +	    }
         1949  +      }
         1950  +
         1951  +  stop:
         1952  +    if (gtif)
         1953  +	GTIFFree (gtif);
         1954  +    if (tiff)
         1955  +	XTIFFClose (tiff);
         1956  +    if (raster)
         1957  +	free (raster);
         1958  +    fclose (asc);
         1959  +    if (triplet)
         1960  +	triple_free (triplet);
         1961  +}
  1050   1962   
  1051   1963   static void
  1052   1964   do_help ()
  1053   1965   {
  1054   1966   /* printing the argument list */
  1055   1967       fprintf (stderr, "\n\nusage: rasterlite_grid ARGLIST\n");
  1056   1968       fprintf (stderr,
................................................................................
  1064   1976       fprintf (stderr,
  1065   1977   	     "-t or --tiff-path     pathname    the GeoTIFF path (output)\n");
  1066   1978       fprintf (stderr,
  1067   1979   	     "-p or --proj4text     proj4text   the PROJ.4 parameters\n");
  1068   1980       fprintf (stderr, "-f or --grid-format   grid-type   [ASCII | FLOAT]\n");
  1069   1981       fprintf (stderr,
  1070   1982   	     "-n or --nodata-color  0xRRGGBB    [default = 0x000000]\n");
  1071         -    fprintf (stderr, "-v or --verbose                   verbose output\n");
         1983  +    fprintf (stderr, "-v or --verbose                   verbose output\n\n");
         1984  +    fprintf (stderr, "Shaded Relief specific arguments:\n");
         1985  +    fprintf (stderr, "---------------------------------\n");
         1986  +    fprintf (stderr,
         1987  +	     "-sr or --shaded-relief            *disabled by default*\n");
         1988  +    fprintf (stderr, "-m or --monochrome    0xRRGGBB    [default = none]\n");
         1989  +    fprintf (stderr, "-z or --z-factor      numeric     [default = 1.0]\n");
         1990  +    fprintf (stderr, "-s or --scale-factor  numeric     [default = 1.0]\n");
         1991  +    fprintf (stderr, "-az or --azimuth      numeric     [default = 315.0]\n");
         1992  +    fprintf (stderr, "-al or --altitude     numeric     [default = 45.0]\n\n");
         1993  +    fprintf (stderr, "Please note: --monochrome and --color-path are\n");
         1994  +    fprintf (stderr, "mutually exclusive options\n");
  1072   1995   }
  1073   1996   
  1074   1997   int
  1075   1998   main (int argc, char *argv[])
  1076   1999   {
  1077   2000   /* the MAIN function simply perform arguments checking */
  1078   2001       int i;
................................................................................
  1085   2008       int verbose = 0;
  1086   2009       int error = 0;
  1087   2010       struct colorTable *color_table = NULL;
  1088   2011       unsigned char no_red = 0;
  1089   2012       unsigned char no_green = 0;
  1090   2013       unsigned char no_blue = 0;
  1091   2014       char error_nodata_color[1024];
         2015  +    int shaded_relief = 0;
         2016  +    int mono_color = 0;
         2017  +    unsigned char mono_red = 0;
         2018  +    unsigned char mono_green = 0;
         2019  +    unsigned char mono_blue = 0;
         2020  +    char error_mono_color[1024];
         2021  +    double z_factor = 1.0;
         2022  +    double scale_factor = 1.0;
         2023  +    double azimuth = 315.0;
         2024  +    double altitude = 45.0;
         2025  +    int colors;
  1092   2026       *error_nodata_color = '\0';
         2027  +    *error_mono_color = '\0';
  1093   2028       for (i = 1; i < argc; i++)
  1094   2029         {
  1095   2030   	  /* parsing the invocation arguments */
  1096   2031   	  if (next_arg != ARG_NONE)
  1097   2032   	    {
  1098   2033   		switch (next_arg)
  1099   2034   		  {
................................................................................
  1115   2050   		      if (strcasecmp (argv[i], "FLOAT") == 0)
  1116   2051   			  grid_type = FLOAT_GRID;
  1117   2052   		      break;
  1118   2053   		  case ARG_NODATA_COLOR:
  1119   2054   		      if (!parse_rgb (argv[i], &no_red, &no_green, &no_blue))
  1120   2055   			  strcpy (error_nodata_color, argv[i]);
  1121   2056   		      break;
         2057  +		  case ARG_MONO_COLOR:
         2058  +		      if (!parse_rgb
         2059  +			  (argv[i], &mono_red, &mono_green, &mono_blue))
         2060  +			  strcpy (error_mono_color, argv[i]);
         2061  +		      else
         2062  +			  mono_color = 1;
         2063  +		      break;
         2064  +		  case ARG_Z_FACTOR:
         2065  +		      z_factor = atof (argv[i]);
         2066  +		      break;
         2067  +		  case ARG_SCALE:
         2068  +		      scale_factor = atof (argv[i]);
         2069  +		      break;
         2070  +		  case ARG_AZIMUTH:
         2071  +		      azimuth = atof (argv[i]);
         2072  +		      break;
         2073  +		  case ARG_ALTITUDE:
         2074  +		      altitude = atof (argv[i]);
         2075  +		      break;
  1122   2076   		  };
  1123   2077   		next_arg = ARG_NONE;
  1124   2078   		continue;
  1125   2079   	    }
  1126   2080   	  if (strcasecmp (argv[i], "--help") == 0
  1127   2081   	      || strcmp (argv[i], "-?") == 0)
  1128   2082   	    {
................................................................................
  1194   2148   		verbose = 1;
  1195   2149   		continue;
  1196   2150   	    }
  1197   2151   	  if (strcmp (argv[i], "-v") == 0)
  1198   2152   	    {
  1199   2153   		verbose = 1;
  1200   2154   		continue;
         2155  +	    }
         2156  +	  if (strcasecmp (argv[i], "--shared-relief") == 0)
         2157  +	    {
         2158  +		shaded_relief = 1;
         2159  +		continue;
         2160  +	    }
         2161  +	  if (strcmp (argv[i], "-sr") == 0)
         2162  +	    {
         2163  +		shaded_relief = 1;
         2164  +		continue;
         2165  +	    }
         2166  +	  if (strcmp (argv[i], "-m") == 0)
         2167  +	    {
         2168  +		next_arg = ARG_MONO_COLOR;
         2169  +		continue;
         2170  +	    }
         2171  +	  if (strcasecmp (argv[i], "--monochrome") == 0)
         2172  +	    {
         2173  +		next_arg = ARG_MONO_COLOR;
         2174  +		continue;
         2175  +	    }
         2176  +	  if (strcmp (argv[i], "-z") == 0)
         2177  +	    {
         2178  +		next_arg = ARG_Z_FACTOR;
         2179  +		continue;
         2180  +	    }
         2181  +	  if (strcasecmp (argv[i], "--z-factor") == 0)
         2182  +	    {
         2183  +		next_arg = ARG_Z_FACTOR;
         2184  +		continue;
         2185  +	    }
         2186  +	  if (strcmp (argv[i], "-s") == 0)
         2187  +	    {
         2188  +		next_arg = ARG_SCALE;
         2189  +		continue;
         2190  +	    }
         2191  +	  if (strcasecmp (argv[i], "--scale-factor") == 0)
         2192  +	    {
         2193  +		next_arg = ARG_SCALE;
         2194  +		continue;
         2195  +	    }
         2196  +	  if (strcmp (argv[i], "-az") == 0)
         2197  +	    {
         2198  +		next_arg = ARG_AZIMUTH;
         2199  +		continue;
         2200  +	    }
         2201  +	  if (strcasecmp (argv[i], "--azimuth") == 0)
         2202  +	    {
         2203  +		next_arg = ARG_AZIMUTH;
         2204  +		continue;
         2205  +	    }
         2206  +	  if (strcmp (argv[i], "-al") == 0)
         2207  +	    {
         2208  +		next_arg = ARG_ALTITUDE;
         2209  +		continue;
         2210  +	    }
         2211  +	  if (strcasecmp (argv[i], "--altitude") == 0)
         2212  +	    {
         2213  +		next_arg = ARG_ALTITUDE;
         2214  +		continue;
  1201   2215   	    }
  1202   2216   	  fprintf (stderr, "unknown argument: %s\n", argv[i]);
  1203   2217   	  error = 1;
  1204   2218         }
  1205   2219       if (error)
  1206   2220         {
  1207   2221   	  do_help ();
................................................................................
  1210   2224   /* checking the arguments */
  1211   2225       if (!grid_path)
  1212   2226         {
  1213   2227   	  fprintf (stderr,
  1214   2228   		   "did you forget setting the --grid-path argument ?\n");
  1215   2229   	  error = 1;
  1216   2230         }
  1217         -    if (!color_path)
         2231  +    if (!shaded_relief)
  1218   2232         {
  1219         -	  fprintf (stderr,
  1220         -		   "did you forget setting the --color-path argument ?\n");
  1221         -	  error = 1;
         2233  +	  if (!color_path)
         2234  +	    {
         2235  +		fprintf (stderr,
         2236  +			 "did you forget setting the --color-path argument ?\n");
         2237  +		error = 1;
         2238  +	    }
  1222   2239         }
  1223   2240       if (!tiff_path)
  1224   2241         {
  1225   2242   	  fprintf (stderr,
  1226   2243   		   "did you forget setting the --tiff-path argument ?\n");
  1227   2244   	  error = 1;
  1228   2245         }
................................................................................
  1238   2255   		   "did you forget setting the --grid-format argument ?\n");
  1239   2256   	  error = 1;
  1240   2257         }
  1241   2258       if (strlen (error_nodata_color))
  1242   2259         {
  1243   2260   	  printf ("invalid NODATA color '%s'\n", error_nodata_color);
  1244   2261   	  error = 1;
         2262  +      }
         2263  +    if (strlen (error_mono_color))
         2264  +      {
         2265  +	  printf ("invalid Shaded Relief Monochrome color '%s'\n",
         2266  +		  error_mono_color);
         2267  +	  error = 1;
         2268  +      }
         2269  +    if (color_path && mono_color)
         2270  +      {
         2271  +	  printf ("--monochrome and --color-path are mutually exclusive\n");
         2272  +	  error = 1;
  1245   2273         }
  1246   2274       if (error)
  1247   2275         {
  1248   2276   	  do_help ();
  1249   2277   	  return -1;
  1250   2278         }
  1251   2279       printf ("=====================================================\n");
  1252   2280       printf ("             Arguments Summary\n");
  1253   2281       printf ("=====================================================\n");
  1254   2282       printf ("Grid       pathname: '%s'\n", grid_path);
  1255         -    printf ("ColorTable pathname: '%s'\n", color_path);
         2283  +    if (color_path)
         2284  +	printf ("ColorTable pathname: '%s'\n", color_path);
  1256   2285       printf ("GeoTIFF    pathname: '%s'\n", tiff_path);
  1257   2286       printf ("PROJ.4       string: '%s'\n", proj4text);
         2287  +    printf ("NoData        color: 0x%02x%02x%02x\n", no_red, no_green, no_blue);
  1258   2288       switch (grid_type)
  1259   2289         {
  1260   2290         case ASCII_GRID:
  1261   2291   	  printf ("Grid Format: ASCII\n");
  1262   2292   	  break;
  1263   2293         case FLOAT_GRID:
  1264   2294   	  printf ("Grid Format: FLOAT\n");
  1265   2295   	  break;
  1266   2296         default:
  1267   2297   	  printf ("Grid Format: UNKNOWN\n");
  1268   2298   	  break;
         2299  +      }
         2300  +    if (shaded_relief)
         2301  +      {
         2302  +	  printf ("\n           Shaded Relief arguments:\n");
         2303  +	  printf ("-----------------------------------------------------\n");
         2304  +	  printf ("Z-factor     : %1.4f\n", z_factor);
         2305  +	  printf ("Scale-factor : %1.4f\n", scale_factor);
         2306  +	  printf ("Azimuth      : %1.4f\n", azimuth);
         2307  +	  printf ("Altitude     : %1.4f\n", altitude);
         2308  +	  if (mono_color)
         2309  +	      printf ("Monochrome   : 0x%02x%02x%02x\n", mono_red, mono_green,
         2310  +		      mono_blue);
  1269   2311         }
  1270   2312       printf ("=====================================================\n\n");
  1271         -    color_table = parse_color_table (color_path);
  1272         -    if (!color_table)
         2313  +    if (color_path)
  1273   2314         {
  1274         -	  fprintf (stderr, "\n*********** Invalid Color Table\n");
  1275         -	  return -1;
         2315  +	  color_table = parse_color_table (color_path, &colors);
         2316  +	  if (!color_table)
         2317  +	    {
         2318  +		fprintf (stderr, "\n*********** Invalid Color Table\n");
         2319  +		return -1;
         2320  +	    }
  1276   2321         }
  1277         -    if (grid_type == FLOAT_GRID)
  1278         -	export_geoTiff_float (color_table, proj4text, grid_path, tiff_path,
  1279         -			      no_red, no_green, no_blue, verbose);
         2322  +    if (shaded_relief)
         2323  +      {
         2324  +	  if (grid_type == FLOAT_GRID)
         2325  +	      export_geoTiff_shaded_float (color_table, colors, proj4text,
         2326  +					   grid_path, tiff_path, no_red,
         2327  +					   no_green, no_blue, mono_color,
         2328  +					   mono_red, mono_green, mono_blue,
         2329  +					   z_factor, scale_factor, azimuth,
         2330  +					   altitude, verbose);
         2331  +	  else
         2332  +	      export_geoTiff_shaded_ascii (color_table, colors, proj4text,
         2333  +					   grid_path, tiff_path, no_red,
         2334  +					   no_green, no_blue, mono_color,
         2335  +					   mono_red, mono_green, mono_blue,
         2336  +					   z_factor, scale_factor, azimuth,
         2337  +					   altitude, verbose);
         2338  +      }
  1280   2339       else
  1281         -	export_geoTiff_ascii (color_table, proj4text, grid_path, tiff_path,
  1282         -			      no_red, no_green, no_blue, verbose);
  1283         -    free (color_table);
         2340  +      {
         2341  +	  if (grid_type == FLOAT_GRID)
         2342  +	      export_geoTiff_float (color_table, colors, proj4text, grid_path,
         2343  +				    tiff_path, no_red, no_green, no_blue,
         2344  +				    verbose);
         2345  +	  else
         2346  +	      export_geoTiff_ascii (color_table, colors, proj4text, grid_path,
         2347  +				    tiff_path, no_red, no_green, no_blue,
         2348  +				    verbose);
         2349  +      }
         2350  +    if (color_table)
         2351  +	free (color_table);
  1284   2352       return 0;
  1285   2353   }

Changes to src/rasterlite_load.c.

  1203   1203       if (ret != SQLITE_OK)
  1204   1204         {
  1205   1205   	  printf ("COMMIT TRANSACTION error: %s\n", sql_err);
  1206   1206   	  sqlite3_free (sql_err);
  1207   1207   	  goto stop;
  1208   1208         }
  1209   1209   
         1210  +    if (tif)
         1211  +	XTIFFClose (tif);
         1212  +    if (gtif)
         1213  +	GTIFFree (gtif);
  1210   1214       for (i = 0; i < NTILES; i++)
  1211   1215         {
  1212   1216   	  tile = &(infos.tiles[i]);
  1213   1217   	  if (tile->geometry)
  1214   1218   	      gaiaFreeGeomColl (tile->geometry);
  1215   1219         }
  1216   1220