001: /*
002: * Copyright 2000-2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.awt.image;
027:
028: import java.awt.Point;
029: import java.awt.image.BufferedImage;
030: import java.awt.image.ColorModel;
031: import java.awt.image.DataBuffer;
032: import java.awt.image.DirectColorModel;
033: import java.awt.image.PixelInterleavedSampleModel;
034: import java.awt.image.SampleModel;
035: import java.awt.image.SinglePixelPackedSampleModel;
036: import java.awt.image.WritableRaster;
037: import sun.java2d.SurfaceData;
038:
039: /**
040: * WritableRasterNative
041: * This class exists to wrap a native DataBuffer object. The
042: * standard WritableRaster object assumes that a DataBuffer
043: * of a given type (e.g., DataBuffer.TYPE_INT) implies a certain
044: * subclass (e.g., DataBufferInt). But this is not always the
045: * case. DataBufferNative, for example, may allow access to
046: * integer-based data, but it is not DataBufferInt (which is a
047: * final class and cannot be subclassed).
048: * So this class exists simply to allow the WritableRaster
049: * functionality for this new kind of DataBuffer object.
050: */
051: public class WritableRasterNative extends WritableRaster {
052:
053: public static WritableRasterNative createNativeRaster(
054: SampleModel sm, DataBuffer db) {
055: return new WritableRasterNative(sm, db);
056: }
057:
058: protected WritableRasterNative(SampleModel sm, DataBuffer db) {
059: super (sm, db, new Point(0, 0));
060: }
061:
062: public static WritableRasterNative createNativeRaster(
063: ColorModel cm, SurfaceData sd, int width, int height) {
064: SampleModel smHw = null;
065: int dataType = 0;
066: int scanStride = width;
067:
068: switch (cm.getPixelSize()) {
069: case 8:
070: case 12:
071: // 8-bits uses PixelInterleavedSampleModel
072: if (cm.getPixelSize() == 8) {
073: dataType = DataBuffer.TYPE_BYTE;
074: } else {
075: dataType = DataBuffer.TYPE_USHORT;
076: }
077: int[] bandOffsets = new int[1];
078: bandOffsets[0] = 0;
079: smHw = new PixelInterleavedSampleModel(dataType, width,
080: height, 1, scanStride, bandOffsets);
081: break;
082:
083: // all others use SinglePixelPackedSampleModel
084: case 15:
085: case 16:
086: dataType = DataBuffer.TYPE_USHORT;
087: int[] bitMasks = new int[3];
088: DirectColorModel dcm = (DirectColorModel) cm;
089: bitMasks[0] = dcm.getRedMask();
090: bitMasks[1] = dcm.getGreenMask();
091: bitMasks[2] = dcm.getBlueMask();
092: smHw = new SinglePixelPackedSampleModel(dataType, width,
093: height, scanStride, bitMasks);
094: break;
095: case 24:
096: case 32:
097: dataType = DataBuffer.TYPE_INT;
098: bitMasks = new int[3];
099: dcm = (DirectColorModel) cm;
100: bitMasks[0] = dcm.getRedMask();
101: bitMasks[1] = dcm.getGreenMask();
102: bitMasks[2] = dcm.getBlueMask();
103: smHw = new SinglePixelPackedSampleModel(dataType, width,
104: height, scanStride, bitMasks);
105: break;
106: default:
107: throw new InternalError("Unsupported depth "
108: + cm.getPixelSize());
109: }
110:
111: DataBuffer dbn = new DataBufferNative(sd, dataType, width,
112: height);
113: return new WritableRasterNative(smHw, dbn);
114: }
115: }
|