Last active
August 29, 2015 14:05
-
-
Save Wollw/4aab3b4dd010ce991063 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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