view src/samer/tools/ImageViewer.java @ 5:b67a33c44de7

Remove some crap, etc
author samer
date Fri, 05 Apr 2019 21:34:25 +0100
parents bf79fb79ee13
children
line wrap: on
line source
/*
 *	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 java.awt.*;
import java.awt.image.*;
import java.util.*;
import samer.core.*;
import samer.core.util.*;

/**
	A base class for displaying images of real values.
	All that is needed is an ImageSource that implements
	ImageSourceBase. The ImageViewer provides a VMap
	for the IMap in the ImageSourceBase, and exposes the
	VMaps commands in a popup menu.
  */

public class ImageViewer extends samer.core.util.heavy.VCanvas implements Observer, Agent
{
	protected ImageSourceBase	ip;
	protected Image		img;
	protected VMap		map;
	protected Observable obs;
	protected int			cx=1, cy=1, iw, ih;
	private boolean		autoscale=false;

	public ImageViewer( ImageSourceBase source, Observable o)
	{
		map = new VMap(source.getMap());
		source.setMap(map.getMap());
		source.setColorModel(
			(IndexColorModel)Shell.datum("colormap")
				.get(ColorModelCodec,ImageSourceBase.GREY));

		ip	 = source; obs = o;
		img = createImage(ip);
		iw=img.getWidth(null);
		ih=img.getHeight(null);

		int cz=Shell.getInt("cell.size",4);
		cx=Shell.getInt("cell.width",cz);
		cy=Shell.getInt("cell.height",cz);

		exposeCommands( this);
		exposeCommands( map);
		map.addObserver(this);
	}

	public void scale() {}
	public void update(Observable o, Object s)
	{
		if (s==this) return;
		else if (s==VMap.NEW_MAP) ip.setMap(map.getMap());
		else if (s==Viewable.DISPOSING) {
			if (o==obs) Shell.releaseViewer(this); return;
		}

		if (autoscale) scale();
		ip.sendPixels();
		if (s instanceof Point) {
			Point p=(Point)s;
			int x=(2*p.x*width+iw)/(2*iw);
			int y=(2*p.y*height+ih)/(2*ih);
			int w=(width+iw-1)/iw;
			int h=(height+ih-1)/ih;
			y=height-y-h;
			repaint(0,x,y,w,h);
		} else repaint(0,0,0,width,height);
	}

	public void attach() { super.attach(); if (obs!=null) obs.addObserver(this);	}
	public void detach()	{ if (obs!=null) obs.deleteObserver(this); super.detach();	}

	// .............. Agent bits ..............................

	public void getCommands(Registry r)	{
		r.add("scale").add("autoscale",autoscale).add("update");
		r.group(); r.add("publish");
	}

	public void execute(String c, Environment env) throws Exception
	{
		if (c.equals("scale"))	{ scale(); update(null,null); }
		else if	(c.equals("autoscale")) {
			autoscale = X._bool(env.datum(),!autoscale);
			if (autoscale) update(null,null);
		} else if (c.equals("update")) { update(null,null); }
		else if (c.equals("publish"))
			Shell.put(X.string(env.datum(),"image"),this);
	}

	// .............. The rest ...............................

	public VMap getVMap() { return map; }
	public IMap getMap() { return ip.getMap(); }
	public void setMap(IMap map) { ip.setMap(map); }
	public void	setColorModel( IndexColorModel cm) {
		ip.setColorModel(cm);
		ip.sendPixels();
		repaint();
	}

	public void update(Graphics g) {
		g.drawImage( img, 0, 0, width, height, null);
	}

	public void paint( Graphics g) {
		g.drawImage( img, 0, 0, width, height, null);
	}

	public void setCellSize( int w, int h) { cx=w; cy=h; }
	public Dimension getPreferredSize() {
		return new Dimension(cx*ip.width,cy*ip.height);
	}

	public static Environment.Codec ColorModelCodec = new Environment.Codec()
	{
		public Class targetClass() { return IndexColorModel.class; }

		public String string(Object o) { return o.toString(); } // ??
		public Object object(Object o) { return o; }
		public Object decode(Object o) {
			if (o instanceof IndexColorModel) return o;
			return Shell.datum(o.toString()).get(this,null);
		}
	};
}