annotate src/samer/tools/ImageViewer.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
samer@0 12 import java.awt.*;
samer@0 13 import java.awt.image.*;
samer@0 14 import java.util.*;
samer@0 15 import samer.core.*;
samer@0 16 import samer.core.util.*;
samer@0 17
samer@0 18 /**
samer@0 19 A base class for displaying images of real values.
samer@0 20 All that is needed is an ImageSource that implements
samer@0 21 ImageSourceBase. The ImageViewer provides a VMap
samer@0 22 for the IMap in the ImageSourceBase, and exposes the
samer@0 23 VMaps commands in a popup menu.
samer@0 24 */
samer@0 25
samer@0 26 public class ImageViewer extends samer.core.util.heavy.VCanvas implements Observer, Agent
samer@0 27 {
samer@0 28 protected ImageSourceBase ip;
samer@0 29 protected Image img;
samer@0 30 protected VMap map;
samer@0 31 protected Observable obs;
samer@0 32 protected int cx=1, cy=1, iw, ih;
samer@0 33 private boolean autoscale=false;
samer@0 34
samer@0 35 public ImageViewer( ImageSourceBase source, Observable o)
samer@0 36 {
samer@0 37 map = new VMap(source.getMap());
samer@0 38 source.setMap(map.getMap());
samer@0 39 source.setColorModel(
samer@0 40 (IndexColorModel)Shell.datum("colormap")
samer@0 41 .get(ColorModelCodec,ImageSourceBase.GREY));
samer@0 42
samer@0 43 ip = source; obs = o;
samer@0 44 img = createImage(ip);
samer@0 45 iw=img.getWidth(null);
samer@0 46 ih=img.getHeight(null);
samer@0 47
samer@0 48 int cz=Shell.getInt("cell.size",4);
samer@0 49 cx=Shell.getInt("cell.width",cz);
samer@0 50 cy=Shell.getInt("cell.height",cz);
samer@0 51
samer@0 52 exposeCommands( this);
samer@0 53 exposeCommands( map);
samer@0 54 map.addObserver(this);
samer@0 55 }
samer@0 56
samer@0 57 public void scale() {}
samer@0 58 public void update(Observable o, Object s)
samer@0 59 {
samer@0 60 if (s==this) return;
samer@0 61 else if (s==VMap.NEW_MAP) ip.setMap(map.getMap());
samer@0 62 else if (s==Viewable.DISPOSING) {
samer@0 63 if (o==obs) Shell.releaseViewer(this); return;
samer@0 64 }
samer@0 65
samer@0 66 if (autoscale) scale();
samer@0 67 ip.sendPixels();
samer@0 68 if (s instanceof Point) {
samer@0 69 Point p=(Point)s;
samer@0 70 int x=(2*p.x*width+iw)/(2*iw);
samer@0 71 int y=(2*p.y*height+ih)/(2*ih);
samer@0 72 int w=(width+iw-1)/iw;
samer@0 73 int h=(height+ih-1)/ih;
samer@0 74 y=height-y-h;
samer@0 75 repaint(0,x,y,w,h);
samer@0 76 } else repaint(0,0,0,width,height);
samer@0 77 }
samer@0 78
samer@0 79 public void attach() { super.attach(); if (obs!=null) obs.addObserver(this); }
samer@0 80 public void detach() { if (obs!=null) obs.deleteObserver(this); super.detach(); }
samer@0 81
samer@0 82 // .............. Agent bits ..............................
samer@0 83
samer@0 84 public void getCommands(Registry r) {
samer@0 85 r.add("scale").add("autoscale",autoscale).add("update");
samer@0 86 r.group(); r.add("publish");
samer@0 87 }
samer@0 88
samer@0 89 public void execute(String c, Environment env) throws Exception
samer@0 90 {
samer@0 91 if (c.equals("scale")) { scale(); update(null,null); }
samer@0 92 else if (c.equals("autoscale")) {
samer@0 93 autoscale = X._bool(env.datum(),!autoscale);
samer@0 94 if (autoscale) update(null,null);
samer@0 95 } else if (c.equals("update")) { update(null,null); }
samer@0 96 else if (c.equals("publish"))
samer@0 97 Shell.put(X.string(env.datum(),"image"),this);
samer@0 98 }
samer@0 99
samer@0 100 // .............. The rest ...............................
samer@0 101
samer@0 102 public VMap getVMap() { return map; }
samer@0 103 public IMap getMap() { return ip.getMap(); }
samer@0 104 public void setMap(IMap map) { ip.setMap(map); }
samer@0 105 public void setColorModel( IndexColorModel cm) {
samer@0 106 ip.setColorModel(cm);
samer@0 107 ip.sendPixels();
samer@0 108 repaint();
samer@0 109 }
samer@0 110
samer@0 111 public void update(Graphics g) {
samer@0 112 g.drawImage( img, 0, 0, width, height, null);
samer@0 113 }
samer@0 114
samer@0 115 public void paint( Graphics g) {
samer@0 116 g.drawImage( img, 0, 0, width, height, null);
samer@0 117 }
samer@0 118
samer@0 119 public void setCellSize( int w, int h) { cx=w; cy=h; }
samer@0 120 public Dimension getPreferredSize() {
samer@0 121 return new Dimension(cx*ip.width,cy*ip.height);
samer@0 122 }
samer@0 123
samer@0 124 public static Environment.Codec ColorModelCodec = new Environment.Codec()
samer@0 125 {
samer@0 126 public Class targetClass() { return IndexColorModel.class; }
samer@0 127
samer@0 128 public String string(Object o) { return o.toString(); } // ??
samer@0 129 public Object object(Object o) { return o; }
samer@0 130 public Object decode(Object o) {
samer@0 131 if (o instanceof IndexColorModel) return o;
samer@0 132 return Shell.datum(o.toString()).get(this,null);
samer@0 133 }
samer@0 134 };
samer@0 135 }