OSDN Git Service

2004-11-30 Thomas Fitzsimmons <fitzsim@redhat.com>
[pf3gnuchains/gcc-fork.git] / libjava / jni / gtk-peer / gnu_java_awt_peer_gtk_GtkImagePainter.c
1 /* gtkimagepainter.c
2    Copyright (C) 1999, 2004 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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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 "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>
42
43
44 JNIEXPORT void JNICALL
45 Java_gnu_java_awt_peer_gtk_GtkImagePainter_drawPixels
46   (JNIEnv *env, jobject obj __attribute__((unused)), jobject gc_obj,
47    jint bg_red, jint bg_green, jint bg_blue, jint x, jint y, jint width,
48    jint height, jintArray jpixels, jint offset, jint scansize,
49    jdoubleArray jaffine)
50 {
51   struct graphics *g;
52   jint *pixels, *elems;
53   guchar *packed;
54   int i;
55   jsize num_pixels;
56   guchar *j_rgba, *c_rgb;
57
58   g = (struct graphics *) NSA_GET_PTR (env, gc_obj);
59
60   if (!jpixels)
61     return;
62
63   elems = (*env)->GetIntArrayElements (env, jpixels, NULL);
64   num_pixels = (*env)->GetArrayLength (env, jpixels);
65  
66   /* get a copy of the pixel data so we can modify it */
67   pixels = malloc (sizeof (jint) * num_pixels);
68   memcpy (pixels, elems, sizeof (jint) * num_pixels);
69  
70   (*env)->ReleaseIntArrayElements (env, jpixels, elems, 0);
71
72 #ifndef WORDS_BIGENDIAN
73   /* convert pixels from 0xBBGGRRAA to 0xAARRGGBB */
74   for (i = 0; i < num_pixels; i++)
75     pixels[i] = SWAPU32 ((unsigned)pixels[i]);
76 #endif
77
78   packed = (guchar *) malloc (sizeof (guchar) * 3 * num_pixels);
79   j_rgba = (guchar *) pixels;
80   c_rgb = packed;
81
82   /* copy over pixels in DirectColorModel format to 24 bit RGB image data,
83      and process the alpha channel */
84   for (i = 0; i < num_pixels; i++)
85     {
86       jint ialpha = *j_rgba++;
87
88       switch (ialpha)
89         {
90         case 0:                 /* full transparency */
91           *c_rgb++ = bg_red;
92           *c_rgb++ = bg_green;
93           *c_rgb++ = bg_blue;
94           j_rgba += 3;
95           break;
96         case 255:               /* opaque */
97           *c_rgb++ = *j_rgba++;
98           *c_rgb++ = *j_rgba++;
99           *c_rgb++ = *j_rgba++;
100           break;
101         default:                /* compositing required */
102           {
103             jfloat alpha = ialpha / 255.0;
104             jfloat comp_alpha = 1.0 - alpha;
105             
106             *c_rgb++ = *j_rgba++ * alpha + bg_red * comp_alpha;
107             *c_rgb++ = *j_rgba++ * alpha + bg_green * comp_alpha;
108             *c_rgb++ = *j_rgba++ * alpha + bg_blue * comp_alpha;
109           }
110           break;
111         }
112     }
113
114   if (jaffine)
115     {
116       jdouble *affine;
117       ArtAlphaGamma *alphagamma = NULL;
118       art_u8 *dst;
119       int new_width, new_height;
120
121       affine = (*env)->GetDoubleArrayElements (env, jaffine, NULL);
122
123       new_width = abs (width * affine[0]);
124       new_height = abs (height * affine[3]);
125
126       dst = (art_u8 *) malloc (sizeof (art_u8) * 3 * (new_width * new_height));
127       
128       art_rgb_affine (dst, 
129                       0, 0,
130                       new_width, new_height,
131                       new_width * 3,
132                       (art_u8 *) packed + offset * 3,
133                       width, height,
134                       scansize * 3,
135                       affine,
136                       ART_FILTER_NEAREST,
137                       alphagamma);
138
139       (*env)->ReleaseDoubleArrayElements (env, jaffine, affine, JNI_ABORT);
140       
141       free (packed);
142       packed = (guchar *) dst;
143
144       width = scansize = new_width;
145       height = new_height;
146       offset = 0;
147     }
148
149   gdk_threads_enter ();
150
151   if (!g || !GDK_IS_DRAWABLE (g->drawable))
152     {
153       gdk_threads_leave ();
154       return;
155     }
156
157   gdk_draw_rgb_image (g->drawable,
158                       g->gc,
159                       x + g->x_offset, 
160                       y + g->y_offset, 
161                       width, height, GDK_RGB_DITHER_NORMAL,
162                       packed + offset * 3, scansize * 3);
163
164   gdk_threads_leave ();
165
166   free (pixels); 
167   free (packed);
168 }