-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathretrospectiff2.lisp
824 lines (773 loc) · 38.9 KB
/
retrospectiff2.lisp
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
(in-package :retrospectiff2)
(defun read-grayscale-strip (stream
image-info
array
start-row
strip-offset
strip-byte-count
image-width
bits-per-sample
compression)
(file-position stream strip-offset)
(let ((compressed-bytes (read-bytes stream strip-byte-count)))
(let ((decompressed-bytes (apply (find-compression-decoder compression) compressed-bytes
(when image-info
(list image-info)))))
(ecase bits-per-sample
(1 (let ((bytes-per-row (1+ (ash (1- image-width) -3))))
(let ((strip-length (ceiling (length decompressed-bytes) bytes-per-row)))
(let ((end-row (+ start-row strip-length)))
(loop for i from start-row below end-row
for strip-row-offset from 0
do
(loop for j below image-width by 8
for byte-index from 0
do
(let ((current-byte (aref decompressed-bytes
(+ (* strip-row-offset bytes-per-row) byte-index))))
;; FIXME! Easy enough to support
;; lsb-to-msb here, so do that too please
(loop for bit from 7 downto 0
for jprime from j
do (setf (pixel array i jprime)
(ldb (byte 1 bit) current-byte))))))))))
(4
(let ((bytes-per-row (1+ (ash (1- image-width) -1))))
(let ((strip-length (ceiling (length decompressed-bytes) bytes-per-row)))
(let ((end-row (+ start-row strip-length)))
(loop for i from start-row below end-row
for strip-row-offset from 0
do
(loop for j below image-width by 2
for byte-index from 0
do
(let ((current-byte (aref decompressed-bytes
(+ (* strip-row-offset bytes-per-row) byte-index))))
(loop for bit below 8 by 4
for jprime from j
do (setf (pixel array i jprime)
(ldb (byte 4 (- 4 bit)) current-byte))))))))))
(8
(let ((bytes-per-row image-width))
(let ((strip-length (ceiling (length decompressed-bytes) bytes-per-row)))
(let ((end-row (+ start-row strip-length)))
(loop for i from start-row below end-row
for strip-row-offset from 0
do
(loop for j below image-width
for byte-index from 0
do
(let ((current-byte (aref decompressed-bytes
(+ (* strip-row-offset bytes-per-row) byte-index))))
(setf (pixel array i j) current-byte))))))))
(16
(let ((bytes-per-row (ash image-width 1)))
(let ((strip-length (ceiling (length decompressed-bytes) bytes-per-row)))
(let ((end-row (+ start-row strip-length)))
(loop for i from start-row below end-row
for strip-row-offset from 0
do
(loop for j below image-width
for byte-index from 0 by 2
do
(let ((current-byte1 (aref decompressed-bytes
(+ (* strip-row-offset bytes-per-row) byte-index)))
(current-byte2 (aref decompressed-bytes
(+ (* strip-row-offset bytes-per-row) (1+ byte-index)))))
(setf (pixel array i j)
(ecase *byte-order*
(:little-endian
(+ (ash current-byte2 8)
current-byte1))
(:big-endian
(+ (ash current-byte1 8)
current-byte2)))))))))))))))
(defun grayscale-horizontal-difference-depredict (image max-value)
(destructuring-bind (image-length image-width)
(array-dimensions image)
(loop for i below image-length
do
(loop for j from 1 below image-width
do
(setf (pixel image i j)
(logand
(+ (pixel image i j)
(pixel image i (1- j)))
max-value))))))
(defun read-grayscale-image (stream ifd)
(let ((image-width (get-ifd-value ifd +image-width-tag+))
(image-length (get-ifd-value ifd +image-length-tag+))
(bits-per-sample (or (get-ifd-value ifd +bits-per-sample-tag+) 1))
(compression (get-ifd-value ifd +compression-tag+))
(photometric-interpretation (get-ifd-value ifd +photometric-interpretation-tag+))
(strip-offsets (get-ifd-values ifd +strip-offsets-tag+))
(rows-per-strip (get-ifd-value ifd +rows-per-strip-tag+))
(strip-byte-counts (get-ifd-values ifd +strip-byte-counts-tag+))
(predictor (get-ifd-value ifd +predictor-tag+))
image-info
(jpeg-tables (get-ifd-values ifd +jpeg-tables+)))
(when jpeg-tables
(setf image-info (make-instance 'jpeg-image-info :jpeg-tables jpeg-tables)))
(case bits-per-sample
(1
(let ((data (make-1-bit-gray-image image-length image-width)))
(loop for strip-offset across strip-offsets
for strip-byte-count across strip-byte-counts
for row-offset = 0 then (+ row-offset rows-per-strip)
do (read-grayscale-strip stream image-info data row-offset
strip-offset strip-byte-count
image-width
bits-per-sample
compression))
(make-instance 'tiff-image
:length image-length :width image-width
:bits-per-sample bits-per-sample
:samples-per-pixel 1 :data data
:byte-order *byte-order*
:min-is-white (= photometric-interpretation
+photometric-interpretation-white-is-zero+))))
(4
(let ((data (make-4-bit-gray-image image-length image-width)))
(loop for strip-offset across strip-offsets
for strip-byte-count across strip-byte-counts
for row-offset = 0 then (+ row-offset rows-per-strip)
do (read-grayscale-strip stream image-info data row-offset
strip-offset strip-byte-count
image-width
bits-per-sample
compression))
(make-instance 'tiff-image
:length image-length :width image-width
:bits-per-sample bits-per-sample
:samples-per-pixel 1 :data data
:byte-order *byte-order*)))
(8
(let ((data (make-8-bit-gray-image image-length image-width)))
(loop for strip-offset across strip-offsets
for strip-byte-count across strip-byte-counts
for row-offset = 0 then (+ row-offset rows-per-strip)
do (read-grayscale-strip stream image-info data row-offset
strip-offset strip-byte-count
image-width
bits-per-sample
compression))
(case predictor
(#.+horizontal-differencing+
(grayscale-horizontal-difference-depredict data #xff)))
(make-instance 'tiff-image
:length image-length :width image-width
:bits-per-sample bits-per-sample
:samples-per-pixel 1 :data data
:byte-order *byte-order*)))
(16
(let ((data (make-16-bit-gray-image image-length image-width)))
(loop for strip-offset across strip-offsets
for strip-byte-count across strip-byte-counts
for row-offset = 0 then (+ row-offset rows-per-strip)
do (read-grayscale-strip stream image-info data row-offset
strip-offset strip-byte-count
image-width
bits-per-sample
compression))
(case predictor
(#.+horizontal-differencing+
(grayscale-horizontal-difference-depredict data #xffff)))
(make-instance 'tiff-image
:length image-length :width image-width
:bits-per-sample bits-per-sample
:samples-per-pixel 1 :data data
:byte-order *byte-order*)))
(t
(error "Unsupported grayscale bit depth: ~A" bits-per-sample)))))
(defun rgb-horizontal-difference-depredict (image max-value)
(destructuring-bind (image-length image-width channels)
(array-dimensions image)
(declare (ignore channels))
(loop for i below image-length
do
(loop for j from 1 below image-width
do
(multiple-value-bind (oldr oldg oldb)
(pixel image i (1- j))
(multiple-value-bind (newr newg newb)
(pixel image i i)
(setf (pixel image i j)
(values (logand (+ oldr newr) max-value)
(logand (+ oldg newg) max-value)
(logand (+ oldb newb) max-value)))))))))
(defun read-rgb-strip (stream image-info array start-row strip-offset
strip-byte-count width bits-per-sample samples-per-pixel
bytes-per-pixel compression)
(file-position stream strip-offset)
(let ((compressed-bytes (read-bytes stream strip-byte-count)))
(let ((decompressed-bytes (apply (find-compression-decoder compression) compressed-bytes
(when image-info
(list image-info)))))
(let ((decoded-offset 0))
(let ((strip-length (/ (length decompressed-bytes) width bytes-per-pixel))
(max-bits-per-sample (reduce #'max bits-per-sample)))
(ecase max-bits-per-sample
(8
(loop for i from start-row below (+ start-row strip-length)
do
(loop for j below width
do
(setf (pixel* array i j)
(loop for k below samples-per-pixel
for bits across bits-per-sample
collect
(prog1
(aref decompressed-bytes decoded-offset)
(incf decoded-offset)))))))
(16
(loop for i from start-row below (+ start-row strip-length)
do
(loop for j below width
do
(setf (pixel* array i j)
(loop for k below samples-per-pixel
for bits across bits-per-sample
collect
(prog1
(ecase *byte-order*
(:big-endian
(+ (ash (aref decompressed-bytes decoded-offset) 8)
(aref decompressed-bytes (1+ decoded-offset))))
(:little-endian
(+ (ash (aref decompressed-bytes (1+ decoded-offset)) 8)
(aref decompressed-bytes decoded-offset))))
(incf decoded-offset 2)))))))))))))
(defun read-planar-rgb-strip (stream image-info array start-row strip-offset
strip-byte-count width plane-bits-per-sample samples-per-pixel
bytes-per-pixel compression plane)
(file-position stream strip-offset)
(let ((compressed-bytes (read-bytes stream strip-byte-count)))
(let* ((decompressed-bytes (apply (find-compression-decoder compression) compressed-bytes
(when image-info
(list image-info))))
(decoded-offset 0)
(bytes-per-sample (/ bytes-per-pixel samples-per-pixel))
(strip-length (/ (length decompressed-bytes) width bytes-per-sample)))
(ecase plane-bits-per-sample
(8
(loop for i from start-row below (+ start-row strip-length)
do
(loop for j below width
do
(setf (aref array i j plane)
(aref decompressed-bytes decoded-offset))
(incf decoded-offset))))
(16
(loop for i from start-row below (+ start-row strip-length)
do
(loop for j below width
do
(setf (aref array i j plane)
(ecase *byte-order*
(:big-endian
(+ (ash (aref decompressed-bytes decoded-offset) 8)
(aref decompressed-bytes (1+ decoded-offset))))
(:little-endian
(+ (ash (aref decompressed-bytes (1+ decoded-offset)) 8)
(aref decompressed-bytes decoded-offset)))))
(incf decoded-offset 2))))))))
(defun read-rgb-image (stream ifd)
(let ((image-width (get-ifd-value ifd +image-width-tag+))
(image-length (get-ifd-value ifd +image-length-tag+))
(samples-per-pixel (get-ifd-value ifd +samples-per-pixel-tag+))
(bits-per-sample (get-ifd-values ifd +bits-per-sample-tag+))
(rows-per-strip (get-ifd-value ifd +rows-per-strip-tag+))
(strip-offsets (get-ifd-values ifd +strip-offsets-tag+))
(strip-byte-counts (get-ifd-values ifd +strip-byte-counts-tag+))
(compression (get-ifd-value ifd +compression-tag+))
;; default planar-configuration is +planar-configuration-chunky+
(planar-configuration (or (get-ifd-value ifd +planar-configuration-tag+)
+planar-configuration-chunky+))
(predictor (get-ifd-value ifd +predictor-tag+))
image-info
(jpeg-tables (get-ifd-values ifd +jpeg-tables+)))
(when jpeg-tables
(setf image-info (make-instance 'jpeg-image-info :jpeg-tables jpeg-tables)))
;; FIXME
;; 1. we need to support predictors for lzw encoded images.
;; 2. Presumably we'll want planar images as well at some point.
(let* ((max-bits-per-sample (reduce #'max bits-per-sample))
(bytes-per-pixel
(* samples-per-pixel (1+ (ash (1- max-bits-per-sample) -3)))))
(let ((data
(ecase max-bits-per-sample
(8 (make-8-bit-rgb-image image-length image-width))
(16 (make-16-bit-rgb-image image-length image-width)))))
(case planar-configuration
(#.+planar-configuration-chunky+
(loop for strip-offset across strip-offsets
for strip-byte-count across strip-byte-counts
for row-offset = 0 then (+ row-offset rows-per-strip)
do (read-rgb-strip stream
image-info
data
row-offset
strip-offset
strip-byte-count
image-width
bits-per-sample
samples-per-pixel
bytes-per-pixel
compression))
(case predictor
(#.+horizontal-differencing+
(rgb-horizontal-difference-depredict data (1- (ash 1 max-bits-per-sample)))))
(make-instance 'tiff-image
:length image-length :width image-width
:bits-per-sample bits-per-sample
:samples-per-pixel samples-per-pixel
:data data :byte-order *byte-order*))
(#.+planar-configuration-planar+
(let* ((strips-per-image
(floor (+ image-length rows-per-strip -1) rows-per-strip)))
(loop for plane below samples-per-pixel
do
(let ((plane-bits-per-sample (elt bits-per-sample plane)))
(loop for strip-offset across (subseq strip-offsets
(* plane strips-per-image)
(* (1+ plane) strips-per-image))
for strip-byte-count across (subseq strip-byte-counts
(* plane strips-per-image)
(* (1+ plane) strips-per-image))
for row-offset = 0 then (+ row-offset rows-per-strip)
do (read-planar-rgb-strip stream
image-info
data
row-offset
strip-offset
strip-byte-count
image-width
plane-bits-per-sample
samples-per-pixel
bytes-per-pixel
compression
plane))))
(case predictor
(rgb-horizontal-difference-depredict data (1- (ash 1 max-bits-per-sample))))
(make-instance 'tiff-image
:length image-length :width image-width
:bits-per-sample bits-per-sample
:samples-per-pixel samples-per-pixel
:data data :byte-order *byte-order*)))
(t
(error "Planar Configuration ~A not supported." planar-configuration)))))))
(defun read-indexed-image (stream ifd)
(let ((image-width (get-ifd-value ifd +image-width-tag+))
(image-length (get-ifd-value ifd +image-length-tag+))
(bits-per-sample (get-ifd-value ifd +bits-per-sample-tag+))
(rows-per-strip (get-ifd-value ifd +rows-per-strip-tag+))
(strip-offsets (get-ifd-values ifd +strip-offsets-tag+))
(strip-byte-counts (get-ifd-values ifd +strip-byte-counts-tag+))
(compression (get-ifd-value ifd +compression-tag+))
(predictor (get-ifd-value ifd +predictor-tag+))
image-info
(jpeg-tables (get-ifd-values ifd +jpeg-tables+))
(color-map (get-ifd-values ifd +color-map-tag+)))
(when jpeg-tables
(setf image-info (make-instance 'jpeg-image-info :jpeg-tables jpeg-tables)))
(let* ((k (expt 2 bits-per-sample))
(color-index (make-array k)))
(loop for i below k
do (setf (aref color-index i)
(list (aref color-map i)
(aref color-map (+ k i))
(aref color-map (+ (ash k 1) i)))))
;; FIXME
;; 1. we need to support predictors for lzw encoded images.
(ecase bits-per-sample
(8
(let ((data (make-8-bit-gray-image image-length image-width)))
(loop for strip-offset across strip-offsets
for strip-byte-count across strip-byte-counts
for row-offset = 0 then (+ row-offset rows-per-strip)
do (read-grayscale-strip stream
image-info
data
row-offset
strip-offset
strip-byte-count
image-width
bits-per-sample
compression))
(case predictor
(#.+horizontal-differencing+
(grayscale-horizontal-difference-depredict data #xff)))
(make-instance 'tiff-image
:length image-length :width image-width
:bits-per-sample bits-per-sample
:data data :byte-order *byte-order*
:color-map color-index)))
(16
(let ((data (make-16-bit-gray-image image-length image-width)))
(loop for strip-offset across strip-offsets
for strip-byte-count across strip-byte-counts
for row-offset = 0 then (+ row-offset rows-per-strip)
do (read-grayscale-strip stream
image-info
data
row-offset
strip-offset
strip-byte-count
image-width
bits-per-sample
compression))
(case predictor
(#.+horizontal-differencing+
(grayscale-horizontal-difference-depredict data #xffff)))
(make-instance 'tiff-image
:length image-length :width image-width
:bits-per-sample bits-per-sample
:data data :byte-order *byte-order*
:color-map color-index)))))))
(defun read-tiff-stream (stream)
(let* ((fields (read-value 'tiff-fields stream))
(ifd (entries (first (ifd-list fields)))))
(let ((photometric-interpretation
(get-ifd-value ifd +photometric-interpretation-tag+)))
(ecase photometric-interpretation
(#.+photometric-interpretation-white-is-zero+
;; FIXME! This image should be inverted
(read-grayscale-image stream ifd))
(#.+photometric-interpretation-black-is-zero+
(read-grayscale-image stream ifd))
(#.+photometric-interpretation-rgb+
(read-rgb-image stream ifd))
(#.+photometric-interpretation-palette-color+
(read-indexed-image stream ifd))))))
(defun read-tiff-file (pathname)
(with-open-file (stream pathname :direction :input :element-type '(unsigned-byte 8))
(read-tiff-stream stream)))
(defun make-tiff-image-for-output (image)
(typecase image
(16-bit-gray-image
(locally
(declare (type 16-bit-gray-image image))
(destructuring-bind (height width)
(array-dimensions image)
(let ((tiff-image (make-instance 'tiff:tiff-image
:width width
:length height
:bits-per-sample 16
:samples-per-pixel 1
:data (make-array (* width height 2)))))
(with-accessors ((image-data tiff:tiff-image-data))
tiff-image
(let ((pixoff 0))
(loop for i below height
do
(loop for j below width
do
(let ((pixval (pixel image i j)))
(ecase *byte-order*
(:little-endian
(setf (aref image-data pixoff) (logand pixval #xff))
(incf pixoff)
(setf (aref image-data pixoff) (ash pixval -8))
(incf pixoff))
(:big-endian
(setf (aref image-data pixoff) (ash pixval -8))
(incf pixoff)
(setf (aref image-data pixoff) (logand pixval #xff))
(incf pixoff))))))))
tiff-image))))
(16-bit-rgb-image
(locally
(declare (type 16-bit-rgb-image image))
(destructuring-bind (height width channels)
(array-dimensions image)
(declare (ignore channels))
(let ((tiff-image (make-instance 'tiff:tiff-image
:width width
:length height
:bits-per-sample '(16 16 16)
:samples-per-pixel 3
:data (make-array (* width height 3 2)))))
(with-accessors ((image-data tiff:tiff-image-data))
tiff-image
(loop for i below height
do
(loop for j below width
do
(let ((pixoff (* 3 2 (+ (* i width) j))))
(multiple-value-bind
(r g b)
(pixel image i j)
(ecase *byte-order*
(:little-endian
(setf (aref image-data pixoff) (logand r #xff)
(aref image-data (incf pixoff)) (ash r -8)
(aref image-data (incf pixoff)) (logand g #xff)
(aref image-data (incf pixoff)) (ash g -8)
(aref image-data (incf pixoff)) (logand b #xff)
(aref image-data (incf pixoff)) (ash b -8)))
(:big-endian
(setf (aref image-data pixoff) (ash r -8)
(aref image-data (incf pixoff)) (logand r #xff)
(aref image-data (incf pixoff)) (ash g -8)
(aref image-data (incf pixoff)) (logand g #xff)
(aref image-data (incf pixoff)) (ash b -8)
(aref image-data (incf pixoff)) (logand b #xff)))))))))
tiff-image))))
(16-bit-rgba-image
(locally
(declare (type 16-bit-rgba-image image))
(destructuring-bind (height width channels)
(array-dimensions image)
(declare (ignore channels))
(let ((tiff-image (make-instance 'tiff:tiff-image
:width width
:length height
:bits-per-sample '(16 16 16 16)
:samples-per-pixel 4
:data (make-array (* width height 4 2)))))
(with-accessors ((image-data tiff:tiff-image-data))
tiff-image
(loop for i below height
do
(loop for j below width
do
(let ((pixoff (* 4 2 (+ (* i width) j))))
(multiple-value-bind
(r g b a)
(pixel image i j)
(setf (aref image-data pixoff) (ash r -8)
(aref image-data (incf pixoff)) (logand r #xff)
(aref image-data (incf pixoff)) (ash g -8)
(aref image-data (incf pixoff)) (logand g #xff)
(aref image-data (incf pixoff)) (ash b -8)
(aref image-data (incf pixoff)) (logand b #xff)
(aref image-data (incf pixoff)) (ash a -8)
(aref image-data (incf pixoff)) (logand a #xff)))))))
tiff-image))))
(8-bit-gray-image
(locally
(declare (type 8-bit-gray-image image))
(destructuring-bind (height width)
(array-dimensions image)
(let ((tiff-image (make-instance 'tiff:tiff-image
:width width
:length height
:bits-per-sample 8
:samples-per-pixel 1
:data (make-array (* width height)
:initial-element 255))))
(with-accessors ((image-data tiff:tiff-image-data))
tiff-image
(let ((pixoff 0))
(loop for i below height
do
(loop for j below width
do
(setf (aref image-data pixoff) (pixel image i j))
(incf pixoff)))))
tiff-image))))
(8-bit-gray-alpha-image
(locally
(declare (type 8-bit-gray-alpha-image image))
(destructuring-bind (height width channels)
(array-dimensions image)
(unless (equal channels 2)
(error "incorrect number of chanbnels in 8-bit-gray-alpha-image"))
(let ((tiff-image (make-instance 'tiff:tiff-image
:width width
:length height
:bits-per-sample 8
:samples-per-pixel 2
:data (make-array (* width height 2)
:initial-element 255))))
(with-accessors ((image-data tiff:tiff-image-data))
tiff-image
(loop for i below height
do
(loop for j below width
do
(let ((pixoff (* 2 (+ (* i width) j))))
(multiple-value-bind
(gray alpha)
(pixel image i j)
(setf (aref image-data pixoff) gray
(aref image-data (incf pixoff)) alpha))
(incf pixoff)))))
tiff-image))))
(8-bit-rgb-image
(locally
(declare (type 8-bit-rgb-image image))
(destructuring-bind (height width channels)
(array-dimensions image)
(declare (ignore channels))
(let ((tiff-image (make-instance 'tiff:tiff-image
:width width
:length height
:bits-per-sample '(8 8 8)
:samples-per-pixel 3
:data (make-array (* width height 3)))))
(with-accessors ((image-data tiff:tiff-image-data))
tiff-image
(loop for i below height
do
(loop for j below width
do
(let ((pixoff (* 3 (+ (* i width) j))))
(multiple-value-bind
(r g b)
(pixel image i j)
(setf (aref image-data pixoff) r
(aref image-data (incf pixoff)) g
(aref image-data (incf pixoff)) b))))))
tiff-image))))
(8-bit-rgba-image
(locally
(declare (type 8-bit-rgba-image image))
(destructuring-bind (height width channels)
(array-dimensions image)
(declare (ignore channels))
(let ((tiff-image (make-instance 'tiff:tiff-image
:width width
:length height
:bits-per-sample '(8 8 8 8)
:samples-per-pixel 4
:data (make-array (* width height 4)))))
(with-accessors ((image-data tiff:tiff-image-data))
tiff-image
(loop for i below height
do
(loop for j below width
do
(let ((pixoff (* 4 (+ (* i width) j))))
(multiple-value-bind
(r g b a)
(pixel image i j)
(setf (aref image-data pixoff) r
(aref image-data (incf pixoff)) g
(aref image-data (incf pixoff)) b
(aref image-data (incf pixoff)) a))))))
tiff-image))))
(4-bit-gray-image
(locally
(declare (type 4-bit-gray-image image))
(destructuring-bind (height width)
(array-dimensions image)
(let ((tiff-image (make-instance 'tiff:tiff-image
:width width
:length height
:bits-per-sample 4
:samples-per-pixel 1
:data (make-array (* width height)
:initial-element 15))))
(with-accessors ((image-data tiff:tiff-image-data))
tiff-image
(let ((byte-offset 0)
(nibble 0))
(loop for i below height
do
(loop for j below width
do
(if (zerop nibble)
(progn
(setf (ldb (byte 4 4)
(aref image-data byte-offset))
(pixel image i j))
(incf nibble))
(progn
(setf (ldb (byte 4 0)
(aref image-data byte-offset))
(pixel image i j))
(setf nibble 0)
(incf byte-offset)))))))
tiff-image))))
(1-bit-gray-image
(locally
(declare (type 1-bit-gray-image image))
(destructuring-bind (height width)
(array-dimensions image)
(let ((tiff-image (make-instance 'tiff:tiff-image
:width width
:length height
:bits-per-sample 1
:samples-per-pixel 1
:data (make-array (* width height)
:initial-element 1))))
(with-accessors ((image-data tiff:tiff-image-data))
tiff-image
(let ((byte-offset 0)
(bit-offset 0))
(loop for i below height
do
(loop for j below width
do
(if (< bit-offset 7)
(progn
(setf (ldb (byte 1 (- 7 bit-offset))
(aref image-data byte-offset))
(pixel image i j))
(incf bit-offset))
(progn
(setf (ldb (byte 1 0)
(aref image-data byte-offset))
(pixel image i j))
(setf bit-offset 0)
(incf byte-offset)))))))
tiff-image))))
(t (error "Cannot write a TIFF image from ~A" (type-of image)))))
;;;
;;; The general strategy here is to:
;;;
;;; 1. make the TIFF Image File Directory (we're only going to deal
;;; with single images per TIFF file for the moment)
;;;
;;; 2. Compute the offsets of the first (and only IFD -- probably 8)
;;;
;;; 3. Compute the offset of the various IFD arrays that aren't
;;; represented inline -- starting at the offset of the IFD + (2 +
;;; number of directory entries * 12)
;;;
;;; 4. Compute the offset of the strip/sample data
;;;
;;; 5. Write the TIFF Header
;;;
;;; 6. Write the IFD directory entries (inline portions), then write
;;; the non-inline values
;;;
;;; 7. Write the sample (strip) data
;;;
;;; FIXME! Writing indexed tiff images is broken!
(defun write-tiff-stream (stream image &key byte-order)
(let ((*byte-order* (or byte-order *byte-order*))
(*tiff-file-offset* 0))
;; FIXME! The typecase below is a hack to get around the fact that
;; we need to handle both a tiff-image and an opticl-core:image
;; here in order to keep opticl happy. I think we need two
;; write-tiff-{file,stream} flavors, one for opticl-core:image and
;; one for tiff-image.
(let ((obj (etypecase
image
(opticl-core:image (make-tiff-image-for-output image))
(t (make-tiff-image-for-output (tiff-image-data image))))))
(multiple-value-bind (fields out-of-line-data-size strip-offsets strip-byte-counts)
(make-tiff-fields obj)
(write-value 'tiff-fields stream fields)
(file-position stream (+ (file-position stream) out-of-line-data-size))
(with-accessors
((image-width tiff-image-width)
(image-length tiff-image-length)
(image-data tiff-image-data))
obj
;; need to convert our opticl-core image into proper TIFF strippable-data
(loop for start in strip-offsets
for count in strip-byte-counts
do
(write-sequence (subseq image-data start
(+ start count))
stream)))))))
(defun write-tiff-file (pathname image &rest args &key (if-exists :error) &allow-other-keys)
(with-open-file (stream pathname
:direction :output
:element-type '(unsigned-byte 8)
:if-exists if-exists)
(apply #'write-tiff-stream stream image (remove-keyword-args :if-exists args))
pathname))