aboutsummaryrefslogtreecommitdiff
path: root/pnm/write.scm
blob: 038bf8249d9fe3e26ad0d4d7056632ab4db567ba (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
(define-library (pnm write)
  (export image-write)
  (import (scheme base)
          (pnm image))
  (begin
    (define (image-write image out)
      (define (write-string-u8 str)
        (string-for-each (lambda (c) (write-u8 (char->integer c) out))
                         str))
      (case (image-type image)
        ((ppm)
         (write-string-u8 "P6\n"))
        ((pgm)
         (write-string-u8 "P5\n"))
        (else
         (error "(pnm write) pnm-write: Not supported type" (image-type image))))
      (write-string-u8 (number->string (image-width image)))
      (write-string-u8 "\n")
      (write-string-u8 (number->string (image-height image)))
      (write-string-u8 "\n")
      (case (image-type image)
        ((pgm ppm)
         (write-string-u8 (number->string (image-maxval image)))
         (write-string-u8 "\n")))
      (write-bytevector (image-data image) out))))