001: /*
002: * @(#)GdkSubImage.java 1.10 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.gtk;
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 GdkSubImage extends Image implements BufferedImagePeer,
046: GdkDrawableImage {
047: /** The source image for which this is a sub image of. */
048:
049: private GdkImage sourceImage;
050: /** The area of the source image this is a sub image of. */
051:
052: private int x, y, width, height;
053: /** The image producer. This is just a CropImageFilter on the origional image. */
054:
055: private ImageProducer producer;
056:
057: GdkSubImage(GdkImage sourceImage, int x, int y, int width,
058: int height) {
059: this .sourceImage = sourceImage;
060: this .x = x;
061: this .y = y;
062: this .width = width;
063: this .height = height;
064: }
065:
066: public int getWidth(ImageObserver observer) {
067: return width;
068: }
069:
070: public int getWidth() {
071: return width;
072: }
073:
074: public int getHeight(ImageObserver observer) {
075: return height;
076: }
077:
078: public int getHeight() {
079: return height;
080: }
081:
082: public synchronized ImageProducer getSource() {
083: if (producer == null)
084: producer = new FilteredImageSource(sourceImage.getSource(),
085: new CropImageFilter(x, y, width, height));
086: return producer;
087: }
088:
089: public Graphics getGraphics() {
090: Graphics g = sourceImage.getGraphics();
091: g.translate(x, y);
092: g.clipRect(0, 0, width, height);
093: return g;
094: }
095:
096: public Object getProperty(String name, ImageObserver observer) {
097: return sourceImage.getProperty(name, observer);
098: }
099:
100: public boolean isComplete() {
101: return sourceImage.isComplete();
102: }
103:
104: public void flush() {
105: }
106:
107: // BufferedImagePeer implementation
108:
109: public int getType() {
110: return sourceImage.getType();
111: }
112:
113: public ColorModel getColorModel() {
114: return sourceImage.getColorModel();
115: }
116:
117: public int getRGB(int x, int y) {
118: return sourceImage.getRGB(x + this .x, y + this .y);
119: }
120:
121: public int[] getRGB(int startX, int startY, int w, int h,
122: int[] rgbArray, int offset, int scansize) {
123: return sourceImage.getRGB(startX + this .x, startY + this .y, w,
124: h, rgbArray, offset, scansize);
125: }
126:
127: public void setRGB(int x, int y, int rgb) {
128: sourceImage.setRGB(x + this .x, y + this .y, rgb);
129: }
130:
131: public void setRGB(int startX, int startY, int w, int h,
132: int[] rgbArray, int offset, int scansize) {
133: sourceImage.setRGB(startX + this .x, startY + this .y, w, h,
134: rgbArray, offset, scansize);
135: }
136:
137: public Object getProperty(String name) {
138: return sourceImage.getProperty(name);
139: }
140:
141: public BufferedImage getSubimage(int x, int y, int w, int h) {
142: if ((x + w < this .x) || (x + w > this .x + this .width)) {
143: throw new RasterFormatException(
144: "(x + width) is outside raster");
145: }
146: if ((y + h < this .y) || (y + h > this .y + this .height)) {
147: throw new RasterFormatException(
148: "(y + height) is outside raster");
149: }
150: return GToolkit.createBufferedImage(new GdkSubImage(
151: sourceImage, x + this .x, y + this .y, width, height));
152: }
153:
154: public String[] getPropertyNames() {
155: return sourceImage.getPropertyNames();
156: }
157:
158: public boolean draw(GdkGraphics g, int x, int y,
159: ImageObserver observer) {
160: return sourceImage.draw(g, x, y, x + width, y + height, this .x,
161: this .y, this .x + width, this .y + height, observer);
162: }
163:
164: public boolean draw(GdkGraphics g, int x, int y, int width,
165: int height, ImageObserver observer) {
166: return sourceImage.draw(g, x, y, x + width, y + height, this .x,
167: this .y, this .x + this .width, this .y + this .height,
168: observer);
169: }
170:
171: public boolean draw(GdkGraphics g, int x, int y, Color bg,
172: ImageObserver observer) {
173: return sourceImage.draw(g, x, y, x + width, y + height, this .x,
174: this .y, this .x + width, this .y + height, bg, observer);
175: }
176:
177: public boolean draw(GdkGraphics g, int x, int y, int width,
178: int height, Color bg, ImageObserver observer) {
179: return sourceImage.draw(g, x, y, x + width, y + height, this .x,
180: this .y, this .x + this .width, this .y + this .height, bg,
181: observer);
182: }
183:
184: public boolean draw(GdkGraphics g, int dx1, int dy1, int dx2,
185: int dy2, int sx1, int sy1, int sx2, int sy2,
186: ImageObserver observer) {
187: return sourceImage.draw(g, dx1, dy1, dx2, dy2, sx1 + this .x,
188: sy1 + this .y, sx2 + this .x, sy2 + this .y, observer);
189: }
190:
191: public boolean draw(GdkGraphics g, int dx1, int dy1, int dx2,
192: int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor,
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, bgcolor,
196: observer);
197: }
198: }
|