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, Point origin) {
70 this(sampleModel,
71 (DataBufferInt) sampleModel.createDataBuffer(),
72 new Rectangle(origin.x,
73 origin.y,
74 sampleModel.getWidth(),
75 sampleModel.getHeight()),
76 origin,
77 null);
78 }
79
80 /**
81 * Constructs a IntegerInterleavedRaster with the given SampleModel
82 * and DataBuffer. The Raster's upper left corner is origin and
83 * it is the same sizes the SampleModel. The DataBuffer is not
84 * initialized and must be a DataBufferInt compatible with SampleModel.
85 * SampleModel must be of type SinglePixelPackedSampleModel.
86 * @param sampleModel The SampleModel that specifies the layout.
87 * @param dataBuffer The DataBufferInt that contains the image data.
88 * @param origin The Point that specifies the origin.
89 */
90 public IntegerInterleavedRaster(SampleModel sampleModel,
91 DataBufferInt dataBuffer,
92 Point origin)
93 {
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 {
128 super(sampleModel,dataBuffer,aRegion,origin,parent);
129 this.maxX = minX + width;
130 this.maxY = minY + height;
131
132 this.data = stealData(dataBuffer, 0);
133
134 if (sampleModel instanceof SinglePixelPackedSampleModel) {
135 SinglePixelPackedSampleModel sppsm =
136 (SinglePixelPackedSampleModel)sampleModel;
137 this.scanlineStride = sppsm.getScanlineStride();
138 this.pixelStride = 1;
139 this.dataOffsets = new int[1];
140 this.dataOffsets[0] = dataBuffer.getOffset();
141 this.bandOffset = this.dataOffsets[0];
142 int xOffset = aRegion.x - origin.x;
143 int yOffset = aRegion.y - origin.y;
144 dataOffsets[0] += xOffset+yOffset*scanlineStride;
145 this.numDataElems = sppsm.getNumDataElements();
146 } else {
147 throw new RasterFormatException("IntegerInterleavedRasters must have"+
148 " SinglePixelPackedSampleModel");
149 }
150 verify();
151 }
152
153
154 /**
155 * Returns a copy of the data offsets array. For each band the data offset
156 * is the index into the band's data array, of the first sample of the
157 * band.
158 */
159 public int[] getDataOffsets() {
160 return dataOffsets.clone();
460 throw new RasterFormatException("y lies outside raster");
461 }
462 if ((x+width < x) || (x+width > this.minX + this.width)) {
463 throw new RasterFormatException("(x + width) is outside raster");
464 }
465 if ((y+height < y) || (y+height > this.minY + this.height)) {
466 throw new RasterFormatException("(y + height) is outside raster");
467 }
468
469 SampleModel sm;
470
471 if (bandList != null)
472 sm = sampleModel.createSubsetSampleModel(bandList);
473 else
474 sm = sampleModel;
475
476 int deltaX = x0 - x;
477 int deltaY = y0 - y;
478
479 return new IntegerInterleavedRaster(sm,
480 (DataBufferInt) dataBuffer,
481 new Rectangle(x0,y0,width,height),
482 new Point(sampleModelTranslateX+deltaX,
483 sampleModelTranslateY+deltaY),
484 this);
485 }
486
487
488 /**
489 * Creates a subraster given a region of the raster. The x and y
490 * coordinates specify the horizontal and vertical offsets
491 * from the upper-left corner of this raster to the upper-left corner
492 * of the subraster. A subset of the bands of the parent raster may
493 * be specified. If this is null, then all the bands are present in the
494 * subRaster. Note that the subraster will reference the same
495 * DataBuffer as the parent raster, but using different offsets.
496 * @param x X offset.
497 * @param y Y offset.
498 * @param width Width (in pixels) of the subraster.
499 * @param height Height (in pixels) of the subraster.
500 * @param x0 Translated X origin of the subRaster.
|