您的位置:首页 > 其它

[GDAL]4.影像的读取和显示

2012-12-31 17:00 218 查看
RasterIO的用法参考:

在MFC项目中添加一个对话框DlgFastShow,添加一个按钮 ,在头文件中添加如下代码:

public:
GDALDataset* m_pDataset;
double m_dScale;            //现有图框与图像的比值

int m_iMinx;
int m_iMiny;
int m_iMaxx;
int m_iMaxy;
void ShowRaster();


在实现文件中添加如下代码:

void DlgFastShow::OnBnClickedBtnOpenimage()
{
CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,_T("IMG影像文件(*.img)|*.img|TIFF影像文件(*.tif)|*.tif||"),AfxGetMainWnd());
CString str;
if (dlg.DoModal()==IDOK)
{
str=dlg.GetPathName();
GDALAllRegister();
CPLSetConfigOption("GDAL_FILENAME_IS_UTF8","NO");
const char* pszFile =(LPCTSTR)str;

//使用只读方式打开图像
m_pDataset = (GDALDataset*)GDALOpen(pszFile,GA_ReadOnly);
if( m_pDataset == NULL )
{
CString ex;
ex.AppendFormat("File: %s不能打开!\n",str);
}
double padfTransform[6] = {0.0};
m_pDataset->GetGeoTransform(padfTransform);
ShowRaster();
}
}
void DlgFastShow::ShowRaster()
{
int iStartCol = 0;
int iStartRow = 0;
int dataWidth = m_pDataset->GetRasterXSize();//spinEndCol->value() - iStartCol;
int dataHeight =m_pDataset->GetRasterYSize();// spinEndRow->value() - iStartRow;
int dataBands =  m_pDataset->GetRasterCount();
CString ex;
ex.AppendFormat("行列数目: %d %d %d",dataWidth,dataHeight,dataBands);
int band_list[3] = {3,2,1};

m_dScale = dataHeight > dataWidth ? dataHeight : dataWidth;
int iViewHeight = 478;
m_dScale = iViewHeight/m_dScale;

int iSize = GDALGetDataTypeSize(GDT_Byte) / 8;//以字节为单位
int iScaleWidth = static_cast<int>(dataWidth*m_dScale+0.5);
int iScaleHeight = static_cast<int>(dataHeight*m_dScale+0.5);
ex.AppendFormat("缓存数目: %d %d",iScaleWidth,iScaleHeight);
iScaleWidth = (iScaleWidth*8+31)/32*4;
ex.AppendFormat("取整数目: %d ",iScaleWidth);
AfxMessageBox(ex);
unsigned char* pBuffer = new unsigned char[iScaleWidth*iScaleHeight*3];
CPLErr err = m_pDataset->RasterIO(GF_Read, 0, 0,dataWidth, dataHeight, pBuffer, iScaleWidth, iScaleHeight,GDT_Byte, 3, band_list, iSize*3, iSize*iScaleWidth*3, iSize);   //读取3个波段的数据

unsigned char* pDataBuffer = NULL;
if (dataBands >=3 )
{
pDataBuffer = pBuffer;
}
else
{
pDataBuffer = new unsigned char[iScaleWidth*iScaleHeight*3];
for (int i=0; i<iScaleWidth*iScaleHeight*3; i++)
pDataBuffer[i] = pBuffer[i/3];
delete []pBuffer;
}

/*CBitmap bitmap;
int flag=bitmap.CreateBitmap(iScaleWidth, iScaleHeight,1,24,pDataBuffer );*/
//int flag=bitmap.CreateCompatibleBitmap(iScaleWidth, iScaleHeight,1,32,pDataBuffer );
/*if (flag==0)
{
AfxMessageBox("创建位图失败!");
}*/
//HBITMAP hBmp;    // 保存CBitmap加载的位图的句柄
//hBmp = (HBITMAP)bitmap.GetSafeHandle();  // 获取bitmap加载位图的句柄

//picImage.SetBitmap(hBmp);
//CClientDC dc(this);
//BITMAP bm;
//bitmap.GetObject(sizeof    (BITMAP),&bm);
//CDC dcMem;
//dcMem.CreateCompatibleDC(&dc);
//CBitmap *pOldbmp=dcMem.SelectObject(&bitmap);
//CDC *pDC=&dc;
//pDC->BitBlt(0,0,bm.bmWidth,bm.bmHeight,&dcMem,0,0,SRCCOPY);
//dcMem.SelectObject(pOldbmp);

CDC dc;
CClientDC cdc(this);

dc.Attach(cdc.m_hDC);

CDC memDC;
memDC.CreateCompatibleDC(&dc);

CBitmap bmp;
bmp.CreateCompatibleBitmap(&dc,500,500);
memDC.SelectObject(&bmp);

BITMAPINFO bmpInfo;
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfo.bmiHeader.biWidth = iScaleWidth;
bmpInfo.bmiHeader.biHeight = -iScaleHeight;
//bmpInfo.bmiHeader.biWidth = 487;
//bmpInfo.bmiHeader.biHeight = -500;
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biBitCount = 24;//24位色
bmpInfo.bmiHeader.biCompression = BI_RGB;
bmpInfo.bmiHeader.biSizeImage = 0;
bmpInfo.bmiHeader.biXPelsPerMeter = 3000;
bmpInfo.bmiHeader.biYPelsPerMeter = 3000;
bmpInfo.bmiHeader.biClrUsed = 0;
bmpInfo.bmiHeader.biClrImportant = 0;

//每行字节数,4字节对齐
112         /*long nLnBytes = (487+3)/4*4*3;
113
114         BYTE *pData = new BYTE[nLnBytes*500];
115         memset(pData,0,nLnBytes*500);
116         for(int i=10; i<90; i++)
117         {
118             pData[50*nLnBytes+i*3]=255;
119             pData[i*nLnBytes+50*3+2]=255;
120         }*/

SetDIBits(dc.m_hDC,bmp,0,iScaleHeight,pDataBuffer,&bmpInfo,DIB_RGB_COLORS);
//delete []pData;
cdc.BitBlt(0,0,500,500,&memDC,0,0,SRCCOPY);
delete []pDataBuffer;

}


存在问题:屏幕一刷新,绘制的图像就没有了。

主要DDB(设备相关位图)和DIB(设备无关位图)的区别。采用了CreateCompatibleBitmap将byte数组创建为位图,采用CreateBitmap始终创只能建黑白图像。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: