[GRASS-SVN] r41213 - grass/branches/develbranch_6/raster/r.out.png
svn_grass at osgeo.org
svn_grass at osgeo.org
Sun Feb 28 00:44:47 EST 2010
Author: hamish
Date: 2010-02-28 00:44:45 -0500 (Sun, 28 Feb 2010)
New Revision: 41213
Modified:
grass/branches/develbranch_6/raster/r.out.png/description.html
grass/branches/develbranch_6/raster/r.out.png/pngfunc.h
grass/branches/develbranch_6/raster/r.out.png/r.out.png.c
Log:
new -t flag for transpatency; new compression= option;
i18n and msg cleanup, EXIT_SUCCESS, G_define_standard_option();
think about bgcolor= option
(trac #967)
Modified: grass/branches/develbranch_6/raster/r.out.png/description.html
===================================================================
--- grass/branches/develbranch_6/raster/r.out.png/description.html 2010-02-28 02:18:51 UTC (rev 41212)
+++ grass/branches/develbranch_6/raster/r.out.png/description.html 2010-02-28 05:44:45 UTC (rev 41213)
@@ -1,16 +1,22 @@
<h2>DESCRIPTION</h2>
-<em>r.out.png</em> exports a GRASS raster map as non-georeferenced PNG image
-format
+<em>r.out.png</em> exports a GRASS raster map in non-georeferenced PNG image
+format, respecting the current region resolution and bounds.
+
<h2>SEE ALSO</h2>
-<em><a href="r.out.tiff.html">r.out.tiff</a>,</em>
-<em><a href="r.out.ascii.html">r.out.ascii</a></em>
+<em>
+<a href="r.out.gdal.html">r.out.gdal</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>
Alex Shevlakov
-<p><i>Last changed: $Date$</i>
+<p>
+<i>Last changed: $Date$</i>
Modified: grass/branches/develbranch_6/raster/r.out.png/pngfunc.h
===================================================================
--- grass/branches/develbranch_6/raster/r.out.png/pngfunc.h 2010-02-28 02:18:51 UTC (rev 41212)
+++ grass/branches/develbranch_6/raster/r.out.png/pngfunc.h 2010-02-28 05:44:45 UTC (rev 41213)
@@ -25,7 +25,6 @@
#if 0
/* unused */
static int filter = -1;
-static int compression = Z_DEFAULT_COMPRESSION;
#endif
static jmpbuf_wrapper pnmtopng_jmpbuf_struct;
Modified: grass/branches/develbranch_6/raster/r.out.png/r.out.png.c
===================================================================
--- grass/branches/develbranch_6/raster/r.out.png/r.out.png.c 2010-02-28 02:18:51 UTC (rev 41212)
+++ grass/branches/develbranch_6/raster/r.out.png/r.out.png.c 2010-02-28 05:44:45 UTC (rev 41213)
@@ -4,8 +4,9 @@
* MODULE: r.out.png
* AUTHOR(S): Bill Brown - USA-CERL
* Alex Shevlakov - sixote at yahoo.com
+ * Hamish Bowman
* PURPOSE: Export GRASS raster as non-georeferenced PNG image.
- * COPYRIGHT: (C) 2000 by the GRASS Development Team
+ * COPYRIGHT: (C) 2000-2010 by the GRASS Development Team
*
* This program is free software under the GNU General Public
* License (>=v2). Read the file COPYING that comes with GRASS
@@ -14,18 +15,15 @@
*****************************************************************************/
/*
+ * Alex Shevlakov, sixote at yahoo.com, 03/2000
* based on r.out.ppm by
* Written by Bill Brown, USA-CERL March 21, 1994
*
- */
-
-/* Use to convert grass raster map to PNG
+ * Use to convert grass raster map to PNG
* uses currently selected region
+ *
*/
-/* Alex Shevlakov, sixote at yahoo.com, 03/2000
- */
-
#include <string.h>
#include <stdlib.h>
@@ -39,49 +37,47 @@
#include <grass/gis.h>
#include <grass/glocale.h>
-#define DEF_RED 255
-#define DEF_GRN 255
-#define DEF_BLU 255
typedef int FILEDESC;
int main(int argc, char *argv[])
{
struct GModule *module;
- struct Option *rast, *png_file;
-
- /* please, remove before GRASS 7 released */
- struct Flag *bequiet;
+ struct Option *rast, *png_file, *compr; /* , *bgcolor; */
+ struct Flag *bequiet, *alpha;
char *cellmap, *map, *p, *basename = NULL, *ofile;
char rastermap[1024];
unsigned char *set, *ored, *ogrn, *oblu;
+ int def_red, def_grn, def_blu;
CELL *cell_buf;
FCELL *fcell_buf;
DCELL *dcell_buf;
void *voidc;
int rtype, row, col, do_stdout = 0;
+ size_t rsize;
+ int png_compr, ret, do_alpha;
struct Cell_head w;
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;
@@ -105,41 +101,49 @@
G_gisinit(argv[0]);
- rast = G_define_option();
- rast->key = "input";
- rast->type = TYPE_STRING;
- rast->required = YES;
- rast->multiple = NO;
- rast->gisprompt = "old,cell,Raster";
- rast->description = "Raster file to be converted.";
+ module = G_define_module();
+ module->keywords = _("raster, png");
+ module->description =
+ _("Export GRASS raster as non-georeferenced PNG image.");
- png_file = G_define_option();
- png_file->key = "output";
- png_file->type = TYPE_STRING;
+ rast = G_define_standard_option(G_OPT_R_INPUT);
+
+ png_file = G_define_standard_option(G_OPT_F_OUTPUT);
png_file->required = NO;
- png_file->multiple = NO;
png_file->answer = "<rasterfilename>.png";
- png_file->description = "Name for new PNG file. (use out=- for stdout)";
+ png_file->description = _("Name for new PNG file (use out=- for stdout)");
+ compr = G_define_option();
+ compr->key = "compression";
+ compr->type = TYPE_INTEGER;
+ compr->required = NO;
+ compr->multiple = NO;
+ compr->options = "0-9";
+ compr->label = _("Compression level of PNG file");
+ compr->description = _("(0 = none, 1 = fastest, 9 = best)");
+ compr->answer = "6";
+
+/* bgcolor = G_define_standard_option(G_OPT_C_BG); */
+
/* please, remove before GRASS 7 released */
bequiet = G_define_flag();
bequiet->key = 'q';
- bequiet->description = "Run quietly";
+ bequiet->description = _("Run quietly");
+ alpha = G_define_flag();
+ alpha->key = 't';
+ alpha->description = _("Make NULL cells transparent");
+
/* see what can be done to convert'em -A.Sh.
* gscale = G_define_flag ();
* gscale->key = 'G';
* gscale->description = "Output greyscale instead of color";
*/
- module = G_define_module();
- module->keywords = _("raster");
- module->description =
- "Export GRASS raster as non-georeferenced PNG image format.";
-
if (G_parser(argc, argv))
exit(EXIT_FAILURE);
+
/* please, remove before GRASS 7 released */
if (bequiet->answer) {
putenv("GRASS_VERBOSE=0");
@@ -147,14 +151,16 @@
"in future. Please use '--quiet' instead."));
}
+ do_alpha = alpha->answer ? TRUE : FALSE;
+
strncpy(rastermap, rast->answer, 1024 * sizeof(char));
if (strcmp(png_file->answer, "<rasterfilename>.png")) {
if (strcmp(png_file->answer, "-"))
basename = G_store(png_file->answer);
else
- do_stdout = 1;
+ do_stdout = TRUE;
}
else {
map = p = rast->answer;
@@ -173,19 +179,36 @@
G_free(basename);
}
+ png_compr = atoi(compr->answer);
+
+#ifdef MAYBE_LATER
+ /* ... if at all */
+ ret = G_str_to_color(bgcolor->answer, &def_red, &def_grn, &def_blu);
+ if (ret == 0)
+ G_fatal_error(_("[%s]: No such color"), bgcolor->answer);
+ else if (ret == 2) { /* (ret==2) is "none" */
+ if(!do_alpha)
+ do_alpha = TRUE;
+ }
+#else
+ ret = G_str_to_color(DEFAULT_BG_COLOR, &def_red, &def_grn, &def_blu);
+#endif
+
+/* png_set_background() */
+
/*G_get_set_window (&w); *//* 10/99 MN: check for current region */
G_get_window(&w);
- G_message(_("rows = %d, cols = %d"), w.rows, w.cols);
+ G_debug(1, "rows = %d, cols = %d", w.rows, w.cols);
/* open raster map for reading */
{
cellmap = G_find_file2("cell", rastermap, "");
if (!cellmap)
- G_fatal_error("Couldn't find raster map %s", rastermap);
+ G_fatal_error(_("Raster map <%s> not found"), rastermap);
if ((cellfile = G_open_cell_old(rast->answer, cellmap)) == -1)
- G_fatal_error("Not able to open cellfile for [%s]", rastermap);
+ G_fatal_error(_("Unable to open raster map <%s>"), rastermap);
}
cell_buf = G_allocate_c_raster_buf();
@@ -202,7 +225,7 @@
if (do_stdout)
fp = stdout;
else if (NULL == (fp = fopen(ofile, "w")))
- G_fatal_error("Not able to open file for [%s]", ofile);
+ G_fatal_error(_("Unable to open output file <%s>"), ofile);
else
G_free(ofile);
}
@@ -244,14 +267,11 @@
png_set_filter(png_ptr, 0, filter);
}
- /* zlib compression-level (or none) required */
- /* ((compression >= -1) && (compression <= 9)) */
- /* { */
- png_set_compression_level(png_ptr, Z_DEFAULT_COMPRESSION);
- /* } */
+ png_set_compression_level(png_ptr, png_compr);
- G_message(_("Converting %s..."), rast->answer);
+ G_verbose_message(_("Converting <%s>..."), rast->answer);
+
{
struct Colors colors;
@@ -265,14 +285,18 @@
else if (rtype == DCELL_TYPE)
voidc = (DCELL *) dcell_buf;
else
- exit(EXIT_FAILURE);
+ G_fatal_error(_("Raster <%s> type mismatch"), rast->answer);
+ rsize = G_raster_size(rtype);
+
/*if(!gscale->answer){ *//* 24BIT COLOR IMAGE */
- if (1) {
+ if (TRUE) {
+ if (do_alpha)
+ info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
+ else
+ info_ptr->color_type = PNG_COLOR_TYPE_RGB;
- info_ptr->color_type = PNG_COLOR_TYPE_RGB;
-
/* write the png-info struct */
png_write_info(png_ptr, info_ptr);
@@ -285,8 +309,10 @@
for (row = 0; row < w.rows; row++) {
G_percent(row, w.rows, 5);
+
if (G_get_raster_row(cellfile, (void *)voidc, row, rtype) < 0)
- exit(EXIT_FAILURE);
+ G_fatal_error(_("Unable to read raster map row %d"), row);
+
G_lookup_raster_colors((void *)voidc, ored, ogrn, oblu, set,
w.cols, &colors, rtype);
@@ -298,20 +324,32 @@
*pp++ = ored[col];
*pp++ = ogrn[col];
*pp++ = oblu[col];
+ if (do_alpha) {
+ if (G_is_null_value(G_incr_void_ptr(voidc, col * rsize), rtype))
+ *pp++ = 0;
+ else
+ *pp++ = 255;
+ }
}
else {
- *pp++ = DEF_RED;
- *pp++ = DEF_GRN;
- *pp++ = DEF_BLU;
+ if (do_alpha) {
+ *pp++ = ored[col];
+ *pp++ = ogrn[col];
+ *pp++ = oblu[col];
+ *pp++ = 0;
+ }
+ else {
+ *pp++ = (unsigned char)def_red;
+ *pp++ = (unsigned char)def_grn;
+ *pp++ = (unsigned char)def_blu;
+ }
}
-
-
}
png_write_row(png_ptr, line);
}
-
+ G_percent(row, w.rows, 5); /* finish it off */
}
else { /* GREYSCALE IMAGE */
@@ -322,7 +360,7 @@
/* pm_message ("don't know yet how to write grey - yumm!!"); */
- G_warning("don't know how to write grey scale!\n");
+ G_warning("don't know how to write grey scale!");
}
G_free_colors(&colors);
@@ -338,7 +376,6 @@
G_close_cell(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
@@ -351,9 +388,10 @@
fclose(fp);
- return (0);
+ exit(EXIT_SUCCESS);
}
+
#ifdef __STDC__
static void pnmtopng_error_handler(png_structp png_ptr, png_const_charp msg)
#else
@@ -373,7 +411,7 @@
* regardless of whether _BSD_SOURCE or anything else has (or has not)
* been defined. */
- G_warning("pnmtopng: fatal libpng error: %s", msg);
+ G_warning("pnmtopng: fatal libpng error: [%s]", msg);
jmpbuf_ptr = png_get_error_ptr(png_ptr);
if (jmpbuf_ptr == NULL) { /* we are completely hosed now */
More information about the grass-commit
mailing list