Martin Mirchev created IMAGING-395:
--------------------------------------

             Summary: Quantized palette colors are wrong for large uniform 
areas: int overflow in ColorSpaceSubset.setAverageRgb
                 Key: IMAGING-395
                 URL: https://issues.apache.org/jira/browse/IMAGING-395
             Project: Commons Imaging
          Issue Type: Bug
            Reporter: Martin Mirchev


{{ColorSpaceSubset.setAverageRgb}} sums {{count * (component << 8 - 
precision)}} into {{long}} variables, but the product is computed in {{int}} 
and overflows before it is widened. When one subset holds more than about 8.4 
million pixels of a bright color, the sum wraps and the subset's palette color 
is wrong. For example, {{PaletteFactory.makeQuantizedRgbPalette}} on a 
3000x3000 image that is all 0xFFFFFF gives the average color 0x1F1F1F instead 
of a near-white one. Images of 9 megapixels are common, so this affects the 
quantized palettes of ordinary large images with large uniform areas.

Reproducer (same package):
{code:java}
int precision = 4;
int[] table = new int[1 << 3 * precision];
table[15 << 8 | 15 << 4 | 15] = 9_000_000;
ColorSpaceSubset subset = new ColorSpaceSubset(9_000_000, precision, new int[] 
{ 15, 15, 15 }, new int[] { 15, 15, 15 });
subset.setAverageRgb(table);
// subset.rgb is 0x131313, expected 0xF0F0F0
{code}

Suggested fix: cast {{count}} to {{long}} before multiplying. A pull request 
with the fix and a test follows.

This issue was found by an LLM-Enhanced Static Analysis Engine and verified 
manually.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to