1 /* qtvolatileimage.cpp --
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. */
44 #include <gnu_java_awt_peer_qt_QtVolatileImage.h>
46 #include "qtstrings.h"
47 #include "qtgraphics.h"
48 #include "nativewrapper.h"
50 /* The constant fields in java.awt.Image */
51 #define SCALE_DEFAULT 1
53 #define SCALE_SMOOTH 4
54 #define SCALE_REPLICATE 8
55 #define SCALE_AREA_AVERAGING 16
57 QPixmap
*getQtVolatileImage( JNIEnv
*env
, jobject obj
)
59 jclass cls
= env
->GetObjectClass( obj
);
60 jfieldID field
= env
->GetFieldID( cls
, "nativeObject", "J" );
61 return (QPixmap
*)env
->GetLongField( obj
, field
);
64 static void setNativePtr( JNIEnv
*env
, jobject obj
, void *value
)
66 jlong longValue
= (jlong
) value
;
67 jclass cls
= env
->GetObjectClass( obj
);
68 jfieldID field
= env
->GetFieldID( cls
, "nativeObject", "J" );
69 env
->SetLongField( obj
, field
, longValue
);
73 * Clears the image to zero.
75 JNIEXPORT
void JNICALL Java_gnu_java_awt_peer_qt_QtVolatileImage_clear
76 (JNIEnv
*env
, jobject obj
)
78 QPixmap
*image
= getQtVolatileImage(env
, obj
);
84 * Returns the pixel data in an int array.
86 JNIEXPORT jintArray JNICALL Java_gnu_java_awt_peer_qt_QtVolatileImage_getPixels
87 (JNIEnv
*env
, jobject obj
)
89 QPixmap
*image
= getQtVolatileImage(env
, obj
);
90 jintArray result_array
;
91 jint
*result_array_ptr
, *dst
;
97 QImage im
= image
->toImage();
99 result_array
= env
->NewIntArray (image
->width() * image
->height());
100 dst
= result_array_ptr
=
101 env
->GetIntArrayElements(result_array
, NULL
);
103 // A bit inefficient.
104 for ( y
= 0; y
< image
->height(); y
++)
105 for ( x
= 0; x
< image
->width(); x
++)
107 current
= im
.pixel(x
, y
);
109 pixel
= (qAlpha(current
) & 0xFF) << 24 |
110 (qRed(current
) & 0xFF) << 16 |
111 (qGreen(current
) & 0xFF) << 8 |
112 (qBlue(current
) & 0xFF);
117 env
->ReleaseIntArrayElements (result_array
, result_array_ptr
, 0);
124 JNIEXPORT
void JNICALL Java_gnu_java_awt_peer_qt_QtVolatileImage_createImage
125 (JNIEnv
*env
, jobject obj
)
131 cls
= env
->GetObjectClass( obj
);
132 field
= env
->GetFieldID (cls
, "width", "I");
134 width
= env
->GetIntField(obj
, field
);
136 field
= env
->GetFieldID(cls
, "height", "I");
138 height
= env
->GetIntField(obj
, field
);
140 QPixmap
*image
= new QPixmap ( width
, height
);
141 setNativePtr(env
, obj
, image
);
145 * Frees the image data.
147 JNIEXPORT
void JNICALL Java_gnu_java_awt_peer_qt_QtVolatileImage_freeImage
148 (JNIEnv
*env
, jobject obj
)
150 QPixmap
*image
= getQtVolatileImage(env
, obj
);
153 setNativePtr(env
, obj
, NULL
);
159 JNIEXPORT
void JNICALL Java_gnu_java_awt_peer_qt_QtVolatileImage_blit__Lgnu_java_awt_peer_qt_QtImage_2
160 (JNIEnv
*env
, jobject obj
, jobject i2
)
162 QPixmap
*image
= getQtVolatileImage(env
, obj
);
165 QImage
*blit
= getQtImage(env
, i2
);
168 QPainter
*p
= new QPainter( image
);
170 p
->drawImage( 0, 0, *blit
);
178 JNIEXPORT
void JNICALL Java_gnu_java_awt_peer_qt_QtVolatileImage_blit__Lgnu_java_awt_peer_qt_QtImage_2IIII
179 (JNIEnv
*env
, jobject obj
, jobject i2
, jint x
, jint y
, jint w
, jint h
)
181 QPixmap
*image
= getQtVolatileImage(env
, obj
);
184 QImage
*blit
= getQtImage(env
, i2
);
187 QPainter
*p
= new QPainter( image
);
189 p
->drawImage( x
, y
, *blit
, x
, y
, w
, h
);
195 * Creates a scaled version.
197 JNIEXPORT
void JNICALL Java_gnu_java_awt_peer_qt_QtVolatileImage_createScaledImage
198 (JNIEnv
*env
, jobject obj
, jobject src
, jint hints
)
204 cls
= env
->GetObjectClass( obj
);
205 field
= env
->GetFieldID(cls
, "width", "I");
207 w
= env
->GetIntField(obj
, field
);
209 field
= env
->GetFieldID(cls
, "height", "I");
211 h
= env
->GetIntField(obj
, field
);
213 QPixmap
*ip
= getQtVolatileImage(env
, src
);
215 QImage image
= ip
->toImage();
218 if (hints
== SCALE_SMOOTH
|| hints
== SCALE_AREA_AVERAGING
)
219 imageScaled
= image
.scaled(w
, h
,
220 Qt::IgnoreAspectRatio
,
221 Qt::SmoothTransformation
);
223 imageScaled
= image
.scaled(w
, h
,
224 Qt::IgnoreAspectRatio
,
225 Qt::FastTransformation
);
226 QImage
*scaledPtr
= new QImage( imageScaled
);
228 // create new QtImage object
229 setNativePtr( env
, obj
, scaledPtr
);
235 JNIEXPORT
void JNICALL Java_gnu_java_awt_peer_qt_QtVolatileImage_drawPixels
236 (JNIEnv
*env
, jobject obj
, jobject graphics
, jint bg_red
, jint bg_green
,
237 jint bg_blue
, jint x
, jint y
, jboolean composite
)
239 QPixmap
*image
= getQtVolatileImage(env
, obj
);
241 QPainter
*painter
= getPainter( env
, graphics
);
244 if(composite
== JNI_TRUE
)
245 painter
->fillRect ( x
, y
, image
->width(), image
->height(),
246 QColor(bg_red
, bg_green
, bg_blue
) );
247 painter
->drawPixmap ( QPoint(x
, y
), *image
);
253 JNIEXPORT
void JNICALL Java_gnu_java_awt_peer_qt_QtVolatileImage_drawPixelsScaled
254 (JNIEnv
*env
, jobject obj
, jobject graphics
,
255 jint bg_red
, jint bg_green
, jint bg_blue
,
256 jint x
, jint y
, jint w
, jint h
, jboolean composite
)
258 QPixmap
*image
= getQtVolatileImage(env
, obj
);
260 QPainter
*painter
= getPainter( env
, graphics
);
263 if(composite
== JNI_TRUE
)
264 painter
->fillRect ( x
, y
, w
, h
, QColor(bg_red
, bg_green
, bg_blue
) );
266 QRectF
*srcRect
= new QRectF((qreal
)0, (qreal
)0,
267 (qreal
)image
->width(), (qreal
)image
->height());
268 QRectF
*dstRect
= new QRectF((qreal
)x
, (qreal
)y
,
271 if(composite
== JNI_TRUE
)
272 painter
->fillRect( *dstRect
, QColor(bg_red
, bg_green
, bg_blue
) );
274 painter
->drawPixmap( *dstRect
, *image
, *srcRect
);
281 * Draw pixels transformed.
283 JNIEXPORT
void JNICALL Java_gnu_java_awt_peer_qt_QtVolatileImage_drawPixelsTransformed
284 (JNIEnv
*env
, jobject obj
, jobject graphics
, jobject transform
)
286 QPixmap
*originalImage
= getQtVolatileImage(env
, obj
);
287 assert( originalImage
);
288 QPainter
*painter
= getPainter( env
, graphics
);
290 QMatrix
*matrix
= (QMatrix
*)getNativeObject(env
, transform
);
293 // FIXME : Add rendering hint support here.
294 QPoint p
= matrix
->map( QPoint(0,0) );
295 QImage image
= originalImage
->toImage().transformed ( *matrix
, Qt::FastTransformation
);
296 painter
->drawImage(p
, image
);
301 * Draw pixels scaled and flipped
303 JNIEXPORT
void JNICALL Java_gnu_java_awt_peer_qt_QtVolatileImage_drawPixelsScaledFlipped
304 (JNIEnv
*env
, jobject obj
, jobject graphics
,
305 jint bg_red
, jint bg_green
, jint bg_blue
,
306 jboolean flipx
, jboolean flipy
,
307 jint srcx
, jint srcy
, jint srcwidth
, jint srcheight
,
308 jint dstx
, jint dsty
, jint dstwidth
, jint dstheight
,
311 QPixmap
*originalImage
= getQtVolatileImage(env
, obj
);
312 assert( originalImage
);
313 QPainter
*painter
= getPainter( env
, graphics
);
316 QRectF
*srcRect
= new QRectF((qreal
)srcx
, (qreal
)srcy
,
317 (qreal
)srcwidth
, (qreal
)srcheight
);
318 QRectF
*dstRect
= new QRectF((qreal
)dstx
, (qreal
)dsty
,
319 (qreal
)dstwidth
, (qreal
)dstheight
);
321 if(composite
== JNI_TRUE
)
322 painter
->fillRect( *dstRect
, QColor(bg_red
, bg_green
, bg_blue
) );
324 if( flipx
== JNI_TRUE
|| flipy
== JNI_TRUE
)
326 QImage im
= originalImage
->toImage().mirrored ( (flipx
== JNI_TRUE
),
327 (flipy
== JNI_TRUE
) );
328 painter
->drawImage ( *dstRect
, im
, *srcRect
);
331 painter
->drawPixmap ( *dstRect
, *originalImage
, *srcRect
);
338 * Copies an area of the image (used by Graphics)
340 JNIEXPORT
void JNICALL Java_gnu_java_awt_peer_qt_QtVolatileImage_copyArea
341 (JNIEnv
*env
, jobject obj
, jint x
, jint y
, jint w
, jint h
, jint dx
, jint dy
)
343 QPixmap
*image
= getQtVolatileImage(env
, obj
);