home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / netpbma.zip / pgm / pgmkernel.c < prev    next >
C/C++ Source or Header  |  1994-02-24  |  3KB  |  91 lines

  1. /* pgmkernel.c - generate a portable graymap convolution kernel
  2. **
  3. ** Creates a Portable Graymap file containing a convolution filter
  4. ** with max value = 255 and minimum value > 127 that can be used as a 
  5. ** smoothing kernel for pnmconvol.
  6. **
  7. ** Copyright (C) 1992 by Alberto Accomazzi, Smithsonian Astrophysical
  8. ** Observatory.
  9. **
  10. ** Permission to use, copy, modify, and distribute this software and its
  11. ** documentation for any purpose and without fee is hereby granted, provided
  12. ** that the above copyright notice appear in all copies and that both that
  13. ** copyright notice and this permission notice appear in supporting
  14. ** documentation.  This software is provided "as is" without express or
  15. ** implied warranty.
  16. */
  17.  
  18. #include <math.h>
  19. #include "pgm.h"
  20.  
  21. int
  22. main ( argc, argv )
  23.     int argc;
  24.     char *argv[];
  25. {
  26.     register     int i, j;
  27.     int     argn = 1, verbose = 0, ixsize, iysize, maxval = 255;
  28.     double     fxsize = 0.0, fysize = 0.0, w = 6.0, kxcenter, kycenter, 
  29.                 tmax = 0, *fkernel;
  30.     char     *usage = "[-weight f] width [height]";
  31.  
  32.     pgm_init( &argc, argv );
  33.  
  34.     while ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' )
  35.         {
  36.     if ( pm_keymatch( argv[argn], "-weight", 2 )) {
  37.         if (++argn >= argc)
  38.         pm_usage( usage );
  39.         else if (sscanf(argv[argn], "%lf", &w) != 1)
  40.         pm_usage( usage );
  41.         }
  42.     else
  43.         pm_usage( usage );
  44.     argn++;
  45.     }
  46.  
  47.     if (argn == argc)
  48.     pm_usage( usage );
  49.     
  50.     if (sscanf(argv[argn], "%lf", &fxsize) != 1) 
  51.     pm_error( "error reading input kernel x size, (%s)\n", argv[argn]);
  52.  
  53.     ++argn;
  54.     if (argn == argc - 1) {
  55.     if (sscanf(argv[argn], "%lf", &fysize) != 1)
  56.         pm_error( "error reading input kernel y size, (%s)\n", argv[argn]);
  57.     }
  58.     else if (argn == argc)
  59.     fysize = fxsize;
  60.     else
  61.     pm_usage( usage );
  62.  
  63.     if (fxsize <= 1 || fysize <= 1)
  64.     pm_usage( usage );
  65.  
  66.     kxcenter = (fxsize - 1) / 2.0;
  67.     kycenter = (fysize - 1) / 2.0;
  68.     ixsize = fxsize + 0.999;
  69.     iysize = fysize + 0.999;
  70.     fkernel = (double *) malloc ((unsigned)(ixsize*iysize*sizeof(double)));
  71.     for (i = 0; i < iysize; i++) 
  72.     for (j = 0; j < ixsize; j++) {
  73.         fkernel[i*ixsize+j] = 1.0 / (1.0 + w * sqrt((double)
  74.                             (i-kycenter)*(i-kycenter)+
  75.                             (j-kxcenter)*(j-kxcenter)));
  76.         if (tmax < fkernel[i*ixsize+j])
  77.         tmax = fkernel[i*ixsize+j];
  78.         }
  79.  
  80.     /* output PGM header + data (ASCII format only) */
  81.     printf("P2\n%d %d\n%d\n", ixsize, iysize, maxval);
  82.     
  83.     for (i = 0; i < iysize; i++, printf("\n"))
  84.     for (j = 0; j < ixsize; j++)
  85.         printf(" %3d", (int)(maxval * (fkernel[i*ixsize+j] / 
  86.                        (2*tmax) + 0.5)));
  87.     
  88.     exit(0);
  89.     }
  90.  
  91.