Mercurial > hg > jslab
comparison src/samer/tools/ImageSourceBase.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 import samer.core.*; | |
12 import samer.core.util.*; | |
13 import java.awt.image.*; | |
14 import java.awt.Color; | |
15 import java.util.*; | |
16 | |
17 import java.util.Hashtable; | |
18 | |
19 /** | |
20 <p> | |
21 Base class for image producers. Derived classes | |
22 must do the following: | |
23 <ul> | |
24 <li> set width and height <b>before<b> any | |
25 consumers are added | |
26 <li> implement getHints() | |
27 <li> implement sendPixels(ImageConsumer) | |
28 </ul> | |
29 | |
30 */ | |
31 | |
32 public abstract class ImageSourceBase implements ImageProducer | |
33 { | |
34 private Hashtable properties = new Hashtable(); | |
35 private Vector consumers = new Vector(); | |
36 protected IndexColorModel model = GREY; | |
37 protected int width, height; | |
38 protected IMap map = new LinearMap(model.getMapSize()); | |
39 | |
40 protected abstract int getHints(); | |
41 protected abstract void sendPixels(ImageConsumer ic); | |
42 | |
43 public IMap getMap() { return map; } | |
44 public void setMap(IMap m) { map=m; m.setIntRange(model.getMapSize()); } | |
45 | |
46 public int getWidth() { return width; } | |
47 public int getHeight() { return height; } | |
48 public void setColorModel(IndexColorModel cm) { | |
49 model=cm; map.setIntRange(model.getMapSize()); | |
50 | |
51 Enumeration cons = consumers.elements(); | |
52 while (cons.hasMoreElements()) { | |
53 ImageConsumer ic = (ImageConsumer)cons.nextElement(); | |
54 ic.setColorModel(model); | |
55 } | |
56 } | |
57 | |
58 public void startProduction(ImageConsumer ic) { addConsumer(ic); } | |
59 public void requestTopDownLeftRightResend(ImageConsumer ic) {} | |
60 public synchronized boolean isConsumer(ImageConsumer ic) { return consumers.contains(ic); } | |
61 public synchronized void removeConsumer(ImageConsumer ic) { consumers.removeElement(ic); } | |
62 public synchronized void addConsumer(ImageConsumer ic) | |
63 { | |
64 if (consumers.contains(ic)) return; | |
65 consumers.addElement(ic); | |
66 | |
67 try { | |
68 ic.setDimensions(width, height); | |
69 ic.setProperties(properties); | |
70 ic.setColorModel(model); | |
71 ic.setHints(getHints()); | |
72 sendPixels(ic); | |
73 ic.imageComplete( ImageConsumer.SINGLEFRAMEDONE); | |
74 } catch (Exception e) { | |
75 ic.imageComplete(ImageConsumer.IMAGEERROR); | |
76 } | |
77 } | |
78 | |
79 | |
80 public void sendPixels() | |
81 { | |
82 Enumeration cons = consumers.elements(); | |
83 while (cons.hasMoreElements()) { | |
84 ImageConsumer ic = (ImageConsumer)cons.nextElement(); | |
85 ic.setDimensions(width, height); | |
86 ic.setProperties(properties); | |
87 ic.setColorModel(model); | |
88 ic.setHints(getHints()); | |
89 sendPixels(ic); | |
90 ic.imageComplete( ImageConsumer.SINGLEFRAMEDONE); | |
91 } | |
92 } | |
93 | |
94 public static IndexColorModel GREY,GREEN; | |
95 static { | |
96 ColorRamp r = new ColorRamp(256); | |
97 r.gradient(Color.black,Color.white); GREY = r.getColorModel(); | |
98 } | |
99 } |