2024-12-24 16:04:27 +01:00
|
|
|
import Ai.Evolutionary ( evolve )
|
|
|
|
import Codec.Picture ( DynamicImage, convertRGB8, readImage )
|
|
|
|
import Data.Colour ( lab2rgb )
|
|
|
|
import qualified Data.Vector as V
|
|
|
|
import Stylix.Output ( makeOutputTable )
|
|
|
|
import Stylix.Palette ( )
|
|
|
|
import System.Environment ( getArgs )
|
|
|
|
import System.Exit ( die )
|
|
|
|
import System.Random ( setStdGen, mkStdGen )
|
|
|
|
import Text.JSON ( encode )
|
2024-12-24 16:59:23 +01:00
|
|
|
import Text.Read (readMaybe)
|
|
|
|
|
|
|
|
-- String to float
|
2024-12-24 17:14:37 +01:00
|
|
|
stringToDouble :: String -> Double
|
|
|
|
stringToDouble s = case readMaybe s of
|
2024-12-24 16:59:23 +01:00
|
|
|
Just x -> x
|
|
|
|
Nothing -> error "Invalid float value"
|
2024-12-24 16:04:27 +01:00
|
|
|
|
|
|
|
-- | Load an image file.
|
|
|
|
loadImage :: String -- ^ Path to the file
|
|
|
|
-> IO DynamicImage
|
|
|
|
loadImage input = either error id <$> readImage input
|
|
|
|
|
2024-12-24 16:33:57 +01:00
|
|
|
mainProcess :: (String,String,String, String, String) -> IO ()
|
2024-12-24 17:17:02 +01:00
|
|
|
mainProcess (polarity, primaryScaleDark, primaryScaleLight, input, output) = do
|
2024-12-24 16:04:27 +01:00
|
|
|
putStrLn $ "Processing " ++ input
|
|
|
|
|
|
|
|
-- Random numbers must be deterministic when running inside Nix.
|
|
|
|
setStdGen $ mkStdGen 0
|
|
|
|
|
|
|
|
image <- loadImage input
|
2024-12-24 17:14:37 +01:00
|
|
|
palette <- evolve (polarity,stringToDouble primaryScaleDark,stringToDouble primaryScaleLight,convertRGB8 image)
|
2024-12-24 16:04:27 +01:00
|
|
|
let outputTable = makeOutputTable $ V.map lab2rgb palette
|
|
|
|
|
|
|
|
writeFile output $ encode outputTable
|
|
|
|
putStrLn $ "Saved to " ++ output
|
|
|
|
|
2024-12-24 16:33:57 +01:00
|
|
|
parseArguments :: [String] -> Either String (String, String,String,String, String)
|
2024-12-24 16:28:52 +01:00
|
|
|
parseArguments [polarity, primaryScaleDark, primaryScaleLight,input, output] = Right (polarity,primaryScaleDark, primaryScaleLight, input, output)
|
|
|
|
parseArguments [_, _,_,_] = Left "Please specify an output file"
|
|
|
|
parseArguments [_, _,_] = Left "Please specify an image"
|
|
|
|
parseArguments [_, _] = Left "Please specify a primary scaling factor for the light polarity"
|
|
|
|
parseArguments [_] = Left "Please specify a primary scaling factor for the dark polarity"
|
2024-12-24 16:04:27 +01:00
|
|
|
parseArguments [] = Left "Please specify a polarity: either, light or dark"
|
|
|
|
parseArguments _ = Left "Too many arguments"
|
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = either die mainProcess . parseArguments =<< getArgs
|