public unsafe void WriteStandardTiffHeader() { //IFH TiffImageFileHeader ifh = new TiffImageFileHeader(); ifh.ByteOrder = 0x4949; ifh.Marker = 0x002A; ifh.FirstIFD = 0x00000008; //第一个IFD,这里只一个 TiffImageFileDirectory ifd = new TiffImageFileDirectory(); ifd.DECount = 10; ifd.NextIFD = 0; int headerLength = sizeof(TiffImageFileHeader) + Marshal.SizeOf(ifd.DECount) + TIFF_DE_LENGTH * ifd.DECount + Marshal.SizeOf(ifd.NextIFD); //tiff文件头长度,不包含DE的数据部分,按1个IFD,10个DE计算 int headerDataLength = 6;//DE的数据部分长度, int headerDataOffset = headerLength;//DE的数据部分偏移量 this._ImageDataOffset = headerLength + headerDataLength; byte[] headerData = new byte[this._ImageDataOffset]; //IFH CopyData2Array((byte*)&ifh, sizeof(TiffImageFileHeader), headerData, 0); //IFD CopyData2Array((byte*)&(ifd.DECount), Marshal.SizeOf(ifd.DECount), headerData, sizeof(TiffImageFileHeader)); //DE #region 开始逐条添加DE int deLength = TIFF_DE_LENGTH; int deOffset = sizeof(TiffImageFileHeader) + Marshal.SizeOf(ifd.DECount); fixed(TiffDirectoryEntry* deArray = new TiffDirectoryEntry[ifd.DECount]) { int idx = 0; //图像宽 deArray[idx] = new TiffDirectoryEntry { TagID = (ushort)TiffDETag.ImageWidth, DataType = (ushort)TiffDEDataType.SHORT, Count = 1, DataOrPtr = this._width }; CopyData2Array((byte*)&(deArray[idx]), deLength, headerData, deOffset + idx * deLength); //图像高 idx = 1; deArray[idx] = new TiffDirectoryEntry { TagID = (ushort)TiffDETag.ImageLength, DataType = (ushort)TiffDEDataType.SHORT, Count = 1, DataOrPtr = this._height }; CopyData2Array((byte*)&(deArray[idx]), deLength, headerData, deOffset + idx * deLength); //颜色深度 idx = 2; deArray[idx] = new TiffDirectoryEntry { TagID = (ushort)TiffDETag.BitsPerSample, DataType = (ushort)TiffDEDataType.SHORT, Count = 3, DataOrPtr = (uint)headerDataOffset }; CopyData2Array((byte*)&(deArray[idx]), deLength, headerData, deOffset + idx * deLength); byte[] colorDepth = new byte[] { 0x08, 0x00, 0x08, 0x00, 0x08, 0x00 }; CopyData2Array(colorDepth, headerData, (int)headerDataOffset); headerDataOffset += colorDepth.Length; //图片压缩 idx = 3; deArray[idx] = new TiffDirectoryEntry { TagID = (ushort)TiffDETag.Compression, DataType = (ushort)TiffDEDataType.SHORT, Count = 1, DataOrPtr = 1 }; CopyData2Array((byte*)&(deArray[idx]), deLength, headerData, deOffset + idx * deLength); //颜色空间 idx = 4; deArray[idx] = new TiffDirectoryEntry { TagID = (ushort)TiffDETag.PhotometricInterpretation, DataType = (ushort)TiffDEDataType.SHORT, Count = 1, DataOrPtr = 2 }; CopyData2Array((byte*)&(deArray[idx]), deLength, headerData, deOffset + idx * deLength); //图像扫描线偏移量 idx = 5; deArray[idx] = new TiffDirectoryEntry { TagID = (ushort)TiffDETag.StripOffsets, DataType = (ushort)TiffDEDataType.LONG, Count = 1, DataOrPtr = (uint)headerData.Length }; CopyData2Array((byte*)&(deArray[idx]), deLength, headerData, deOffset + idx * deLength); //方向 idx = 6; deArray[idx] = new TiffDirectoryEntry { TagID = (ushort)TiffDETag.Orientation, DataType = (ushort)TiffDEDataType.SHORT, Count = 1, DataOrPtr = 1 }; CopyData2Array((byte*)&(deArray[idx]), deLength, headerData, deOffset + idx * deLength); //每像素样本 idx = 7; deArray[idx] = new TiffDirectoryEntry { TagID = (ushort)TiffDETag.SamplesPerPixel, DataType = (ushort)TiffDEDataType.SHORT, Count = 1, DataOrPtr = 3 }; CopyData2Array((byte*)&(deArray[idx]), deLength, headerData, deOffset + idx * deLength); //每块行数 idx = 8; deArray[idx] = new TiffDirectoryEntry { TagID = (ushort)TiffDETag.RowsPerStrip, DataType = (ushort)TiffDEDataType.LONG, Count = 1, DataOrPtr = this._height }; CopyData2Array((byte*)&(deArray[idx]), deLength, headerData, deOffset + idx * deLength); //每块的字节数 idx = 9; deArray[idx] = new TiffDirectoryEntry { TagID = (ushort)TiffDETag.StripByteCounts, DataType = (ushort)TiffDEDataType.LONG, Count = 1, DataOrPtr = (uint)this.ImageDataLength }; CopyData2Array((byte*)&(deArray[idx]), deLength, headerData, deOffset + idx * deLength); } #endregion _fs.Write(headerData, 0, headerData.Length); }
public unsafe void WriteStandardTiffHeader() { //IFH TiffImageFileHeader ifh = new TiffImageFileHeader(); ifh.ByteOrder = 0x4949; ifh.Marker = 0x002A; ifh.FirstIFD = 0x00000008; //第一个IFD,这里只一个 TiffImageFileDirectory ifd = new TiffImageFileDirectory(); ifd.DECount = 10; ifd.NextIFD = 0; int headerLength = sizeof(TiffImageFileHeader) + Marshal.SizeOf(ifd.DECount) + TIFF_DE_LENGTH * ifd.DECount + Marshal.SizeOf(ifd.NextIFD); //tiff文件头长度,不包含DE的数据部分,按1个IFD,10个DE计算 int headerDataLength = 6; //DE的数据部分长度, int headerDataOffset = headerLength; //DE的数据部分偏移量 this._ImageDataOffset = headerLength + headerDataLength; byte[] headerData = new byte[this._ImageDataOffset]; //IFH CopyData2Array((byte *)&ifh, sizeof(TiffImageFileHeader), headerData, 0); //IFD CopyData2Array((byte *)&(ifd.DECount), Marshal.SizeOf(ifd.DECount), headerData, sizeof(TiffImageFileHeader)); //DE #region 开始逐条添加DE int deLength = TIFF_DE_LENGTH; int deOffset = sizeof(TiffImageFileHeader) + Marshal.SizeOf(ifd.DECount); fixed(TiffDirectoryEntry *deArray = new TiffDirectoryEntry[ifd.DECount]) { int idx = 0; //图像宽 deArray[idx] = new TiffDirectoryEntry { TagID = (ushort)TiffDETag.ImageWidth, DataType = (ushort)TiffDEDataType.SHORT, Count = 1, DataOrPtr = this._width }; CopyData2Array((byte *)&(deArray[idx]), deLength, headerData, deOffset + idx * deLength); //图像高 idx = 1; deArray[idx] = new TiffDirectoryEntry { TagID = (ushort)TiffDETag.ImageLength, DataType = (ushort)TiffDEDataType.SHORT, Count = 1, DataOrPtr = this._height }; CopyData2Array((byte *)&(deArray[idx]), deLength, headerData, deOffset + idx * deLength); //颜色深度 idx = 2; deArray[idx] = new TiffDirectoryEntry { TagID = (ushort)TiffDETag.BitsPerSample, DataType = (ushort)TiffDEDataType.SHORT, Count = 3, DataOrPtr = (uint)headerDataOffset }; CopyData2Array((byte *)&(deArray[idx]), deLength, headerData, deOffset + idx * deLength); byte[] colorDepth = new byte[] { 0x08, 0x00, 0x08, 0x00, 0x08, 0x00 }; CopyData2Array(colorDepth, headerData, (int)headerDataOffset); headerDataOffset += colorDepth.Length; //图片压缩 idx = 3; deArray[idx] = new TiffDirectoryEntry { TagID = (ushort)TiffDETag.Compression, DataType = (ushort)TiffDEDataType.SHORT, Count = 1, DataOrPtr = 1 }; CopyData2Array((byte *)&(deArray[idx]), deLength, headerData, deOffset + idx * deLength); //颜色空间 idx = 4; deArray[idx] = new TiffDirectoryEntry { TagID = (ushort)TiffDETag.PhotometricInterpretation, DataType = (ushort)TiffDEDataType.SHORT, Count = 1, DataOrPtr = 2 }; CopyData2Array((byte *)&(deArray[idx]), deLength, headerData, deOffset + idx * deLength); //图像扫描线偏移量 idx = 5; deArray[idx] = new TiffDirectoryEntry { TagID = (ushort)TiffDETag.StripOffsets, DataType = (ushort)TiffDEDataType.LONG, Count = 1, DataOrPtr = (uint)headerData.Length }; CopyData2Array((byte *)&(deArray[idx]), deLength, headerData, deOffset + idx * deLength); //方向 idx = 6; deArray[idx] = new TiffDirectoryEntry { TagID = (ushort)TiffDETag.Orientation, DataType = (ushort)TiffDEDataType.SHORT, Count = 1, DataOrPtr = 1 }; CopyData2Array((byte *)&(deArray[idx]), deLength, headerData, deOffset + idx * deLength); //每像素样本 idx = 7; deArray[idx] = new TiffDirectoryEntry { TagID = (ushort)TiffDETag.SamplesPerPixel, DataType = (ushort)TiffDEDataType.SHORT, Count = 1, DataOrPtr = 3 }; CopyData2Array((byte *)&(deArray[idx]), deLength, headerData, deOffset + idx * deLength); //每块行数 idx = 8; deArray[idx] = new TiffDirectoryEntry { TagID = (ushort)TiffDETag.RowsPerStrip, DataType = (ushort)TiffDEDataType.LONG, Count = 1, DataOrPtr = this._height }; CopyData2Array((byte *)&(deArray[idx]), deLength, headerData, deOffset + idx * deLength); //每块的字节数 idx = 9; deArray[idx] = new TiffDirectoryEntry { TagID = (ushort)TiffDETag.StripByteCounts, DataType = (ushort)TiffDEDataType.LONG, Count = 1, DataOrPtr = (uint)this.ImageDataLength }; CopyData2Array((byte *)&(deArray[idx]), deLength, headerData, deOffset + idx * deLength); } #endregion _fs.Write(headerData, 0, headerData.Length); }