Merge from mainline.
[official-gcc.git] / libjava / classpath / java / awt / image / renderable / RenderableImageProducer.java
blobd8cca653527a72dfb1e8b730f7912e7635dd589f
1 /* RenderableImageProducer.java --
2 Copyright (C) 2002, 2006 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., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 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 java.awt.image.renderable;
41 import java.awt.image.ColorModel;
42 import java.awt.image.DataBuffer;
43 import java.awt.image.ImageConsumer;
44 import java.awt.image.ImageProducer;
45 import java.awt.image.Raster;
46 import java.awt.image.RenderedImage;
47 import java.awt.image.SampleModel;
48 import java.util.ArrayList;
49 import java.util.Iterator;
51 public class RenderableImageProducer implements ImageProducer, Runnable
53 private RenderableImage image;
54 private RenderContext context;
55 private ArrayList consumers = new ArrayList();
57 public RenderableImageProducer(RenderableImage image, RenderContext context)
59 this.image = image;
60 this.context = context;
63 public void setRenderContext(RenderContext context)
65 this.context = context;
68 public void addConsumer(ImageConsumer consumer)
70 synchronized (consumers)
72 if (! consumers.contains(consumer))
73 consumers.add(consumer);
77 public boolean isConsumer(ImageConsumer consumer)
79 synchronized (consumers)
81 return consumers.contains(consumer);
85 public void removeConsumer(ImageConsumer consumer)
87 synchronized (consumers)
89 consumers.remove(consumer);
93 public void startProduction(ImageConsumer consumer)
95 addConsumer(consumer);
96 Thread t = new Thread(this, "RenderableImageProducerWorker");
97 t.start();
100 public void requestTopDownLeftRightResend(ImageConsumer consumer)
102 // Do nothing. The contract says we can ignore this call, so we do.
105 public void run()
107 // This isn't ideal but it avoids fail-fast problems.
108 // Alternatively, we could clone 'consumers' here.
109 synchronized (consumers)
111 RenderedImage newImage;
112 if (context == null)
113 newImage = image.createDefaultRendering();
114 else
115 newImage = image.createRendering(context);
116 Raster newData = newImage.getData();
117 ColorModel colorModel = newImage.getColorModel();
118 if (colorModel == null)
119 colorModel = ColorModel.getRGBdefault();
120 SampleModel sampleModel = newData.getSampleModel();
121 DataBuffer dataBuffer = newData.getDataBuffer();
122 int width = newData.getWidth();
123 int height = newData.getHeight();
125 // Initialize the consumers.
126 Iterator it = consumers.iterator();
127 while (it.hasNext())
129 ImageConsumer target = (ImageConsumer) it.next();
130 target.setHints(ImageConsumer.COMPLETESCANLINES
131 | ImageConsumer.SINGLEFRAME
132 | ImageConsumer.SINGLEPASS
133 | ImageConsumer.TOPDOWNLEFTRIGHT);
134 target.setDimensions(width, height);
137 // Work in scan-line order.
138 int[] newLine = new int[width];
139 int[] bands = new int[sampleModel.getNumBands()];
140 for (int y = 0; y < height; ++y)
142 for (int x = 0; x < width; ++x)
144 sampleModel.getPixel(x, y, bands, dataBuffer);
145 newLine[x] = colorModel.getDataElement(bands, 0);
148 // Tell the consumers about the new scan line.
149 it = consumers.iterator();
150 while (it.hasNext())
152 ImageConsumer target = (ImageConsumer) it.next();
153 target.setPixels(0, y, width, 1, colorModel, newLine, 0, width);
157 // Tell the consumers that we're done.
158 it = consumers.iterator();
159 while (it.hasNext())
161 ImageConsumer target = (ImageConsumer) it.next();
162 target.imageComplete(ImageConsumer.STATICIMAGEDONE);
166 } // class RenderableImageProducer