qtvolatileimage.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /* qtvolatileimage.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 <QPixmap>
  33. #include <QImage>
  34. #include <QColor>
  35. #include <QMatrix>
  36. #include <QPainter>
  37. #include <gnu_java_awt_peer_qt_QtVolatileImage.h>
  38. #include "qtimage.h"
  39. #include "qtstrings.h"
  40. #include "qtgraphics.h"
  41. #include "nativewrapper.h"
  42. /* The constant fields in java.awt.Image */
  43. #define SCALE_DEFAULT 1
  44. #define SCALE_FAST 2
  45. #define SCALE_SMOOTH 4
  46. #define SCALE_REPLICATE 8
  47. #define SCALE_AREA_AVERAGING 16
  48. QPixmap *getQtVolatileImage( JNIEnv *env, jobject obj )
  49. {
  50. jclass cls = env->GetObjectClass( obj );
  51. jfieldID field = env->GetFieldID( cls, "nativeObject", "J" );
  52. return (QPixmap *)env->GetLongField( obj, field );
  53. }
  54. static void setNativePtr( JNIEnv *env, jobject obj, void *value )
  55. {
  56. jlong longValue = (jlong) value;
  57. jclass cls = env->GetObjectClass( obj );
  58. jfieldID field = env->GetFieldID( cls, "nativeObject", "J" );
  59. env->SetLongField( obj, field, longValue );
  60. }
  61. /*
  62. * Clears the image to zero.
  63. */
  64. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtVolatileImage_clear
  65. (JNIEnv *env, jobject obj)
  66. {
  67. QPixmap *image = getQtVolatileImage(env, obj);
  68. assert( image );
  69. image->fill();
  70. }
  71. /*
  72. * Returns the pixel data in an int array.
  73. */
  74. JNIEXPORT jintArray JNICALL Java_gnu_java_awt_peer_qt_QtVolatileImage_getPixels
  75. (JNIEnv *env, jobject obj)
  76. {
  77. QPixmap *image = getQtVolatileImage(env, obj);
  78. jintArray result_array;
  79. jint *result_array_ptr, *dst;
  80. int x, y;
  81. jint pixel;
  82. QRgb current;
  83. assert( image );
  84. QImage im = image->toImage();
  85. result_array = env->NewIntArray (image->width() * image->height());
  86. dst = result_array_ptr =
  87. env->GetIntArrayElements(result_array, NULL);
  88. // A bit inefficient.
  89. for ( y = 0; y < image->height(); y++)
  90. for ( x = 0; x < image->width(); x++)
  91. {
  92. current = im.pixel(x, y);
  93. pixel = 0;
  94. pixel = (qAlpha(current) & 0xFF) << 24 |
  95. (qRed(current) & 0xFF) << 16 |
  96. (qGreen(current) & 0xFF) << 8 |
  97. (qBlue(current) & 0xFF);
  98. *dst = pixel;
  99. dst++;
  100. }
  101. env->ReleaseIntArrayElements (result_array, result_array_ptr, 0);
  102. return result_array;
  103. }
  104. /*
  105. * Creates a QImage.
  106. */
  107. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtVolatileImage_createImage
  108. (JNIEnv *env, jobject obj)
  109. {
  110. int width, height;
  111. jclass cls;
  112. jfieldID field;
  113. cls = env->GetObjectClass( obj );
  114. field = env->GetFieldID (cls, "width", "I");
  115. assert (field != 0);
  116. width = env->GetIntField(obj, field);
  117. field = env->GetFieldID(cls, "height", "I");
  118. assert (field != 0);
  119. height = env->GetIntField(obj, field);
  120. QPixmap *image = new QPixmap ( width, height );
  121. setNativePtr(env, obj, image);
  122. }
  123. /*
  124. * Frees the image data.
  125. */
  126. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtVolatileImage_freeImage
  127. (JNIEnv *env, jobject obj)
  128. {
  129. QPixmap *image = getQtVolatileImage(env, obj);
  130. if ( image )
  131. delete image;
  132. setNativePtr(env, obj, NULL);
  133. }
  134. /*
  135. * Blits a QImage
  136. */
  137. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtVolatileImage_blit__Lgnu_java_awt_peer_qt_QtImage_2
  138. (JNIEnv *env, jobject obj, jobject i2)
  139. {
  140. QPixmap *image = getQtVolatileImage(env, obj);
  141. assert( image );
  142. QImage *blit = getQtImage(env, i2);
  143. assert( blit );
  144. QPainter *p = new QPainter( image );
  145. assert( p );
  146. p->drawImage( 0, 0, *blit );
  147. delete p;
  148. }
  149. /*
  150. * Blits a QImage
  151. */
  152. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtVolatileImage_blit__Lgnu_java_awt_peer_qt_QtImage_2IIII
  153. (JNIEnv *env, jobject obj, jobject i2, jint x, jint y, jint w, jint h)
  154. {
  155. QPixmap *image = getQtVolatileImage(env, obj);
  156. assert( image );
  157. QImage *blit = getQtImage(env, i2);
  158. assert( blit );
  159. QPainter *p = new QPainter( image );
  160. assert( p );
  161. p->drawImage( x, y, *blit, x, y, w, h);
  162. delete p;
  163. }
  164. /*
  165. * Creates a scaled version.
  166. */
  167. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtVolatileImage_createScaledImage
  168. (JNIEnv *env, jobject obj, jobject src, jint hints)
  169. {
  170. int w,h;
  171. jclass cls;
  172. jfieldID field;
  173. cls = env->GetObjectClass( obj );
  174. field = env->GetFieldID(cls, "width", "I");
  175. assert (field != 0);
  176. w = env->GetIntField(obj, field);
  177. field = env->GetFieldID(cls, "height", "I");
  178. assert (field != 0);
  179. h = env->GetIntField(obj, field);
  180. QPixmap *ip = getQtVolatileImage(env, src);
  181. assert( ip );
  182. QImage image = ip->toImage();
  183. QImage imageScaled;
  184. if (hints == SCALE_SMOOTH || hints == SCALE_AREA_AVERAGING)
  185. imageScaled = image.scaled(w, h,
  186. Qt::IgnoreAspectRatio,
  187. Qt::SmoothTransformation);
  188. else
  189. imageScaled = image.scaled(w, h,
  190. Qt::IgnoreAspectRatio,
  191. Qt::FastTransformation);
  192. QImage *scaledPtr = new QImage( imageScaled );
  193. // create new QtImage object
  194. setNativePtr( env, obj, scaledPtr );
  195. }
  196. /*
  197. * DrawPixels.
  198. */
  199. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtVolatileImage_drawPixels
  200. (JNIEnv *env, jobject obj, jobject graphics, jint bg_red, jint bg_green,
  201. jint bg_blue, jint x, jint y, jboolean composite)
  202. {
  203. QPixmap *image = getQtVolatileImage(env, obj);
  204. assert( image );
  205. QPainter *painter = getPainter( env, graphics );
  206. assert( painter );
  207. if(composite == JNI_TRUE)
  208. painter->fillRect ( x, y, image->width(), image->height(),
  209. QColor(bg_red, bg_green, bg_blue ) );
  210. painter->drawPixmap ( QPoint(x, y), *image );
  211. }
  212. /*
  213. * DrawPixels scaled.
  214. */
  215. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtVolatileImage_drawPixelsScaled
  216. (JNIEnv *env, jobject obj, jobject graphics,
  217. jint bg_red, jint bg_green, jint bg_blue,
  218. jint x, jint y, jint w, jint h, jboolean composite)
  219. {
  220. QPixmap *image = getQtVolatileImage(env, obj);
  221. assert( image );
  222. QPainter *painter = getPainter( env, graphics );
  223. assert( painter );
  224. if(composite == JNI_TRUE)
  225. painter->fillRect ( x, y, w, h, QColor(bg_red, bg_green, bg_blue ) );
  226. QRectF *srcRect = new QRectF((qreal)0, (qreal)0,
  227. (qreal)image->width(), (qreal)image->height());
  228. QRectF *dstRect = new QRectF((qreal)x, (qreal)y,
  229. (qreal)w, (qreal)h);
  230. if(composite == JNI_TRUE)
  231. painter->fillRect( *dstRect, QColor(bg_red, bg_green, bg_blue ) );
  232. painter->drawPixmap( *dstRect, *image, *srcRect);
  233. delete srcRect;
  234. delete dstRect;
  235. }
  236. /*
  237. * Draw pixels transformed.
  238. */
  239. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtVolatileImage_drawPixelsTransformed
  240. (JNIEnv *env, jobject obj, jobject graphics, jobject transform)
  241. {
  242. QPixmap *originalImage = getQtVolatileImage(env, obj);
  243. assert( originalImage );
  244. QPainter *painter = getPainter( env, graphics );
  245. assert( painter );
  246. QMatrix *matrix = (QMatrix *)getNativeObject(env, transform);
  247. assert( matrix );
  248. // FIXME : Add rendering hint support here.
  249. QPoint p = matrix->map( QPoint(0,0) );
  250. QImage image = originalImage->toImage().transformed ( *matrix, Qt::FastTransformation );
  251. painter->drawImage(p, image);
  252. }
  253. /**
  254. * Draw pixels scaled and flipped
  255. */
  256. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtVolatileImage_drawPixelsScaledFlipped
  257. (JNIEnv *env, jobject obj, jobject graphics,
  258. jint bg_red, jint bg_green, jint bg_blue,
  259. jboolean flipx, jboolean flipy,
  260. jint srcx, jint srcy, jint srcwidth, jint srcheight,
  261. jint dstx, jint dsty, jint dstwidth, jint dstheight,
  262. jboolean composite)
  263. {
  264. QPixmap *originalImage = getQtVolatileImage(env, obj);
  265. assert( originalImage );
  266. QPainter *painter = getPainter( env, graphics );
  267. assert( painter );
  268. QRectF *srcRect = new QRectF((qreal)srcx, (qreal)srcy,
  269. (qreal)srcwidth, (qreal)srcheight);
  270. QRectF *dstRect = new QRectF((qreal)dstx, (qreal)dsty,
  271. (qreal)dstwidth, (qreal)dstheight);
  272. if(composite == JNI_TRUE)
  273. painter->fillRect( *dstRect, QColor(bg_red, bg_green, bg_blue ) );
  274. if( flipx == JNI_TRUE || flipy == JNI_TRUE )
  275. {
  276. QImage im = originalImage->toImage().mirrored ( (flipx == JNI_TRUE),
  277. (flipy == JNI_TRUE) );
  278. painter->drawImage ( *dstRect, im, *srcRect);
  279. }
  280. else
  281. painter->drawPixmap ( *dstRect, *originalImage, *srcRect);
  282. delete srcRect;
  283. delete dstRect;
  284. }
  285. /**
  286. * Copies an area of the image (used by Graphics)
  287. */
  288. JNIEXPORT void JNICALL Java_gnu_java_awt_peer_qt_QtVolatileImage_copyArea
  289. (JNIEnv *env, jobject obj , jint x, jint y, jint w, jint h, jint dx, jint dy)
  290. {
  291. QPixmap *image = getQtVolatileImage(env, obj);
  292. assert( image );
  293. // FIXME
  294. }