Closed faberf closed 2 months ago
Here are some values that end up as part of the ImageContent (as an RGB array)
What tells you that any of the components of the container are negative rather than too large?
I don't exactly know what you are referring to, but I am working on a fix now.
I think @lucaro is right: I'd presume the problem to be AverageColor.analyse()
.
We use an IntArray
for the sum. This can lead to an overflow of Int
's max range (and thus negative values).
I think the problem is in the constructor for RGBColorContainer. I will make a PR now.
The negative values you are showing in your screenshot are not necessarily related to the problem you are observing, as they are concatenated byte values. If the most significant byte is >=127, the most significant bit will be high, and hence the whole int will be negative.
If there is indeed an overflow problem in averaging the values together, switching to a DoubelArray should be more than enough to prevent this, at the cost of using twice as much memory. Since all that stuff is using a lot of memory already, I don't think this will make a meaningful difference
I think I disagree. The problem is not the int that is aggregated. The problem is in the constructor of the RGBColorContainer when an Integer is being turned into four bytes. These bytes should be interpreted as values between 0 and 255 not as values between -127 and 127. What am I missing?
Yes my bad - I was thinking about the main constructor.
What you are proposing makes sense. However, are you sure the culprit isn't the part that converts single Int
to component-wise Byte
representation?
The problem is that the .toFloat()
on the byte value, you would need to do something like (byte & 0xff).toInt().toFloat()
. Using the UByte
is essentially that but with some syntactic sugar to make it more readable. I'd say since Kotlin already has unsigned types, we might as well use them, so the pull request is fine by me...
Merged! Thanks @faberf
So I guess this can be closed now?
Yes
https://github.com/user-attachments/assets/bcbd82fb-197f-40ff-975d-271f1b905471
For the file above, the following pipeline gives an IllegalArgumentException resulting from an RGB value that is negative.