jdk/src/share/classes/sun/awt/image/OffScreenImageSource.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 10870 710b7036a3a1
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1995, 2003, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.awt.image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.image.ImageConsumer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.image.ImageProducer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.image.Raster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.image.WritableRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.image.ColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.image.IndexColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.image.DirectColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.image.BufferedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.image.DataBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class OffScreenImageSource implements ImageProducer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    BufferedImage image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    int width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    int height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    Hashtable properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public OffScreenImageSource(BufferedImage image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
                                Hashtable properties) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        this.image = image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        if (properties != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            this.properties = properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            this.properties = new Hashtable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        width  = image.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        height = image.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public OffScreenImageSource(BufferedImage image) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        this(image, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // We can only have one consumer since we immediately return the data...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private ImageConsumer theConsumer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public synchronized void addConsumer(ImageConsumer ic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        theConsumer = ic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        produce();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public synchronized boolean isConsumer(ImageConsumer ic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        return (ic == theConsumer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public synchronized void removeConsumer(ImageConsumer ic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (theConsumer == ic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            theConsumer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public void startProduction(ImageConsumer ic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        addConsumer(ic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public void requestTopDownLeftRightResend(ImageConsumer ic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private void sendPixels() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        ColorModel cm = image.getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        WritableRaster raster = image.getRaster();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        int numDataElements = raster.getNumDataElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        int dataType = raster.getDataBuffer().getDataType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        int[] scanline = new int[width*numDataElements];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        boolean needToCvt = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (cm instanceof IndexColorModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            byte[] pixels = new byte[width];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            theConsumer.setColorModel(cm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            if (raster instanceof ByteComponentRaster) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                needToCvt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                for (int y=0; y < height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    raster.getDataElements(0, y, width, 1, pixels);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    theConsumer.setPixels(0, y, width, 1, cm, pixels, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                          width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            else if (raster instanceof BytePackedRaster) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                needToCvt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                // Binary image.  Need to unpack it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                for (int y=0; y < height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    raster.getPixels(0, y, width, 1, scanline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    for (int x=0; x < width; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                        pixels[x] = (byte) scanline[x];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    theConsumer.setPixels(0, y, width, 1, cm, pixels, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                                          width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            else if (dataType == DataBuffer.TYPE_SHORT ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                     dataType == DataBuffer.TYPE_INT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                // Probably a short or int "GRAY" image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                needToCvt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                for (int y=0; y < height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    raster.getPixels(0, y, width, 1, scanline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                          width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        else if (cm instanceof DirectColorModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            theConsumer.setColorModel(cm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            needToCvt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            switch (dataType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            case DataBuffer.TYPE_INT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                for (int y=0; y < height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    raster.getDataElements(0, y, width, 1, scanline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                                          width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            case DataBuffer.TYPE_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                byte[] bscanline = new byte[width];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                for (int y=0; y < height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    raster.getDataElements(0, y, width, 1, bscanline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    for (int x=0; x < width; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                        scanline[x] = bscanline[x]&0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                                          width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            case DataBuffer.TYPE_USHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                short[] sscanline = new short[width];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                for (int y=0; y < height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    raster.getDataElements(0, y, width, 1, sscanline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    for (int x=0; x < width; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                        scanline[x] = sscanline[x]&0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                                          width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                needToCvt = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (needToCvt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            // REMIND: Need to add other types of CMs here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            ColorModel newcm = ColorModel.getRGBdefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            theConsumer.setColorModel(newcm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            for (int y=0; y < height; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                for (int x=0; x < width; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    scanline[x] = image.getRGB(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                theConsumer.setPixels(0, y, width, 1, newcm, scanline, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                                      width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    private void produce() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            theConsumer.setDimensions(image.getWidth(), image.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            theConsumer.setProperties(properties);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            sendPixels();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            theConsumer.imageComplete(ImageConsumer.SINGLEFRAMEDONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        } catch (NullPointerException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            if (theConsumer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                theConsumer.imageComplete(ImageConsumer.IMAGEERROR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
}