Twinside / Juicy.Pixels

Haskell library to load & save pictures

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bug in `isColorCountBelow` in ColorQuant.hs

jeffreyrosenbluth opened this issue · comments

In the code below count should only be incremented when a new color is added to the
set, but we are incrementing every time through the loop, which means we almost always return False. This actually causes problems when making GIFs with diagrams since they often have less than 256 colors. A fix is below which i think is fine since Set.size is O(1)

isColorCountBelow :: Int -> Image PixelRGB8 -> (Set.Set PixelRGB8, Bool)
isColorCountBelow maxColorCount img = go 0 0 Set.empty
  where rawData = imageData img
        maxIndex = VS.length rawData

        go !count !idx !allColors
            | count > maxColorCount = (Set.empty, False)
            | idx >= maxIndex - 2 = (allColors, True)
            | otherwise = go (count + 1) (idx + 3)
                        $ Set.insert px allColors
                where px = unsafePixelAt rawData idx 
| otherwise = go ((Set.size allColors) + 1) (idx + 3)
                        $ Set.insert px allColors
                where px = unsafePixelAt rawData idx

I've also bumped the hackage version ( http://hackage.haskell.org/package/JuicyPixels-3.1.3.1 ), so the fix is cabal update ready

Great thanks