1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17/**
18 * @author Igor V. Stolyarov
19 * @version $Revision$
20 */
21
22package org.apache.harmony.awt.gl.image;
23
24import java.awt.image.BufferedImage;
25import java.awt.image.ColorModel;
26import java.awt.image.ComponentColorModel;
27import java.awt.image.DataBuffer;
28import java.awt.image.DataBufferByte;
29import java.awt.image.DataBufferInt;
30import java.awt.image.DirectColorModel;
31import java.awt.image.ImageConsumer;
32import java.awt.image.ImageProducer;
33import java.awt.image.IndexColorModel;
34import java.awt.image.WritableRaster;
35import java.util.Hashtable;
36
37public class BufferedImageSource implements ImageProducer {
38
39    private Hashtable<?, ?> properties;
40    private ColorModel cm;
41    private WritableRaster raster;
42    private int width;
43    private int height;
44
45    private ImageConsumer ic;
46
47    public BufferedImageSource(BufferedImage image, Hashtable<?, ?> properties){
48        if(properties == null) {
49            this.properties = new Hashtable<Object, Object>();
50        } else {
51            this.properties = properties;
52        }
53
54        width = image.getWidth();
55        height = image.getHeight();
56        cm = image.getColorModel();
57        raster = image.getRaster();
58    }
59
60    public BufferedImageSource(BufferedImage image){
61        this(image, null);
62    }
63
64    public boolean isConsumer(ImageConsumer ic) {
65        return (this.ic == ic);
66    }
67
68    public void startProduction(ImageConsumer ic) {
69        addConsumer(ic);
70    }
71
72    public void requestTopDownLeftRightResend(ImageConsumer ic) {
73    }
74
75    public void removeConsumer(ImageConsumer ic) {
76        if (this.ic == ic) {
77            this.ic = null;
78        }
79    }
80
81    public void addConsumer(ImageConsumer ic) {
82        this.ic = ic;
83        startProduction();
84    }
85
86    private void startProduction(){
87        try {
88            ic.setDimensions(width, height);
89            ic.setProperties(properties);
90            ic.setColorModel(cm);
91            ic.setHints(ImageConsumer.TOPDOWNLEFTRIGHT |
92                    ImageConsumer.COMPLETESCANLINES |
93                    ImageConsumer.SINGLEFRAME |
94                    ImageConsumer.SINGLEPASS);
95            if(cm instanceof IndexColorModel &&
96                    raster.getTransferType() == DataBuffer.TYPE_BYTE ||
97                    cm instanceof ComponentColorModel &&
98                    raster.getTransferType() == DataBuffer.TYPE_BYTE &&
99                    raster.getNumDataElements() == 1){
100                DataBufferByte dbb = (DataBufferByte) raster.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.getDataBuffer();
107                int data[] = dbi.getData();
108                int off = dbi.getOffset();
109                ic.setPixels(0, 0, width, height, cm, data, off, width);
110            }else if(cm instanceof DirectColorModel &&
111                    raster.getTransferType() == DataBuffer.TYPE_BYTE){
112                DataBufferByte dbb = (DataBufferByte) raster.getDataBuffer();
113                byte data[] = dbb.getData();
114                int off = dbb.getOffset();
115                ic.setPixels(0, 0, width, height, cm, data, off, width);
116            }else{
117                ColorModel rgbCM = ColorModel.getRGBdefault();
118                int pixels[] = new int[width];
119                Object pix = null;
120                for(int y = 0; y < height; y++){
121                    for(int x = 0 ; x < width; x++){
122                        pix = raster.getDataElements(x, y, pix);
123                        pixels[x] = cm.getRGB(pix);
124                    }
125                    ic.setPixels(0, y, width, 1, rgbCM, pixels, 0, width);
126                }
127            }
128            ic.imageComplete(ImageConsumer.STATICIMAGEDONE);
129        }catch (NullPointerException e){
130            if (ic != null) {
131                ic.imageComplete(ImageConsumer.IMAGEERROR);
132            }
133        }
134    }
135
136}
137