home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / hips / libsrc / h_bthin.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-17  |  3.7 KB  |  150 lines

  1.  
  2.  
  3. /*   bthin.c                            Brian Tierney,  LBL  3/90
  4.  *
  5.  *  program to this binary object to a single pixel width line
  6.  *
  7.  *   Author:  Brian L. Tierney
  8.  *            Lawrence Berkeley Laboratory
  9.  *            Imaging and Distributed Computing Group
  10.  *            email: bltierney@lbl.gov
  11.  *
  12.  *  converted to HIPS2
  13.  *            Felix K. Huang
  14.  *            Lawrence Berkeley Laboratory
  15.  *            Imaging and Distributed Computing Group
  16.  *            email: fhuang@george.lbl.gov
  17. */
  18.  
  19. /*   This program is copyright (C) 1990, Regents  of  the
  20. University  of  California.   Anyone may reproduce this software,
  21. in whole or in part, provided that:
  22. (1)  Any copy  or  redistribution  must  show  the
  23.      Regents  of  the  University of California, through its
  24.      Lawrence Berkeley Laboratory, as the source,  and  must
  25.      include this notice;
  26. (2)  Any use of this software must reference this  distribu-
  27.      tion,  state that the software copyright is held by the
  28.      Regents of the University of California, and  that  the
  29.      software is used by their permission.
  30.  
  31.      It is acknowledged that the U.S. Government has  rights
  32. to this software under  Contract DE-AC03-765F00098 between the U.S.
  33. Department of Energy and the University of California.
  34.  
  35.      This software is provided as a professional  academic  contribu-
  36. tion  for  joint exchange.  Thus it is experimental, is pro-
  37. vided ``as is'', with no warranties of any kind  whatsoever,
  38. no  support,  promise  of updates, or printed documentation.
  39. Bug reports or fixes may be sent to the author, who may or may
  40. not act on them as he desires.
  41. */
  42.  
  43. #define Calloc(x,y) (y *)calloc((unsigned)(x), sizeof(y))
  44.  
  45. #include <stdio.h>
  46. #include <hipl_format.h>
  47.  
  48. extern int pval;    /* new pixel value for thinned output objects       */
  49.  
  50. extern int nrow;
  51. extern int ncol;
  52. extern int i_ocol;
  53.  
  54. byte    **roi1, **roi2;
  55.  
  56. h_bthin(hdi, hdo)
  57.     struct header *hdi, *hdo;
  58. {
  59.     i_to_r(hdi->firstpix);
  60.     thin_binary_image();
  61.     r_to_o(hdo->firstpix);
  62. }
  63.  
  64.  
  65. /********************************************************************/
  66. i_to_r(imagei)            /* input image to roi1     */
  67.     byte     *imagei;
  68. {
  69.     int       x, y;
  70.  
  71.     roi1 = Calloc(nrow, byte *);
  72.     for (y = 0; y < nrow; y++)
  73.     roi1[y] = Calloc(ncol, byte);
  74.  
  75.     roi2 = Calloc(nrow, byte *);
  76.     for (y = 0; y < nrow; y++)
  77.     roi2[y] = Calloc(ncol, byte);
  78.  
  79.     for (y = 0; y < nrow; y++, imagei = imagei + i_ocol)
  80.     for (x = 0; x < ncol; x++)
  81.         roi1[y][x] = imagei[x];
  82. }
  83.  
  84. r_to_o(imageo)            /* roi2 to output image     */
  85.     byte     *imageo;
  86. {
  87.     int       x, y;
  88.  
  89.     for (y = 0; y < nrow; y++, imageo = imageo + i_ocol)
  90.     for (x = 0; x < ncol; x++)
  91.         imageo[x] = roi2[y][x];
  92. }
  93.  
  94.  
  95. /********************************************************************/
  96. thin_binary_image()
  97. {
  98.     register int i, j, i1, j1, i2, j2, ni, nj, di, dj;
  99.  
  100.     for (i = 0; i < nrow; i++) {
  101.     for (j = 0; j < ncol; j++) {
  102.  
  103.         if (roi1[i][j] > 0) {    /* pixel is part of an object */
  104.         i1 = i2 = i;
  105.         j1 = j2 = j;
  106.  
  107.         /* find 4 points for edge of the circle  */
  108.  
  109.         while (i1 < nrow && roi1[i1][j] > 0)
  110.             i1++;
  111.  
  112.         while (i2 > 0 && roi1[i2][j] > 0)
  113.             i2--;
  114.  
  115.         while (j1 < ncol && roi1[i][j1] > 0)
  116.             j1++;
  117.  
  118.         while (j2 > 0 && roi1[i][j2] > 0)
  119.             j2--;
  120.  
  121.  
  122.         di = i1 - i2;
  123.         dj = j1 - j2;
  124.  
  125.         /* calc new center based on smaller diameter */
  126.         if (di <= 1 || dj <= 1) {
  127.             ni = i;
  128.             nj = j;
  129.             roi2[ni][nj] = pval;
  130.         } else if (di > dj) {
  131.             ni = i;
  132.             nj = (j1 + j2) / 2;
  133.             roi2[ni][nj] = pval;
  134.         } else if (di < dj) {
  135.             ni = (i1 + i2) / 2;
  136.             nj = j;
  137.             roi2[ni][nj] = pval;
  138.         } else {    /* d1 == dj, do both */
  139.             ni = i;
  140.             nj = (j1 + j2) / 2;
  141.             roi2[ni][nj] = pval;
  142.             ni = (i1 + i2) / 2;
  143.             nj = j;
  144.             roi2[ni][nj] = pval;
  145.         }
  146.         }            /* if */
  147.     }            /* for j */
  148.     }                /* for i */
  149. }
  150.