001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Igor V. Stolyarov
019: * @version $Revision$
020: */package java.awt.image.renderable;
021:
022: import java.awt.image.ColorModel;
023: import java.awt.image.ImageConsumer;
024: import java.awt.image.ImageProducer;
025: import java.awt.image.Raster;
026: import java.awt.image.RenderedImage;
027: import java.util.Vector;
028:
029: public class RenderableImageProducer implements ImageProducer, Runnable {
030:
031: RenderableImage rbl;
032: RenderContext rc;
033: Vector<ImageConsumer> consumers = new Vector<ImageConsumer>();
034:
035: public RenderableImageProducer(RenderableImage rdblImage,
036: RenderContext rc) {
037: this .rbl = rdblImage;
038: this .rc = rc;
039: }
040:
041: public synchronized void setRenderContext(RenderContext rc) {
042: this .rc = rc;
043: }
044:
045: public synchronized boolean isConsumer(ImageConsumer ic) {
046: return consumers.contains(ic);
047: }
048:
049: public synchronized void startProduction(ImageConsumer ic) {
050: addConsumer(ic);
051: Thread t = new Thread(this , "RenderableImageProducer thread"); //$NON-NLS-1$
052: t.start();
053: }
054:
055: public void requestTopDownLeftRightResend(ImageConsumer ic) {
056: }
057:
058: public synchronized void removeConsumer(ImageConsumer ic) {
059: if (ic != null) {
060: consumers.removeElement(ic);
061: }
062: }
063:
064: public synchronized void addConsumer(ImageConsumer ic) {
065: if (ic != null && !consumers.contains(ic)) {
066: consumers.addElement(ic);
067: }
068: }
069:
070: public void run() {
071: if (rbl == null) {
072: return;
073: }
074:
075: RenderedImage rd;
076: if (rc != null) {
077: rd = rbl.createRendering(rc);
078: } else {
079: rd = rbl.createDefaultRendering();
080: }
081:
082: ColorModel cm = rd.getColorModel();
083: if (cm == null) {
084: cm = ColorModel.getRGBdefault();
085: }
086:
087: Raster r = rd.getData();
088: int w = r.getWidth();
089: int h = r.getHeight();
090:
091: for (ImageConsumer c : consumers) {
092: c.setDimensions(w, h);
093: c.setHints(ImageConsumer.TOPDOWNLEFTRIGHT
094: | ImageConsumer.COMPLETESCANLINES
095: | ImageConsumer.SINGLEFRAME
096: | ImageConsumer.SINGLEPASS);
097: }
098:
099: int scanLine[] = new int[w];
100: int pixel[] = null;
101:
102: for (int y = 0; y < h; y++) {
103: for (int x = 0; x < w; x++) {
104: pixel = r.getPixel(x, y, pixel);
105: scanLine[x] = cm.getDataElement(pixel, 0);
106: }
107:
108: for (ImageConsumer c : consumers) {
109: c.setPixels(0, y, w, 1, cm, scanLine, 0, w);
110: }
111: }
112:
113: for (ImageConsumer c : consumers) {
114: c.imageComplete(ImageConsumer.STATICIMAGEDONE);
115: }
116: }
117:
118: }
|