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

  1. /* pbmtopgm.c - convert bitmap to greymap by totalling pixels over sample area
  2.  * AJCD 12/12/90
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include "pgm.h"
  7.  
  8. int
  9. main(argc, argv)
  10.      int argc;
  11.      char *argv[];
  12. {
  13.    register gray *outrow, maxval;
  14.    register int right, left, down, up;
  15.    register bit **inbits;
  16.    int rows, cols;
  17.    FILE *ifd;
  18.    int col, row, width, height;
  19.    char *usage = "<w> <h> [pbmfile]";
  20.    
  21.  
  22.    pgm_init( &argc, argv );
  23.  
  24.    if (argc > 4 || argc < 3)
  25.       pm_usage(usage);
  26.  
  27.    width = atoi(argv[1]);
  28.    height = atoi(argv[2]);
  29.    if (width < 1 || height < 1)
  30.       pm_error("width and height must be > 0");
  31.    left=width/2; right=width-left;
  32.    up=width/2; down=height-up;
  33.  
  34.    if (argc == 4)
  35.       ifd = pm_openr(argv[3]);
  36.    else
  37.       ifd = stdin ;
  38.  
  39.    inbits = pbm_readpbm(ifd, &cols, &rows) ;
  40.  
  41.    if (width > cols || height > rows)
  42.       pm_error("sample size greater than bitmap size");
  43.  
  44.    outrow = pgm_allocrow(cols) ;
  45.    maxval = width*height;
  46.    pgm_writepgminit(stdout, cols, rows, maxval, 0) ;
  47.  
  48.    for (row = 0; row < rows; row++) {
  49.       int t = (row > up) ? (row-up) : 0;
  50.       int b = (row+down < rows) ? (row+down) : rows;
  51.       int onv = height - (t-row+up) - (row+down-b);
  52.       for (col = 0; col < cols; col++) {
  53.      int l = (col > left) ? (col-left) : 0;
  54.      int r = (col+right < cols) ? (col+right) : cols;
  55.      int onh = width - (l-col+left) - (col+right-r);
  56.      int value = 0, x, y;
  57.      for (x = l; x < r; x++)
  58.         for (y = t; y < b; y++)
  59.            if (inbits[y][x] == PBM_WHITE) value++;
  60.      outrow[col] = maxval*value/(onh*onv);
  61.       }
  62.       pgm_writepgmrow(stdout, outrow, cols, maxval, 0) ;
  63.    }
  64.    pm_close(ifd);
  65.    exit(0);
  66. }
  67.