Sometimes my images are too big and I get this error:
Exception in thread "main" java.lang.NegativeArraySizeException at java.awt.image.DataBufferByte.(Unknown Source) at java.awt.image.Raster.createInterleavedRaster(Unknown Source) at java.awt.image.BufferedImage.(Unknown Source)
Whenever I get this I want to adjust my image to the highest possible size while maintaining the ratio.
I ended up with the following formulas:
if ( targetWidth * targetHeight >= Integer.MAX_VALUE || targetWidth * targetHeight < 0 ) {
System.out.println( "Target image too big... Size will be adjusted!" );
if ( targetWidth > targetHeight ) {
targetWidth = (int)Math.sqrt( ( Integer.MAX_VALUE ) * (float)( targetWidth / targetHeight ) );
targetHeight = ( Integer.MAX_VALUE ) / targetWidth;
} else {
targetHeight = (int)Math.sqrt( ( Integer.MAX_VALUE ) * (float)( targetHeight / targetWidth ) );
targetWidth = ( Integer.MAX_VALUE ) / targetHeight;
}
}
I still get the same problem, and my conditions are satisfied. I guess that
WIDTH * HEIGHT < Integer.MAX_VALUE
Is clearly not the condition I am looking for Any help?
Edit: After some discussion I think the real question to this problem is: What is the biggest possible size that I can pass to the BufferedImage constructor in order to not get a NegativeArraySizeException at:
at java.awt.image.DataBufferByte.(Unknown Source)