1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43:
44: public class MemoryImageSource implements ImageProducer
45: {
46: private boolean animated = false;
47: private boolean fullbuffers = false;
48: private int[] pixeli;
49: private int width;
50: private int height;
51: private int offset;
52: private int scansize;
53: private byte[] pixelb;
54: private ColorModel cm;
55: private Hashtable props = new Hashtable();
56: private Vector consumers = new Vector();
57:
58:
69: public MemoryImageSource(int w, int h, ColorModel cm, byte[] pix, int off,
70: int scan)
71: {
72: this(w, h, cm, pix, off, scan, null);
73: }
74:
75:
78: public MemoryImageSource(int w, int h, ColorModel cm, byte[] pix, int off,
79: int scan, Hashtable props)
80: {
81: width = w;
82: height = h;
83: this.cm = cm;
84: offset = off;
85: scansize = scan;
86: this.props = props;
87: int max = ((scansize > width) ? scansize : width);
88: pixelb = pix;
89: }
90:
91:
102: public MemoryImageSource(int w, int h, ColorModel cm, int[] pix, int off,
103: int scan)
104: {
105: this(w, h, cm, pix, off, scan, null);
106: }
107:
108:
111: public MemoryImageSource(int w, int h, ColorModel cm, int[] pix, int off,
112: int scan, Hashtable props)
113: {
114: width = w;
115: height = h;
116: this.cm = cm;
117: offset = off;
118: scansize = scan;
119: this.props = props;
120: int max = ((scansize > width) ? scansize : width);
121: pixeli = pix;
122: }
123:
124:
127: public MemoryImageSource(int w, int h, int[] pix, int off, int scan,
128: Hashtable props)
129: {
130: this(w, h, ColorModel.getRGBdefault(), pix, off, scan, props);
131: }
132:
133:
136: public MemoryImageSource(int w, int h, int[] pix, int off, int scan)
137: {
138: this(w, h, ColorModel.getRGBdefault(), pix, off, scan, null);
139: }
140:
141:
145: public synchronized void addConsumer(ImageConsumer ic)
146: {
147: if (consumers.contains(ic))
148: return;
149:
150: consumers.addElement(ic);
151: }
152:
153:
157: public synchronized boolean isConsumer(ImageConsumer ic)
158: {
159: if (consumers.contains(ic))
160: return true;
161: return false;
162: }
163:
164:
168: public synchronized void removeConsumer(ImageConsumer ic)
169: {
170: consumers.removeElement(ic);
171: }
172:
173:
179: public void startProduction(ImageConsumer ic)
180: {
181: if (! (consumers.contains(ic)))
182: consumers.addElement(ic);
183:
184: Vector list = (Vector) consumers.clone();
185: for (int i = 0; i < list.size(); i++)
186: {
187: ic = (ImageConsumer) list.elementAt(i);
188: sendPicture(ic);
189: if (animated)
190: ic.imageComplete(ImageConsumer.SINGLEFRAMEDONE);
191: else
192: ic.imageComplete(ImageConsumer.STATICIMAGEDONE);
193: }
194: }
195:
196:
201: public void requestTopDownLeftRightResend(ImageConsumer ic)
202: {
203: startProduction(ic);
204: }
205:
206:
212: public synchronized void setAnimated(boolean animated)
213: {
214: this.animated = animated;
215: }
216:
217:
224: public synchronized void setFullBufferUpdates(boolean fullbuffers)
225: {
226: this.fullbuffers = fullbuffers;
227: }
228:
229:
232: public void newPixels()
233: {
234: if (animated == true)
235: {
236: ImageConsumer ic;
237: Vector list = (Vector) consumers.clone();
238: for (int i = 0; i < list.size(); i++)
239: {
240: ic = (ImageConsumer) list.elementAt(i);
241: sendPicture(ic);
242: ic.imageComplete(ImageConsumer.SINGLEFRAME);
243: }
244: }
245: }
246:
247: private void sendPicture(ImageConsumer ic)
248: {
249: ic.setHints(ImageConsumer.TOPDOWNLEFTRIGHT);
250: if (props != null)
251: ic.setProperties(props);
252: ic.setDimensions(width, height);
253: ic.setColorModel(cm);
254: if (pixeli != null)
255: ic.setPixels(0, 0, width, height, cm, pixeli, offset, scansize);
256: else
257: ic.setPixels(0, 0, width, height, cm, pixelb, offset, scansize);
258: }
259:
260:
264: public synchronized void newPixels(int x, int y, int w, int h)
265: {
266: if (animated == true)
267: {
268: if (fullbuffers)
269: newPixels();
270: else
271: {
272: ImageConsumer ic;
273: Vector list = (Vector) consumers.clone();
274: for (int i = 0; i < list.size(); i++)
275: {
276: ic = (ImageConsumer) list.elementAt(i);
277: ic.setHints(ImageConsumer.TOPDOWNLEFTRIGHT);
278: if (props != null)
279: ic.setProperties(props);
280: if (pixeli != null)
281: {
282: int[] pixelbuf = new int[w * h];
283: for (int row = y; row < y + h; row++)
284: System.arraycopy(pixeli, row * scansize + x + offset,
285: pixelbuf, 0, w * h);
286: ic.setPixels(x, y, w, h, cm, pixelbuf, 0, w);
287: }
288: else
289: {
290: byte[] pixelbuf = new byte[w * h];
291: for (int row = y; row < y + h; row++)
292: System.arraycopy(pixelb, row * scansize + x + offset,
293: pixelbuf, 0, w * h);
294:
295: ic.setPixels(x, y, w, h, cm, pixelbuf, 0, w);
296: }
297: ic.imageComplete(ImageConsumer.SINGLEFRAME);
298: }
299: }
300: }
301: }
302:
303:
310: public synchronized void newPixels(int x, int y, int w, int h,
311: boolean framenotify)
312: {
313: if (animated == true)
314: {
315: if (fullbuffers)
316: newPixels();
317: else
318: {
319: ImageConsumer ic;
320: Vector list = (Vector) consumers.clone();
321: for (int i = 0; i < list.size(); i++)
322: {
323: ic = (ImageConsumer) list.elementAt(i);
324: ic.setHints(ImageConsumer.TOPDOWNLEFTRIGHT);
325: if (props != null)
326: ic.setProperties(props);
327: if (pixeli != null)
328: {
329: int[] pixelbuf = new int[w * h];
330: for (int row = y; row < y + h; row++)
331: System.arraycopy(pixeli, row * scansize + x + offset,
332: pixelbuf, 0, w * h);
333: ic.setPixels(x, y, w, h, cm, pixelbuf, 0, w);
334: }
335: else
336: {
337: byte[] pixelbuf = new byte[w * h];
338: for (int row = y; row < y + h; row++)
339: System.arraycopy(pixelb, row * scansize + x + offset,
340: pixelbuf, 0, w * h);
341: ic.setPixels(x, y, w, h, cm, pixelbuf, 0, w);
342: }
343: if (framenotify == true)
344: ic.imageComplete(ImageConsumer.SINGLEFRAME);
345: }
346: }
347: }
348: }
349:
350: public synchronized void newPixels(byte[] newpix, ColorModel newmodel,
351: int offset, int scansize)
352: {
353: pixeli = null;
354: pixelb = newpix;
355: cm = newmodel;
356: this.offset = offset;
357: this.scansize = scansize;
358: if (animated == true)
359: newPixels();
360: }
361:
362: public synchronized void newPixels(int[] newpix, ColorModel newmodel,
363: int offset, int scansize)
364: {
365: pixelb = null;
366: pixeli = newpix;
367: cm = newmodel;
368: this.offset = offset;
369: this.scansize = scansize;
370: if (animated == true)
371: newPixels();
372: }
373: }