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