1:
36:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44:
45:
48: public class RescaleOp implements BufferedImageOp, RasterOp
49: {
50: private float[] scale;
51: private float[] offsets;
52: private RenderingHints hints = null;
53:
54: public RescaleOp(float[] scaleFactors,
55: float[] offsets,
56: RenderingHints hints)
57: {
58: this.scale = scaleFactors;
59: this.offsets = offsets;
60: this.hints = hints;
61: }
62:
63: public RescaleOp(float scaleFactor,
64: float offset,
65: RenderingHints hints)
66: {
67: scale = new float[]{ scaleFactor };
68: offsets = new float[]{offset};
69: this.hints = hints;
70: }
71:
72: public final float[] getScaleFactors(float[] scaleFactors)
73: {
74: if (scaleFactors == null)
75: scaleFactors = new float[scale.length];
76: System.arraycopy(scale, 0, scaleFactors, 0, scale.length);
77: return scaleFactors;
78: }
79:
80: public final float[] getOffsets(float[] offsets)
81: {
82: if (offsets == null)
83: offsets = new float[this.offsets.length];
84: System.arraycopy(this.offsets, 0, offsets, 0, this.offsets.length);
85: return offsets;
86: }
87:
88: public final int getNumFactors()
89: {
90: return scale.length;
91: }
92:
93:
96: public final RenderingHints getRenderingHints()
97: {
98: return hints;
99: }
100:
101:
104: public final BufferedImage filter(BufferedImage src, BufferedImage dst)
105: {
106:
107:
108:
109: if (scale.length != offsets.length)
110: throw new IllegalArgumentException();
111:
112: ColorModel scm = src.getColorModel();
113: if (dst == null) dst = createCompatibleDestImage(src, null);
114:
115: WritableRaster wsrc = src.getRaster();
116: WritableRaster wdst = dst.getRaster();
117:
118:
119: if (scale.length == 1 || scale.length == scm.getNumColorComponents())
120: {
121:
122: int[] subbands = new int[scm.getNumColorComponents()];
123: for (int i=0; i < subbands.length; i++) subbands[i] = i;
124: wsrc =
125: wsrc.createWritableChild(wsrc.minX, wsrc.minY, wsrc.width, wsrc.height,
126: wsrc.minX, wsrc.minY, subbands);
127: }
128:
129:
130: filter(wsrc, wdst);
131: return dst;
132: }
133:
134:
137: public final WritableRaster filter(Raster src, WritableRaster dest)
138: {
139: if (dest == null) dest = src.createCompatibleWritableRaster();
140:
141:
142: if (src.numBands != dest.numBands || scale.length != offsets.length)
143: throw new IllegalArgumentException();
144: if (scale.length != 1 && scale.length != src.numBands)
145: throw new IllegalArgumentException();
146:
147:
148: float[] lscale = scale;
149: float[] loff = offsets;
150: if (scale.length == 1)
151: {
152: lscale = new float[src.numBands];
153: Arrays.fill(lscale, scale[0]);
154: loff = new float[src.numBands];
155: Arrays.fill(loff, offsets[0]);
156: }
157:
158:
159:
160: float[] pixel = new float[src.numBands];
161: for (int y = src.minY; y < src.height + src.minY; y++)
162: for (int x = src.minX; x < src.width + src.minX; x++)
163: {
164: src.getPixel(x, y, pixel);
165: for (int b = 0; b < src.numBands; b++)
166: pixel[b] = pixel[b] * lscale[b] + loff[b];
167: dest.setPixel(x, y, pixel);
168: }
169: return dest;
170: }
171:
172:
175: public BufferedImage createCompatibleDestImage(BufferedImage src,
176: ColorModel dstCM)
177: {
178: if (dstCM == null) dstCM = src.getColorModel();
179: WritableRaster wr = src.getRaster().createCompatibleWritableRaster();
180: BufferedImage image
181: = new BufferedImage(dstCM, wr, src.isPremultiplied, null);
182: return image;
183: }
184:
185:
188: public WritableRaster createCompatibleDestRaster(Raster src)
189: {
190: return src.createCompatibleWritableRaster();
191: }
192:
193:
196: public final Rectangle2D getBounds2D(BufferedImage src)
197: {
198: return src.getRaster().getBounds();
199: }
200:
201:
204: public final Rectangle2D getBounds2D(Raster src)
205: {
206: return src.getBounds();
207: }
208:
209:
212: public final Point2D getPoint2D(Point2D src, Point2D dst) {
213: if (dst == null) dst = (Point2D) src.clone();
214: else dst.setLocation(src);
215: return dst;
216: }
217:
218: }