001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Igor V. Stolyarov
019: * @version $Revision$
020: */package org.apache.harmony.awt.gl.image;
021:
022: import java.awt.image.BufferedImage;
023: import java.awt.image.ColorModel;
024: import java.awt.image.ComponentColorModel;
025: import java.awt.image.DataBuffer;
026: import java.awt.image.DataBufferByte;
027: import java.awt.image.DataBufferInt;
028: import java.awt.image.DirectColorModel;
029: import java.awt.image.ImageConsumer;
030: import java.awt.image.ImageProducer;
031: import java.awt.image.IndexColorModel;
032: import java.awt.image.WritableRaster;
033: import java.util.Hashtable;
034:
035: public class BufferedImageSource implements ImageProducer {
036:
037: private Hashtable<?, ?> properties;
038: private ColorModel cm;
039: private WritableRaster raster;
040: private int width;
041: private int height;
042:
043: private ImageConsumer ic;
044:
045: public BufferedImageSource(BufferedImage image,
046: Hashtable<?, ?> properties) {
047: if (properties == null) {
048: this .properties = new Hashtable<Object, Object>();
049: } else {
050: this .properties = properties;
051: }
052:
053: width = image.getWidth();
054: height = image.getHeight();
055: cm = image.getColorModel();
056: raster = image.getRaster();
057: }
058:
059: public BufferedImageSource(BufferedImage image) {
060: this (image, null);
061: }
062:
063: public boolean isConsumer(ImageConsumer ic) {
064: return (this .ic == ic);
065: }
066:
067: public void startProduction(ImageConsumer ic) {
068: addConsumer(ic);
069: }
070:
071: public void requestTopDownLeftRightResend(ImageConsumer ic) {
072: }
073:
074: public void removeConsumer(ImageConsumer ic) {
075: if (this .ic == ic) {
076: this .ic = null;
077: }
078: }
079:
080: public void addConsumer(ImageConsumer ic) {
081: this .ic = ic;
082: startProduction();
083: }
084:
085: private void startProduction() {
086: try {
087: ic.setDimensions(width, height);
088: ic.setProperties(properties);
089: ic.setColorModel(cm);
090: ic.setHints(ImageConsumer.TOPDOWNLEFTRIGHT
091: | ImageConsumer.COMPLETESCANLINES
092: | ImageConsumer.SINGLEFRAME
093: | ImageConsumer.SINGLEPASS);
094: if (cm instanceof IndexColorModel
095: && raster.getTransferType() == DataBuffer.TYPE_BYTE
096: || cm instanceof ComponentColorModel
097: && raster.getTransferType() == DataBuffer.TYPE_BYTE
098: && raster.getNumDataElements() == 1) {
099: DataBufferByte dbb = (DataBufferByte) raster
100: .getDataBuffer();
101: byte data[] = dbb.getData();
102: int off = dbb.getOffset();
103: ic.setPixels(0, 0, width, height, cm, data, off, width);
104: } else if (cm instanceof DirectColorModel
105: && raster.getTransferType() == DataBuffer.TYPE_INT) {
106: DataBufferInt dbi = (DataBufferInt) raster
107: .getDataBuffer();
108: int data[] = dbi.getData();
109: int off = dbi.getOffset();
110: ic.setPixels(0, 0, width, height, cm, data, off, width);
111: } else if (cm instanceof DirectColorModel
112: && raster.getTransferType() == DataBuffer.TYPE_BYTE) {
113: DataBufferByte dbb = (DataBufferByte) raster
114: .getDataBuffer();
115: byte data[] = dbb.getData();
116: int off = dbb.getOffset();
117: ic.setPixels(0, 0, width, height, cm, data, off, width);
118: } else {
119: ColorModel rgbCM = ColorModel.getRGBdefault();
120: int pixels[] = new int[width];
121: Object pix = null;
122: for (int y = 0; y < height; y++) {
123: for (int x = 0; x < width; x++) {
124: pix = raster.getDataElements(x, y, pix);
125: pixels[x] = cm.getRGB(pix);
126: }
127: ic.setPixels(0, y, width, 1, rgbCM, pixels, 0,
128: width);
129: }
130: }
131: ic.imageComplete(ImageConsumer.STATICIMAGEDONE);
132: } catch (NullPointerException e) {
133: if (ic != null) {
134: ic.imageComplete(ImageConsumer.IMAGEERROR);
135: }
136: }
137: }
138:
139: }
|