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];
191 this.dataOffsets[0] = dbOffset;
192 int xOffset = aRegion.x - origin.x;
193 int yOffset = aRegion.y - origin.y;
194 dataOffsets[0] += xOffset+yOffset*scanlineStride;
195 } else {
196 throw new RasterFormatException("ShortComponentRasters must have"+
197 "ComponentSampleModel or SinglePixelPackedSampleModel");
198 }
199 this.bandOffset = this.dataOffsets[0];
200
201 verify(false);
202 }
203
204 /**
205 * Returns a copy of the data offsets array. For each band the data offset
206 * is the index into the band's data array, of the first sample of the
207 * band.
208 */
209 public int[] getDataOffsets() {
210 return (int[]) dataOffsets.clone();
211 }
212
213 /**
214 * Returns the data offset for the specified band. The data offset
215 * is the index into the data array in which the first sample
216 * of the first scanline is stored.
217 * @param band The band whose offset is returned.
218 */
219 public int getDataOffset(int band) {
220 return dataOffsets[band];
221 }
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 }
782
783 /**
784 * Creates a Raster with the same layout and the same
785 * width and height, and with new zeroed data arrays. If
786 * the Raster is a subRaster, this will call
787 * createCompatibleRaster(width, height).
788 */
789 public WritableRaster createCompatibleWritableRaster() {
790 return createCompatibleWritableRaster(width,height);
791 }
792
793 /**
794 * Verify that the layout parameters are consistent with
795 * the data. If strictCheck
796 * is false, this method will check for ArrayIndexOutOfBounds conditions. If
797 * strictCheck is true, this method will check for additional error
798 * conditions such as line wraparound (width of a line greater than
799 * the scanline stride).
800 * @return String Error string, if the layout is incompatible with
801 * the data. Otherwise returns null.
802 */
803 private void verify (boolean strictCheck) {
804 // Make sure data for Raster is in a legal range
805 for (int i=0; i < dataOffsets.length; i++) {
806 if (dataOffsets[i] < 0) {
807 throw new RasterFormatException("Data offsets for band "+i+
808 "("+dataOffsets[i]+
809 ") must be >= 0");
810 }
811 }
812
813 int maxSize = 0;
814 int size;
815
816 for (int i=0; i < numDataElements; i++) {
817 size = (height-1)*scanlineStride + (width-1)*pixelStride +
818 dataOffsets[i];
819 if (size > maxSize) {
820 maxSize = size;
821 }
822 }
823 if (data.length < maxSize) {
824 throw new RasterFormatException("Data array too small (should be "+
825 maxSize+" )");
826 }
827 }
828
829 public String toString() {
830 return new String ("ShortComponentRaster: width = "+width
831 +" height = " + height
832 +" #numDataElements "+numDataElements);
833 // +" xOff = "+xOffset+" yOff = "+yOffset);
834 }
835
836 }
|
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];
191 this.dataOffsets[0] = dbOffset;
192 int xOffset = aRegion.x - origin.x;
193 int yOffset = aRegion.y - origin.y;
194 dataOffsets[0] += xOffset+yOffset*scanlineStride;
195 } else {
196 throw new RasterFormatException("ShortComponentRasters must have"+
197 "ComponentSampleModel or SinglePixelPackedSampleModel");
198 }
199 this.bandOffset = this.dataOffsets[0];
200
201 verify();
202 }
203
204 /**
205 * Returns a copy of the data offsets array. For each band the data offset
206 * is the index into the band's data array, of the first sample of the
207 * band.
208 */
209 public int[] getDataOffsets() {
210 return (int[]) dataOffsets.clone();
211 }
212
213 /**
214 * Returns the data offset for the specified band. The data offset
215 * is the index into the data array in which the first sample
216 * of the first scanline is stored.
217 * @param band The band whose offset is returned.
218 */
219 public int getDataOffset(int band) {
220 return dataOffsets[band];
221 }
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 }
782
783 /**
784 * Creates a Raster with the same layout and the same
785 * width and height, and with new zeroed data arrays. If
786 * the Raster is a subRaster, this will call
787 * createCompatibleRaster(width, height).
788 */
789 public WritableRaster createCompatibleWritableRaster() {
790 return createCompatibleWritableRaster(width,height);
791 }
792
793 /**
794 * Verify that the layout parameters are consistent with the data.
795 *
796 * The method verifies whether scanline stride and pixel stride do not
797 * cause an integer overflow during calculation of a position of the pixel
798 * in data buffer. It also verifies whether the data buffer has enough data
799 * to correspond the raster layout attributes.
800 *
801 * @throws RasterFormatException if an integer overflow is detected,
802 * or if data buffer has not enough capacity.
803 */
804 protected final void verify() {
805 for (int i = 0; i < dataOffsets.length; i++) {
806 if (dataOffsets[i] < 0) {
807 throw new RasterFormatException("Data offsets for band " + i
808 + "(" + dataOffsets[i]
809 + ") must be >= 0");
810 }
811 }
812
813 int maxSize = 0;
814 int size;
815
816 // we can be sure that width and height are greater than 0
817 if (scanlineStride < 0 ||
818 scanlineStride > (Integer.MAX_VALUE / height))
819 {
820 // integer overflow
821 throw new RasterFormatException("Incorrect scanline stride: "
822 + scanlineStride);
823 }
824 int lastScanOffset = (height - 1) * scanlineStride;
825
826 if (pixelStride < 0 ||
827 pixelStride > (Integer.MAX_VALUE / width))
828 {
829 // integer overflow
830 throw new RasterFormatException("Incorrect pixel stride: "
831 + pixelStride);
832 }
833 int lastPixelOffset = (width - 1) * pixelStride;
834
835 if (lastPixelOffset > (Integer.MAX_VALUE - lastScanOffset)) {
836 // integer overflow
837 throw new RasterFormatException("Incorrect raster attributes");
838 }
839 lastPixelOffset += lastScanOffset;
840
841 for (int i = 0; i < numDataElements; i++) {
842 size = lastPixelOffset + dataOffsets[i];
843 if (dataOffsets[i] > (Integer.MAX_VALUE - lastPixelOffset)) {
844 throw new RasterFormatException("Incorrect band offset: "
845 + dataOffsets[i]);
846 }
847
848 if (size > maxSize) {
849 maxSize = size;
850 }
851 }
852 if (data.length < maxSize) {
853 throw new RasterFormatException("Data array too small (should be "
854 + maxSize + " )");
855 }
856 }
857
858 public String toString() {
859 return new String ("ShortComponentRaster: width = "+width
860 +" height = " + height
861 +" #numDataElements "+numDataElements);
862 // +" xOff = "+xOffset+" yOff = "+yOffset);
863 }
864
865 }
|