Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite loop in palettize #213

Open
Fabus1184 opened this issue Nov 11, 2022 · 0 comments
Open

Infinite loop in palettize #213

Fabus1184 opened this issue Nov 11, 2022 · 0 comments

Comments

@Fabus1184
Copy link

Fabus1184 commented Nov 11, 2022

In the palettize function when the PaletteCreationMode Uniform is used, in the corresponding function uniformQuantization when maxCols is less than 8 this causes bitDiv3 to return 0 as at least one element of the tuple (bg, br, bb) which then causes the corresponding list to evaluate to [0, 0 .. 255] which will never terminate.

uniformQuantization :: PaletteOptions -> Image PixelRGB8 -> (Image Pixel8, Palette)
uniformQuantization opts img
-- -| colorCount img <= maxCols = colorQuantExact img
| enableImageDithering opts =
(pixelMap paletteIndex (pixelMapXY dither img), palette)
| otherwise = (pixelMap paletteIndex img, palette)
where
maxCols = paletteColorCount opts
palette = listToPalette paletteList
paletteList = [PixelRGB8 r g b | r <- [0,dr..255]
, g <- [0,dg..255]
, b <- [0,db..255]]
(bg, br, bb) = bitDiv3 maxCols
(dr, dg, db) = (2^(8-br), 2^(8-bg), 2^(8-bb))
paletteIndex (PixelRGB8 r g b) = fromIntegral $ fromMaybe 0 (elemIndex
(PixelRGB8 (r .&. negate dr) (g .&. negate dg) (b .&. negate db))
paletteList)

In my case, this causes the program to use 100% of the CPU filling up the RAM until the system hangs.

If values of less than 8 are not considered valid then this should be at least mentioned in the documentation or there should be a check of some sort.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant