-- eidolon -- A simple gallery in Haskell and Yesod -- Copyright (C) 2015-2016 Amedeo Molnár -- -- This program is free software: you can redistribute it and/or modify -- it under the terms of the GNU Affero General Public License as published -- by the Free Software Foundation, either version 3 of the License, or -- (at your option) any later version. -- -- This program is distributed in the hope that it will be useful, -- but WITHOUT ANY WARRANTY; without even the implied warranty of -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -- GNU Affero General Public License for more details. -- -- You should have received a copy of the GNU Affero General Public License -- along with this program. If not, see . module Scale (scaleBilinearAlpha ) where import Import import Codec.Picture import Control.Monad.ST import qualified Codec.Picture.Types as M scaleBilinearAlpha :: Int -- ^ Desired width -> Int -- ^ Desired height -> Image PixelRGBA8 -- ^ Original image -> Image PixelRGBA8 -- ^ Scaled image scaleBilinearAlpha width height img@Image {..} = runST $ do mimg <- M.newMutableImage width height let sx, sy :: Float sx = fromIntegral imageWidth / fromIntegral width sy = fromIntegral imageHeight / fromIntegral height go x' y' | x' >= width = go 0 (y' + 1) | y' >= height = M.unsafeFreezeImage mimg | otherwise = do let xf = fromIntegral x' * sx yf = fromIntegral y' * sy x, y :: Int x = floor xf y = floor yf δx = xf - fromIntegral x δy = yf - fromIntegral y pixelAt' i j = if i >= imageWidth || j >= imageHeight then PixelRGBA8 0 0 0 0 else pixelAt img i j writePixel mimg x' y' $ mulp (pixelAt' x y) ((1 - δx) * (1 - δy)) `addp` mulp (pixelAt' (x + 1) y) (δx * (1 - δy)) `addp` mulp (pixelAt' x (y + 1)) ((1 - δx) * δy) `addp` mulp (pixelAt' (x + 1) (y + 1)) (δx * δy) go (x' + 1) y' go 0 0 mulp :: PixelRGBA8 -> Float -> PixelRGBA8 mulp pixel x = colorMap (floor . (* x) . fromIntegral) pixel {-# INLINE mulp #-} addp :: PixelRGBA8 -> PixelRGBA8 -> PixelRGBA8 addp = mixWith (const f) where f x y = fromIntegral $ (0xff :: Pixel8) `min` (fromIntegral x + fromIntegral y)