[GRASS-SVN] r39530 - in grass/trunk: general general/g.ppmtopng raster/r.colors

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Oct 16 12:31:42 EDT 2009


Author: glynn
Date: 2009-10-16 12:31:39 -0400 (Fri, 16 Oct 2009)
New Revision: 39530

Added:
   grass/trunk/general/g.ppmtopng/
   grass/trunk/general/g.ppmtopng/Makefile
   grass/trunk/general/g.ppmtopng/g.ppmtopng.html
   grass/trunk/general/g.ppmtopng/main.c
Modified:
   grass/trunk/general/Makefile
   grass/trunk/raster/r.colors/Makefile
   grass/trunk/raster/r.colors/thumbnails.py
Log:
Add, use g.ppmtopng utility
Error creating r.colors' thumbnails shouldn't be fatal


Modified: grass/trunk/general/Makefile
===================================================================
--- grass/trunk/general/Makefile	2009-10-16 15:19:24 UTC (rev 39529)
+++ grass/trunk/general/Makefile	2009-10-16 16:31:39 UTC (rev 39530)
@@ -20,6 +20,7 @@
 	g.mremove \
 	g.parser \
 	g.pnmcomp \
+	g.ppmtopng \
 	g.proj \
 	g.region \
 	g.remove \

Added: grass/trunk/general/g.ppmtopng/Makefile
===================================================================
--- grass/trunk/general/g.ppmtopng/Makefile	                        (rev 0)
+++ grass/trunk/general/g.ppmtopng/Makefile	2009-10-16 16:31:39 UTC (rev 39530)
@@ -0,0 +1,15 @@
+
+MODULE_TOPDIR = ../..
+
+PGM = g.ppmtopng
+LIBES = $(GISLIB) $(PNGLIB)
+DEPENDENCIES = $(GISDEP)
+
+EXTRA_CFLAGS=$(ZLIBINCPATH) $(PNGINC)
+
+include $(MODULE_TOPDIR)/include/Make/Module.make
+
+ifneq ($(USE_PNG),)
+default: cmd
+endif
+

Added: grass/trunk/general/g.ppmtopng/g.ppmtopng.html
===================================================================
--- grass/trunk/general/g.ppmtopng/g.ppmtopng.html	                        (rev 0)
+++ grass/trunk/general/g.ppmtopng/g.ppmtopng.html	2009-10-16 16:31:39 UTC (rev 39530)
@@ -0,0 +1,8 @@
+<H2>DESCRIPTION</H2>
+
+<p>A utility to convert between PPM/PGM and PNG image formats.</p>
+
+<H2>AUTHOR</H2>
+Glynn Clements
+
+<p><i>Last changed: $Date: 2008-08-15 07:16:42 +0100 (Fri, 15 Aug 2008) $</i>

Added: grass/trunk/general/g.ppmtopng/main.c
===================================================================
--- grass/trunk/general/g.ppmtopng/main.c	                        (rev 0)
+++ grass/trunk/general/g.ppmtopng/main.c	2009-10-16 16:31:39 UTC (rev 39530)
@@ -0,0 +1,142 @@
+/*
+ * MODULE:       g.ppmtopng
+ * AUTHOR(S):    Glynn Clements
+ * PURPOSE:      g.ppmtopng isn't meant for end users. It's an internal tool for use by
+ *               the script to generate thumbnails for the r.colors manual page.
+ * COPYRIGHT:    (C) 2009 by Glynn Clements and the GRASS Development Team
+ *
+ *               This program is free software under the GNU General Public
+ *               License (>=v2). Read the file COPYING that comes with GRASS
+ *               for details.
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <png.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+
+static int width, height;
+static unsigned char *buf;
+
+static void read_ppm(const char *filename)
+{
+    FILE *input;
+    int x, y;
+    int maxval;
+    unsigned char *p;
+
+    input = fopen(filename, "rb");
+    if (!input)
+	G_fatal_error(_("unable to open input file %s"), filename);
+
+    if (fscanf(input, "P6 %d %d %d", &width, &height, &maxval) != 3)
+	G_fatal_error(_("invalid input file %s"), filename);
+
+    fgetc(input);
+
+    buf = G_malloc(width * height * 3);
+
+    p = buf;
+    for (y = 0; y < height; y++) {
+	for (x = 0; x < width; x++) {
+	    int r = fgetc(input);
+	    int g = fgetc(input);
+	    int b = fgetc(input);
+
+	    *p++ = (unsigned char) (r * 255 / maxval);
+	    *p++ = (unsigned char) (g * 255 / maxval);
+	    *p++ = (unsigned char) (b * 255 / maxval);
+	}
+    }
+
+    fclose(input);
+}
+
+static void write_png(const char *filename)
+{
+    static jmp_buf jbuf;
+    static png_struct *png_ptr;
+    static png_info *info_ptr;
+    FILE *output;
+    int y;
+    unsigned char *p;
+
+    png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, &jbuf, NULL, NULL);
+    if (!png_ptr)
+	G_fatal_error(_("unable to allocate PNG structure"));
+
+    info_ptr = png_create_info_struct(png_ptr);
+    if (!info_ptr)
+	G_fatal_error(_("unable to allocate PNG structure"));
+
+    if (setjmp(png_jmpbuf(png_ptr)))
+	G_fatal_error(_("error writing PNG file"));
+
+    output = fopen(filename, "wb");
+    if (!output)
+	G_fatal_error(_("unable to open output file %s"), filename);
+
+    png_init_io(png_ptr, output);
+
+    png_set_IHDR(png_ptr, info_ptr,
+		 width, height,
+		 8, PNG_COLOR_TYPE_RGB,
+		 PNG_INTERLACE_NONE,
+		 PNG_COMPRESSION_TYPE_DEFAULT,
+		 PNG_FILTER_TYPE_DEFAULT);
+
+    png_set_invert_alpha(png_ptr);
+
+    png_write_info(png_ptr, info_ptr);
+
+    for (y = 0, p = buf; y < height; y++, p += 3 * width)
+	png_write_row(png_ptr, p);
+
+    png_write_end(png_ptr, info_ptr);
+
+    png_destroy_write_struct(&png_ptr, &info_ptr);
+
+    fclose(output);
+}
+
+int main(int argc, char *argv[])
+{
+    struct GModule *module;
+    struct
+    {
+	struct Option *in, *out;
+    } opt;
+
+    G_gisinit(argv[0]);
+
+    module = G_define_module();
+    G_add_keyword(_("general"));
+    module->description = "Converts between PPM/PGM and PNG image formats";
+
+    opt.in = G_define_option();
+    opt.in->key = "input";
+    opt.in->type = TYPE_STRING;
+    opt.in->required = YES;
+    opt.in->multiple = YES;
+    opt.in->description = _("Name of input file");
+    opt.in->gisprompt = "old_file,file,input";
+
+    opt.out = G_define_option();
+    opt.out->key = "output";
+    opt.out->type = TYPE_STRING;
+    opt.out->required = YES;
+    opt.out->description = _("Name of output file");
+    opt.out->gisprompt = "new_file,file,output";
+
+    if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+
+    read_ppm(opt.in->answer);
+    write_png(opt.out->answer);
+
+    return 0;
+}

Modified: grass/trunk/raster/r.colors/Makefile
===================================================================
--- grass/trunk/raster/r.colors/Makefile	2009-10-16 15:19:24 UTC (rev 39529)
+++ grass/trunk/raster/r.colors/Makefile	2009-10-16 16:31:39 UTC (rev 39530)
@@ -17,7 +17,7 @@
 	$(MAKE) thumbnails
 
 thumbnails: $(BIN)/r.mapcalc$(EXE)
-	$(call run_grass, ./thumbnails.py)
+	-$(call run_grass, ./thumbnails.py)
 
 $(BIN)/r.mapcalc$(EXE):
 	$(MAKE) -C ../r.mapcalc

Modified: grass/trunk/raster/r.colors/thumbnails.py
===================================================================
--- grass/trunk/raster/r.colors/thumbnails.py	2009-10-16 15:19:24 UTC (rev 39529)
+++ grass/trunk/raster/r.colors/thumbnails.py	2009-10-16 16:31:39 UTC (rev 39530)
@@ -63,21 +63,23 @@
     return dstd
 
 def ppmtopng(dst, src):
-    fh = open(dst, 'wb')
-    grass.call(["pnmtopng", tmp_img], stdout = fh)
-    fh.close()
-
-def convert_and_rotate(src, dst):
-    if grass.find_program("convert"):
-	grass.call(["convert", "-rotate", "90", src, dst])
+    if grass.find_program("g.ppmtopng"):
+	grass.run_command('g.ppmtopng', input = src, output = dst)
     elif grass.find_program("pnmtopng"):
-	srcd = read_ppm(src)
-	dstd = rotate_ppm(srcd)
-	write_ppm(tmp_img, dstd)
-	ppmtopng(dst, tmp_img)
+	fh = open(dst, 'wb')
+	grass.call(["pnmtopng", src], stdout = fh)
+	fh.close()
+    elif grass.find_program("convert"):
+	grass.call(["convert", src, dst])
     else:
-	grass.fatal(_("Cannot find either convert or pnmtopng"))
+	grass.fatal(_("Cannot find g.ppmtopng, pnmtopng or convert"))
 
+def convert_and_rotate(src, dst):
+    srcd = read_ppm(src)
+    dstd = rotate_ppm(srcd)
+    write_ppm(tmp_img, dstd)
+    ppmtopng(dst, tmp_img)
+
 def main():
     global tmp_img, tmp_grad_abs, tmp_grad_rel
 



More information about the grass-commit mailing list