annotate src/samer/tools/ImageSourceBase.java @ 8:5e3cbbf173aa tip

Reorganise some more
author samer
date Fri, 05 Apr 2019 22:41:58 +0100
parents bf79fb79ee13
children
rev   line source
samer@0 1 /*
samer@0 2 * Copyright (c) 2000, Samer Abdallah, King's College London.
samer@0 3 * All rights reserved.
samer@0 4 *
samer@0 5 * This software is provided AS iS and WITHOUT ANY WARRANTY;
samer@0 6 * without even the implied warranty of MERCHANTABILITY or
samer@0 7 * FITNESS FOR A PARTICULAR PURPOSE.
samer@0 8 */
samer@0 9
samer@0 10 package samer.tools;
samer@0 11 import samer.core.*;
samer@0 12 import samer.core.util.*;
samer@0 13 import java.awt.image.*;
samer@0 14 import java.awt.Color;
samer@0 15 import java.util.*;
samer@0 16
samer@0 17 import java.util.Hashtable;
samer@0 18
samer@0 19 /**
samer@0 20 <p>
samer@0 21 Base class for image producers. Derived classes
samer@0 22 must do the following:
samer@0 23 <ul>
samer@0 24 <li> set width and height <b>before<b> any
samer@0 25 consumers are added
samer@0 26 <li> implement getHints()
samer@0 27 <li> implement sendPixels(ImageConsumer)
samer@0 28 </ul>
samer@0 29
samer@0 30 */
samer@0 31
samer@0 32 public abstract class ImageSourceBase implements ImageProducer
samer@0 33 {
samer@0 34 private Hashtable properties = new Hashtable();
samer@0 35 private Vector consumers = new Vector();
samer@0 36 protected IndexColorModel model = GREY;
samer@0 37 protected int width, height;
samer@0 38 protected IMap map = new LinearMap(model.getMapSize());
samer@0 39
samer@0 40 protected abstract int getHints();
samer@0 41 protected abstract void sendPixels(ImageConsumer ic);
samer@0 42
samer@0 43 public IMap getMap() { return map; }
samer@0 44 public void setMap(IMap m) { map=m; m.setIntRange(model.getMapSize()); }
samer@0 45
samer@0 46 public int getWidth() { return width; }
samer@0 47 public int getHeight() { return height; }
samer@0 48 public void setColorModel(IndexColorModel cm) {
samer@0 49 model=cm; map.setIntRange(model.getMapSize());
samer@0 50
samer@0 51 Enumeration cons = consumers.elements();
samer@0 52 while (cons.hasMoreElements()) {
samer@0 53 ImageConsumer ic = (ImageConsumer)cons.nextElement();
samer@0 54 ic.setColorModel(model);
samer@0 55 }
samer@0 56 }
samer@0 57
samer@0 58 public void startProduction(ImageConsumer ic) { addConsumer(ic); }
samer@0 59 public void requestTopDownLeftRightResend(ImageConsumer ic) {}
samer@0 60 public synchronized boolean isConsumer(ImageConsumer ic) { return consumers.contains(ic); }
samer@0 61 public synchronized void removeConsumer(ImageConsumer ic) { consumers.removeElement(ic); }
samer@0 62 public synchronized void addConsumer(ImageConsumer ic)
samer@0 63 {
samer@0 64 if (consumers.contains(ic)) return;
samer@0 65 consumers.addElement(ic);
samer@0 66
samer@0 67 try {
samer@0 68 ic.setDimensions(width, height);
samer@0 69 ic.setProperties(properties);
samer@0 70 ic.setColorModel(model);
samer@0 71 ic.setHints(getHints());
samer@0 72 sendPixels(ic);
samer@0 73 ic.imageComplete( ImageConsumer.SINGLEFRAMEDONE);
samer@0 74 } catch (Exception e) {
samer@0 75 ic.imageComplete(ImageConsumer.IMAGEERROR);
samer@0 76 }
samer@0 77 }
samer@0 78
samer@0 79
samer@0 80 public void sendPixels()
samer@0 81 {
samer@0 82 Enumeration cons = consumers.elements();
samer@0 83 while (cons.hasMoreElements()) {
samer@0 84 ImageConsumer ic = (ImageConsumer)cons.nextElement();
samer@0 85 ic.setDimensions(width, height);
samer@0 86 ic.setProperties(properties);
samer@0 87 ic.setColorModel(model);
samer@0 88 ic.setHints(getHints());
samer@0 89 sendPixels(ic);
samer@0 90 ic.imageComplete( ImageConsumer.SINGLEFRAMEDONE);
samer@0 91 }
samer@0 92 }
samer@0 93
samer@0 94 public static IndexColorModel GREY,GREEN;
samer@0 95 static {
samer@0 96 ColorRamp r = new ColorRamp(256);
samer@0 97 r.gradient(Color.black,Color.white); GREY = r.getColorModel();
samer@0 98 }
samer@0 99 }