| 1 | /* RenderableImageProducer.java -- |
| 2 | Copyright (C) 2002, 2006 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., 51 Franklin Street, Fifth Floor, Boston, MA |
| 19 | 02110-1301 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 | |
| 39 | package java.awt.image.renderable; |
| 40 | |
| 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; |
| 50 | |
| 51 | public class RenderableImageProducer implements ImageProducer, Runnable |
| 52 | { |
| 53 | private RenderableImage image; |
| 54 | private RenderContext context; |
| 55 | private ArrayList consumers = new ArrayList(); |
| 56 | |
| 57 | public RenderableImageProducer(RenderableImage image, RenderContext context) |
| 58 | { |
| 59 | this.image = image; |
| 60 | this.context = context; |
| 61 | } |
| 62 | |
| 63 | public void setRenderContext(RenderContext context) |
| 64 | { |
| 65 | this.context = context; |
| 66 | } |
| 67 | |
| 68 | public void addConsumer(ImageConsumer consumer) |
| 69 | { |
| 70 | synchronized (consumers) |
| 71 | { |
| 72 | if (! consumers.contains(consumer)) |
| 73 | consumers.add(consumer); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | public boolean isConsumer(ImageConsumer consumer) |
| 78 | { |
| 79 | synchronized (consumers) |
| 80 | { |
| 81 | return consumers.contains(consumer); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | public void removeConsumer(ImageConsumer consumer) |
| 86 | { |
| 87 | synchronized (consumers) |
| 88 | { |
| 89 | consumers.remove(consumer); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | public void startProduction(ImageConsumer consumer) |
| 94 | { |
| 95 | addConsumer(consumer); |
| 96 | Thread t = new Thread(this, "RenderableImageProducerWorker"); |
| 97 | t.start(); |
| 98 | } |
| 99 | |
| 100 | public void requestTopDownLeftRightResend(ImageConsumer consumer) |
| 101 | { |
| 102 | // Do nothing. The contract says we can ignore this call, so we do. |
| 103 | } |
| 104 | |
| 105 | public void run() |
| 106 | { |
| 107 | // This isn't ideal but it avoids fail-fast problems. |
| 108 | // Alternatively, we could clone 'consumers' here. |
| 109 | synchronized (consumers) |
| 110 | { |
| 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(); |
| 124 | |
| 125 | // Initialize the consumers. |
| 126 | Iterator it = consumers.iterator(); |
| 127 | while (it.hasNext()) |
| 128 | { |
| 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); |
| 135 | } |
| 136 | |
| 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) |
| 141 | { |
| 142 | for (int x = 0; x < width; ++x) |
| 143 | { |
| 144 | sampleModel.getPixel(x, y, bands, dataBuffer); |
| 145 | newLine[x] = colorModel.getDataElement(bands, 0); |
| 146 | } |
| 147 | |
| 148 | // Tell the consumers about the new scan line. |
| 149 | it = consumers.iterator(); |
| 150 | while (it.hasNext()) |
| 151 | { |
| 152 | ImageConsumer target = (ImageConsumer) it.next(); |
| 153 | target.setPixels(0, y, width, 1, colorModel, newLine, 0, width); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // Tell the consumers that we're done. |
| 158 | it = consumers.iterator(); |
| 159 | while (it.hasNext()) |
| 160 | { |
| 161 | ImageConsumer target = (ImageConsumer) it.next(); |
| 162 | target.imageComplete(ImageConsumer.STATICIMAGEDONE); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | } // class RenderableImageProducer |