1 /* QtVolatileImage.java --
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu
.java
.awt
.peer
.qt
;
40 import java
.awt
.Graphics
;
41 import java
.awt
.Graphics2D
;
42 import java
.awt
.Color
;
43 import java
.awt
.Image
;
44 import java
.awt
.ImageCapabilities
;
45 import java
.awt
.GraphicsConfiguration
;
46 import java
.awt
.image
.BufferedImage
;
47 import java
.awt
.image
.ColorModel
;
48 import java
.awt
.image
.DirectColorModel
;
49 import java
.awt
.image
.MemoryImageSource
;
50 import java
.awt
.image
.ImageConsumer
;
51 import java
.awt
.image
.ImageObserver
;
52 import java
.awt
.image
.ImageProducer
;
53 import java
.awt
.image
.VolatileImage
;
55 import java
.io
.IOException
;
56 import java
.util
.Hashtable
;
57 import java
.util
.WeakHashMap
;
58 import java
.util
.Vector
;
61 * QtVolatileImage - wraps a QImage
64 public class QtVolatileImage
extends VolatileImage
66 int width
= -1, height
= -1;
74 * Pointer to the QImage
79 * The 32-bit AARRGGBB format the uses.
81 static ColorModel nativeModel
= new DirectColorModel(32,
88 * Clears the image to RGBA 0
90 public native void clear();
93 * Returns a copy of the pixel data as a java array.
95 private native int[] getPixels();
100 private native void createImage();
103 * HashMap of Graphics objects painting on this Image.
105 WeakHashMap painters
;
108 * Flags if this image is to be destroyed.
115 private native void freeImage();
120 public native void blit(QtImage i
);
121 public native void blit(QtImage i
, int x
, int y
, int w
, int h
);
124 * Sets the image to scaled copy of src image. hints are rendering hints.
126 private native void createScaledImage(QtVolatileImage src
, int hints
);
129 * Draws the image optionally composited.
131 private native void drawPixels (QtGraphics gc
,
132 int bg_red
, int bg_green
, int bg_blue
,
136 * Draws the image, optionally scaled and composited.
138 private native void drawPixelsScaled (QtGraphics gc
,
139 int bg_red
, int bg_green
, int bg_blue
,
140 int x
, int y
, int width
, int height
,
144 * Draws the image transformed.
146 private native void drawPixelsTransformed (QtGraphics gc
, QMatrix transform
);
149 * Draws the image scaled flipped and optionally composited.
151 native void drawPixelsScaledFlipped (QtGraphics gc
,
152 int bg_red
, int bg_green
,
154 boolean flipX
, boolean flipY
,
156 int srcWidth
, int srcHeight
,
158 int dstWidth
, int dstHeight
,
162 * Constructs an empty QtVolatileImage.
164 public QtVolatileImage (int width
, int height
)
167 this.height
= height
;
168 props
= new Hashtable();
174 * Constructs a scaled version of the src bitmap, using Qt
176 private QtVolatileImage (QtVolatileImage src
, int width
, int height
,
180 this.height
= height
;
181 props
= new Hashtable();
183 createScaledImage(src
, hints
);
187 public void finalize()
192 public void dispose()
194 if( painters
== null || painters
.isEmpty() )
197 killFlag
= true; // can't destroy image yet.
198 // Do so when all painters are gone.
201 // java.awt.Image methods ////////////////////////////////////////////////
203 public int getWidth (ImageObserver observer
)
208 public int getHeight (ImageObserver observer
)
213 public Object
getProperty (String name
, ImageObserver observer
)
215 Object value
= props
.get (name
);
216 return (value
== null) ? UndefinedProperty
: value
;
220 * Returns the source of this image.
222 public ImageProducer
getSource ()
224 return new MemoryImageSource(width
, height
, nativeModel
, getPixels(),
228 void putPainter(QtImageGraphics g
)
230 if( painters
== null )
231 painters
= new WeakHashMap();
232 painters
.put( g
, "dummy" );
235 void removePainter(QtImageGraphics g
)
237 painters
.remove( g
);
238 if( killFlag
&& painters
.isEmpty() )
243 * Creates a Graphics context for this image.
245 public Graphics
getGraphics ()
247 QtImageGraphics g
= new QtImageGraphics( this );
253 * Returns a scaled instance of this image.
255 public Image
getScaledInstance(int width
,
259 if (width
<= 0 || height
<= 0)
260 throw new IllegalArgumentException("Width and height of scaled bitmap"+
263 return new QtVolatileImage(this, width
, height
, hints
);
274 * Returns the image status, used by QtToolkit
276 public int checkImage (ImageObserver observer
)
278 return ImageObserver
.ALLBITS
| ImageObserver
.WIDTH
| ImageObserver
.HEIGHT
;
281 // Drawing methods ////////////////////////////////////////////////
284 * Draws an image with eventual scaling/transforming.
286 public boolean drawImage (QtGraphics g
, QMatrix matrix
,
287 ImageObserver observer
)
289 drawPixelsTransformed (g
, matrix
);
294 * Draws an image to the QtGraphics context, at (x,y) with optional
295 * compositing with a background color.
297 public boolean drawImage (QtGraphics g
, int x
, int y
,
298 Color bgcolor
, ImageObserver observer
)
301 drawPixels(g
, bgcolor
.getRed (), bgcolor
.getGreen (),
302 bgcolor
.getBlue (), x
, y
, true);
304 drawPixels(g
, 0, 0, 0, x
, y
, false);
310 * Draws an image to the QtGraphics context, at (x,y) scaled to
311 * width and height, with optional compositing with a background color.
313 public boolean drawImage (QtGraphics g
, int x
, int y
, int width
, int height
,
314 Color bgcolor
, ImageObserver observer
)
317 drawPixelsScaled(g
, bgcolor
.getRed (), bgcolor
.getGreen (),
318 bgcolor
.getBlue (), x
, y
, width
, height
, true);
320 drawPixelsScaled(g
, 0, 0, 0, x
, y
, width
, height
, false);
326 * Draws an image with eventual scaling/transforming.
328 public boolean drawImage (QtGraphics g
, int dx1
, int dy1
, int dx2
, int dy2
,
329 int sx1
, int sy1
, int sx2
, int sy2
,
330 Color bgcolor
, ImageObserver observer
)
332 boolean flipX
= (dx1
> dx2
)^
(sx1
> sx2
);
333 boolean flipY
= (dy1
> dy2
)^
(sy1
> sy2
);
334 int dstWidth
= Math
.abs (dx2
- dx1
);
335 int dstHeight
= Math
.abs (dy2
- dy1
);
336 int srcWidth
= Math
.abs (sx2
- sx1
);
337 int srcHeight
= Math
.abs (sy2
- sy1
);
338 int srcX
= (sx1
< sx2
) ? sx1
: sx2
;
339 int srcY
= (sy1
< sy2
) ? sy1
: sy2
;
340 int dstX
= (dx1
< dx2
) ? dx1
: dx2
;
341 int dstY
= (dy1
< dy2
) ? dy1
: dy2
;
343 // Clipping. This requires the dst to be scaled as well,
344 if (srcWidth
> width
)
346 dstWidth
= (int)((double)dstWidth
*((double)width
/(double)srcWidth
));
347 srcWidth
= width
- srcX
;
350 if (srcHeight
> height
)
352 dstHeight
= (int)((double)dstHeight
*((double)height
/(double)srcHeight
));
353 srcHeight
= height
- srcY
;
356 if (srcWidth
+ srcX
> width
)
358 dstWidth
= (int)((double)dstWidth
* (double)(width
- srcX
)/(double)srcWidth
);
359 srcWidth
= width
- srcX
;
362 if (srcHeight
+ srcY
> height
)
364 dstHeight
= (int)((double)dstHeight
* (double)(width
- srcY
)/(double)srcHeight
);
365 srcHeight
= height
- srcY
;
368 if ( srcWidth
<= 0 || srcHeight
<= 0 || dstWidth
<= 0 || dstHeight
<= 0)
372 drawPixelsScaledFlipped (g
, bgcolor
.getRed (), bgcolor
.getGreen (),
381 drawPixelsScaledFlipped (g
, 0, 0, 0, flipX
, flipY
,
382 srcX
, srcY
, srcWidth
, srcHeight
,
383 dstX
, dstY
, dstWidth
, dstHeight
,
388 public native void copyArea(int x
, int y
, int width
, int height
,
391 //******************** VolatileImage stuff ********************
393 public boolean contentsLost()
398 public Graphics2D
createGraphics()
400 QtImageGraphics g
= new QtImageGraphics(this);
405 public ImageCapabilities
getCapabilities()
407 return new ImageCapabilities(false)
409 public boolean isTrueVolatile()
416 public int getHeight()
421 public BufferedImage
getSnapshot()
423 BufferedImage bi
= new BufferedImage(width
, height
,
424 BufferedImage
.TYPE_INT_ARGB_PRE
);
425 bi
.setRGB( 0, 0, width
, height
, getPixels(), 0, width
);
429 public int getWidth()
434 public int validate(GraphicsConfiguration gc
)