home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / NETWORK / netpbm_src.lzh / NETPBM / PBM / pbmclean.c < prev    next >
C/C++ Source or Header  |  1996-11-18  |  2KB  |  98 lines

  1. /* pbmclean.c - pixel cleaning. Remove pixel if less than n connected
  2.  *              identical neighbours, n=1 default.
  3.  * AJCD 20/9/90
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include "pbm.h"
  8.  
  9. /* prototypes */
  10. void nextrow ARGS((FILE *ifd, int row));
  11.  
  12. #define PBM_INVERT(p) ((p) == PBM_WHITE ? PBM_BLACK : PBM_WHITE)
  13.  
  14. /* input bitmap size and storage */
  15. int rows, columns, format ;
  16. bit *inrow[3] ;
  17.  
  18. #define thisrow (1)
  19.  
  20. /* compass directions from west clockwise */
  21. int xd[] = { -1, -1,  0,  1, 1, 1, 0, -1 } ;
  22. int yd[] = {  0, -1, -1, -1, 0, 1, 1,  1 } ;
  23.  
  24. /* get a new row
  25.  */
  26.  
  27. void nextrow(ifd, row)
  28.      FILE *ifd;
  29.      int row;
  30. {
  31.    bit *shuffle = inrow[0] ;
  32.    inrow[0] = inrow[1];
  33.    inrow[1] = inrow[2];
  34.    inrow[2] = shuffle ;
  35.    if (row < rows) {
  36.       if (shuffle == NULL)
  37.          inrow[2] = shuffle = pbm_allocrow(columns);
  38.       pbm_readpbmrow(ifd, inrow[2], columns, format) ;
  39.    } else inrow[2] = NULL; /* discard storage */
  40.  
  41. }
  42.  
  43. int
  44. main(argc, argv)
  45.      int argc;
  46.      char *argv[];
  47. {
  48.    FILE *ifd;
  49.    register bit *outrow;
  50.    register int row, col, i;
  51.    int connect ;
  52.  
  53.  
  54.    pbm_init( &argc, argv );
  55.  
  56.    if (argc > 3)
  57.       pm_usage("[-connect] [pbmfile]");
  58.  
  59.    if (argv[1][0] == '-') {
  60.       connect = atoi(argv[1]+1);
  61.       argv++; argc--;
  62.    }
  63.    else connect = 1;
  64.  
  65.    if (argc == 2)
  66.       ifd = pm_openr(argv[1]);
  67.    else
  68.       ifd = stdin ;
  69.  
  70.    inrow[0] = inrow[1] = inrow[2] = NULL;
  71.    pbm_readpbminit(ifd, &columns, &rows, &format) ;
  72.  
  73.    outrow = pbm_allocrow(columns) ;
  74.  
  75.    pbm_writepbminit(stdout, columns, rows, 0) ;
  76.  
  77.    nextrow(ifd, 0);
  78.    for (row = 0; row < rows; row++) {
  79.       nextrow(ifd, row+1);
  80.       for (col = 0; col < columns; col++) {
  81.          int point = inrow[thisrow][col];
  82.          int joined = 0 ;
  83.          for (i = 0; i < 8; i++) {
  84.             int x = col + xd[i] ;
  85.             int y = thisrow + yd[i] ;
  86.             if (x < 0 || x >= columns) {
  87.                if (point == PBM_WHITE) joined++;
  88.             }
  89.             else if (inrow[y] && inrow[y][x] == point) joined++ ;
  90.          }
  91.          outrow[col] = (joined < connect) ? PBM_INVERT(point) : point;
  92.       }
  93.       pbm_writepbmrow(stdout, outrow, columns, 0) ;
  94.    }
  95.    pm_close(ifd);
  96.    exit(0);
  97. }
  98.