[prev in list] [next in list] [prev in thread] [next in thread] 

List:       openjdk-2d-dev
Subject:    [OpenJDK 2D-Dev] Relax pixelStride check inside PixelInterleavedSampleModel constructor?
From:       Martin Desruisseaux <martin.desruisseaux () geomatys ! com>
Date:       2021-07-05 13:43:03
Message-ID: 0e6a7f51-14e0-2402-83f4-b5e23790831b () geomatys ! com
[Download RAW message or body]

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


[Attachment #3 (text/html)]

<html>
  <head>

    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>Hello</p>
    <p><font face="monospace">PixelInterleavedSampleModel</font>
      constructor has the following argument check at line 100:</p>
    <blockquote>
      <pre>if (pixelStride*w &gt; scanlineStride) {
    throw new IllegalArgumentException("Pixel stride times width must be less than or \
equal to the scanline stride"); }
</pre>
    </blockquote>
    <p>It seems to me that the following check would be more accurate:</p>
    <blockquote>
      <pre>if (pixelStride*(w-1) + maxBandOff &gt;= scanlineStride) {
    ...
}
</pre>
    </blockquote>
    <p>Rational: consider a subsampling operation where a new <font
        face="monospace">PixelInterleavedSampleModel</font> is created
      for an image with a subset of the pixels of the original image.
      This is an operation similar to the existing <font
        face="monospace">PixelInterleavedSampleModel.createSubsetModel(int[]
        bands)</font> method, which allows to create a view without
      coyping the <font face="monospace">DataBuffer</font> content.
      Consider the following subsampling factors:</p>
    <blockquote>
      <pre>sourceXSubsampling = 5
sourceYSubsampling = 1
</pre>
    </blockquote>
    <p>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.<br>
    </p>
    <blockquote>
      <pre>X----X----X----X
X----X----X----X
X----X----X----X
</pre>
    </blockquote>
    <p>A subsampled image view can be created with a <font
        face="monospace">PixelInterleavedSampleModel</font> 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 <font
        face="monospace">IllegalArgumentException</font> is thrown. It
      can be seen from the following condition:</p>
    <blockquote>
      <pre>pixelStride*w &gt; scanlineStride
          5*4 &gt; 16
           20 &gt; 16
           true -&gt; IllegalArgumentException
</pre>
    </blockquote>
    <p>Above condition is equivalent to requiring image to be like that:</p>
    <blockquote>
      <pre>X----X----X----X----
</pre>
    </blockquote>
    <p>Instead of:</p>
    <blockquote>
      <pre>X----X----X----X
</pre>
    </blockquote>
    <p>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:</p>
    <blockquote>
      <pre>pixelStride*(w-1) + maxBandOff &gt;= scanlineStride
5*(4-1) + 0 &gt;= 16
15 &gt;= 16
false -&gt; no exception thrown
</pre>
    </blockquote>
    <p>I can create a pull request, but before spending time on it I
      would like to know if this is considered worth fixing?<br>
    </p>
    <p>    Regards,</p>
    <p>        Martin</p>
    <p><br>
    </p>
  </body>
</html>



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic