PPM 画像を作る

Common LispPPM フォーマットの画像を作った。

Ascii 形式の PPM 画像

(defun test-ppm ()
  (with-open-file (out "/tmp/tmp.ppm" :direction :output :if-exists :supersede)
    (format out "P3~%")
    (format out "100 100~%")
    (format out "255~%")
    (loop for i from 0 to (* 100 100) do
         (format out "~a ~a ~a~%" 0 255 0))))

Binary 形式の PPM 画像

Binary 形式の PPM 画像も、ほとんど同じように作れる。

(defun test-ppm-bin ()
  (with-open-file (out "/tmp/tmp.ppm" :direction :output :if-exists :supersede :element-type '(unsigned-byte 8))
    (loop for c across "P6 100 100 255\n"
       do (write-byte (char-code c) out))
    (loop for i from 0 to (* 100 100) do
         (write-byte (random 256) out)
         (write-byte (random 256) out)
         (write-byte (random 256) out))))

REPL へ表示

SLIME の REPL に作った画像を表示することももちろんできる。
Emacs の image-types という変数を評価すると、わたしの環境だと以下のようになっている。

(png gif tiff jpeg xpm postscript xbm pbm)

ので、pbm (注意: ppm ではなく pbm)を指定すればよい。