讀取tiff文件信息

来源:互联网 发布:人员疏散模拟软件 编辑:程序博客网 时间:2024/05/23 20:50
//TiffStruct.h
#ifndef _TIFFSTRUCT_
#define _TIFFSTRUCT_

typedef struct tagIMAGEFILEHEADER
{
        WORD byteOrder;
        WORD version;
        DWORD offsetToIFD;
}IFH;
typedef struct tagDIRECTORYENTRY
{
        WORD tag;
        WORD type;
        DWORD length;
        DWORD valueOffset;
}DE;

#endif

        //用于读取tif文件信息
        IFH ifh;
        ZeroMemory(&ifh, sizeof(IFH));
        CFile file;
        CFileException fe;
        WORD wDECount = 0;

        vector<int> m_imginfo; //保存tif文件的SamplesPerPixel

                        /*********************************************************************************/
                                                //用CFlie打开文件 读取tiff信息
                        /*********************************************************************************/

                        if(0 == file.Open(pathname, CFile::modeRead | CFile::shareDenyWrite, &fe))
                        {
                                AfxMessageBox("打开文件失败");
                                break;
                        }

                        //读IFH文件头
                        if(sizeof(IFH) != file.Read(&ifh, sizeof(IFH)))
                        {
                                AfxMessageBox("读TIF文件头失败");
                                break;
                        }
                        if(0x2a != ifh.version)
                        {
                                AfxMessageBox("该文件不是TIF格式,读文件失败");
                                break;
                        }
                        if(0x4949 != ifh.byteOrder)
                        {
                                AfxMessageBox("该TIF文件不是IBMPC字节序,读文件失败");
                                break;
                        }

                        file.Seek(ifh.offsetToIFD, CFile::begin);//将文件指针定位到IFD

                        //读文件有多少个目录入口
                        if(2 != file.Read(&wDECount, 2))
                        {
                                AfxMessageBox("无法获得TIF文件目录入口数量");
                                break;
                        }

                        //创建DE数组,接收信息,数组中有wDECount个元
                        DE* pde = new DE[wDECount];
                        DE* pTemp = pde;
                        memset(pde, 0, sizeof(DE)*wDECount);

                        if(sizeof(DE)*wDECount != file.Read(pde, sizeof(DE)*wDECount))
                        {
                                AfxMessageBox("读图象文件目录失败");
                                delete []pde;
                                break;
                        }

                        //把图像的大小和图像数据的容量保存到成员变量中
                        //CString strTemp;
                        for(int i=0; i<wDECount; i++)
                        {
                                pTemp = pde + i;
                                if(277 == pTemp->tag) //tag为277的目录入口中的变量标识了SamplesPerPixel 每像素样本数
                                {
                                        
                                        m_imginfo.push_back(pTemp->valueOffset);
                                        //strTemp.Format("%d",m_imginfo.at(ipic));
                                        //AfxMessageBox("var is " + strTemp);
                                }
                        }

                        file.Close();


TIFF(Tag Image File Format)图像文件说明:

http://blog.csdn.net/eiyaya/article/details/649461


原创粉丝点击