Skip to content

Instantly share code, notes, and snippets.

@Wollw
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Wollw/4aab3b4dd010ce991063 to your computer and use it in GitHub Desktop.
Save Wollw/4aab3b4dd010ce991063 to your computer and use it in GitHub Desktop.
{-# LANGUAGE DeriveDataTypeable #-}
import Codec.Picture
import Data.Numbers.Primes
import System.Console.CmdArgs.Implicit
import Prelude hiding (exponent)
import System.IO
import System.Exit
data Options = Options
{ exponent :: Double
, input :: FilePath
, output :: FilePath
} deriving (Show, Data, Typeable)
options = Options
{ exponent = 1.0 &= help "Exponent argument"
, input = def &= argPos 0 &= typ "INPUT"
, output = def &= argPos 1 &= typ "OUTPUT"
} &= summary "PrimeImage v1"
main = do
o <- cmdArgs options
(Right di) <- readImage $ input o
case di of
(ImageRGB8 i) -> writePng (output o)
$ pixelMap (colorMap (largestPrimeExp $ exponent o)) i
_ -> hPutStrLn stderr "Input must be 8-bit RGB PNG" >> exitFailure
largestPrimeExp :: Integral a => Double -> a -> a
largestPrimeExp e x = -- 251 manually checked for due to a bug (?)
if x == 1 || x == 251 || isPrime x
then x
else min 255 (truncate $ fromIntegral (maximum . primeFactors $ x) ** e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment