[gdal-dev] Re: Problem w\images with one raster band

AksakTimur timchab at yahoo.com
Wed Jan 11 05:08:56 EST 2012


http://osgeo-org.1803224.n2.nabble.com/file/n7175677/Image.cpp Image.cpp 


Ari,


void CViewMain::OnDraw()
{
....
	if (m_image.IsOpen())
	{
	
m_image.Draw(hdcMem,m_image.m_XOff,m_image.m_YOff,m_image.m_imgWidth,m_image.m_imgHeight,
			m_image.m_xSrc,m_image.m_ySrc,m_image.m_srcWidth,m_image.m_srcHeight,
		1,1,1);
	}
....
}
void CViewMain::OnGdalOpen()
{

	LPCTSTR  szFilter = _T("Windows bitmap(*.bmp)|*.bmp|All Files
(*.*)|*.*||"); 
	CString filePath(_T("")); 
	CFileDialog fileOpenDlg(TRUE, _T("bmp"), NULL,OFN_HIDEREADONLY,szFilter); 
	if (fileOpenDlg.DoModal() == IDOK)  
	{ 
		VERIFY(filePath = fileOpenDlg.GetPathName()); 
	} 
 
	const char* pszFilename = CStringToCharUTF8(filePath);

	
	m_image.Open(filePath);

}

void CGDALImage::Draw(CDC *pDC, long xOff, long yOff, long width, long
height, long xSrc, long ySrc, long srcWidth, long srcHeight, long rband,
long gband, long bband)   
{   
       
    if(pDC==NULL)   
        return;    
    if (!m_pDataset)   
        return;   
   
    if(m_DC.m_hDC>0)   
        m_DC.Detach();   
    m_DC.Attach(pDC->m_hDC);   
    m_hdc=pDC->m_hDC;   
   
    if (rband == 0 || rband>m_bandNum)   
        rband = m_CurrentBand;   
    if (gband == 0 || gband>m_bandNum)   
        gband = m_CurrentBand;   
    if (bband == 0 || bband>m_bandNum)   
        bband = m_CurrentBand;   
   
    m_red=rband;   
    m_green=gband;   
    m_blue=bband;   
           
    if(xSrc<0 ||xSrc >m_imgWidth) xSrc=0;   
    if(ySrc<0 ||ySrc >m_imgHeight) ySrc=0;   
    if(srcWidth<1 || srcWidth > m_imgWidth) srcWidth=m_imgWidth;   
    if(srcHeight<1 || srcHeight > m_imgHeight) srcHeight=m_imgHeight;    
      m_XOff=xSrc;//¶БИлµЅДЪґжµДУ°ПсЖрКјО»ЦГ   
      m_YOff=ySrc;   
      m_buffWidth=srcWidth;//»єґжµДёЯ¶ИЈ¬їн¶И   
      m_buffHeight=srcHeight;   
    if(double(width)/
double(height)>double(srcWidth)/double(srcHeight))//±ЈЦ¤і¤їнІ»»ы±д   
    {   
        double tmp=height*double(srcWidth)/double(srcHeight);   
        width=tmp-long(tmp)>0.5?long(tmp)+1:long(tmp);      
    }   
    else   
    {   
        double tmp=width*double(srcHeight)/double(srcWidth);   
        height=tmp-long(tmp)>0.5?long(tmp)+1:long(tmp);     
    }   
   
    m_WndWidth=width;   
    m_WnDHeight=height;   
    m_WndXOff=xOff;   
    m_WndYOff=yOff;   
     m_xSrc=xSrc;   
     m_ySrc=ySrc;   
     m_srcWidth=srcWidth;   
     m_srcHeight= srcHeight;   
   
    //byte* buf = new byte[srcWidth*srcHeight];   
    int dataType=0;   
    if(m_DataType=="Byte")dataType=0;   
    if(m_DataType=="UInt16")dataType=1;   
    if(m_DataType=="Int16")dataType=2;   
    if(m_DataType=="UInt32")dataType=3;   
    if(m_DataType=="Int32")dataType=4;   
    if(m_DataType=="Float32")dataType=5;   
    if(m_DataType=="Float64")dataType=6;   
    if(m_DataType=="CInt16")dataType=7;   
    if(m_DataType=="CInt32")dataType=8;   
    if(m_DataType=="CFloat32")dataType=9;   
    if(m_DataType=="CFloat64")dataType=10;   
       
    GDALRasterBand  *m_pBand=NULL;   
    byte* m_ByteBuff;   
    float* m_FloatBuff;   
    byte* buf =NULL;   
    float* buffFloat=NULL;   
    int x,y;   
    CDC dcMem;   
    dcMem.CreateCompatibleDC(pDC);   
    CBitmap bitmap;   
    bitmap.CreateCompatibleBitmap(pDC,width,height);   
    dcMem.SelectObject(&bitmap);   
    int i;   
    switch(dataType)   
    {   
        case 0://byteѕНbyte        
               
                m_ByteBuff = new byte[width*height*3];             
                m_buffHeight=height;   
                m_buffWidth=width;                 
            m_buffSize=height*width*3;   
            buf = new byte[width*height];   
               
            m_pBand = m_pDataset->GetRasterBand(rband);     
            if (m_pBand)   
            {   
                if (CE_None==m_pBand->RasterIO( GF_Read, xSrc,ySrc,
srcWidth, srcHeight, buf, width,height, GDT_Byte, 0, 0 ))   
                {   
                    for ( i = 0; i < width*height; i ++)    
                        m_ByteBuff[3*i+2] = buf[i];   
                }   
            }      
            m_pBand = m_pDataset->GetRasterBand(gband);   
            if (m_pBand)   
            {      
                if (CE_None==m_pBand->RasterIO( GF_Read, xSrc,ySrc,
srcWidth, srcHeight, buf, width,height, GDT_Byte, 0, 0 ))   
                {   
                    for ( i = 0; i <width*height; i ++)    
                        m_ByteBuff[3*i+1] = buf[i];   
                }   
            }   
               
            m_pBand = m_pDataset->GetRasterBand(bband);   
            if (m_pBand)   
            {      
                if (CE_None==m_pBand->RasterIO( GF_Read, xSrc,ySrc,
srcWidth, srcHeight, buf, width,height, GDT_Byte,    0, 0 ))   
                {   
                    for ( i = 0; i < width*height; i ++)    
                        m_ByteBuff[3*i+0] = buf[i];   
                }   
            }   
            for ( x = 0; x < width; x ++)   
            {   
                for ( y = 0; y < height; y ++)    
                {   
                   
dcMem.SetPixel(x,y,RGB(m_ByteBuff[(y*width+x)*3+2],m_ByteBuff[(y*width+x)*3+1],m_ByteBuff[(y*width+x)*3+0]));   
                }   
            }   
            pDC->BitBlt(xOff,yOff,width,height,&dcMem,0,0,SRCCOPY);   
            delete buf;   
            delete  m_ByteBuff;   
            m_ByteBuff=NULL;   
            buf=NULL;   
            break;   
        default://ЖдЛыАаРНµД¶јУГfloat   
               
               
                m_FloatBuff = new float[width*height*3];               
                m_buffHeight=height;   
                m_buffWidth=width;   
               
            m_buffSize=height*width*3;   
            buffFloat = new float[width*height];   
               
            m_pBand = m_pDataset->GetRasterBand(rband);     
            if (m_pBand)   
            {   
                if (CE_None==m_pBand->RasterIO( GF_Read, xSrc,ySrc,
srcWidth, srcHeight, buffFloat, width,height, GDT_Float32, 0, 0 ))   
                {   
                    for ( i = 0; i  <width*height; i ++)    
                        m_FloatBuff[3*i+2] = buffFloat[i];   
                }   
            }      
            m_pBand = m_pDataset->GetRasterBand(gband);   
            if (m_pBand)   
            {      
                if (CE_None==m_pBand->RasterIO( GF_Read, xSrc,ySrc,
srcWidth, srcHeight, buffFloat, width,height, GDT_Float32, 0, 0 ))   
                {   
                    for ( i = 0; i <width*height; i ++)    
                        m_FloatBuff[3*i+1] = buffFloat[i];   
                }   
            }   
               
            m_pBand = m_pDataset->GetRasterBand(bband);   
            if (m_pBand)   
            {      
                if (CE_None==m_pBand->RasterIO( GF_Read, xSrc,ySrc,
srcWidth, srcHeight, buffFloat, width,height, GDT_Float32,   0, 0 ))   
                {   
                    for ( i = 0; i < width*height; i ++)    
                        m_FloatBuff[3*i+0] = buffFloat[i];   
                }   
            }   
            for ( x = 0; x < width; x ++)   
            {   
                for ( y = 0; y < height; y ++)    
                {   
                   
dcMem.SetPixel(x,y,RGB(m_FloatBuff[(y*width+x)*3+2],m_FloatBuff[(y*width+x)*3+1],m_FloatBuff[(y*width+x)*3+0]));   
                }   
            }   
            pDC->BitBlt(xOff,yOff,width,height,&dcMem,0,0,SRCCOPY);                 
            delete buffFloat;   
            delete  m_FloatBuff;   
            m_FloatBuff=NULL;   
            buffFloat=NULL;   
            break;   
    }   
       
  
}  


For imagesc with bands >= 3 the  OnDraw function works fine:
m_image.Draw(hdcMem,m_image.m_XOff,m_image.m_YOff,m_image.m_imgWidth,m_image.m_imgHeight,
			m_image.m_xSrc,m_image.m_ySrc,m_image.m_srcWidth,m_image.m_srcHeight,
		1,2,3);

Best Regards

--
View this message in context: http://osgeo-org.1803224.n2.nabble.com/gdal-dev-Problem-w-images-with-one-raster-band-tp7175398p7175677.html
Sent from the GDAL - Dev mailing list archive at Nabble.com.


More information about the gdal-dev mailing list