about summary refs log tree commit diff
path: root/editor/specialty/ppmrelief.c
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2016-03-27 01:38:28 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2016-03-27 01:38:28 +0000
commit367c9cb514c9da766488b9bdb218a18e31cb7624 (patch)
treef9e343be94161a4837f0f1c1d072a35538ae0f63 /editor/specialty/ppmrelief.c
parent6e88e3326cb0c7f7975b56189278cab3f84ba1bd (diff)
downloadnetpbm-mirror-367c9cb514c9da766488b9bdb218a18e31cb7624.tar.gz
netpbm-mirror-367c9cb514c9da766488b9bdb218a18e31cb7624.tar.xz
netpbm-mirror-367c9cb514c9da766488b9bdb218a18e31cb7624.zip
Promote Stable (10.47) to Super Stable
git-svn-id: http://svn.code.sf.net/p/netpbm/code/super_stable@2691 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'editor/specialty/ppmrelief.c')
-rw-r--r--editor/specialty/ppmrelief.c96
1 files changed, 96 insertions, 0 deletions
diff --git a/editor/specialty/ppmrelief.c b/editor/specialty/ppmrelief.c
new file mode 100644
index 00000000..1c408aec
--- /dev/null
+++ b/editor/specialty/ppmrelief.c
@@ -0,0 +1,96 @@
+/* ppmrelief.c - generate a relief map of a portable pixmap
+**
+** Copyright (C) 1990 by Wilson H. Bent, Jr.
+**
+** 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.
+*/
+
+#include <stdio.h>
+#include "pm_c_util.h"
+#include "ppm.h"
+
+int
+main(int argc, char * argv[]) {
+
+    FILE* ifp;
+    pixel** inputbuf;
+    pixel* outputrow;
+    int argn, rows, cols, format, row;
+    register int col;
+    pixval maxval, mv2;
+    const char* const usage = "[ppmfile]";
+
+    ppm_init( &argc, argv );
+
+    argn = 1;
+
+    if ( argn != argc ) {
+        ifp = pm_openr( argv[argn] );
+        ++argn;
+    } else
+        ifp = stdin;
+
+    if ( argn != argc )
+        pm_usage( usage );
+    
+    ppm_readppminit( ifp, &cols, &rows, &maxval, &format );
+
+    if (cols < 3 || rows < 3 )
+        pm_error("Input image too small: %u x %u.  Must be at least 3x3",
+                  cols, rows);
+
+    mv2 = maxval / 2;
+
+    /* Allocate space for 3 input rows, plus an output row. */
+    inputbuf = ppm_allocarray( cols, 3 );
+    outputrow = ppm_allocrow( cols );
+
+    ppm_writeppminit( stdout, cols, rows, maxval, 0 );
+
+    /* Read in the first two rows. */
+    ppm_readppmrow( ifp, inputbuf[0], cols, maxval, format );
+    ppm_readppmrow( ifp, inputbuf[1], cols, maxval, format );
+
+    /* Write out the first row, all zeros. */
+    for ( col = 0; col < cols; ++col )
+        PPM_ASSIGN( outputrow[col], 0, 0, 0 );
+    ppm_writeppmrow( stdout, outputrow, cols, maxval, 0 );
+
+    /* Now the rest of the image - read in the 3rd row of inputbuf,
+    ** and convolve with the first row into the output buffer.
+    */
+    for ( row = 2 ; row < rows; ++row ) {
+        pixval r, g, b;
+        int rowa, rowb;
+
+        rowa = row % 3;
+        rowb = (row + 2) % 3;
+        ppm_readppmrow( ifp, inputbuf[rowa], cols, maxval, format );
+        
+        for ( col = 0; col < cols - 2; ++col ) {
+            r = MAX(0, MIN(maxval, PPM_GETR( inputbuf[rowa][col] ) +
+                           ( mv2 - PPM_GETR( inputbuf[rowb][col + 2] ) )));
+            g = MAX(0, MIN(maxval, PPM_GETG( inputbuf[rowa][col] ) +
+                           ( mv2 - PPM_GETG( inputbuf[rowb][col + 2] ) )));
+            b = MAX(0, MIN(maxval, PPM_GETB( inputbuf[rowa][col] ) +
+                           ( mv2 - PPM_GETB( inputbuf[rowb][col + 2] ) )));
+            PPM_ASSIGN( outputrow[col + 1], r, g, b );
+        }
+        ppm_writeppmrow( stdout, outputrow, cols, maxval, 0 );
+    }
+
+    /* And write the last row, zeros again. */
+    for ( col = 0; col < cols; ++col )
+        PPM_ASSIGN( outputrow[col], 0, 0, 0 );
+    ppm_writeppmrow( stdout, outputrow, cols, maxval, 0 );
+
+    pm_close( ifp );
+    pm_close( stdout );
+
+    exit( 0 );
+}