Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / gnu / java / awt / peer / gtk / GtkImagePainter.java
blob1e5eb4f6ab36593e34de96ce2ea72ade7be87a1a
1 /* GtkImagePainter.java
2 Copyright (C) 1999, 2000 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. */
39 package gnu.java.awt.peer.gtk;
41 import java.awt.Color;
42 import java.awt.Rectangle;
43 import java.awt.image.ColorModel;
44 import java.awt.image.ImageConsumer;
45 import java.awt.image.ImageObserver;
46 import java.util.Hashtable;
48 public class GtkImagePainter implements Runnable, ImageConsumer
50 GtkImage image;
51 GdkGraphics gc;
52 int startX, startY;
53 int redBG;
54 int greenBG;
55 int blueBG;
56 double affine[];
57 int width, height;
58 boolean flipX, flipY;
59 Rectangle clip;
60 int s_width, s_height;
61 ImageObserver observer;
63 public
64 GtkImagePainter (GtkImage image, GdkGraphics gc, int x, int y,
65 int width, int height, Color bgcolor, ImageObserver o)
67 this.image = image;
68 this.gc = (GdkGraphics) gc.create ();
69 startX = x;
70 startY = y;
71 redBG = bgcolor.getRed ();
72 greenBG = bgcolor.getGreen ();
73 blueBG = bgcolor.getBlue ();
74 this.width = width;
75 this.height = height;
76 flipX = flipY = false;
77 s_width = s_height = 0;
78 clip = null;
79 observer = o;
81 run ();
84 public
85 GtkImagePainter (GtkImage image, GdkGraphics gc,
86 int dx1, int dy1, int dx2, int dy2,
87 int sx1, int sy1, int sx2, int sy2,
88 Color bgcolor, ImageObserver o)
90 this.image = image;
91 this.gc = (GdkGraphics) gc.create ();
92 startX = (dx1 < dx2) ? dx1 : dx2;
93 startY = dy1;
94 redBG = bgcolor.getRed ();
95 greenBG = bgcolor.getGreen ();
96 blueBG = bgcolor.getBlue ();
97 observer = o;
99 this.width = Math.abs (dx2 - dx1);
100 this.height = Math.abs (dy2 - dy1);
102 flipX = ((dx1 > dx2 && sx2 > sx1)
103 || (dx1 < dx2 && sx2 < sx1));
105 flipY = ((dy1 > dy2 && sy2 > sy1)
106 || (dy1 < dy2 && sy2 < sy1));
108 s_width = Math.abs (sx2 - sx1);
109 s_height = Math.abs (sy2 - sy1);
110 clip = new Rectangle (sx1, sy1, s_width, s_height);
112 run ();
115 public void
116 run ()
118 image.startProduction (this);
119 gc.dispose ();
122 /* Convert pixel data into a format that gdkrgb can understand */
123 static int[]
124 convertPixels (int[] pixels, ColorModel model)
126 if (pixels == null || model == null)
128 return null;
131 if (model.equals (ColorModel.getRGBdefault ()))
132 return pixels;
134 int ret[] = new int[pixels.length];
136 for (int i = 0; i < pixels.length; i++)
137 ret[i] = model.getRGB (pixels[i]);
139 return ret;
142 static int[]
143 convertPixels (byte[] pixels, ColorModel model)
145 if (pixels == null || model == null)
147 return null;
150 int ret[] = new int[pixels.length];
152 for (int i = 0; i < pixels.length; i++)
153 ret[i] = model.getRGB (pixels[i]);
155 return ret;
158 native void
159 drawPixels (GdkGraphics gc, int bg_red, int bg_green, int bg_blue,
160 int x, int y, int width, int height, int[] pixels, int offset,
161 int scansize, double affine[]);
164 public void
165 setPixels (int x, int y, int width, int height, ColorModel model,
166 int[] pixels, int offset, int scansize)
168 if (clip != null)
170 Rectangle r;
171 r = clip.intersection (new Rectangle (x, y, width, height));
172 if (r.width == 0 && r.height == 0)
173 return;
175 offset += r.y * scansize + r.x;
177 width = r.width;
178 height = r.height;
179 x = r.x;
180 y = r.y;
183 drawPixels (gc, redBG, greenBG, blueBG,
184 startX + x, startY + y,
185 width, height, convertPixels (pixels, model), offset,
186 scansize, affine);
188 if (observer != null)
189 observer.imageUpdate (image,
190 ImageObserver.SOMEBITS,
191 x, y, width, height);
194 public void
195 setPixels (int x, int y, int width, int height, ColorModel model,
196 byte[] pixels, int offset, int scansize)
198 setPixels (x, y, width, height, ColorModel.getRGBdefault(),
199 convertPixels (pixels, model), offset, scansize);
202 public void
203 setDimensions (int width, int height)
205 if (!flipX && !flipY &&
206 ((this.width == -1 && this.height == -1)
207 || (this.width == width && this.height == height)))
208 return;
210 affine = new double[6];
211 affine[1] = affine[2] = affine[4] = affine[5] = 0;
213 if (clip != null)
215 affine[0] = this.width / (double) s_width;
216 affine[3] = this.height / (double) s_height;
218 else
220 affine[0] = this.width / (double) width;
221 affine[3] = this.height / (double) height;
224 if (flipX)
226 affine[0] = -affine[0];
227 affine[4] = this.width;
230 if (flipY)
232 affine[3] = -affine[3];
233 affine[5] = this.height;
236 if (affine[0] == 1 && affine[3] == 1)
237 affine = null;
240 public void
241 setProperties (Hashtable props)
245 public void
246 setColorModel (ColorModel model)
250 public void
251 setHints (int flags)
255 public void
256 imageComplete (int status)
258 image.imageComplete(status);
260 if (observer != null)
262 if (status == ImageConsumer.IMAGEERROR)
263 observer.imageUpdate (null,
264 ImageObserver.ERROR,
265 -1, -1, -1, -1);
266 else
267 observer.imageUpdate (null,
268 ImageObserver.ALLBITS,
269 -1, -1, -1, -1);