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.BandedSampleModel;
32 import java.awt.image.DataBuffer;
33 import java.awt.image.DataBufferByte;
34 import java.awt.Rectangle;
35 import java.awt.Point;
36
37 /**
38 * This class defines a Raster with pixels consisting of multiple
39 * 8-bit samples stored in possibly separate arrays for each band.
40 * Operations on sets of pixels are performed on a given band in the
41 * Raster before moving on to the next band. The arrays used for
42 * storage may be distinct or shared between some or all of the bands.
43 * Each band additionally has an offset that is added to determine the
44 * DataBuffer location of each pixel.
45 *
46 * There is only one scanline stride for all bands. The pixel stride
47 * is always equal to one. This type of raster can be used with a
48 * ComponentColorModel. This class requires a BandedSampleModel.
49 *
50 */
51 public class ByteBandedRaster extends SunWritableRaster {
52
57 int scanlineStride;
58
59 /** The image data array. */
60 byte[][] data;
61
62 /** A cached copy of minX + width for use in bounds checks. */
63 private int maxX;
64
65 /** A cached copy of minY + height for use in bounds checks. */
66 private int maxY;
67
68 /**
69 * Constructs a ByteBandedRaster with the given sampleModel. The
70 * Raster's upper left corner is origin and it is the same
71 * size as the SampleModel. A dataBuffer large
72 * enough to describe the Raster is automatically created. SampleModel
73 * must be of type BandedSampleModel.
74 * @param sampleModel The SampleModel that specifies the layout.
75 * @param origin The Point that specifies the origin.
76 */
77 public ByteBandedRaster(SampleModel sampleModel,
78 Point origin) {
79 this(sampleModel,
80 sampleModel.createDataBuffer(),
81 new Rectangle(origin.x,
82 origin.y,
83 sampleModel.getWidth(),
84 sampleModel.getHeight()),
85 origin,
86 null);
87 }
88
89 /**
90 * Constructs a ByteBanded Raster with the given sampleModel
91 * and DataBuffer. The Raster's upper left corner is origin and
92 * it is the same size as the SampleModel. The DataBuffer is not
93 * initialized and must be a DataBufferShort compatible with SampleModel.
94 * SampleModel must be of type BandedSampleModel.
95 * @param sampleModel The SampleModel that specifies the layout.
96 * @param dataBuffer The DataBufferShort that contains the image data.
97 * @param origin The Point that specifies the origin.
98 */
99 public ByteBandedRaster(SampleModel sampleModel,
100 DataBuffer dataBuffer,
101 Point origin) {
102 this(sampleModel, dataBuffer,
103 new Rectangle(origin.x , origin.y,
104 sampleModel.getWidth(),
105 sampleModel.getHeight()),
106 origin, null);
107 }
108
109 /**
110 * Constructs a ByteBandedRaster with the given sampleModel,
111 * DataBuffer, and parent. DataBuffer must be a DataBufferShort and
112 * SampleModel must be of type BandedSampleModel.
113 * When translated into the base Raster's
114 * coordinate system, aRegion must be contained by the base Raster.
115 * Origin is the coordinate in the new Raster's coordinate system of
116 * the origin of the base Raster. (The base Raster is the Raster's
117 * ancestor which has no parent.)
118 *
119 * Note that this constructor should generally be called by other
120 * constructors or create methods, it should not be used directly.
121 * @param sampleModel The SampleModel that specifies the layout.
122 * @param dataBuffer The DataBufferShort that contains the image data.
123 * @param aRegion The Rectangle that specifies the image area.
124 * @param origin The Point that specifies the origin.
125 * @param parent The parent (if any) of this raster.
126 */
127 public ByteBandedRaster(SampleModel sampleModel,
128 DataBuffer dataBuffer,
129 Rectangle aRegion,
130 Point origin,
131 ByteBandedRaster parent) {
132
133 super(sampleModel, dataBuffer, aRegion, origin, parent);
134 this.maxX = minX + width;
135 this.maxY = minY + height;
136
137 if (!(dataBuffer instanceof DataBufferByte)) {
138 throw new RasterFormatException("ByteBandedRaster must have" +
139 "byte DataBuffers");
140 }
141 DataBufferByte dbb = (DataBufferByte)dataBuffer;
142
143 if (sampleModel instanceof BandedSampleModel) {
144 BandedSampleModel bsm = (BandedSampleModel)sampleModel;
145 this.scanlineStride = bsm.getScanlineStride();
146 int bankIndices[] = bsm.getBankIndices();
147 int bandOffsets[] = bsm.getBandOffsets();
148 int dOffsets[] = dbb.getOffsets();
149 dataOffsets = new int[bankIndices.length];
150 data = new byte[bankIndices.length][];
151 int xOffset = aRegion.x - origin.x;
152 int yOffset = aRegion.y - origin.y;
153 for (int i = 0; i < bankIndices.length; i++) {
154 data[i] = stealData(dbb, bankIndices[i]);
155 dataOffsets[i] = dOffsets[bankIndices[i]] +
156 xOffset + yOffset*scanlineStride + bandOffsets[i];
157 }
158 } else {
159 throw new RasterFormatException("ByteBandedRasters must have"+
160 "BandedSampleModels");
161 }
162 verify();
163 }
164
165
166 /**
167 * Returns a copy of the data offsets array. For each band the data
168 * offset is the index into the band's data array, of the first sample
169 * of the band.
170 */
171 public int[] getDataOffsets() {
172 return dataOffsets.clone();
173 }
174
655 throw new RasterFormatException("y lies outside raster");
656 }
657 if ((x+width < x) || (x+width > this.width + this.minX)) {
658 throw new RasterFormatException("(x + width) is outside raster") ;
659 }
660 if ((y+height < y) || (y+height > this.height + this.minY)) {
661 throw new RasterFormatException("(y + height) is outside raster");
662 }
663
664 SampleModel sm;
665
666 if (bandList != null)
667 sm = sampleModel.createSubsetSampleModel(bandList);
668 else
669 sm = sampleModel;
670
671 int deltaX = x0 - x;
672 int deltaY = y0 - y;
673
674 return new ByteBandedRaster(sm,
675 dataBuffer,
676 new Rectangle(x0,y0,width,height),
677 new Point(sampleModelTranslateX+deltaX,
678 sampleModelTranslateY+deltaY),
679 this);
680 }
681
682 /**
683 * Creates a subraster given a region of the raster. The x and y
684 * coordinates specify the horizontal and vertical offsets
685 * from the upper-left corner of this raster to the upper-left corner
686 * of the subraster. A subset of the bands of the parent Raster may
687 * be specified. If this is null, then all the bands are present in the
688 * subRaster. A translation to the subRaster may also be specified.
689 * Note that the subraster will reference the same
690 * DataBuffers as the parent raster, but using different offsets.
691 * @param x X offset.
692 * @param y Y offset.
693 * @param width Width (in pixels) of the subraster.
694 * @param height Height (in pixels) of the subraster.
695 * @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.BandedSampleModel;
32 import java.awt.image.DataBufferByte;
33 import java.awt.Rectangle;
34 import java.awt.Point;
35
36 /**
37 * This class defines a Raster with pixels consisting of multiple
38 * 8-bit samples stored in possibly separate arrays for each band.
39 * Operations on sets of pixels are performed on a given band in the
40 * Raster before moving on to the next band. The arrays used for
41 * storage may be distinct or shared between some or all of the bands.
42 * Each band additionally has an offset that is added to determine the
43 * DataBuffer location of each pixel.
44 *
45 * There is only one scanline stride for all bands. The pixel stride
46 * is always equal to one. This type of raster can be used with a
47 * ComponentColorModel. This class requires a BandedSampleModel.
48 *
49 */
50 public class ByteBandedRaster extends SunWritableRaster {
51
56 int scanlineStride;
57
58 /** The image data array. */
59 byte[][] data;
60
61 /** A cached copy of minX + width for use in bounds checks. */
62 private int maxX;
63
64 /** A cached copy of minY + height for use in bounds checks. */
65 private int maxY;
66
67 /**
68 * Constructs a ByteBandedRaster with the given sampleModel. The
69 * Raster's upper left corner is origin and it is the same
70 * size as the SampleModel. A dataBuffer large
71 * enough to describe the Raster is automatically created. SampleModel
72 * must be of type BandedSampleModel.
73 * @param sampleModel The SampleModel that specifies the layout.
74 * @param origin The Point that specifies the origin.
75 */
76 public ByteBandedRaster(SampleModel sampleModel, Point origin) {
77 this(sampleModel,
78 (DataBufferByte) sampleModel.createDataBuffer(),
79 new Rectangle(origin.x,
80 origin.y,
81 sampleModel.getWidth(),
82 sampleModel.getHeight()),
83 origin,
84 null);
85 }
86
87 /**
88 * Constructs a ByteBanded Raster with the given sampleModel
89 * and DataBuffer. The Raster's upper left corner is origin and
90 * it is the same size as the SampleModel. The DataBuffer is not
91 * initialized and must be a DataBufferShort compatible with SampleModel.
92 * SampleModel must be of type BandedSampleModel.
93 * @param sampleModel The SampleModel that specifies the layout.
94 * @param dataBuffer The DataBufferByte that contains the image data.
95 * @param origin The Point that specifies the origin.
96 */
97 public ByteBandedRaster(SampleModel sampleModel,
98 DataBufferByte dataBuffer,
99 Point origin)
100 {
101 this(sampleModel, dataBuffer,
102 new Rectangle(origin.x , origin.y,
103 sampleModel.getWidth(),
104 sampleModel.getHeight()),
105 origin, null);
106 }
107
108 /**
109 * Constructs a ByteBandedRaster with the given sampleModel,
110 * DataBuffer, and parent. DataBuffer must be a DataBufferShort and
111 * SampleModel must be of type BandedSampleModel.
112 * When translated into the base Raster's
113 * coordinate system, aRegion must be contained by the base Raster.
114 * Origin is the coordinate in the new Raster's coordinate system of
115 * the origin of the base Raster. (The base Raster is the Raster's
116 * ancestor which has no parent.)
117 *
118 * Note that this constructor should generally be called by other
119 * constructors or create methods, it should not be used directly.
120 * @param sampleModel The SampleModel that specifies the layout.
121 * @param dataBuffer The DataBufferByte that contains the image data.
122 * @param aRegion The Rectangle that specifies the image area.
123 * @param origin The Point that specifies the origin.
124 * @param parent The parent (if any) of this raster.
125 */
126 public ByteBandedRaster(SampleModel sampleModel,
127 DataBufferByte dataBuffer,
128 Rectangle aRegion,
129 Point origin,
130 ByteBandedRaster parent)
131 {
132 super(sampleModel, dataBuffer, aRegion, origin, parent);
133 this.maxX = minX + width;
134 this.maxY = minY + height;
135
136 if (sampleModel instanceof BandedSampleModel) {
137 BandedSampleModel bsm = (BandedSampleModel)sampleModel;
138 this.scanlineStride = bsm.getScanlineStride();
139 int bankIndices[] = bsm.getBankIndices();
140 int bandOffsets[] = bsm.getBandOffsets();
141 int dOffsets[] = dataBuffer.getOffsets();
142 dataOffsets = new int[bankIndices.length];
143 data = new byte[bankIndices.length][];
144 int xOffset = aRegion.x - origin.x;
145 int yOffset = aRegion.y - origin.y;
146 for (int i = 0; i < bankIndices.length; i++) {
147 data[i] = stealData(dataBuffer, bankIndices[i]);
148 dataOffsets[i] = dOffsets[bankIndices[i]] +
149 xOffset + yOffset*scanlineStride + bandOffsets[i];
150 }
151 } else {
152 throw new RasterFormatException("ByteBandedRasters must have"+
153 "BandedSampleModels");
154 }
155 verify();
156 }
157
158
159 /**
160 * Returns a copy of the data offsets array. For each band the data
161 * offset is the index into the band's data array, of the first sample
162 * of the band.
163 */
164 public int[] getDataOffsets() {
165 return dataOffsets.clone();
166 }
167
648 throw new RasterFormatException("y lies outside raster");
649 }
650 if ((x+width < x) || (x+width > this.width + this.minX)) {
651 throw new RasterFormatException("(x + width) is outside raster") ;
652 }
653 if ((y+height < y) || (y+height > this.height + this.minY)) {
654 throw new RasterFormatException("(y + height) is outside raster");
655 }
656
657 SampleModel sm;
658
659 if (bandList != null)
660 sm = sampleModel.createSubsetSampleModel(bandList);
661 else
662 sm = sampleModel;
663
664 int deltaX = x0 - x;
665 int deltaY = y0 - y;
666
667 return new ByteBandedRaster(sm,
668 (DataBufferByte) dataBuffer,
669 new Rectangle(x0,y0,width,height),
670 new Point(sampleModelTranslateX+deltaX,
671 sampleModelTranslateY+deltaY),
672 this);
673 }
674
675 /**
676 * Creates a subraster given a region of the raster. The x and y
677 * coordinates specify the horizontal and vertical offsets
678 * from the upper-left corner of this raster to the upper-left corner
679 * of the subraster. A subset of the bands of the parent Raster may
680 * be specified. If this is null, then all the bands are present in the
681 * subRaster. A translation to the subRaster may also be specified.
682 * Note that the subraster will reference the same
683 * DataBuffers as the parent raster, but using different offsets.
684 * @param x X offset.
685 * @param y Y offset.
686 * @param width Width (in pixels) of the subraster.
687 * @param height Height (in pixels) of the subraster.
688 * @param x0 Translated X origin of the subraster.
|