001: /*
002: * @(#)QtSubImage.java 1.6 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package sun.awt.qt;
029:
030: import sun.awt.image.BufferedImagePeer;
031: import java.awt.Color;
032: import java.awt.Image;
033: import java.awt.Graphics;
034: import java.awt.image.ColorModel;
035: import java.awt.image.ImageObserver;
036: import java.awt.image.ImageProducer;
037: import java.awt.image.FilteredImageSource;
038: import java.awt.image.CropImageFilter;
039: import java.awt.image.BufferedImage;
040: import java.awt.image.RasterFormatException;
041:
042: /** An image which represents an area of another image. It shares the same pixel storage
043: as the source. */
044:
045: class QtSubImage extends Image implements BufferedImagePeer,
046: QtDrawableImage {
047:
048: /** The source image for which this is a sub image of. */
049:
050: private QtImage sourceImage;
051:
052: /** The area of the source image this is a sub image of. */
053:
054: private int x, y, width, height;
055:
056: /** The image producer. This is just a CropImageFilter on the origional image. */
057:
058: private ImageProducer producer;
059:
060: QtSubImage(QtImage sourceImage, int x, int y, int width, int height) {
061: this .sourceImage = sourceImage;
062: this .x = x;
063: this .y = y;
064: this .width = width;
065: this .height = height;
066: }
067:
068: public int getWidth(ImageObserver observer) {
069: return width;
070: }
071:
072: public int getWidth() {
073: return width;
074: }
075:
076: public int getHeight(ImageObserver observer) {
077: return height;
078: }
079:
080: public int getHeight() {
081: return height;
082: }
083:
084: public synchronized ImageProducer getSource() {
085: if (producer == null)
086: producer = new FilteredImageSource(sourceImage.getSource(),
087: new CropImageFilter(x, y, width, height));
088:
089: return producer;
090: }
091:
092: public Graphics getGraphics() {
093: Graphics g = sourceImage.getGraphics();
094:
095: g.translate(x, y);
096: g.clipRect(0, 0, width, height);
097:
098: return g;
099: }
100:
101: public Object getProperty(String name, ImageObserver observer) {
102: return sourceImage.getProperty(name, observer);
103: }
104:
105: public boolean isComplete() {
106: return sourceImage.isComplete();
107: }
108:
109: public void flush() {
110: }
111:
112: // BufferedImagePeer implementation
113:
114: public int getType() {
115: return sourceImage.getType();
116: }
117:
118: public ColorModel getColorModel() {
119: return sourceImage.getColorModel();
120: }
121:
122: public int getRGB(int x, int y) {
123: return sourceImage.getRGB(x + this .x, y + this .y);
124: }
125:
126: public int[] getRGB(int startX, int startY, int w, int h,
127: int[] rgbArray, int offset, int scansize) {
128: return sourceImage.getRGB(startX + this .x, startY + this .y, w,
129: h, rgbArray, offset, scansize);
130: }
131:
132: public void setRGB(int x, int y, int rgb) {
133: sourceImage.setRGB(x + this .x, y + this .y, rgb);
134: }
135:
136: public void setRGB(int startX, int startY, int w, int h,
137: int[] rgbArray, int offset, int scansize) {
138: sourceImage.setRGB(startX + this .x, startY + this .y, w, h,
139: rgbArray, offset, scansize);
140: }
141:
142: public Object getProperty(String name) {
143: return sourceImage.getProperty(name);
144: }
145:
146: public BufferedImage getSubimage(int x, int y, int w, int h) {
147: if ((x + w < this .x) || (x + w > this .x + this .width)) {
148: throw new RasterFormatException(
149: "(x + width) is outside raster");
150: }
151:
152: if ((y + h < this .y) || (y + h > this .y + this .height)) {
153: throw new RasterFormatException(
154: "(y + height) is outside raster");
155: }
156:
157: return QtToolkit.createBufferedImage(new QtSubImage(
158: sourceImage, x + this .x, y + this .y, width, height));
159: }
160:
161: public String[] getPropertyNames() {
162: return sourceImage.getPropertyNames();
163: }
164:
165: public boolean draw(QtGraphics g, int x, int y,
166: ImageObserver observer) {
167: return sourceImage.draw(g, x, y, x + width, y + height, this .x,
168: this .y, this .x + width, this .y + height, observer);
169: }
170:
171: public boolean draw(QtGraphics g, int x, int y, int width,
172: int height, ImageObserver observer) {
173: return sourceImage.draw(g, x, y, x + width, y + height, this .x,
174: this .y, this .x + this .width, this .y + this .height,
175: observer);
176: }
177:
178: public boolean draw(QtGraphics g, int x, int y, Color bg,
179: ImageObserver observer) {
180: return sourceImage.draw(g, x, y, x + width, y + height, this .x,
181: this .y, this .x + width, this .y + height, bg, observer);
182: }
183:
184: public boolean draw(QtGraphics g, int x, int y, int width,
185: int height, Color bg, ImageObserver observer) {
186: return sourceImage.draw(g, x, y, x + width, y + height, this .x,
187: this .y, this .x + this .width, this .y + this .height, bg,
188: observer);
189: }
190:
191: public boolean draw(QtGraphics g, int dx1, int dy1, int dx2,
192: int dy2, int sx1, int sy1, int sx2, int sy2,
193: ImageObserver observer) {
194: return sourceImage.draw(g, dx1, dy1, dx2, dy2, sx1 + this .x,
195: sy1 + this .y, sx2 + this .x, sy2 + this .y, observer);
196: }
197:
198: public boolean draw(QtGraphics g, int dx1, int dy1, int dx2,
199: int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor,
200: ImageObserver observer) {
201: return sourceImage.draw(g, dx1, dy1, dx2, dy2, sx1 + this.x,
202: sy1 + this.y, sx2 + this.x, sy2 + this.y, bgcolor,
203: observer);
204: }
205: }
|