Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
FloatBlitter.java
1 package ij.process;
2 import ij.Prefs;
3 import java.awt.*;
4 
6 public class FloatBlitter implements Blitter {
7 
8  public static float divideByZeroValue;
9 
10  private FloatProcessor ip;
11  private int width, height;
12  private float[] pixels;
13 
14  static {
15  divideByZeroValue = (float)Prefs.getDouble(Prefs.DIV_BY_ZERO_VALUE, Float.POSITIVE_INFINITY);
16  if (divideByZeroValue==Float.MAX_VALUE)
17  divideByZeroValue = Float.POSITIVE_INFINITY;
18  }
19 
22  this.ip = ip;
23  width = ip.getWidth();
24  height = ip.getHeight();
25  pixels = (float[])ip.getPixels();
26  }
27 
28  public void setTransparentColor(Color c) {
29  }
30 
32  public void copyBits(ImageProcessor ip, int xloc, int yloc, int mode) {
33  Rectangle r1, r2;
34  int srcIndex, dstIndex;
35  int xSrcBase, ySrcBase;
36  float[] srcPixels;
37 
38  if (!(ip instanceof FloatProcessor))
39  ip = ip.convertToFloat();
40  int srcWidth = ip.getWidth();
41  int srcHeight = ip.getHeight();
42  r1 = new Rectangle(srcWidth, srcHeight);
43  r1.setLocation(xloc, yloc);
44  r2 = new Rectangle(width, height);
45  if (!r1.intersects(r2))
46  return;
47  srcPixels = (float [])ip.getPixels();
48  r1 = r1.intersection(r2);
49  xSrcBase = (xloc<0)?-xloc:0;
50  ySrcBase = (yloc<0)?-yloc:0;
51  boolean useDBZValue = !Float.isInfinite(divideByZeroValue);
52  float src, dst;
53  for (int y=r1.y; y<(r1.y+r1.height); y++) {
54  srcIndex = (y-yloc)*srcWidth + (r1.x-xloc);
55  dstIndex = y * width + r1.x;
56  switch (mode) {
57  case COPY: case COPY_INVERTED:
58  for (int i=r1.width; --i>=0;)
59  pixels[dstIndex++] = srcPixels[srcIndex++];
60  break;
61  case ADD:
62  for (int i=r1.width; --i>=0; srcIndex++, dstIndex++)
63  pixels[dstIndex] = srcPixels[srcIndex]+pixels[dstIndex];
64  break;
65  case AVERAGE:
66  for (int i=r1.width; --i>=0;) {
67  dst =(srcPixels[srcIndex++]+pixels[dstIndex])/2;
68  pixels[dstIndex++] = dst;
69  }
70  break;
71  case DIFFERENCE:
72  for (int i=r1.width; --i>=0; srcIndex++, dstIndex++) {
73  dst = pixels[dstIndex]-srcPixels[srcIndex];
74  pixels[dstIndex] = dst<0?-dst:dst;
75  }
76  break;
77  case SUBTRACT:
78  for (int i=r1.width; --i>=0; srcIndex++, dstIndex++)
79  pixels[dstIndex] = pixels[dstIndex]-srcPixels[srcIndex];
80  break;
81  case MULTIPLY:
82  for (int i=r1.width; --i>=0; srcIndex++, dstIndex++)
83  pixels[dstIndex] = srcPixels[srcIndex]*pixels[dstIndex];
84  break;
85  case DIVIDE:
86  for (int i=r1.width; --i>=0; srcIndex++, dstIndex++) {
87  src = srcPixels[srcIndex];
88  if (useDBZValue && src==0.0)
89  pixels[dstIndex] = divideByZeroValue;
90  else
91  pixels[dstIndex] = pixels[dstIndex]/src;
92  }
93  break;
94  case AND: case OR: case XOR:
95  break;
96  case MIN:
97  for (int i=r1.width; --i>=0;) {
98  src = srcPixels[srcIndex++];
99  dst = pixels[dstIndex];
100  if (src<dst) dst = src;
101  pixels[dstIndex++] = dst;
102  }
103  break;
104  case MAX:
105  for (int i=r1.width; --i>=0;) {
106  src = srcPixels[srcIndex++];
107  dst = pixels[dstIndex];
108  if (src>dst) dst = src;
109  pixels[dstIndex++] = dst;
110  }
111  break;
112  }
113  if (y%20==0)
114  ip.showProgress((double)(y-r1.y)/r1.height);
115  }
116  ip.hideProgress();
117  }
118 }