-
Notifications
You must be signed in to change notification settings - Fork 1
/
bmp.sls
372 lines (348 loc) · 13.1 KB
/
bmp.sls
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
(library (bmp)
(export read
write
;; Constructors and Selectors
make-bmp
bmp?
bmp-height
bmp-width
bmp-bytevector)
(import (except (rnrs) read write))
(define-record-type bmp
(fields width height bytevector))
(define-record-type file
(fields size offset))
(define-record-type description
(fields size
width
height
planes
bit-count
compression
size-image
x-ppm
y-ppm
clr-used
clr-important
red-mask
green-mask
blue-mask
alpha-mask
cs-type
endpoints
gamma-red
gamma-green
gamma-blue
intent
profile-data
profile-size
reserved))
(define (read port)
;;; Construct a bmp from a file port.
;;;
;; File port reader utilities.
(define datum (make-bytevector 4))
(define (read-u32)
(get-bytevector-n! port datum 0 4)
(bytevector-uint-ref datum 0 (endianness little) 4))
(define (read-s32)
(get-bytevector-n! port datum 0 4)
(bytevector-sint-ref datum 0 (endianness little) 4))
(define (read-u16)
(get-bytevector-n! port datum 0 2)
(bytevector-uint-ref datum 0 (endianness little) 2))
(define (read-s16)
(get-bytevector-n! port datum 0 2)
(bytevector-sint-ref datum 0 (endianness little) 2))
(define (read-u8)
(get-bytevector-n! port datum 0 1)
(bytevector-uint-ref datum 0 (endianness little) 1))
(define (read-header port)
;;; Read the BMP header from the file port.
;;;
(define head (get-bytevector-n port 14))
(if (and (= #x42 (bytevector-uint-ref head 0 (endianness little) 1))
(= #x4d (bytevector-uint-ref head 1 (endianness little) 1)))
(make-file (bytevector-uint-ref head 2 (endianness little) 4)
(bytevector-uint-ref head 10 (endianness little) 4))
(error 'BMP "Unsupported BMP file.")))
(define (read-description port)
;;; Read the DIB metadata from the file port.
;;;
;; Determine the descriptor version.
(define version (read-u32))
;; (define core? (= version 12))
;; (define os2x? (or (= version 16) (= version 64)))
;; (define info? (= version 40))
;; (define ver2? (= version 52))
;; (define ver3? (= version 56))
;; (define ver4? (= version 108))
;; (define ver5? (= version 124))
;; Read descriptor into record.
(define (read-version-5)
(make-description version
(read-s32)
(read-s32)
(read-u16)
(read-u16)
(read-u32)
(read-u32)
(read-s32)
(read-s32)
(read-u32)
(read-u32)
(read-u32)
(read-u32)
(read-u32)
(read-u32)
(read-u32)
(list (read-u32)
(read-u32)
(read-u32)
(read-u32)
(read-u32)
(read-u32)
(read-u32)
(read-u32)
(read-u32))
(read-u32)
(read-u32)
(read-u32)
(read-u32)
(read-u32)
(read-u32)
(read-u32)))
(define (read-version-4)
(make-description version
(read-s32)
(read-s32)
(read-u16)
(read-u16)
(read-u32)
(read-u32)
(read-s32)
(read-s32)
(read-u32)
(read-u32)
(read-u32)
(read-u32)
(read-u32)
(read-u32)
(read-u32)
(list (read-u32)
(read-u32)
(read-u32)
(read-u32)
(read-u32)
(read-u32)
(read-u32)
(read-u32)
(read-u32))
(read-u32)
(read-u32)
(read-u32)
#f
#f
#f
#f))
(cond ((= version 124) (read-version-5))
((= version 108) (read-version-4))
((= version 56) (error 'BMP "BITMAPV3INFOHEADER unsupported."))
((= version 52) (error 'BMP "BITMAPV2INFOHEADER unsupported."))
((= version 40) (error 'BMP "BITMAPINFOHEADER unsupported."))
((or (= version 16)
(= version 64))
(error 'BMP "OS22XBITMAPHEADER unsupported."))
((= version 12)
(error 'BMP "BITMAPCOREHEADER unsupported."))
(else
(error 'BMP "Unsupported BMP verison." version))))
(define (read-palette port)
;;; Read the color table from the file port.
;;;
(error 'BMP "Color tables unimplemented."))
;; Read metadata into records.
(define header (read-header port))
(define description (read-description port))
;; Read color table if any.
(define palette
(if (> (description-clr-used description) 0)
(read-palette port)
#f))
(define bytevector
(make-bytevector (* (description-width description) (description-height description) 4)))
(define (plain)
;;; Reads bitmap encoded data into a bmp record.
;;;
(make-bmp (description-width description)
(description-height description)
(cond ((= 16 (description-bit-count description))
(let read-pixels ((i 0))
(if (= i (bytevector-length bytevector)) bytevector
(let ((pixel-word (read-u16))
(a 255))
;; BGR 565 - other formats exist...
(bytevector-u8-set! bytevector (+ i 0) (* (/ (bitwise-bit-field pixel-word 0 5) 32) 255))
(bytevector-u8-set! bytevector (+ i 1) (* (/ (bitwise-bit-field pixel-word 5 11) 64) 255))
(bytevector-u8-set! bytevector (+ i 2) (* (/ (bitwise-bit-field pixel-word 11 16) 32) 255))
(bytevector-u8-set! bytevector (+ i 3) a)
(read-pixels (+ 4 i))))))
((= 24 (description-bit-count description))
(let read-pixels ((i 0))
(if (= i (bytevector-length bytevector)) bytevector
(let ((r (read-u8))
(g (read-u8))
(b (read-u8))
(a 255))
(bytevector-u8-set! bytevector (+ i 0) b)
(bytevector-u8-set! bytevector (+ i 1) g)
(bytevector-u8-set! bytevector (+ i 2) r)
(bytevector-u8-set! bytevector (+ i 3) a)
(read-pixels (+ 4 i))))))
((= 32 (description-bit-count description))
(let read-pixels ((i 0))
(if (= i (bytevector-length bytevector)) bytevector
(let ((r (read-u8))
(g (read-u8))
(b (read-u8))
(a (read-u8)))
(bytevector-u8-set! bytevector (+ i 0) b)
(bytevector-u8-set! bytevector (+ i 1) g)
(bytevector-u8-set! bytevector (+ i 2) r)
(bytevector-u8-set! bytevector (+ i 3) a)
(read-pixels (+ 4 i))))))
(else
(error 'BMP "Unsupported pixel bit count." (description-bit-count description))))))
(define (run-length-encoding-8)
;;; Reads RLE8 encoded data into a bmp record.
;;;
(error 'BMP "RLE8 compression unsupported."))
(define (run-length-encoding-4)
;;; Reads RLE4 encoded data into a bmp record.
;;;
(error 'BMP "RLE4 compression unsupported."))
(define (bitfield)
;;; Reads bitfield encoded data into a bmp record.
;;;
(define (unmask pixel-word mask)
(define start (bitwise-bit-count (- (bitwise-and mask (+ (bitwise-not mask) 1)) 1)))
(define end (+ start (bitwise-bit-count mask)))
(define value (bitwise-bit-field (bitwise-and pixel-word mask) start end))
(define length (- (expt 2 (bitwise-bit-count mask)) 1))
(truncate (* (/ value length) 255)))
(make-bmp (description-width description)
(description-height description)
(cond ((= 16 (description-bit-count description))
(let read-pixels ((i 0))
(if (= i (bytevector-length bytevector)) bytevector
(let ((word (read-u16)))
(bytevector-u8-set! bytevector (+ i 0) (unmask word (description-blue-mask description)))
(bytevector-u8-set! bytevector (+ i 1) (unmask word (description-green-mask description)))
(bytevector-u8-set! bytevector (+ i 2) (unmask word (description-red-mask description)))
(bytevector-u8-set! bytevector (+ i 3) (if (> (description-alpha-mask description) 0)
(unmask word (description-alpha-mask description))
255))
(read-pixels (+ 4 i))))))
((= 32 (description-bit-count description))
(let read-pixels ((i 0))
(if (= i (bytevector-length bytevector)) bytevector
(let ((word (read-u32)))
(bytevector-u8-set! bytevector (+ i 0) (unmask word (description-blue-mask description)))
(bytevector-u8-set! bytevector (+ i 1) (unmask word (description-green-mask description)))
(bytevector-u8-set! bytevector (+ i 2) (unmask word (description-red-mask description)))
(bytevector-u8-set! bytevector (+ i 3) (if (> (description-alpha-mask description) 0)
(unmask word (description-alpha-mask description))
255))
(read-pixels (+ 4 i))))))
(else
(error 'BMP "Unsupported pixel bit count." (description-bit-count description))))))
(define (jpeg)
;;; Reads JPEG encoded data into a bmp record.
;;;
(error 'BMP "JPEG compression unsupported."))
(define (png)
;;; Reads PNG encoded data into a bmp record.
;;;
(error 'BMP "PNG compression unsupported."))
(define (alpha-bitfield)
;;; Reads alpha bitfields encoded data into a bmp record.
;;;
(error 'BMP "Alpha bitfield masked data unsupported."))
;; Determine pixel data encoding.
;; (define (bmpe?) (= (description-compression description) 0))
;; (define (rle8?) (= (description-compression description) 1))
;; (define (rle4?) (= (description-compression description) 2))
;; (define (bitf?) (= (description-compression description) 3))
;; (define (jpeg?) (= (description-compression description) 4))
;; (define (pnge?) (= (description-compression description) 5))
;; (define (abit?) (= (description-compression description) 6))
(cond ((= (description-compression description) 0) (plain))
((= (description-compression description) 1) (run-length-encoding-8))
((= (description-compression description) 2) (run-length-encoding-4))
((= (description-compression description) 3) (bitfield))
((= (description-compression description) 4) (jpeg))
((= (description-compression description) 5) (png))
((= (description-compression description) 6) (alpha-bitfield))
(else
(error 'BMP "Unsupported compression encoding." (description-compression description)))))
(define (write bmp port)
;;; Store a bmp to a file port.
;;;
;; File port writer utilities.
(define datum (make-bytevector 4))
(define (write-u32 value)
(bytevector-uint-set! datum 0 value (endianness little) 4)
(put-bytevector port datum 0 4))
(define (write-s32 value)
(bytevector-sint-set! datum 0 value (endianness little) 4)
(put-bytevector port datum 0 4))
(define (write-u16 value)
(bytevector-uint-set! datum 0 value (endianness little) 2)
(put-bytevector port datum 0 2))
(define (write-s16 value)
(bytevector-sint-set! datum 0 value (endianness little) 2)
(put-bytevector port datum 0 2))
(define (write-u8 value)
(bytevector-uint-set! datum 0 value (endianness little) 1)
(put-bytevector port datum 0 1))
;; Write header to port.
(write-u8 #x42)
(write-u8 #x4D)
(write-u32 (+ 14 124 (* (bmp-width bmp) (bmp-height bmp) 4)))
(write-u32 0)
(write-u32 (+ 14 124))
;; Write description to port.
(write-u32 124)
(write-s32 (bmp-width bmp))
(write-s32 (bmp-height bmp))
(write-u16 1)
(write-u16 32)
(write-u32 3)
(write-u32 (* (bmp-width bmp) (bmp-height bmp) 4))
(write-s32 3780)
(write-s32 3780)
(write-u32 0)
(write-u32 0)
(write-u32 #x00FF0000)
(write-u32 #x0000FF00)
(write-u32 #x000000FF)
(write-u32 #xFF000000)
(write-u32 #x73524742)
(write-u32 0)
(write-u32 0)
(write-u32 0)
(write-u32 0)
(write-u32 0)
(write-u32 0)
(write-u32 0)
(write-u32 0)
(write-u32 0)
(write-u32 0)
(write-u32 0)
(write-u32 0)
(write-u32 2)
(write-u32 0)
(write-u32 0)
(write-u32 0)
;; Write pixel data to port.
(put-bytevector port (bmp-bytevector bmp))))