diff src/samer/tools/ImageSourceBase.java @ 0:bf79fb79ee13

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