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.SinglePixelPackedSampleModel;
32 import java.awt.image.DataBuffer;
33 import java.awt.image.DataBufferInt;
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 32-bit
39 * data elements stored in close proximity to each other in a integer array.
40 * The bit precision per data element is that
41 * of the data type (that is, the bit precision for this raster is 32).
42 * There is only one pixel stride and one scanline stride for all
43 * bands. For a given pixel, all samples fit in N data elements and these
44 * N data elements hold samples for only one pixel. This type of Raster
45 * can be used with a PackedColorModel.
46 * <p>
47 * For example, if there is only one data element per pixel, a
48 * SinglePixelPackedSampleModel can be used to represent multiple
49 * bands with a PackedColorModel (including a DirectColorModel) for
50 * color interpretation.
51 *
52 */
90
91 /** A cached copy of minY + height for use in bounds checks. */
92 private int maxY;
93
94 private static native void initIDs();
95 static {
96 /* ensure that the necessary native libraries are loaded */
97 NativeLibLoader.loadLibraries();
98 initIDs();
99 }
100
101 /**
102 * Constructs a IntegerComponentRaster with the given SampleModel.
103 * The Raster's upper left corner is origin and it is the same
104 * size as the SampleModel. A DataBuffer large enough to describe the
105 * Raster is automatically created. SampleModel must be of type
106 * SinglePixelPackedSampleModel.
107 * @param sampleModel The SampleModel that specifies the layout.
108 * @param origin The Point that specified the origin.
109 */
110 public IntegerComponentRaster(SampleModel sampleModel,
111 Point origin) {
112 this(sampleModel,
113 sampleModel.createDataBuffer(),
114 new Rectangle(origin.x,
115 origin.y,
116 sampleModel.getWidth(),
117 sampleModel.getHeight()),
118 origin,
119 null);
120 }
121
122 /**
123 * Constructs a IntegerComponentRaster with the given SampleModel
124 * and DataBuffer. The Raster's upper left corner is origin and
125 * it is the same sizes the SampleModel. The DataBuffer is not
126 * initialized and must be a DataBufferInt compatible with SampleModel.
127 * SampleModel must be of type SinglePixelPackedSampleModel.
128 * @param sampleModel The SampleModel that specifies the layout.
129 * @param dataBuffer The DataBufferInt that contains the image data.
130 * @param origin The Point that specifies the origin.
131 */
132 public IntegerComponentRaster(SampleModel sampleModel,
133 DataBuffer dataBuffer,
134 Point origin) {
135 this(sampleModel,
136 dataBuffer,
137 new Rectangle(origin.x,
138 origin.y,
139 sampleModel.getWidth(),
140 sampleModel.getHeight()),
141 origin,
142 null);
143 }
144
145 /**
146 * Constructs a IntegerComponentRaster with the given SampleModel,
147 * DataBuffer, and parent. DataBuffer must be a DataBufferInt and
148 * SampleModel must be of type SinglePixelPackedSampleModel.
149 * When translated into the base Raster's
150 * coordinate system, aRegion must be contained by the base Raster.
151 * Origin is the coodinate in the new Raster's coordinate system of
152 * the origin of the base Raster. (The base Raster is the Raster's
153 * ancestor which has no parent.)
154 *
155 * Note that this constructor should generally be called by other
156 * constructors or create methods, it should not be used directly.
157 * @param sampleModel The SampleModel that specifies the layout.
158 * @param dataBuffer The DataBufferInt that contains the image data.
159 * @param aRegion The Rectangle that specifies the image area.
160 * @param origin The Point that specifies the origin.
161 * @param parent The parent (if any) of this raster.
162 */
163 public IntegerComponentRaster(SampleModel sampleModel,
164 DataBuffer dataBuffer,
165 Rectangle aRegion,
166 Point origin,
167 IntegerComponentRaster parent){
168 super(sampleModel,dataBuffer,aRegion,origin,parent);
169 this.maxX = minX + width;
170 this.maxY = minY + height;
171 if (!(dataBuffer instanceof DataBufferInt)) {
172 throw new RasterFormatException("IntegerComponentRasters must have" +
173 "integer DataBuffers");
174 }
175 DataBufferInt dbi = (DataBufferInt)dataBuffer;
176 if (dbi.getNumBanks() != 1) {
177 throw new
178 RasterFormatException("DataBuffer for IntegerComponentRasters"+
179 " must only have 1 bank.");
180 }
181 this.data = stealData(dbi, 0);
182
183 if (sampleModel instanceof SinglePixelPackedSampleModel) {
184 SinglePixelPackedSampleModel sppsm =
185 (SinglePixelPackedSampleModel)sampleModel;
186 int[] boffsets = sppsm.getBitOffsets();
187 boolean notByteBoundary = false;
188 for (int i=1; i < boffsets.length; i++) {
189 if ((boffsets[i]%8) != 0) {
190 notByteBoundary = true;
191 }
192 }
193 this.type = (notByteBoundary
194 ? IntegerComponentRaster.TYPE_INT_PACKED_SAMPLES
195 : IntegerComponentRaster.TYPE_INT_8BIT_SAMPLES);
196
197 this.scanlineStride = sppsm.getScanlineStride();
198 this.pixelStride = 1;
199 this.dataOffsets = new int[1];
200 this.dataOffsets[0] = dbi.getOffset();
201 this.bandOffset = this.dataOffsets[0];
202 int xOffset = aRegion.x - origin.x;
203 int yOffset = aRegion.y - origin.y;
204 dataOffsets[0] += xOffset+yOffset*scanlineStride;
205 this.numDataElems = sppsm.getNumDataElements();
206 } else {
207 throw new RasterFormatException("IntegerComponentRasters must have"+
208 " SinglePixelPackedSampleModel");
209 }
210
211 verify();
212 }
213
214
215 /**
216 * Returns a copy of the data offsets array. For each band the data offset
217 * is the index into the band's data array, of the first sample of the
218 * band.
219 */
220 public int[] getDataOffsets() {
552 throw new RasterFormatException("y lies outside raster");
553 }
554 if ((x+width < x) || (x+width > this.minX + this.width)) {
555 throw new RasterFormatException("(x + width) is outside raster");
556 }
557 if ((y+height < y) || (y+height > this.minY + this.height)) {
558 throw new RasterFormatException("(y + height) is outside raster");
559 }
560
561 SampleModel sm;
562
563 if (bandList != null)
564 sm = sampleModel.createSubsetSampleModel(bandList);
565 else
566 sm = sampleModel;
567
568 int deltaX = x0 - x;
569 int deltaY = y0 - y;
570
571 return new IntegerComponentRaster(sm,
572 dataBuffer,
573 new Rectangle(x0,y0,width,height),
574 new Point(sampleModelTranslateX+deltaX,
575 sampleModelTranslateY+deltaY),
576 this);
577 }
578
579
580 /**
581 * Creates a subraster given a region of the raster. The x and y
582 * coordinates specify the horizontal and vertical offsets
583 * from the upper-left corner of this raster to the upper-left corner
584 * of the subraster. A subset of the bands of the parent raster may
585 * be specified. If this is null, then all the bands are present in the
586 * subRaster. Note that the subraster will reference the same
587 * DataBuffer as the parent raster, but using different offsets.
588 * @param x X offset.
589 * @param y Y offset.
590 * @param width Width (in pixels) of the subraster.
591 * @param height Height (in pixels) of the subraster.
592 * @param x0 Translated X origin of the subRaster.
| 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.SinglePixelPackedSampleModel;
32 import java.awt.image.DataBufferInt;
33 import java.awt.Rectangle;
34 import java.awt.Point;
35
36 /**
37 * This class defines a Raster with pixels consisting of one or more 32-bit
38 * data elements stored in close proximity to each other in a integer array.
39 * The bit precision per data element is that
40 * of the data type (that is, the bit precision for this raster is 32).
41 * There is only one pixel stride and one scanline stride for all
42 * bands. For a given pixel, all samples fit in N data elements and these
43 * N data elements hold samples for only one pixel. This type of Raster
44 * can be used with a PackedColorModel.
45 * <p>
46 * For example, if there is only one data element per pixel, a
47 * SinglePixelPackedSampleModel can be used to represent multiple
48 * bands with a PackedColorModel (including a DirectColorModel) for
49 * color interpretation.
50 *
51 */
89
90 /** A cached copy of minY + height for use in bounds checks. */
91 private int maxY;
92
93 private static native void initIDs();
94 static {
95 /* ensure that the necessary native libraries are loaded */
96 NativeLibLoader.loadLibraries();
97 initIDs();
98 }
99
100 /**
101 * Constructs a IntegerComponentRaster with the given SampleModel.
102 * The Raster's upper left corner is origin and it is the same
103 * size as the SampleModel. A DataBuffer large enough to describe the
104 * Raster is automatically created. SampleModel must be of type
105 * SinglePixelPackedSampleModel.
106 * @param sampleModel The SampleModel that specifies the layout.
107 * @param origin The Point that specified the origin.
108 */
109 public IntegerComponentRaster(SampleModel sampleModel, Point origin) {
110 this(sampleModel,
111 (DataBufferInt) sampleModel.createDataBuffer(),
112 new Rectangle(origin.x,
113 origin.y,
114 sampleModel.getWidth(),
115 sampleModel.getHeight()),
116 origin,
117 null);
118 }
119
120 /**
121 * Constructs a IntegerComponentRaster with the given SampleModel
122 * and DataBuffer. The Raster's upper left corner is origin and
123 * it is the same sizes the SampleModel. The DataBuffer is not
124 * initialized and must be a DataBufferInt compatible with SampleModel.
125 * SampleModel must be of type SinglePixelPackedSampleModel.
126 * @param sampleModel The SampleModel that specifies the layout.
127 * @param dataBuffer The DataBufferInt that contains the image data.
128 * @param origin The Point that specifies the origin.
129 */
130 public IntegerComponentRaster(SampleModel sampleModel,
131 DataBufferInt dataBuffer,
132 Point origin)
133 {
134 this(sampleModel,
135 dataBuffer,
136 new Rectangle(origin.x,
137 origin.y,
138 sampleModel.getWidth(),
139 sampleModel.getHeight()),
140 origin,
141 null);
142 }
143
144 /**
145 * Constructs a IntegerComponentRaster with the given SampleModel,
146 * DataBuffer, and parent. DataBuffer must be a DataBufferInt and
147 * SampleModel must be of type SinglePixelPackedSampleModel.
148 * When translated into the base Raster's
149 * coordinate system, aRegion must be contained by the base Raster.
150 * Origin is the coodinate in the new Raster's coordinate system of
151 * the origin of the base Raster. (The base Raster is the Raster's
152 * ancestor which has no parent.)
153 *
154 * Note that this constructor should generally be called by other
155 * constructors or create methods, it should not be used directly.
156 * @param sampleModel The SampleModel that specifies the layout.
157 * @param dataBuffer The DataBufferInt that contains the image data.
158 * @param aRegion The Rectangle that specifies the image area.
159 * @param origin The Point that specifies the origin.
160 * @param parent The parent (if any) of this raster.
161 */
162 public IntegerComponentRaster(SampleModel sampleModel,
163 DataBufferInt dataBuffer,
164 Rectangle aRegion,
165 Point origin,
166 IntegerComponentRaster parent)
167 {
168 super(sampleModel,dataBuffer,aRegion,origin,parent);
169 this.maxX = minX + width;
170 this.maxY = minY + height;
171
172 if (dataBuffer.getNumBanks() != 1) {
173 throw new
174 RasterFormatException("DataBuffer for IntegerComponentRasters"+
175 " must only have 1 bank.");
176 }
177 this.data = stealData(dataBuffer, 0);
178
179 if (sampleModel instanceof SinglePixelPackedSampleModel) {
180 SinglePixelPackedSampleModel sppsm =
181 (SinglePixelPackedSampleModel)sampleModel;
182 int[] boffsets = sppsm.getBitOffsets();
183 boolean notByteBoundary = false;
184 for (int i=1; i < boffsets.length; i++) {
185 if ((boffsets[i]%8) != 0) {
186 notByteBoundary = true;
187 }
188 }
189 this.type = (notByteBoundary
190 ? IntegerComponentRaster.TYPE_INT_PACKED_SAMPLES
191 : IntegerComponentRaster.TYPE_INT_8BIT_SAMPLES);
192
193 this.scanlineStride = sppsm.getScanlineStride();
194 this.pixelStride = 1;
195 this.dataOffsets = new int[1];
196 this.dataOffsets[0] = dataBuffer.getOffset();
197 this.bandOffset = this.dataOffsets[0];
198 int xOffset = aRegion.x - origin.x;
199 int yOffset = aRegion.y - origin.y;
200 dataOffsets[0] += xOffset+yOffset*scanlineStride;
201 this.numDataElems = sppsm.getNumDataElements();
202 } else {
203 throw new RasterFormatException("IntegerComponentRasters must have"+
204 " SinglePixelPackedSampleModel");
205 }
206
207 verify();
208 }
209
210
211 /**
212 * Returns a copy of the data offsets array. For each band the data offset
213 * is the index into the band's data array, of the first sample of the
214 * band.
215 */
216 public int[] getDataOffsets() {
548 throw new RasterFormatException("y lies outside raster");
549 }
550 if ((x+width < x) || (x+width > this.minX + this.width)) {
551 throw new RasterFormatException("(x + width) is outside raster");
552 }
553 if ((y+height < y) || (y+height > this.minY + this.height)) {
554 throw new RasterFormatException("(y + height) is outside raster");
555 }
556
557 SampleModel sm;
558
559 if (bandList != null)
560 sm = sampleModel.createSubsetSampleModel(bandList);
561 else
562 sm = sampleModel;
563
564 int deltaX = x0 - x;
565 int deltaY = y0 - y;
566
567 return new IntegerComponentRaster(sm,
568 (DataBufferInt) dataBuffer,
569 new Rectangle(x0,y0,width,height),
570 new Point(sampleModelTranslateX+deltaX,
571 sampleModelTranslateY+deltaY),
572 this);
573 }
574
575
576 /**
577 * Creates a subraster given a region of the raster. The x and y
578 * coordinates specify the horizontal and vertical offsets
579 * from the upper-left corner of this raster to the upper-left corner
580 * of the subraster. A subset of the bands of the parent raster may
581 * be specified. If this is null, then all the bands are present in the
582 * subRaster. Note that the subraster will reference the same
583 * DataBuffer as the parent raster, but using different offsets.
584 * @param x X offset.
585 * @param y Y offset.
586 * @param width Width (in pixels) of the subraster.
587 * @param height Height (in pixels) of the subraster.
588 * @param x0 Translated X origin of the subRaster.
|