diff --git a/src/java.desktop/share/classes/java/awt/image/Raster.java b/src/java.desktop/share/classes/java/awt/image/Raster.java index 9f346cb304a..053e7c1eec5 100644 --- a/src/java.desktop/share/classes/java/awt/image/Raster.java +++ b/src/java.desktop/share/classes/java/awt/image/Raster.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -265,9 +265,15 @@ public class Raster { * {@code location.x + w} or * {@code location.y + h} results in integer overflow * @throws IllegalArgumentException if {@code scanlineStride} - * is less than 0 - * @throws IllegalArgumentException if {@code pixelStride} is less than 0 + * is less than or equal to 0 + * @throws IllegalArgumentException if {@code pixelStride} is less than or equal to 0 + * @throws IllegalArgumentException if {@code w * pixelStride} is greater + * than {@code scanlineStride} + * @throws IllegalArgumentException if the data size need to store all + * lines of the image is greater than {@code Integer.MAX_VALUE} * @throws NullPointerException if {@code bandOffsets} is null + * @throws IllegalArgumentException if any element of {@code bandOffsets} is greater + * than {@code pixelStride} or the {@code scanlineStride} */ public static WritableRaster createInterleavedRaster(int dataType, int w, int h, @@ -285,14 +291,25 @@ public class Raster { throw new IllegalArgumentException("Dimensions (width="+w+ " height="+h+") are too large"); } - if (pixelStride < 0) { - throw new IllegalArgumentException("pixelStride is < 0"); + if (pixelStride <= 0) { + throw new IllegalArgumentException("pixelStride is <= 0"); } - if (scanlineStride < 0) { - throw new IllegalArgumentException("scanlineStride is < 0"); + if (scanlineStride <= 0) { + throw new IllegalArgumentException("scanlineStride is <= 0"); } - int size = scanlineStride * (h - 1) + // first (h - 1) scans - pixelStride * w; // last scan + if (bandOffsets == null) { + throw new NullPointerException("bandOffsets is null"); + } + lsz = (long)w * pixelStride; + if (lsz > scanlineStride) { + throw new IllegalArgumentException("w * pixelStride is too large"); + } + lsz = (long)scanlineStride * (long)(h - 1) + // first (h - 1) scans + (long)pixelStride * (long)w; // last scan + if (lsz > Integer.MAX_VALUE) { + throw new IllegalArgumentException("size too large to store image"); + } + int size = (int)lsz; if (location == null) { location = new Point(0, 0); @@ -415,6 +432,10 @@ public class Raster { * is less than 0 * @throws ArrayIndexOutOfBoundsException if {@code bankIndices} * is {@code null} + * @throws IllegalArgumentException if the lengths of {@code bankIndices} + * and {@code bandOffsets} are different. + * @throws IllegalArgumentException if the data size need to store all + * lines of a bank of the image is greater than {@code Integer.MAX_VALUE} * @throws NullPointerException if {@code bandOffsets} is {@code null} */ public static WritableRaster createBandedRaster(int dataType, @@ -423,9 +444,6 @@ public class Raster { int[] bankIndices, int[] bandOffsets, Point location) { - DataBuffer d; - int bands = bandOffsets.length; - if (w <= 0 || h <= 0) { throw new IllegalArgumentException("w and h must be positive"); } @@ -440,7 +458,11 @@ public class Raster { } if (bandOffsets == null) { throw new - ArrayIndexOutOfBoundsException("Band offsets array is null"); + NullPointerException("Band offsets array is null"); + } + if (bandOffsets.length != bankIndices.length) { + throw new IllegalArgumentException( + "bankIndices.length != bandOffsets.length"); } if (location != null) { if ((w + location.getX() > Integer.MAX_VALUE) || @@ -451,6 +473,9 @@ public class Raster { } } + DataBuffer d; + int bands = bandOffsets.length; + // Figure out the #banks and the largest band offset int maxBank = bankIndices[0]; int maxBandOff = bandOffsets[0]; @@ -463,9 +488,15 @@ public class Raster { } } int banks = maxBank + 1; - int size = maxBandOff + - scanlineStride * (h - 1) + // first (h - 1) scans - w; // last scan + + lsz = (long) maxBandOff + + (long)scanlineStride * (h - 1) + // first (h - 1) scans + w; // last scan + if (lsz > Integer.MAX_VALUE) { + throw new IllegalArgumentException("storage size is too large"); + } + + int size = (int)lsz; switch(dataType) { case DataBuffer.TYPE_BYTE: @@ -508,11 +539,14 @@ public class Raster { * @param location the upper-left corner of the {@code Raster} * @return a WritableRaster object with the specified data type, * width, height, and band masks. - * @throws RasterFormatException if {@code w} or {@code h} - * is less than or equal to zero, or computing either + * @throws NullPointerException if {@code bandMasks} is null + * @throws IllegalArgumentException if {@code w} and {@code h} + * are not both greater than 0 + * @throws IllegalArgumentException if the product of {@code w} + * and {@code h} is greater than {@code Integer.MAX_VALUE} + * @throws RasterFormatException if computing either * {@code location.x + w} or - * {@code location.y + h} results in integer - * overflow + * {@code location.y + h} results in integer overflow * @throws IllegalArgumentException if {@code dataType} is not * one of the supported data types, which are * {@code DataBuffer.TYPE_BYTE}, @@ -525,6 +559,24 @@ public class Raster { Point location) { DataBuffer d; + if (w <= 0 || h <= 0) { + throw new IllegalArgumentException("w and h must be positive"); + } + long lsz = (long)w * h; + if (lsz > Integer.MAX_VALUE) { + throw new IllegalArgumentException("Dimensions (width="+w+ + " height="+h+") are too large"); + } + + if (location != null) { + if ((w + location.getX() > Integer.MAX_VALUE) || + (h + location.getY() > Integer.MAX_VALUE)) { + throw new RasterFormatException( + "location.x + w and location.y + h " + + " cannot exceed Integer.MAX_VALUE"); + } + } + switch(dataType) { case DataBuffer.TYPE_BYTE: d = new DataBufferByte(w*h); @@ -573,17 +625,19 @@ public class Raster { * @param location the upper-left corner of the {@code Raster} * @return a WritableRaster object with the specified data type, * width, height, number of bands, and bits per band. - * @throws RasterFormatException if {@code w} or {@code h} - * is less than or equal to zero, or computing either - * {@code location.x + w} or - * {@code location.y + h} results in integer - * overflow + * @throws IllegalArgumentException if {@code bitsPerBand} or + * {@code bands} is not greater than zero * @throws IllegalArgumentException if the product of * {@code bitsPerBand} and {@code bands} is * greater than the number of bits held by * {@code dataType} - * @throws IllegalArgumentException if {@code bitsPerBand} or - * {@code bands} is not greater than zero + * @throws IllegalArgumentException if {@code w} and {@code h} + * are not both greater than 0 + * @throws IllegalArgumentException if the product of {@code w} + * and {@code h} is greater than {@code Integer.MAX_VALUE} + * @throws RasterFormatException if computing either + * {@code location.x + w} or + * {@code location.y + h} results in integer overflow * @throws IllegalArgumentException if {@code dataType} is not * one of the supported data types, which are * {@code DataBuffer.TYPE_BYTE}, @@ -607,18 +661,37 @@ public class Raster { ") must be greater than 0"); } + if (w <= 0 || h <= 0) { + throw new IllegalArgumentException("w and h must be positive"); + } + long lsz = (long)w * h; + if (lsz > Integer.MAX_VALUE) { + throw new IllegalArgumentException("Dimensions (width="+w+ + " height="+h+") are too large"); + } + + if (location != null) { + if ((w + location.getX() > Integer.MAX_VALUE) || + (h + location.getY() > Integer.MAX_VALUE)) { + throw new RasterFormatException( + "location.x + w and location.y + h " + + " cannot exceed Integer.MAX_VALUE"); + } + } + + int shift = (bands-1)*bitsPerBand; + + /* Make sure the total mask size will fit in the data type */ + if (shift+bitsPerBand > DataBuffer.getDataTypeSize(dataType)) { + throw new IllegalArgumentException("bitsPerBand("+ + bitsPerBand+") * bands is "+ + " greater than data type "+ + "size."); + } if (bands != 1) { int[] masks = new int[bands]; int mask = (1 << bitsPerBand) - 1; - int shift = (bands-1)*bitsPerBand; - /* Make sure the total mask size will fit in the data type */ - if (shift+bitsPerBand > DataBuffer.getDataTypeSize(dataType)) { - throw new IllegalArgumentException("bitsPerBand("+ - bitsPerBand+") * bands is "+ - " greater than data type "+ - "size."); - } switch(dataType) { case DataBuffer.TYPE_BYTE: case DataBuffer.TYPE_USHORT: @@ -693,6 +766,7 @@ public class Raster { * {@code DataBuffer.TYPE_USHORT}. * @throws RasterFormatException if {@code dataBuffer} has more * than one bank. + * @throws RasterFormatException if {@code dataBuffer} is too small. * @throws IllegalArgumentException if {@code w} and {@code h} are not * both > 0 * @throws IllegalArgumentException if the product of {@code w} @@ -704,6 +778,8 @@ public class Raster { * is less than 0 * @throws IllegalArgumentException if {@code pixelStride} is less than 0 * @throws NullPointerException if {@code bandOffsets} is null + * @throws IllegalArgumentException if any element of {@code bandOffsets} is greater + * than {@code pixelStride} or the {@code scanlineStride} */ public static WritableRaster createInterleavedRaster(DataBuffer dataBuffer, @@ -779,6 +855,10 @@ public class Raster { * bank indices and band offsets. * @throws NullPointerException if {@code dataBuffer} is null, * or {@code bankIndices} is null, or {@code bandOffsets} is null + * @throws IllegalArgumentException if the lengths of {@code bankIndices} + * and {@code bandOffsets} are different. + * @throws ArrayIndexOutOfBoundsException if any element of {@code bankIndices} + * is greater or equal to the number of bands in {@code dataBuffer} * @throws IllegalArgumentException if {@code dataType} is not * one of the supported data types, which are * {@code DataBuffer.TYPE_BYTE}, @@ -883,11 +963,14 @@ public class Raster { * @return a WritableRaster object with the specified * {@code DataBuffer}, width, height, scanline stride, * and band masks. - * @throws RasterFormatException if {@code w} or {@code h} - * is less than or equal to zero, or computing either + * @throws NullPointerException if {@code bandMasks} is null + * @throws IllegalArgumentException if {@code w} and {@code h} + * are not both greater than 0 + * @throws IllegalArgumentException if the product of {@code w} + * and {@code h} is greater than {@code Integer.MAX_VALUE} + * @throws RasterFormatException if computing either * {@code location.x + w} or - * {@code location.y + h} results in integer - * overflow + * {@code location.y + h} results in integer overflow * @throws IllegalArgumentException if {@code dataBuffer} is not * one of the supported data types, which are * {@code DataBuffer.TYPE_BYTE}, @@ -906,6 +989,25 @@ public class Raster { if (dataBuffer == null) { throw new NullPointerException("DataBuffer cannot be null"); } + + if (w <= 0 || h <= 0) { + throw new IllegalArgumentException("w and h must be positive"); + } + long lsz = (long)w * h; + if (lsz > Integer.MAX_VALUE) { + throw new IllegalArgumentException("Dimensions (width="+w+ + " height="+h+") are too large"); + } + + if (location != null) { + if ((w + location.getX() > Integer.MAX_VALUE) || + (h + location.getY() > Integer.MAX_VALUE)) { + throw new RasterFormatException( + "location.x + w and location.y + h " + + " cannot exceed Integer.MAX_VALUE"); + } + } + if (location == null) { location = new Point(0,0); } @@ -960,11 +1062,13 @@ public class Raster { * @return a WritableRaster object with the specified * {@code DataBuffer}, width, height, and * bits per pixel. - * @throws RasterFormatException if {@code w} or {@code h} - * is less than or equal to zero, or computing either + * @throws IllegalArgumentException if {@code w} and {@code h} + * are not both greater than 0 + * @throws IllegalArgumentException if the product of {@code w} + * and {@code h} is greater than {@code Integer.MAX_VALUE} + * @throws RasterFormatException if computing either * {@code location.x + w} or - * {@code location.y + h} results in integer - * overflow + * {@code location.y + h} results in integer overflow * @throws IllegalArgumentException if {@code dataType} is not * one of the supported data types, which are * {@code DataBuffer.TYPE_BYTE}, @@ -972,6 +1076,8 @@ public class Raster { * or {@code DataBuffer.TYPE_INT} * @throws RasterFormatException if {@code dataBuffer} has more * than one bank. + * @throws RasterFormatException if {@code bitsPixel} is less than 1 or + * not a power of 2 or exceeds the {@code dataBuffer} element size. * @throws NullPointerException if {@code dataBuffer} is null */ public static WritableRaster createPackedRaster(DataBuffer dataBuffer, @@ -982,6 +1088,25 @@ public class Raster { if (dataBuffer == null) { throw new NullPointerException("DataBuffer cannot be null"); } + if (w <= 0 || h <= 0) { + throw new IllegalArgumentException("w and h must be positive"); + } + long lsz = (long)w * h; + + if (lsz > Integer.MAX_VALUE) { + throw new IllegalArgumentException("Dimensions (width="+w+ + " height="+h+") are too large"); + } + + if (location != null) { + if ((w + location.getX() > Integer.MAX_VALUE) || + (h + location.getY() > Integer.MAX_VALUE)) { + throw new RasterFormatException( + "location.x + w and location.y + h " + + " cannot exceed Integer.MAX_VALUE"); + } + } + if (location == null) { location = new Point(0,0); } @@ -1000,6 +1125,12 @@ public class Raster { " must only have 1 bank."); } + if ((bitsPerPixel < 1) || (bitsPerPixel > DataBuffer.getDataTypeSize(dataType))) { + // NB MPPSM checks power of 2 condition + throw new + RasterFormatException("bitsPerPixel must be > 0 and a power of 2 that " + + "does not exceed data buffer element size"); + } MultiPixelPackedSampleModel mppsm = new MultiPixelPackedSampleModel(dataType, w, h, bitsPerPixel); diff --git a/test/jdk/java/awt/image/Raster/CreateRasterExceptionTest.java b/test/jdk/java/awt/image/Raster/CreateRasterExceptionTest.java index defef9b3b65..d89de44401b 100644 --- a/test/jdk/java/awt/image/Raster/CreateRasterExceptionTest.java +++ b/test/jdk/java/awt/image/Raster/CreateRasterExceptionTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,7 +23,7 @@ /* * @test - * @bug 8255800 + * @bug 8255800 8369129 * @summary verify Raster + SampleModel creation vs spec. */ @@ -42,26 +42,40 @@ public class CreateRasterExceptionTest { static int[] negBankIndices = new int[] { -1, 0}; static int[] bandOffsets = new int[] { 0, 0}; static int[] bandOffsets2 = new int[] { 0, 0, 0, 0}; + static int[] bandMasks1 = new int[] { 0x0ff }; static int[] zeroBandOffsets = new int[] {}; static DataBuffer dBuffer = new DataBufferByte(15); + static DataBuffer dBuffer1 = new DataBufferByte(1); static void noException() { Thread.dumpStack(); throw new RuntimeException("No expected exception"); } - /* Except a version starting with "17" or higher */ - static void checkIsOldVersion(Throwable t) { + /** + * If running on a JDK of the targetVersion or later, throw + * a RuntimeException because the exception argument + * should not have occured. However it is expected on + * prior versions because that was the previous behaviour. + * @param targetVersion to check + * @param t the thrown exception to print + */ + static void checkIsOldVersion(int targetVersion, Throwable t) { String version = System.getProperty("java.version"); version = version.split("\\D")[0]; int v = Integer.parseInt(version); - if (v >= 17) { + if (v >= targetVersion) { t.printStackTrace(); throw new RuntimeException( "Unexpected exception for version " + v); } } + /* Except a version starting with "17" or higher */ + static void checkIsOldVersion(Throwable t) { + checkIsOldVersion(17, t); + } + public static void main(String[] args) { componentSampleModelTests1(); componentSampleModelTests2(); @@ -73,6 +87,10 @@ public class CreateRasterExceptionTest { interleavedRasterTests1(); interleavedRasterTests2(); interleavedRasterTests3(); + packedRasterTests1(); + packedRasterTests2(); + packedRasterTests3(); + packedRasterTests4(); System.out.println(); System.out.println(" ** Test Passed **"); } @@ -734,7 +752,7 @@ public class CreateRasterExceptionTest { /* @throws ArrayIndexOutOfBoundsException if * {@code bankIndices} is null */ - Raster.createBandedRaster(DataBuffer.TYPE_INT, 1, 1, 0, + Raster.createBandedRaster(DataBuffer.TYPE_INT, 1, 1, 1, null, bandOffsets, null); noException(); } catch (ArrayIndexOutOfBoundsException t) { @@ -747,7 +765,7 @@ public class CreateRasterExceptionTest { /* @throws NullPointerException if {@code bandOffsets} * is null */ - Raster.createBandedRaster(DataBuffer.TYPE_INT, 1, 1, 0, + Raster.createBandedRaster(DataBuffer.TYPE_INT, 1, 1, 1, bankIndices, null, null); noException(); } catch (NullPointerException t) { @@ -756,6 +774,21 @@ public class CreateRasterExceptionTest { System.out.println(t); } + try { + /* @throws IllegalArgumentException if the lengths of {@code bankIndices} + * and {@code bandOffsets} are different. + */ + Raster.createBandedRaster(DataBuffer.TYPE_INT, 1, 1, 1, + bankIndices, bandOffsets2, null); + noException(); + } catch (ArrayIndexOutOfBoundsException t) { + checkIsOldVersion(26, t); + } catch (IllegalArgumentException t) { + System.out.println( + "Got expected exception for different array lengths"); + System.out.println(t); + } + try { /* @throws IllegalArgumentException if {@code dataType} is * not one of the supported data types for this sample model @@ -894,6 +927,21 @@ public class CreateRasterExceptionTest { System.out.println(t); } + try { + /* @throws ArrayIndexOutOfBoundsException if any element of {@code bankIndices} + * is greater or equal to the number of bands in {@code dataBuffer} + */ + int[] indices = new int[] { 0, 1, 2 }; + int[] offsets = new int[] { 0, 0, 0 }; + Raster.createBandedRaster(dBuffer, 1, 1, 1, + indices, offsets, null); + noException(); + } catch (ArrayIndexOutOfBoundsException t) { + System.out.println( + "Got expected exception for bad bank index"); + System.out.println(t); + } + try { /* * @throws IllegalArgumentException if {@code dataType} @@ -1045,6 +1093,25 @@ public class CreateRasterExceptionTest { System.out.println(t); } + try { + /* @throws IllegalArgumentException if the data size + * needs to store all lines is greater than + * {@code Integer.MAX_VALUE} + */ + Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, + 1000, 1000, + Integer.MAX_VALUE/2 , 1, + bandOffsets, null); + noException(); + } catch (IllegalArgumentException t) { + System.out.println("Got expected exception for overflow"); + System.out.println(t); + } catch (NegativeArraySizeException t) { + checkIsOldVersion(26, t); + System.out.println("Got expected exception for overflow"); + System.out.println(t); + } + try { /* @throws RasterFormatException if computing either * {@code location.x + w} or @@ -1062,10 +1129,10 @@ public class CreateRasterExceptionTest { try { /* @throws IllegalArgumentException if - * {@code scanlineStride} is less than 0 + * {@code scanlineStride} is less than or equal to 0 */ Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, - 1, 1, -3, 1, bandOffsets, null); + 1, 1, 0, 1, bandOffsets, null); noException(); } catch (IllegalArgumentException t) { System.out.println( @@ -1075,15 +1142,17 @@ public class CreateRasterExceptionTest { try { /* @throws IllegalArgumentException if {@code pixelStride} - * is less than 0 + * is less than or equal to 0 */ Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, - 1, 1, 3, -1, bandOffsets, null); + 1, 1, 3, 0, bandOffsets, null); noException(); } catch (IllegalArgumentException t) { System.out.println( "Got expected exception for pixelStride < 0"); System.out.println(t); + } catch (RasterFormatException t) { + checkIsOldVersion(26, t); } catch (NegativeArraySizeException t) { checkIsOldVersion(t); System.out.println( @@ -1091,12 +1160,25 @@ public class CreateRasterExceptionTest { System.out.println(t); } + try { + /* @throws IllegalArgumentException if (w * pixelStride) + * is greater than scanlineStride + */ + Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, + 1, 1, 0, 1, bandOffsets, null); + noException(); + } catch (IllegalArgumentException t) { + System.out.println( + "Got expected exception for incorrect stride"); + System.out.println(t); + } + try { /* @throws NullPointerException if {@code bandOffsets} * is null */ Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, - 1, 1, 0, 1, null, null); + 1, 1, 1, 1, null, null); noException(); } catch (NullPointerException t) { System.out.println( @@ -1187,6 +1269,18 @@ public class CreateRasterExceptionTest { System.out.println(t); } + try { + /* @throws RasterFormatException if {@code dataBuffer} is too small. + */ + Raster.createInterleavedRaster(dBuffer1, 5, 1, 15, 1, + bandOffsets, null); + noException(); + } catch (RasterFormatException t) { + System.out.println( + "Got expected exception for databuffer too small"); + System.out.println(t); + } + try { /* @throws IllegalArgumentException if {@code pixelStride} * is less than 0 @@ -1199,7 +1293,6 @@ public class CreateRasterExceptionTest { "Got expected exception for pixelStride < 0"); System.out.println(t); } catch (NegativeArraySizeException t) { -if (t != null) throw t; checkIsOldVersion(t); System.out.println( "Got expected exception for pixelStride < 0"); @@ -1242,9 +1335,398 @@ if (t != null) throw t; bandOffsets, null); noException(); } catch (RasterFormatException t) { + System.out.println( + "Got expected exception for bad databuffer banks"); + System.out.println(t); + } + + try { + /* @throws IllegalArgumentException if any element of {@code bandOffsets} is greater + * than {@code pixelStride} or the {@code scanlineStride} + */ + int[] offsets = new int[] { 0, 1, 2}; + Raster.createInterleavedRaster(dBuffer, + 1, 1, 1, 1, offsets, null); + noException(); + } catch (IllegalArgumentException t) { + System.out.println( + "Got expected exception for element too large"); + System.out.println(t); + } + + } + + /* createPackedRaster(int dataType, + * int w, int h, + * int[] bandMasks, + * Point location) + * + */ + static void packedRasterTests1() { + + System.out.println(); + System.out.println("** packedRasterTests1"); + + try { + /* @throws IllegalArgumentException if {@code w} and {@code h} + * are not both greater than 0 + */ + Raster.createPackedRaster(DataBuffer.TYPE_BYTE, 0, 0, bandMasks1, null); + noException(); + } catch (IllegalArgumentException t) { + System.out.println( + "Got expected exception for zero w / h"); + System.out.println(t); + } + + try { + /* @throws IllegalArgumentException if {@code w} * {@code h} + * is greater than {@code Integer.MAX_VALUE} + */ + Raster.createPackedRaster(DataBuffer.TYPE_BYTE, + Integer.MAX_VALUE/10, Integer.MAX_VALUE/10, + bandMasks1, null); + noException(); + } catch (IllegalArgumentException t) { + System.out.println( + "Got expected exception for overflow"); + System.out.println(t); + } + + try { + /* @throws RasterFormatException if computing either + * {@code location.x + w} or + * {@code location.y + h} results in integer overflow + */ + Point pt = new Point(5, 1); + Raster.createPackedRaster(DataBuffer.TYPE_BYTE, + Integer.MAX_VALUE-2, 1, + bandMasks1, pt); + noException(); + } catch (RasterFormatException t) { + System.out.println("Got expected exception for overflow"); + System.out.println(t); + } + + try { + /* @throws IllegalArgumentException if {@code dataType} + * is not one of the supported data types + */ + Raster.createPackedRaster(1000, 1, 1, bandMasks1, null); + noException(); + } catch (IllegalArgumentException t) { + System.out.println( + "Got expected exception for bad databuffer type"); + System.out.println(t); + } + } + + /* createPackedRaster(int dataType, + * int w, int h, + * int bands, + * int bitsPerBand, + * Point location) + */ + static void packedRasterTests2() { + + System.out.println(); + System.out.println("** packedRasterTests2"); + + try { + /* @throws IllegalArgumentException if {@code w} and {@code h} + * are not both greater than 0 + */ + Raster.createPackedRaster(DataBuffer.TYPE_BYTE, 0, 0, 1, 8, null); + noException(); + } catch (IllegalArgumentException t) { + System.out.println( + "Got expected exception for zero w / h"); + System.out.println(t); + } + + try { + /* @throws IllegalArgumentException if {@code w} * {@code h} + * is greater than {@code Integer.MAX_VALUE} + */ + Raster.createPackedRaster(DataBuffer.TYPE_BYTE, + Integer.MAX_VALUE/10, Integer.MAX_VALUE/10, + 1, 8, null); + noException(); + } catch (IllegalArgumentException t) { + System.out.println( + "Got expected exception for overflow"); + System.out.println(t); + } + + try { + /* @throws RasterFormatException if computing either + * {@code location.x + w} or + * {@code location.y + h} results in integer overflow + */ + Point pt = new Point(5, 1); + Raster.createPackedRaster(DataBuffer.TYPE_BYTE, + Integer.MAX_VALUE-2, 1, + 1, 8, pt); + noException(); + } catch (RasterFormatException t) { + System.out.println("Got expected exception for overflow"); + System.out.println(t); + } + + try { + /* @throws IllegalArgumentException if {@code bitsPerBand} or + * {@code bands} is not greater than zero + */ + Raster.createPackedRaster(DataBuffer.TYPE_BYTE, 1, 1, + 0, 8, null); + noException(); + } catch (IllegalArgumentException t) { + System.out.println( + "Got expected exception for 0 bands"); + System.out.println(t); + } + + try { + /* @throws IllegalArgumentException if {@code bitsPerBand} or + * {@code bands} is not greater than zero + */ + Raster.createPackedRaster(DataBuffer.TYPE_BYTE, 1, 1, + 8, 0, null); + noException(); + } catch (IllegalArgumentException t) { + System.out.println( + "Got expected exception for 0 bitsPerBand"); + System.out.println(t); + } + + try { + /* @throws IllegalArgumentException if the product of + * {@code bitsPerBand} and {@code bands} is + * greater than the number of bits held by + * {@code dataType} + */ + Raster.createPackedRaster(DataBuffer.TYPE_BYTE, 1, 1, + 2, 8, null); + noException(); + } catch (IllegalArgumentException t) { + System.out.println( + "Got expected exception for bands per sample"); + System.out.println(t); + } + + try { + /* @throws IllegalArgumentException if {@code dataType} + * is not one of the supported data types + */ + Raster.createPackedRaster(1000, 1, 1, 1, 8, null); + noException(); + } catch (IllegalArgumentException t) { System.out.println( "Got expected exception for bad databuffer type"); System.out.println(t); } } + + /* createPackedRaster(DataBuffer dataBuffer, + * int w, int h, + * int scanlineStride, + * int[] bandMasks, + * Point location) + */ + static void packedRasterTests3() { + + System.out.println(); + System.out.println("** packedRasterTests3"); + + try { + /* @throws IllegalArgumentException if {@code w} and {@code h} + * are not both greater than 0 + */ + Raster.createPackedRaster(dBuffer, 0, 1, 1, bandMasks1, null); + noException(); + } catch (IllegalArgumentException t) { + System.out.println( + "Got expected exception for zero w / h"); + System.out.println(t); + } + + try { + /* @throws IllegalArgumentException if {@code w} * {@code h} + * is greater than {@code Integer.MAX_VALUE} + */ + Raster.createPackedRaster(dBuffer, + Integer.MAX_VALUE/10, Integer.MAX_VALUE/10, + 1, bandMasks1, null); + noException(); + } catch (IllegalArgumentException t) { + System.out.println( + "Got expected exception for overflow"); + System.out.println(t); + } + + try { + /* @throws RasterFormatException if computing either + * {@code location.x + w} or + * {@code location.y + h} results in integer overflow + */ + Point pt = new Point(5, 1); + Raster.createPackedRaster(dBuffer, + Integer.MAX_VALUE-2, 1, + 1, bandMasks1, pt); + noException(); + } catch (RasterFormatException t) { + System.out.println("Got expected exception for overflow"); + System.out.println(t); + } + + try { + /* @throws NullPointerException if databuffer is null. + */ + Raster.createPackedRaster(null, + 1, 1, + 1, bandMasks1, null); + noException(); + } catch (NullPointerException t) { + System.out.println( + "Got expected exception for null data buffer"); + System.out.println(t); + } + + try { + /* @throws RasterFormatException if {@code dataBuffer} + * has more than one bank. + */ + DataBufferByte dbuffer2 = new DataBufferByte(20, 2); + Raster.createPackedRaster(dbuffer2, 1, 1, 1, bandMasks1, null); + noException(); + } catch (RasterFormatException t) { + System.out.println( + "Got expected exception for bad databuffer banks"); + System.out.println(t); + } + try { + /* @throws IllegalArgumentException if {@code dataBuffer} + * is not one of the supported data types + */ + DataBufferFloat dbFloat = new DataBufferFloat(20); + Raster.createPackedRaster(dbFloat, 1, 1, 1, bandMasks1, null); + noException(); + } catch (IllegalArgumentException t) { + System.out.println( + "Got expected exception for bad databuffer type"); + System.out.println(t); + } + } + + /* createPackedRaster(DataBuffer dataBuffer, + * int w, int h, + * int bitsPerPixel, + * Point location) + */ + static void packedRasterTests4() { + + System.out.println(); + System.out.println("** packedRasterTests4"); + + try { + /* @throws IllegalArgumentException if {@code w} and {@code h} + * are not both greater than 0 + */ + Raster.createPackedRaster(dBuffer, 0, 1, 8, null); + noException(); + } catch (IllegalArgumentException t) { + System.out.println( + "Got expected exception for zero w / h"); + System.out.println(t); + } + + try { + /* @throws IllegalArgumentException if {@code w} * {@code h} + * is greater than {@code Integer.MAX_VALUE} + */ + Raster.createPackedRaster(dBuffer, + Integer.MAX_VALUE/10, Integer.MAX_VALUE/10, + 8, null); + noException(); + } catch (IllegalArgumentException t) { + System.out.println( + "Got expected exception for overflow"); + System.out.println(t); + } + + try { + /* @throws RasterFormatException if computing either + * {@code location.x + w} or + * {@code location.y + h} results in integer overflow + */ + Point pt = new Point(5, 1); + Raster.createPackedRaster(dBuffer, + Integer.MAX_VALUE-2, 1, + 8, pt); + noException(); + } catch (RasterFormatException t) { + System.out.println("Got expected exception for overflow"); + System.out.println(t); + } + + try { + /* @throws IllegalArgumentException if {@code dataBuffer} + * is not one of the supported data types + */ + DataBufferFloat dbFloat = new DataBufferFloat(20); + Raster.createPackedRaster(dbFloat, 1, 1, 8, null); + noException(); + } catch (IllegalArgumentException t) { + System.out.println( + "Got expected exception for bad databuffer type"); + System.out.println(t); + } + + try { + /* @throws RasterFormatException if {@code dataBuffer} + * has more than one bank. + */ + DataBufferByte dbb = new DataBufferByte(100, 2); + Raster.createPackedRaster(dbb, 1, 1, 8, null); + noException(); + } catch (RasterFormatException t) { + System.out.println( + "Got expected exception for bad databuffer banks"); + System.out.println(t); + } + + try { + /* @throws NullPointerException if databuffer is null. + */ + Raster.createPackedRaster(null, 1, 1, 8, null); + noException(); + } catch (NullPointerException t) { + System.out.println( + "Got expected exception for null data buffer"); + System.out.println(t); + } + + int[] badbpp = { 0, 6, 16 }; + for (int bpp : badbpp) { + try { + /* @throws RasterFormatException if {@code bitsPixel} is less than 1 or + * not a power of 2 or exceeds the {@code dataBuffer} element size. + */ + System.out.println("Test bpp=" + bpp); + Raster.createPackedRaster(dBuffer, 1, 1, bpp, null); + noException(); + } catch (RasterFormatException t) { + System.out.println( + "Got expected exception for bitsPerPixel"); + System.out.println(t); + } catch (ArithmeticException t) { + checkIsOldVersion(26, t); + if (bpp != 0) { + throw new RuntimeException("Unexpected arithmetic exception"); + } + System.out.println("Got expected arithmetic exception"); + System.out.println(t); + } + } + } }