Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / native / jni / qt-peer / qtvolatileimage.cpp
blob9c28db6d14e2e08199fd4cb645a5f4565fc7befd
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)
9 any later version.
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
19 02110-1301 USA.
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
24 combination.
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 #include <assert.h>
39 #include <QPixmap>
40 #include <QImage>
41 #include <QColor>
42 #include <QMatrix>
43 #include <QPainter>
44 #include <gnu_java_awt_peer_qt_QtVolatileImage.h>
45 #include "qtimage.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
52 #define SCALE_FAST 2
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);
79 assert( image );
80 image->fill();
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;
92 int x, y;
93 jint pixel;
94 QRgb current;
96 assert( image );
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);
108 pixel = 0;
109 pixel = (qAlpha(current) & 0xFF) << 24 |
110 (qRed(current) & 0xFF) << 16 |
111 (qGreen(current) & 0xFF) << 8 |
112 (qBlue(current) & 0xFF);
113 *dst = pixel;
114 dst++;
117 env->ReleaseIntArrayElements (result_array, result_array_ptr, 0);
118 return result_array;
122 * Creates a QImage.
124 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtVolatileImage_createImage
125 (JNIEnv *env, jobject obj)
127 int width, height;
128 jclass cls;
129 jfieldID field;
131 cls = env->GetObjectClass( obj );
132 field = env->GetFieldID (cls, "width", "I");
133 assert (field != 0);
134 width = env->GetIntField(obj, field);
136 field = env->GetFieldID(cls, "height", "I");
137 assert (field != 0);
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);
151 if ( image )
152 delete image;
153 setNativePtr(env, obj, NULL);
157 * Blits a QImage
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);
163 assert( image );
165 QImage *blit = getQtImage(env, i2);
166 assert( blit );
168 QPainter *p = new QPainter( image );
169 assert( p );
170 p->drawImage( 0, 0, *blit );
172 delete p;
176 * Blits a QImage
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);
182 assert( image );
184 QImage *blit = getQtImage(env, i2);
185 assert( blit );
187 QPainter *p = new QPainter( image );
188 assert( p );
189 p->drawImage( x, y, *blit, x, y, w, h);
191 delete p;
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)
200 int w,h;
201 jclass cls;
202 jfieldID field;
204 cls = env->GetObjectClass( obj );
205 field = env->GetFieldID(cls, "width", "I");
206 assert (field != 0);
207 w = env->GetIntField(obj, field);
209 field = env->GetFieldID(cls, "height", "I");
210 assert (field != 0);
211 h = env->GetIntField(obj, field);
213 QPixmap *ip = getQtVolatileImage(env, src);
214 assert( ip );
215 QImage image = ip->toImage();
216 QImage imageScaled;
218 if (hints == SCALE_SMOOTH || hints == SCALE_AREA_AVERAGING)
219 imageScaled = image.scaled(w, h,
220 Qt::IgnoreAspectRatio,
221 Qt::SmoothTransformation);
222 else
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 );
233 * DrawPixels.
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);
240 assert( image );
241 QPainter *painter = getPainter( env, graphics );
242 assert( painter );
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 );
251 * DrawPixels scaled.
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);
259 assert( image );
260 QPainter *painter = getPainter( env, graphics );
261 assert( painter );
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,
269 (qreal)w, (qreal)h);
271 if(composite == JNI_TRUE)
272 painter->fillRect( *dstRect, QColor(bg_red, bg_green, bg_blue ) );
274 painter->drawPixmap( *dstRect, *image, *srcRect);
276 delete srcRect;
277 delete dstRect;
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 );
289 assert( painter );
290 QMatrix *matrix = (QMatrix *)getNativeObject(env, transform);
291 assert( matrix );
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,
309 jboolean composite)
311 QPixmap *originalImage = getQtVolatileImage(env, obj);
312 assert( originalImage );
313 QPainter *painter = getPainter( env, graphics );
314 assert( painter );
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);
330 else
331 painter->drawPixmap ( *dstRect, *originalImage, *srcRect);
333 delete srcRect;
334 delete dstRect;
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);
344 assert( image );
346 // FIXME