1 /*
2  * Copyright (c) 2000, 2004, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
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.
10  *
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).
16  *
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.
20  *
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
23  * questions.
24  */
25 
26 
27 
28 package sun.awt.image;
29 
30 import java.awt.Point;
31 import java.awt.image.BufferedImage;
32 import java.awt.image.ColorModel;
33 import java.awt.image.DataBuffer;
34 import java.awt.image.DirectColorModel;
35 import java.awt.image.PixelInterleavedSampleModel;
36 import java.awt.image.SampleModel;
37 import java.awt.image.SinglePixelPackedSampleModel;
38 import java.awt.image.WritableRaster;
39 import sun.java2d.SurfaceData;
40 
41 /**
42  * WritableRasterNative
43  * This class exists to wrap a native DataBuffer object.  The
44  * standard WritableRaster object assumes that a DataBuffer
45  * of a given type (e.g., DataBuffer.TYPE_INT) implies a certain
46  * subclass (e.g., DataBufferInt).  But this is not always the
47  * case.  DataBufferNative, for example, may allow access to
48  * integer-based data, but it is not DataBufferInt (which is a
49  * final class and cannot be subclassed).
50  * So this class exists simply to allow the WritableRaster
51  * functionality for this new kind of DataBuffer object.
52  */
53 public class WritableRasterNative extends WritableRaster {
54 
createNativeRaster(SampleModel sm, DataBuffer db)55     public static WritableRasterNative createNativeRaster(SampleModel sm,
56                                                           DataBuffer db)
57     {
58         return new WritableRasterNative(sm, db);
59     }
60 
WritableRasterNative(SampleModel sm, DataBuffer db)61     protected WritableRasterNative(SampleModel sm, DataBuffer db) {
62         super(sm, db, new Point(0, 0));
63     }
64 
createNativeRaster(ColorModel cm, SurfaceData sd, int width, int height)65     public static WritableRasterNative createNativeRaster(ColorModel cm,
66                                                           SurfaceData sd,
67                                                           int width,
68                                                           int height)
69     {
70         SampleModel smHw = null;
71         int dataType = 0;
72         int scanStride = width;
73 
74         switch (cm.getPixelSize()) {
75         case 8:
76         case 12:
77             // 8-bits uses PixelInterleavedSampleModel
78             if (cm.getPixelSize() == 8) {
79                 dataType = DataBuffer.TYPE_BYTE;
80             } else {
81                 dataType = DataBuffer.TYPE_USHORT;
82             }
83             int[] bandOffsets = new int[1];
84             bandOffsets[0] = 0;
85             smHw = new PixelInterleavedSampleModel(dataType, width,
86                                                    height,
87                                                    1, scanStride,
88                                                    bandOffsets);
89             break;
90 
91             // all others use SinglePixelPackedSampleModel
92         case 15:
93         case 16:
94             dataType = DataBuffer.TYPE_USHORT;
95             int[] bitMasks = new int[3];
96             DirectColorModel dcm = (DirectColorModel)cm;
97             bitMasks[0] = dcm.getRedMask();
98             bitMasks[1] = dcm.getGreenMask();
99             bitMasks[2] = dcm.getBlueMask();
100             smHw = new SinglePixelPackedSampleModel(dataType, width,
101                                                     height, scanStride,
102                                                     bitMasks);
103             break;
104         case 24:
105         case 32:
106             dataType = DataBuffer.TYPE_INT;
107             bitMasks = new int[3];
108             dcm = (DirectColorModel)cm;
109             bitMasks[0] = dcm.getRedMask();
110             bitMasks[1] = dcm.getGreenMask();
111             bitMasks[2] = dcm.getBlueMask();
112             smHw = new SinglePixelPackedSampleModel(dataType, width,
113                                                     height, scanStride,
114                                                     bitMasks);
115             break;
116         default:
117             throw new InternalError("Unsupported depth " +
118                                     cm.getPixelSize());
119         }
120 
121         DataBuffer dbn = new DataBufferNative(sd, dataType,
122                                               width, height);
123         return new WritableRasterNative(smHw, dbn);
124     }
125 }
126