[GRASS-SVN] r42410 - grass/trunk/raster/r.out.png

svn_grass at osgeo.org svn_grass at osgeo.org
Mon May 31 22:54:14 EDT 2010


Author: hamish
Date: 2010-05-31 22:54:13 -0400 (Mon, 31 May 2010)
New Revision: 42410

Modified:
   grass/trunk/raster/r.out.png/main.c
   grass/trunk/raster/r.out.png/r.out.png.html
Log:
add flag to export a world file; rename some variables (wish #1077)

Modified: grass/trunk/raster/r.out.png/main.c
===================================================================
--- grass/trunk/raster/r.out.png/main.c	2010-05-31 14:08:29 UTC (rev 42409)
+++ grass/trunk/raster/r.out.png/main.c	2010-06-01 02:54:13 UTC (rev 42410)
@@ -26,6 +26,7 @@
 
 #include <string.h>
 #include <stdlib.h>
+#include <float.h>
 
 #ifndef _MYINCLUDE_H
 #define _MYINCLUDE_H
@@ -42,13 +43,17 @@
 
 typedef int FILEDESC;
 
+/* global functions */
+static int write_wld(const char *, const struct Cell_head *);
+
+
 int main(int argc, char *argv[])
 {
     struct GModule *module;
     struct Option *rast, *png_file, *compr; /* , *bgcolor; */
-    struct Flag *alpha;
+    struct Flag *alpha, *wld_flag;
     char *rastermap;
-    char *basename = NULL, *ofile;
+    char *basename = NULL, *outfile = NULL;
     unsigned char *set, *ored, *ogrn, *oblu;
     int def_red, def_grn, def_blu;
     CELL *cell_buf;
@@ -58,28 +63,28 @@
     int rtype, row, col, do_stdout = 0;
     size_t rsize;
     int png_compr, ret, do_alpha;
-    struct Cell_head w;
+    struct Cell_head win;
     FILEDESC cellfile = 0;
     FILE *fp;
 
     /* now goes from pnmtopng.c* -A.Sh */
     /*
-     * * pnmtopng.c -
-     * * read a portable anymap and produce a Portable Network Graphics file
-     * *
-     * * derived from pnmtorast.c (c) 1990,1991 by Jef Poskanzer and some
-     * * parts derived from ppmtogif.c by Marcel Wijkstra <wijkstra at fwi.uva.nl>
-     * * thanks to Greg Roelofs <newt at pobox.com> for contributions and bug-fixes
-     * *
-     * * Copyright (C) 1995-1998 by Alexander Lehmann <alex at hal.rhein-main.de>
-     * *			and Willem van Schaik <willem at schaik.com>
-     * *
-     * * Permission to use, copy, modify, and distribute this software and its
-     * * documentation for any purpose and without fee is hereby granted, provided
-     * * that the above copyright notice appear in all copies and that both that
-     * * copyright notice and this permission notice appear in supporting
-     * * documentation.  This software is provided "as is" without express or
-     * * implied warranty.
+     * pnmtopng.c -
+     * read a portable anymap and produce a Portable Network Graphics file
+     *
+     * derived from pnmtorast.c (c) 1990,1991 by Jef Poskanzer and some
+     * parts derived from ppmtogif.c by Marcel Wijkstra <wijkstra at fwi.uva.nl>
+     * thanks to Greg Roelofs <newt at pobox.com> for contributions and bug-fixes
+     *
+     * Copyright (C) 1995-1998 by Alexander Lehmann <alex at hal.rhein-main.de>
+     *  		      and Willem van Schaik <willem at schaik.com>
+     *
+     * Permission to use, copy, modify, and distribute this software and its
+     * documentation for any purpose and without fee is hereby granted, provided
+     * that the above copyright notice appear in all copies and that both that
+     * copyright notice and this permission notice appear in supporting
+     * documentation.  This software is provided "as is" without express or
+     * implied warranty.
      */
 
     png_struct *png_ptr;
@@ -132,6 +137,10 @@
     alpha->key = 't';
     alpha->description = _("Make NULL cells transparent");
 
+    wld_flag = G_define_flag();
+    wld_flag->key = 'w';
+    wld_flag->description = _("Output world file");
+
     /* see what can be done to convert'em -A.Sh.
      * gscale = G_define_flag ();
      * gscale->key = 'g';
@@ -153,9 +162,8 @@
 
     if (basename) {
 	G_basename(basename, "png");
-	ofile = G_malloc(strlen(basename) + 5);
-	sprintf(ofile, "%s.png", basename);
-	G_free(basename);
+	outfile = G_malloc(strlen(basename) + 5);
+	sprintf(outfile, "%s.png", basename);
     }
 
     png_compr = atoi(compr->answer);
@@ -173,10 +181,10 @@
     ret = G_str_to_color(DEFAULT_BG_COLOR, &def_red, &def_grn, &def_blu);
 #endif
 
-    /*G_get_set_window (&w); *//* 10/99 MN: check for current region */
-    G_get_window(&w);
+    /*G_get_set_window (&win); *//* 10/99 MN: check for current region */
+    G_get_window(&win);
 
-    G_debug(1, "rows = %d, cols = %d", w.rows, w.cols);
+    G_debug(1, "rows = %d, cols = %d", win.rows, win.cols);
 
     /* open raster map for reading */
     cellfile = Rast_open_old(rast->answer, "");
@@ -185,21 +193,18 @@
     fcell_buf = Rast_allocate_f_buf();
     dcell_buf = Rast_allocate_d_buf();
 
-    ored = G_malloc(w.cols);
-    ogrn = G_malloc(w.cols);
-    oblu = G_malloc(w.cols);
-    set  = G_malloc(w.cols);
+    ored = G_malloc(win.cols);
+    ogrn = G_malloc(win.cols);
+    oblu = G_malloc(win.cols);
+    set  = G_malloc(win.cols);
 
     /* open png file for writing */
-    {
-	if (do_stdout)
-	    fp = stdout;
-	else if (NULL == (fp = fopen(ofile, "w")))
-	    G_fatal_error(_("Unable to open output file <%s>"), ofile);
-	else
-	    G_free(ofile);
-    }
+    if (do_stdout)
+	fp = stdout;
+    else if (NULL == (fp = fopen(outfile, "w")))
+	G_fatal_error(_("Unable to open output file <%s>"), outfile);
 
+
     png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
 				      &pnmtopng_jmpbuf_struct,
 				      pnmtopng_error_handler, NULL);
@@ -228,8 +233,8 @@
     png_info_init(info_ptr);
 #endif
     png_init_io(png_ptr, fp);
-    info_ptr->width = w.cols;
-    info_ptr->height = w.rows;
+    info_ptr->width = win.cols;
+    info_ptr->height = win.rows;
     info_ptr->bit_depth = depth;
 
     /* explicit filter-type (or none) required */
@@ -281,18 +286,18 @@
 	    png_set_packing(png_ptr);
 
 	    /* max: 3 color channels, one alpha channel, 16-bit */
-	    line = (png_byte *) G_malloc(w.cols * 8 * sizeof(char));
+	    line = (png_byte *) G_malloc(win.cols * 8 * sizeof(char));
 
-	    for (row = 0; row < w.rows; row++) {
+	    for (row = 0; row < win.rows; row++) {
 
-		G_percent(row, w.rows, 5);
+		G_percent(row, win.rows, 5);
 		Rast_get_row(cellfile, (void *)voidc, row, rtype);
 		Rast_lookup_colors((void *)voidc, ored, ogrn, oblu, set,
-				   w.cols, &colors, rtype);
+				   win.cols, &colors, rtype);
 
 		pp = line;
 
-		for (col = 0; col < w.cols; col++) {
+		for (col = 0; col < win.cols; col++) {
 
 		    if (set[col]) {
 			*pp++ = ored[col];
@@ -324,7 +329,7 @@
 		png_write_row(png_ptr, line);
 
 	    }
-	    G_percent(row, w.rows, 5); /* finish it off */
+	    G_percent(row, win.rows, 5); /* finish it off */
 	}
 	else {			/* GREYSCALE IMAGE */
 
@@ -341,6 +346,7 @@
 	Rast_free_colors(&colors);
 
     }
+
     G_free(cell_buf);
     G_free(fcell_buf);
     G_free(dcell_buf);
@@ -350,19 +356,32 @@
     G_free(set);
     Rast_close(cellfile);
 
-
     png_write_end(png_ptr, info_ptr);
     /* png_write_destroy (png_ptr); this is no longer supported with libpng, al 11/2000 */
     /* flush first because G_free (png_ptr) can segfault due to jmpbuf problems
      * in png_write_destroy */
+
     fflush(stdout);
     /* G_free (png_ptr); */
     /* G_free (info_ptr); */
     png_destroy_write_struct(&png_ptr, &info_ptr);	/* al 11/2000 */
 
-
     fclose(fp);
 
+    if (wld_flag->answer) {
+	if(do_stdout)
+	    outfile = G_store("png_map.wld");
+	else
+	    sprintf(outfile, "%s.wld", basename);
+
+	write_wld(outfile, &win);
+    }
+
+    if(basename)
+	G_free(basename);
+    if(outfile)
+	G_free(outfile);
+
     exit(EXIT_SUCCESS);
 }
 
@@ -396,3 +415,29 @@
 
     longjmp(jmpbuf_ptr->jmpbuf, 1);
 }
+
+
+static int write_wld(const char *fname, const struct Cell_head *win)
+{
+    int width = DBL_DIG;
+    FILE *ofile;
+
+    G_verbose_message(_("Writing world file"));
+
+    if (fname == NULL)
+	G_fatal_error(_("Got null file name"));
+    if (win == NULL)
+	G_fatal_error(_("Got null region struct"));
+    if ((ofile = fopen(fname, "w")) == NULL)
+	G_fatal_error(_("Unable to open world file for writing"));
+
+    fprintf(ofile, "%36.*f \n", width, win->ew_res);
+    fprintf(ofile, "%36.*f \n", width, 0.0);
+    fprintf(ofile, "%36.*f \n", width, 0.0);
+    fprintf(ofile, "%36.*f \n", width, -1 * win->ns_res);
+    fprintf(ofile, "%36.*f \n", width, win->west + win->ew_res / 2.0);
+    fprintf(ofile, "%36.*f \n", width, win->north - win->ns_res / 2.0);
+
+    fclose(ofile);
+    return 0;
+}

Modified: grass/trunk/raster/r.out.png/r.out.png.html
===================================================================
--- grass/trunk/raster/r.out.png/r.out.png.html	2010-05-31 14:08:29 UTC (rev 42409)
+++ grass/trunk/raster/r.out.png/r.out.png.html	2010-06-01 02:54:13 UTC (rev 42410)
@@ -3,19 +3,27 @@
 <em>r.out.png</em> exports a GRASS raster map in non-georeferenced PNG image
 format, respecting the current region resolution and bounds.
 
+<p>
+Optionally the user can choose to export a World File to provide basic
+georeferencing support. When used with the transparency flag this can
+create images useful for KML, TMS, or WMS overlays. (e.g. for use in
+Google Earth or as OpenLayers tiles)
 
+
 <h2>SEE ALSO</h2>
 
 <em>
 <a href="r.out.gdal.html">r.out.gdal</a>,
+<a href="r.out.ppm.html">r.out.ppm</a>,
 <a href="r.out.tiff.html">r.out.tiff</a>,
 <a href="r.out.ascii.html">r.out.ascii</a>
 </em>
 
 
-<h2>AUTHOR</h2>
+<h2>AUTHORS</h2>
 
-Alex Shevlakov
+Alex Shevlakov<BR>
+Hamish Bowman
 
 <p>
 <i>Last changed: $Date$</i>



More information about the grass-commit mailing list