This project has moved. For the latest updates, please go here.

Project Description

Saraff.Tiff.NET is the skillful component which represents a writer and a reader that provides fast, non-cached, forward-only access to TIFF data.

Writer

using(var _stream=File.Create("sample.tif")) {
    var _writer=TiffWriter.Create(_stream);

    var _handle=_writer.WriteHeader(); // Write Header

    for(int _page=0; _page<4; _page++) {
        var _strips=new TiffHandle[16];
        var _stripByteCounts=new uint[16];
        for(byte i=0; i<_strips.Length; i++) {
            var _buf=new byte[Program._imageWidth*(Program._imageHeight/_strips.Length)*3];
            
            // Generating data ...

            _strips[i]=_writer.WriteData(_buf); // Write Strip
            _stripByteCounts[i]=(uint)_buf.Length;
        }

        // Write Image File Directory
        _handle=_writer.WriteImageFileDirectory(_handle, new Collection<ITag> {
            Tag<uint>.Create(TiffTags.ImageWidth,Program._imageWidth),
            Tag<uint>.Create(TiffTags.ImageLength,Program._imageHeight),
            Tag<ushort>.Create(TiffTags.BitsPerSample,8,8,8),
            Tag<TiffCompression>.Create(TiffTags.Compression,TiffCompression.NONE),
            Tag<TiffPhotoMetric>.Create(TiffTags.PhotometricInterpretation,TiffPhotoMetric.RGB),
            Tag<TiffHandle>.Create(TiffTags.StripOffsets,_strips),
            Tag<ushort>.Create(TiffTags.SamplesPerPixel,3),
            Tag<uint>.Create(TiffTags.RowsPerStrip,Program._imageHeight/16),
            Tag<uint>.Create(TiffTags.StripByteCounts,_stripByteCounts),
            Tag<ulong>.Create(TiffTags.XResolution,(1UL<<32)|300UL),
            Tag<ulong>.Create(TiffTags.YResolution,(1UL<<32)|300UL),
            Tag<TiffResolutionUnit>.Create(TiffTags.ResolutionUnit,TiffResolutionUnit.INCH),
            Tag<char>.Create(TiffTags.Software,"SARAFF SOFTWARE".ToCharArray()),
            Tag<char>.Create(TiffTags.Copyright,"(c) SARAFF 2014".ToCharArray())
        });
    }
}

Example of the created a TIFF file
Download Image File

Reader

using(var _stream=File.Open("sample.tif",FileMode.Open)) {
    var _reader=TiffReader.Create(_stream);

    _reader.ReadHeader(); // Read Header

    // Read Image File Directories
    for(var _count=_reader.ReadImageFileDirectory(); _count!=0; _count=_reader.ReadImageFileDirectory()) {
        Console.WriteLine("ImageFileDirectory: {0} tags.",_count);
        var _dict=new Dictionary<TiffTags,Collection<object>>();

        // Read Tags
        for(ITag _tag=_reader.ReadTag(); _tag!=null; _tag=_reader.ReadTag()) {
            Console.Write("{0}: {{ ",_tag.TagId);
            _dict.Add(_tag.TagId,new Collection<object>());
            switch(_tag.TagId) {
                case TiffTags.StripOffsets:
                    // Read Values of Tag
                    for(object _value=_reader.ReadHandle(); _value!=null; _value=_reader.ReadHandle()) {
                        Console.Write("{0} ",_value);
                        _dict[_tag.TagId].Add(_value);
                    }
                    break;
                default:
                    // Read Values of Tag
                    for(object _value=_reader.ReadValue(); _value!=null; _value=_reader.ReadValue()) {
                        Console.Write("{0} ",(_value is ulong)?((float)((ulong)_value&0xffffffff)/(float)((ulong)_value>>32)):_value);
                        _dict[_tag.TagId].Add(_value);
                    }
                    break;
            }
            Console.WriteLine("}");
        }

        // Read Strips
        for(int i=0; i<_dict[TiffTags.StripOffsets].Count; i++) {
            Console.WriteLine("Strip {0}: ",i);
            var _data=_reader.ReadData((TiffHandle)_dict[TiffTags.StripOffsets][i],Convert.ToInt64(_dict[TiffTags.StripByteCounts][i]));
            for(int ii=0; ii<_data.Length; ii++) {
                // Show Data
            }
        }
    }
}

Example of the read a TIFF file
ImageFileDirectory: 14 tags.
ImageWidth: { 150 }
ImageLength: { 160 }
BitsPerSample: { 8 8 8 }
Compression: { NONE }
PhotometricInterpretation: { RGB }
StripOffsets: { 00000008 0000119C 00002330 000034C4 00004658 000057EC 00006980 00007B14 00008CA8 00009E3C 0000AFD0 0000C164 0000D2F8 0000E48C 0000F620 000107B4 }
SamplesPerPixel: { 3 }
RowsPerStrip: { 10 }
StripByteCounts: { 4500 4500 4500 4500 4500 4500 4500 4500 4500 4500 4500 4500 4500 4500 4500 4500 }
XResolution: { 300 }
YResolution: { 300 }
ResolutionUnit: { INCH }
Software: { S A R A F F S O F T W A R E }
Copyright: { ( c ) S A R A F F 2 0 1 4 }



Strip 0:

0000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
...

Strip 1:

0000: 11 00 00 11 00 00 11 00 00 11 00 00 11 00 00 11
0010: 00 00 11 00 00 11 00 00 11 00 00 11 00 00 11 00
...
Download File

Last edited May 27, 2015 at 4:44 PM by SARAFF, version 9