comparison src/samer/tools/ImageViewer.java @ 0:bf79fb79ee13

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