1 /*
2 * Copyright (c) 1998, 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 */
53 public class IntegerInterleavedRaster extends IntegerComponentRaster {
54
55 /** A cached copy of minX + width for use in bounds checks. */
56 private int maxX;
57
58 /** A cached copy of minY + height for use in bounds checks. */
59 private int maxY;
60
61 /**
62 * Constructs a IntegerInterleavedRaster with the given SampleModel.
63 * The Raster's upper left corner is origin and it is the same
64 * size as the SampleModel. A DataBuffer large enough to describe the
65 * Raster is automatically created. SampleModel must be of type
66 * SinglePixelPackedSampleModel.
67 * @param sampleModel The SampleModel that specifies the layout.
68 * @param origin The Point that specified the origin.
69 */
70 public IntegerInterleavedRaster(SampleModel sampleModel,
71 Point origin) {
72 this(sampleModel,
73 sampleModel.createDataBuffer(),
74 new Rectangle(origin.x,
75 origin.y,
76 sampleModel.getWidth(),
77 sampleModel.getHeight()),
78 origin,
79 null);
80 }
81
82 /**
83 * Constructs a IntegerInterleavedRaster with the given SampleModel
84 * and DataBuffer. The Raster's upper left corner is origin and
85 * it is the same sizes the SampleModel. The DataBuffer is not
86 * initialized and must be a DataBufferInt compatible with SampleModel.
87 * SampleModel must be of type SinglePixelPackedSampleModel.
88 * @param sampleModel The SampleModel that specifies the layout.
89 * @param dataBuffer The DataBufferInt that contains the image data.
90 * @param origin The Point that specifies the origin.
91 */
92 public IntegerInterleavedRaster(SampleModel sampleModel,
93 DataBuffer dataBuffer,
94 Point origin) {
95 this(sampleModel,
96 dataBuffer,
97 new Rectangle(origin.x,
98 origin.y,
99 sampleModel.getWidth(),
100 sampleModel.getHeight()),
101 origin,
102 null);
103 }
104
105 /**
106 * Constructs a IntegerInterleavedRaster with the given SampleModel,
107 * DataBuffer, and parent. DataBuffer must be a DataBufferInt and
108 * SampleModel must be of type SinglePixelPackedSampleModel.
109 * When translated into the base Raster's
110 * coordinate system, aRegion must be contained by the base Raster.
111 * Origin is the coodinate in the new Raster's coordinate system of
112 * the origin of the base Raster. (The base Raster is the Raster's
113 * ancestor which has no parent.)
114 *
115 * Note that this constructor should generally be called by other
116 * constructors or create methods, it should not be used directly.
117 * @param sampleModel The SampleModel that specifies the layout.
118 * @param dataBuffer The DataBufferInt that contains the image data.
119 * @param aRegion The Rectangle that specifies the image area.
120 * @param origin The Point that specifies the origin.
121 * @param parent The parent (if any) of this raster.
122 */
123 public IntegerInterleavedRaster(SampleModel sampleModel,
124 DataBuffer dataBuffer,
125 Rectangle aRegion,
126 Point origin,
127 IntegerInterleavedRaster parent){
128 super(sampleModel,dataBuffer,aRegion,origin,parent);
129 this.maxX = minX + width;
130 this.maxY = minY + height;
131 if (!(dataBuffer instanceof DataBufferInt)) {
132 throw new RasterFormatException("IntegerInterleavedRasters must have" +
133 "integer DataBuffers");
134 }
135 DataBufferInt dbi = (DataBufferInt)dataBuffer;
136 this.data = stealData(dbi, 0);
137
138 if (sampleModel instanceof SinglePixelPackedSampleModel) {
139 SinglePixelPackedSampleModel sppsm =
140 (SinglePixelPackedSampleModel)sampleModel;
141 this.scanlineStride = sppsm.getScanlineStride();
142 this.pixelStride = 1;
143 this.dataOffsets = new int[1];
144 this.dataOffsets[0] = dbi.getOffset();
145 this.bandOffset = this.dataOffsets[0];
146 int xOffset = aRegion.x - origin.x;
147 int yOffset = aRegion.y - origin.y;
148 dataOffsets[0] += xOffset+yOffset*scanlineStride;
149 this.numDataElems = sppsm.getNumDataElements();
150 } else {
151 throw new RasterFormatException("IntegerInterleavedRasters must have"+
152 " SinglePixelPackedSampleModel");
153 }
154 verify();
155 }
156
157
158 /**
159 * Returns a copy of the data offsets array. For each band the data offset
160 * is the index into the band's data array, of the first sample of the
161 * band.
162 */
163 public int[] getDataOffsets() {
164 return dataOffsets.clone();
464 throw new RasterFormatException("y lies outside raster");
465 }
466 if ((x+width < x) || (x+width > this.minX + this.width)) {
467 throw new RasterFormatException("(x + width) is outside raster");
468 }
469 if ((y+height < y) || (y+height > this.minY + this.height)) {
470 throw new RasterFormatException("(y + height) is outside raster");
471 }
472
473 SampleModel sm;
474
475 if (bandList != null)
476 sm = sampleModel.createSubsetSampleModel(bandList);
477 else
478 sm = sampleModel;
479
480 int deltaX = x0 - x;
481 int deltaY = y0 - y;
482
483 return new IntegerInterleavedRaster(sm,
484 dataBuffer,
485 new Rectangle(x0,y0,width,height),
486 new Point(sampleModelTranslateX+deltaX,
487 sampleModelTranslateY+deltaY),
488 this);
489 }
490
491
492 /**
493 * Creates a subraster given a region of the raster. The x and y
494 * coordinates specify the horizontal and vertical offsets
495 * from the upper-left corner of this raster to the upper-left corner
496 * of the subraster. A subset of the bands of the parent raster may
497 * be specified. If this is null, then all the bands are present in the
498 * subRaster. Note that the subraster will reference the same
499 * DataBuffer as the parent raster, but using different offsets.
500 * @param x X offset.
501 * @param y Y offset.
502 * @param width Width (in pixels) of the subraster.
503 * @param height Height (in pixels) of the subraster.
504 * @param x0 Translated X origin of the subRaster.
|
1 /*
2 * Copyright (c) 1998, 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 */
52 public class IntegerInterleavedRaster extends IntegerComponentRaster {
53
54 /** A cached copy of minX + width for use in bounds checks. */
55 private int maxX;
56
57 /** A cached copy of minY + height for use in bounds checks. */
58 private int maxY;
59
60 /**
61 * Constructs a IntegerInterleavedRaster with the given SampleModel.
62 * The Raster's upper left corner is origin and it is the same
63 * size as the SampleModel. A DataBuffer large enough to describe the
64 * Raster is automatically created. SampleModel must be of type
65 * SinglePixelPackedSampleModel.
66 * @param sampleModel The SampleModel that specifies the layout.
67 * @param origin The Point that specified the origin.
68 */
69 public IntegerInterleavedRaster(SampleModel sampleModel,
70 Point origin) {
71 this(sampleModel,
72 (DataBufferInt)sampleModel.createDataBuffer(),
73 new Rectangle(origin.x,
74 origin.y,
75 sampleModel.getWidth(),
76 sampleModel.getHeight()),
77 origin,
78 null);
79 }
80
81 /**
82 * Constructs a IntegerInterleavedRaster with the given SampleModel
83 * and DataBuffer. The Raster's upper left corner is origin and
84 * it is the same sizes the SampleModel. The DataBuffer is not
85 * initialized and must be a DataBufferInt compatible with SampleModel.
86 * SampleModel must be of type SinglePixelPackedSampleModel.
87 * @param sampleModel The SampleModel that specifies the layout.
88 * @param dataBuffer The DataBufferInt that contains the image data.
89 * @param origin The Point that specifies the origin.
90 */
91 public IntegerInterleavedRaster(SampleModel sampleModel,
92 DataBufferInt dataBuffer,
93 Point origin) {
94 this(sampleModel,
95 dataBuffer,
96 new Rectangle(origin.x,
97 origin.y,
98 sampleModel.getWidth(),
99 sampleModel.getHeight()),
100 origin,
101 null);
102 }
103
104 /**
105 * Constructs a IntegerInterleavedRaster with the given SampleModel,
106 * DataBuffer, and parent. DataBuffer must be a DataBufferInt and
107 * SampleModel must be of type SinglePixelPackedSampleModel.
108 * When translated into the base Raster's
109 * coordinate system, aRegion must be contained by the base Raster.
110 * Origin is the coodinate in the new Raster's coordinate system of
111 * the origin of the base Raster. (The base Raster is the Raster's
112 * ancestor which has no parent.)
113 *
114 * Note that this constructor should generally be called by other
115 * constructors or create methods, it should not be used directly.
116 * @param sampleModel The SampleModel that specifies the layout.
117 * @param dataBuffer The DataBufferInt that contains the image data.
118 * @param aRegion The Rectangle that specifies the image area.
119 * @param origin The Point that specifies the origin.
120 * @param parent The parent (if any) of this raster.
121 */
122 public IntegerInterleavedRaster(SampleModel sampleModel,
123 DataBufferInt dataBuffer,
124 Rectangle aRegion,
125 Point origin,
126 IntegerInterleavedRaster parent) {
127 super(sampleModel,dataBuffer,aRegion,origin,parent);
128 this.maxX = minX + width;
129 this.maxY = minY + height;
130
131 this.data = stealData(dataBuffer, 0);
132
133 if (sampleModel instanceof SinglePixelPackedSampleModel) {
134 SinglePixelPackedSampleModel sppsm =
135 (SinglePixelPackedSampleModel)sampleModel;
136 this.scanlineStride = sppsm.getScanlineStride();
137 this.pixelStride = 1;
138 this.dataOffsets = new int[1];
139 this.dataOffsets[0] = dataBuffer.getOffset();
140 this.bandOffset = this.dataOffsets[0];
141 int xOffset = aRegion.x - origin.x;
142 int yOffset = aRegion.y - origin.y;
143 dataOffsets[0] += xOffset+yOffset*scanlineStride;
144 this.numDataElems = sppsm.getNumDataElements();
145 } else {
146 throw new RasterFormatException("IntegerInterleavedRasters must have"+
147 " SinglePixelPackedSampleModel");
148 }
149 verify();
150 }
151
152
153 /**
154 * Returns a copy of the data offsets array. For each band the data offset
155 * is the index into the band's data array, of the first sample of the
156 * band.
157 */
158 public int[] getDataOffsets() {
159 return dataOffsets.clone();
459 throw new RasterFormatException("y lies outside raster");
460 }
461 if ((x+width < x) || (x+width > this.minX + this.width)) {
462 throw new RasterFormatException("(x + width) is outside raster");
463 }
464 if ((y+height < y) || (y+height > this.minY + this.height)) {
465 throw new RasterFormatException("(y + height) is outside raster");
466 }
467
468 SampleModel sm;
469
470 if (bandList != null)
471 sm = sampleModel.createSubsetSampleModel(bandList);
472 else
473 sm = sampleModel;
474
475 int deltaX = x0 - x;
476 int deltaY = y0 - y;
477
478 return new IntegerInterleavedRaster(sm,
479 (DataBufferInt)dataBuffer,
480 new Rectangle(x0,y0,width,height),
481 new Point(sampleModelTranslateX+deltaX,
482 sampleModelTranslateY+deltaY),
483 this);
484 }
485
486
487 /**
488 * Creates a subraster given a region of the raster. The x and y
489 * coordinates specify the horizontal and vertical offsets
490 * from the upper-left corner of this raster to the upper-left corner
491 * of the subraster. A subset of the bands of the parent raster may
492 * be specified. If this is null, then all the bands are present in the
493 * subRaster. Note that the subraster will reference the same
494 * DataBuffer as the parent raster, but using different offsets.
495 * @param x X offset.
496 * @param y Y offset.
497 * @param width Width (in pixels) of the subraster.
498 * @param height Height (in pixels) of the subraster.
499 * @param x0 Translated X origin of the subRaster.
|