0
|
1 |
"
|
6
|
2 |
COPYRIGHT (c) 1991 by Claus Gittinger
|
28
|
3 |
All Rights Reserved
|
0
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
|
13 |
ImageReader subclass:#TIFFReader
|
135
|
14 |
instanceVariableNames:'planarConfiguration subFileType stripOffsets rowsPerStrip
|
|
15 |
fillOrder compression group3options predictor stripByteCounts
|
|
16 |
currentOffset stripOffsetsPos stripByteCountsPos bitsPerSamplePos
|
|
17 |
colorMapPos'
|
28
|
18 |
classVariableNames:''
|
|
19 |
poolDictionaries:''
|
32
|
20 |
category:'Graphics-Images support'
|
0
|
21 |
!
|
|
22 |
|
|
23 |
!TIFFReader class methodsFor:'documentation'!
|
|
24 |
|
21
|
25 |
copyright
|
|
26 |
"
|
|
27 |
COPYRIGHT (c) 1991 by Claus Gittinger
|
28
|
28 |
All Rights Reserved
|
21
|
29 |
|
|
30 |
This software is furnished under a license and may be used
|
|
31 |
only in accordance with the terms of that license and with the
|
|
32 |
inclusion of the above copyright notice. This software may not
|
|
33 |
be provided or otherwise made available to, or used by, any
|
|
34 |
other person. No title to or ownership of the software is
|
|
35 |
hereby transferred.
|
|
36 |
"
|
|
37 |
!
|
|
38 |
|
0
|
39 |
documentation
|
|
40 |
"
|
21
|
41 |
This class knows how to read TIFF files and how to
|
|
42 |
write uncompressed TIFF files.
|
14
|
43 |
|
21
|
44 |
Only single image files are supported.
|
38
|
45 |
Not all formats are implemented, and of those that are, not all are tested.
|
21
|
46 |
It should work with most rgb, mono and 2-plane greyscale
|
38
|
47 |
images, since this is what I have as test material on the NeXT.
|
21
|
48 |
It supports reading of uncompressed, LZW and G3 compressed
|
|
49 |
images; JPEG and packbits are currently not implemented.
|
14
|
50 |
|
21
|
51 |
Only writing of uncompressed images is currently implemented.
|
|
52 |
More formats will come ...
|
30
|
53 |
|
|
54 |
TODO: since I dont want to spend all of my life adding more formats here and
|
|
55 |
reinventing the wheel, this code should be changed to use the tiff library.
|
|
56 |
That would give us most formats and also writing capabilities for free.
|
0
|
57 |
"
|
|
58 |
! !
|
|
59 |
|
28
|
60 |
!TIFFReader class methodsFor:'initialization'!
|
|
61 |
|
|
62 |
initialize
|
30
|
63 |
Image fileFormats at:'.tiff' put:self.
|
28
|
64 |
Image fileFormats at:'.tif' put:self.
|
|
65 |
Image fileFormats at:'.TIF' put:self.
|
|
66 |
! !
|
|
67 |
|
3
|
68 |
!TIFFReader class methodsFor:'testing'!
|
|
69 |
|
135
|
70 |
canRepresent:anImage
|
|
71 |
"return true, if anImage can be represented in my file format.
|
|
72 |
Any image is supported."
|
|
73 |
|
|
74 |
^ true
|
|
75 |
!
|
|
76 |
|
3
|
77 |
isValidImageFile:aFileName
|
|
78 |
"return true, if aFileName contains a GIF image"
|
|
79 |
|
|
80 |
|inStream char1 char2 version|
|
|
81 |
|
11
|
82 |
inStream := self streamReadingFile:aFileName.
|
3
|
83 |
inStream isNil ifTrue:[^ false].
|
|
84 |
|
|
85 |
char1 := inStream next.
|
|
86 |
char2 := inStream next.
|
|
87 |
|
|
88 |
((char1 ~~ char2) or:[(char1 ~~ $I) and:[char1 ~~ $M]]) ifTrue:[
|
28
|
89 |
inStream close.
|
|
90 |
^ false
|
3
|
91 |
].
|
|
92 |
|
|
93 |
inStream binary.
|
|
94 |
version := inStream nextShortMSB:(char1 == $M).
|
|
95 |
inStream close.
|
|
96 |
|
|
97 |
(version ~~ 42) ifTrue:[^ false].
|
|
98 |
^ true
|
0
|
99 |
! !
|
|
100 |
|
|
101 |
!TIFFReader methodsFor:'private'!
|
|
102 |
|
|
103 |
decodeTiffTag:tagType numberType:numberType length:length
|
|
104 |
|offset value valueArray
|
40
|
105 |
val scaleFactor rV gV bV
|
|
106 |
n "{ Class: SmallInteger }"
|
35
|
107 |
i2 "{ Class: SmallInteger }"
|
|
108 |
i3 "{ Class: SmallInteger }" |
|
0
|
109 |
|
|
110 |
(numberType == 3) ifTrue:[
|
28
|
111 |
"short"
|
|
112 |
valueArray := self readShorts:length.
|
|
113 |
value := valueArray at:1
|
0
|
114 |
] ifFalse:[
|
28
|
115 |
(numberType == 4) ifTrue:[
|
|
116 |
"integer"
|
|
117 |
valueArray := self readLongs:length.
|
|
118 |
value := valueArray at:1
|
|
119 |
] ifFalse:[
|
|
120 |
(numberType == 2) ifTrue:[
|
|
121 |
"character"
|
|
122 |
value := self readChars:length
|
|
123 |
] ifFalse:[
|
|
124 |
(numberType == 5) ifTrue:[
|
|
125 |
"fraction"
|
|
126 |
valueArray := self readFracts:length.
|
|
127 |
value := valueArray at:1
|
|
128 |
] ifFalse:[
|
35
|
129 |
offset := (inStream nextLongMSB:(byteOrder ~~ #lsb))
|
28
|
130 |
]
|
|
131 |
]
|
|
132 |
]
|
0
|
133 |
].
|
|
134 |
|
|
135 |
(tagType == 254) ifTrue:[
|
28
|
136 |
"NewSubfileType"
|
|
137 |
"newSubFileType := value."
|
14
|
138 |
"
|
28
|
139 |
'newSubfiletype ' print. value printNewline.
|
14
|
140 |
"
|
28
|
141 |
^ self
|
0
|
142 |
].
|
|
143 |
(tagType == 255) ifTrue:[
|
28
|
144 |
"SubfileType"
|
|
145 |
subFileType := value.
|
14
|
146 |
"
|
28
|
147 |
'subfiletype ' print. value printNewline.
|
14
|
148 |
"
|
28
|
149 |
^ self
|
0
|
150 |
].
|
|
151 |
(tagType == 256) ifTrue:[
|
28
|
152 |
"ImageWidth"
|
|
153 |
width := value.
|
21
|
154 |
"
|
28
|
155 |
'width ' print. width printNewline.
|
14
|
156 |
"
|
28
|
157 |
^ self
|
0
|
158 |
].
|
|
159 |
(tagType == 257) ifTrue:[
|
28
|
160 |
"ImageHeight"
|
|
161 |
height := value.
|
14
|
162 |
"
|
28
|
163 |
'height ' print. height printNewline.
|
14
|
164 |
"
|
28
|
165 |
^ self
|
0
|
166 |
].
|
|
167 |
(tagType == 258) ifTrue:[
|
28
|
168 |
"bitspersample"
|
|
169 |
bitsPerSample := valueArray.
|
14
|
170 |
"
|
28
|
171 |
'bitspersample ' print. bitsPerSample printNewline.
|
14
|
172 |
"
|
28
|
173 |
^ self
|
0
|
174 |
].
|
|
175 |
(tagType == 259) ifTrue:[
|
28
|
176 |
"compression"
|
|
177 |
compression := value.
|
14
|
178 |
"
|
28
|
179 |
'compression ' print. compression printNewline.
|
14
|
180 |
"
|
28
|
181 |
^ self
|
0
|
182 |
].
|
|
183 |
(tagType == 262) ifTrue:[
|
28
|
184 |
"photometric"
|
30
|
185 |
|
28
|
186 |
(value == 0) ifTrue:[
|
|
187 |
photometric := #whiteIs0
|
|
188 |
] ifFalse:[
|
|
189 |
(value == 1) ifTrue:[
|
|
190 |
photometric := #blackIs0
|
|
191 |
] ifFalse:[
|
|
192 |
(value == 2) ifTrue:[
|
|
193 |
photometric := #rgb
|
|
194 |
] ifFalse:[
|
|
195 |
(value == 3) ifTrue:[
|
|
196 |
photometric := #palette
|
|
197 |
] ifFalse:[
|
|
198 |
(value == 4) ifTrue:[
|
|
199 |
photometric := #transparency
|
|
200 |
] ifFalse:[
|
30
|
201 |
(value == 5) ifTrue:[
|
|
202 |
photometric := #separated "/ color separations
|
|
203 |
] ifFalse:[
|
|
204 |
(value == 6) ifTrue:[
|
|
205 |
photometric := #ycbr "/ CCIR 601
|
|
206 |
] ifFalse:[
|
|
207 |
(value == 8) ifTrue:[
|
|
208 |
photometric := #cielab "/ 1976 CIE L*a*b*
|
|
209 |
] ifFalse:[
|
|
210 |
photometric := nil
|
|
211 |
]
|
|
212 |
]
|
|
213 |
]
|
28
|
214 |
]
|
|
215 |
]
|
|
216 |
]
|
|
217 |
]
|
|
218 |
].
|
14
|
219 |
"
|
28
|
220 |
'photometric ' print. photometric printNewline.
|
14
|
221 |
"
|
28
|
222 |
^ self
|
0
|
223 |
].
|
|
224 |
(tagType == 263) ifTrue:[
|
28
|
225 |
"Treshholding"
|
|
226 |
"threshholding := value."
|
14
|
227 |
"
|
28
|
228 |
'treshholding ' print. value printNewline.
|
14
|
229 |
"
|
28
|
230 |
^ self
|
0
|
231 |
].
|
|
232 |
(tagType == 264) ifTrue:[
|
28
|
233 |
"CellWidth"
|
|
234 |
"cellWidth:= value."
|
14
|
235 |
"
|
28
|
236 |
'cellWidth ' print. value printNewline.
|
14
|
237 |
"
|
28
|
238 |
^ self
|
0
|
239 |
].
|
|
240 |
(tagType == 265) ifTrue:[
|
28
|
241 |
"CellLength"
|
|
242 |
"cellLength:= value."
|
14
|
243 |
"
|
28
|
244 |
'cellLength ' print. value printNewline.
|
14
|
245 |
"
|
28
|
246 |
^ self
|
0
|
247 |
].
|
|
248 |
(tagType == 266) ifTrue:[
|
28
|
249 |
"fillOrder"
|
|
250 |
(value == 1) ifTrue:[
|
|
251 |
fillOrder := #msb
|
|
252 |
] ifFalse:[
|
|
253 |
(value == 2) ifTrue:[
|
|
254 |
fillOrder := #lsb
|
|
255 |
] ifFalse:[
|
|
256 |
fillOrder := nil
|
|
257 |
]
|
|
258 |
].
|
14
|
259 |
"
|
28
|
260 |
'fillorder ' print. fillOrder printNewline.
|
14
|
261 |
"
|
28
|
262 |
^ self
|
0
|
263 |
].
|
|
264 |
(tagType == 269) ifTrue:[
|
30
|
265 |
"documentName - info only"
|
14
|
266 |
"
|
28
|
267 |
'documentName ' print. value printNewline.
|
14
|
268 |
"
|
28
|
269 |
^ self
|
0
|
270 |
].
|
|
271 |
(tagType == 270) ifTrue:[
|
30
|
272 |
"imageDescription - info only"
|
14
|
273 |
"
|
28
|
274 |
'imageDescription ' print. value printNewline.
|
14
|
275 |
"
|
28
|
276 |
^ self
|
0
|
277 |
].
|
|
278 |
(tagType == 271) ifTrue:[
|
30
|
279 |
"make - info only"
|
14
|
280 |
"
|
28
|
281 |
'make ' print. value printNewline.
|
14
|
282 |
"
|
28
|
283 |
^ self
|
0
|
284 |
].
|
|
285 |
(tagType == 272) ifTrue:[
|
30
|
286 |
"model - info only"
|
14
|
287 |
"
|
28
|
288 |
'model ' print. value printNewline.
|
14
|
289 |
"
|
28
|
290 |
^ self
|
0
|
291 |
].
|
|
292 |
(tagType == 273) ifTrue:[
|
28
|
293 |
"stripoffsets"
|
|
294 |
stripOffsets := valueArray.
|
14
|
295 |
"
|
28
|
296 |
'stripOffsets Array(' print. stripOffsets size print. ')' printNewline.
|
14
|
297 |
"
|
28
|
298 |
^ self
|
0
|
299 |
].
|
|
300 |
(tagType == 274) ifTrue:[
|
28
|
301 |
"Orientation"
|
|
302 |
"orientation:= value."
|
14
|
303 |
"
|
28
|
304 |
'orientation ' print. value printNewline.
|
14
|
305 |
"
|
28
|
306 |
^ self
|
0
|
307 |
].
|
|
308 |
(tagType == 277) ifTrue:[
|
28
|
309 |
"samplesPerPixel"
|
|
310 |
samplesPerPixel := value.
|
14
|
311 |
"
|
28
|
312 |
'samplesperpixel ' print. samplesPerPixel printNewline.
|
14
|
313 |
"
|
28
|
314 |
^ self
|
0
|
315 |
].
|
|
316 |
(tagType == 278) ifTrue:[
|
28
|
317 |
"rowsperstrip"
|
|
318 |
rowsPerStrip := value.
|
14
|
319 |
"
|
28
|
320 |
'rowsperstrip ' print. rowsPerStrip printNewline.
|
14
|
321 |
"
|
28
|
322 |
^ self
|
0
|
323 |
].
|
|
324 |
(tagType == 279) ifTrue:[
|
28
|
325 |
"stripbytecount"
|
|
326 |
stripByteCounts := valueArray.
|
14
|
327 |
"
|
28
|
328 |
'stripByteCounts Array(' print.
|
|
329 |
stripByteCounts size print.
|
|
330 |
')' printNewline.
|
14
|
331 |
"
|
28
|
332 |
^ self
|
0
|
333 |
].
|
|
334 |
(tagType == 280) ifTrue:[
|
28
|
335 |
"MinSampleValue"
|
|
336 |
"minSampleValue:= value."
|
14
|
337 |
"
|
28
|
338 |
'minSampleValue ' print. value printNewline.
|
14
|
339 |
"
|
28
|
340 |
^ self
|
0
|
341 |
].
|
|
342 |
(tagType == 281) ifTrue:[
|
28
|
343 |
"MaxSampleValue"
|
|
344 |
"maxSampleValue:= value."
|
14
|
345 |
"
|
28
|
346 |
'maxSampleValue ' print. value printNewline.
|
14
|
347 |
"
|
28
|
348 |
^ self
|
0
|
349 |
].
|
|
350 |
(tagType == 282) ifTrue:[
|
28
|
351 |
"xResolution"
|
14
|
352 |
"
|
28
|
353 |
'xres ' print. value printNewline.
|
14
|
354 |
"
|
28
|
355 |
^ self
|
0
|
356 |
].
|
|
357 |
(tagType == 283) ifTrue:[
|
28
|
358 |
"yResolution"
|
14
|
359 |
"
|
28
|
360 |
'yres ' print. value printNewline.
|
14
|
361 |
"
|
28
|
362 |
^ self
|
0
|
363 |
].
|
|
364 |
(tagType == 284) ifTrue:[
|
28
|
365 |
"planarconfig"
|
|
366 |
(value == 1) ifTrue:[
|
|
367 |
planarConfiguration := 1
|
|
368 |
] ifFalse:[
|
|
369 |
(value == 2) ifTrue:[
|
|
370 |
planarConfiguration := 2
|
|
371 |
] ifFalse:[
|
|
372 |
planarConfiguration := nil
|
|
373 |
]
|
|
374 |
].
|
14
|
375 |
"
|
28
|
376 |
'planarconfig ' print. planarConfiguration printNewline.
|
14
|
377 |
"
|
28
|
378 |
^ self
|
0
|
379 |
].
|
|
380 |
(tagType == 285) ifTrue:[
|
28
|
381 |
"pageName"
|
14
|
382 |
"
|
28
|
383 |
'pageName ' print. value printNewline.
|
14
|
384 |
"
|
28
|
385 |
^ self
|
0
|
386 |
].
|
|
387 |
(tagType == 286) ifTrue:[
|
28
|
388 |
"xPosition"
|
14
|
389 |
"
|
28
|
390 |
'xPos ' print. value printNewline.
|
14
|
391 |
"
|
28
|
392 |
^ self
|
0
|
393 |
].
|
|
394 |
(tagType == 287) ifTrue:[
|
28
|
395 |
"yPosition"
|
14
|
396 |
"
|
28
|
397 |
'yPos ' print. value printNewline.
|
14
|
398 |
"
|
28
|
399 |
^ self
|
0
|
400 |
].
|
|
401 |
(tagType == 288) ifTrue:[
|
28
|
402 |
"freeOffsets"
|
14
|
403 |
"
|
28
|
404 |
'freeOffsets ' print. value printNewline.
|
14
|
405 |
"
|
28
|
406 |
^ self
|
0
|
407 |
].
|
|
408 |
(tagType == 289) ifTrue:[
|
28
|
409 |
"freeByteCounts"
|
14
|
410 |
"
|
28
|
411 |
'freeByteCounts ' print. value printNewline.
|
14
|
412 |
"
|
28
|
413 |
^ self
|
0
|
414 |
].
|
|
415 |
(tagType == 290) ifTrue:[
|
28
|
416 |
"grayResponceUnit"
|
14
|
417 |
"
|
28
|
418 |
'grayResponceUnit' print. value printNewline.
|
14
|
419 |
"
|
28
|
420 |
^ self
|
0
|
421 |
].
|
|
422 |
(tagType == 291) ifTrue:[
|
28
|
423 |
"grayResponceCurve"
|
14
|
424 |
"
|
28
|
425 |
'grayResponceCurve' print. value printNewline.
|
14
|
426 |
"
|
28
|
427 |
^ self
|
0
|
428 |
].
|
|
429 |
(tagType == 292) ifTrue:[
|
28
|
430 |
"group3options"
|
|
431 |
group3options := value.
|
14
|
432 |
"
|
28
|
433 |
'group3options ' print. group3options printNewline.
|
14
|
434 |
"
|
28
|
435 |
^ self
|
0
|
436 |
].
|
|
437 |
(tagType == 293) ifTrue:[
|
28
|
438 |
"group4options"
|
|
439 |
"group4options := value."
|
14
|
440 |
"
|
28
|
441 |
'group4options ' print. value printNewline.
|
14
|
442 |
"
|
28
|
443 |
^ self
|
0
|
444 |
].
|
|
445 |
(tagType == 296) ifTrue:[
|
28
|
446 |
"resolutionunit"
|
14
|
447 |
"
|
28
|
448 |
(value == 1) ifTrue:[
|
|
449 |
'res-unit pixel' printNewline
|
|
450 |
] ifFalse:[
|
|
451 |
(value == 2) ifTrue:[
|
|
452 |
'res-unit inch' printNewline
|
|
453 |
] ifFalse:[
|
|
454 |
(value == 3) ifTrue:[
|
|
455 |
'res-unit mm' printNewline
|
|
456 |
] ifFalse:[
|
|
457 |
'res-unit invalid' printNewline
|
|
458 |
]
|
|
459 |
]
|
|
460 |
].
|
14
|
461 |
"
|
28
|
462 |
"resolutionUnit := value."
|
|
463 |
^ self
|
0
|
464 |
].
|
|
465 |
(tagType == 297) ifTrue:[
|
28
|
466 |
"pageNumber"
|
|
467 |
"pageNumber := value."
|
14
|
468 |
"
|
28
|
469 |
'pageNumber ' print. value printNewline.
|
14
|
470 |
"
|
28
|
471 |
^ self
|
0
|
472 |
].
|
|
473 |
(tagType == 300) ifTrue:[
|
28
|
474 |
"colorResponceUnit"
|
14
|
475 |
"
|
28
|
476 |
'colorResponceUnit' print. value printNewline.
|
14
|
477 |
"
|
28
|
478 |
^ self
|
0
|
479 |
].
|
|
480 |
(tagType == 301) ifTrue:[
|
28
|
481 |
"colorResponceCurve"
|
14
|
482 |
"
|
28
|
483 |
'colorResponceCurve' print. value printNewline.
|
14
|
484 |
"
|
28
|
485 |
^ self
|
0
|
486 |
].
|
30
|
487 |
(tagType == 305) ifTrue:[
|
|
488 |
"software - info only"
|
|
489 |
"
|
|
490 |
'software' print. value printNewline.
|
|
491 |
"
|
|
492 |
^ self
|
|
493 |
].
|
0
|
494 |
(tagType == 306) ifTrue:[
|
30
|
495 |
"dateTime - info only"
|
14
|
496 |
"
|
28
|
497 |
'dateTime ' print. value printNewline.
|
14
|
498 |
"
|
28
|
499 |
^ self
|
0
|
500 |
].
|
|
501 |
(tagType == 315) ifTrue:[
|
30
|
502 |
"artist - info only"
|
14
|
503 |
"
|
28
|
504 |
'artist ' print. value printNewline.
|
14
|
505 |
"
|
28
|
506 |
^ self
|
0
|
507 |
].
|
30
|
508 |
(tagType == 316) ifTrue:[
|
|
509 |
"host computer - info only"
|
|
510 |
"
|
|
511 |
'host ' print. value printNewline.
|
|
512 |
"
|
|
513 |
^ self
|
|
514 |
].
|
0
|
515 |
(tagType == 317) ifTrue:[
|
28
|
516 |
"predictor"
|
|
517 |
predictor := value.
|
14
|
518 |
"
|
28
|
519 |
'predictor ' print. predictor printNewline.
|
14
|
520 |
"
|
28
|
521 |
^ self
|
0
|
522 |
].
|
30
|
523 |
(tagType == 318) ifTrue:[
|
|
524 |
"whitePoint"
|
|
525 |
"
|
|
526 |
'whitePoint ' print. value printNewline.
|
|
527 |
"
|
|
528 |
^ self
|
|
529 |
].
|
|
530 |
(tagType == 319) ifTrue:[
|
|
531 |
"primaryChromatics"
|
|
532 |
"
|
|
533 |
'primaryChromatics ' print. value printNewline.
|
|
534 |
"
|
|
535 |
^ self
|
|
536 |
].
|
0
|
537 |
(tagType == 320) ifTrue:[
|
28
|
538 |
"colorMap"
|
14
|
539 |
"
|
28
|
540 |
'colorMap (size=' print. valueArray size print. ')' printNewline.
|
14
|
541 |
"
|
35
|
542 |
"
|
|
543 |
the tiff colormap contains 16bit values;
|
|
544 |
our colormap expects 8bit values
|
|
545 |
"
|
28
|
546 |
n := valueArray size // 3.
|
|
547 |
|
35
|
548 |
rV := ByteArray uninitializedNew:n.
|
|
549 |
gV := ByteArray uninitializedNew:n.
|
|
550 |
bV := ByteArray uninitializedNew:n.
|
28
|
551 |
scaleFactor := 255.0 / 16rFFFF.
|
35
|
552 |
i2 := n+1.
|
|
553 |
i3 := 2*n+1.
|
|
554 |
1 to:n do:[:vi |
|
|
555 |
val := ((valueArray at:vi) * scaleFactor) rounded.
|
|
556 |
rV at:vi put:val.
|
|
557 |
val := ((valueArray at:i2) * scaleFactor) rounded.
|
|
558 |
gV at:vi put:val.
|
|
559 |
val := ((valueArray at:i3) * scaleFactor) rounded.
|
|
560 |
bV at:vi put:val.
|
|
561 |
i2 := i2 + 1.
|
|
562 |
i3 := i3 + 1.
|
28
|
563 |
].
|
35
|
564 |
colorMap := Colormap redVector:rV greenVector:gV blueVector:bV.
|
28
|
565 |
^ self
|
0
|
566 |
].
|
30
|
567 |
(tagType == 332) ifTrue:[
|
|
568 |
"ink set"
|
|
569 |
"
|
|
570 |
'ink set' print. value printNewline.
|
|
571 |
"
|
|
572 |
^ self
|
|
573 |
].
|
|
574 |
(tagType == 333) ifTrue:[
|
|
575 |
"ink names"
|
|
576 |
"
|
|
577 |
'ink names' print. value printNewline.
|
|
578 |
"
|
|
579 |
^ self
|
|
580 |
].
|
|
581 |
(tagType == 336) ifTrue:[
|
|
582 |
"dot range"
|
|
583 |
"
|
|
584 |
'dot range' print. value printNewline.
|
|
585 |
"
|
|
586 |
^ self
|
|
587 |
].
|
|
588 |
(tagType == 337) ifTrue:[
|
|
589 |
"target printer"
|
|
590 |
"
|
|
591 |
'target printer' print. value printNewline.
|
|
592 |
"
|
|
593 |
^ self
|
|
594 |
].
|
|
595 |
(tagType == 339) ifTrue:[
|
|
596 |
"sample format"
|
|
597 |
"
|
|
598 |
'sample format' print. value printNewline.
|
|
599 |
"
|
|
600 |
^ self
|
|
601 |
].
|
|
602 |
(tagType == 340) ifTrue:[
|
|
603 |
"min sample value"
|
|
604 |
"
|
|
605 |
'min sample value' print. value printNewline.
|
|
606 |
"
|
|
607 |
^ self
|
|
608 |
].
|
|
609 |
(tagType == 341) ifTrue:[
|
|
610 |
"max sample value"
|
|
611 |
"
|
|
612 |
'max sample value' print. value printNewline.
|
|
613 |
"
|
|
614 |
^ self
|
|
615 |
].
|
|
616 |
(tagType == 512) ifTrue:[
|
|
617 |
"jpeg proc"
|
|
618 |
"
|
|
619 |
'jpeg proc' print. value printNewline.
|
|
620 |
"
|
|
621 |
^ self
|
|
622 |
].
|
|
623 |
(tagType == 513) ifTrue:[
|
|
624 |
"jpeg proc"
|
|
625 |
"
|
|
626 |
'jpeg proc' print. value printNewline.
|
|
627 |
"
|
|
628 |
^ self
|
|
629 |
].
|
|
630 |
(tagType == 32995) ifTrue:[
|
|
631 |
"matteing"
|
|
632 |
"
|
|
633 |
'matteing' print. value printNewline.
|
|
634 |
"
|
|
635 |
^ self
|
|
636 |
].
|
|
637 |
(tagType == 32996) ifTrue:[
|
|
638 |
"datatype"
|
|
639 |
"
|
|
640 |
'datatype' print. value printNewline.
|
|
641 |
"
|
|
642 |
^ self
|
|
643 |
].
|
|
644 |
(tagType == 32997) ifTrue:[
|
|
645 |
"imagedepth"
|
|
646 |
"
|
|
647 |
'imagedepth' print. value printNewline.
|
|
648 |
"
|
|
649 |
^ self
|
|
650 |
].
|
|
651 |
(tagType == 32998) ifTrue:[
|
|
652 |
"tiledepth"
|
|
653 |
"
|
|
654 |
'tiledepth' print. value printNewline.
|
|
655 |
"
|
|
656 |
^ self
|
|
657 |
].
|
0
|
658 |
|
21
|
659 |
"
|
25
|
660 |
'TIFFReader: tag:' print. tagType print. ' typ:' print. numberType print.
|
0
|
661 |
' len:' print. length print. ' offs:' print. offset print.
|
|
662 |
' val:' print. value print. ' valArr:' print. valueArray printNewline.
|
21
|
663 |
"
|
25
|
664 |
'TIFFReader: unknown tag type ' errorPrint. tagType errorPrintNL
|
0
|
665 |
!
|
|
666 |
|
135
|
667 |
readCCITT3RLETiffImageData
|
|
668 |
'TIFFReader: ccitt mod Huffman (rle) compression not implemented' errorPrintNL.
|
|
669 |
!
|
|
670 |
|
|
671 |
readCCITT3RLEWTiffImageData
|
|
672 |
'TIFFReader: ccitt mod Huffman (rlew) compression not implemented' errorPrintNL.
|
|
673 |
!
|
|
674 |
|
|
675 |
readCCITTGroup3TiffImageData
|
|
676 |
"not really tested - all I got is a single
|
|
677 |
fax from NeXT step"
|
|
678 |
|
|
679 |
|bytesPerRow bitsPerRow compressedStrip nPlanes
|
|
680 |
stripNr "{ Class: SmallInteger }"
|
|
681 |
offset "{ Class: SmallInteger }"
|
|
682 |
row "{ Class: SmallInteger }"
|
|
683 |
bytesPerStrip "{ Class: SmallInteger }" |
|
|
684 |
|
|
685 |
nPlanes := samplesPerPixel.
|
|
686 |
(nPlanes == 2) ifTrue:[
|
|
687 |
'TIFFReader: ignoring alpha plane' errorPrintNL.
|
|
688 |
nPlanes := 1
|
|
689 |
].
|
|
690 |
|
|
691 |
(nPlanes ~~ 1) ifTrue:[
|
|
692 |
self error:'only monochrome/greyscale supported'.
|
|
693 |
^ nil
|
|
694 |
].
|
0
|
695 |
|
135
|
696 |
stripByteCounts isNil ifTrue:[
|
|
697 |
self error:'currently require stripByteCounts'.
|
|
698 |
^ nil
|
|
699 |
].
|
|
700 |
(rowsPerStrip ~~ 1) isNil ifTrue:[
|
|
701 |
self error:'currently require rowsPerStrip to be 1'.
|
|
702 |
^ nil
|
|
703 |
].
|
|
704 |
|
|
705 |
'TIFFReader: decompressing CCITT-3 ...' infoPrintNL.
|
|
706 |
|
|
707 |
bitsPerRow := width * (bitsPerSample at:1).
|
|
708 |
bytesPerRow := bitsPerRow // 8.
|
|
709 |
((bitsPerRow \\ 8) ~~ 0) ifTrue:[
|
|
710 |
bytesPerRow := bytesPerRow + 1
|
|
711 |
].
|
|
712 |
|
|
713 |
data := ByteArray new:(bytesPerRow * height).
|
|
714 |
compressedStrip := ByteArray uninitializedNew:bytesPerRow.
|
|
715 |
|
|
716 |
offset := 1.
|
|
717 |
stripNr := 0.
|
|
718 |
|
|
719 |
row := 1.
|
|
720 |
bytesPerStrip := bytesPerRow * rowsPerStrip.
|
|
721 |
[row <= height] whileTrue:[
|
|
722 |
stripNr := stripNr + 1.
|
|
723 |
inStream position:((stripOffsets at:stripNr) + 1).
|
|
724 |
inStream nextBytes:(stripByteCounts at:stripNr) into:compressedStrip.
|
|
725 |
self class decompressCCITT3From:compressedStrip
|
|
726 |
into:data
|
|
727 |
startingAt:offset
|
|
728 |
count:width.
|
|
729 |
offset := offset + bytesPerStrip.
|
|
730 |
row := row + rowsPerStrip
|
|
731 |
]
|
|
732 |
!
|
|
733 |
|
|
734 |
readCCITTGroup4TiffImageData
|
|
735 |
'TIFFReader: ccitt group4 fax compression not implemented' errorPrintNL.
|
|
736 |
!
|
0
|
737 |
|
135
|
738 |
readChars:n
|
|
739 |
"read n characters and return them in a string"
|
|
740 |
|
|
741 |
|oldPos offset string|
|
|
742 |
|
|
743 |
string := String new:(n - 1).
|
|
744 |
(n <= 4) ifTrue:[
|
|
745 |
inStream nextBytes:(n - 1) into:string
|
|
746 |
] ifFalse:[
|
|
747 |
offset := inStream nextLongMSB:(byteOrder ~~ #lsb).
|
|
748 |
oldPos := inStream position.
|
|
749 |
inStream position:(offset + 1).
|
|
750 |
inStream nextBytes:(n - 1) into:string.
|
|
751 |
inStream position:oldPos
|
|
752 |
].
|
|
753 |
^ string
|
|
754 |
!
|
|
755 |
|
|
756 |
readFracts:nFracts
|
|
757 |
"read nFracts fractions (2 32bit words) and return them in an array"
|
|
758 |
|
|
759 |
|oldPos offset values numerator denominator msb
|
|
760 |
n "{ Class: SmallInteger }" |
|
|
761 |
|
|
762 |
n := nFracts.
|
|
763 |
|
|
764 |
msb := byteOrder ~~ #lsb.
|
|
765 |
values := Array basicNew:n.
|
|
766 |
offset := inStream nextLongMSB:msb.
|
|
767 |
oldPos := inStream position.
|
|
768 |
inStream position:(offset + 1).
|
|
769 |
1 to:n do:[:index |
|
|
770 |
numerator := inStream nextLongMSB:msb.
|
|
771 |
denominator := inStream nextLongMSB:msb.
|
|
772 |
values at:index put:(Fraction numerator:numerator denominator:denominator)
|
|
773 |
].
|
|
774 |
inStream position:oldPos.
|
|
775 |
^ values
|
|
776 |
!
|
|
777 |
|
|
778 |
readJPEGTiffImageData
|
|
779 |
'TIFFReader: jpeg compression not implemented' errorPrintNL
|
|
780 |
!
|
|
781 |
|
|
782 |
readLZWTiffImageData
|
|
783 |
"read LZW compressed tiff data; this method only
|
|
784 |
handles 3x8 rgb and 1x2 or 2x2 greyscale images.
|
|
785 |
For 2x2 greyscale images, the alpha plane is ignored.
|
|
786 |
(maybe other formats work also - its simply not
|
|
787 |
tested)"
|
|
788 |
|
|
789 |
|bytesPerRow compressedStrip nPlanes
|
|
790 |
bytesPerStrip "{ Class: SmallInteger }"
|
|
791 |
nBytes "{ Class: SmallInteger }"
|
|
792 |
prevSize "{ Class: SmallInteger }"
|
|
793 |
stripNr "{ Class: SmallInteger }"
|
|
794 |
offset "{ Class: SmallInteger }"
|
|
795 |
row "{ Class: SmallInteger }" |
|
|
796 |
|
|
797 |
nPlanes := samplesPerPixel.
|
|
798 |
|
|
799 |
(nPlanes == 3) ifTrue:[
|
|
800 |
((bitsPerSample at:1) ~~ 8) ifTrue:[
|
|
801 |
self error:'only 8 bit/sample supported'.
|
|
802 |
^ nil
|
|
803 |
].
|
|
804 |
((bitsPerSample at:2) ~~ 8) ifTrue:[
|
|
805 |
self error:'only 8 bit/sample supported'.
|
|
806 |
^ nil
|
|
807 |
].
|
|
808 |
((bitsPerSample at:3) ~~ 8) ifTrue:[
|
|
809 |
self error:'only 8 bit/sample supported'.
|
|
810 |
^ nil
|
|
811 |
].
|
|
812 |
bytesPerRow := width * samplesPerPixel.
|
0
|
813 |
] ifFalse:[
|
135
|
814 |
(nPlanes == 2) ifTrue:[
|
|
815 |
(planarConfiguration ~~ 2) ifTrue:[
|
|
816 |
self error:'only separate planes supported'.
|
|
817 |
^ nil
|
|
818 |
].
|
|
819 |
'TIFFReader: ignoring alpha plane' errorPrintNL.
|
|
820 |
nPlanes := 1
|
|
821 |
].
|
|
822 |
(nPlanes == 1) ifFalse:[
|
|
823 |
self error:'only 3-sample rgb / monochrome supported'.
|
|
824 |
^ nil
|
|
825 |
].
|
|
826 |
bytesPerRow := (width * (bitsPerSample at:1) + 7) // 8.
|
|
827 |
].
|
|
828 |
|
|
829 |
stripByteCounts isNil ifTrue:[
|
|
830 |
self error:'currently require stripByteCounts'.
|
|
831 |
^ nil
|
|
832 |
].
|
|
833 |
|
|
834 |
'TIFFReader: decompressing LZW ...' infoPrintNL.
|
|
835 |
|
|
836 |
data := ByteArray uninitializedNew:(bytesPerRow * height).
|
|
837 |
|
|
838 |
offset := 1.
|
|
839 |
stripNr := 0.
|
|
840 |
|
|
841 |
row := 1.
|
|
842 |
bytesPerStrip := bytesPerRow * rowsPerStrip.
|
|
843 |
prevSize := 0.
|
|
844 |
[row <= height] whileTrue:[
|
|
845 |
stripNr := stripNr + 1.
|
|
846 |
inStream position:((stripOffsets at:stripNr) + 1).
|
|
847 |
nBytes := stripByteCounts at:stripNr.
|
|
848 |
(nBytes > prevSize) ifTrue:[
|
|
849 |
compressedStrip := ByteArray uninitializedNew:nBytes.
|
|
850 |
prevSize := nBytes
|
|
851 |
].
|
|
852 |
inStream nextBytes:nBytes
|
|
853 |
into:compressedStrip.
|
|
854 |
self class decompressLZWFrom:compressedStrip
|
|
855 |
count:nBytes
|
|
856 |
into:data
|
|
857 |
startingAt:offset.
|
|
858 |
offset := offset + bytesPerStrip.
|
|
859 |
row := row + rowsPerStrip
|
|
860 |
].
|
|
861 |
|
|
862 |
(predictor == 2) ifTrue:[
|
|
863 |
self class decodeDelta:3 in:data width:width height:height
|
|
864 |
]
|
|
865 |
!
|
|
866 |
|
|
867 |
readLongs:nLongs
|
|
868 |
"read nLongs long numbers (32bit) and return them in an array"
|
|
869 |
|
|
870 |
|oldPos offset values msb
|
|
871 |
n "{ Class: SmallInteger }" |
|
|
872 |
|
|
873 |
n := nLongs.
|
|
874 |
|
|
875 |
msb := byteOrder ~~ #lsb.
|
|
876 |
values := Array basicNew:n.
|
|
877 |
(n == 1) ifTrue:[
|
|
878 |
values at:1 put:(inStream nextLongMSB:msb).
|
|
879 |
] ifFalse:[
|
|
880 |
offset := inStream nextLongMSB:msb.
|
|
881 |
oldPos := inStream position.
|
|
882 |
inStream position:(offset + 1).
|
|
883 |
1 to:n do:[:index |
|
|
884 |
values at:index put:(inStream nextLongMSB:msb)
|
|
885 |
].
|
|
886 |
inStream position:oldPos
|
|
887 |
].
|
|
888 |
^ values
|
|
889 |
!
|
|
890 |
|
|
891 |
readNeXTJPEGTiffImageData
|
|
892 |
'TIFFReader: jpeg compression not implemented' errorPrintNL
|
|
893 |
!
|
|
894 |
|
|
895 |
readNeXTRLE2TiffImageData
|
|
896 |
'TIFFReader: next 2bit rle compression not implemented' errorPrintNL.
|
|
897 |
!
|
|
898 |
|
|
899 |
readPackbitsTiffImageData
|
|
900 |
"had no samples yet - however, packbits decompression
|
|
901 |
is rather trivial to add ..."
|
|
902 |
|
|
903 |
'TIFFReader: packbits compression not implemented' errorPrintNL
|
|
904 |
!
|
|
905 |
|
|
906 |
readShorts:nShorts
|
|
907 |
"read nShorts short numbers (16bit) and return them in an array"
|
|
908 |
|
|
909 |
|oldPos offset values msb val2
|
|
910 |
n "{ Class: SmallInteger }" |
|
0
|
911 |
|
135
|
912 |
n := nShorts.
|
|
913 |
|
|
914 |
msb := (byteOrder ~~ #lsb).
|
|
915 |
values := Array basicNew:n.
|
|
916 |
(n <= 2) ifTrue:[
|
|
917 |
values at:1 put:(inStream nextUnsignedShortMSB:msb).
|
|
918 |
val2 := inStream nextUnsignedShortMSB:msb.
|
|
919 |
|
|
920 |
(n == 2) ifTrue:[
|
|
921 |
values at:2 put:val2
|
|
922 |
]
|
|
923 |
] ifFalse:[
|
|
924 |
offset := inStream nextLongMSB:msb.
|
|
925 |
oldPos := inStream position.
|
|
926 |
inStream position:(offset + 1).
|
|
927 |
1 to:n do:[:index |
|
|
928 |
values at:index put:(inStream nextUnsignedShortMSB:msb)
|
|
929 |
].
|
|
930 |
inStream position:oldPos
|
|
931 |
].
|
|
932 |
^ values
|
|
933 |
!
|
|
934 |
|
|
935 |
readUncompressedTiffImageData
|
|
936 |
|bytesPerRow bitsPerRow nPlanes
|
|
937 |
stripNr "{ Class: SmallInteger }"
|
|
938 |
offset "{ Class: SmallInteger }"
|
|
939 |
row "{ Class: SmallInteger }"
|
|
940 |
nBytes "{ Class: SmallInteger }"
|
|
941 |
bitsPerPixel overAllBytes|
|
|
942 |
|
|
943 |
nPlanes := samplesPerPixel.
|
|
944 |
|
|
945 |
"only support 1-sample/pixel,
|
|
946 |
with alpha - if separate planes,
|
|
947 |
or rgb - if non separate planes and no alpha"
|
|
948 |
|
|
949 |
(nPlanes == 2) ifTrue:[
|
|
950 |
(planarConfiguration ~~ 2) ifTrue:[
|
|
951 |
self error:'with alpha, only separate planes supported'.
|
|
952 |
^ nil
|
28
|
953 |
].
|
135
|
954 |
'TIFFReader: ignoring alpha plane' errorPrintNL.
|
|
955 |
nPlanes := 1.
|
|
956 |
bitsPerPixel := bitsPerSample at:1.
|
|
957 |
bitsPerSample := Array with:bitsPerPixel.
|
|
958 |
samplesPerPixel := 1.
|
|
959 |
] ifFalse:[
|
|
960 |
(nPlanes == 3) ifTrue:[
|
|
961 |
(planarConfiguration ~~ 1) ifTrue:[
|
|
962 |
self error:'only non separate planes supported'.
|
|
963 |
^ nil
|
|
964 |
].
|
|
965 |
bitsPerSample ~= #(8 8 8) ifTrue:[
|
|
966 |
self error:'only 8/8/8 rgb images supported'.
|
|
967 |
^ nil
|
|
968 |
].
|
|
969 |
bitsPerPixel := 24
|
|
970 |
] ifFalse:[
|
|
971 |
(nPlanes ~~ 1) ifTrue:[
|
|
972 |
self error:'format not supported'.
|
|
973 |
^ nil
|
|
974 |
].
|
|
975 |
bitsPerPixel := bitsPerSample at:1.
|
|
976 |
]
|
0
|
977 |
].
|
135
|
978 |
|
|
979 |
bitsPerRow := width * bitsPerPixel.
|
|
980 |
bytesPerRow := bitsPerRow // 8.
|
|
981 |
((bitsPerRow \\ 8) ~~ 0) ifTrue:[
|
|
982 |
bytesPerRow := bytesPerRow + 1
|
|
983 |
].
|
|
984 |
|
|
985 |
overAllBytes := bytesPerRow * height.
|
|
986 |
data := ByteArray uninitializedNew:overAllBytes.
|
|
987 |
|
|
988 |
offset := 1.
|
|
989 |
stripNr := 0.
|
|
990 |
|
|
991 |
row := 1.
|
|
992 |
[row <= height] whileTrue:[
|
|
993 |
stripNr := stripNr + 1.
|
|
994 |
nBytes := stripByteCounts at:stripNr.
|
|
995 |
inStream position:((stripOffsets at:stripNr) + 1).
|
|
996 |
|
|
997 |
inStream nextBytes:nBytes into:data startingAt:offset.
|
|
998 |
offset := offset + nBytes.
|
|
999 |
row := row + rowsPerStrip
|
|
1000 |
]
|
|
1001 |
!
|
|
1002 |
|
|
1003 |
writeBitsPerSample
|
14
|
1004 |
"
|
135
|
1005 |
'bitsPerSample: ' print. bitsPerSample printNewline.
|
|
1006 |
'store bitspersample at: ' print. outStream position printNewline.
|
14
|
1007 |
"
|
135
|
1008 |
bitsPerSamplePos := outStream position.
|
|
1009 |
bitsPerSample do:[:n |
|
|
1010 |
self writeShort:n
|
|
1011 |
]
|
0
|
1012 |
!
|
|
1013 |
|
|
1014 |
writeColorMap
|
28
|
1015 |
|n|
|
|
1016 |
|
0
|
1017 |
colorMapPos := outStream position.
|
35
|
1018 |
#(red green blue) do:[:component |
|
28
|
1019 |
n := 0.
|
35
|
1020 |
colorMap do:[:clr |
|
|
1021 |
|entry|
|
|
1022 |
|
|
1023 |
clr isNil ifTrue:[
|
|
1024 |
entry := 0
|
28
|
1025 |
] ifFalse:[
|
35
|
1026 |
entry := clr perform:component.
|
|
1027 |
"
|
|
1028 |
tiff map is 16 bit - scale from percent to 0..16rFFFF
|
|
1029 |
"
|
|
1030 |
entry := (entry * 16rFFFF / 100) rounded.
|
28
|
1031 |
].
|
35
|
1032 |
self writeShort:entry.
|
28
|
1033 |
n := n + 1
|
|
1034 |
].
|
|
1035 |
"
|
|
1036 |
fill to 256 entries
|
|
1037 |
"
|
|
1038 |
[n < 256] whileTrue:[
|
|
1039 |
self writeShort:0.
|
|
1040 |
n := n + 1.
|
|
1041 |
]
|
0
|
1042 |
]
|
|
1043 |
!
|
|
1044 |
|
|
1045 |
writeStripByteCounts
|
3
|
1046 |
"
|
0
|
1047 |
'stripByteCounts: ' print. stripByteCounts printNewline.
|
|
1048 |
'store stripbytecounts at: ' print. outStream position printNewline.
|
3
|
1049 |
"
|
0
|
1050 |
stripByteCountsPos := outStream position.
|
|
1051 |
stripByteCounts do:[:c |
|
28
|
1052 |
self writeShort:c
|
0
|
1053 |
]
|
|
1054 |
!
|
|
1055 |
|
135
|
1056 |
writeStripOffsets
|
3
|
1057 |
"
|
135
|
1058 |
'stripOffsets: ' print. stripOffsets printNewline.
|
|
1059 |
'store stripoffsets at: ' print. outStream position printNewline.
|
3
|
1060 |
"
|
135
|
1061 |
stripOffsetsPos := outStream position.
|
|
1062 |
stripOffsets do:[:o |
|
|
1063 |
self writeLong:o
|
0
|
1064 |
]
|
|
1065 |
!
|
|
1066 |
|
|
1067 |
writeTag:tagType
|
|
1068 |
self writeTiffTag:tagType.
|
|
1069 |
!
|
|
1070 |
|
|
1071 |
writeTiffTag:tagType
|
|
1072 |
|value valueArray numberType count address|
|
|
1073 |
|
|
1074 |
count := 1.
|
|
1075 |
address := nil.
|
|
1076 |
(tagType == 253) ifTrue:[
|
28
|
1077 |
"tiff class"
|
0
|
1078 |
].
|
|
1079 |
(tagType == 254) ifTrue:[
|
|
1080 |
].
|
|
1081 |
(tagType == 255) ifTrue:[
|
28
|
1082 |
"SubfileType"
|
|
1083 |
value := subFileType.
|
|
1084 |
numberType := #long.
|
0
|
1085 |
].
|
|
1086 |
(tagType == 256) ifTrue:[
|
28
|
1087 |
"ImageWidth"
|
|
1088 |
value := width.
|
|
1089 |
numberType := #short.
|
0
|
1090 |
].
|
|
1091 |
(tagType == 257) ifTrue:[
|
28
|
1092 |
"ImageHeight"
|
|
1093 |
value := height.
|
|
1094 |
numberType := #short.
|
0
|
1095 |
].
|
|
1096 |
(tagType == 258) ifTrue:[
|
28
|
1097 |
"bitspersample"
|
|
1098 |
address := bitsPerSamplePos - 1.
|
|
1099 |
numberType := #short.
|
|
1100 |
count := bitsPerSample size.
|
|
1101 |
valueArray := bitsPerSample
|
0
|
1102 |
].
|
|
1103 |
(tagType == 259) ifTrue:[
|
28
|
1104 |
"compression"
|
|
1105 |
value := compression.
|
|
1106 |
numberType := #short.
|
0
|
1107 |
].
|
|
1108 |
(tagType == 262) ifTrue:[
|
28
|
1109 |
"photometric"
|
|
1110 |
(photometric == #whiteIs0) ifTrue:[
|
|
1111 |
value := 0
|
|
1112 |
] ifFalse:[
|
|
1113 |
(photometric == #blackIs0) ifTrue:[
|
|
1114 |
value := 1
|
|
1115 |
] ifFalse:[
|
|
1116 |
(photometric == #rgb) ifTrue:[
|
|
1117 |
value := 2
|
|
1118 |
] ifFalse:[
|
|
1119 |
(photometric == #palette) ifTrue:[
|
|
1120 |
value := 3
|
|
1121 |
] ifFalse:[
|
|
1122 |
(photometric == #transparency) ifTrue:[
|
|
1123 |
value := 4
|
|
1124 |
] ifFalse:[
|
|
1125 |
self error:'bad photometric'
|
|
1126 |
]
|
|
1127 |
]
|
|
1128 |
]
|
|
1129 |
]
|
|
1130 |
].
|
|
1131 |
numberType := #short.
|
0
|
1132 |
].
|
|
1133 |
(tagType == 263) ifTrue:[
|
|
1134 |
].
|
|
1135 |
(tagType == 264) ifTrue:[
|
|
1136 |
].
|
|
1137 |
(tagType == 265) ifTrue:[
|
|
1138 |
].
|
|
1139 |
(tagType == 266) ifTrue:[
|
28
|
1140 |
"fillOrder"
|
|
1141 |
(fillOrder == #msb) ifTrue:[
|
|
1142 |
value := 1
|
|
1143 |
] ifFalse:[
|
|
1144 |
(fillOrder == #lsb) ifTrue:[
|
|
1145 |
value := 2
|
|
1146 |
] ifFalse:[
|
|
1147 |
self error:'bad fillOrder'
|
|
1148 |
]
|
|
1149 |
].
|
|
1150 |
numberType := #short.
|
0
|
1151 |
].
|
|
1152 |
(tagType == 269) ifTrue:[
|
|
1153 |
].
|
|
1154 |
(tagType == 270) ifTrue:[
|
|
1155 |
].
|
|
1156 |
(tagType == 271) ifTrue:[
|
|
1157 |
].
|
|
1158 |
(tagType == 272) ifTrue:[
|
|
1159 |
].
|
|
1160 |
(tagType == 273) ifTrue:[
|
28
|
1161 |
"stripoffsets"
|
|
1162 |
address := stripOffsetsPos - 1.
|
|
1163 |
numberType := #long.
|
|
1164 |
count := stripOffsets size.
|
|
1165 |
valueArray := stripOffsets
|
0
|
1166 |
].
|
|
1167 |
(tagType == 274) ifTrue:[
|
|
1168 |
].
|
|
1169 |
(tagType == 277) ifTrue:[
|
28
|
1170 |
"samplesPerPixel"
|
|
1171 |
value := samplesPerPixel.
|
|
1172 |
numberType := #short.
|
0
|
1173 |
].
|
|
1174 |
(tagType == 278) ifTrue:[
|
28
|
1175 |
"rowsperstrip"
|
|
1176 |
value := rowsPerStrip.
|
|
1177 |
numberType := #short.
|
0
|
1178 |
].
|
|
1179 |
(tagType == 279) ifTrue:[
|
28
|
1180 |
"stripbytecount"
|
|
1181 |
address := stripByteCountsPos - 1.
|
|
1182 |
numberType := #short.
|
|
1183 |
count := stripByteCounts size.
|
|
1184 |
valueArray := stripByteCounts
|
0
|
1185 |
].
|
|
1186 |
(tagType == 280) ifTrue:[
|
28
|
1187 |
"min sample value"
|
0
|
1188 |
].
|
|
1189 |
(tagType == 281) ifTrue:[
|
28
|
1190 |
"max sample value"
|
0
|
1191 |
].
|
|
1192 |
(tagType == 282) ifTrue:[
|
28
|
1193 |
"x resolution"
|
0
|
1194 |
].
|
|
1195 |
(tagType == 283) ifTrue:[
|
28
|
1196 |
"y resolution"
|
0
|
1197 |
].
|
|
1198 |
(tagType == 284) ifTrue:[
|
28
|
1199 |
"planarconfig"
|
|
1200 |
value := planarConfiguration.
|
|
1201 |
numberType := #short.
|
0
|
1202 |
].
|
|
1203 |
(tagType == 285) ifTrue:[
|
28
|
1204 |
"pageName"
|
0
|
1205 |
].
|
|
1206 |
(tagType == 286) ifTrue:[
|
28
|
1207 |
"xPosition"
|
0
|
1208 |
].
|
|
1209 |
(tagType == 287) ifTrue:[
|
28
|
1210 |
"yPosition"
|
0
|
1211 |
].
|
|
1212 |
(tagType == 288) ifTrue:[
|
28
|
1213 |
"freeOffsets"
|
0
|
1214 |
].
|
|
1215 |
(tagType == 289) ifTrue:[
|
28
|
1216 |
"freeByteCounts"
|
0
|
1217 |
].
|
|
1218 |
(tagType == 290) ifTrue:[
|
28
|
1219 |
"grayResponceUnit"
|
0
|
1220 |
].
|
|
1221 |
(tagType == 291) ifTrue:[
|
28
|
1222 |
"grayResponceCurve"
|
0
|
1223 |
].
|
|
1224 |
(tagType == 292) ifTrue:[
|
28
|
1225 |
"group3options"
|
|
1226 |
value := group3options.
|
|
1227 |
numberType := #long.
|
0
|
1228 |
].
|
|
1229 |
(tagType == 293) ifTrue:[
|
28
|
1230 |
"group4options"
|
0
|
1231 |
].
|
|
1232 |
(tagType == 296) ifTrue:[
|
28
|
1233 |
"resolutionunit"
|
|
1234 |
^ self
|
0
|
1235 |
].
|
|
1236 |
(tagType == 297) ifTrue:[
|
28
|
1237 |
"pageNumber"
|
0
|
1238 |
].
|
|
1239 |
(tagType == 300) ifTrue:[
|
28
|
1240 |
"colorResponceUnit"
|
0
|
1241 |
].
|
|
1242 |
(tagType == 301) ifTrue:[
|
28
|
1243 |
"colorResponceCurve"
|
0
|
1244 |
].
|
|
1245 |
(tagType == 306) ifTrue:[
|
28
|
1246 |
"dateTime"
|
0
|
1247 |
].
|
|
1248 |
(tagType == 315) ifTrue:[
|
28
|
1249 |
"artist"
|
0
|
1250 |
].
|
|
1251 |
(tagType == 317) ifTrue:[
|
28
|
1252 |
"predictor"
|
0
|
1253 |
].
|
|
1254 |
(tagType == 320) ifTrue:[
|
28
|
1255 |
"colormap"
|
|
1256 |
address := colorMapPos - 1.
|
|
1257 |
numberType := #short.
|
|
1258 |
count := 256 "(colorMap at:1) size" * 3.
|
0
|
1259 |
].
|
|
1260 |
|
|
1261 |
(value isNil and:[address isNil]) ifTrue:[
|
28
|
1262 |
self error:'unhandled tag'.
|
|
1263 |
^ self
|
0
|
1264 |
].
|
|
1265 |
|
14
|
1266 |
"
|
0
|
1267 |
'tag:' print. tagType print. ' typ:' print. numberType print.
|
|
1268 |
' len:' print. count print.
|
|
1269 |
' val:' print. value printNewline.
|
14
|
1270 |
"
|
0
|
1271 |
|
|
1272 |
self writeShort:tagType.
|
|
1273 |
numberType == #short ifTrue:[
|
28
|
1274 |
self writeShort:3.
|
|
1275 |
self writeLong:count.
|
0
|
1276 |
] ifFalse:[
|
28
|
1277 |
numberType == #long ifTrue:[
|
|
1278 |
self writeShort:4.
|
|
1279 |
self writeLong:count.
|
|
1280 |
] ifFalse:[
|
|
1281 |
numberType == #byte ifTrue:[
|
|
1282 |
self writeShort:1.
|
|
1283 |
self writeLong:count.
|
|
1284 |
] ifFalse:[
|
|
1285 |
self error:'bad numbertype'
|
|
1286 |
]
|
|
1287 |
]
|
0
|
1288 |
].
|
|
1289 |
address notNil ifTrue:[
|
28
|
1290 |
(numberType == #long and:[count == 1]) ifTrue:[
|
|
1291 |
self writeLong:(valueArray at:1).
|
|
1292 |
^ self
|
|
1293 |
].
|
|
1294 |
(numberType == #short and:[count <= 2]) ifTrue:[
|
|
1295 |
self writeShort:(valueArray at:1).
|
|
1296 |
count == 2 ifTrue:[
|
|
1297 |
self writeShort:(valueArray at:2).
|
|
1298 |
] ifFalse:[
|
|
1299 |
self writeShort:0
|
|
1300 |
].
|
|
1301 |
^ self
|
|
1302 |
].
|
|
1303 |
(numberType == #byte and:[count <= 4]) ifTrue:[
|
|
1304 |
outStream nextPut:(valueArray at:1).
|
|
1305 |
count > 1 ifTrue:[
|
|
1306 |
outStream nextPut:(valueArray at:2).
|
|
1307 |
count > 2 ifTrue:[
|
|
1308 |
outStream nextPut:(valueArray at:3).
|
|
1309 |
count > 3 ifTrue:[
|
|
1310 |
outStream nextPut:(valueArray at:4).
|
|
1311 |
] ifFalse:[
|
|
1312 |
outStream nextPut:0
|
|
1313 |
].
|
|
1314 |
] ifFalse:[
|
|
1315 |
outStream nextPut:0
|
|
1316 |
].
|
|
1317 |
] ifFalse:[
|
|
1318 |
outStream nextPut:0
|
|
1319 |
].
|
|
1320 |
^ self
|
|
1321 |
].
|
|
1322 |
self writeLong:address.
|
|
1323 |
^ self
|
0
|
1324 |
].
|
|
1325 |
numberType == #short ifTrue:[
|
28
|
1326 |
self writeShort:value.
|
|
1327 |
self writeShort:0
|
0
|
1328 |
] ifFalse:[
|
28
|
1329 |
numberType == #long ifTrue:[
|
|
1330 |
self writeLong:value
|
|
1331 |
] ifFalse:[
|
|
1332 |
numberType == #byte ifTrue:[
|
|
1333 |
outStream nextPut:value.
|
|
1334 |
outStream nextPut:0.
|
|
1335 |
outStream nextPut:0.
|
|
1336 |
outStream nextPut:0.
|
|
1337 |
] ifFalse:[
|
|
1338 |
self error:'bad numbertype'
|
|
1339 |
]
|
|
1340 |
]
|
0
|
1341 |
].
|
|
1342 |
!
|
|
1343 |
|
135
|
1344 |
writeUncompressedBits
|
|
1345 |
"write bits as one or multiple strips"
|
|
1346 |
|
|
1347 |
|offs bytesPerRow nBytes
|
|
1348 |
h "{ Class: SmallInteger }"|
|
0
|
1349 |
|
135
|
1350 |
nBytes := data size.
|
|
1351 |
nBytes < 16rFFFF ifTrue:[
|
|
1352 |
stripOffsets := Array with:(outStream position - 1).
|
|
1353 |
stripByteCounts := Array with:nBytes.
|
|
1354 |
outStream nextPutBytes:nBytes from:data.
|
|
1355 |
rowsPerStrip := height
|
|
1356 |
] ifFalse:[
|
|
1357 |
stripOffsets := Array basicNew:height.
|
|
1358 |
bytesPerRow := nBytes // height.
|
|
1359 |
stripByteCounts := (Array basicNew:height) atAllPut:bytesPerRow.
|
0
|
1360 |
|
135
|
1361 |
offs := 1.
|
|
1362 |
h := height.
|
|
1363 |
1 to:h do:[:row |
|
|
1364 |
stripOffsets at:row put:(outStream position - 1).
|
|
1365 |
outStream nextPutBytes:bytesPerRow from:data startingAt:offs.
|
|
1366 |
offs := offs + bytesPerRow
|
28
|
1367 |
].
|
135
|
1368 |
rowsPerStrip := 1
|
|
1369 |
].
|
|
1370 |
"
|
|
1371 |
'stripOffsets: ' print. stripOffsets printNewline.
|
|
1372 |
'stripByteCounts: ' print. stripByteCounts printNewline.
|
|
1373 |
"
|
|
1374 |
! !
|
|
1375 |
|
|
1376 |
!TIFFReader methodsFor:'reading from file'!
|
|
1377 |
|
|
1378 |
fromStream:aStream
|
|
1379 |
"read an image from aStream"
|
|
1380 |
|
|
1381 |
|char1 char2 version
|
|
1382 |
numberOfTags "{ Class: SmallInteger }"
|
|
1383 |
tagType "{ Class: SmallInteger }"
|
|
1384 |
numberType "{ Class: SmallInteger }"
|
|
1385 |
length "{ Class: SmallInteger }"
|
|
1386 |
result offset ok msb|
|
|
1387 |
|
|
1388 |
inStream := aStream.
|
|
1389 |
|
|
1390 |
char1 := aStream next.
|
|
1391 |
char2 := aStream next.
|
|
1392 |
(char1 ~~ char2) ifTrue:[
|
|
1393 |
'TIFFReader: not a tiff file' errorPrintNL.
|
|
1394 |
^ nil
|
|
1395 |
].
|
|
1396 |
(char1 == $I) ifTrue:[
|
|
1397 |
byteOrder := #lsb.
|
|
1398 |
msb := false.
|
0
|
1399 |
] ifFalse:[
|
135
|
1400 |
(char1 == $M) ifTrue:[
|
|
1401 |
byteOrder := #msb.
|
|
1402 |
msb := true.
|
28
|
1403 |
] ifFalse:[
|
135
|
1404 |
'TIFFReader: not a tiff file' errorPrintNL.
|
|
1405 |
^ nil
|
28
|
1406 |
]
|
0
|
1407 |
].
|
|
1408 |
|
135
|
1409 |
aStream binary.
|
|
1410 |
|
|
1411 |
version := self readShort.
|
|
1412 |
(version ~~ 42) ifTrue:[
|
|
1413 |
'TIFFReader: version of tiff-file not supported' errorPrintNL.
|
|
1414 |
^ nil
|
0
|
1415 |
].
|
|
1416 |
|
135
|
1417 |
"setup default values"
|
0
|
1418 |
|
135
|
1419 |
compression := 1. "none"
|
|
1420 |
fillOrder := #msb.
|
|
1421 |
planarConfiguration := 1.
|
|
1422 |
photometric := nil.
|
|
1423 |
bitsPerSample := 1.
|
|
1424 |
samplesPerPixel := 1.
|
|
1425 |
width := nil.
|
|
1426 |
height := nil.
|
|
1427 |
stripOffsets := nil.
|
|
1428 |
rowsPerStrip := nil.
|
|
1429 |
"resolutionUnit := 2."
|
|
1430 |
predictor := 1.
|
0
|
1431 |
|
135
|
1432 |
offset := aStream nextLongMSB:msb.
|
|
1433 |
aStream position:offset + 1.
|
|
1434 |
|
|
1435 |
numberOfTags := self readShort.
|
|
1436 |
1 to:numberOfTags do:[:index |
|
|
1437 |
tagType := self readShort.
|
|
1438 |
numberType := self readShort.
|
|
1439 |
length := aStream nextLongMSB:msb.
|
|
1440 |
self decodeTiffTag:tagType numberType:numberType length:length
|
|
1441 |
].
|
0
|
1442 |
|
135
|
1443 |
offset := aStream nextLongMSB:msb.
|
|
1444 |
(offset ~~ 0) ifTrue:[
|
|
1445 |
'TIFFReader: more tags ignored' errorPrintNL
|
|
1446 |
].
|
0
|
1447 |
|
135
|
1448 |
"check for required tags"
|
|
1449 |
ok := true.
|
|
1450 |
width isNil ifTrue:[
|
|
1451 |
'TIFFReader: missing width tag' errorPrintNL.
|
|
1452 |
ok := false
|
|
1453 |
].
|
0
|
1454 |
|
135
|
1455 |
height isNil ifTrue:[
|
|
1456 |
'TIFFReader: missing length tag' errorPrintNL.
|
|
1457 |
ok := false
|
|
1458 |
].
|
|
1459 |
|
|
1460 |
photometric isNil ifTrue:[
|
|
1461 |
'TIFFReader: missing photometric tag' errorPrintNL.
|
|
1462 |
ok := false
|
|
1463 |
].
|
|
1464 |
|
|
1465 |
stripOffsets isNil ifTrue:[
|
|
1466 |
'TIFFReader: missing stripOffsets tag' errorPrintNL.
|
|
1467 |
ok := false
|
0
|
1468 |
].
|
|
1469 |
|
|
1470 |
stripByteCounts isNil ifTrue:[
|
135
|
1471 |
stripOffsets size == 1 ifTrue:[
|
|
1472 |
stripByteCounts := Array with:(self bitsPerPixel // 8) * width * height
|
|
1473 |
]
|
|
1474 |
].
|
|
1475 |
|
|
1476 |
stripByteCounts isNil ifTrue:[
|
|
1477 |
'TIFFReader: missing stripByteCounts tag' errorPrintNL.
|
|
1478 |
ok := false
|
|
1479 |
].
|
|
1480 |
|
|
1481 |
ok ifFalse:[
|
28
|
1482 |
^ nil
|
0
|
1483 |
].
|
|
1484 |
|
135
|
1485 |
"given all the information, read the bits"
|
0
|
1486 |
|
135
|
1487 |
rowsPerStrip isNil ifTrue:[
|
|
1488 |
rowsPerStrip := height
|
0
|
1489 |
].
|
|
1490 |
|
135
|
1491 |
ok := false.
|
|
1492 |
(compression == 1) ifTrue:[
|
|
1493 |
result := self readUncompressedTiffImageData.
|
|
1494 |
ok := true
|
|
1495 |
].
|
|
1496 |
(compression == 2) ifTrue:[
|
|
1497 |
result := self readCCITT3RLETiffImageData.
|
|
1498 |
ok := true
|
|
1499 |
].
|
|
1500 |
(compression == 3) ifTrue:[
|
|
1501 |
result := self readCCITTGroup3TiffImageData.
|
|
1502 |
ok := true
|
|
1503 |
].
|
|
1504 |
(compression == 4) ifTrue:[
|
|
1505 |
result := self readCCITTGroup4TiffImageData.
|
|
1506 |
ok := true
|
|
1507 |
].
|
|
1508 |
(compression == 5) ifTrue:[
|
|
1509 |
result := self readLZWTiffImageData.
|
|
1510 |
ok := true
|
|
1511 |
].
|
|
1512 |
(compression == 6) ifTrue:[
|
|
1513 |
result := self readJPEGTiffImageData.
|
|
1514 |
ok := true
|
|
1515 |
].
|
|
1516 |
(compression == 32766) ifTrue:[
|
|
1517 |
result := self readNeXTRLE2TiffImageData.
|
|
1518 |
ok := true
|
|
1519 |
].
|
|
1520 |
(compression == 32771) ifTrue:[
|
|
1521 |
result := self readCCITTRLEWTiffImageData.
|
|
1522 |
ok := true
|
|
1523 |
].
|
|
1524 |
(compression == 32773) ifTrue:[
|
|
1525 |
result := self readPackbitsTiffImageData.
|
|
1526 |
ok := true
|
|
1527 |
].
|
|
1528 |
(compression == 32865) ifTrue:[
|
|
1529 |
result := self readNeXTJPEGTiffImageData.
|
|
1530 |
ok := true
|
|
1531 |
].
|
|
1532 |
ok ifFalse:[
|
|
1533 |
'TIFFReader: compression type ' errorPrint. compression errorPrint.
|
|
1534 |
' not known' errorPrintNL
|
|
1535 |
].
|
|
1536 |
^ result
|
|
1537 |
! !
|
0
|
1538 |
|
135
|
1539 |
!TIFFReader methodsFor:'writing to file'!
|
|
1540 |
|
|
1541 |
save:image onFile:aFileName
|
|
1542 |
"save image as (uncompressed) TIFF file on aFileName"
|
0
|
1543 |
|
135
|
1544 |
|pos1 pos|
|
0
|
1545 |
|
135
|
1546 |
outStream := FileStream newFileNamed:aFileName.
|
|
1547 |
outStream isNil ifTrue:[
|
|
1548 |
'TIFFReader: create error' errorPrintNL.
|
28
|
1549 |
^ nil
|
0
|
1550 |
].
|
|
1551 |
|
135
|
1552 |
"save as msb"
|
|
1553 |
|
|
1554 |
byteOrder := #msb.
|
|
1555 |
"
|
|
1556 |
byteOrder := #lsb.
|
|
1557 |
"
|
|
1558 |
fillOrder := #msb.
|
|
1559 |
width := image width.
|
|
1560 |
height := image height.
|
|
1561 |
photometric := image photometric.
|
|
1562 |
samplesPerPixel := image samplesPerPixel.
|
|
1563 |
bitsPerSample := image bitsPerSample.
|
|
1564 |
colorMap := image colorMap.
|
|
1565 |
planarConfiguration := 1.
|
|
1566 |
compression := 1. "none"
|
|
1567 |
data := image bits.
|
|
1568 |
|
|
1569 |
currentOffset := 0.
|
|
1570 |
|
|
1571 |
(byteOrder == #msb) ifTrue:[
|
|
1572 |
outStream nextPut:$M.
|
|
1573 |
outStream nextPut:$M.
|
|
1574 |
] ifFalse:[
|
|
1575 |
outStream nextPut:$I.
|
|
1576 |
outStream nextPut:$I.
|
0
|
1577 |
].
|
135
|
1578 |
currentOffset := currentOffset + 2.
|
0
|
1579 |
|
135
|
1580 |
outStream binary.
|
|
1581 |
|
|
1582 |
self writeShort:42.
|
|
1583 |
currentOffset := currentOffset + 2.
|
0
|
1584 |
|
135
|
1585 |
pos1 := outStream position.
|
|
1586 |
self writeLong:0. "start of tags - filled in later"
|
|
1587 |
currentOffset := currentOffset + 4.
|
|
1588 |
|
|
1589 |
"output strips"
|
|
1590 |
|
|
1591 |
self writeUncompressedBits. "this outputs bits as strips, sets stripOffsets and stripByteCounts"
|
|
1592 |
self writeStripOffsets. "this outputs strip offsets, sets stripOffsetsPos"
|
|
1593 |
self writeStripByteCounts. "this outputs strip bytecounts, sets stripByteCountPos"
|
|
1594 |
self writeBitsPerSample. "this outputs bitsPerSample, sets bitsPerSamplePos"
|
|
1595 |
photometric == #palette ifTrue:[
|
|
1596 |
self writeColorMap "this outputs colorMap, sets colorMapPos"
|
0
|
1597 |
].
|
|
1598 |
|
135
|
1599 |
pos := outStream position. "backpatch tag offset"
|
|
1600 |
outStream position:pos1.
|
|
1601 |
self writeLong:(pos - 1). "fill in tag offset"
|
|
1602 |
outStream position:pos.
|
|
1603 |
"
|
|
1604 |
('patch tag offset at: ', (pos1 printStringRadix:16) , ' to ',
|
|
1605 |
(pos printStringRadix:16)) printNewline.
|
|
1606 |
"
|
|
1607 |
"output tag data"
|
0
|
1608 |
|
135
|
1609 |
photometric == #palette ifTrue:[
|
|
1610 |
self writeShort:10. "10 tags"
|
|
1611 |
] ifFalse:[
|
|
1612 |
self writeShort:9. "9 tags"
|
|
1613 |
].
|
|
1614 |
self writeTag:256. "image width"
|
|
1615 |
self writeTag:257. "image height"
|
|
1616 |
self writeTag:258. "bits per sample"
|
|
1617 |
self writeTag:259. "compression"
|
|
1618 |
self writeTag:262. "photometric"
|
|
1619 |
self writeTag:273. "strip offsets"
|
|
1620 |
self writeTag:278. "rowsPerStrip"
|
|
1621 |
self writeTag:279. "strip byte counts"
|
|
1622 |
self writeTag:284. "planarconfig"
|
|
1623 |
photometric == #palette ifTrue:[
|
|
1624 |
self writeTag:320 "colorMap"
|
|
1625 |
].
|
|
1626 |
self writeLong:0. "end of tags mark"
|
|
1627 |
outStream close
|
|
1628 |
! !
|
30
|
1629 |
|
135
|
1630 |
!TIFFReader class methodsFor:'documentation'!
|
30
|
1631 |
|
135
|
1632 |
version
|
|
1633 |
^ '$Header: /cvs/stx/stx/libview2/TIFFReader.st,v 1.27 1995-12-07 11:38:46 cg Exp $'
|
0
|
1634 |
! !
|
135
|
1635 |
TIFFReader initialize!
|