01: /*
02: * $RCSfile: PlanarImageProducer.java,v $
03: *
04: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
05: *
06: * Use is subject to license terms.
07: *
08: * $Revision: 1.1 $
09: * $Date: 2005/02/11 04:57:01 $
10: * $State: Exp $
11: */
12: package com.sun.media.jai.util;
13:
14: import java.awt.Rectangle;
15: import java.awt.image.ColorModel;
16: import java.awt.image.ImageConsumer;
17: import java.awt.image.ImageProducer;
18: import java.awt.image.Raster;
19: import java.util.Vector;
20: import javax.media.jai.PlanarImage;
21:
22: public class PlanarImageProducer implements ImageProducer {
23:
24: PlanarImage im;
25: Vector consumers = new Vector();
26:
27: public PlanarImageProducer(PlanarImage im) {
28: this .im = im.createSnapshot();
29: }
30:
31: public void addConsumer(ImageConsumer ic) {
32: if (!consumers.contains(ic)) {
33: consumers.add(ic);
34: }
35: produceImage();
36: }
37:
38: public boolean isConsumer(ImageConsumer ic) {
39: return consumers.contains(ic);
40: }
41:
42: public void removeConsumer(ImageConsumer ic) {
43: consumers.remove(ic);
44: }
45:
46: public void requestTopDownLeftRightResend(ImageConsumer ic) {
47: startProduction(ic);
48: }
49:
50: public void startProduction(ImageConsumer ic) {
51: if (!consumers.contains(ic)) {
52: consumers.add(ic);
53: }
54: produceImage();
55: }
56:
57: private synchronized void produceImage() {
58: int numConsumers = consumers.size();
59:
60: int minX = im.getMinX();
61: int minY = im.getMinY();
62: int width = im.getWidth();
63: int height = im.getHeight();
64: int numBands = im.getSampleModel().getNumBands();
65: int scansize = width * numBands;
66: ColorModel colorModel = im.getColorModel();
67: int[] pixels = new int[scansize];
68:
69: Rectangle rect = new Rectangle(minX, minY, width, 1);
70:
71: for (int i = 0; i < numConsumers; i++) {
72: ImageConsumer ic = (ImageConsumer) consumers.elementAt(i);
73: ic.setHints(ImageConsumer.COMPLETESCANLINES
74: | ImageConsumer.TOPDOWNLEFTRIGHT
75: | ImageConsumer.SINGLEFRAME);
76: }
77:
78: for (int y = minY; y < minY + height; y++) {
79: rect.y = y;
80: Raster row = im.getData(rect);
81: row.getPixels(minX, y, width, 1, pixels);
82:
83: for (int i = 0; i < numConsumers; i++) {
84: ImageConsumer ic = (ImageConsumer) consumers
85: .elementAt(i);
86: ic.setPixels(0, y - minY, width, 1, colorModel, pixels,
87: 0, scansize);
88: }
89: }
90:
91: for (int i = 0; i < numConsumers; i++) {
92: ImageConsumer ic = (ImageConsumer) consumers.elementAt(i);
93: ic.imageComplete(ImageConsumer.STATICIMAGEDONE);
94: }
95: }
96: }
|