TIFFRdr.st
changeset 192 947cc10f86dc
parent 159 327da5085900
child 195 7975a2c4a890
equal deleted inserted replaced
191:cb2815b77100 192:947cc10f86dc
     9  other person.  No title to or ownership of the software is
     9  other person.  No title to or ownership of the software is
    10  hereby transferred.
    10  hereby transferred.
    11 "
    11 "
    12 
    12 
    13 ImageReader subclass:#TIFFReader
    13 ImageReader subclass:#TIFFReader
    14 	 instanceVariableNames:'planarConfiguration subFileType stripOffsets rowsPerStrip
    14 	instanceVariableNames:'planarConfiguration subFileType stripOffsets rowsPerStrip
    15 		fillOrder compression group3options predictor stripByteCounts
    15 		fillOrder compression group3options predictor stripByteCounts
    16 		currentOffset stripOffsetsPos stripByteCountsPos bitsPerSamplePos
    16 		currentOffset stripOffsetsPos stripByteCountsPos bitsPerSamplePos
    17 		colorMapPos'
    17 		colorMapPos'
    18 	 classVariableNames:''
    18 	classVariableNames:''
    19 	 poolDictionaries:''
    19 	poolDictionaries:''
    20 	 category:'Graphics-Images support'
    20 	category:'Graphics-Images support'
    21 !
    21 !
    22 
    22 
    23 !TIFFReader class methodsFor:'documentation'!
    23 !TIFFReader class methodsFor:'documentation'!
    24 
    24 
    25 copyright
    25 copyright
  1375 ! !
  1375 ! !
  1376 
  1376 
  1377 !TIFFReader methodsFor:'reading from file'!
  1377 !TIFFReader methodsFor:'reading from file'!
  1378 
  1378 
  1379 fromStream:aStream
  1379 fromStream:aStream
  1380     "read an image from aStream"
  1380     "read a stream containing a TIFF image.
       
  1381      Leave image description in instance variables."
  1381 
  1382 
  1382     |char1 char2 version 
  1383     |char1 char2 version 
  1383      numberOfTags "{ Class: SmallInteger }"
  1384      numberOfTags "{ Class: SmallInteger }"
  1384      tagType      "{ Class: SmallInteger }"
  1385      tagType      "{ Class: SmallInteger }"
  1385      numberType   "{ Class: SmallInteger }"
  1386      numberType   "{ Class: SmallInteger }"
  1389     inStream := aStream.
  1390     inStream := aStream.
  1390 
  1391 
  1391     char1 := aStream next.
  1392     char1 := aStream next.
  1392     char2 := aStream next.
  1393     char2 := aStream next.
  1393     (char1 ~~ char2) ifTrue:[
  1394     (char1 ~~ char2) ifTrue:[
  1394 	'TIFFReader: not a tiff file' errorPrintNL.
  1395         'TIFFReader: not a tiff file' errorPrintNL.
  1395 	^ nil
  1396         ^ nil
  1396     ].
  1397     ].
  1397     (char1 == $I) ifTrue:[
  1398     (char1 == $I) ifTrue:[
  1398 	byteOrder := #lsb.
  1399         byteOrder := #lsb.
  1399 	msb := false.
  1400         msb := false.
  1400     ] ifFalse:[
  1401     ] ifFalse:[
  1401 	(char1 == $M) ifTrue:[
  1402         (char1 == $M) ifTrue:[
  1402 	    byteOrder := #msb.
  1403             byteOrder := #msb.
  1403 	    msb := true.
  1404             msb := true.
  1404 	] ifFalse:[
  1405         ] ifFalse:[
  1405 	    'TIFFReader: not a tiff file' errorPrintNL.
  1406             'TIFFReader: not a tiff file' errorPrintNL.
  1406 	    ^ nil
  1407             ^ nil
  1407 	]
  1408         ]
  1408     ].
  1409     ].
  1409 
  1410 
  1410     aStream binary.
  1411     aStream binary.
  1411 
  1412 
  1412     version := self readShort.
  1413     version := self readShort.
  1413     (version ~~ 42) ifTrue:[
  1414     (version ~~ 42) ifTrue:[
  1414 	'TIFFReader: version of tiff-file not supported' errorPrintNL.
  1415         'TIFFReader: version of tiff-file not supported' errorPrintNL.
  1415 	^ nil
  1416         ^ nil
  1416     ].
  1417     ].
  1417 
  1418 
  1418     "setup default values"
  1419     "setup default values"
  1419 
  1420 
  1420     compression := 1. "none"
  1421     compression := 1. "none"
  1433     offset := aStream nextLongMSB:msb.
  1434     offset := aStream nextLongMSB:msb.
  1434     aStream position:offset + 1.
  1435     aStream position:offset + 1.
  1435 
  1436 
  1436     numberOfTags := self readShort.
  1437     numberOfTags := self readShort.
  1437     1 to:numberOfTags do:[:index |
  1438     1 to:numberOfTags do:[:index |
  1438 	tagType := self readShort.
  1439         tagType := self readShort.
  1439 	numberType := self readShort.
  1440         numberType := self readShort.
  1440 	length := aStream nextLongMSB:msb.
  1441         length := aStream nextLongMSB:msb.
  1441 	self decodeTiffTag:tagType numberType:numberType length:length
  1442         self decodeTiffTag:tagType numberType:numberType length:length
  1442     ].
  1443     ].
  1443 
  1444 
  1444     offset := aStream nextLongMSB:msb.
  1445     offset := aStream nextLongMSB:msb.
  1445     (offset ~~ 0) ifTrue:[
  1446     (offset ~~ 0) ifTrue:[
  1446 	'TIFFReader: more tags ignored' errorPrintNL
  1447         'TIFFReader: more tags ignored' errorPrintNL
  1447     ].
  1448     ].
  1448 
  1449 
  1449     "check for required tags"
  1450     "check for required tags"
  1450     ok := true.
  1451     ok := true.
  1451     width isNil ifTrue:[
  1452     width isNil ifTrue:[
  1452 	'TIFFReader: missing width tag' errorPrintNL.
  1453         'TIFFReader: missing width tag' errorPrintNL.
  1453 	ok := false
  1454         ok := false
  1454     ].
  1455     ].
  1455 
  1456 
  1456     height isNil ifTrue:[
  1457     height isNil ifTrue:[
  1457 	'TIFFReader: missing length tag' errorPrintNL.
  1458         'TIFFReader: missing length tag' errorPrintNL.
  1458 	ok := false
  1459         ok := false
  1459     ].
  1460     ].
  1460 
  1461 
  1461     photometric isNil ifTrue:[
  1462     photometric isNil ifTrue:[
  1462 	'TIFFReader: missing photometric tag' errorPrintNL.
  1463         'TIFFReader: missing photometric tag' errorPrintNL.
  1463 	ok := false
  1464         ok := false
  1464     ].
  1465     ].
  1465 
  1466 
  1466     stripOffsets isNil ifTrue:[
  1467     stripOffsets isNil ifTrue:[
  1467 	'TIFFReader: missing stripOffsets tag' errorPrintNL.
  1468         'TIFFReader: missing stripOffsets tag' errorPrintNL.
  1468 	ok := false
  1469         ok := false
  1469     ].
  1470     ].
  1470 
  1471 
  1471     stripByteCounts isNil ifTrue:[
  1472     stripByteCounts isNil ifTrue:[
  1472 	stripOffsets size == 1 ifTrue:[
  1473         stripOffsets size == 1 ifTrue:[
  1473 	    stripByteCounts := Array with:(self bitsPerPixel // 8) * width * height
  1474             stripByteCounts := Array with:(self bitsPerPixel // 8) * width * height
  1474 	]
  1475         ]
  1475     ].
  1476     ].
  1476 
  1477 
  1477     stripByteCounts isNil ifTrue:[
  1478     stripByteCounts isNil ifTrue:[
  1478 	'TIFFReader: missing stripByteCounts tag' errorPrintNL.
  1479         'TIFFReader: missing stripByteCounts tag' errorPrintNL.
  1479 	ok := false
  1480         ok := false
  1480     ].
  1481     ].
  1481 
  1482 
  1482     ok ifFalse:[
  1483     ok ifFalse:[
  1483 	^ nil
  1484         ^ nil
  1484     ].
  1485     ].
  1485 
  1486 
  1486     "given all the information, read the bits"
  1487     "given all the information, read the bits"
  1487 
  1488 
  1488     rowsPerStrip isNil ifTrue:[
  1489     rowsPerStrip isNil ifTrue:[
  1489 	rowsPerStrip := height
  1490         rowsPerStrip := height
  1490     ].
  1491     ].
  1491 
  1492 
  1492     ok := false.
  1493     ok := false.
  1493     (compression == 1) ifTrue:[
  1494     (compression == 1) ifTrue:[
  1494 	result := self readUncompressedTiffImageData.
  1495         result := self readUncompressedTiffImageData.
  1495 	ok := true
  1496         ok := true
  1496     ].
  1497     ].
  1497     (compression == 2) ifTrue:[
  1498     (compression == 2) ifTrue:[
  1498 	result := self readCCITT3RLETiffImageData.
  1499         result := self readCCITT3RLETiffImageData.
  1499 	ok := true
  1500         ok := true
  1500     ].
  1501     ].
  1501     (compression == 3) ifTrue:[
  1502     (compression == 3) ifTrue:[
  1502 	result := self readCCITTGroup3TiffImageData.
  1503         result := self readCCITTGroup3TiffImageData.
  1503 	ok := true
  1504         ok := true
  1504     ]. 
  1505     ]. 
  1505     (compression == 4) ifTrue:[
  1506     (compression == 4) ifTrue:[
  1506 	result := self readCCITTGroup4TiffImageData.
  1507         result := self readCCITTGroup4TiffImageData.
  1507 	ok := true
  1508         ok := true
  1508     ]. 
  1509     ]. 
  1509     (compression == 5) ifTrue:[
  1510     (compression == 5) ifTrue:[
  1510 	result := self readLZWTiffImageData.
  1511         result := self readLZWTiffImageData.
  1511 	ok := true
  1512         ok := true
  1512     ].
  1513     ].
  1513     (compression == 6) ifTrue:[
  1514     (compression == 6) ifTrue:[
  1514 	result := self readJPEGTiffImageData.
  1515         result := self readJPEGTiffImageData.
  1515 	ok := true
  1516         ok := true
  1516     ].
  1517     ].
  1517     (compression == 32766) ifTrue:[
  1518     (compression == 32766) ifTrue:[
  1518 	result := self readNeXTRLE2TiffImageData.
  1519         result := self readNeXTRLE2TiffImageData.
  1519 	ok := true
  1520         ok := true
  1520     ].
  1521     ].
  1521     (compression == 32771) ifTrue:[
  1522     (compression == 32771) ifTrue:[
  1522 	result := self readCCITTRLEWTiffImageData.
  1523         result := self readCCITTRLEWTiffImageData.
  1523 	ok := true
  1524         ok := true
  1524     ].
  1525     ].
  1525     (compression == 32773) ifTrue:[
  1526     (compression == 32773) ifTrue:[
  1526 	result := self readPackbitsTiffImageData.
  1527         result := self readPackbitsTiffImageData.
  1527 	ok := true
  1528         ok := true
  1528     ].
  1529     ].
  1529     (compression == 32865) ifTrue:[
  1530     (compression == 32865) ifTrue:[
  1530 	result := self readNeXTJPEGTiffImageData.
  1531         result := self readNeXTJPEGTiffImageData.
  1531 	ok := true
  1532         ok := true
  1532     ].
  1533     ].
  1533     ok ifFalse:[
  1534     ok ifFalse:[
  1534 	'TIFFReader: compression type ' errorPrint. compression errorPrint.
  1535         'TIFFReader: compression type ' errorPrint. compression errorPrint.
  1535 	' not known' errorPrintNL
  1536         ' not known' errorPrintNL
  1536     ].
  1537     ].
  1537     ^ result
  1538     ^ result
       
  1539 
       
  1540     "Modified: 22.4.1996 / 19:12:12 / cg"
  1538 ! !
  1541 ! !
  1539 
  1542 
  1540 !TIFFReader methodsFor:'writing to file'!
  1543 !TIFFReader methodsFor:'writing to file'!
  1541 
  1544 
  1542 save:image onFile:aFileName
  1545 save:image onFile:aFileName
  1628 ! !
  1631 ! !
  1629 
  1632 
  1630 !TIFFReader class methodsFor:'documentation'!
  1633 !TIFFReader class methodsFor:'documentation'!
  1631 
  1634 
  1632 version
  1635 version
  1633     ^ '$Header: /cvs/stx/stx/libview2/Attic/TIFFRdr.st,v 1.28 1996-02-04 15:35:54 cg Exp $'
  1636     ^ '$Header: /cvs/stx/stx/libview2/Attic/TIFFRdr.st,v 1.29 1996-04-22 17:38:48 cg Exp $'
  1634 ! !
  1637 ! !
  1635 TIFFReader initialize!
  1638 TIFFReader initialize!