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.float locals
14 TUPLE: loading-tiff endianness the-answer ifd-offset ifds ;
16 : <loading-tiff> ( -- tiff )
17 loading-tiff new V{ } clone >>ifds ;
19 TUPLE: ifd count ifd-entries next
20 processed-tags strips bitmap ;
22 : <ifd> ( count ifd-entries next -- ifd )
28 TUPLE: ifd-entry tag type count offset/value ;
30 : <ifd-entry> ( tag type count offset/value -- ifd-entry )
37 SINGLETONS: photometric-interpretation
38 photometric-interpretation-white-is-zero
39 photometric-interpretation-black-is-zero
40 photometric-interpretation-rgb
41 photometric-interpretation-palette-color
42 photometric-interpretation-transparency-mask
43 photometric-interpretation-separated
44 photometric-interpretation-ycbcr
45 photometric-interpretation-cielab
46 photometric-interpretation-icclab
47 photometric-interpretation-itulab
48 photometric-interpretation-logl
49 photometric-interpretation-logluv ;
51 ERROR: bad-photometric-interpretation n ;
52 : lookup-photometric-interpretation ( n -- singleton )
54 { 0 [ photometric-interpretation-white-is-zero ] }
55 { 1 [ photometric-interpretation-black-is-zero ] }
56 { 2 [ photometric-interpretation-rgb ] }
57 { 3 [ photometric-interpretation-palette-color ] }
58 { 4 [ photometric-interpretation-transparency-mask ] }
59 { 5 [ photometric-interpretation-separated ] }
60 { 6 [ photometric-interpretation-ycbcr ] }
61 { 8 [ photometric-interpretation-cielab ] }
62 { 9 [ photometric-interpretation-icclab ] }
63 { 10 [ photometric-interpretation-itulab ] }
64 { 32844 [ photometric-interpretation-logl ] }
65 { 32845 [ photometric-interpretation-logluv ] }
66 [ bad-photometric-interpretation ]
69 SINGLETONS: compression
77 compression-adobe-deflate
84 compression-thunderscan
96 ERROR: bad-compression n ;
97 : lookup-compression ( n -- compression )
99 { 1 [ compression-none ] }
100 { 2 [ compression-CCITT-2 ] }
101 { 3 [ compression-CCITT-3 ] }
102 { 4 [ compression-CCITT-4 ] }
103 { 5 [ compression-lzw ] }
104 { 6 [ compression-jpeg-old ] }
105 { 7 [ compression-jpeg-new ] }
106 { 8 [ compression-adobe-deflate ] }
107 { 9 [ compression-9 ] }
108 { 10 [ compression-10 ] }
109 { 32766 [ compression-next ] }
110 { 32771 [ compression-ccittrlew ] }
111 { 32773 [ compression-pack-bits ] }
112 { 32809 [ compression-thunderscan ] }
113 { 32895 [ compression-it8ctpad ] }
114 { 32896 [ compression-it8lw ] }
115 { 32897 [ compression-it8mp ] }
116 { 32898 [ compression-it8bl ] }
117 { 32908 [ compression-pixarfilm ] }
118 { 32909 [ compression-pixarlog ] }
119 { 32946 [ compression-deflate ] }
120 { 32947 [ compression-dcs ] }
121 { 34661 [ compression-jbig ] }
122 { 34676 [ compression-sgilog ] }
123 { 34677 [ compression-sgilog24 ] }
124 { 34712 [ compression-jp2000 ] }
128 SINGLETONS: resolution-unit
131 resolution-unit-centimeter ;
132 ERROR: bad-resolution-unit n ;
133 : lookup-resolution-unit ( n -- object )
135 { 1 [ resolution-unit-none ] }
136 { 2 [ resolution-unit-inch ] }
137 { 3 [ resolution-unit-centimeter ] }
138 [ bad-resolution-unit ]
141 SINGLETONS: predictor
143 predictor-horizontal-differencing ;
144 ERROR: bad-predictor n ;
145 : lookup-predictor ( n -- object )
147 { 1 [ predictor-none ] }
148 { 2 [ predictor-horizontal-differencing ] }
152 SINGLETONS: planar-configuration
153 planar-configuration-chunky
154 planar-configuration-planar ;
155 ERROR: bad-planar-configuration n ;
156 : lookup-planar-configuration ( n -- object )
158 { 1 [ planar-configuration-chunky ] }
159 { 2 [ planar-configuration-planar ] }
160 [ bad-planar-configuration ]
163 SINGLETONS: sample-format
165 sample-format-unsigned-integer
166 sample-format-signed-integer
167 sample-format-ieee-float
168 sample-format-undefined-data ;
169 ERROR: bad-sample-format n ;
170 : lookup-sample-format ( sequence -- object )
173 { 0 [ sample-format-none ] }
174 { 1 [ sample-format-unsigned-integer ] }
175 { 2 [ sample-format-signed-integer ] }
176 { 3 [ sample-format-ieee-float ] }
177 { 4 [ sample-format-undefined-data ] }
178 [ bad-sample-format ]
182 SINGLETONS: extra-samples
183 extra-samples-unspecified-alpha-data
184 extra-samples-associated-alpha-data
185 extra-samples-unassociated-alpha-data ;
186 ERROR: bad-extra-samples n ;
187 : lookup-extra-samples ( sequence -- object )
189 { 0 [ extra-samples-unspecified-alpha-data ] }
190 { 1 [ extra-samples-associated-alpha-data ] }
191 { 2 [ extra-samples-unassociated-alpha-data ] }
192 [ bad-extra-samples ]
195 SINGLETONS: image-length image-width x-resolution y-resolution
196 rows-per-strip strip-offsets strip-byte-counts bits-per-sample
197 samples-per-pixel new-subfile-type subfile-type orientation
198 software date-time photoshop exif-ifd sub-ifd inter-color-profile
199 xmp iptc fill-order document-name page-number page-name
200 x-position y-position host-computer copyright artist
201 min-sample-value max-sample-value tiff-make tiff-model cell-width cell-length
202 gray-response-unit gray-response-curve color-map threshholding
203 image-description free-offsets free-byte-counts tile-width tile-length
204 matteing data-type image-depth tile-depth
205 ycbcr-subsampling gdal-metadata
206 tile-offsets tile-byte-counts jpeg-qtables jpeg-dctables jpeg-actables
207 ycbcr-positioning ycbcr-coefficients reference-black-white halftone-hints
208 jpeg-interchange-format
209 jpeg-interchange-format-length
210 jpeg-restart-interval jpeg-tables
211 t4-options clean-fax-data bad-fax-lines consecutive-bad-fax-lines
212 sto-nits print-image-matching-info
213 unhandled-ifd-entry ;
215 SINGLETONS: jpeg-proc
219 ERROR: bad-jpeg-proc n ;
221 : lookup-jpeg-proc ( sequence -- object )
223 { 1 [ jpeg-proc-baseline ] }
224 { 14 [ jpeg-proc-lossless ] }
228 ERROR: bad-tiff-magic bytes ;
229 : tiff-endianness ( byte-array -- ? )
231 { B{ CHAR: M CHAR: M } [ big-endian ] }
232 { B{ CHAR: I CHAR: I } [ little-endian ] }
236 : read-header ( tiff -- tiff )
237 2 read tiff-endianness [ >>endianness ] keep
239 2 read endian> >>the-answer
240 4 read endian> >>ifd-offset
243 : push-ifd ( tiff ifd -- tiff ) over ifds>> push ;
245 : read-ifd ( -- ifd )
249 4 read endian> <ifd-entry> ;
251 : read-ifds ( tiff offset -- tiff )
252 seek-absolute seek-input
254 dup [ read-ifd ] replicate
256 [ <ifd> push-ifd ] [ dup 0 = [ drop ] [ read-ifds ] if ] bi ;
258 ERROR: no-tag class ;
260 : find-tag* ( ifd class -- tag/class ? )
261 swap processed-tags>> ?at ;
263 : find-tag ( ifd class -- tag )
264 find-tag* [ no-tag ] unless ;
266 : tag? ( ifd class -- tag )
267 swap processed-tags>> key? ;
269 : read-strips ( ifd -- ifd )
271 [ strip-byte-counts find-tag ]
272 [ strip-offsets find-tag ] bi
273 2dup [ integer? ] both? [
274 seek-absolute seek-input read 1array
276 [ seek-absolute seek-input read ] { } 2map-as
279 ERROR: unknown-ifd-type n ;
281 : bytes>bits ( n/byte-array -- n )
282 dup byte-array? [ byte-array>bignum ] when ;
284 : value-length ( ifd-entry -- n )
285 [ count>> ] [ type>> ] bi {
302 ERROR: bad-small-ifd-type n ;
304 : adjust-offset/value ( ifd-entry -- obj )
305 [ offset/value>> 4 >endian ] [ type>> ] bi
307 { 1 [ 1 head endian> ] }
308 { 3 [ 2 head endian> ] }
310 { 6 [ 1 head endian> 8 >signed ] }
311 { 8 [ 2 head endian> 16 >signed ] }
312 { 9 [ endian> 32 >signed ] }
313 { 11 [ endian> bits>float ] }
314 { 13 [ endian> 32 >signed ] }
315 [ bad-small-ifd-type ]
318 : offset-bytes>obj ( bytes type -- obj )
321 { 2 [ ] } ! read c strings here
322 { 3 [ 2 <sliced-groups> [ endian> ] map ] }
323 { 4 [ 4 <sliced-groups> [ endian> ] map ] }
324 { 5 [ 8 <sliced-groups> [ "II" unpack first2 / ] map ] }
325 { 6 [ [ 8 >signed ] map ] }
327 { 8 [ 2 <sliced-groups> [ endian> 16 >signed ] map ] }
328 { 9 [ 4 <sliced-groups> [ endian> 32 >signed ] map ] }
329 { 10 [ 8 group [ "ii" unpack first2 / ] map ] }
330 { 11 [ 4 group [ "f" unpack ] map ] }
331 { 12 [ 8 group [ "d" unpack ] map ] }
335 : ifd-entry-value ( ifd-entry -- n )
336 dup value-length 4 <= [
339 [ offset/value>> seek-absolute seek-input ]
340 [ value-length read ]
341 [ type>> ] tri offset-bytes>obj
344 : process-ifd-entry ( ifd-entry -- value class )
345 [ ifd-entry-value ] [ tag>> ] bi {
346 { 254 [ new-subfile-type ] }
347 { 255 [ subfile-type ] }
348 { 256 [ image-width ] }
349 { 257 [ image-length ] }
350 { 258 [ bits-per-sample ] }
351 { 259 [ lookup-compression compression ] }
352 { 262 [ lookup-photometric-interpretation photometric-interpretation ] }
353 { 263 [ threshholding ] }
354 { 264 [ cell-width ] }
355 { 265 [ cell-length ] }
356 { 266 [ fill-order ] }
357 { 269 [ ascii decode document-name ] }
358 { 270 [ ascii decode image-description ] }
359 { 271 [ ascii decode tiff-make ] }
360 { 272 [ ascii decode tiff-model ] }
361 { 273 [ strip-offsets ] }
362 { 274 [ orientation ] }
363 { 277 [ samples-per-pixel ] }
364 { 278 [ rows-per-strip ] }
365 { 279 [ strip-byte-counts ] }
366 { 280 [ min-sample-value ] }
367 { 281 [ max-sample-value ] }
368 { 282 [ first x-resolution ] }
369 { 283 [ first y-resolution ] }
370 { 284 [ lookup-planar-configuration planar-configuration ] }
371 { 285 [ page-name ] }
372 { 286 [ x-position ] }
373 { 287 [ y-position ] }
374 { 288 [ free-offsets ] }
375 { 289 [ free-byte-counts ] }
376 { 290 [ gray-response-unit ] }
377 { 291 [ gray-response-curve ] }
378 { 292 [ t4-options ] }
379 { 296 [ lookup-resolution-unit resolution-unit ] }
380 { 297 [ page-number ] }
381 { 305 [ ascii decode software ] }
382 { 306 [ ascii decode date-time ] }
383 { 315 [ ascii decode artist ] }
384 { 316 [ ascii decode host-computer ] }
385 { 317 [ lookup-predictor predictor ] }
386 { 320 [ color-map ] }
387 { 321 [ halftone-hints ] }
388 { 322 [ tile-width ] }
389 { 323 [ tile-length ] }
390 { 324 [ tile-offsets ] }
391 { 325 [ tile-byte-counts ] }
392 { 326 [ bad-fax-lines ] }
393 { 327 [ clean-fax-data ] }
394 { 328 [ consecutive-bad-fax-lines ] }
396 { 338 [ lookup-extra-samples extra-samples ] }
397 { 339 [ lookup-sample-format sample-format ] }
398 { 347 [ jpeg-tables ] }
399 { 512 [ lookup-jpeg-proc jpeg-proc ] }
400 { 513 [ jpeg-interchange-format ] }
401 { 514 [ jpeg-interchange-format-length ] }
402 { 515 [ jpeg-restart-interval ] }
403 { 519 [ jpeg-qtables ] }
404 { 520 [ jpeg-dctables ] }
405 { 521 [ jpeg-actables ] }
406 { 529 [ ycbcr-coefficients ] }
407 { 530 [ ycbcr-subsampling ] }
408 { 531 [ ycbcr-positioning ] }
409 { 532 [ reference-black-white ] }
410 { 700 [ utf8 decode xmp ] }
411 { 32995 [ matteing ] }
412 { 32996 [ data-type ] }
413 { 32997 [ image-depth ] }
414 { 32998 [ tile-depth ] }
415 { 33432 [ copyright ] }
417 { 34377 [ photoshop ] }
418 { 34665 [ exif-ifd ] }
419 { 34675 [ inter-color-profile ] }
420 { 37439 [ sto-nits ] }
421 { 42112 [ gdal-metadata ] }
422 { 50341 [ print-image-matching-info ] }
423 [ nip unhandled-ifd-entry swap ]
426 : process-ifds ( loading-tiff -- loading-tiff )
430 [ process-ifd-entry swap ] H{ } map>assoc >>processed-tags
434 ERROR: unhandled-compression compression ;
436 : (uncompress-strips) ( strips compression -- uncompressed-strips )
438 { compression-none [ ] }
439 { compression-lzw [ [ lzw-uncompress ] map ] }
440 [ unhandled-compression ]
443 : uncompress-strips ( ifd -- ifd )
445 _ compression find-tag (uncompress-strips)
448 : strips>bitmap ( ifd -- ifd )
449 dup strips>> concat >>bitmap ;
451 : (strips-predictor) ( ifd -- ifd )
453 [ image-width find-tag ]
454 [ samples-per-pixel find-tag ] tri
458 [ _ group unclip [ v+ ] accumulate swap suffix concat ] map
462 : strips-predictor ( ifd -- ifd )
464 dup predictor find-tag
466 { predictor-none [ ] }
467 { predictor-horizontal-differencing [ (strips-predictor) ] }
472 ERROR: unknown-component-order ifd ;
474 : fix-bitmap-endianness ( ifd -- ifd )
475 dup [ bitmap>> ] [ bits-per-sample find-tag ] bi
477 { { 32 32 32 32 } [ 4 seq>native-endianness ] }
478 { { 32 32 32 } [ 4 seq>native-endianness ] }
479 { { 16 16 16 16 } [ 2 seq>native-endianness ] }
480 { { 16 16 16 } [ 2 seq>native-endianness ] }
484 [ unknown-component-order ]
487 : ifd-component-order ( ifd -- component-order component-type )
488 bits-per-sample find-tag {
489 { { 32 32 32 32 } [ RGBA float-components ] }
490 { { 32 32 32 } [ RGB float-components ] }
491 { { 16 16 16 16 } [ RGBA ushort-components ] }
492 { { 16 16 16 } [ RGB ushort-components ] }
493 { { 8 8 8 8 } [ RGBA ubyte-components ] }
494 { { 8 8 8 } [ RGB ubyte-components ] }
495 { 8 [ LA ubyte-components ] }
496 [ unknown-component-order ]
499 : handle-alpha-data ( ifd -- ifd )
500 dup extra-samples find-tag {
501 { extra-samples-associated-alpha-data [ ] }
502 { extra-samples-unspecified-alpha-data [ ] }
503 { extra-samples-unassociated-alpha-data [ ] }
504 [ bad-extra-samples ]
507 : ifd>image ( ifd -- image )
509 [ [ image-width find-tag ] [ image-length find-tag ] bi 2array >>dim ]
510 [ ifd-component-order [ >>component-order ] [ >>component-type ] bi* ]
511 [ bitmap>> >>bitmap ]
514 : tiff>image ( image -- image )
515 ifds>> [ ifd>image ] map first ;
517 : with-tiff-endianness ( loading-tiff quot -- )
518 [ dup endianness>> ] dip with-endianness ; inline
520 : load-tiff-ifds ( stream -- loading-tiff )
524 dup ifd-offset>> read-ifds
526 ] with-tiff-endianness
527 ] with-input-stream* ;
529 : process-chunky-ifd ( ifd -- )
533 fix-bitmap-endianness
535 dup extra-samples tag? [ handle-alpha-data ] when
538 : process-planar-ifd ( ifd -- )
539 "planar ifd not supported" throw ;
541 : dispatch-planar-configuration ( ifd planar-configuration -- )
543 { planar-configuration-chunky [ process-chunky-ifd ] }
544 { planar-configuration-planar [ process-planar-ifd ] }
547 : process-ifd ( ifd -- )
548 dup planar-configuration find-tag* [
549 dispatch-planar-configuration
551 drop "no planar configuration" throw
554 : process-tif-ifds ( loading-tiff -- )
555 ifds>> [ process-ifd ] each ;
557 : load-tiff ( path -- loading-tiff )
558 [ load-tiff-ifds dup ]
560 [ [ 0 seek-absolute ] dip stream-seek ]
563 [ process-tif-ifds ] with-tiff-endianness
568 ! tiff files can store several images -- we just take the first for now
569 M: tiff-image stream>image ( stream tiff-image -- image )
570 drop load-tiff tiff>image ;
572 { "tif" "tiff" } [ tiff-image register-image-class ] each