Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
ShortBlitter.java
1 package ij.process;
2 import java.awt.*;
3 
5 public class ShortBlitter implements Blitter {
6 
7  private ShortProcessor ip;
8  private int width, height;
9  private short[] pixels;
10 
11  public void setTransparentColor(Color c) {
12  }
13 
16  this.ip = ip;
17  width = ip.getWidth();
18  height = ip.getHeight();
19  pixels = (short[])ip.getPixels();
20  }
21 
23  public void copyBits(ImageProcessor ip, int xloc, int yloc, int mode) {
24  Rectangle r1, r2;
25  int srcIndex, dstIndex;
26  int xSrcBase, ySrcBase;
27  short[] srcPixels;
28 
29  int srcWidth = ip.getWidth();
30  int srcHeight = ip.getHeight();
31  r1 = new Rectangle(srcWidth, srcHeight);
32  r1.setLocation(xloc, yloc);
33  r2 = new Rectangle(width, height);
34  if (!r1.intersects(r2))
35  return;
36  srcPixels = (short [])ip.getPixels();
37 //new ij.ImagePlus("srcPixels", new ShortProcessor(srcWidth, srcHeight, srcPixels, null)).show();
38  r1 = r1.intersection(r2);
39  xSrcBase = (xloc<0)?-xloc:0;
40  ySrcBase = (yloc<0)?-yloc:0;
41  int src, dst;
42  for (int y=r1.y; y<(r1.y+r1.height); y++) {
43  srcIndex = (y-yloc)*srcWidth + (r1.x-xloc);
44  dstIndex = y * width + r1.x;
45  switch (mode) {
46  case COPY: case COPY_INVERTED: case COPY_TRANSPARENT:
47  for (int i=r1.width; --i>=0;)
48  pixels[dstIndex++] = srcPixels[srcIndex++];
49  break;
50  case ADD:
51  for (int i=r1.width; --i>=0;) {
52  dst = (srcPixels[srcIndex++]&0xffff)+(pixels[dstIndex]&0xffff);
53  if (dst<0) dst = 0;
54  if (dst>65535) dst = 65535;
55  pixels[dstIndex++] = (short)dst;
56  }
57  break;
58  case AVERAGE:
59  for (int i=r1.width; --i>=0;) {
60  dst = ((srcPixels[srcIndex++]&0xffff)+(pixels[dstIndex]&0xffff))/2;
61  pixels[dstIndex++] = (short)dst;
62  }
63  break;
64  case DIFFERENCE:
65  for (int i=r1.width; --i>=0;) {
66  dst = (pixels[dstIndex]&0xffff)-(srcPixels[srcIndex++]&0xffff);
67  if (dst<0) dst = -dst;
68  if (dst>65535) dst = 65535;
69  pixels[dstIndex++] = (short)dst;
70  }
71  break;
72  case SUBTRACT:
73  for (int i=r1.width; --i>=0;) {
74  dst = (pixels[dstIndex]&0xffff)-(srcPixels[srcIndex++]&0xffff);
75  if (dst<0) dst = 0;
76  if (dst>65535) dst = 65535;
77  pixels[dstIndex++] = (short)dst;
78  }
79  break;
80  case MULTIPLY:
81  for (int i=r1.width; --i>=0;) {
82  dst = (srcPixels[srcIndex++]&0xffff)*(pixels[dstIndex]&0xffff);
83  if (dst<0) dst = 0;
84  if (dst>65535) dst = 65535;
85  pixels[dstIndex++] = (short)dst;
86  }
87  break;
88  case DIVIDE:
89  for (int i=r1.width; --i>=0;) {
90  src = srcPixels[srcIndex++]&0xffff;
91  if (src==0)
92  dst = 65535;
93  else
94  dst = pixels[dstIndex]/src;
95  pixels[dstIndex++] = (short)dst;
96  }
97  break;
98  case AND:
99  for (int i=r1.width; --i>=0;) {
100  dst = srcPixels[srcIndex++]&pixels[dstIndex]&0xffff;
101  pixels[dstIndex++] = (short)dst;
102  }
103  break;
104  case OR:
105  for (int i=r1.width; --i>=0;) {
106  dst = srcPixels[srcIndex++]|pixels[dstIndex];
107  pixels[dstIndex++] = (short)dst;
108  }
109  break;
110  case XOR:
111  for (int i=r1.width; --i>=0;) {
112  dst = srcPixels[srcIndex++]^pixels[dstIndex];
113  pixels[dstIndex++] = (short)dst;
114  }
115  break;
116  case MIN:
117  for (int i=r1.width; --i>=0;) {
118  src = srcPixels[srcIndex++]&0xffff;
119  dst = pixels[dstIndex]&0xffff;
120  if (src<dst) dst = src;
121  pixels[dstIndex++] = (short)dst;
122  }
123  break;
124  case MAX:
125  for (int i=r1.width; --i>=0;) {
126  src = srcPixels[srcIndex++]&0xffff;
127  dst = pixels[dstIndex]&0xffff;
128  if (src>dst) dst = src;
129  pixels[dstIndex++] = (short)dst;
130  }
131  break;
132  }
133  if (y%20==0)
134  ip.showProgress((double)(y-r1.y)/r1.height);
135  }
136  ip.hideProgress();
137  }
138 }