aboutsummaryrefslogtreecommitdiff
path: root/pnm/write.scm
blob: 849e3dee279c3e46f34ff5ddfa29172d4549cda6 (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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
;;; R7RS-PNM --- Library for reading and writing PNM (Portable Any Map) files for R7RS
;;; Copyright © 2024 Masaya Tojo <masaya@tojo.tokyo>
;;;
;;; This file is part of R7RS-PNM.
;;;
;;; R7RS-PNM is free software: you can redistribute it and/or modify it
;;; under the terms of the GNU Lesser General Public License as published
;;; by the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; R7RS-PNM is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;; GNU Lesser General Public License for more details.
;;;
;;; You should have received a copy of the GNU Lesser General Public License
;;; along with R7RS-PNM. If not, see <https://www.gnu.org/licenses/>.

(define-library (pnm write)
  (export image-write)
  (import (scheme base)
          (scheme case-lambda)
          (pnm image))
  (begin
    (define image-write
      (case-lambda
        ((image out)
         (case (image-type image)
           ((ppm)
            (write-string-u8 "P6\n" out))
           ((pgm)
            (write-string-u8 "P5\n" out))
           ((pbm)
            (write-string-u8 "P4\n" out))
           (else
            (error "(pnm write) image-write: Not supported type" (image-type image))))
         (write-string-u8 (number->string (image-width image)) out)
         (write-string-u8 " " out)
         (write-string-u8 (number->string (image-height image)) out)
         (write-string-u8 "\n" out)
         (case (image-type image)
           ((pgm ppm)
            (write-string-u8 (number->string (image-maxval image)) out)
            (write-string-u8 "\n" out)))
         (write-bytevector (image-data image) out))
        ((image out plain?)
         (if plain?
             (image-write/plan image out)
             (image-write image out)))))

    (define (image-write/plan image out)
      (case (image-type image)
        ((ppm)
         (write-string-u8 "P3\n" out))
        ((pgm)
         (write-string-u8 "P2\n" out))
        ((pbm)
         (write-string-u8 "P1\n" out))
        (else
         (error "(pnm write) image-write: Not supported type" (image-type image))))
      (write-string-u8 (number->string (image-width image)) out)
      (write-string-u8 " " out)
      (write-string-u8 (number->string (image-height image)) out)
      (write-string-u8 "\n" out)
      (case (image-type image)
        ((pgm ppm)
         (write-string-u8 (number->string (image-maxval image)) out)
         (write-string-u8 "\n" out)))
      (let-values (((write-token write-newline)
                    (limit-line-length-writer 70 out)))
        (let ((width (image-width image))
              (height (image-height image)))
          (define (write-raster write-pixel)
            (do ((y 0 (+ y 1)))
                ((= y height))
              (write-pixel 0 y)
              (do ((x 1 (+ x 1)))
                  ((= x width))
                (write-pixel x y))
              (write-newline)))
          (case (image-type image)
            ((ppm)
             (write-raster
              (lambda (x y)
                (let-values (((r g b) (image-ref image x y)))
                  (write-token (number->string r))
                  (write-token (number->string g))
                  (write-token (number->string b))))))
            ((pgm)
             (write-raster
              (lambda (x y)
                (let ((v (image-ref image x y)))
                  (write-token (number->string v))))))
            ((pbm)
             (write-raster
              (lambda (x y)
                (let ((b (image-ref image x y)))
                  (write-token (if b "1" "0"))))))))))

    (define (write-string-u8 str out)
      (string-for-each (lambda (c) (write-u8 (char->integer c) out))
                       str))

    (define (limit-line-length-writer limit out)
      (let ((current-length 0))
        (define (write-token str)
          (let ((str-len (string-length str)))
            (cond ((zero? current-length)
                   (write-string-u8 str out)
                   (set! current-length str-len))
                  ((<= (+ current-length str-len 1) limit)
                   (write-string-u8 " " out)
                   (write-string-u8 str out)
                   (set! current-length (+ current-length 1 str-len)))
                  (else
                   (write-string-u8 "\n" out)
                   (write-string-u8 str out)
                   (set! current-length str-len)))))
        (define (write-newline)
          (write-string-u8 "\n" out)
          (set! current-length 0))
        (values write-token
                write-newline)))))