001: /*
002: * @(#)GdkImage.java 1.17 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 java.awt.Component;
031: import java.awt.Graphics;
032: import java.awt.GraphicsEnvironment;
033: import java.awt.Color;
034: import java.awt.image.ImageProducer;
035: import java.awt.image.ImageObserver;
036: import java.awt.image.ColorModel;
037: import java.awt.image.DirectColorModel;
038: import java.awt.image.BufferedImage;
039: import java.awt.image.RasterFormatException;
040: import sun.awt.image.ImageRepresentation;
041: import sun.awt.image.BufferedImagePeer;
042:
043: class GdkImage extends sun.awt.image.Image implements
044: BufferedImagePeer, GdkDrawableImage {
045: GdkGraphicsConfiguration gc;
046:
047: /* Tunnel the Offscreen image constructor to the super class. */
048: protected GdkImage(Component c, int w, int h) {
049: super (c, w, h);
050: gc = (GdkGraphicsConfiguration) c.getGraphicsConfiguration();
051: if (gc == null) {
052: gc = (GdkGraphicsConfiguration) GraphicsEnvironment
053: .getLocalGraphicsEnvironment()
054: .getDefaultScreenDevice().getDefaultConfiguration();
055: }
056: }
057:
058: /**
059: * Construct an image from an ImageProducer object.
060: */
061: public GdkImage(ImageProducer producer) {
062: super (producer);
063: gc = (GdkGraphicsConfiguration) GraphicsEnvironment
064: .getLocalGraphicsEnvironment().getDefaultScreenDevice()
065: .getDefaultConfiguration();
066: }
067:
068: public Graphics getGraphics() {
069: Graphics g = new GdkGraphics(this );
070: initGraphics(g);
071: return g;
072: }
073:
074: public boolean isComplete() {
075: return ((getImageRep().check(null) & ImageObserver.ALLBITS) != 0);
076: }
077:
078: protected ImageRepresentation makeImageRep() {
079: return new GdkImageRepresentation(this );
080: }
081:
082: protected ImageRepresentation getImageRep() {
083: return super .getImageRep();
084: }
085:
086: // BufferedImagePeer implementation
087:
088: public int getType() {
089: return BufferedImage.TYPE_CUSTOM;
090: }
091:
092: public ColorModel getColorModel() {
093: return gc.getColorModel();
094: }
095:
096: public Object getProperty(String name) {
097: return super .getProperty(name, null);
098: }
099:
100: public BufferedImage getSubimage(int x, int y, int w, int h) {
101: if ((x + w < x) || (x + w > getWidth())) {
102: throw new RasterFormatException(
103: "(x + width) is outside raster");
104: }
105: if ((y + h < y) || (y + h > getHeight())) {
106: throw new RasterFormatException(
107: "(y + height) is outside raster");
108: }
109: return GToolkit.createBufferedImage(new GdkSubImage(this , x, y,
110: w, h));
111: }
112:
113: public String[] getPropertyNames() {
114: return null;
115: }
116:
117: public int getRGB(int x, int y) {
118: return ((GdkImageRepresentation) getImageRep()).getRGB(
119: ColorModel.getRGBdefault(), x, y);
120: }
121:
122: public int[] getRGB(int startX, int startY, int w, int h,
123: int[] rgbArray, int offset, int scansize) {
124: if (null == rgbArray)
125: rgbArray = new int[offset + h * scansize];
126: else if (rgbArray.length < offset + h * scansize)
127: throw new IllegalArgumentException(
128: "rgbArray is not large enough to store all the values");
129: ((GdkImageRepresentation) getImageRep()).getRGBs(ColorModel
130: .getRGBdefault(), startX, startY, w, h, rgbArray,
131: offset, scansize);
132: return rgbArray;
133: }
134:
135: /** The color model used for setRGB calls. We don't use the default RGB color model because this has
136: alpha and BufferedImages don't have an alpha channel. This may be changed in the future if we
137: add alpha channel rendering to the Graphics object for BufferedImages.
138: see bug 4732134 */
139:
140: private static final ColorModel COLOR_MODEL_FOR_SET_RGB = new DirectColorModel(
141: 24, 0xff0000, 0xff00, 0xff);
142:
143: public void setRGB(int x, int y, int rgb) {
144: getImageRep().setPixels(x, y, 1, 1, COLOR_MODEL_FOR_SET_RGB,
145: new int[] { rgb }, 0, 0);
146: }
147:
148: public void setRGB(int startX, int startY, int w, int h,
149: int[] rgbArray, int offset, int scansize) {
150: getImageRep().setPixels(startX, startY, w, h,
151: COLOR_MODEL_FOR_SET_RGB, rgbArray, offset, scansize);
152: }
153:
154: // GdkDrawableImage implementation
155:
156: public boolean draw(GdkGraphics g, int x, int y,
157: ImageObserver observer) {
158: if (hasError()) {
159: if (observer != null) {
160: observer.imageUpdate(this , ImageObserver.ERROR
161: | ImageObserver.ABORT, -1, -1, -1, -1);
162: }
163: return false;
164: }
165: return getImageRep().drawImage(g, x, y, null, observer);
166: }
167:
168: public boolean draw(GdkGraphics g, int x, int y, int width,
169: int height, ImageObserver observer) {
170: if (width == 0 || height == 0) {
171: return true;
172: }
173: if (hasError()) {
174: if (observer != null) {
175: observer.imageUpdate(this , ImageObserver.ERROR
176: | ImageObserver.ABORT, -1, -1, -1, -1);
177: }
178: return false;
179: }
180: return getImageRep().drawScaledImage(g, x, y, width, height,
181: null, observer);
182: }
183:
184: public boolean draw(GdkGraphics g, int x, int y, Color bg,
185: ImageObserver observer) {
186: if (hasError()) {
187: if (observer != null) {
188: observer.imageUpdate(this , ImageObserver.ERROR
189: | ImageObserver.ABORT, -1, -1, -1, -1);
190: }
191: return false;
192: }
193: return getImageRep().drawImage(g, x, y, bg, observer);
194: }
195:
196: public boolean draw(GdkGraphics g, int x, int y, int width,
197: int height, Color bg, ImageObserver observer) {
198: if (width == 0 || height == 0) {
199: return true;
200: }
201: if (hasError()) {
202: if (observer != null) {
203: observer.imageUpdate(this , ImageObserver.ERROR
204: | ImageObserver.ABORT, -1, -1, -1, -1);
205: }
206: return false;
207: }
208: return getImageRep().drawScaledImage(g, x, y, width, height,
209: bg, observer);
210: }
211:
212: public boolean draw(GdkGraphics g, int dx1, int dy1, int dx2,
213: int dy2, int sx1, int sy1, int sx2, int sy2,
214: ImageObserver observer) {
215: if (dx1 == dx2 || dy1 == dy2) {
216: return true;
217: }
218: if (hasError()) {
219: if (observer != null) {
220: observer.imageUpdate(this , ImageObserver.ERROR
221: | ImageObserver.ABORT, -1, -1, -1, -1);
222: }
223: return false;
224: }
225: return getImageRep().drawStretchImage(g, dx1, dy1, dx2, dy2,
226: sx1, sy1, sx2, sy2, null, observer);
227: }
228:
229: public boolean draw(GdkGraphics g, int dx1, int dy1, int dx2,
230: int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor,
231: ImageObserver observer) {
232: if (dx1 == dx2 || dy1 == dy2) {
233: return true;
234: }
235: if (hasError()) {
236: if (observer != null) {
237: observer.imageUpdate(this , ImageObserver.ERROR
238: | ImageObserver.ABORT, -1, -1, -1, -1);
239: }
240: return false;
241: }
242: return getImageRep().drawStretchImage(g, dx1, dy1, dx2, dy2,
243: sx1, sy1, sx2, sy2, bgcolor, observer);
244: }
245: }
|