| 1 |
774 |
jeremybenn |
/* qtimage.cpp --
|
| 2 |
|
|
Copyright (C) 2005 Free Software Foundation, Inc.
|
| 3 |
|
|
|
| 4 |
|
|
This file is part of GNU Classpath.
|
| 5 |
|
|
|
| 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.
|
| 10 |
|
|
|
| 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.
|
| 15 |
|
|
|
| 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.
|
| 20 |
|
|
|
| 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.
|
| 25 |
|
|
|
| 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. */
|
| 37 |
|
|
|
| 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"
|
| 48 |
|
|
|
| 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
|
| 55 |
|
|
|
| 56 |
|
|
QImage *getQtImage( JNIEnv *env, jobject obj )
|
| 57 |
|
|
{
|
| 58 |
|
|
jclass cls = env->GetObjectClass( obj );
|
| 59 |
|
|
jfieldID field = env->GetFieldID( cls, "nativeObject", "J" );
|
| 60 |
|
|
return (QImage *)env->GetLongField( obj, field );
|
| 61 |
|
|
}
|
| 62 |
|
|
|
| 63 |
|
|
static void setNativePtr( JNIEnv *env, jobject obj, void *value )
|
| 64 |
|
|
{
|
| 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 );
|
| 69 |
|
|
}
|
| 70 |
|
|
|
| 71 |
|
|
/*
|
| 72 |
|
|
* Creates a QImage.
|
| 73 |
|
|
*/
|
| 74 |
|
|
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtImage_createImage
|
| 75 |
|
|
(JNIEnv *env, jobject obj)
|
| 76 |
|
|
{
|
| 77 |
|
|
int width, height;
|
| 78 |
|
|
jclass cls;
|
| 79 |
|
|
jfieldID field;
|
| 80 |
|
|
|
| 81 |
|
|
cls = env->GetObjectClass( obj );
|
| 82 |
|
|
field = env->GetFieldID (cls, "width", "I");
|
| 83 |
|
|
assert (field != 0);
|
| 84 |
|
|
width = env->GetIntField(obj, field);
|
| 85 |
|
|
|
| 86 |
|
|
field = env->GetFieldID(cls, "height", "I");
|
| 87 |
|
|
assert (field != 0);
|
| 88 |
|
|
height = env->GetIntField(obj, field);
|
| 89 |
|
|
|
| 90 |
|
|
QImage *image = new QImage ( width, height,
|
| 91 |
|
|
QImage::Format_ARGB32_Premultiplied );
|
| 92 |
|
|
setNativePtr(env, obj, image);
|
| 93 |
|
|
}
|
| 94 |
|
|
|
| 95 |
|
|
/*
|
| 96 |
|
|
* Frees the image data.
|
| 97 |
|
|
*/
|
| 98 |
|
|
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtImage_freeImage
|
| 99 |
|
|
(JNIEnv *env, jobject obj)
|
| 100 |
|
|
{
|
| 101 |
|
|
QImage *image = getQtImage(env, obj);
|
| 102 |
|
|
setNativePtr(env, obj, NULL);
|
| 103 |
|
|
if ( image )
|
| 104 |
|
|
delete image;
|
| 105 |
|
|
}
|
| 106 |
|
|
|
| 107 |
|
|
/*
|
| 108 |
|
|
* Clears the image to zero.
|
| 109 |
|
|
*/
|
| 110 |
|
|
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtImage_clear
|
| 111 |
|
|
(JNIEnv *env, jobject obj)
|
| 112 |
|
|
{
|
| 113 |
|
|
QImage *image = getQtImage(env, obj);
|
| 114 |
|
|
assert( image );
|
| 115 |
|
|
image->fill(0);
|
| 116 |
|
|
}
|
| 117 |
|
|
|
| 118 |
|
|
/*
|
| 119 |
|
|
* Returns the pixel data in an int array.
|
| 120 |
|
|
*/
|
| 121 |
|
|
JNIEXPORT jintArray JNICALL Java_gnu_java_awt_peer_qt_QtImage_getPixels
|
| 122 |
|
|
(JNIEnv *env, jobject obj)
|
| 123 |
|
|
{
|
| 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;
|
| 130 |
|
|
|
| 131 |
|
|
assert( image );
|
| 132 |
|
|
|
| 133 |
|
|
result_array = env->NewIntArray (image->width() * image->height());
|
| 134 |
|
|
dst = result_array_ptr =
|
| 135 |
|
|
env->GetIntArrayElements(result_array, NULL);
|
| 136 |
|
|
|
| 137 |
|
|
// A bit inefficient.
|
| 138 |
|
|
for ( y = 0; y < image->height(); y++)
|
| 139 |
|
|
for ( x = 0; x < image->width(); x++)
|
| 140 |
|
|
{
|
| 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++;
|
| 149 |
|
|
}
|
| 150 |
|
|
|
| 151 |
|
|
env->ReleaseIntArrayElements (result_array, result_array_ptr, 0);
|
| 152 |
|
|
return result_array;
|
| 153 |
|
|
}
|
| 154 |
|
|
|
| 155 |
|
|
/*
|
| 156 |
|
|
* Sets the pixel data.
|
| 157 |
|
|
*/
|
| 158 |
|
|
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtImage_setPixels
|
| 159 |
|
|
(JNIEnv *env, jobject obj, jintArray pixels)
|
| 160 |
|
|
{
|
| 161 |
|
|
QImage *image = getQtImage(env, obj);
|
| 162 |
|
|
assert( image );
|
| 163 |
|
|
|
| 164 |
|
|
int width, height;
|
| 165 |
|
|
jint *src_array, *src;
|
| 166 |
|
|
|
| 167 |
|
|
width = image->width();
|
| 168 |
|
|
height = image->height();
|
| 169 |
|
|
|
| 170 |
|
|
src = src_array =
|
| 171 |
|
|
env->GetIntArrayElements(pixels, NULL);
|
| 172 |
|
|
|
| 173 |
|
|
for(int i = 0 ; i < height; i++)
|
| 174 |
|
|
{
|
| 175 |
|
|
uchar *scanline = image->scanLine( i );
|
| 176 |
|
|
memcpy((void *)scanline, (void *)src, width * 4);
|
| 177 |
|
|
src += width;
|
| 178 |
|
|
}
|
| 179 |
|
|
|
| 180 |
|
|
env->ReleaseIntArrayElements(pixels, src_array, 0);
|
| 181 |
|
|
}
|
| 182 |
|
|
|
| 183 |
|
|
|
| 184 |
|
|
/*
|
| 185 |
|
|
* Loads an image from a file,
|
| 186 |
|
|
* returns true on success, false on failure.
|
| 187 |
|
|
*/
|
| 188 |
|
|
JNIEXPORT jboolean JNICALL Java_gnu_java_awt_peer_qt_QtImage_loadImage
|
| 189 |
|
|
(JNIEnv *env, jobject obj, jstring fn)
|
| 190 |
|
|
{
|
| 191 |
|
|
QString *filename = getQString(env, fn);
|
| 192 |
|
|
|
| 193 |
|
|
QImage *image = new QImage();
|
| 194 |
|
|
bool retVal = image->load( *filename );
|
| 195 |
|
|
delete filename;
|
| 196 |
|
|
|
| 197 |
|
|
if(image->isNull() && !retVal)
|
| 198 |
|
|
{
|
| 199 |
|
|
setNativePtr(env, obj, NULL);
|
| 200 |
|
|
return JNI_FALSE;
|
| 201 |
|
|
}
|
| 202 |
|
|
|
| 203 |
|
|
setNativePtr(env, obj, image);
|
| 204 |
|
|
|
| 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() );
|
| 210 |
|
|
|
| 211 |
|
|
return JNI_TRUE;
|
| 212 |
|
|
}
|
| 213 |
|
|
|
| 214 |
|
|
/*
|
| 215 |
|
|
* Creates the image from an array of java bytes.
|
| 216 |
|
|
*/
|
| 217 |
|
|
JNIEXPORT jboolean JNICALL Java_gnu_java_awt_peer_qt_QtImage_loadImageFromData
|
| 218 |
|
|
(JNIEnv *env, jobject obj, jbyteArray data)
|
| 219 |
|
|
{
|
| 220 |
|
|
jbyte *src_array, *src;
|
| 221 |
|
|
bool retVal;
|
| 222 |
|
|
|
| 223 |
|
|
src = env->GetByteArrayElements(data, NULL);
|
| 224 |
|
|
int len = env->GetArrayLength( data );
|
| 225 |
|
|
|
| 226 |
|
|
QImage *image = new QImage();
|
| 227 |
|
|
retVal = image->loadFromData( (uchar *) src, len);
|
| 228 |
|
|
env->ReleaseByteArrayElements(data, src, 0);
|
| 229 |
|
|
|
| 230 |
|
|
if(image->isNull() || retVal == false)
|
| 231 |
|
|
{
|
| 232 |
|
|
setNativePtr(env, obj, NULL);
|
| 233 |
|
|
return JNI_FALSE;
|
| 234 |
|
|
}
|
| 235 |
|
|
|
| 236 |
|
|
setNativePtr(env, obj, image);
|
| 237 |
|
|
|
| 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() );
|
| 243 |
|
|
|
| 244 |
|
|
return JNI_TRUE;
|
| 245 |
|
|
}
|
| 246 |
|
|
|
| 247 |
|
|
|
| 248 |
|
|
/*
|
| 249 |
|
|
*/
|
| 250 |
|
|
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtImage_createScaledImage
|
| 251 |
|
|
(JNIEnv *env, jobject obj, jobject src, jint hints)
|
| 252 |
|
|
{
|
| 253 |
|
|
int w,h;
|
| 254 |
|
|
jclass cls;
|
| 255 |
|
|
jfieldID field;
|
| 256 |
|
|
|
| 257 |
|
|
cls = env->GetObjectClass( obj );
|
| 258 |
|
|
field = env->GetFieldID(cls, "width", "I");
|
| 259 |
|
|
assert (field != 0);
|
| 260 |
|
|
w = env->GetIntField(obj, field);
|
| 261 |
|
|
|
| 262 |
|
|
field = env->GetFieldID(cls, "height", "I");
|
| 263 |
|
|
assert (field != 0);
|
| 264 |
|
|
h = env->GetIntField(obj, field);
|
| 265 |
|
|
|
| 266 |
|
|
QImage *image = getQtImage(env, src);
|
| 267 |
|
|
assert( image );
|
| 268 |
|
|
QImage imageScaled;
|
| 269 |
|
|
|
| 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 );
|
| 279 |
|
|
|
| 280 |
|
|
// create new QtImage object
|
| 281 |
|
|
setNativePtr( env, obj, scaledPtr );
|
| 282 |
|
|
}
|
| 283 |
|
|
|
| 284 |
|
|
/*
|
| 285 |
|
|
* Simple draw without scaling.
|
| 286 |
|
|
*/
|
| 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)
|
| 290 |
|
|
{
|
| 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 );
|
| 299 |
|
|
}
|
| 300 |
|
|
|
| 301 |
|
|
/*
|
| 302 |
|
|
* Draw the image with scaling.
|
| 303 |
|
|
*/
|
| 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)
|
| 308 |
|
|
{
|
| 309 |
|
|
QImage *image = getQtImage(env, obj);
|
| 310 |
|
|
assert( image );
|
| 311 |
|
|
QPainter *painter = getPainter( env, graphics );
|
| 312 |
|
|
assert( painter );
|
| 313 |
|
|
|
| 314 |
|
|
if(composite == JNI_TRUE)
|
| 315 |
|
|
painter->fillRect ( x, y, w, h, QColor(bg_red, bg_green, bg_blue ) );
|
| 316 |
|
|
|
| 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);
|
| 321 |
|
|
|
| 322 |
|
|
if(composite == JNI_TRUE)
|
| 323 |
|
|
painter->fillRect( *dstRect, QColor(bg_red, bg_green, bg_blue ) );
|
| 324 |
|
|
|
| 325 |
|
|
painter->drawImage( *dstRect, *image, *srcRect);
|
| 326 |
|
|
|
| 327 |
|
|
delete srcRect;
|
| 328 |
|
|
delete dstRect;
|
| 329 |
|
|
}
|
| 330 |
|
|
|
| 331 |
|
|
/*
|
| 332 |
|
|
* Draws a transformed image.
|
| 333 |
|
|
*/
|
| 334 |
|
|
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtImage_drawPixelsTransformed
|
| 335 |
|
|
(JNIEnv *env, jobject obj, jobject graphics, jobject transform)
|
| 336 |
|
|
{
|
| 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 );
|
| 343 |
|
|
|
| 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);
|
| 348 |
|
|
}
|
| 349 |
|
|
|
| 350 |
|
|
/**
|
| 351 |
|
|
* Draws the pixbuf at x, y, scaled to width and height and
|
| 352 |
|
|
* optionally composited and/or flipped with a given background color.
|
| 353 |
|
|
*/
|
| 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)
|
| 362 |
|
|
{
|
| 363 |
|
|
QImage *originalImage = getQtImage(env, obj);
|
| 364 |
|
|
assert( originalImage );
|
| 365 |
|
|
QPainter *painter = getPainter( env, graphics );
|
| 366 |
|
|
assert( painter );
|
| 367 |
|
|
|
| 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);
|
| 372 |
|
|
|
| 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;
|
| 379 |
|
|
|
| 380 |
|
|
if(composite == JNI_TRUE)
|
| 381 |
|
|
painter->fillRect( *dstRect, QColor(bg_red, bg_green, bg_blue ) );
|
| 382 |
|
|
|
| 383 |
|
|
painter->drawImage( *dstRect, image, *srcRect);
|
| 384 |
|
|
|
| 385 |
|
|
delete srcRect;
|
| 386 |
|
|
delete dstRect;
|
| 387 |
|
|
}
|
| 388 |
|
|
|
| 389 |
|
|
/**
|
| 390 |
|
|
* Copies an area of the image (used by Graphics)
|
| 391 |
|
|
*/
|
| 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)
|
| 394 |
|
|
{
|
| 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;
|
| 401 |
|
|
}
|