1 /*
2 * Copyright (c) 1997, 2014, 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 package sun.awt.image;
27 import java.awt.image.Raster;
28 import java.awt.image.WritableRaster;
29 import java.awt.image.RasterFormatException;
30 import java.awt.image.SampleModel;
31 import java.awt.image.ComponentSampleModel;
32 import java.awt.image.SinglePixelPackedSampleModel;
33 import java.awt.image.DataBuffer;
34 import java.awt.image.DataBufferUShort;
35 import java.awt.Rectangle;
36 import java.awt.Point;
37
38 /**
39 * This class defines a Raster with pixels consisting of one or more 16-bit
40 * data elements stored in close proximity to each other in a short integer
41 * array. The bit precision per data element is that
42 * of the data type (that is, the bit precision for this Raster is 16).
43 * There is only one pixel stride and one scanline stride for all
44 * bands. This type of Raster can be used with a
45 * ComponentColorModel if there are multiple bands, or a
46 * IndexColorModel if there is only one band.
47 * <p>
48 * For example, 5-6-5 RGB image data can be represented by a
49 * ShortComponentRaster using a SinglePixelPackedSampleModel and
50 * a ComponentColorModel.
51 *
52 *
53 */
77 private int maxY;
78
79 private static native void initIDs();
80 static {
81 /* ensure that the necessary native libraries are loaded */
82 NativeLibLoader.loadLibraries();
83 initIDs();
84 }
85
86 /**
87 * Constructs a ShortComponentRaster with the given SampleModel.
88 * The Raster's upper left corner is origin and it is the same
89 * size as the SampleModel. A DataBuffer large enough to describe the
90 * Raster is automatically created. SampleModel must be of type
91 * ComponentSampleModel or SinglePixelPackedSampleModel.
92 * @param sampleModel The SampleModel that specifies the layout.
93 * @param origin The Point that specified the origin.
94 */
95 public ShortComponentRaster(SampleModel sampleModel, Point origin) {
96 this(sampleModel,
97 sampleModel.createDataBuffer(),
98 new Rectangle(origin.x,
99 origin.y,
100 sampleModel.getWidth(),
101 sampleModel.getHeight()),
102 origin,
103 null);
104 }
105
106 /**
107 * Constructs a ShortComponentRaster with the given SampleModel
108 * and DataBuffer. The Raster's upper left corner is origin and
109 * it is the same sizes the SampleModel. The DataBuffer is not
110 * initialized and must be a DataBufferUShort compatible with SampleModel.
111 * SampleModel must be of type ComponentSampleModel or
112 * SinglePixelPackedSampleModel.
113 * @param sampleModel The SampleModel that specifies the layout.
114 * @param dataBuffer The DataBufferUShort that contains the image data.
115 * @param origin The Point that specifies the origin.
116 */
117 public ShortComponentRaster(SampleModel sampleModel,
118 DataBuffer dataBuffer,
119 Point origin) {
120 this(sampleModel,
121 dataBuffer,
122 new Rectangle(origin.x,
123 origin.y,
124 sampleModel.getWidth(),
125 sampleModel.getHeight()),
126 origin,
127 null);
128 }
129
130 /**
131 * Constructs a ShortComponentRaster with the given SampleModel,
132 * DataBuffer, and parent. DataBuffer must be a DataBufferUShort and
133 * SampleModel must be of type ComponentSampleModel or
134 * SinglePixelPackedSampleModel. When translated into the base Raster's
135 * coordinate system, aRegion must be contained by the base Raster.
136 * Origin is the coodinate in the new Raster's coordinate system of
137 * the origin of the base Raster. (The base Raster is the Raster's
138 * ancestor which has no parent.)
139 *
140 * Note that this constructor should generally be called by other
141 * constructors or create methods, it should not be used directly.
142 * @param sampleModel The SampleModel that specifies the layout.
143 * @param dataBuffer The DataBufferUShort that contains the image data.
144 * @param aRegion The Rectangle that specifies the image area.
145 * @param origin The Point that specifies the origin.
146 * @param parent The parent (if any) of this raster.
147 */
148 public ShortComponentRaster(SampleModel sampleModel,
149 DataBuffer dataBuffer,
150 Rectangle aRegion,
151 Point origin,
152 ShortComponentRaster parent) {
153
154 super(sampleModel, dataBuffer, aRegion, origin, parent);
155 this.maxX = minX + width;
156 this.maxY = minY + height;
157
158 if(!(dataBuffer instanceof DataBufferUShort)) {
159 throw new RasterFormatException("ShortComponentRasters must have "+
160 "short DataBuffers");
161 }
162
163 DataBufferUShort dbus = (DataBufferUShort)dataBuffer;
164 this.data = stealData(dbus, 0);
165 if (dbus.getNumBanks() != 1) {
166 throw new
167 RasterFormatException("DataBuffer for ShortComponentRasters"+
168 " must only have 1 bank.");
169 }
170 int dbOffset = dbus.getOffset();
171
172 if (sampleModel instanceof ComponentSampleModel) {
173 ComponentSampleModel csm = (ComponentSampleModel)sampleModel;
174 this.type = IntegerComponentRaster.TYPE_USHORT_SAMPLES;
175 this.scanlineStride = csm.getScanlineStride();
176 this.pixelStride = csm.getPixelStride();
177 this.dataOffsets = csm.getBandOffsets();
178 int xOffset = aRegion.x - origin.x;
179 int yOffset = aRegion.y - origin.y;
180 for (int i = 0; i < getNumDataElements(); i++) {
181 dataOffsets[i] += dbOffset +
182 xOffset*pixelStride+yOffset*scanlineStride;
183 }
184 } else if (sampleModel instanceof SinglePixelPackedSampleModel) {
185 SinglePixelPackedSampleModel sppsm =
186 (SinglePixelPackedSampleModel)sampleModel;
187 this.type = IntegerComponentRaster.TYPE_USHORT_PACKED_SAMPLES;
188 this.scanlineStride = sppsm.getScanlineStride();
189 this.pixelStride = 1;
190 this.dataOffsets = new int[1];
741 throw new RasterFormatException("y lies outside the raster");
742 }
743 if ((x+width < x) || (x+width > this.minX + this.width)) {
744 throw new RasterFormatException("(x + width) is outside of Raster");
745 }
746 if ((y+height < y) || (y+height > this.minY + this.height)) {
747 throw new RasterFormatException("(y + height) is outside of Raster");
748 }
749
750 SampleModel sm;
751
752 if (bandList != null)
753 sm = sampleModel.createSubsetSampleModel(bandList);
754 else
755 sm = sampleModel;
756
757 int deltaX = x0 - x;
758 int deltaY = y0 - y;
759
760 return new ShortComponentRaster(sm,
761 dataBuffer,
762 new Rectangle(x0, y0, width, height),
763 new Point(sampleModelTranslateX+deltaX,
764 sampleModelTranslateY+deltaY),
765 this);
766 }
767
768 /**
769 * Creates a Raster with the same layout but using a different
770 * width and height, and with new zeroed data arrays.
771 */
772 public WritableRaster createCompatibleWritableRaster(int w, int h) {
773 if (w <= 0 || h <=0) {
774 throw new RasterFormatException("negative "+
775 ((w <= 0) ? "width" : "height"));
776 }
777
778 SampleModel sm = sampleModel.createCompatibleSampleModel(w, h);
779
780 return new ShortComponentRaster(sm, new Point(0, 0));
781 }
|
1 /*
2 * Copyright (c) 1997, 2016, 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 package sun.awt.image;
27 import java.awt.image.Raster;
28 import java.awt.image.WritableRaster;
29 import java.awt.image.RasterFormatException;
30 import java.awt.image.SampleModel;
31 import java.awt.image.ComponentSampleModel;
32 import java.awt.image.SinglePixelPackedSampleModel;
33 import java.awt.image.DataBufferUShort;
34 import java.awt.Rectangle;
35 import java.awt.Point;
36
37 /**
38 * This class defines a Raster with pixels consisting of one or more 16-bit
39 * data elements stored in close proximity to each other in a short integer
40 * array. The bit precision per data element is that
41 * of the data type (that is, the bit precision for this Raster is 16).
42 * There is only one pixel stride and one scanline stride for all
43 * bands. This type of Raster can be used with a
44 * ComponentColorModel if there are multiple bands, or a
45 * IndexColorModel if there is only one band.
46 * <p>
47 * For example, 5-6-5 RGB image data can be represented by a
48 * ShortComponentRaster using a SinglePixelPackedSampleModel and
49 * a ComponentColorModel.
50 *
51 *
52 */
76 private int maxY;
77
78 private static native void initIDs();
79 static {
80 /* ensure that the necessary native libraries are loaded */
81 NativeLibLoader.loadLibraries();
82 initIDs();
83 }
84
85 /**
86 * Constructs a ShortComponentRaster with the given SampleModel.
87 * The Raster's upper left corner is origin and it is the same
88 * size as the SampleModel. A DataBuffer large enough to describe the
89 * Raster is automatically created. SampleModel must be of type
90 * ComponentSampleModel or SinglePixelPackedSampleModel.
91 * @param sampleModel The SampleModel that specifies the layout.
92 * @param origin The Point that specified the origin.
93 */
94 public ShortComponentRaster(SampleModel sampleModel, Point origin) {
95 this(sampleModel,
96 (DataBufferUShort)sampleModel.createDataBuffer(),
97 new Rectangle(origin.x,
98 origin.y,
99 sampleModel.getWidth(),
100 sampleModel.getHeight()),
101 origin,
102 null);
103 }
104
105 /**
106 * Constructs a ShortComponentRaster with the given SampleModel
107 * and DataBuffer. The Raster's upper left corner is origin and
108 * it is the same sizes the SampleModel. The DataBuffer is not
109 * initialized and must be a DataBufferUShort compatible with SampleModel.
110 * SampleModel must be of type ComponentSampleModel or
111 * SinglePixelPackedSampleModel.
112 * @param sampleModel The SampleModel that specifies the layout.
113 * @param dataBuffer The DataBufferUShort that contains the image data.
114 * @param origin The Point that specifies the origin.
115 */
116 public ShortComponentRaster(SampleModel sampleModel,
117 DataBufferUShort dataBuffer,
118 Point origin) {
119 this(sampleModel,
120 dataBuffer,
121 new Rectangle(origin.x,
122 origin.y,
123 sampleModel.getWidth(),
124 sampleModel.getHeight()),
125 origin,
126 null);
127 }
128
129 /**
130 * Constructs a ShortComponentRaster with the given SampleModel,
131 * DataBuffer, and parent. DataBuffer must be a DataBufferUShort and
132 * SampleModel must be of type ComponentSampleModel or
133 * SinglePixelPackedSampleModel. When translated into the base Raster's
134 * coordinate system, aRegion must be contained by the base Raster.
135 * Origin is the coodinate in the new Raster's coordinate system of
136 * the origin of the base Raster. (The base Raster is the Raster's
137 * ancestor which has no parent.)
138 *
139 * Note that this constructor should generally be called by other
140 * constructors or create methods, it should not be used directly.
141 * @param sampleModel The SampleModel that specifies the layout.
142 * @param dataBuffer The DataBufferUShort that contains the image data.
143 * @param aRegion The Rectangle that specifies the image area.
144 * @param origin The Point that specifies the origin.
145 * @param parent The parent (if any) of this raster.
146 */
147 public ShortComponentRaster(SampleModel sampleModel,
148 DataBufferUShort dataBuffer,
149 Rectangle aRegion,
150 Point origin,
151 ShortComponentRaster parent) {
152
153 super(sampleModel, dataBuffer, aRegion, origin, parent);
154 this.maxX = minX + width;
155 this.maxY = minY + height;
156
157 this.data = stealData(dataBuffer, 0);
158 if (dataBuffer.getNumBanks() != 1) {
159 throw new
160 RasterFormatException("DataBuffer for ShortComponentRasters"+
161 " must only have 1 bank.");
162 }
163 int dbOffset = dataBuffer.getOffset();
164
165 if (sampleModel instanceof ComponentSampleModel) {
166 ComponentSampleModel csm = (ComponentSampleModel)sampleModel;
167 this.type = IntegerComponentRaster.TYPE_USHORT_SAMPLES;
168 this.scanlineStride = csm.getScanlineStride();
169 this.pixelStride = csm.getPixelStride();
170 this.dataOffsets = csm.getBandOffsets();
171 int xOffset = aRegion.x - origin.x;
172 int yOffset = aRegion.y - origin.y;
173 for (int i = 0; i < getNumDataElements(); i++) {
174 dataOffsets[i] += dbOffset +
175 xOffset*pixelStride+yOffset*scanlineStride;
176 }
177 } else if (sampleModel instanceof SinglePixelPackedSampleModel) {
178 SinglePixelPackedSampleModel sppsm =
179 (SinglePixelPackedSampleModel)sampleModel;
180 this.type = IntegerComponentRaster.TYPE_USHORT_PACKED_SAMPLES;
181 this.scanlineStride = sppsm.getScanlineStride();
182 this.pixelStride = 1;
183 this.dataOffsets = new int[1];
734 throw new RasterFormatException("y lies outside the raster");
735 }
736 if ((x+width < x) || (x+width > this.minX + this.width)) {
737 throw new RasterFormatException("(x + width) is outside of Raster");
738 }
739 if ((y+height < y) || (y+height > this.minY + this.height)) {
740 throw new RasterFormatException("(y + height) is outside of Raster");
741 }
742
743 SampleModel sm;
744
745 if (bandList != null)
746 sm = sampleModel.createSubsetSampleModel(bandList);
747 else
748 sm = sampleModel;
749
750 int deltaX = x0 - x;
751 int deltaY = y0 - y;
752
753 return new ShortComponentRaster(sm,
754 (DataBufferUShort)dataBuffer,
755 new Rectangle(x0, y0, width, height),
756 new Point(sampleModelTranslateX+deltaX,
757 sampleModelTranslateY+deltaY),
758 this);
759 }
760
761 /**
762 * Creates a Raster with the same layout but using a different
763 * width and height, and with new zeroed data arrays.
764 */
765 public WritableRaster createCompatibleWritableRaster(int w, int h) {
766 if (w <= 0 || h <=0) {
767 throw new RasterFormatException("negative "+
768 ((w <= 0) ? "width" : "height"));
769 }
770
771 SampleModel sm = sampleModel.createCompatibleSampleModel(w, h);
772
773 return new ShortComponentRaster(sm, new Point(0, 0));
774 }
|