/
InverseColorMapOp.java
230 lines (203 loc) · 6.86 KB
/
InverseColorMapOp.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
* GeoTools - The Open Source Java GIS Toolkit
* http://geotools.org
*
* (C) 2016, Open Source Geospatial Foundation (OSGeo)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package org.geotools.image.palette;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ColorModel;
import java.awt.image.IndexColorModel;
import java.awt.image.Raster;
import java.awt.image.RenderedImage;
import java.awt.image.WritableRaster;
/**
* This class provide an Image oriented interface for the
* {@link EfficientInverseColorMapComputation}. Specifically, it is designed in
* order to implement the {@link BufferedImage} for processing
* {@link BufferedImage}s efficiently accessing the raster pixels directly but
* it also provide a method to process general {@link RenderedImage}s
* implementations.
*
* @author Simone Giannecchini - GeoSolutions SAS
* @see EfficientInverseColorMapComputation
*
*
*
* @source $URL$
*/
public final class InverseColorMapOp implements BufferedImageOp {
protected final InverseColorMapRasterOp rasterOp;
protected final IndexColorModel icm;
protected final int alphaThreshold;
protected final boolean hasAlpha;
protected final int transparencyIndex;
public InverseColorMapOp(final IndexColorModel destCM,
final int quantizationColors, final int alphaThreshold) {
this.rasterOp = new InverseColorMapRasterOp(destCM, quantizationColors,
alphaThreshold);
this.icm = destCM;
this.alphaThreshold = alphaThreshold;
hasAlpha = icm.hasAlpha();
transparencyIndex = icm.getTransparentPixel();
}
public InverseColorMapOp(final IndexColorModel destCM) {
this(destCM, InverseColorMapRasterOp.DEFAULT_QUANTIZATION_COLORS,
InverseColorMapRasterOp.DEFAULT_ALPHA_TH);
}
public BufferedImage createCompatibleDestImage(BufferedImage src,
ColorModel destCM) {
if (!(destCM instanceof IndexColorModel)
|| ((IndexColorModel) destCM).getTransparency() == Transparency.TRANSLUCENT)
return null;
return new BufferedImage(src.getWidth(), src.getHeight(),
BufferedImage.TYPE_BYTE_INDEXED, (IndexColorModel) destCM);
}
public BufferedImage filter(BufferedImage src, BufferedImage dest) {
if (dest == null)
dest = new BufferedImage(src.getWidth(), src.getHeight(),
BufferedImage.TYPE_BYTE_INDEXED, icm);
else {
if (!(dest.getColorModel() instanceof IndexColorModel)
|| ((IndexColorModel) dest.getColorModel())
.getTransparency() != this.transparencyIndex)
throw new IllegalArgumentException();
if (((IndexColorModel) dest.getColorModel()).getTransparentPixel() != this.transparencyIndex)
throw new IllegalArgumentException();
}
final WritableRaster wr = dest.getRaster();
final Raster ir = src.getRaster();
this.rasterOp.filter(ir, wr);
return dest;
}
public BufferedImage filterRenderedImage(RenderedImage src) {
// //
//
// ShortCut for using bufferedimages and avoiding tiling
//
// //
if (src instanceof BufferedImage)
return filter((BufferedImage) src, null);
// //
//
// Create the destination image
//
// //
final BufferedImage dest = new BufferedImage(src.getWidth(), src
.getHeight(), BufferedImage.TYPE_BYTE_INDEXED, icm);
final WritableRaster destWr = dest.getRaster();
// //
//
// Filter the image out
//
// //
// /
//
// Optimize the hell out of this code. We have a single tile, let's go
// fast!
//
// //
if (src.getNumXTiles() == 1 && src.getNumYTiles() == 1) {
final int minTileX = src.getMinTileX();
final int minTileY = src.getMinTileY();
final Raster sourceR = src.getTile(minTileX, minTileY);
rasterOp.filter(sourceR.createChild(src.getMinX(), src.getMinY(),
src.getWidth(), src.getHeight(), 0, 0, null), destWr);
return dest;
}
// //
//
// Collecting info about the source image
//
// //
final int numBands = src.getSampleModel().getNumBands();
final int[] rgba = new int[numBands];
final boolean sourceHasAlpha = (numBands % 2 == 0);
final int alphaBand = sourceHasAlpha ? numBands - 1 : -1;
final EfficientInverseColorMapComputation invCM = rasterOp.getInvCM();
final int minx_ = src.getMinX();
final int miny_ = src.getMinY();
final int srcW_ = src.getWidth();
final int srcH_ = src.getHeight();
final int maxx_ = minx_ + srcW_;
final int maxy_ = miny_ + srcH_;
final int minTileX = src.getMinTileX();
final int minTileY = src.getMinTileY();
final int tileW = src.getTileWidth();
final int tileH = src.getTileHeight();
final int maxTileX = minTileX + src.getNumXTiles();
final int maxTileY = minTileY + src.getNumYTiles();
int dstTempX = 0;
int dstTempY = 0;
for (int ty = minTileY; ty < maxTileY; ty++) {
dstTempX = 0;
int actualWidth = 0;
int actualHeight = 0;
for (int tx = minTileX; tx < maxTileX; tx++) {
// get the source raster
final Raster r = src.getTile(tx, ty);
int minx = r.getMinX();
int miny = r.getMinY();
minx = minx < minx_ ? minx_ : minx;
miny = miny < miny_ ? miny_ : miny;
int maxx = minx + tileW;
int maxy = miny + tileH;
maxx = maxx > maxx_ ? maxx_ : maxx;
maxy = maxy > maxy_ ? maxy_ : maxy;
actualWidth = maxx - minx;
actualHeight = maxy - miny;
for (int j = miny, jj = dstTempY; j < maxy; j++, jj++) {
for (int i = minx, ii = dstTempX; i < maxx; i++, ii++) {
r.getPixel(i, j, rgba);
// wr.setPixel(i, j, rgba);
if (!sourceHasAlpha
|| !hasAlpha
|| (sourceHasAlpha && hasAlpha && rgba[alphaBand] >= this.alphaThreshold)) {
int val = invCM.getIndexNearest(rgba[0] & 0xff,
rgba[numBands == 1 ? 0 : 1] & 0xff, rgba[numBands == 1 ? 0 : 2]);
if (hasAlpha && val >= transparencyIndex)
val++;
destWr.setSample(ii, jj, 0, (byte) (val & 0xff));
} else
destWr.setSample(ii, jj, 0, transparencyIndex);
}
}
dstTempX += actualWidth;
}
dstTempY += actualHeight;
}
return dest;
}
public Rectangle2D getBounds2D(BufferedImage src) {
return new Rectangle(src.getWidth(), src.getHeight());
}
public Point2D getPoint2D(Point2D srcPt, Point2D dstPt) {
if (dstPt == null)
dstPt = new Point();
dstPt.setLocation(srcPt);
return dstPt;
}
public RenderingHints getRenderingHints() {
return null;
}
public IndexColorModel getIcm() {
return icm;
}
}