Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
BinaryProcessor.java
1 package ij.process;
2 //import ij.*;
3 import java.awt.*;
4 
6 public class BinaryProcessor extends ByteProcessor {
7 
8  private ByteProcessor parent;
9 
14  super(ip.getWidth(), ip.getHeight(), (byte[])ip.getPixels(), ip.getColorModel());
15  setRoi(ip.getRoi());
16  parent = ip;
17  }
18 
19  static final int OUTLINE=0;
20 
21  void process(int type, int count) {
22  int p1, p2, p3, p4, p5, p6, p7, p8, p9;
23  int inc = roiHeight/25;
24  if (inc<1) inc = 1;
25  int bgColor = 255;
26  if (parent.isInvertedLut())
27  bgColor = 0;
28 
29  byte[] pixels2 = (byte[])parent.getPixelsCopy();
30  int offset, v=0, sum;
31  int rowOffset = width;
32  for (int y=yMin; y<=yMax; y++) {
33  offset = xMin + y * width;
34  p2 = pixels2[offset-rowOffset-1]&0xff;
35  p3 = pixels2[offset-rowOffset]&0xff;
36  p5 = pixels2[offset-1]&0xff;
37  p6 = pixels2[offset]&0xff;
38  p8 = pixels2[offset+rowOffset-1]&0xff;
39  p9 = pixels2[offset+rowOffset]&0xff;
40 
41  for (int x=xMin; x<=xMax; x++) {
42  p1 = p2; p2 = p3;
43  p3 = pixels2[offset-rowOffset+1]&0xff;
44  p4 = p5; p5 = p6;
45  p6 = pixels2[offset+1]&0xff;
46  p7 = p8; p8 = p9;
47  p9 = pixels2[offset+rowOffset+1]&0xff;
48 
49  switch (type) {
50  case OUTLINE:
51  v = p5;
52  if (v!=bgColor) {
53  if (!(p1==bgColor || p2==bgColor || p3==bgColor || p4==bgColor
54  || p6==bgColor || p7==bgColor || p8==bgColor || p9==bgColor))
55  v = bgColor;
56  }
57  break;
58  }
59 
60  pixels[offset++] = (byte)v;
61  }
62  if (y%inc==0)
63  parent.showProgress((double)(y-roiY)/roiHeight);
64  }
65  parent.hideProgress();
66  }
67 
78  public void skeletonize() {
79  int[] table =
80  //0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1
81  {0,0,0,1,0,0,1,3,0,0,3,1,1,0,1,3,0,0,0,0,0,0,0,0,2,0,2,0,3,0,3,3,
82  0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,3,0,2,2,
83  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
84  2,0,0,0,0,0,0,0,2,0,0,0,2,0,0,0,3,0,0,0,0,0,0,0,3,0,0,0,3,0,2,0,
85  0,0,3,1,0,0,1,3,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
86  3,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
87  2,3,1,3,0,0,1,3,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
88  2,3,0,1,0,0,0,1,0,0,0,0,0,0,0,0,3,3,0,1,0,0,0,0,2,2,0,0,2,0,0,0};
89  int pass = 0;
90  int pixelsRemoved;
91  //ImageStack movie=null;
92  //boolean debug = IJ.altKeyDown();
93  //if (debug) movie = new ImageStack(width, height);
94  do {
95  setColor(Color.white);
96  if (roiX==0) {moveTo(0,0); lineTo(0,height-1);}
97  if (roiY==0) {moveTo(0,0); lineTo(width-1,0);}
98  if (roiWidth==width) {moveTo(width-1,0); lineTo(width-1,height-1);}
99  if (roiHeight==height) {moveTo(0,height-1); lineTo(width/*-1*/,height-1);}
100  snapshot();
101  //if (debug) movie.addSlice(""+pass, duplicate());
102  pixelsRemoved = thin(pass++, table);
103  snapshot();
104  //if (debug) movie.addSlice(""+pass, duplicate());
105  pixelsRemoved = thin(pass++, table);
106  //ij.IJ.write(pass+" "+pixelsRemoved);
107  } while (pixelsRemoved>0);
108  //if (debug) new ImagePlus("Skel Movie", movie).show();
109  }
110 
111  int thin(int pass, int[] table) {
112  int p1, p2, p3, p4, p5, p6, p7, p8, p9;
113  int inc = roiHeight/25;
114  if (inc<1) inc = 1;
115  int bgColor = 255;
116  if (parent.isInvertedLut())
117  bgColor = 0;
118 
119  byte[] pixels2 = (byte[])getPixelsCopy();
120  int v, index, code;
121  int offset, rowOffset = width;
122  int pixelsRemoved = 0;
123  int count = 100;
124  for (int y=yMin; y<=yMax; y++) {
125  offset = xMin + y * width;
126  for (int x=xMin; x<=xMax; x++) {
127  p5 = pixels2[offset]&0xff;
128  v = p5;
129  if (v!=bgColor) {
130  p1 = pixels2[offset-rowOffset-1]&0xff;
131  p2 = pixels2[offset-rowOffset]&0xff;
132  p3 = pixels2[offset-rowOffset+1]&0xff;
133  p4 = pixels2[offset-1]&0xff;
134  p6 = pixels2[offset+1]&0xff;
135  p7 = pixels2[offset+rowOffset-1]&0xff;
136  p8 = pixels2[offset+rowOffset]&0xff;
137  p9 = pixels2[offset+rowOffset+1]&0xff;
138  index = 0;
139  if (p1!=bgColor) index |= 1;
140  if (p2!=bgColor) index |= 2;
141  if (p3!=bgColor) index |= 4;
142  if (p6!=bgColor) index |= 8;
143  if (p9!=bgColor) index |= 16;
144  if (p8!=bgColor) index |= 32;
145  if (p7!=bgColor) index |= 64;
146  if (p4!=bgColor) index |= 128;
147  code = table[index];
148  if ((pass&1)==1) { //odd pass
149  if (code==2 || code==3) {
150  v = bgColor;
151  pixelsRemoved++;
152  }
153  } else { //even pass
154  if (code==1 || code==3) {
155  v = bgColor;
156  pixelsRemoved++;
157  }
158  }
159  }
160  pixels[offset++] = (byte)v;
161  }
162  if (y%inc==0)
163  showProgress((double)(y-roiY)/roiHeight);
164  }
165  hideProgress();
166  return pixelsRemoved;
167  }
168 
169  public void outline() {
170  process(OUTLINE, 0);
171  }
172 
173 }