[OpenJDK 2D-Dev] Relax pixelStride check inside PixelInterleavedSampleModel constructor?

Martin Desruisseaux martin.desruisseaux at geomatys.com
Mon Jul 5 13:43:03 UTC 2021


Hello

PixelInterleavedSampleModel constructor has the following argument check 
at line 100:

    if (pixelStride*w > scanlineStride) {
         throw new IllegalArgumentException("Pixel stride times width must be less than or equal to the scanline stride");
    }

It seems to me that the following check would be more accurate:

    if (pixelStride*(w-1) + maxBandOff >= scanlineStride) {
         ...
    }

Rational: consider a subsampling operation where a new 
PixelInterleavedSampleModel is created for an image with a subset of the 
pixels of the original image. This is an operation similar to the 
existing PixelInterleavedSampleModel.createSubsetModel(int[] bands) 
method, which allows to create a view without coyping the DataBuffer 
content. Consider the following subsampling factors:

    sourceXSubsampling = 5
    sourceYSubsampling = 1

Consider an image of size 16 × 3 pixels with a single band. In the 
illustration below, "X" and "-" are pixels from the source images and 
"X" are pixels retained in the subsampled image. Note that the last 
column of the source image is included in the subsampled image; it is 
important for this issue.

    X----X----X----X
    X----X----X----X
    X----X----X----X

A subsampled image view can be created with a 
PixelInterleavedSampleModel having a pixel stride of 5, a width of 4 and 
everything else identical, in particular a scanline stride of 16. It 
works well in the general case. But for the case illustrated above, an 
IllegalArgumentException is thrown. It can be seen from the following 
condition:

    pixelStride*w > scanlineStride
               5*4 > 16
                20 > 16
                true -> IllegalArgumentException

Above condition is equivalent to requiring image to be like that:

    X----X----X----X----

Instead of:

    X----X----X----X

The amended check proposed at the beginning of this email checks if 
there is enough room for storing the last sample value of a row, 
ignoring the remaining of pixel stride that are just skipped. The check 
in this case become:

    pixelStride*(w-1) + maxBandOff >= scanlineStride
    5*(4-1) + 0 >= 16
    15 >= 16
    false -> no exception thrown

I can create a pull request, but before spending time on it I would like 
to know if this is considered worth fixing?

     Regards,

         Martin


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/2d-dev/attachments/20210705/ece6858a/attachment.htm>


More information about the 2d-dev mailing list