[Swift-commit] r6308 - in SwiftApps/modis: bin tutorial

davidk at ci.uchicago.edu davidk at ci.uchicago.edu
Sat Feb 23 06:45:14 CST 2013


Author: davidk
Date: 2013-02-23 06:44:52 -0600 (Sat, 23 Feb 2013)
New Revision: 6308

Added:
   SwiftApps/modis/bin/rgb_to_png.py
Removed:
   SwiftApps/modis/bin/is_land.pl
Modified:
   SwiftApps/modis/bin/draw_rectangle.pl
   SwiftApps/modis/bin/markmap.sh
   SwiftApps/modis/tutorial/modis04.swift
Log:
Fix draw_rectangle.pl to work with rgb images
Added rgb_to_png.py and integrate into modis04


Modified: SwiftApps/modis/bin/draw_rectangle.pl
===================================================================
--- SwiftApps/modis/bin/draw_rectangle.pl	2013-02-22 22:19:48 UTC (rev 6307)
+++ SwiftApps/modis/bin/draw_rectangle.pl	2013-02-23 12:44:52 UTC (rev 6308)
@@ -1,26 +1,18 @@
 #!/usr/bin/perl 
 
-# Draw a rectangle on a PPM 
-# Usage: draw_rectangle.pl infile.ppm xmin ymin xmax ymax outfile.ppm
+# Draw a rectangle on an RGB file
+# Usage: draw_rectangle.pl infile.rgb xmin ymin xmax ymax outfile.rgb
 
-my ($pgm_input_filename, $xmin, $ymin, $xmax, $ymax, $pgm_output_filename) = @ARGV;
-open(PGMFILE_INPUT, "$pgm_input_filename") || die "Unable to open $pgm_input_filename!";
-open(PGMFILE_OUTPUT, ">$pgm_output_filename") || die "Unable to create $pgm_output_filename";
+my ($input_filename, $xres, $yres, $xmin, $ymin, $xmax, $ymax, $output_filename) = @ARGV;
+open(FILE_INPUT, "$input_filename") || die "Unable to open $input_filename!";
+open(FILE_OUTPUT, ">$output_filename") || die "Unable to create $output_filename";
 
-# Read header
-my $magic = <PGMFILE_INPUT>; 
-my $resolution = <PGMFILE_INPUT>;
-my $maxvalue = <PGMFILE_INPUT>;
-(my $xres, my $yres) = split(' ', $resolution);
-
-# Write new header
-print PGMFILE_OUTPUT "$magic" . "$resolution" . "$maxvalue";
-
 # Read data three bytes at a time (RGB)
 $/ = \3;
 my $x=0, $y=0;
-while(<PGMFILE_INPUT>) {
+while(<FILE_INPUT>) {
    (my $red, my $green, my $blue) = unpack('C3', $_);
+   #print "$red $green $blue\n";
 
    # Left and right of rectangle, 2 pixels wide
    if ( $x == $xmin || $x == $xmin+1 || $x == $xmax || $x == $xmax-1 ) {
@@ -35,11 +27,11 @@
       }
    }
    
-   print PGMFILE_OUTPUT pack('C3', $red, $green, $blue);
+   print FILE_OUTPUT pack('C3', $red, $green, $blue);
 
    if($x == $xres-1) { $x = 0; $y++; } 
    else { $x++; }
 }
 
-close(PGMFILE_INPUT);
-close(PGMFILE_OUTPUT);
+close(FILE_INPUT);
+close(FILE_OUTPUT);

Deleted: SwiftApps/modis/bin/is_land.pl
===================================================================
--- SwiftApps/modis/bin/is_land.pl	2013-02-22 22:19:48 UTC (rev 6307)
+++ SwiftApps/modis/bin/is_land.pl	2013-02-23 12:44:52 UTC (rev 6308)
@@ -1,34 +0,0 @@
-#!/usr/bin/perl -w
-
-# Usage: is_land.pl myfile.pgm.gz
-# Returns 0 if image is land, 1 if water
-
-my $pgm_filename = shift;
-open(PGMFILE, "gunzip -c $pgm_filename |") || die "Unable to open $pgm_filename!\n";
-
-# Header
-my $magic = <PGMFILE>;
-my $resolution = <PGMFILE>;
-my $maxvalue = <PGMFILE>;
-my ($xres, $yres) = split(' ', $resolution);
-
-# Use arrays to count frequencies
-my @pixelcount;
-foreach my $count(0..$maxvalue) {
-  $pixelcount[$count] = 0;
-}
-
-# Read values
-foreach(<PGMFILE>) {
-   foreach $word(split) {
-      $pixelcount[$word]++;
-   }
-}
-close(PGMFILE);
-
-my $total_pixel_count=$xres*$yres;
-my $water_count=$pixelcount[0];
-
-if($water_count >= ($total_pixel_count/2)) { print "0\n"; } 
-else { print "1\n"; }
-

Modified: SwiftApps/modis/bin/markmap.sh
===================================================================
--- SwiftApps/modis/bin/markmap.sh	2013-02-22 22:19:48 UTC (rev 6307)
+++ SwiftApps/modis/bin/markmap.sh	2013-02-23 12:44:52 UTC (rev 6308)
@@ -5,15 +5,19 @@
 
 tilefile=$1
 outmap=$2
-asciimap=$3
+xres=721
+yres=361
 
-cp $BINDIR/grid.ppm $outmap
+cp $BINDIR/world.rgb $outmap.step
 
 cat $tilefile | while read f ; do
   hv=$(echo $f | sed -e 's,^.*/,,' -e 's/\..*//')
   h=$(echo $hv | sed -e 's/h//' -e 's/v..//' -e 's/^0//')
   v=$(echo $hv | sed -e 's/h..//' -e 's/v//' -e 's/^0//')
-  $BINDIR/draw_rectangle.pl $outmap $((34+$h*16)) $((51+$v*16)) $((34+14+$h*16)) $((51+14+$v*16)) $outmap.tmp
-  mv $outmap.tmp $outmap
+  $BINDIR/draw_rectangle.pl $outmap.step $xres $yres $(( $h * 20 )) $(( $v * 20 )) $(( $h * 20 + 20 )) $(( $v * 20 + 20)) $outmap.tmp
+  mv $outmap.tmp $outmap.step
 done
 
+# Convert output to PNG
+$BINDIR/rgb_to_png.py $outmap.step $xres $yres $outmap
+rm $outmap.step

Added: SwiftApps/modis/bin/rgb_to_png.py
===================================================================
--- SwiftApps/modis/bin/rgb_to_png.py	                        (rev 0)
+++ SwiftApps/modis/bin/rgb_to_png.py	2013-02-23 12:44:52 UTC (rev 6308)
@@ -0,0 +1,54 @@
+#!/usr/bin/env python
+import struct, sys
+
+def be32(n):
+    return struct.pack('>I', n)
+
+def png_chunk(ty, data):
+    return be32(len(data)) + ty + data + be32(crc(ty + data))
+
+def png_header(width, height):
+    return png_chunk('IHDR',
+        struct.pack('>IIBBBBB', width, height, 8, 2, 0, 0, 0))
+
+MAX_DEFLATE = 0xffff
+def deflate_block(data, last=False):
+    n = len(data)
+    assert n <= MAX_DEFLATE
+    return struct.pack('<BHH', bool(last), n, 0xffff ^ n) + data
+
+def pieces(seq, n):
+    return [seq[i:i+n] for i in xrange(0, len(seq), n)]
+
+def zlib_stream(data):
+    segments = pieces(data, MAX_DEFLATE)
+    blocks = ''.join(deflate_block(p) for p in segments[:-1])
+    blocks += deflate_block(segments[-1], last=True)
+    return '\x78\x01' + blocks + be32(adler32(data))
+
+def to_png(width, height, data):
+    lines = ''.join('\0'+p for p in pieces(data, 3*width))
+    return ('\x89PNG\r\n\x1a\n'
+        + png_header(width, height)
+        + png_chunk('IDAT', zlib_stream(lines))
+        + png_chunk('IEND', ''))
+
+def crc(data):
+    c = 0xffffffff
+    for x in data:
+        c ^= ord(x)
+        for k in xrange(8):
+            v = 0xedb88320 if c & 1 else 0
+            c = v ^ (c >> 1)
+    return c ^ 0xffffffff
+
+def adler32(data):
+    s1, s2 = 1, 0
+    for x in data:
+        s1 = (s1 + ord(x)) % 65521
+        s2 = (s2 + s1) % 65521
+    return (s2 << 16) + s1
+
+img = open(sys.argv[1], 'r').read()
+w, h = int(sys.argv[2]), int(sys.argv[3])
+open(sys.argv[4], 'wb').write(to_png(w, h, img))


Property changes on: SwiftApps/modis/bin/rgb_to_png.py
___________________________________________________________________
Added: svn:executable
   + *

Modified: SwiftApps/modis/tutorial/modis04.swift
===================================================================
--- SwiftApps/modis/tutorial/modis04.swift	2013-02-22 22:19:48 UTC (rev 6307)
+++ SwiftApps/modis/tutorial/modis04.swift	2013-02-23 12:44:52 UTC (rev 6308)
@@ -39,6 +39,5 @@
 (topSelected, selectedTiles) = analyzeLandUse(land, landType, nSelect);
 
 # Mark the top N tiles on a sinusoidal gridded map
-imagefile gridMap <"markedGrid.ppm">;
-file asciiMap <"map.txt">;
-gridMap = markMap(topSelected);
+imagefile gridmap <"gridmap.png">;
+gridmap = markMap(topSelected);




More information about the Swift-commit mailing list