RFR: 4617681: constructor of BufferedImage throws unexpected IllegalArgumentException
Jayathirth D V
jdv at openjdk.org
Wed Oct 29 13:31:23 UTC 2025
On Tue, 14 Oct 2025 16:17:49 GMT, Phil Race <prr at openjdk.org> wrote:
>>> Do you have a conformant sample implementation that we could consider ?
>>
>> We already have an implementation of BandedSampleModel and buffers that store color components in separate bands (different arrays). Similarly, we can implement a new subclass of ComponentSampleModel that stores "x lines of the image per bank".
>> And it should be possible to reuse an existed api of buffers like: https://github.com/openjdk/jdk/blob/4ca4485e9af10a49ca95710c4e26aa3895835d47/src/java.desktop/share/classes/java/awt/image/DataBufferInt.java#L254
>>
>> Initially these images will the slowest loops, but we can add some new here and there.
>
> BandedSampleModel is not precluded by the current proposed wording, in fact it is explicitly accommodated.
>
> The spec. for BandedSampleModel has this as its first sentence :
> This class represents image data which is stored in a band interleaved fashion and for which each sample of a pixel occupies one data element of the DataBuffer
>
> I had actually thought already about the way you suggest with splitting an image so that different parts of it
> are in different data buffers. But that seems nearly impossible. There's too many things in the spec. and
> implementation that would need revisision. I see no value in diluting the wording to allow an impossibility.
> If we ever did that (unlikely) then revising these few words in the spec. would be an insignificant part of it.
>
> Essentially the proposed spec is saying is "array length imposes a hard limit".
>
> So I do not see any problem with the spec as proposed.
I see that none of the code path under this API uses BandedSampleModel.
We can continue to create DataBuffer to hold >Integer.MAX_VALUE and use it to create a Raster with BandedSampleModel and then create a BufferedImage out of it.
import java.awt.Transparency;
import java.awt.color.ColorSpace;
import java.awt.image.BandedSampleModel;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.ComponentColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
public class BandedBufferedImage {
public static void main (String[] args) {
int width = 1;
int height = Integer.MAX_VALUE - 5;
int numBands = 3; // For RGB
int dataType = DataBuffer.TYPE_BYTE; // 8-bit per band
// Create arrays for bank indices and band offsets
int[] bankIndices = new int[numBands];
int[] bandOffsets = new int[numBands];
for (int i = 0; i < numBands; i++) {
bankIndices[i] = i; // Each band in its own bank
bandOffsets[i] = 0; // Offset within each bank
}
BandedSampleModel sampleModel = new BandedSampleModel(
dataType, width, height, width, bankIndices, bandOffsets
);
DataBuffer dataBuffer = new DataBufferByte(width * height, numBands);
WritableRaster raster = Raster.createWritableRaster(sampleModel, dataBuffer, null);
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
int[] bits = {8, 8, 8}; // 8 bits per color component (R, G, B)
ColorModel colorModel = new ComponentColorModel(cs, bits, false, false, Transparency.OPAQUE, dataType);
BufferedImage bufferedImage = new BufferedImage(colorModel, raster, false, null);
}
}
So there are no restrictions on these values. Even if someone extends ComponentSampleModel and divides data into separate bands they can continue to do so.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/27640#discussion_r2473056310
More information about the client-libs-dev
mailing list