0 引言

最近想在Unity中加载一张TIFF图片,因为该图片存储的是海洋流场数据,所以每个像素存的是四通道的32位float,并且还采用了LZW压缩。在网上找了很多读取TIFF文件的代码,也试了下载FreeImage.Net包,但都无法读取该格式的TIFF。与其继续在网上找下去,还不如自己写一个。
代码在这:OSC_TIFF

1 TIFF图像格式详解

要解码TIFF,首先要了解TIFF文件格式。当然是去看官方提供的说明文档最为直接。
TIFF 6.0
除此之外,一些中文博客也提供了很好的讲解。可以直接参考这篇文章:
解码TIFF文件

2 C#解码TIFF图像

解码TIFF实际上是个很简单的工作,只要有耐心读官方的说明文档,人人都可以自己写代码解码TIFF,只不过TIFF格式的图像种类太多,要想适用于所有的TIFF文件,对于个人来说是件非常耗时的事情。

下面我就来针对我自己想要解码的图像(32位Float * 四通道),来做一个解码小程序,希望也能对其他人有一点点帮助。

我这里适用C#来解码图像,但其实用什么语言并不影响,逻辑对的就行。

2.1

我们先写个TIFF类,里面主要放TIFF图像的各种属性和解码用到的函数。

public class TIFF
{byte[] data;//把TIFF文件读到byte数组中//接下来是TIFF文件的各种属性
bool ByteOrder;//true:II  false:MM
public int ImageWidth = 0;
public int ImageLength = 0;
public List<int> BitsPerSample = new List<int>();
public int PixelBytes = 0;
public int Compression = 0;
public int PhotometricInterpretation = 0;
public List<int> StripOffsets = new List<int>();
public int RowsPerStrip = 0;
public List<int> StripByteCounts = new List<int>();
public float XResolution = 0f;
public float YResolution = 0f;
public int ResolutionUnit = 0;
public int Predictor = 0;
public List<int> SampleFormat = new List<int>();
public string DateTime = "";
public string Software = "";public void Decode(string path){//...
}
private int DecodeIFH(){//...
}
public int DecodeIFD(int Pos){//...
}
private void DecodeDE(int Pos){//...
}
private void GetDEValue(int TagIndex, int TypeIndex, int Count, byte[] val){//...
}
private void DecodeStrips(){//...
}
static private DType[] TypeArray = {//...
};
struct DType
{public DType(string n, int s){   //...}public string name;public int size;
}

我们从Init函数开始。

public void Decode(string path){data = File.ReadAllBytes(path);//首先解码文件头,获得编码方式是大端还是小端,以及第一个IFD的位置int pIFD = DecodeIFH();//然后解码第一个IFD,返回值是下一个IFD的地址while (pIFD != 0){pIFD = DecodeIFD(pIFD);}}

Decode函数的参数是TIFF文件的位置,我们把文件数据读进来,放在byte数组中。接下来,我们需要解码TIFF文件中的各种信息。首先解码的是IFH,它可以告诉我们文件的编码方式,这直接影响了我们如何将byte数组转换成Int、Float等类型。

private int DecodeIFH(){string byteOrder = GetString(0,2);if (byteOrder == "II")ByteOrder = true;else if (byteOrder == "MM")ByteOrder = false;elsethrow new UnityException("The order value is not II or MM.");int Version = GetInt(2, 2);if (Version != 42)throw new UnityException("Not TIFF.");return GetInt(4, 4);}

来看看II和MM的区别,它将影响后面GetInt和GetFloat函数

private int GetInt(int startPos, int Length){int value = 0;if (ByteOrder)// "II")for (int i = 0; i < Length; i++) value |= data[startPos + i] << i * 8;else // "MM")for (int i = 0; i < Length; i++) value |= data[startPos + Length - 1 - i] << i * 8;return value;}private float GetRational(int startPos){int A = GetInt(startPos,4);int B = GetInt(startPos+4,4);return A / B;}private float GetFloat(byte[] b, int startPos){byte[] byteTemp;if (ByteOrder)// "II")byteTemp =new byte[]{b[startPos],b[startPos+1],b[startPos+2],b[startPos+3]};elsebyteTemp =new byte[]{b[startPos+3],b[startPos+2],b[startPos+1],b[startPos]};float fTemp = BitConverter.ToSingle(byteTemp,0);return fTemp;}private string GetString(int startPos, int Length)//II和MM对String没有影响{string tmp = "";for (int i = 0; i < Length; i++)tmp += (char)data[startPos];return tmp;}

读出的第二个数据是值为42的标志位,它是TIFF文件的标志。因为我是用在Unity中的,所以使用的是Unity中的抛出异常。可以删掉或替换程其他形式,这个无关紧要。

Decode函数的最后一部分是一个while循环,不停的解码IFD,直到读完所有的IFD文件。DecodeIFD这个函数返回的是下一个IFD的位置,如果返回的是0的话,就说明读完了,也就是说整个文件读完了。不过一般的TIFF,比如我的这个,只有一个IFD文件。(可能多页TIFF会有多个IFD文件吧,但这个我还没有验证过)

public int DecodeIFD(int Pos){int n = Pos;int DECount = GetInt(n, 2);n += 2;for (int i = 0; i < DECount; i++){DecodeDE(n);n += 12;}//已获得每条扫描线位置,大小,压缩方式和数据类型,接下来进行解码DecodeStrips();int pNext = GetInt(n, 4);return pNext;}

每个IFD文件里存的第一个信息是该IFD中DE的个数。DE里存的就是我们要读取的TIFF文件信息。每个DE占12字节,因此我们先用个循环,解码所有的DE,在这个过程中,我们将会获得TIFF图像的高度、宽度、压缩方式、图像数据的开始位置等信息。在这之后,就到了解码扫描线数据的环节。

我们先来看看DE的解码

public void DecodeDE(int Pos){int TagIndex = GetInt(Pos, 2);int TypeIndex = GetInt(Pos + 2, 2);int Count = GetInt(Pos + 4, 4);//Debug.Log("Tag: " + Tag(TagIndex) + " DataType: " + TypeArray[TypeIndex].name + " Count: " + Count);//先把找到数据的位置int pData = Pos + 8;int totalSize = TypeArray[TypeIndex].size * Count;if (totalSize > 4)pData = GetInt(pData, 4);//再根据Tag把值读出并存起来GetDEValue(TagIndex, TypeIndex, Count, pData);}

对于每一个DE,首先解码前两个字符,它存的是改DE的标签,根据标签我们就可以找到该DE存的是什么值(见表4)。然后再解码两个字符,它存的是该DE存放的数据的类型号,根据类型号可以找到数据类型(见表1)。在代码中,我写了个结构体DType存数据类型的名称和长度,有创建了一个DType的数组存放12种数据类型,数组的下标正好队形类型号。

struct DType
{public DType(string n, int s){name = n;size = s;}public string name;public int size;
}
static private DType[] TypeArray = {new DType("???",0),new DType("byte",1), //8-bit unsigned integernew DType("ascii",1),//8-bit byte that contains a 7-bit ASCII code; the last byte must be NUL (binary zero)new DType("short",2),//16-bit (2-byte) unsigned integer.new DType("long",4),//32-bit (4-byte) unsigned integer.new DType("rational",8),//Two LONGs: the first represents the numerator of a fraction; the second, the denominator.new DType("sbyte",1),//An 8-bit signed (twos-complement) integernew DType("undefined",1),//An 8-bit byte that may contain anything, depending on the definition of the fieldnew DType("sshort",1),//A 16-bit (2-byte) signed (twos-complement) integer.new DType("slong",1),// A 32-bit (4-byte) signed (twos-complement) integer.new DType("srational",1),//Two SLONG’s: the first represents the numerator of a fraction, the second the denominator.new DType("float",4),//Single precision (4-byte) IEEE formatnew DType("double",8)//Double precision (8-byte) IEEE format};

接着解码四个字节,这四个字节存的是数据的个数,因为有的数据是数组,比如每个通道的bit数,RGBA图像有4个。我的TIFF文件是128位的RGBA,所以我的BitsPerSample这一项是32,32,32,32四个数。

一般DE中数据的存放位置是该DE的第8到第12个字节。而像存放数组的,或者存的数据比较大的DE,这4个字节只存数据的位置,数据放在其他地方。因此,我们先要根据数据所占字节数,判断数据的其实位置。

//先把找到数据的位置
int pData = Pos + 8;
int totalSize = TypeArray[TypeIndex].size * Count;
if (totalSize > 4)pData = GetInt(pData, 4);

找到数据位置之后,再把数据读出来。根据标签,把TIFF类里对应的属性值填上(见表4)

private void GetDEValue(int TagIndex, int TypeIndex, int Count, int pdata){int typesize = TypeArray[TypeIndex].size;switch (TagIndex){case 254: break;//NewSubfileTypecase 255: break;//SubfileTypecase 256://ImageWidthImageWidth = GetInt(pdata,typesize);break;case 257://ImageLengthif (TypeIndex == 3)//shortImageLength = GetInt(pdata,typesize);break;case 258://BitsPerSamplefor (int i = 0; i < Count; i++){int v = GetInt(pdata+i*typesize,typesize);BitsPerSample.Add(v);PixelBytes += v/8;}break;case 259: //CompressionCompression = GetInt(pdata,typesize);break;case 262: //PhotometricInterpretationPhotometricInterpretation = GetInt(pdata,typesize);break;case 273://StripOffsetsfor (int i = 0; i < Count; i++){int v = GetInt(pdata+i*typesize,typesize);StripOffsets.Add(v);}break;case 274: break;//Orientationcase 277: break;//SamplesPerPixelcase 278://RowsPerStripRowsPerStrip = GetInt(pdata,typesize);break;case 279://StripByteCountsfor (int i = 0; i < Count; i++){int v = GetInt(pdata+i*typesize,typesize);StripByteCounts.Add(v);}break;case 282: //XResolutionXResolution = GetRational(pdata); break;case 283://YResolutionYResolution = GetRational(pdata); break;case 284: break;//PlanarConfigcase 296://ResolutionUnitResolutionUnit = GetInt(pdata,typesize);break;case 305://SoftwareSoftware = GetString(pdata,typesize); break;case 306://DateTimeDateTime = GetString(pdata,typesize); break;case 315: break;//Artistcase 317: //Differencing PredictorPredictor = GetInt(pdata,typesize);break;case 320: break;//ColorDistributionTablecase 338: break;//ExtraSamplescase 339: //SampleFormatfor (int i = 0; i < Count; i++){int v = GetInt(pdata+i*typesize,typesize);SampleFormat.Add(v);} break;default: break;}}

当所有的DE都被解码后,我们就可以来解码图像数据了。因为图像数据是一条一条的存放在TIFF文件中,DE 273 StripOffsets记录了每条扫描线的位置。DE 278 RowsPerStrip 记录了一条扫描线存了多少行图形数据。DE 279 StripByteCounts是一个数组,记录了每条扫描线数据的长度。如果不经过压缩的话,每条扫描线长度一般是相同的。

应为我的TIFF文件是采用了LZW压缩,DE 259 Compression =5,下面我就针对这种数据来解码一波。

private void DecodeStrips(){int pStrip = 0;int size = 0;tex = new Texture2D(ImageWidth,ImageLength,TextureFormat.RGBA32,false);Color[] colors = new Color[ImageWidth*ImageLength];if (Compression == 5){int stripLength = ImageWidth * RowsPerStrip * BitsPerSample.Count * BitsPerSample[1] / 8;CompressionLZW.CreateBuffer(stripLength);if(Predictor==1){int index = 0;for (int y = 0; y < StripOffsets.Count; y++){pStrip = StripOffsets[y];//起始位置size = StripByteCounts[y];//读取长度byte[] Dval = CompressionLZW.Decode(data, pStrip, size);for(int x = 0;x<ImageWidth;x++){float R = GetFloat(Dval, x * PixelBytes   );float G = GetFloat(Dval, x * PixelBytes+4 );float B = GetFloat(Dval, x * PixelBytes+8 );float A = GetFloat(Dval, x * PixelBytes+12);colors[index++] = new Color(R,G,B,A);}}} else{}}tex.SetPixels(colors);tex.Apply();}

因为是在Unity中开发的脚本,所以使用的是Unity的Texture,这个可以换成其他的,无关紧要。这里面我专门写了个类来解码LZW压缩的文件。解码后的数据直接转成Float存在Colors[]数组中,最后赋值给Texture。DE 274 Orientation就先不管了,先把图像读出来再说,无非是显示出来的图像是正的还是倒的或是镜像对称的。

下面来着重介绍一下LZW的解压方式。

while ((Code = GetNextCode()) != EoiCode) {if (Code == ClearCode) {InitializeTable();Code = GetNextCode();if (Code == EoiCode)break;WriteString(StringFromCode(Code));OldCode = Code;} /* end of ClearCode case */else {if (IsInTable(Code)) {WriteString(StringFromCode(Code));AddStringToTable(StringFromCode(OldCode)+FirstChar(StringFromCode(Code)));OldCode = Code;} else {OutString = StringFromCode(OldCode) +FirstChar(StringFromCode(OldCode));WriteString(OutString);AddStringToTable(OutString);OldCode = Code;}} /* end of not-ClearCode case */
} /* end of while loop */

其实也是比较简单的,上面写的是TIFF官方说明文档中解压TIFF的伪代码,我直接把它copy下来,粘贴在我的程序中,然后逐个实现里面的函数就好了。剩下的就是不断的调试了,总会遇到各式各样的bug。下面是我写的CompressionLZW类的大体框架。

public class CompressionLZW
{static private int Code = 0;static private int EoiCode = 257;static private int ClearCode = 256;static private int OldCode = 256;static private string[] Dic= new string[4096];static private int DicIndex;static private byte[] Input;static private int startPos;static private byte[] Output;static private int resIndex;static private int current=0;static private int bitsCount = 0;static string combine ="{0}{1}";static private void ResetPara(){OldCode = 256;DicIndex = 0;current = 0;resIndex = 0;}static public void CreateBuffer(int size){//...}static public byte[] Decode(byte[] input,int _startPos,int _readLength){//...}static private int GetNextCode(){//...}static private int GetBit(int x){//...}static private int GetStep(){//...}static private void InitializeTable(){//...}static private void WriteResult(string code){//...}
}

先来看看核心函数Decode

static public byte[] Decode(byte[] input,int _startPos,int _readLength){Input = input;startPos = _startPos;bitsCount = _readLength*8;ResetPara();while ((Code = GetNextCode()) != EoiCode) {if (Code == ClearCode) {InitializeTable();Code = GetNextCode();if (Code == EoiCode)break;WriteResult(Dic[Code]);OldCode = Code;}else {if (Dic[Code]!=null) {WriteResult(Dic[Code]);Dic[DicIndex++] =string.Format(combine, Dic[OldCode],Dic[Code][0]);OldCode = Code;} else {   string outs = string.Format(combine, Dic[OldCode], Dic[OldCode][0]);WriteResult(outs);Dic[DicIndex++] =outs;OldCode = Code;}}}return Output;}

按照TIFF官方说明文档中的伪代码写完后,我遇到的第一个bug就是没有重置一些变量。当然,这是非常低级的错误了。因为我用的是静态函数,所以,每次调用Decode函数时,都要注意将一些变量重置一些。

这串代码里最重要的应该就是GetNextCode()了。

static private int GetNextCode()
{int tmp = 0;int step = GetStep();if (current + step > bitsCount)return EoiCode;for (int i = 0; i<step; i++){int x = current + i;int bit = GetBit(x)<<(step-1-i);tmp+=bit;}current += step;//一开始读9个bit//读到510的时候,下一个开始读10bit//读到1022的时候,下一个开始读11bit//读到2046的时候,下一个开始读11bitreturn tmp;
}
static private int GetStep()
{int res = 12;int tmp = DicIndex-2047;//如果大于2046.则为正或零res+=(tmp>>31);tmp = DicIndex-1023;res+=(tmp>>31);tmp = DicIndex-511;res+=(tmp>>31);return res;
}
static private int GetBit(int x)
{int byteIndex = x/8; //该bit在第几个byte里int bitIndex =7-x+byteIndex*8;//该bit是这个byte的第几位byte b = Input[startPos + byteIndex];return (b>>bitIndex)&1;
}

因为这几个函数可能会被上百万次的调用,我这里尽量使用位操作替代了if/else语句,所以看起来不是很直观。GetNextCode()函数的任务就是获取下一个字符,但是下一个字符占几位需要判断一下,这是有GetStep()函数来完成的。

因为tmp是有符号整型,当tmp<0时,tmp的最高位为1,代表负数,右移31位后,代表负数的1移动到了最低位,但由于移位也不改变符号,所以tmp变成了-1;当tmp>=0时,tmp的最高位为0,代表正数,右移31位后,代表正数的0移动到了最低位,所以tmp变成了0。这样便避免了使用多个if/else语句。

GetBit函数直接根据下标读原始的TIFF数据数组,我没有用BitArray去操作,这里用它效率不高。要特别注意这里

int bitIndex =7-x+byteIndex*8;//该bit是这个byte的第几位

将被LZW压缩过的数据进TIFF文件的时候,是按字节写进去的。

假设我们的TIFF图像是一个只有一个像素的图像,该像素的RGB值为(16,16,16) ,将它进行LZW压缩后得到的是

100000000 000010000 1000000101 00000001 0000

但它是按字节存进去的:

10000000 00000100 00100000 01010000 00010000

如果我们直接从0开始读的话,得到的结果是这样的。

00000001 00100000 00000100 00001010 00001000

这是因为被LZW压缩后的数据是按高位存在低位的方式写入字节数据的。所以一定要注意将bit数组转换成int时候不要读错。
今天先更新到这里吧~

C#读取TIFF文件相关推荐

  1. python读取tiff文件进行波段计算_python+tifffile之tiff文件读写方式

    背景 使用python操作一批同样分辨率的图片,合并为tiff格式的文件. 由于opencv主要用于读取单帧的tiff文件,对多帧的文件支持并不好. 通过搜索发现了两个比较有用的包:TiffCaptu ...

  2. python读取tiff文件_python+tifffile之tiff文件读写方式

    背景 使用python操作一批同样分辨率的图片,合并为tiff格式的文件. 由于opencv主要用于读取单帧的tiff文件,对多帧的文件支持并不好. 通过搜索发现了两个比较有用的包:TiffCaptu ...

  3. GeoTools读取Tiff文件

    最近有个需求,要使用GeoServer自动发布InSAR处理的Tiff文件结果,基本思路: (1)上传TIFF文件,通过GeoServer REST API发布成WMTS服务: (2)使用GeoToo ...

  4. python批量读取tiff文件_Python图像分析:从共焦显微镜读取多维TIFF文件

    我不确定'hyperstack to stack'函数是否是您想要的.超stack是简单的多维图像,可以是4D或5D(宽度.高度.切片.通道(例如,3个用于RGB)和时间帧).在ImageJ中,每个维 ...

  5. Python学习之读取TIFF文件

    opencv读取 #导入cv模块 import cv2 as cv import numpy as np #读取图像,支持 bmp.jpg.png.tiff 等常用格式 #第二个参数是通道数和位深的参 ...

  6. python读取tiff影像_python+tifffile之tiff文件读写方式

    背景 使用python操作一批同样分辨率的图片,合并为tiff格式的文件. 由于opencv主要用于读取单帧的tiff文件,对多帧的文件支持并不好. 通过搜索发现了两个比较有用的包:TiffCaptu ...

  7. 保存多序列tiff文件_解码TIFF文件

    0 引言 最近想在Unity中加载一张TIFF图片,因为该图片存储的是海洋流场数据,所以每个像素存的是四通道的32位float,并且还采用了LZW压缩.在网上找了很多读取TIFF文件的代码,也试了下载 ...

  8. jai-code实现tiff文件转jpg功能

    源码下载地址:https://download.csdn.net/download/qq_37922915/12572959 //包含tif图片,jar等 import com.sun.media.j ...

  9. gdal处理tiff文件的小问题

    问题一 :ERROR 1: TIFFResetField:xxxxxxxx: Could not find tag 273. 使用gdal读取tiff文件时遇到的,把dataset用GDALclose ...

  10. python读取图片文件显示_Python—图像基本操作以及图像格式转换

    关于图像处理的模块常用的有 PIL,openCV等,不过应为要处理 tif 格式的图片,故特来写下这篇博客. 关于安装模块 libtiff 直接pip install libtiff 安装模块,发现无 ...

最新文章

  1. 迁移博客到香港虚拟空间
  2. 收藏!深度学习计算机视觉模型解析!
  3. 四位科研牛人介绍的文献阅读经验
  4. Requires: libstdc++.so.6(GLIBCXX_3.4.15)
  5. uoj22 外星人(dp)
  6. html5录音功能代码,recorder.js 基于 HTML5 实现录音功能
  7. CPI通常用于衡量计算机性能,2021考研408计算机组成原理习题:计算机的性能指标...
  8. 专业课课本复习(数电,通原,数信,信号与系统,数据结构)
  9. ImageLoader(UIL)自定义HTTP Header信息
  10. 超快速!10分钟入门Keras指南
  11. 无线网络CSMA/CA原理分析以及相关技术的介绍和分析
  12. 46FPS+1080Px2超分+手机NPU,arm提出一种基于重参数化思想的超高效图像超分方案
  13. idea提交git报401错误解决办法
  14. java判断经纬度是否在扇形内_地图学复习 (题) (带答案)
  15. 如何将明细数据关联对照表后生成汇总统计表
  16. Zookeeper客户端错误 Unable to read additional data from server
  17. linux 开机连接wifi密码忘了怎么办,WIFI密码忘了怎么办 两种方法轻松找回密码
  18. [samba]Samba协议基础
  19. matlab fir系数,Matlab 生成fir滤波器抽头系数
  20. CentOS 7下編譯安裝和配置GoldenDict

热门文章

  1. 【C语言】关机程序的实现以及函数
  2. PDF转CAD怎么弄?推荐两个转换方法
  3. ET_01-ET框架开发环境搭建及Demo运行(客户端版)
  4. 基于单片机的功放protues_基于单片机的音乐播放器设计
  5. 在IBM AIX p750小机上为Oracle扩展逻辑卷-视频分享
  6. excel vb连接mysql数据库教程视频教程_VB6.0与Access数据库关联、VB6.0与Excel数据导入导出案例...
  7. ASUS RT-N16 刷番茄Tomato
  8. android系统解压zip文件,如何在Android手机上解压缩rar / zip文件
  9. 如何换ip ,最简单有效的换ip方法在这里
  10. CaysnPrinter - Windows平台开发包打印示例程序及接口说明文档 - 20170710