home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / netpbma.zip / ppm / ppmrelief.c < prev    next >
C/C++ Source or Header  |  1993-10-04  |  3KB  |  97 lines

  1. /* ppmrelief.c - generate a relief map of a portable pixmap
  2. **
  3. ** Copyright (C) 1990 by Wilson H. Bent, Jr.
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include <stdio.h>
  14. #include "ppm.h"
  15.  
  16. int
  17. main( argc, argv )
  18.     int argc;
  19.     char* argv[];
  20.     {
  21.     FILE* ifp;
  22.     pixel** inputbuf;
  23.     pixel* outputrow;
  24.     int argn, rows, cols, format, row, rowa, rowb;
  25.     register int col;
  26.     pixval maxval, mv2, r, g, b;
  27.     char* usage = "[ppmfile]";
  28.  
  29.  
  30.     ppm_init( &argc, argv );
  31.  
  32.     argn = 1;
  33.  
  34.     if ( argn != argc )
  35.     {
  36.     ifp = pm_openr( argv[argn] );
  37.     ++argn;
  38.     }
  39.     else
  40.     ifp = stdin;
  41.  
  42.     if ( argn != argc )
  43.     pm_usage( usage );
  44.  
  45.     ppm_pbmmaxval = PPM_MAXMAXVAL;    /* use larger value for better results */
  46.  
  47.     ppm_readppminit( ifp, &cols, &rows, &maxval, &format );
  48.     mv2 = maxval / 2;
  49.  
  50.     /* Allocate space for 3 input rows, plus an output row. */
  51.     inputbuf = ppm_allocarray( cols, 3 );
  52.     outputrow = ppm_allocrow( cols );
  53.  
  54.     ppm_writeppminit( stdout, cols, rows, maxval, 0 );
  55.  
  56.     /* Read in the first two rows. */
  57.     ppm_readppmrow( ifp, inputbuf[0], cols, maxval, format );
  58.     ppm_readppmrow( ifp, inputbuf[1], cols, maxval, format );
  59.  
  60.     /* Write out the first row, all zeros. */
  61.     for ( col = 0; col < cols; ++col )
  62.         PPM_ASSIGN( outputrow[col], 0, 0, 0 );
  63.     ppm_writeppmrow( stdout, outputrow, cols, maxval, 0 );
  64.  
  65.     /* Now the rest of the image - read in the 3rd row of inputbuf,
  66.     ** and convolve with the first row into the output buffer.
  67.     */
  68.     for ( row = 2 ; row < rows; ++row )
  69.     {
  70.     rowa = row % 3;
  71.     rowb = (row + 2) % 3;
  72.     ppm_readppmrow( ifp, inputbuf[rowa], cols, maxval, format );
  73.  
  74.     for ( col = 0; col < cols - 2; ++col )
  75.         {
  76.         r = PPM_GETR( inputbuf[rowa][col] ) +
  77.         ( mv2 - PPM_GETR( inputbuf[rowb][col + 2] ) );
  78.         g = PPM_GETG( inputbuf[rowa][col] ) +
  79.         ( mv2 - PPM_GETG( inputbuf[rowb][col + 2] ) );
  80.         b = PPM_GETB( inputbuf[rowa][col] ) +
  81.         ( mv2 - PPM_GETB( inputbuf[rowb][col + 2] ) );
  82.         PPM_ASSIGN( outputrow[col + 1], r, g, b );
  83.         }
  84.     ppm_writeppmrow( stdout, outputrow, cols, maxval, 0 );
  85.     }
  86.  
  87.     /* And write the last row, zeros again. */
  88.     for ( col = 0; col < cols; ++col )
  89.         PPM_ASSIGN( outputrow[col], 0, 0, 0 );
  90.     ppm_writeppmrow( stdout, outputrow, cols, maxval, 0 );
  91.  
  92.     pm_close( ifp );
  93.     pm_close( stdout );
  94.  
  95.     exit( 0 );
  96.     }
  97.