sixel 0.1.2.2 → 0.1.2.3
raw patch · 5 files changed
+94/−4 lines, 5 filesPVP: minor bump suggested
API additions: PVP suggests at least a minor version bump
API changes (from Hackage documentation)
+ Data.Sixel: img2palettizedSixel :: Image PixelRGB8 -> ByteString
+ Data.Sixel.Internal: c_img2palettized_sixel :: Ptr () -> Ptr () -> Ptr () -> CInt -> CInt -> IO CInt
Files
- CHANGELOG.md +5/−0
- csrc/sixel.c +70/−0
- sixel.cabal +1/−1
- src/Data/Sixel.hs +16/−2
- src/Data/Sixel/Internal.hs +2/−1
CHANGELOG.md view
@@ -15,3 +15,8 @@ ## 0.1.2.2 -- 2020-05-22 * Support for OSC1337++## 0.1.2.32 -- 2020-05-25++* Use the palette of 256 colors to fix not working on xterm.+* Change from convert(imagemagick) to gs(ghostscript), because convert does not work on ubuntu18.04.
csrc/sixel.c view
@@ -113,3 +113,73 @@ return p; } ++int+img2palettized_sixel(unsigned char* buf, unsigned char* img, unsigned char* colors,int width,int height){+ int i,j,k;+ int p=0;+ int c;+ int prev = -1;+ buf[p++] = 033; // ESC+ buf[p++] = 'P';+ buf[p++] = '8';+ buf[p++] = ';';+ buf[p++] = '1';+ buf[p++] = ';';+ buf[p++] = '0';+ buf[p++] = 'q';+ buf[p++] = '"';+ buf[p++] = '1';+ buf[p++] = ';';+ buf[p++] = '1';+ buf[p++] = ';';+ putnum(buf,&p,width);+ buf[p++] = ';';+ putnum(buf,&p,height);+ for(i=0;i<256;i++){+ buf[p++] = '#';+ putnum(buf,&p,i);+ buf[p++] = ';';+ buf[p++] = '2';+ buf[p++] = ';';+ putnum(buf,&p,colors[i*3]*101/256);+ buf[p++] = ';';+ putnum(buf,&p,colors[i*3+1]*101/256);+ buf[p++] = ';';+ putnum(buf,&p,colors[i*3+2]*101/256);+ }+ for(j=0;j<height;j++){+ for(i=0;i<width;i++){+ int cur = img[j*width+i];+ if (prev != cur){+ buf[p++] = '#';+ putnum(buf,&p,cur);+ } else {+ int num;+ for(k=i+1;k<width;k++){+ int nxt = img[j*width+k];+ if(nxt!=cur){+ break;+ }+ }+ num = (k-i)-1;+ if(num > 3){+ buf[p++] = '!';+ putnum(buf,&p,num+1);+ i=k-1;+ }+ }+ buf[p++] = (1 << (j%6))+0x3f;+ prev = cur;+ }+ if(j%6 ==5){+ buf[p++] = '-'; // LR+ }else{+ buf[p++] = '$'; // CR+ }+ }+ buf[p++] = 033; // END+ buf[p++] = '\\'; // END+ return p;+}+
sixel.cabal view
@@ -1,6 +1,6 @@ cabal-version: >=1.10 name: sixel-version: 0.1.2.2+version: 0.1.2.3 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
src/Data/Sixel.hs view
@@ -88,8 +88,8 @@ putSixel img = putSixel $ convertRGB8 img instance {-# OVERLAPS #-} ToSixel (Image PixelRGB8) where- toSixel img = SixelImage (BC.unpack $ img2sixel img)- putSixel img = BC.putStr $ img2sixel img+ toSixel img = SixelImage (BC.unpack $ img2palettizedSixel img)+ putSixel img = BC.putStr $ img2palettizedSixel img instance {-# OVERLAPS #-} ToSixel SixelImage where toSixel = id@@ -158,6 +158,20 @@ withForeignPtr sptr $ \src -> do len <- c_img2sixel (castPtr dst) (castPtr src) (fromIntegral w) (fromIntegral h) return (fromIntegral len)++img2palettizedSixel :: Image PixelRGB8 -> ByteString+img2palettizedSixel img = unsafePerformIO $ do+ let (img',palette) = palettize (PaletteOptions MedianMeanCut True 256) img+ (Image w h vec) = img'+ (Image _ _ p) = palette+ bsize <- c_bufsize (fromIntegral w) (fromIntegral h)+ let (sptr, _) = V.unsafeToForeignPtr0 vec+ (spalette, _) = V.unsafeToForeignPtr0 p+ B.createAndTrim (fromIntegral bsize) $ \dst -> do+ withForeignPtr spalette $ \colors -> do+ withForeignPtr sptr $ \src -> do+ len <- c_img2palettized_sixel (castPtr dst) (castPtr src) (castPtr colors) (fromIntegral w) (fromIntegral h)+ return (fromIntegral len) -- | Display sixel image via ByteString --
src/Data/Sixel/Internal.hs view
@@ -15,6 +15,7 @@ foreign import ccall "bufsize" c_bufsize :: CInt -> CInt -> IO CInt foreign import ccall "img2sixel" c_img2sixel :: Ptr () -> Ptr () -> CInt -> CInt -> IO CInt+foreign import ccall "img2palettized_sixel" c_img2palettized_sixel :: Ptr () -> Ptr () -> Ptr () -> CInt -> CInt -> IO CInt data LatexStr = LatexStr@@ -50,7 +51,7 @@ withSystemTempDirectory "sixel" $ \dir -> do writeFile (dir ++ "/sixel.tex") (latexStr str size) (_, outlog, errlog) <- readProcessWithExitCode "pdflatex" ["-output-directory=" ++ dir, dir ++ "/sixel.tex"] ""- readProcessWithExitCode "convert" [dir ++ "/sixel.pdf", "-quality", "90", dir ++ "/sixel.png"] ""+ readProcessWithExitCode "gs" ["-dSAFER", "-r75", "-sDEVICE=png16m", "-o", dir ++ "/sixel.png", dir ++ "/sixel.pdf"] "" readImage (dir ++ "/sixel.png") >>= \case Left err -> return $ Left $ "can not read sixel.png. \n" ++ err ++ "\n" ++ errlog ++ "\n" ++ outlog Right img -> return $ Right img