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: public class CropImageFilter extends ImageFilter {
025:
026: private final int X, Y, WIDTH, HEIGHT;
027:
028: public CropImageFilter(int x, int y, int w, int h) {
029: X = x;
030: Y = y;
031: WIDTH = w;
032: HEIGHT = h;
033: }
034:
035: @SuppressWarnings("unchecked")
036: @Override
037: public void setProperties(Hashtable<?, ?> props) {
038: Hashtable<Object, Object> fprops;
039: if (props == null) {
040: fprops = new Hashtable<Object, Object>();
041: } else {
042: fprops = (Hashtable<Object, Object>) props.clone();
043: }
044: String propName = "Crop Filters"; //$NON-NLS-1$
045: String prop = "x=" + X + "; y=" + Y + "; width=" + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
046: WIDTH + "; height=" + HEIGHT; //$NON-NLS-1$
047: Object o = fprops.get(propName);
048: if (o != null) {
049: if (o instanceof String) {
050: prop = (String) o + "; " + prop; //$NON-NLS-1$
051: } else {
052: prop = o.toString() + "; " + prop; //$NON-NLS-1$
053: }
054: }
055: fprops.put(propName, prop);
056: consumer.setProperties(fprops);
057: }
058:
059: @Override
060: public void setPixels(int x, int y, int w, int h, ColorModel model,
061: int[] pixels, int off, int scansize) {
062:
063: if (x + w < X || X + WIDTH < x || y + h < Y || Y + HEIGHT < y) {
064: return;
065: }
066:
067: int destX, destY, destWidth, destHeight, endX, endY, srcEndX, srcEndY;
068:
069: int newOffset = off;
070:
071: endX = X + WIDTH;
072: endY = Y + HEIGHT;
073:
074: srcEndX = x + w;
075: srcEndY = y + h;
076:
077: if (x <= X) {
078: destX = 0;
079: newOffset += X;
080: if (endX >= srcEndX) {
081: destWidth = srcEndX - X;
082: } else {
083: destWidth = WIDTH;
084: }
085: } else {
086: destX = x - X;
087: if (endX >= srcEndX) {
088: destWidth = w;
089: } else {
090: destWidth = endX - x;
091: }
092: }
093:
094: if (y <= Y) {
095: newOffset += scansize * (Y - y);
096: destY = 0;
097: if (endY >= srcEndY) {
098: destHeight = srcEndY - Y;
099: } else {
100: destHeight = HEIGHT;
101: }
102: } else {
103: destY = y - Y;
104: if (endY >= srcEndY) {
105: destHeight = h;
106: } else {
107: destHeight = endY - y;
108: }
109: }
110: consumer.setPixels(destX, destY, destWidth, destHeight, model,
111: pixels, newOffset, scansize);
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 (x + w < X || X + WIDTH < x || y + h < Y || Y + HEIGHT < y) {
119: return;
120: }
121:
122: int destX, destY, destWidth, destHeight, endX, endY, srcEndX, srcEndY;
123:
124: int newOffset = off;
125:
126: endX = X + WIDTH;
127: endY = Y + HEIGHT;
128:
129: srcEndX = x + w;
130: srcEndY = y + h;
131:
132: if (x <= X) {
133: destX = 0;
134: newOffset += X;
135: if (endX >= srcEndX) {
136: destWidth = srcEndX - X;
137: } else {
138: destWidth = WIDTH;
139: }
140: } else {
141: destX = x - X;
142: if (endX >= srcEndX) {
143: destWidth = w;
144: } else {
145: destWidth = endX - x;
146: }
147: }
148:
149: if (y <= Y) {
150: newOffset += scansize * (Y - y);
151: destY = 0;
152: if (endY >= srcEndY) {
153: destHeight = srcEndY - Y;
154: } else {
155: destHeight = HEIGHT;
156: }
157: } else {
158: destY = y - Y;
159: if (endY >= srcEndY) {
160: destHeight = h;
161: } else {
162: destHeight = endY - y;
163: }
164: }
165: consumer.setPixels(destX, destY, destWidth, destHeight, model,
166: pixels, newOffset, scansize);
167: }
168:
169: @Override
170: public void setDimensions(int w, int h) {
171: consumer.setDimensions(WIDTH, HEIGHT);
172: }
173:
174: }
|