2007-01-17 Jack Howarth <howarth@bromo.med.uc.edu>
[official-gcc.git] / libjava / classpath / native / jni / qt-peer / qtimage.cpp
blob4d2b5b90aeb2f12d2cd211a012f6c233f709c0e4
1 /* qtimage.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 <QImage>
40 #include <QColor>
41 #include <QMatrix>
42 #include <QPainter>
43 #include <gnu_java_awt_peer_qt_QtImage.h>
44 #include "qtimage.h"
45 #include "qtstrings.h"
46 #include "qtgraphics.h"
47 #include "nativewrapper.h"
49 /* The constant fields in java.awt.Image */
50 #define SCALE_DEFAULT 1
51 #define SCALE_FAST 2
52 #define SCALE_SMOOTH 4
53 #define SCALE_REPLICATE 8
54 #define SCALE_AREA_AVERAGING 16
56 QImage *getQtImage( JNIEnv *env, jobject obj )
58 jclass cls = env->GetObjectClass( obj );
59 jfieldID field = env->GetFieldID( cls, "nativeObject", "J" );
60 return (QImage *)env->GetLongField( obj, field );
63 static void setNativePtr( JNIEnv *env, jobject obj, void *value )
65 jlong longValue = (jlong) value;
66 jclass cls = env->GetObjectClass( obj );
67 jfieldID field = env->GetFieldID( cls, "nativeObject", "J" );
68 env->SetLongField( obj, field, longValue );
72 * Creates a QImage.
74 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtImage_createImage
75 (JNIEnv *env, jobject obj)
77 int width, height;
78 jclass cls;
79 jfieldID field;
81 cls = env->GetObjectClass( obj );
82 field = env->GetFieldID (cls, "width", "I");
83 assert (field != 0);
84 width = env->GetIntField(obj, field);
86 field = env->GetFieldID(cls, "height", "I");
87 assert (field != 0);
88 height = env->GetIntField(obj, field);
90 QImage *image = new QImage ( width, height,
91 QImage::Format_ARGB32_Premultiplied );
92 setNativePtr(env, obj, image);
96 * Frees the image data.
98 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtImage_freeImage
99 (JNIEnv *env, jobject obj)
101 QImage *image = getQtImage(env, obj);
102 setNativePtr(env, obj, NULL);
103 if ( image )
104 delete image;
108 * Clears the image to zero.
110 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtImage_clear
111 (JNIEnv *env, jobject obj)
113 QImage *image = getQtImage(env, obj);
114 assert( image );
115 image->fill(0);
119 * Returns the pixel data in an int array.
121 JNIEXPORT jintArray JNICALL Java_gnu_java_awt_peer_qt_QtImage_getPixels
122 (JNIEnv *env, jobject obj)
124 QImage *image = getQtImage(env, obj);
125 jintArray result_array;
126 jint *result_array_ptr, *dst;
127 int x, y;
128 jint pixel;
129 QRgb current;
131 assert( image );
133 result_array = env->NewIntArray (image->width() * image->height());
134 dst = result_array_ptr =
135 env->GetIntArrayElements(result_array, NULL);
137 // A bit inefficient.
138 for ( y = 0; y < image->height(); y++)
139 for ( x = 0; x < image->width(); x++)
141 current = image->pixel(x, y);
142 pixel = 0;
143 pixel = (qAlpha(current) & 0xFF) << 24 |
144 (qRed(current) & 0xFF) << 16 |
145 (qGreen(current) & 0xFF) << 8 |
146 (qBlue(current) & 0xFF);
147 *dst = pixel;
148 dst++;
151 env->ReleaseIntArrayElements (result_array, result_array_ptr, 0);
152 return result_array;
156 * Sets the pixel data.
158 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtImage_setPixels
159 (JNIEnv *env, jobject obj, jintArray pixels)
161 QImage *image = getQtImage(env, obj);
162 assert( image );
164 int width, height;
165 jint *src_array, *src;
167 width = image->width();
168 height = image->height();
170 src = src_array =
171 env->GetIntArrayElements(pixels, NULL);
173 for(int i = 0 ; i < height; i++)
175 uchar *scanline = image->scanLine( i );
176 memcpy((void *)scanline, (void *)src, width * 4);
177 src += width;
180 env->ReleaseIntArrayElements(pixels, src_array, 0);
185 * Loads an image from a file,
186 * returns true on success, false on failure.
188 JNIEXPORT jboolean JNICALL Java_gnu_java_awt_peer_qt_QtImage_loadImage
189 (JNIEnv *env, jobject obj, jstring fn)
191 QString *filename = getQString(env, fn);
193 QImage *image = new QImage();
194 bool retVal = image->load( *filename );
195 delete filename;
197 if(image->isNull() && !retVal)
199 setNativePtr(env, obj, NULL);
200 return JNI_FALSE;
203 setNativePtr(env, obj, image);
205 jclass cls = env->GetObjectClass( obj );
206 jfieldID field = env->GetFieldID( cls, "width", "I" );
207 env->SetIntField( obj, field, image->width() );
208 field = env->GetFieldID( cls, "height", "I" );
209 env->SetIntField( obj, field, image->height() );
211 return JNI_TRUE;
215 * Creates the image from an array of java bytes.
217 JNIEXPORT jboolean JNICALL Java_gnu_java_awt_peer_qt_QtImage_loadImageFromData
218 (JNIEnv *env, jobject obj, jbyteArray data)
220 jbyte *src_array, *src;
221 bool retVal;
223 src = env->GetByteArrayElements(data, NULL);
224 int len = env->GetArrayLength( data );
226 QImage *image = new QImage();
227 retVal = image->loadFromData( (uchar *) src, len);
228 env->ReleaseByteArrayElements(data, src, 0);
230 if(image->isNull() || retVal == false)
232 setNativePtr(env, obj, NULL);
233 return JNI_FALSE;
236 setNativePtr(env, obj, image);
238 jclass cls = env->GetObjectClass( obj );
239 jfieldID field = env->GetFieldID( cls, "width", "I" );
240 env->SetIntField( obj, field, image->width() );
241 field = env->GetFieldID( cls, "height", "I" );
242 env->SetIntField( obj, field, image->height() );
244 return JNI_TRUE;
250 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtImage_createScaledImage
251 (JNIEnv *env, jobject obj, jobject src, jint hints)
253 int w,h;
254 jclass cls;
255 jfieldID field;
257 cls = env->GetObjectClass( obj );
258 field = env->GetFieldID(cls, "width", "I");
259 assert (field != 0);
260 w = env->GetIntField(obj, field);
262 field = env->GetFieldID(cls, "height", "I");
263 assert (field != 0);
264 h = env->GetIntField(obj, field);
266 QImage *image = getQtImage(env, src);
267 assert( image );
268 QImage imageScaled;
270 if (hints == SCALE_SMOOTH || hints == SCALE_AREA_AVERAGING)
271 imageScaled = image->scaled(w, h,
272 Qt::IgnoreAspectRatio,
273 Qt::SmoothTransformation);
274 else
275 imageScaled = image->scaled(w, h,
276 Qt::IgnoreAspectRatio,
277 Qt::FastTransformation);
278 QImage *scaledPtr = new QImage( imageScaled );
280 // create new QtImage object
281 setNativePtr( env, obj, scaledPtr );
285 * Simple draw without scaling.
287 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtImage_drawPixels
288 (JNIEnv *env, jobject obj, jobject graphics, jint bg_red, jint bg_green,
289 jint bg_blue, jint x, jint y, jboolean composite)
291 QImage *image = getQtImage(env, obj);
292 assert( image );
293 QPainter *painter = getPainter( env, graphics );
294 assert( painter );
295 if(composite == JNI_TRUE)
296 painter->fillRect ( x, y, image->width(), image->height(),
297 QColor(bg_red, bg_green, bg_blue ) );
298 painter->drawImage ( QPoint(x, y), *image );
302 * Draw the image with scaling.
304 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtImage_drawPixelsScaled
305 (JNIEnv *env, jobject obj, jobject graphics,
306 jint bg_red, jint bg_green, jint bg_blue,
307 jint x, jint y, jint w, jint h, jboolean composite)
309 QImage *image = getQtImage(env, obj);
310 assert( image );
311 QPainter *painter = getPainter( env, graphics );
312 assert( painter );
314 if(composite == JNI_TRUE)
315 painter->fillRect ( x, y, w, h, QColor(bg_red, bg_green, bg_blue ) );
317 QRectF *srcRect = new QRectF((qreal)0, (qreal)0,
318 (qreal)image->width(), (qreal)image->height());
319 QRectF *dstRect = new QRectF((qreal)x, (qreal)y,
320 (qreal)w, (qreal)h);
322 if(composite == JNI_TRUE)
323 painter->fillRect( *dstRect, QColor(bg_red, bg_green, bg_blue ) );
325 painter->drawImage( *dstRect, *image, *srcRect);
327 delete srcRect;
328 delete dstRect;
332 * Draws a transformed image.
334 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtImage_drawPixelsTransformed
335 (JNIEnv *env, jobject obj, jobject graphics, jobject transform)
337 QImage *originalImage = getQtImage(env, obj);
338 assert( originalImage );
339 QPainter *painter = getPainter( env, graphics );
340 assert( painter );
341 QMatrix *matrix = (QMatrix *)getNativeObject(env, transform);
342 assert( matrix );
344 // FIXME : Add rendering hint support here.
345 QPoint p = matrix->map( QPoint(0,0) );
346 QImage image = originalImage->transformed ( *matrix, Qt::FastTransformation );
347 painter->drawImage(p, image);
351 * Draws the pixbuf at x, y, scaled to width and height and
352 * optionally composited and/or flipped with a given background color.
354 JNIEXPORT void JNICALL
355 Java_gnu_java_awt_peer_qt_QtImage_drawPixelsScaledFlipped
356 (JNIEnv *env, jobject obj, jobject graphics,
357 jint bg_red, jint bg_green, jint bg_blue,
358 jboolean flipx, jboolean flipy,
359 jint srcx, jint srcy, jint srcwidth, jint srcheight,
360 jint dstx, jint dsty, jint dstwidth, jint dstheight,
361 jboolean composite)
363 QImage *originalImage = getQtImage(env, obj);
364 assert( originalImage );
365 QPainter *painter = getPainter( env, graphics );
366 assert( painter );
368 QRectF *srcRect = new QRectF((qreal)srcx, (qreal)srcy,
369 (qreal)srcwidth, (qreal)srcheight);
370 QRectF *dstRect = new QRectF((qreal)dstx, (qreal)dsty,
371 (qreal)dstwidth, (qreal)dstheight);
373 QImage image;
374 if( flipx == JNI_TRUE || flipy == JNI_TRUE)
375 image = originalImage->mirrored ( (flipx == JNI_TRUE),
376 (flipy == JNI_TRUE) );
377 else
378 image = *originalImage;
380 if(composite == JNI_TRUE)
381 painter->fillRect( *dstRect, QColor(bg_red, bg_green, bg_blue ) );
383 painter->drawImage( *dstRect, image, *srcRect);
385 delete srcRect;
386 delete dstRect;
390 * Copies an area of the image (used by Graphics)
392 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtImage_copyArea
393 (JNIEnv *env, jobject obj , jint x, jint y, jint w, jint h, jint dx, jint dy)
395 QImage *image = getQtImage(env, obj);
396 assert( image );
397 QImage area = image->copy(x, y, w, h);
398 QPainter *p = new QPainter( image );
399 p->drawImage( x + dx, y + dy, area );
400 delete p;