FSF GCC merge 02/23/03
[official-gcc.git] / libjava / jni / gtk-peer / gnu_java_awt_peer_gtk_GtkImagePainter.c
blobdd446c89489ab4aea40def9e0e1f76dc23e42c30
1 /* gtkimagepainter.c
2 Copyright (C) 1999 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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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 "gtkpeer.h"
39 #include "gnu_java_awt_peer_gtk_GtkImagePainter.h"
40 #include <libart_lgpl/art_misc.h>
41 #include <libart_lgpl/art_rgb_affine.h>
43 #define SWAPU32(w) \
44 (((w) << 24) | (((w) & 0xff00) << 8) | (((w) >> 8) & 0xff00) | ((w) >> 24))
46 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GtkImagePainter_drawPixels
47 (JNIEnv *env, jobject obj, jobject gc_obj, jint bg_red, jint bg_green,
48 jint bg_blue, jint x, jint y, jint width, jint height, jintArray jpixels,
49 jint offset, jint scansize, jdoubleArray jaffine)
51 struct graphics *g;
52 jint *pixels, *elems;
53 guchar *packed;
54 int i;
55 jsize num_pixels;
56 guchar *j_rgba, *c_rgb;
58 g = (struct graphics *) NSA_GET_PTR (env, gc_obj);
60 elems = (*env)->GetIntArrayElements (env, jpixels, NULL);
61 num_pixels = (*env)->GetArrayLength (env, jpixels);
63 /* get a copy of the pixel data so we can modify it */
64 pixels = malloc (sizeof (jint) * num_pixels);
65 memcpy (pixels, elems, sizeof (jint) * num_pixels);
67 (*env)->ReleaseIntArrayElements (env, jpixels, elems, 0);
69 #ifndef WORDS_BIGENDIAN
70 /* convert pixels from 0xBBGGRRAA to 0xAARRGGBB */
71 for (i = 0; i < num_pixels; i++)
72 pixels[i] = SWAPU32 ((unsigned)pixels[i]);
73 #endif
75 packed = (guchar *) malloc (sizeof (guchar) * 3 * num_pixels);
76 j_rgba = (guchar *) pixels;
77 c_rgb = packed;
79 /* copy over pixels in DirectColorModel format to 24 bit RGB image data,
80 and process the alpha channel */
81 for (i = 0; i < num_pixels; i++)
83 jint ialpha = *j_rgba++;
85 switch (ialpha)
87 case 0: /* full transparency */
88 *c_rgb++ = bg_red;
89 *c_rgb++ = bg_green;
90 *c_rgb++ = bg_blue;
91 j_rgba += 3;
92 break;
93 case 255: /* opaque */
94 *c_rgb++ = *j_rgba++;
95 *c_rgb++ = *j_rgba++;
96 *c_rgb++ = *j_rgba++;
97 break;
98 default: /* compositing required */
100 jfloat alpha = ialpha / 255.0;
101 jfloat comp_alpha = 1.0 - alpha;
103 *c_rgb++ = *j_rgba++ * alpha + bg_red * comp_alpha;
104 *c_rgb++ = *j_rgba++ * alpha + bg_green * comp_alpha;
105 *c_rgb++ = *j_rgba++ * alpha + bg_blue * comp_alpha;
107 break;
111 if (jaffine)
113 jdouble *affine;
114 ArtAlphaGamma *alphagamma = NULL;
115 art_u8 *dst;
116 int new_width, new_height;
117 int i;
119 affine = (*env)->GetDoubleArrayElements (env, jaffine, NULL);
121 new_width = abs (width * affine[0]);
122 new_height = abs (height * affine[3]);
124 dst = (art_u8 *) malloc (sizeof (art_u8) * 3 * (new_width * new_height));
126 art_rgb_affine (dst,
127 0, 0,
128 new_width, new_height,
129 new_width * 3,
130 (art_u8 *) packed + offset * 3,
131 width, height,
132 scansize * 3,
133 affine,
134 ART_FILTER_NEAREST,
135 alphagamma);
137 (*env)->ReleaseDoubleArrayElements (env, jaffine, affine, JNI_ABORT);
139 free (packed);
140 packed = (guchar *) dst;
142 width = scansize = new_width;
143 height = new_height;
144 offset = 0;
147 gdk_threads_enter ();
149 gdk_draw_rgb_image (g->drawable,
150 g->gc,
151 x + g->x_offset,
152 y + g->y_offset,
153 width, height, GDK_RGB_DITHER_NORMAL,
154 packed + offset * 3, scansize * 3);
156 gdk_threads_leave ();
158 free (pixels);
159 free (packed);