[gdal-dev] TR: Load GDALDataset Into DIB

Ismael BELAAOUAD Ismael.BELAAOUAD at circet.fr
Thu Aug 29 09:11:13 PDT 2013


Hello,
I finally load GDALDataSet into DIB but when I display the raster it's inverted on Y axis.
Did someone have an idea?

BOOL CDib::LoadFromGDAL (LPCTSTR szFileName)
{
	BOOL bRet=FALSE;
	BOOL   bOK;
	UINT   ncol, size, w;

	Init();

	GDALDataset  *poDataset;

	GDALAllRegister();

	poDataset = (GDALDataset *) GDALOpen( szFileName, GA_ReadOnly );
	if( poDataset != NULL )
	{
		BITMAPINFO bm = { sizeof(BITMAPINFOHEADER), poDataset->GetRasterXSize(),  poDataset->GetRasterYSize(), 1, 24, BI_RGB };

		/*DIBINFOGDAL rbmi(bm);*/
		Init();
		bmi.Header=bm.bmiHeader;

		int nBands = poDataset->GetRasterCount();

		if ( nBands == 1 )
		{
			bmi.Header.biBitCount = 8 ;

			switch ( poDataset->GetRasterBand(1)->GetColorInterpretation())
			{
				case GCI_PaletteIndex :
				{
					GDALColorTable * pgdColorTable = poDataset->GetRasterBand(1)->GetColorTable();

					bmi.Header.biClrUsed = pgdColorTable->GetColorEntryCount();

					if ( ( 	bmi.Header.biClrUsed > 256 ) || ( pgdColorTable->GetPaletteInterpretation() != GPI_RGB ))
					{
						return NULL; // unsupported image format
					}
					// Fill the color table
					for ( UINT i=0 ; i < 	bmi.Header.biClrUsed ; i++ )
					{
						bmi.Colors[i].rgbBlue =(BYTE) pgdColorTable->GetColorEntry(i)->c3;
						bmi.Colors[i].rgbGreen =(BYTE) pgdColorTable->GetColorEntry(i)->c2;
						bmi.Colors[i].rgbRed =(BYTE) pgdColorTable->GetColorEntry(i)->c1;
						bmi.Colors[i].rgbReserved =0;

						/*
						rbmi.ColorTable(i)->rgbBlue =(BYTE) pgdColorTable->GetColorEntry(i)->c3;
						rbmi.ColorTable(i)->rgbGreen =(BYTE) pgdColorTable->GetColorEntry(i)->c2;
						rbmi.ColorTable(i)->rgbRed =(BYTE) pgdColorTable->GetColorEntry(i)->c1;
						rbmi.ColorTable(i)->rgbReserved = 0;
						*/
					}
					break;
				}
				case GCI_GrayIndex :
				{
					bmi.Header.biClrUsed = 256 ;
					// Fill the color table
					for ( UINT i=0 ; i < bmi.Header.biClrUsed ; i++ )
					{
						RGBQUAD q = {i,i,i,0} ;
						bmi.Colors[i] = q ;
						//*rbmi.ColorTable(i) = q ;
					}
				}
				break;
				default :
					return NULL ;
			}
		}
		// Create the DIB
		//HDC dc = CreateCompatibleDC( NULL );
		LPVOID pBits;
		HBITMAP hbm = CreateDIBSection(NULL,(BITMAPINFO*)&bmi,DIB_RGB_COLORS, &pBits, NULL, NULL );
		int nError = GetLastError();
		if ( ! hbm )
		{
			//DeleteDC(dc);
			SetLastError( ERROR_NO_SYSTEM_RESOURCES );
			LocalFree(pBits);
			return FALSE;
		}

		// Fill the Bits
		CPLErr gdErr = CE_Failure;
		int nScanSize = ((bmi.Header.biWidth  * bmi.Header.biBitCount + 31) & (~31) ) / 8;
		int nBandsRead = 1 ;

		if ( bmi.Header.biBitCount == 8 ) // Paletted or Gray
		{
			gdErr = poDataset->GetRasterBand(1)->RasterIO(
			            GF_Read , 0 ,0 , poDataset->GetRasterXSize(), poDataset->GetRasterYSize(),
			            pBits,bmi.Header.biWidth,bmi.Header.biHeight,GDT_Byte,
			            1, nScanSize );


		}
		else
		{
			for ( int iBand = 1 ,nBandsRead = 0 ; iBand <= nBands ; iBand ++ )
			{
				int iColorOffset;
				switch( poDataset->GetRasterBand(iBand)->GetColorInterpretation() )
				{
						// BGR
					case GCI_RedBand :
						iColorOffset = 2;
						break;
					case GCI_GreenBand :
						iColorOffset = 1;
						break;
					case GCI_BlueBand :
						iColorOffset = 0;
						break;
					default :
						continue ;
				}
				gdErr = poDataset->GetRasterBand(iBand)->RasterIO(
				            GF_Read , 0 ,0 , poDataset->GetRasterXSize(), poDataset->GetRasterYSize(),
				            ((LPBYTE)pBits) + iColorOffset ,bmi.Header.biWidth,bmi.Header.biHeight,
				            GDT_Byte, 3, nScanSize );
				nBandsRead ++;
			}

		}



		if  (( gdErr != CE_None ) || !nBandsRead )
		{
			::DeleteObject(hbm);

			return FALSE;
		}
}

-----Message d'origine-----
De : gdal-dev-bounces at lists.osgeo.org [mailto:gdal-dev-bounces at lists.osgeo.org] De la part de Ismael BELAAOUAD
Envoyé : jeudi 29 août 2013 09:39
À : gdal-dev at lists.osgeo.org
Objet : [gdal-dev] Load GDALDataset Into DIB

Hello,

Can someone help me on loading GDALDataset into DIB in C++ project?

Best regards,



Ismaël BELAAOUAD 
Project Manager


_______________________________________________
gdal-dev mailing list
gdal-dev at lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/gdal-dev


More information about the gdal-dev mailing list