tiff或tif文件的读取
以下是VC下读取TIFF文件的代码
char* szFileName = "K:\\地图\\fujian-DEM\\fujian1.tif";
TIFF* tiff = TIFFOpen(szFileName, "r");//打开Tiff文件,得到指针,以后所有的操作都通过指针进行 int nTotalFrame = TIFFNumberOfDirectories(tiff); //得到图像的总帧数 //TIFFSetDirectory(tiff,0);
//我们打开第一幅图,也就是第0帧,如果是第1帧,第二个参数写1,由此类推。因为Windows下图像基本
//操作都是以BMP格式进行,我们读出该帧并转成BMP格式。 char *dtitle;
TIFFGetField(tiff,TIFFTAG_PAGENAME,&dtitle);
//得到该帧的名字,存放在dtitle中。 int width,height;
TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &width); //得到宽度
TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &height);//得到高度 float resolution = max(width,height); uint16 bitspersample = ;
uint16 samplesperpixel = ; TIFFGetField(tiff, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel);
//每个像素占多少机器字,24位图samplesperpixel应该等于3。
TIFFGetField(tiff, TIFFTAG_BITSPERSAMPLE, &bitspersample);
//每一个机器字长,这里应为8。 uint16 bitsperpixel = bitspersample * samplesperpixel;
//算出每个像素占多少bit,24位图,值为24
DWORD dwBytePerLine = (width*bitsperpixel+)/ *;
//由上面几个参数算出图像每行所占字节(BYTE)数。 DWORD64 dwLeng = height*dwBytePerLine;//在内存里存放这帧图像数据所需要的长度
BYTE* pData = new BYTE[dwLeng]; //为存放数据分配内存空间 uint32* raster;
uint32 *row;
raster = (uint32*)malloc(width * height * sizeof (uint32));
TIFFReadRGBAImage(tiff, width, height, (uint32*)pData, );
//以上几行读出该帧数据,保存到raster中。 row = &raster[];
LPBYTE bits2 = pData;
for (int y = ; y < height; y++)
{ LPBYTE bits = bits2;
for (int x = ; x < width; x++)
{
*bits++ = (BYTE)TIFFGetB(row[x]);
*bits++ = (BYTE)TIFFGetG(row[x]);
*bits++ = (BYTE)TIFFGetR(row[x]);
}
row += width;
bits2 += dwBytePerLine;
}
_TIFFfree(raster); //因为Tif的数据存放顺序和Windows下的BMP相反,上面这几句进行转换。
//转换结束后,数据存在pData里,释放raster所用内存。 LPBITMAPINFO pInfo = new BITMAPINFO;
pInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pInfo->bmiHeader.biWidth = width;
pInfo->bmiHeader.biHeight = width;
pInfo->bmiHeader.biCompression = BI_RGB; pInfo->bmiHeader.biClrUsed = ;
pInfo->bmiHeader.biClrImportant = ;
pInfo->bmiHeader.biPlanes = ;
pInfo->bmiHeader.biBitCount = ;
pInfo->bmiHeader.biSizeImage = dwLeng; float xres,yres;
uint16 res_unit;
//解析度单位:如是英寸,厘米
TIFFGetFieldDefaulted(tiff, TIFFTAG_RESOLUTIONUNIT, &res_unit); if(TIFFGetField(tiff, TIFFTAG_XRESOLUTION, &xres) == )
{
pInfo->bmiHeader.biXPelsPerMeter = ;
}
else
{
if(res_unit == ) //英寸
{
pInfo->bmiHeader.biXPelsPerMeter = xres * / ;
}
else if(res_unit == ) //厘米
{
pInfo->bmiHeader.biXPelsPerMeter = xres * ;
}
else
{
pInfo->bmiHeader.biXPelsPerMeter = ;
}
}
//得到该帧TIFF横向解析度,并计算出m_pInfo->bmiHeader.biXPelsPerMeter if(TIFFGetField(tiff, TIFFTAG_YRESOLUTION, &yres) == )
{
pInfo->bmiHeader.biYPelsPerMeter = ;
}
else
{
if(res_unit == ) //英寸
{
pInfo->bmiHeader.biYPelsPerMeter = yres * / ;
}
else if(res_unit == ) //厘米
{
pInfo->bmiHeader.biYPelsPerMeter = yres * ;
}
else
{
pInfo->bmiHeader.biYPelsPerMeter = ;
}
}
//得到该帧TIFF纵向解析度,并计算出m_pInfo->bmiHeader.biYPelsPerMeter BITMAPFILEHEADER bmheader;
bmheader.bfType=0x4d42;
bmheader.bfSize=;
bmheader.bfReserved1=;
bmheader.bfReserved2=;
bmheader.bfOffBits=;
//这几句是生成bmp文件的头结构 CFile bmpFile;
bmpFile.Open(_T("c://test.bmp"),CFile::modeCreate|CFile::modeWrite);
bmpFile.Write(&bmheader,sizeof(BITMAPFILEHEADER));
bmpFile.Write(&(pInfo->bmiHeader),sizeof(BITMAPINFOHEADER));
bmpFile.Write(pData,dwLeng);
bmpFile.Close(); //这里,把该帧TIFF保存到了C盘的test.bmp中,可以用看图软件打开浏览一下。 //记得释放内存空间
delete pInfo;
pInfo = NULL;
delete pData;
pData = NULL;
//如果想直接显示,就不需要释放,调用StretchDIBits在客户区的DC上就可以显示了。 //如果再打开其他帧的话,从TIFFSetDirectory开始循环运行,比如取下一帧就是
TIFFSetDirectory(tiff,);
//记得保存时另换一个bmp文件名。
//最后,对这个TIFF文件全部操作结束,记得调用
TIFFClose(tiff);
下面的代码是用GDAL打开的
char* szFileName = "K:\\地图\\fujian-DEM\\fujian1.tif";
GDALDataset *poDataset; //GDAL数据集
GDALAllRegister(); poDataset = (GDALDataset*)GDALOpen(szFileName,GA_ReadOnly);
if( poDataset == NULL )
{
AfxMessageBox(_T("文件打开失败!!!"));
return;
} GDALRasterBand *poBand; //遥感的一个波段
int nBandCount = poDataset->GetRasterCount();
poBand = poDataset->GetRasterBand(); //和数组下标有点不同 //获得图像显示窗口的尺寸
GetClientRect(&m_ViewRect); int nImgSizeX = poDataset->GetRasterXSize();
int nImgSizeY = poDataset->GetRasterYSize(); double adfGeoTransform[];
poDataset->GetGeoTransform( adfGeoTransform ); double right = adfGeoTransform[] + nImgSizeX*adfGeoTransform[];
double bottom = adfGeoTransform[] + nImgSizeY*adfGeoTransform[]; int nBufferSizeX,nBufferSizeY; nBufferSizeX = nImgSizeX;
nBufferSizeY = nImgSizeY; int nScrrenWidth = m_ViewRect.Width();
int nScrrenHeight= m_ViewRect.Height(); BYTE *pafScanblock1,*TempLock1;
pafScanblock1 = (BYTE *) CPLMalloc((nScrrenWidth)*(nScrrenHeight));
TempLock1 = pafScanblock1; poBand->RasterIO( GF_Read, , ,nBufferSizeX,nBufferSizeY,
pafScanblock1,nScrrenWidth,nScrrenHeight, GDT_Byte,, ); //在View逐点显示图像
DWORD dwBytes = (nScrrenWidth * ) / ;
while(((DWORD) dwBytes) % )
{
dwBytes++;
} BYTE *szBuffer = new BYTE[nScrrenHeight*dwBytes];
memset(szBuffer,,nScrrenHeight*dwBytes);
BYTE *pTemp = szBuffer;
CClientDC dc(this);
int nIndex = ;
for (int i=;i<nScrrenHeight;i++)
{
for (int j=;j<nScrrenWidth;j++)
{ BYTE dn1 = *pafScanblock1; memcpy(szBuffer,(char*)(&dn1),);
szBuffer += ; pafScanblock1 ++; } szBuffer = pTemp+dwBytes*i; }
CPLFree(TempLock1); BITMAPINFOHEADER bmiHdr;
BITMAPINFO MapInfo;
memset(&bmiHdr, , sizeof(BITMAPINFOHEADER));
bmiHdr.biBitCount = *;
bmiHdr.biClrImportant = ;
bmiHdr.biClrUsed = ;
bmiHdr.biCompression = BI_RGB;
bmiHdr.biHeight = -nScrrenHeight;
bmiHdr.biPlanes = ;
bmiHdr.biSize = sizeof(BITMAPINFOHEADER);
bmiHdr.biSizeImage = ;
bmiHdr.biWidth = nScrrenWidth; bmiHdr.biXPelsPerMeter = ;
bmiHdr.biYPelsPerMeter = ; MapInfo.bmiHeader = bmiHdr;
MapInfo.bmiColors[].rgbBlue = ;
MapInfo.bmiColors[].rgbGreen = ;
MapInfo.bmiColors[].rgbRed = ;
MapInfo.bmiColors[].rgbReserved = ; dc.SetStretchBltMode(MAXSTRETCHBLTMODE);
::StretchDIBits(dc.GetSafeHdc(), , , nScrrenWidth, nScrrenHeight,
, , bmiHdr.biWidth, -bmiHdr.biHeight,
pTemp, (LPBITMAPINFO)(&MapInfo), DIB_RGB_COLORS, SRCCOPY); GDALClose(poDataset);
delete []pTemp;
原文链接:tiff文件读取
tiff或tif文件的读取的更多相关文章
- 将多个图片合并到一个TIF文件里(非 GDAL) 优化版
不知道为什么,网上对TIF的操作的资料少得可怜,包括CodeProject上都没有找到多少,在网上大多用GDAL,但这个东西,对只想做个合并图片的功能来说,实在是牛刀杀鸡,(9个DLL要带全,相当的恐 ...
- Tif文件合并类
using System; using System.Collections; using System.Collections.Generic; using System.Drawing; usin ...
- C# tif文件转jpg
需要添加WindowBase,PresentationCore的引用. 代码如下: private Stream GetImageStream() { //可以通过网络或本地文件的形式,返回Tif文件 ...
- python 读hdf4文件,再转写成一个tif文件
1.安装pyhdf包 (1)通过此链接查找并下载pyhdf包:https://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame(根据自己的系统及python版本选择 ...
- excel to datatable (c#用NPOI将excel文件内容读取到datatable数据表中)
将excel文件内容读取到datatable数据表中,支持97-2003和2007两种版本的excel 1.第一种是根据excel文件路径读取excel并返回datatable /// <sum ...
- 条形码的应用三-----------从Excel文件中读取条形码
条形码的应用三------从Excel文件中读取条形码 介绍 上一篇文章,我向大家展示了生成多个条形码并存储到Excel文件中的一个方法.后来我又有了个想法:既然条码插入到excel中了,我可不可以从 ...
- java通过文件路径读取该路径下的所有文件并将其放入list中
java通过文件路径读取该路径下的所有文件并将其放入list中 java中可以通过递归的方式获取指定路径下的所有文件并将其放入List集合中.假设指定路径为path,目标集合为fileList,遍 ...
- [html5+java]文件异步读取及上传核心代码
html5+java 文件异步读取及上传关键代码段 功能: 1.多文件文件拖拽上传,file input 多文件选择 2.html5 File Api 异步FormData,blob上传,图片显示 3 ...
- Servlet从本地文件中读取图片,并显示在页面中
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpSer ...
随机推荐
- Unity3D事件函数的执行顺序 - 包含渲染等模块的完整版,中英文对照
原文地址: http://www.cnblogs.com/ysdyaoguai/p/3746828.html In Unity scripting, there are a number of eve ...
- Masonry介绍与使用实践(快速上手Autolayout)
MagicNumber -> autoresizingMask -> autolayout 以上是纯手写代码所经历的关于页面布局的三个时期 在iphone1-iphone3gs时代 win ...
- 分享git的常用命令
Git操作笔记 1.创建目录 $ mkdir learngit $ cd learngit 2.把新建的目录变成仓库 $ git ...
- class的继承,从基类开始
#include <iostream> #include <stdio.h> using namespace std; class A { public: A() { puts ...
- Spring-2-B Save the Students(SPOJ AMR11B)解题报告及测试数据
Save the Students Time Limit:134MS Memory Limit:0KB 64bit IO Format:%lld & %llu Descri ...
- Eclipse報錯:Could not find or load main class
代碼正確,但在Eclipse中無法運行,一直報錯: Could not find or load main class
- c#注册表对象映射
用于快捷保存与读取注册表,为对应的对象 示例 [RegistryRoot(Name = "superAcxxxxx")] public class Abc : IRegistry ...
- cocos2d-x之事件传递
bool HelloWorld::init() { if ( !Layer::init() ) { return false; } Size size=Director::getInstance()- ...
- 设计模式C#实现(四)——迭代器模式
迭代器模式:提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示. UML类图: 煎饼屋和餐厅合并了!但是有个小问题,虽然两家都同意实现相同的菜单项MenuItem,但是煎饼屋想使用A ...
- js删除所有子元素
没有removeAll的API,但也十分容易实现: var lis = $("#yetai_tbody").find("tr"); $(lis).each(fu ...