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

  1. /* pgmramp.c - generate a grayscale ramp
  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 "pgm.h"
  14.  
  15. int
  16. main( argc, argv )
  17. int argc;
  18. char *argv[];
  19.     {
  20.     gray *grayrow;
  21.     register gray *gP;
  22.     int rows, cols, rowso2, colso2, row;
  23.     register int col;
  24.     int ramptype;
  25. #define RT_LR 1
  26. #define RT_TB 2
  27. #define RT_RECT 3
  28. #define RT_ELLIP 4
  29.     char *usage = "-lr|-tb|-rectangle|-ellipse <width> <height>";
  30.  
  31.  
  32.     pgm_init( &argc, argv );
  33.  
  34.     if ( argc != 4 )
  35.     pm_usage( usage );
  36.  
  37.     if ( pm_keymatch( argv[1], "-lr", 2 ) )
  38.     ramptype = RT_LR;
  39.     else if ( pm_keymatch( argv[1], "-tb", 2 ) )
  40.     ramptype = RT_TB;
  41.     else if ( pm_keymatch( argv[1], "-rectangle", 2 ) )
  42.     ramptype = RT_RECT;
  43.     else if ( pm_keymatch( argv[1], "-ellipse", 2 ) )
  44.     ramptype = RT_ELLIP;
  45.     else
  46.     pm_usage( usage );
  47.     
  48.     if ( sscanf( argv[2], "%d", &cols ) != 1 )
  49.     pm_usage( usage );
  50.     if ( sscanf( argv[3], "%d", &rows ) != 1 )
  51.     pm_usage( usage );
  52.  
  53.     colso2 = cols / 2;
  54.     rowso2 = rows / 2;
  55.  
  56.     pgm_writepgminit( stdout, cols, rows, PGM_MAXMAXVAL, 0 );
  57.     grayrow = pgm_allocrow( cols );
  58.  
  59.     for ( row = 0; row < rows; ++row )
  60.     {
  61.     for ( col = 0, gP = grayrow; col < cols; ++col, ++gP )
  62.         {
  63.         switch ( ramptype )
  64.         {
  65.         case RT_LR:
  66.         *gP = col * PGM_MAXMAXVAL / ((cols == 1) ? 1 : (cols - 1));
  67.         break;
  68.  
  69.         case RT_TB:
  70.         *gP = row * PGM_MAXMAXVAL / ((rows == 1) ? 1 : (rows - 1));
  71.         break;
  72.  
  73.         case RT_RECT:
  74.         {
  75.         float r, c;
  76.         r = abs( rowso2 - row ) / (float) rowso2;
  77.         c = abs( colso2 - col ) / (float) colso2;
  78.         *gP = PGM_MAXMAXVAL - ( r + c ) / 2.0 * PGM_MAXMAXVAL;
  79.         }
  80.         break;
  81.  
  82.         case RT_ELLIP:
  83.         {
  84.         float r, c, v;
  85.         r = abs( rowso2 - row ) / (float) rowso2;
  86.         c = abs( colso2 - col ) / (float) colso2;
  87.         v = r * r + c * c;
  88.         if ( v < 0.0 ) v = 0.0;
  89.         else if ( v > 1.0 ) v = 1.0;
  90.         *gP = PGM_MAXMAXVAL - v * PGM_MAXMAXVAL;
  91.         }
  92.         break;
  93.  
  94.         default:
  95.         pm_error( "can't happen" );
  96.         }
  97.         }
  98.     pgm_writepgmrow( stdout, grayrow, cols, PGM_MAXMAXVAL, 0 );
  99.     }
  100.  
  101.     pm_close( stdout );
  102.     exit( 0 );
  103.     }
  104.