aboutsummaryrefslogtreecommitdiff
path: root/image-format/pnm/write.scm
blob: b2f807cb4a0eb8681a049ebe83390d4fd24f2997 (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 (image-format pnm write)
  (export pnm-image-write)
  (import (scheme base)
          (scheme case-lambda)
          (image-format pnm image))
  (begin
    (define pnm-image-write
      (case-lambda
        ((pnm-image out)
         (case (pnm-image-type pnm-image)
           ((ppm)
            (write-string-u8 "P6\n" out))
           ((pgm)
            (write-string-u8 "P5\n" out))
           ((pbm)
            (write-string-u8 "P4\n" out))
           (else
            (error "(image-format pnm write) pnm-image-write: Not supported type" (pnm-image-type pnm-image))))
         (write-string-u8 (number->string (pnm-image-width pnm-image)) out)
         (write-string-u8 " " out)
         (write-string-u8 (number->string (pnm-image-height pnm-image)) out)
         (write-string-u8 "\n" out)
         (case (pnm-image-type pnm-image)
           ((pgm ppm)
            (write-string-u8 (number->string (pnm-image-maxval pnm-image)) out)
            (write-string-u8 "\n" out)))
         (write-bytevector (pnm-image-data pnm-image) out))
        ((pnm-image out plain?)
         (if plain?
             (pnm-image-write/plan pnm-image out)
             (pnm-image-write pnm-image out)))))

    (define (pnm-image-write/plan pnm-image out)
      (case (pnm-image-type pnm-image)
        ((ppm)
         (write-string-u8 "P3\n" out))
        ((pgm)
         (write-string-u8 "P2\n" out))
        ((pbm)
         (write-string-u8 "P1\n" out))
        (else
         (error "(image-format pnm write) image-write: Not supported type" (pnm-image-type pnm-image))))
      (write-string-u8 (number->string (pnm-image-width pnm-image)) out)
      (write-string-u8 " " out)
      (write-string-u8 (number->string (pnm-image-height pnm-image)) out)
      (write-string-u8 "\n" out)
      (case (pnm-image-type pnm-image)
        ((pgm ppm)
         (write-string-u8 (number->string (pnm-image-maxval pnm-image)) out)
         (write-string-u8 "\n" out)))
      (let-values (((write-token write-newline)
                    (limit-line-length-writer 70 out)))
        (let ((width (pnm-image-width pnm-image))
              (height (pnm-image-height pnm-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 (pnm-image-type pnm-image)
            ((ppm)
             (write-raster
              (lambda (x y)
                (let-values (((r g b) (pnm-image-ref pnm-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 (pnm-image-ref pnm-image x y)))
                  (write-token (number->string v))))))
            ((pbm)
             (write-raster
              (lambda (x y)
                (let ((b (pnm-image-ref pnm-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)))))