packages feed

sixel (empty) → 0.1.0.0

raw patch · 7 files changed

+208/−0 lines, 7 filesdep +JuicyPixelsdep +basedep +sixelsetup-changedbinary-added

Dependencies added: JuicyPixels, base, sixel

Files

+ CHANGELOG.md view
@@ -0,0 +1,5 @@+# Revision history for sixel++## 0.1.0.0 -- 2020-05-17++* First version.
+ LICENSE view
@@ -0,0 +1,41 @@+BSD 3-Clause License++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++* Redistributions of source code must retain the above copyright notice, this+  list of conditions and the following disclaimer.++* Redistributions in binary form must reproduce the above copyright notice,+  this list of conditions and the following disclaimer in the documentation+  and/or other materials provided with the distribution.++* Neither the name of the copyright holder nor the names of its+  contributors may be used to endorse or promote products derived from+  this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.++Copyright Junji Hashimoto (c) 2020++Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:++1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.++2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.++3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.+
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ app/Main.hs view
@@ -0,0 +1,10 @@+module Main where++import Data.Sixel+import System.Environment (getArgs)++main :: IO ()+main = do+  args <- getArgs+  let file = args !! 0+  putImage file
+ demo.png view

binary file changed (absent → 94084 bytes)

+ sixel.cabal view
@@ -0,0 +1,29 @@+cabal-version:       >=1.10+name:                sixel+version:             0.1.0.0+synopsis:            Sixel library to show images in a terminal emulator+description:         Sixel can show graphics on a terminal emulator. This library is developed to showing images on ghci.+license:             BSD3+license-file:        LICENSE+homepage:            https://github.com/junjihashimoto/sixel#readme+author:              Junji Hashimoto+maintainer:          junji.hashimoto@gmail.com+copyright:           2020 Junji hashimoto+category:            graphics+build-type:          Simple+extra-source-files:  CHANGELOG.md+                   , demo.png++library+  exposed-modules:     Data.Sixel+  hs-source-dirs:      src+  default-language:    Haskell2010+  build-depends:       base >= 4.7 && < 5+                     , JuicyPixels++executable sixel-exe+  main-is:             Main.hs+  hs-source-dirs:      app+  build-depends:       base >= 4.7 && < 5+                     , sixel+  default-language:    Haskell2010
+ src/Data/Sixel.hs view
@@ -0,0 +1,121 @@+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE LambdaCase #-}++module Data.Sixel where++import Codec.Picture+import Data.Char (chr)+import Data.Word (Word8)++type ColorNumber = Word8++type PixelPattern = Word8++type Width = Int++type Height = Int++data SixelCmd+  = Start Int Int Int+  | End+  | Size Int Int Int Int+  | ColorMapRGB ColorNumber Word8 Word8 Word8+  | ColorMapHLS ColorNumber Int Int Int+  | Color ColorNumber+  | Sixel PixelPattern+  | Repeat Int PixelPattern+  | CR+  | LF+  deriving (Eq)++instance Show SixelCmd where+  show = \case+    (Start p1 p2 p3) -> "\ESCP" ++ show p1 ++ ";" ++ show p2 ++ ";" ++ show p3 ++ "q"+    End -> "\ESC\\"+    (Size pan pad width height) -> concat ["\"", show pan, ";", show pad, ";", show width, ";", show height]+    (ColorMapRGB number x y z) -> concat ["#", show number, ";2;", show x, ";", show y, ";", show z]+    (ColorMapHLS number h l s) -> concat ["#", show number, ";1;", show h, ";", show l, ";", show s]+    (Color number) -> concat ["#", show number]+    (Sixel pat) -> [chr (fromIntegral pat + 0x3f)]+    (Repeat num pat) -> concat ["!", show num, [chr (fromIntegral pat + 0x3f)]]+    CR -> "$"+    LF -> "-"++instance {-# OVERLAPS #-} Show [SixelCmd] where+  show xs = concat $ map show xs++numDigits :: (Integral a, Ord a, Num a) => a -> Int+numDigits n+  | n < 10 = 1+  | otherwise = numDigits (n `div` 10) + 1++class ToSixel a where+  toSixel :: a -> [SixelCmd]++instance ToSixel DynamicImage where+  toSixel dimg = toSixel $ convertRGB8 dimg++instance ToSixel (Image PixelRGB8) where+  toSixel img =+    let width = imageWidth img -1+        height = imageHeight img -1+        header =+          [ Start 8 1 0,+            Size 1 1 width height,+            ColorMapRGB 0 100 100 100,+            Color 0+          ]+        footer = End+        putSixel j = case j `mod` 6 of+          0 -> Sixel 1+          1 -> Sixel 2+          2 -> Sixel 4+          3 -> Sixel 8+          4 -> Sixel 16+          5 -> Sixel 32+        pixels =+          concat+            [ header,+              concat+                ( flip map [0 .. (height -1)] $ \j ->+                    concat+                      [ concat+                          ( flip map [0 .. (width -1)] $ \i ->+                              [ pixel2colorMap img i j,+                                putSixel j+                              ]+                          ),+                        if (j `mod` 6) == 5 then [LF] else [CR]+                      ]+                ),+              [footer]+            ]+     in pixels++pixel2colorMap :: Image PixelRGB8 -> Int -> Int -> SixelCmd+pixel2colorMap img i j =+  let p@(PixelRGB8 r g b) = pixelAt img i j+      rr = fromIntegral $ ((fromIntegral r :: Int) * 101) `div` 256+      gg = fromIntegral $ ((fromIntegral g :: Int) * 101) `div` 256+      bb = fromIntegral $ ((fromIntegral b :: Int) * 101) `div` 256+   in ColorMapRGB 0 rr gg bb++putImage :: FilePath -> IO ()+putImage file = do+  readImage file >>= \case+    Left err -> print err+    Right img -> putStr $ show $ toSixel img++demo :: [SixelCmd]+demo =+  [ Start 0 0 8,+    Size 1 1 100 100,+    ColorMapRGB 0 50 0 0,+    Color 0,+    Sixel 1,+    Sixel 2,+    Sixel 4,+    Sixel 8,+    Sixel 16,+    End+  ]