2
* Copyright (c) 1995, 2003, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
26
package sun.awt.image;
28
import java.util.Hashtable;
29
import java.awt.image.ImageConsumer;
30
import java.awt.image.ImageProducer;
31
import java.awt.image.Raster;
32
import java.awt.image.WritableRaster;
33
import java.awt.image.ColorModel;
34
import java.awt.image.IndexColorModel;
35
import java.awt.image.DirectColorModel;
36
import java.awt.image.BufferedImage;
37
import java.awt.image.DataBuffer;
39
public class OffScreenImageSource implements ImageProducer {
45
public OffScreenImageSource(BufferedImage image,
46
Hashtable properties) {
48
if (properties != null) {
49
this.properties = properties;
51
this.properties = new Hashtable();
53
width = image.getWidth();
54
height = image.getHeight();
57
public OffScreenImageSource(BufferedImage image) {
61
// We can only have one consumer since we immediately return the data...
62
private ImageConsumer theConsumer;
64
public synchronized void addConsumer(ImageConsumer ic) {
69
public synchronized boolean isConsumer(ImageConsumer ic) {
70
return (ic == theConsumer);
73
public synchronized void removeConsumer(ImageConsumer ic) {
74
if (theConsumer == ic) {
79
public void startProduction(ImageConsumer ic) {
83
public void requestTopDownLeftRightResend(ImageConsumer ic) {
86
private void sendPixels() {
87
ColorModel cm = image.getColorModel();
88
WritableRaster raster = image.getRaster();
89
int numDataElements = raster.getNumDataElements();
90
int dataType = raster.getDataBuffer().getDataType();
91
int[] scanline = new int[width*numDataElements];
92
boolean needToCvt = true;
94
if (cm instanceof IndexColorModel) {
95
byte[] pixels = new byte[width];
96
theConsumer.setColorModel(cm);
98
if (raster instanceof ByteComponentRaster) {
100
for (int y=0; y < height; y++) {
101
raster.getDataElements(0, y, width, 1, pixels);
102
theConsumer.setPixels(0, y, width, 1, cm, pixels, 0,
106
else if (raster instanceof BytePackedRaster) {
108
// Binary image. Need to unpack it
109
for (int y=0; y < height; y++) {
110
raster.getPixels(0, y, width, 1, scanline);
111
for (int x=0; x < width; x++) {
112
pixels[x] = (byte) scanline[x];
114
theConsumer.setPixels(0, y, width, 1, cm, pixels, 0,
118
else if (dataType == DataBuffer.TYPE_SHORT ||
119
dataType == DataBuffer.TYPE_INT)
121
// Probably a short or int "GRAY" image
123
for (int y=0; y < height; y++) {
124
raster.getPixels(0, y, width, 1, scanline);
125
theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
130
else if (cm instanceof DirectColorModel) {
131
theConsumer.setColorModel(cm);
134
case DataBuffer.TYPE_INT:
135
for (int y=0; y < height; y++) {
136
raster.getDataElements(0, y, width, 1, scanline);
137
theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
141
case DataBuffer.TYPE_BYTE:
142
byte[] bscanline = new byte[width];
143
for (int y=0; y < height; y++) {
144
raster.getDataElements(0, y, width, 1, bscanline);
145
for (int x=0; x < width; x++) {
146
scanline[x] = bscanline[x]&0xff;
148
theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
152
case DataBuffer.TYPE_USHORT:
153
short[] sscanline = new short[width];
154
for (int y=0; y < height; y++) {
155
raster.getDataElements(0, y, width, 1, sscanline);
156
for (int x=0; x < width; x++) {
157
scanline[x] = sscanline[x]&0xffff;
159
theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
169
// REMIND: Need to add other types of CMs here
170
ColorModel newcm = ColorModel.getRGBdefault();
171
theConsumer.setColorModel(newcm);
173
for (int y=0; y < height; y++) {
174
for (int x=0; x < width; x++) {
175
scanline[x] = image.getRGB(x, y);
177
theConsumer.setPixels(0, y, width, 1, newcm, scanline, 0,
183
private void produce() {
185
theConsumer.setDimensions(image.getWidth(), image.getHeight());
186
theConsumer.setProperties(properties);
188
theConsumer.imageComplete(ImageConsumer.SINGLEFRAMEDONE);
189
} catch (NullPointerException e) {
190
if (theConsumer != null) {
191
theConsumer.imageComplete(ImageConsumer.IMAGEERROR);