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