packages feed

camh 0.0.2 → 0.0.3

raw patch · 4 files changed

+39/−20 lines, 4 files

Files

.hg_archival.txt view
@@ -1,5 +1,6 @@ repo: ab857342ad48bfaaaf218f888cfdae5caf759dbf-node: 7987734218cc68bc5df405db3940ce2f1443285a+node: 87270a4553bece4f7d872061193921f70e9b7c02 branch: default latesttag: null-latesttagdistance: 14+latesttagdistance: 18+changessincelatesttag: 18
camh.1 view
@@ -1,4 +1,4 @@-.TH CAMH "1" "August 2013" "CAMH 0.0.1" "User Commands"+.TH CAMH "1" "May 2017" "CAMH 0.0.3" "User Commands" .SH NAME camh \- image-to-text converter for 256-colored terminals .SH SYNOPSIS@@ -19,6 +19,9 @@ .TP .B \-v, \-\-version Show version information.+.TP+.B \-T, \-\-true\-color+Enable 24 bit color. .TP .B \-h, \-\-height-percent=HEIGHT Specify output height percent to that of the terminal.
camh.cabal view
@@ -1,6 +1,6 @@ Name:           camh-Version:        0.0.2-Synopsis:       Image converter to 256-colored text.+Version:        0.0.3+Synopsis:       write image files onto 256(or 24bit) color terminals. Description:    Camh is a program to display image files onto text terminals.  License:        BSD3@@ -13,7 +13,7 @@ Homepage:       not yet available Bug-Reports:    not yet available Category:       Graphics-Tested-With:    GHC == 7.6.3+Tested-With:    GHC == 8.0.1  Data-Files:     camh.1 
camh.hs view
@@ -16,6 +16,9 @@ import System.Exit (exitSuccess) import System.IO (stderr, hPutStrLn) +data ColorType = ColorType256 | ColorType24bit+    deriving (Eq, Show)+ from24bitColor :: Word32 -> Int from24bitColor w =     let p = fromIntegral w@@ -28,22 +31,33 @@ changeBgColor256 col =     "\x1b[48;5;" `B.append` B.pack (show col) `B.append` "m" +changeBgColor24bit :: Word32 -> B.ByteString+changeBgColor24bit w =+    let p = fromIntegral w :: Int+        r = p `shift` (-16) .&. 255+        g = p `shift`  (-8) .&. 255+        b = p               .&. 255+    in "\x1b[48;2;" `B.append` B.pack (show r) `B.append` ";"+                    `B.append` B.pack (show g) `B.append` ";"+                    `B.append` B.pack (show b) `B.append` "m"+ restoreBgColor :: B.ByteString restoreBgColor = "\x1b[m" -colorLine :: [Word32] -> B.ByteString-colorLine row =+colorLine :: ColorType -> [Word32] -> B.ByteString+colorLine c row =     let g        = group row-        e        = changeBgColor256 . from24bitColor+        e        = if c == ColorType256 then changeBgColor256 . from24bitColor+                                        else changeBgColor24bit         s n      = B.replicate n ' '         f (w:ws) = e w `B.append` s (fromIntegral $ 1 + length ws)     in B.concat (map f g) `B.append` restoreBgColor -writeImage :: Int -> Int -> Ptr Word32 -> IO ()-writeImage w h p =+writeImage :: ColorType -> Int -> Int -> Ptr Word32 -> IO ()+writeImage c w h p =     forM_ [0 .. h-1] $ \j -> do       row <- forM [0 .. w-1] $ \i -> peekElemOff p (w * j + i)-      B.putStrLn $ colorLine row+      B.putStrLn $ colorLine c row  dealFile :: Config -> FilePath -> IO () dealFile conf filename =@@ -75,7 +89,7 @@             applyColorModifier             freeColorModifier -            imageWithData $ writeImage rw rh+            imageWithData $ writeImage (getColorType conf) rw rh             freeImage              contextSetImage image@@ -86,16 +100,13 @@ getTermSize :: IO (Maybe (Int, Int)) getTermSize = handle (const $ return Nothing                           :: SetupTermError -> IO (Maybe a)) $ do-  term <- setupTermFromEnv-  let size = do-        lines <- tiGetNum "lines"-        cols  <- tiGetNum "cols"-        return (lines, cols)--  return $ getCapability term size+                term <- setupTermFromEnv+                let size = liftM2 (,) (tiGetNum "lines") (tiGetNum "cols")+                return $ getCapability term size  data Opts = Help           | Version+          | TrueColor           | WidthChr   Int           | HeightChr  Int           | Gamma      Double@@ -108,6 +119,7 @@         th = getHeight conf     in [ Option "?" ["help"] (NoArg Help) "Show this message"        , Option "v" ["version"] (NoArg Version) "Show version info"+       , Option "T" ["true-color"] (NoArg TrueColor) "enable 24bit color"        , Option "W" ["width-char"]                     (ReqArg (WidthChr . read) "WIDTH")                     "Set output width (in chars)"@@ -139,12 +151,14 @@  data Config = Config { getWidth      :: Int                      , getHeight     :: Int+                     , getColorType  :: ColorType                      , getGamma      :: Maybe Double                      , getBrightness :: Maybe Double                      , getContrast   :: Maybe Double }  defaultConfig = Config { getWidth      = 80                        , getHeight     = 25+                       , getColorType  = ColorType256                        , getGamma      = Nothing                        , getBrightness = Nothing                        , getContrast   = Nothing }@@ -162,6 +176,7 @@     case opts of       Help         -> putStr   usage   >> exitSuccess       Version      -> putStrLn version >> exitSuccess+      TrueColor    -> return conf { getColorType  = ColorType24bit }       WidthChr   w -> return conf { getWidth      = w }       HeightChr  h -> return conf { getHeight     = h }       Gamma      g -> return conf { getGamma      = Just g }