001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Igor V. Stolyarov
019: * @version $Revision$
020: */package java.awt.image;
021:
022: import java.util.Hashtable;
023:
024: import org.apache.harmony.awt.internal.nls.Messages;
025:
026: public class ReplicateScaleFilter extends ImageFilter {
027:
028: protected int srcWidth;
029:
030: protected int srcHeight;
031:
032: protected int destWidth;
033:
034: protected int destHeight;
035:
036: protected int[] srcrows;
037:
038: protected int[] srccols;
039:
040: protected Object outpixbuf;
041:
042: public ReplicateScaleFilter(int width, int height) {
043: if (width == 0 || height == 0) {
044: // awt.234=Width or Height equals zero
045: throw new IllegalArgumentException(Messages
046: .getString("awt.234")); //$NON-NLS-1$
047: }
048:
049: this .destWidth = width;
050: this .destHeight = height;
051: }
052:
053: @SuppressWarnings("unchecked")
054: @Override
055: public void setProperties(Hashtable<?, ?> props) {
056: Hashtable<Object, Object> fprops;
057: if (props == null) {
058: fprops = new Hashtable<Object, Object>();
059: } else {
060: fprops = (Hashtable<Object, Object>) props.clone();
061: }
062: String propName = "Rescale Filters"; //$NON-NLS-1$
063: String prop = "destWidth=" + destWidth + "; " + //$NON-NLS-1$ //$NON-NLS-2$
064: "destHeight=" + destHeight; //$NON-NLS-1$
065: Object o = fprops.get(propName);
066: if (o != null) {
067: if (o instanceof String) {
068: prop = (String) o + "; " + prop; //$NON-NLS-1$
069: } else {
070: prop = o.toString() + "; " + prop; //$NON-NLS-1$
071: }
072: }
073: fprops.put(propName, prop);
074: consumer.setProperties(fprops);
075: }
076:
077: // setPixels methods produce pixels according to Java API Spacification
078: @Override
079: public void setPixels(int x, int y, int w, int h, ColorModel model,
080: int[] pixels, int off, int scansize) {
081:
082: if (srccols == null) {
083: initArrays();
084: }
085: int buff[];
086: if (outpixbuf == null || !(outpixbuf instanceof int[])) {
087: buff = new int[destWidth];
088: outpixbuf = buff;
089: } else {
090: buff = (int[]) outpixbuf;
091: }
092:
093: int wa = (srcWidth - 1) >>> 1;
094: int ha = (srcHeight - 1) >>> 1;
095: int dstX = (x * destWidth + wa) / srcWidth;
096: int dstY = (y * destHeight + ha) / srcHeight;
097:
098: int sx, sy, dx, dy;
099: dy = dstY;
100: while ((dy < destHeight) && ((sy = srcrows[dy]) < y + h)) {
101: dx = dstX;
102: int srcOff = off + (sy - y) * scansize;
103: while ((dx < destWidth) && ((sx = srccols[dx]) < x + w)) {
104: buff[dx] = pixels[srcOff + (sx - x)];
105: dx++;
106: }
107:
108: consumer.setPixels(dstX, dy, dx - dstX, 1, model, buff,
109: dstX, destWidth);
110: dy++;
111: }
112: }
113:
114: @Override
115: public void setPixels(int x, int y, int w, int h, ColorModel model,
116: byte[] pixels, int off, int scansize) {
117:
118: if (srccols == null) {
119: initArrays();
120: }
121: byte buff[];
122: if (outpixbuf == null || !(outpixbuf instanceof byte[])) {
123: buff = new byte[destWidth];
124: outpixbuf = buff;
125: } else {
126: buff = (byte[]) outpixbuf;
127: }
128:
129: int wa = (srcWidth - 1) >>> 1;
130: int ha = (srcHeight - 1) >>> 1;
131: int dstX = (x * destWidth + wa) / srcWidth;
132: int dstY = (y * destHeight + ha) / srcHeight;
133:
134: int sx, sy, dx, dy;
135: dy = dstY;
136: while ((dy < destHeight) && ((sy = srcrows[dy]) < y + h)) {
137: dx = dstX;
138: int srcOff = off + (sy - y) * scansize;
139: while ((dx < destWidth) && ((sx = srccols[dx]) < x + w)) {
140: buff[dx] = pixels[srcOff + (sx - x)];
141: dx++;
142: }
143:
144: consumer.setPixels(dstX, dy, dx - dstX, 1, model, buff,
145: dstX, destWidth);
146: dy++;
147: }
148: }
149:
150: @Override
151: public void setDimensions(int w, int h) {
152: srcWidth = w;
153: srcHeight = h;
154:
155: if (destWidth < 0 && destHeight < 0) {
156: destWidth = srcWidth;
157: destHeight = srcHeight;
158: } else if (destWidth < 0) {
159: destWidth = destHeight * srcWidth / srcHeight;
160: } else if (destHeight < 0) {
161: destHeight = destWidth * srcHeight / srcWidth;
162: }
163: consumer.setDimensions(destWidth, destHeight);
164: }
165:
166: /**
167: * Initialization of srccols and srcrows arrays
168: */
169: private void initArrays() {
170: if ((destWidth < 0) || (destHeight < 0)) {
171: throw new IndexOutOfBoundsException();
172: }
173:
174: srccols = new int[destWidth];
175: int ca = srcWidth >>> 1;
176: for (int i = 0; i < destWidth; i++) {
177: srccols[i] = (i * srcWidth + ca) / destWidth;
178: }
179:
180: srcrows = new int[destHeight];
181: int ra = srcHeight >>> 1;
182: for (int i = 0; i < destHeight; i++) {
183: srcrows[i] = (i * srcHeight + ra) / destHeight;
184: }
185: }
186:
187: }
|