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

  1. /* pgmedge.c - edge-detect a portable graymap
  2. **
  3. ** Copyright (C) 1989 by Jef Poskanzer.
  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 <math.h>
  14. #include "pgm.h"
  15.  
  16. int
  17. main( argc, argv )
  18. int argc;
  19. char *argv[];
  20.     {
  21.     FILE *ifp;
  22.     gray *row0, *row1, *row2, *tmprow, *orow;
  23.     int argn, rows, cols, format, row;
  24.     register int col;
  25.     gray maxval;
  26.     double sum1, sum2, sum;
  27.     char *usage = "[pgmfile]";
  28.  
  29.  
  30.     pgm_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.     pgm_pbmmaxval = 255;    /* use larger value for better results */
  46.  
  47.     pgm_readpgminit( ifp, &cols, &rows, &maxval, &format );
  48.     if ( cols < 3 || rows < 3 )
  49.     pm_error( "the image is too small" );
  50.  
  51.     row0 = pgm_allocrow( cols );
  52.     row1 = pgm_allocrow( cols );
  53.     row2 = pgm_allocrow( cols );
  54.     orow = pgm_allocrow( cols );
  55.  
  56.     pgm_writepgminit( stdout, cols, rows, maxval, 0 );
  57.  
  58.     /* Read in the first two rows. */
  59.     pgm_readpgmrow( ifp, row0, cols, maxval, format );
  60.     pgm_readpgmrow( ifp, row1, cols, maxval, format );
  61.  
  62.     /* Write out the first row, all zeros. */
  63.     for ( col = 0; col < cols; ++col )
  64.     orow[col] = 0;
  65.     pgm_writepgmrow( stdout, orow, cols, maxval, 0 );
  66.  
  67.     /* Now the rest of the image -- read in the next row, and write
  68.     ** write out the current row.
  69.     */
  70.     for ( row = 1; row < rows - 1; row++ )
  71.     {
  72.     pgm_readpgmrow( ifp, row2, cols, maxval, format );
  73.  
  74.     for ( col = 1; col < cols - 1; col++ )
  75.         {
  76.         sum1 = (double) row0[col+1] - (double) row0[col-1] +
  77.            2.0 * ( (double) row1[col+1] - (double) row1[col-1] ) +
  78.            (double) row2[col+1] - (double) row2[col-1];
  79.         sum2 = ( (double) row2[col-1] + 2.0 * (double) row2[col] +
  80.              (double) row2[col+1] ) -
  81.            ( (double) row0[col-1] + 2.0 * (double) row0[col] +
  82.              (double) row0[col+1] );
  83.         sum = sqrt( sum1 * sum1 + sum2 * sum2 );
  84.         sum /= 1.8;        /* arbitrary scaling factor */
  85.         if ( sum > maxval ) sum = maxval;
  86.         orow[col] = sum;
  87.         }
  88.  
  89.     /* Write out a row. */
  90.     pgm_writepgmrow( stdout, orow, cols, maxval, 0 );
  91.  
  92.     /* Rotate rows. */
  93.     tmprow = row0;
  94.     row0 = row1;
  95.     row1 = row2;
  96.     row2 = tmprow;
  97.     }
  98.     pm_close( ifp );
  99.  
  100.     /* And write the last row, zeros again. */
  101.     for ( col = 0; col < cols; ++col )
  102.     orow[col] = 0;
  103.     pgm_writepgmrow( stdout, orow, cols, maxval, 0 );
  104.  
  105.     pm_close( stdout );
  106.  
  107.     exit( 0 );
  108.     }
  109.