IT練習ノート

IT関連で調べたこと(実際は嵌ったこと)を書いています。

PPMのライブラリを使ってみる

モジュールの確認

[work04]$ cabal list | grep PPM
Warning: The package list for 'hackage.haskell.org' is 34 days old.
Run 'cabal update' to get the latest list of available packages.
* AC-PPM
    Synopsis: Trivial package for writing PPM images.
    Synopsis: Loading PBM, PGM, PPM image files
    Synopsis: a tiny PPM image generator

インストール

[work04]$ cabal install AC-PPM
Warning: The package list for 'hackage.haskell.org' is 34 days old.
Run 'cabal update' to get the latest list of available packages.
Resolving dependencies...
Downloading AC-PPM-1.1.1...
Configuring AC-PPM-1.1.1...
Building AC-PPM-1.1.1...
Preprocessing library AC-PPM-1.1.1...
[1 of 3] Compiling Codec.PPM.Text   ( Codec/PPM/Text.hs, dist/build/Codec/PPM/Text.o )
[2 of 3] Compiling Codec.PPM.Binary ( Codec/PPM/Binary.hs, dist/build/Codec/PPM/Binary.o )
[3 of 3] Compiling Codec.PPM        ( Codec/PPM.hs, dist/build/Codec/PPM.o )
In-place registering AC-PPM-1.1.1...
Installing library in
/home/ec2-user/.cabal/lib/x86_64-linux-ghc-7.6.3/AC-PPM-1.1.1
Registering AC-PPM-1.1.1...
Installed AC-PPM-1.1.1

ghciの起動とモジュールロード

[work04]$ ghci
GHCi, version 7.6.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> :m Codec.PPM.Binary
Prelude Codec.PPM.Binary> 

データの仕込み

Prelude Codec.PPM.Binary> let x1 = (255,255,255) :: (Data.Word.Word8,Data.Word.Word8,Data.Word.Word8)
Prelude Codec.PPM.Binary> let x2 = (  0,255,255) :: (Data.Word.Word8,Data.Word.Word8,Data.Word.Word8)
Prelude Codec.PPM.Binary> let x3 = (255,  0,255) :: (Data.Word.Word8,Data.Word.Word8,Data.Word.Word8)
Prelude Codec.PPM.Binary> let x4 = (255,255,  0) :: (Data.Word.Word8,Data.Word.Word8,Data.Word.Word8)

ファイル出力

Prelude Codec.PPM.Binary> writePPM "./samle05.ppm" (2,2) [x1,x2,x3,x4]
Loading package array-0.4.0.1 ... linking ... done.
Loading package deepseq-1.3.0.1 ... linking ... done.
Loading package bytestring-0.10.0.2 ... linking ... done.
Loading package AC-PPM-1.1.1 ... linking ... done.

出力ファイルの確認

Prelude Codec.PPM.Binary> :q
Leaving GHCi.
[work04]$ hexdump -C ./sample05.ppm | head -n 5
hexdump: ./sample05.ppm: そのようなファイルやディレクトリはありません
hexdump: all input file arguments failed
[work04]$ ls
girl.ppm  girl_out.ppm  girl_out2.ppm  image_sorce.txt  ppm-2009.5.13.tar.gz  samle05.ppm  sample01.hs  sample02.hs  sample03.hs  sample04.hs  sample05.hs  test.ppm  test_out.ppm
[ec2-user@ip-172-31-25-234 work04]$ hexdump -C ./samle05.ppm | head -n 5
00000000  50 36 0a 32 20 32 0a 32  35 35 0a ff ff ff 00 ff  |P6.2 2.255......|
00000010  ff ff 00 ff ff ff 00                              |.......|
00000017