1 ! Copyright (C) 2009 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs byte-arrays classes combinators
4 compression.lzw endian fry grouping images io
5 io.binary io.encodings.ascii io.encodings.binary
6 io.encodings.string io.encodings.utf8 io.files kernel math
7 math.bitwise math.order math.parser pack prettyprint sequences
8 strings math.vectors specialized-arrays locals
10 SPECIALIZED-ARRAY: float
15 TUPLE: loading-tiff endianness the-answer ifd-offset ifds ;
17 : <loading-tiff> ( -- tiff )
18 loading-tiff new V{ } clone >>ifds ;
20 TUPLE: ifd count ifd-entries next
21 processed-tags strips bitmap ;
23 : <ifd> ( count ifd-entries next -- ifd )
29 TUPLE: ifd-entry tag type count offset/value ;
31 : <ifd-entry> ( tag type count offset/value -- ifd-entry )
38 SINGLETONS: photometric-interpretation
39 photometric-interpretation-white-is-zero
40 photometric-interpretation-black-is-zero
41 photometric-interpretation-rgb
42 photometric-interpretation-palette-color
43 photometric-interpretation-transparency-mask
44 photometric-interpretation-separated
45 photometric-interpretation-ycbcr
46 photometric-interpretation-cielab
47 photometric-interpretation-icclab
48 photometric-interpretation-itulab
49 photometric-interpretation-logl
50 photometric-interpretation-logluv ;
52 ERROR: bad-photometric-interpretation n ;
53 : lookup-photometric-interpretation ( n -- singleton )
55 { 0 [ photometric-interpretation-white-is-zero ] }
56 { 1 [ photometric-interpretation-black-is-zero ] }
57 { 2 [ photometric-interpretation-rgb ] }
58 { 3 [ photometric-interpretation-palette-color ] }
59 { 4 [ photometric-interpretation-transparency-mask ] }
60 { 5 [ photometric-interpretation-separated ] }
61 { 6 [ photometric-interpretation-ycbcr ] }
62 { 8 [ photometric-interpretation-cielab ] }
63 { 9 [ photometric-interpretation-icclab ] }
64 { 10 [ photometric-interpretation-itulab ] }
65 { 32844 [ photometric-interpretation-logl ] }
66 { 32845 [ photometric-interpretation-logluv ] }
67 [ bad-photometric-interpretation ]
70 SINGLETONS: compression
78 compression-adobe-deflate
85 compression-thunderscan
97 ERROR: bad-compression n ;
98 : lookup-compression ( n -- compression )
100 { 1 [ compression-none ] }
101 { 2 [ compression-CCITT-2 ] }
102 { 3 [ compression-CCITT-3 ] }
103 { 4 [ compression-CCITT-4 ] }
104 { 5 [ compression-lzw ] }
105 { 6 [ compression-jpeg-old ] }
106 { 7 [ compression-jpeg-new ] }
107 { 8 [ compression-adobe-deflate ] }
108 { 9 [ compression-9 ] }
109 { 10 [ compression-10 ] }
110 { 32766 [ compression-next ] }
111 { 32771 [ compression-ccittrlew ] }
112 { 32773 [ compression-pack-bits ] }
113 { 32809 [ compression-thunderscan ] }
114 { 32895 [ compression-it8ctpad ] }
115 { 32896 [ compression-it8lw ] }
116 { 32897 [ compression-it8mp ] }
117 { 32898 [ compression-it8bl ] }
118 { 32908 [ compression-pixarfilm ] }
119 { 32909 [ compression-pixarlog ] }
120 { 32946 [ compression-deflate ] }
121 { 32947 [ compression-dcs ] }
122 { 34661 [ compression-jbig ] }
123 { 34676 [ compression-sgilog ] }
124 { 34677 [ compression-sgilog24 ] }
125 { 34712 [ compression-jp2000 ] }
129 SINGLETONS: resolution-unit
132 resolution-unit-centimeter ;
133 ERROR: bad-resolution-unit n ;
134 : lookup-resolution-unit ( n -- object )
136 { 1 [ resolution-unit-none ] }
137 { 2 [ resolution-unit-inch ] }
138 { 3 [ resolution-unit-centimeter ] }
139 [ bad-resolution-unit ]
142 SINGLETONS: predictor
144 predictor-horizontal-differencing ;
145 ERROR: bad-predictor n ;
146 : lookup-predictor ( n -- object )
148 { 1 [ predictor-none ] }
149 { 2 [ predictor-horizontal-differencing ] }
153 SINGLETONS: planar-configuration
154 planar-configuration-chunky
155 planar-configuration-planar ;
156 ERROR: bad-planar-configuration n ;
157 : lookup-planar-configuration ( n -- object )
159 { 1 [ planar-configuration-chunky ] }
160 { 2 [ planar-configuration-planar ] }
161 [ bad-planar-configuration ]
164 SINGLETONS: sample-format
166 sample-format-unsigned-integer
167 sample-format-signed-integer
168 sample-format-ieee-float
169 sample-format-undefined-data ;
170 ERROR: bad-sample-format n ;
171 : lookup-sample-format ( sequence -- object )
174 { 0 [ sample-format-none ] }
175 { 1 [ sample-format-unsigned-integer ] }
176 { 2 [ sample-format-signed-integer ] }
177 { 3 [ sample-format-ieee-float ] }
178 { 4 [ sample-format-undefined-data ] }
179 [ bad-sample-format ]
183 SINGLETONS: extra-samples
184 extra-samples-unspecified-alpha-data
185 extra-samples-associated-alpha-data
186 extra-samples-unassociated-alpha-data ;
187 ERROR: bad-extra-samples n ;
188 : lookup-extra-samples ( sequence -- object )
190 { 0 [ extra-samples-unspecified-alpha-data ] }
191 { 1 [ extra-samples-associated-alpha-data ] }
192 { 2 [ extra-samples-unassociated-alpha-data ] }
193 [ bad-extra-samples ]
196 SINGLETONS: image-length image-width x-resolution y-resolution
197 rows-per-strip strip-offsets strip-byte-counts bits-per-sample
198 samples-per-pixel new-subfile-type subfile-type orientation
199 software date-time photoshop exif-ifd sub-ifd inter-color-profile
200 xmp iptc fill-order document-name page-number page-name
201 x-position y-position host-computer copyright artist
202 min-sample-value max-sample-value tiff-make tiff-model cell-width cell-length
203 gray-response-unit gray-response-curve color-map threshholding
204 image-description free-offsets free-byte-counts tile-width tile-length
205 matteing data-type image-depth tile-depth
206 ycbcr-subsampling gdal-metadata
207 tile-offsets tile-byte-counts jpeg-qtables jpeg-dctables jpeg-actables
208 ycbcr-positioning ycbcr-coefficients reference-black-white halftone-hints
209 jpeg-interchange-format
210 jpeg-interchange-format-length
211 jpeg-restart-interval jpeg-tables
212 t4-options clean-fax-data bad-fax-lines consecutive-bad-fax-lines
213 sto-nits print-image-matching-info
214 unhandled-ifd-entry ;
216 SINGLETONS: jpeg-proc
220 ERROR: bad-jpeg-proc n ;
222 : lookup-jpeg-proc ( sequence -- object )
224 { 1 [ jpeg-proc-baseline ] }
225 { 14 [ jpeg-proc-lossless ] }
229 ERROR: bad-tiff-magic bytes ;
230 : tiff-endianness ( byte-array -- ? )
232 { B{ CHAR: M CHAR: M } [ big-endian ] }
233 { B{ CHAR: I CHAR: I } [ little-endian ] }
237 : read-header ( tiff -- tiff )
238 2 read tiff-endianness [ >>endianness ] keep
240 2 read endian> >>the-answer
241 4 read endian> >>ifd-offset
244 : push-ifd ( tiff ifd -- tiff ) over ifds>> push ;
246 : read-ifd ( -- ifd )
250 4 read endian> <ifd-entry> ;
252 : read-ifds ( tiff offset -- tiff )
253 seek-absolute seek-input
255 dup [ read-ifd ] replicate
257 [ <ifd> push-ifd ] [ dup 0 = [ drop ] [ read-ifds ] if ] bi ;
259 ERROR: no-tag class ;
261 : find-tag* ( ifd class -- tag/class ? )
262 swap processed-tags>> ?at ;
264 : find-tag ( ifd class -- tag )
265 find-tag* [ no-tag ] unless ;
267 : tag? ( ifd class -- tag )
268 swap processed-tags>> key? ;
270 : read-strips ( ifd -- ifd )
272 [ strip-byte-counts find-tag ]
273 [ strip-offsets find-tag ] bi
274 2dup [ integer? ] both? [
275 seek-absolute seek-input read 1array
277 [ seek-absolute seek-input read ] { } 2map-as
280 ERROR: unknown-ifd-type n ;
282 : bytes>bits ( n/byte-array -- n )
283 dup byte-array? [ byte-array>bignum ] when ;
285 : value-length ( ifd-entry -- n )
286 [ count>> ] [ type>> ] bi {
303 ERROR: bad-small-ifd-type n ;
305 : adjust-offset/value ( ifd-entry -- obj )
306 [ offset/value>> 4 >endian ] [ type>> ] bi
308 { 1 [ 1 head endian> ] }
309 { 3 [ 2 head endian> ] }
311 { 6 [ 1 head endian> 8 >signed ] }
312 { 8 [ 2 head endian> 16 >signed ] }
313 { 9 [ endian> 32 >signed ] }
314 { 11 [ endian> bits>float ] }
315 { 13 [ endian> 32 >signed ] }
316 [ bad-small-ifd-type ]
319 : offset-bytes>obj ( bytes type -- obj )
322 { 2 [ ] } ! read c strings here
323 { 3 [ 2 <sliced-groups> [ endian> ] map ] }
324 { 4 [ 4 <sliced-groups> [ endian> ] map ] }
325 { 5 [ 8 <sliced-groups> [ "II" unpack first2 / ] map ] }
326 { 6 [ [ 8 >signed ] map ] }
328 { 8 [ 2 <sliced-groups> [ endian> 16 >signed ] map ] }
329 { 9 [ 4 <sliced-groups> [ endian> 32 >signed ] map ] }
330 { 10 [ 8 group [ "ii" unpack first2 / ] map ] }
331 { 11 [ 4 group [ "f" unpack ] map ] }
332 { 12 [ 8 group [ "d" unpack ] map ] }
336 : ifd-entry-value ( ifd-entry -- n )
337 dup value-length 4 <= [
340 [ offset/value>> seek-absolute seek-input ]
341 [ value-length read ]
342 [ type>> ] tri offset-bytes>obj
345 : process-ifd-entry ( ifd-entry -- value class )
346 [ ifd-entry-value ] [ tag>> ] bi {
347 { 254 [ new-subfile-type ] }
348 { 255 [ subfile-type ] }
349 { 256 [ image-width ] }
350 { 257 [ image-length ] }
351 { 258 [ bits-per-sample ] }
352 { 259 [ lookup-compression compression ] }
353 { 262 [ lookup-photometric-interpretation photometric-interpretation ] }
354 { 263 [ threshholding ] }
355 { 264 [ cell-width ] }
356 { 265 [ cell-length ] }
357 { 266 [ fill-order ] }
358 { 269 [ ascii decode document-name ] }
359 { 270 [ ascii decode image-description ] }
360 { 271 [ ascii decode tiff-make ] }
361 { 272 [ ascii decode tiff-model ] }
362 { 273 [ strip-offsets ] }
363 { 274 [ orientation ] }
364 { 277 [ samples-per-pixel ] }
365 { 278 [ rows-per-strip ] }
366 { 279 [ strip-byte-counts ] }
367 { 280 [ min-sample-value ] }
368 { 281 [ max-sample-value ] }
369 { 282 [ first x-resolution ] }
370 { 283 [ first y-resolution ] }
371 { 284 [ lookup-planar-configuration planar-configuration ] }
372 { 285 [ page-name ] }
373 { 286 [ x-position ] }
374 { 287 [ y-position ] }
375 { 288 [ free-offsets ] }
376 { 289 [ free-byte-counts ] }
377 { 290 [ gray-response-unit ] }
378 { 291 [ gray-response-curve ] }
379 { 292 [ t4-options ] }
380 { 296 [ lookup-resolution-unit resolution-unit ] }
381 { 297 [ page-number ] }
382 { 305 [ ascii decode software ] }
383 { 306 [ ascii decode date-time ] }
384 { 315 [ ascii decode artist ] }
385 { 316 [ ascii decode host-computer ] }
386 { 317 [ lookup-predictor predictor ] }
387 { 320 [ color-map ] }
388 { 321 [ halftone-hints ] }
389 { 322 [ tile-width ] }
390 { 323 [ tile-length ] }
391 { 324 [ tile-offsets ] }
392 { 325 [ tile-byte-counts ] }
393 { 326 [ bad-fax-lines ] }
394 { 327 [ clean-fax-data ] }
395 { 328 [ consecutive-bad-fax-lines ] }
397 { 338 [ lookup-extra-samples extra-samples ] }
398 { 339 [ lookup-sample-format sample-format ] }
399 { 347 [ jpeg-tables ] }
400 { 512 [ lookup-jpeg-proc jpeg-proc ] }
401 { 513 [ jpeg-interchange-format ] }
402 { 514 [ jpeg-interchange-format-length ] }
403 { 515 [ jpeg-restart-interval ] }
404 { 519 [ jpeg-qtables ] }
405 { 520 [ jpeg-dctables ] }
406 { 521 [ jpeg-actables ] }
407 { 529 [ ycbcr-coefficients ] }
408 { 530 [ ycbcr-subsampling ] }
409 { 531 [ ycbcr-positioning ] }
410 { 532 [ reference-black-white ] }
411 { 700 [ utf8 decode xmp ] }
412 { 32995 [ matteing ] }
413 { 32996 [ data-type ] }
414 { 32997 [ image-depth ] }
415 { 32998 [ tile-depth ] }
416 { 33432 [ copyright ] }
418 { 34377 [ photoshop ] }
419 { 34665 [ exif-ifd ] }
420 { 34675 [ inter-color-profile ] }
421 { 37439 [ sto-nits ] }
422 { 42112 [ gdal-metadata ] }
423 { 50341 [ print-image-matching-info ] }
424 [ nip unhandled-ifd-entry swap ]
427 : process-ifds ( loading-tiff -- loading-tiff )
431 [ process-ifd-entry swap ] H{ } map>assoc >>processed-tags
435 ERROR: unhandled-compression compression ;
437 : (uncompress-strips) ( strips compression -- uncompressed-strips )
439 { compression-none [ ] }
440 { compression-lzw [ [ lzw-uncompress ] map ] }
441 [ unhandled-compression ]
444 : uncompress-strips ( ifd -- ifd )
446 _ compression find-tag (uncompress-strips)
449 : strips>bitmap ( ifd -- ifd )
450 dup strips>> concat >>bitmap ;
452 : (strips-predictor) ( ifd -- ifd )
454 [ image-width find-tag ]
455 [ samples-per-pixel find-tag ] tri
459 [ _ group unclip [ v+ ] accumulate swap suffix concat ] map
463 : strips-predictor ( ifd -- ifd )
465 dup predictor find-tag
467 { predictor-none [ ] }
468 { predictor-horizontal-differencing [ (strips-predictor) ] }
473 ERROR: unknown-component-order ifd ;
475 : fix-bitmap-endianness ( ifd -- ifd )
476 dup [ bitmap>> ] [ bits-per-sample find-tag ] bi
478 { { 32 32 32 32 } [ 4 seq>native-endianness ] }
479 { { 32 32 32 } [ 4 seq>native-endianness ] }
480 { { 16 16 16 16 } [ 2 seq>native-endianness ] }
481 { { 16 16 16 } [ 2 seq>native-endianness ] }
485 [ unknown-component-order ]
488 : ifd-component-order ( ifd -- component-order component-type )
489 bits-per-sample find-tag {
490 { { 32 32 32 32 } [ RGBA float-components ] }
491 { { 32 32 32 } [ RGB float-components ] }
492 { { 16 16 16 16 } [ RGBA ushort-components ] }
493 { { 16 16 16 } [ RGB ushort-components ] }
494 { { 8 8 8 8 } [ RGBA ubyte-components ] }
495 { { 8 8 8 } [ RGB ubyte-components ] }
496 { 8 [ LA ubyte-components ] }
497 [ unknown-component-order ]
500 : handle-alpha-data ( ifd -- ifd )
501 dup extra-samples find-tag {
502 { extra-samples-associated-alpha-data [ ] }
503 { extra-samples-unspecified-alpha-data [ ] }
504 { extra-samples-unassociated-alpha-data [ ] }
505 [ bad-extra-samples ]
508 : ifd>image ( ifd -- image )
510 [ [ image-width find-tag ] [ image-length find-tag ] bi 2array >>dim ]
511 [ ifd-component-order [ >>component-order ] [ >>component-type ] bi* ]
512 [ bitmap>> >>bitmap ]
515 : tiff>image ( image -- image )
516 ifds>> [ ifd>image ] map first ;
518 : with-tiff-endianness ( loading-tiff quot -- )
519 [ dup endianness>> ] dip with-endianness ; inline
521 : load-tiff-ifds ( stream -- loading-tiff )
525 dup ifd-offset>> read-ifds
527 ] with-tiff-endianness
528 ] with-input-stream* ;
530 : process-chunky-ifd ( ifd -- )
534 fix-bitmap-endianness
536 dup extra-samples tag? [ handle-alpha-data ] when
539 : process-planar-ifd ( ifd -- )
540 "planar ifd not supported" throw ;
542 : dispatch-planar-configuration ( ifd planar-configuration -- )
544 { planar-configuration-chunky [ process-chunky-ifd ] }
545 { planar-configuration-planar [ process-planar-ifd ] }
548 : process-ifd ( ifd -- )
549 dup planar-configuration find-tag* [
550 dispatch-planar-configuration
552 drop "no planar configuration" throw
555 : process-tif-ifds ( loading-tiff -- )
556 ifds>> [ process-ifd ] each ;
558 : load-tiff ( path -- loading-tiff )
559 [ load-tiff-ifds dup ]
561 [ [ 0 seek-absolute ] dip stream-seek ]
564 [ process-tif-ifds ] with-tiff-endianness
569 ! tiff files can store several images -- we just take the first for now
570 M: tiff-image stream>image ( stream tiff-image -- image )
571 drop load-tiff tiff>image ;
573 { "tif" "tiff" } [ tiff-image register-image-class ] each