eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'TIFFReader':

Home

Documentation
www.exept.de
Everywhere
for:
[back]

Class: TIFFReader


Inheritance:

   Object
   |
   +--ImageReader
      |
      +--TIFFReader

Package:
stx:libview2
Category:
Graphics-Images-Readers
Version:
rev: 1.106 date: 2017/09/18 07:09:00
user: cg
file: TIFFReader.st directory: libview2
module: stx stc-classLibrary: libview2
Author:
Claus Gittinger

Description:


This class knows how to read TIFF files and how to write uncompressed TIFF files.

Implemented & Missing Features:

  - Only single image files are supported.

  - Not all formats are implemented, and of those that are, not all are tested.
    It should read with most rgb, palette, mono and greyscale images, 
    although the alpha channel is currently not supported and ignored.
    It supports reading of uncompressed, LZW, packbits and CCITT-G3 compressed images
    JPEG and many other formats are currently not implemented.

  - Only writing of uncompressed images is currently implemented.
    It should write (at least) mono, 8-bit palette and 24 bit rgb formats.

  - bigTiff is supported
  
  - some dng tags are supported

More formats will come... (will they ever be needed?)

TODO (?): 
    since I don't want to spend all of my life adding more formats here and
    reinventing the wheel, this code should be changed to use the tiff library.
    That would give us most formats and also writing capabilities for free.

    Late note: 
        I hate C and interfacing to C libraries: it almost always leads to trouble
        w.r.t. memory leaks, non-reentrancy, non-interruptability etc.
        (we recently fixed a malloc-non-reentrant bug for some architecture...)
        So its probably better to do it all in a real programming language ;-)


Related information:

    Image
    Form
    Icon
    BlitImageReader
    FaceReader
    GIFReader
    JPEGReader
    PBMReader
    PCXReader
    ST80FormReader
    SunRasterReader
    TargaReader
    WindowsIconReader
    XBMReader
    XPMReader
    XWDReader

Class protocol:

initialization
o  initialize
install myself in the Image classes fileFormat table
for the `.tiff' and `.tif' extensions.
usage example(s):
     self initialize

testing
o  canRepresent: anImage
return true, if anImage can be represented in my file format.
Any image is supported.

o  isValidImageFile: aFileName
return true, if aFileName contains a TIFF image


Instance protocol:

accessing
o  decodeMetaTags: aBoolean
by default, extra meta tags (photoshop) are not decoded;
set to true to get them in metatags

o  loadFullResolutionImage: aBoolean
by default, the full-resolution dng image is not decoded;
set to true to get it in the imageSequence

private-data reading
o  readAdobeDeflateTiffImageData

o  readCCITT3RLETiffImageData

o  readCCITT3RLEWTiffImageData

o  readCCITTGroup3TiffImageData
not really tested - all I got is a single fax from NeXT step

o  readCCITTGroup4TiffImageData

o  readCCITTRLEWTiffImageData

o  readDCSTiffImageData

o  readDeflateTiffImageData
|bytesPerRowIn bytesPerRow nPlanes overAllBytes

o  readImageDataUsingDecompressor: decompressorBlock
}

o  readJBIGTiffImageData

o  readJPEGTiffImageData
single strip

o  readLZWTiffImageData
"read LZW compressed tiff data;

o  readNeXTJPEGTiffImageData

o  readNeXTRLE2TiffImageData

o  readNewJPEGTiffImageData

o  readPackbitsTiffImageData
"this has only been tested with monochrome images"

o  readPixarFilmTiffImageData

o  readPixarLogTiffImageData

o  readSGI24TiffImageData

o  readSGI32TiffImageData

o  readThunderScanTiffImageData
}

o  readTiffImageData
1

o  readTiledJPEGTiffImageData

o  readTiledLZWTiffImageData

o  readTiledNewJPEGTiffImageData

o  readTiledTiffImageData
1

o  readTiledUncompressedTiffImageData
}

o  readUncompressedTiffImageData
|bytesPerRow "{ Class: SmallInteger }"

private-reading
o  decodePhotoshopImageResourceBlock: bytes
8BIM is the signature for Photoshop Image Resource Block (IRB).
See http://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#50577409_38034.

This kind of information could be found in images such as TIFF, JPEG, Photoshop native image format etc.
It could also be found in non-image documents such as in PDF.
The structure of the IRB is as follows:
Each IRB block starts with 4 bytes signature which translates to string '8BIM.'
After that, is a 2 bytes unique identifier denoting the kind of resource for this IRB.
For example:
0x040c for thumbnail;
0x041a for slices;
0x0408 for grid information;
0x040f for ICC Profile etc.

After the identifier is a variable length string for name.
The first byte of the string tells the length of the string (excluding the first length byte).
After the first byte comes the string itself.
There is a requirement that the length of the whole string (including the length byte) should be even.
Otherwise, pad one more byte after the string.

The next 4 bytes specifies the size of the actual data for this resource block followed by the data with the specified length.
The total length of the data also should be an even number. So if the size of the data is odd, pad another one byte.
This finishes a whole 8BIM.

There could be more than one IRBs but they all conform to the same structure as described above.
How to interpret the data depends on the unique identifier.

Now let's see how the IRBs are include in images.
For a JPEG image, metadata could be present as one of the application (APPn) segment.
Since different application could use the same APPn segment to store it's own metadata,
there must be some kind of identifier to let the image reader know what kind of information is contained inside the APPn.
Photoshop uses APP13 as it's IRB container and the APP13 contains 'Photoshop 3.0' as it's identifier.

For TIFF image which is tag based and arranged in a directory structure.
There is a private tag 16r8649 called 'PHOTOSHOP' to insert IRB information.

0x03E8 (Obsolete--Photoshop 2.0 only ) Contains five 2-byte values: number of channels, rows, columns, depth, and mode
0x03E9 Macintosh print manager print info record
0x03EA Macintosh page format information. No longer read by Photoshop. (Obsolete)
0x03EB Obsolete--Photoshop 2.0 only ) Indexed color table
0x03ED ResolutionInfo structure. See Appendix A in Photoshop API Guide.pdf.
0x03EE Names of the alpha channels as a series of Pascal strings.
0x03EF (Obsolete) See ID 1077DisplayInfo structure. See Appendix A in Photoshop API Guide.pdf.
0x03F0 The caption as a Pascal string.
0x03F1 Border information. Contains a fixed number (2 bytes real, 2 bytes fraction) for the border width, and 2 bytes for border units (1 = inches, 2 = cm, 3 = points, 4 = picas, 5 = columns).
0x03F2 Background color. See See Color structure.
0x03F3 Print flags. A series of one-byte boolean values (see Page Setup dialog): labels, crop marks, color bars, registration marks, negative, flip, interpolate, caption, print flags.
0x03F4 Grayscale and multichannel halftoning information
0x03F5 Color halftoning information
0x03F6 Duotone halftoning information
0x03F7 Grayscale and multichannel transfer function
0x03F8 Color transfer functions
0x03F9 Duotone transfer functions
0x03FA Duotone image information
0x03FB Two bytes for the effective black and white values for the dot range
0x03FC (Obsolete)
0x03FD EPS options
0x03FE Quick Mask information. 2 bytes containing Quick Mask channel ID; 1- byte boolean indicating whether the mask was initially empty.
0x03FF (Obsolete)
0x0400 Layer state information. 2 bytes containing the index of target layer (0 = bottom layer).
0x0401 Working path (not saved). See See Path resource format.
0x0402 Layers group information. 2 bytes per layer containing a group ID for the dragging groups. Layers in a group have the same group ID.
0x0403 (Obsolete)
0x0404 IPTC-NAA record. Contains the File Info... information. See the documentation in the IPTC folder of the Documentation folder.
0x0405 Image mode for raw format files
0x0406 JPEG quality. Private.
0x0408 (Photoshop 4.0) Grid and guides information. See See Grid and guides resource format.
0x0409 (Photoshop 4.0) Thumbnail resource for Photoshop 4.0 only. See See Thumbnail resource format.
0x040A (Photoshop 4.0) Copyright flag. Boolean indicating whether image is copyrighted. Can be set via Property suite or by user in File Info...
0x040B (Photoshop 4.0) URL. Handle of a text string with uniform resource locator. Can be set via Property suite or by user in File Info...
0x040C (Photoshop 5.0) Thumbnail resource (supersedes resource 1033). See See Thumbnail resource format.
0x040D (Photoshop 5.0) Global Angle. 4 bytes that contain an integer between 0 and 359, which is the global lighting angle for effects layer. If not present, assumed to be 30.
0x040E (Obsolete) See ID 1073 below. (Photoshop 5.0) Color samplers resource. See See Color samplers resource format.
0x040F (Photoshop 5.0) ICC Profile. The raw bytes of an ICC (International Color Consortium) format profile. See ICC1v42_2006-05.pdf in the Documentation folder and icProfileHeader.h in Sample Code\Common\Includes .
0x0410 (Photoshop 5.0) Watermark. One byte.
0x0411 (Photoshop 5.0) ICC Untagged Profile. 1 byte that disables any assumed profile handling when opening the file. 1 = intentionally untagged.
0x0412 (Photoshop 5.0) Effects visible. 1-byte global flag to show/hide all the effects layer. Only present when they are hidden.
0x0413 (Photoshop 5.0) Spot Halftone. 4 bytes for version, 4 bytes for length, and the variable length data.
0x0414 (Photoshop 5.0) Document-specific IDs seed number. 4 bytes: Base value, starting at which layer IDs will be generated (or a greater value if existing IDs already exceed it). Its purpose is to avoid the case where we add layers, flatten, save, open, and then add more layers that end up with the same IDs as the first set.
0x0415 (Photoshop 5.0) Unicode Alpha Names. Unicode string
0x0416 (Photoshop 6.0) Indexed Color Table Count. 2 bytes for the number of colors in table that are actually defined
0x0417 (Photoshop 6.0) Transparency Index. 2 bytes for the index of transparent color, if any.
0x0419 (Photoshop 6.0) Global Altitude. 4 byte entry for altitude
0x041A (Photoshop 6.0) Slices. See See Slices resource format.
0x041B (Photoshop 6.0) Workflow URL. Unicode string
0x041C (Photoshop 6.0) Jump To XPEP. 2 bytes major version, 2 bytes minor version, 4 bytes count. Following is repeated for count: 4 bytes block size, 4 bytes key, if key = 'jtDd' , then next is a Boolean for the dirty flag; otherwise it's a 4 byte entry for the mod date.
0x041D (Photoshop 6.0) Alpha Identifiers. 4 bytes of length, followed by 4 bytes each for every alpha identifier.
0x041E (Photoshop 6.0) URL List. 4 byte count of URLs, followed by 4 byte long, 4 byte ID, and Unicode string for each count.
0x0421 (Photoshop 6.0) Version Info. 4 bytes version, 1 byte hasRealMergedData , Unicode string: writer name, Unicode string: reader name, 4 bytes file version.
0x0422 (Photoshop 7.0) EXIF data 1. See http://www.kodak.com/global/plugins/acrobat/en/service/digCam/exifStandard2.pdf
0x0423 (Photoshop 7.0) EXIF data 3. See http://www.kodak.com/global/plugins/acrobat/en/service/digCam/exifStandard2.pdf
0x0424 (Photoshop 7.0) XMP metadata. File info as XML description. See http://www.adobe.com/devnet/xmp/
0x0425 (Photoshop 7.0) Caption digest. 16 bytes: RSA Data Security, MD5 message-digest algorithm
0x0426 (Photoshop 7.0) Print scale. 2 bytes style (0 = centered, 1 = size to fit, 2 = user defined). 4 bytes x location (floating point). 4 bytes y location (floating point). 4 bytes scale (floating point)
0x0428 (Photoshop CS) Pixel Aspect Ratio. 4 bytes (version = 1 or 2), 8 bytes double, x / y of a pixel. Version 2, attempting to correct values for NTSC and PAL, previously off by a factor of approx. 5%.
0x0429 (Photoshop CS) Layer Comps. 4 bytes (descriptor version = 16), Descriptor (see See Descriptor structure)
0x042A (Photoshop CS) Alternate Duotone Colors. 2 bytes (version = 1), 2 bytes count, following is repeated for each count: [ Color: 2 bytes for space followed by 4 * 2 byte color component ], following this is another 2 byte count, usually 256, followed by Lab colors one byte each for L, a, b. This resource is not read or used by Photoshop.
0x042B (Photoshop CS)Alternate Spot Colors. 2 bytes (version = 1), 2 bytes channel count, following is repeated for each count: 4 bytes channel ID, Color: 2 bytes for space followed by 4 * 2 byte color component. This resource is not read or used by Photoshop.
0x042D (Photoshop CS2) Layer Selection ID(s). 2 bytes count, following is repeated for each count: 4 bytes layer ID
0x042E (Photoshop CS2) HDR Toning information
0x042F (Photoshop CS2) Print info
0x0430 (Photoshop CS2) Layer Group(s) Enabled ID. 1 byte for each layer in the document, repeated by length of the resource. NOTE: Layer groups have start and end markers
0x0431 (Photoshop CS3) Color samplers resource. Also see ID 1038 for old format. See See Color samplers resource format.
0x0432 (Photoshop CS3) Measurement Scale. 4 bytes (descriptor version = 16), Descriptor (see See Descriptor structure)
0x0433 (Photoshop CS3) Timeline Information. 4 bytes (descriptor version = 16), Descriptor (see See Descriptor structure)
0x0434 (Photoshop CS3) Sheet Disclosure. 4 bytes (descriptor version = 16), Descriptor (see See Descriptor structure)
0x0435 (Photoshop CS3) DisplayInfo structure to support floating point clors. Also see ID 1007. See Appendix A in Photoshop API Guide.pdf .
0x0436 (Photoshop CS3) Onion Skins. 4 bytes (descriptor version = 16), Descriptor (see See Descriptor structure)
0x0438 (Photoshop CS4) Count Information. 4 bytes (descriptor version = 16), Descriptor (see See Descriptor structure) Information about the count in the document. See the Count Tool.
0x043A (Photoshop CS5) Print Information. 4 bytes (descriptor version = 16), Descriptor (see See Descriptor structure) Information about the current print settings in the document. The color management options.
0x043B (Photoshop CS5) Print Style. 4 bytes (descriptor version = 16), Descriptor (see See Descriptor structure) Information about the current print style in the document. The printing marks, labels, ornaments, etc.
0x043C (Photoshop CS5) Macintosh NSPrintInfo. Variable OS specific info for Macintosh. NSPrintInfo. It is recommened that you do not interpret or use this data.
0x043D (Photoshop CS5) Windows DEVMODE. Variable OS specific info for Windows. DEVMODE. It is recommened that you do not interpret or use this data.
0x043E (Photoshop CS6) Auto Save File Path. Unicode string. It is recommened that you do not interpret or use this data.
0x043F (Photoshop CS6) Auto Save Format. Unicode string. It is recommened that you do not interpret or use this data.
0x0440 (Photoshop CC) Path Selection State. 4 bytes (descriptor version = 16), Descriptor (see See Descriptor structure) Information about the current path selection state.
0x07D0-0x0BB6 Path Information (saved paths). See See Path resource format.
0x0BB7 Name of clipping path. See See Path resource format.
0x0BB8 (Photoshop CC) Origin Path Info. 4 bytes (descriptor version = 16), Descriptor (see See Descriptor structure) Information about the origin path data.
0x0FA0-0x1387 Plug-In resource(s). Resources added by a plug-in. See the plug-in API found in the SDK documentation
0x1B58 Image Ready variables. XML representation of variables definition
0x1B59 Image Ready data sets
0x1B5A Image Ready default selected state
0x1B5B Image Ready 7 rollover expanded state
0x1B5C Image Ready rollover expanded state
0x1B5D Image Ready save layer settings
0x1B5E Image Ready version
0x1F40 (Photoshop CS3) Lightroom workflow, if present the document is in the middle of a Lightroom workflow.
0x2710 Print flags information. 2 bytes version ( = 1), 1 byte center crop marks, 1 byte ( = 0), 4 bytes bleed width value, 2 bytes bleed width scale.

o  decodeTiffTag: tagType numberType: numberType length: length
}

o  positionToStrip: stripNr

o  positionToTile: tileNr

o  readBytes: n signed: isSigned
read n 8bit signed or unsigned integers and return them in an array or byteArray

o  readChars: n
read n characters and return them in a string

o  readDoubles: nFloats
read nFloats IEEE 64bit doubles and return them in an array

o  readFloats: nFloats
read nFloats IEEE 32bit floats and return them in an array

o  readFracts: nFracts signed: isSigned
read nFracts fractions (2 32bit words) and return them in an array

o  readLong8s: nLongs signed: isSigned
read nLongs signed or unsigned long8 numbers (64bit) and return them in an array

o  readLongs: nLongs signed: isSigned
read nLongs signed or unsigned long numbers (32bit) and return them in an array

o  readShorts: nShorts signed: isSigned
read nShorts signed or unsigned short numbers (16bit) and return them in an array

o  readSingleTagFrom: aStream

o  readTagsFrom: aStream
EOF

private-writing
o  writeBitsPerSample
'bitsPerSample: ' print. bitsPerSample printNewline.
'store bitspersample at: ' print. outStream position printNewline.

o  writeColorMap
tiff map is 16 bit - scale from percent to 0..16rFFFF

o  writeStripByteCounts
'stripByteCounts: ' print. stripByteCounts printNewline.
'store stripbytecounts at: ' print. outStream position printNewline.

o  writeStripOffsets
'stripOffsets: ' print. stripOffsets printNewline.
'store stripoffsets at: ' print. outStream position printNewline.

o  writeTag: tagType

o  writeTiffTag: tagType
tiff class

o  writeUncompressedBits
write bits as one or multiple strips

reading
o  fromStream: aStream
read a stream containing a TIFF image.
Leave image description in instance variables.
(i.e. to get the image, ask with image).

writing
o  save: image onStream: aStream
save image as (uncompressed) TIFF file on aFileName


Private classes:

    TIFFMetaData


ST/X 7.1.0.0; WebServer 1.663 at exept.de:8081; Wed, 12 Dec 2018 09:28:44 GMT