qtimage.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /* qtimage.cpp --
  2. Copyright (C) 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. #include <assert.h>
  32. #include <QImage>
  33. #include <QColor>
  34. #include <QMatrix>
  35. #include <QPainter>
  36. #include <gnu_java_awt_peer_qt_QtImage.h>
  37. #include "qtimage.h"
  38. #include "qtstrings.h"
  39. #include "qtgraphics.h"
  40. #include "nativewrapper.h"
  41. /* The constant fields in java.awt.Image */
  42. #define SCALE_DEFAULT 1
  43. #define SCALE_FAST 2
  44. #define SCALE_SMOOTH 4
  45. #define SCALE_REPLICATE 8
  46. #define SCALE_AREA_AVERAGING 16
  47. QImage *getQtImage( JNIEnv *env, jobject obj )
  48. {
  49. jclass cls = env->GetObjectClass( obj );
  50. jfieldID field = env->GetFieldID( cls, "nativeObject", "J" );
  51. return (QImage *)env->GetLongField( obj, field );
  52. }
  53. static void setNativePtr( JNIEnv *env, jobject obj, void *value )
  54. {
  55. jlong longValue = (jlong) value;
  56. jclass cls = env->GetObjectClass( obj );
  57. jfieldID field = env->GetFieldID( cls, "nativeObject", "J" );
  58. env->SetLongField( obj, field, longValue );
  59. }
  60. /*
  61. * Creates a QImage.
  62. */
  63. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtImage_createImage
  64. (JNIEnv *env, jobject obj)
  65. {
  66. int width, height;
  67. jclass cls;
  68. jfieldID field;
  69. cls = env->GetObjectClass( obj );
  70. field = env->GetFieldID (cls, "width", "I");
  71. assert (field != 0);
  72. width = env->GetIntField(obj, field);
  73. field = env->GetFieldID(cls, "height", "I");
  74. assert (field != 0);
  75. height = env->GetIntField(obj, field);
  76. QImage *image = new QImage ( width, height,
  77. QImage::Format_ARGB32_Premultiplied );
  78. setNativePtr(env, obj, image);
  79. }
  80. /*
  81. * Frees the image data.
  82. */
  83. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtImage_freeImage
  84. (JNIEnv *env, jobject obj)
  85. {
  86. QImage *image = getQtImage(env, obj);
  87. setNativePtr(env, obj, NULL);
  88. if ( image )
  89. delete image;
  90. }
  91. /*
  92. * Clears the image to zero.
  93. */
  94. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtImage_clear
  95. (JNIEnv *env, jobject obj)
  96. {
  97. QImage *image = getQtImage(env, obj);
  98. assert( image );
  99. image->fill(0);
  100. }
  101. /*
  102. * Returns the pixel data in an int array.
  103. */
  104. JNIEXPORT jintArray JNICALL Java_gnu_java_awt_peer_qt_QtImage_getPixels
  105. (JNIEnv *env, jobject obj)
  106. {
  107. QImage *image = getQtImage(env, obj);
  108. jintArray result_array;
  109. jint *result_array_ptr, *dst;
  110. int x, y;
  111. jint pixel;
  112. QRgb current;
  113. assert( image );
  114. result_array = env->NewIntArray (image->width() * image->height());
  115. dst = result_array_ptr =
  116. env->GetIntArrayElements(result_array, NULL);
  117. // A bit inefficient.
  118. for ( y = 0; y < image->height(); y++)
  119. for ( x = 0; x < image->width(); x++)
  120. {
  121. current = image->pixel(x, y);
  122. pixel = 0;
  123. pixel = (qAlpha(current) & 0xFF) << 24 |
  124. (qRed(current) & 0xFF) << 16 |
  125. (qGreen(current) & 0xFF) << 8 |
  126. (qBlue(current) & 0xFF);
  127. *dst = pixel;
  128. dst++;
  129. }
  130. env->ReleaseIntArrayElements (result_array, result_array_ptr, 0);
  131. return result_array;
  132. }
  133. /*
  134. * Sets the pixel data.
  135. */
  136. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtImage_setPixels
  137. (JNIEnv *env, jobject obj, jintArray pixels)
  138. {
  139. QImage *image = getQtImage(env, obj);
  140. assert( image );
  141. int width, height;
  142. jint *src_array, *src;
  143. width = image->width();
  144. height = image->height();
  145. src = src_array =
  146. env->GetIntArrayElements(pixels, NULL);
  147. for(int i = 0 ; i < height; i++)
  148. {
  149. uchar *scanline = image->scanLine( i );
  150. memcpy((void *)scanline, (void *)src, width * 4);
  151. src += width;
  152. }
  153. env->ReleaseIntArrayElements(pixels, src_array, 0);
  154. }
  155. /*
  156. * Loads an image from a file,
  157. * returns true on success, false on failure.
  158. */
  159. JNIEXPORT jboolean JNICALL Java_gnu_java_awt_peer_qt_QtImage_loadImage
  160. (JNIEnv *env, jobject obj, jstring fn)
  161. {
  162. QString *filename = getQString(env, fn);
  163. QImage *image = new QImage();
  164. bool retVal = image->load( *filename );
  165. delete filename;
  166. if(image->isNull() && !retVal)
  167. {
  168. setNativePtr(env, obj, NULL);
  169. return JNI_FALSE;
  170. }
  171. setNativePtr(env, obj, image);
  172. jclass cls = env->GetObjectClass( obj );
  173. jfieldID field = env->GetFieldID( cls, "width", "I" );
  174. env->SetIntField( obj, field, image->width() );
  175. field = env->GetFieldID( cls, "height", "I" );
  176. env->SetIntField( obj, field, image->height() );
  177. return JNI_TRUE;
  178. }
  179. /*
  180. * Creates the image from an array of java bytes.
  181. */
  182. JNIEXPORT jboolean JNICALL Java_gnu_java_awt_peer_qt_QtImage_loadImageFromData
  183. (JNIEnv *env, jobject obj, jbyteArray data)
  184. {
  185. jbyte *src_array, *src;
  186. bool retVal;
  187. src = env->GetByteArrayElements(data, NULL);
  188. int len = env->GetArrayLength( data );
  189. QImage *image = new QImage();
  190. retVal = image->loadFromData( (uchar *) src, len);
  191. env->ReleaseByteArrayElements(data, src, 0);
  192. if(image->isNull() || retVal == false)
  193. {
  194. setNativePtr(env, obj, NULL);
  195. return JNI_FALSE;
  196. }
  197. setNativePtr(env, obj, image);
  198. jclass cls = env->GetObjectClass( obj );
  199. jfieldID field = env->GetFieldID( cls, "width", "I" );
  200. env->SetIntField( obj, field, image->width() );
  201. field = env->GetFieldID( cls, "height", "I" );
  202. env->SetIntField( obj, field, image->height() );
  203. return JNI_TRUE;
  204. }
  205. /*
  206. */
  207. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtImage_createScaledImage
  208. (JNIEnv *env, jobject obj, jobject src, jint hints)
  209. {
  210. int w,h;
  211. jclass cls;
  212. jfieldID field;
  213. cls = env->GetObjectClass( obj );
  214. field = env->GetFieldID(cls, "width", "I");
  215. assert (field != 0);
  216. w = env->GetIntField(obj, field);
  217. field = env->GetFieldID(cls, "height", "I");
  218. assert (field != 0);
  219. h = env->GetIntField(obj, field);
  220. QImage *image = getQtImage(env, src);
  221. assert( image );
  222. QImage imageScaled;
  223. if (hints == SCALE_SMOOTH || hints == SCALE_AREA_AVERAGING)
  224. imageScaled = image->scaled(w, h,
  225. Qt::IgnoreAspectRatio,
  226. Qt::SmoothTransformation);
  227. else
  228. imageScaled = image->scaled(w, h,
  229. Qt::IgnoreAspectRatio,
  230. Qt::FastTransformation);
  231. QImage *scaledPtr = new QImage( imageScaled );
  232. // create new QtImage object
  233. setNativePtr( env, obj, scaledPtr );
  234. }
  235. /*
  236. * Simple draw without scaling.
  237. */
  238. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtImage_drawPixels
  239. (JNIEnv *env, jobject obj, jobject graphics, jint bg_red, jint bg_green,
  240. jint bg_blue, jint x, jint y, jboolean composite)
  241. {
  242. QImage *image = getQtImage(env, obj);
  243. assert( image );
  244. QPainter *painter = getPainter( env, graphics );
  245. assert( painter );
  246. if(composite == JNI_TRUE)
  247. painter->fillRect ( x, y, image->width(), image->height(),
  248. QColor(bg_red, bg_green, bg_blue ) );
  249. painter->drawImage ( QPoint(x, y), *image );
  250. }
  251. /*
  252. * Draw the image with scaling.
  253. */
  254. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtImage_drawPixelsScaled
  255. (JNIEnv *env, jobject obj, jobject graphics,
  256. jint bg_red, jint bg_green, jint bg_blue,
  257. jint x, jint y, jint w, jint h, jboolean composite)
  258. {
  259. QImage *image = getQtImage(env, obj);
  260. assert( image );
  261. QPainter *painter = getPainter( env, graphics );
  262. assert( painter );
  263. if(composite == JNI_TRUE)
  264. painter->fillRect ( x, y, w, h, QColor(bg_red, bg_green, bg_blue ) );
  265. QRectF *srcRect = new QRectF((qreal)0, (qreal)0,
  266. (qreal)image->width(), (qreal)image->height());
  267. QRectF *dstRect = new QRectF((qreal)x, (qreal)y,
  268. (qreal)w, (qreal)h);
  269. if(composite == JNI_TRUE)
  270. painter->fillRect( *dstRect, QColor(bg_red, bg_green, bg_blue ) );
  271. painter->drawImage( *dstRect, *image, *srcRect);
  272. delete srcRect;
  273. delete dstRect;
  274. }
  275. /*
  276. * Draws a transformed image.
  277. */
  278. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtImage_drawPixelsTransformed
  279. (JNIEnv *env, jobject obj, jobject graphics, jobject transform)
  280. {
  281. QImage *originalImage = getQtImage(env, obj);
  282. assert( originalImage );
  283. QPainter *painter = getPainter( env, graphics );
  284. assert( painter );
  285. QMatrix *matrix = (QMatrix *)getNativeObject(env, transform);
  286. assert( matrix );
  287. // FIXME : Add rendering hint support here.
  288. QPoint p = matrix->map( QPoint(0,0) );
  289. QImage image = originalImage->transformed ( *matrix, Qt::FastTransformation );
  290. painter->drawImage(p, image);
  291. }
  292. /**
  293. * Draws the pixbuf at x, y, scaled to width and height and
  294. * optionally composited and/or flipped with a given background color.
  295. */
  296. JNIEXPORT void JNICALL
  297. Java_gnu_java_awt_peer_qt_QtImage_drawPixelsScaledFlipped
  298. (JNIEnv *env, jobject obj, jobject graphics,
  299. jint bg_red, jint bg_green, jint bg_blue,
  300. jboolean flipx, jboolean flipy,
  301. jint srcx, jint srcy, jint srcwidth, jint srcheight,
  302. jint dstx, jint dsty, jint dstwidth, jint dstheight,
  303. jboolean composite)
  304. {
  305. QImage *originalImage = getQtImage(env, obj);
  306. assert( originalImage );
  307. QPainter *painter = getPainter( env, graphics );
  308. assert( painter );
  309. QRectF *srcRect = new QRectF((qreal)srcx, (qreal)srcy,
  310. (qreal)srcwidth, (qreal)srcheight);
  311. QRectF *dstRect = new QRectF((qreal)dstx, (qreal)dsty,
  312. (qreal)dstwidth, (qreal)dstheight);
  313. QImage image;
  314. if( flipx == JNI_TRUE || flipy == JNI_TRUE)
  315. image = originalImage->mirrored ( (flipx == JNI_TRUE),
  316. (flipy == JNI_TRUE) );
  317. else
  318. image = *originalImage;
  319. if(composite == JNI_TRUE)
  320. painter->fillRect( *dstRect, QColor(bg_red, bg_green, bg_blue ) );
  321. painter->drawImage( *dstRect, image, *srcRect);
  322. delete srcRect;
  323. delete dstRect;
  324. }
  325. /**
  326. * Copies an area of the image (used by Graphics)
  327. */
  328. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtImage_copyArea
  329. (JNIEnv *env, jobject obj , jint x, jint y, jint w, jint h, jint dx, jint dy)
  330. {
  331. QImage *image = getQtImage(env, obj);
  332. assert( image );
  333. QImage area = image->copy(x, y, w, h);
  334. QPainter *p = new QPainter( image );
  335. p->drawImage( x + dx, y + dy, area );
  336. delete p;
  337. }