On Wed, 2 Apr 2025 07:47:43 GMT, Nikita Gubarkov <ngubar...@openjdk.org> wrote:

>The idea of the current approach, as I understand it is that we find the last 
>band of the first pixel, be it 3 for pixel-interleaved RGBA, or 30000 for 
>band-interleaved image, and then add 1 to it, meaning the size sufficient to 
>fit that last band:
>int size = maxBandOff + 1;
>At this point we can already fit the first pixel, so what's left is to fit the 
>remaining first scanline (hence width - 1):

This looks suspicious. Let's try calculating the size or pointer to the last 
pixel using the formula:
"starting offset + pointer to the last pixel + pixelStride" but in reverse 
order.
   
    // This moves the pointer to the start of the last row.
    size = scanlineStride * (height - 1); 
    // This moves the pointer to the start of the last pixel in the last row.
    size += pixelStride * (width - 1);

**Note**: At this point, the size is not necessarily aligned to pixelStride 
because scanlineStride may include a gap larger than pixelStride at the end.

Now, we need to account for the last pixel in the last row. In a theoretical 
case where the offset is 0, simply adding pixelStride one more time would be 
sufficient.

However, in our case, we add maxBandOff + 1, which seems odd since maxBandOff 
represents the offset of the largest component, placing it somewhere within the 
pixelStride.

To fix this, we should actually add the second part of the pixelStride only if 
maxBandOff is smaller than pixelStride.

btw, I'm not sure why we can't use the first component instead of the last one:


 int minBandOff = bandOffsets[0];
 for (int i = 1; i < bandOffsets.length; i++) {
     minBandOff = Math.min(minBandOff, bandOffsets[i]);
 }

 long size = 0;

 if (minBandOff >= 0)
     size += minBandOff;
 if (pixelStride > 0)
     size += pixelStride * width;
 if (scanlineStride > 0)
     size += scanlineStride * (height - 1);         
 return (int) size;


...Still investigating this.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/24111#discussion_r2024628842

Reply via email to