samer@0: /*
samer@0: * Copyright (c) 2000, Samer Abdallah, King's College London.
samer@0: * All rights reserved.
samer@0: *
samer@0: * This software is provided AS iS and WITHOUT ANY WARRANTY;
samer@0: * without even the implied warranty of MERCHANTABILITY or
samer@0: * FITNESS FOR A PARTICULAR PURPOSE.
samer@0: */
samer@0:
samer@0: package samer.tools;
samer@0: import samer.core.*;
samer@0: import samer.core.util.*;
samer@0: import java.awt.image.*;
samer@0: import java.awt.Color;
samer@0: import java.util.*;
samer@0:
samer@0: import java.util.Hashtable;
samer@0:
samer@0: /**
samer@0:
samer@0: Base class for image producers. Derived classes
samer@0: must do the following:
samer@0:
samer@0: - set width and height before any
samer@0: consumers are added
samer@0:
- implement getHints()
samer@0:
- implement sendPixels(ImageConsumer)
samer@0:
samer@0:
samer@0: */
samer@0:
samer@0: public abstract class ImageSourceBase implements ImageProducer
samer@0: {
samer@0: private Hashtable properties = new Hashtable();
samer@0: private Vector consumers = new Vector();
samer@0: protected IndexColorModel model = GREY;
samer@0: protected int width, height;
samer@0: protected IMap map = new LinearMap(model.getMapSize());
samer@0:
samer@0: protected abstract int getHints();
samer@0: protected abstract void sendPixels(ImageConsumer ic);
samer@0:
samer@0: public IMap getMap() { return map; }
samer@0: public void setMap(IMap m) { map=m; m.setIntRange(model.getMapSize()); }
samer@0:
samer@0: public int getWidth() { return width; }
samer@0: public int getHeight() { return height; }
samer@0: public void setColorModel(IndexColorModel cm) {
samer@0: model=cm; map.setIntRange(model.getMapSize());
samer@0:
samer@0: Enumeration cons = consumers.elements();
samer@0: while (cons.hasMoreElements()) {
samer@0: ImageConsumer ic = (ImageConsumer)cons.nextElement();
samer@0: ic.setColorModel(model);
samer@0: }
samer@0: }
samer@0:
samer@0: public void startProduction(ImageConsumer ic) { addConsumer(ic); }
samer@0: public void requestTopDownLeftRightResend(ImageConsumer ic) {}
samer@0: public synchronized boolean isConsumer(ImageConsumer ic) { return consumers.contains(ic); }
samer@0: public synchronized void removeConsumer(ImageConsumer ic) { consumers.removeElement(ic); }
samer@0: public synchronized void addConsumer(ImageConsumer ic)
samer@0: {
samer@0: if (consumers.contains(ic)) return;
samer@0: consumers.addElement(ic);
samer@0:
samer@0: try {
samer@0: ic.setDimensions(width, height);
samer@0: ic.setProperties(properties);
samer@0: ic.setColorModel(model);
samer@0: ic.setHints(getHints());
samer@0: sendPixels(ic);
samer@0: ic.imageComplete( ImageConsumer.SINGLEFRAMEDONE);
samer@0: } catch (Exception e) {
samer@0: ic.imageComplete(ImageConsumer.IMAGEERROR);
samer@0: }
samer@0: }
samer@0:
samer@0:
samer@0: public void sendPixels()
samer@0: {
samer@0: Enumeration cons = consumers.elements();
samer@0: while (cons.hasMoreElements()) {
samer@0: ImageConsumer ic = (ImageConsumer)cons.nextElement();
samer@0: ic.setDimensions(width, height);
samer@0: ic.setProperties(properties);
samer@0: ic.setColorModel(model);
samer@0: ic.setHints(getHints());
samer@0: sendPixels(ic);
samer@0: ic.imageComplete( ImageConsumer.SINGLEFRAMEDONE);
samer@0: }
samer@0: }
samer@0:
samer@0: public static IndexColorModel GREY,GREEN;
samer@0: static {
samer@0: ColorRamp r = new ColorRamp(256);
samer@0: r.gradient(Color.black,Color.white); GREY = r.getColorModel();
samer@0: }
samer@0: }