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