[Gdal-dev] How to save picture into bmp-file?
ryabis
ryabis at gmail.com
Wed Sep 2 08:04:17 EDT 2009
Sorry, but I`m just started to use C++ and GDAL library... :thinking:
I have opened the image, using this code:
void BIScrollView::loadFile(QString biFileName)
{
canv->resize(biLbl->width(), biLbl->height());
if( biFileName.isEmpty() )
{
biClose();
biFileName = QFileDialog::getOpenFileName( "/data/" );
}
pszFilename = biFileName;
if( biIsOpen )
{
biClose();
}
if( !biFileName.isEmpty() ){
m_Dataset_1 = NULL;
GDALAllRegister();
m_Dataset_1 = ( GDALDataset* )GDALOpen( biFileName, GA_ReadOnly );
if( m_Dataset_1 != NULL ){
biIsOpen = true;
imgInfo_1.DriverMeta = m_Dataset_1->GetDriver()->GetMetadataItem(
GDAL_DMD_LONGNAME );
imgInfo_1.DriverDescr = m_Dataset_1->GetDriver()->GetDescription();
imgInfo_1.RasterXSize = m_Dataset_1->GetRasterXSize();
imgInfo_1.RasterYSize = m_Dataset_1->GetRasterYSize();
imgInfo_1.RasterCount = m_Dataset_1->GetRasterCount();
imgInfo_1.DataType = m_Dataset_1->GetRasterBand( 1
)->GetRasterDataType();
GDALColorTable * ColorTable = m_Dataset_1->GetRasterBand( 1
)->GetColorTable();
if( ColorTable ){
if( imgInfo_1.RasterCount == 1 ){
imgInfo_1.ColorEntryCount = ColorTable->GetColorEntryCount();
}
}
xScrollBar->setMaxValue( imgInfo_1.RasterXSize - biLbl->width() );
yScrollBar->setMaxValue( imgInfo_1.RasterYSize - biLbl->height() );
qWarning( "RasterXSize = "+QString::number( imgInfo_1.RasterXSize -
biLbl->width() ) );
qWarning( "RasterYSize = "+QString::number( imgInfo_1.RasterYSize -
biLbl->height() ) );
switch( imgInfo_1.DataType )
{
case GDT_Byte : qWarning( "DataType = Eight bit unsigned integer"
); break;
case GDT_UInt16 : qWarning( "DataType = Sixteen bit unsigned integer"
); break;
case GDT_Int16 : qWarning( "DataType = Sixteen bit signed integer"
); break;
case GDT_UInt32 : qWarning( "DataType = Thirty two bit unsigned
integer" ); break;
case GDT_Int32 : qWarning( "DataType = Thirty two bit signed
integer" ); break;
case GDT_Float32 : qWarning( "DataType = Thirty two bit floating
point" ); break;
case GDT_Float64 : qWarning( "DataType = Sixty four bit floating
point" ); break;
case GDT_CInt16 : qWarning( "DataType = Complex Int16" );
break;
case GDT_CInt32 : qWarning( "DataType = Complex Int32" );
break;
case GDT_CFloat32 : qWarning( "DataType = Complex Float32" );
break;
case GDT_CFloat64 : qWarning( "DataType = Complex Float64" );
break;
}
poBand_1 = m_Dataset_1->GetRasterBand( 1 );
imgInfo_1.ColorInterp = poBand_1->GetColorInterpretation();
switch( imgInfo_1.ColorInterp )
{
case GCI_GrayIndex : qWarning( "ColorInterp = Greyscale" );
break;
case GCI_PaletteIndex : qWarning( "ColorInterp = Paletted (see
associated color table)" ); break;
case GCI_RedBand : qWarning( "ColorInterp = Red band of RGBA
image" ); break;
case GCI_GreenBand : qWarning( "ColorInterp = Green band of RGBA
image" ); break;
case GCI_BlueBand : qWarning( "ColorInterp = Blue band of RGBA
image" ); break;
case GCI_AlphaBand : qWarning( "ColorInterp = Alpha
(0=transparent, 255=opaque)" ); break;
case GCI_HueBand : qWarning( "ColorInterp = Hue band of HLS
image" ); break;
case GCI_SaturationBand : qWarning( "ColorInterp = Saturation band of
HLS image" ); break;
case GCI_LightnessBand : qWarning( "ColorInterp = Lightness band of
HLS image" ); break;
case GCI_CyanBand : qWarning( "ColorInterp = Cyan band of CMYK
image" ); break;
case GCI_MagentaBand : qWarning( "ColorInterp = Magenta band of CMYK
image" ); break;
case GCI_YellowBand : qWarning( "ColorInterp = Yellow band of CMYK
image" ); break;
case GCI_BlackBand : qWarning( "ColorInterp = Black band of CMLY
image" ); break;
// case GCI_YCbCr_YBand : qWarning( "ColorInterp = Y Luminance" );
break;
// case GCI_YCbCr_CbBand : qWarning( "ColorInterp = Cb Chroma" );
break;
// case GCI_YCbCr_CrBand : qWarning( "ColorInterp = Cr Chroma" );
break;
case GCI_Undefined : qWarning( "ColorInterp = GCI_Undefined" );
break;
//case GCI_Max : qWarning( "ColorInterp = Max current value" );
break;
}
biPaintTile();
}
}
}
void BIScrollView::biPaintTile()
{
if( biIsOpen == true ){
QImage *result;
QVector<QRgb> colors;
uchar * pafScanline;
int y_off = yScrollBar->value();
int x_off = xScrollBar->value();
int xsize = biLbl->width();
int ysize = biLbl->height();
if( imgInfo_1.RasterCount >= 2 )
{
pafScanline = ( uchar* )CPLMalloc( xsize * ysize * 4 );
Q_CHECK_PTR( pafScanline );
for( int i = 1; i <= 3; ++i )
{
/*GDALRasterBand **/poBand_1;
poBand_1 = m_Dataset_1->GetRasterBand( i );
Q_CHECK_PTR( poBand_1 );
static int smap[ 4 ] = { 0, 2, 1, 0 };
if( poBand_1->RasterIO( GF_Read, x_off, y_off, xsize, ysize,
pafScanline + smap[ i ], xsize, ysize, GDT_Byte,
4, xsize * 4 ) == CE_Failure ){
qWarning( "RasterIO error:"+QString( CPLGetLastErrorMsg() ) );
}
else pafScanline_res = pafScanline;
}
result = new QImage( xsize, ysize, 32, 32 );
Q_CHECK_PTR( result );
for( int y = 0; y < ysize; ++y )
{
QRgb *sLine = ( QRgb * )result->scanLine( y );
memcpy( sLine, pafScanline + y * xsize * 4, xsize * 4 );
}
pafScanline_res = pafScanline; return;
CPLFree( pafScanline );
canv->setBackgroundPixmap( QPixmap( *result ) );
delete result;
}
if( imgInfo_1.RasterCount == 1 ){
pafScanline = ( uchar* )CPLMalloc( xsize * ysize );
int bps = 8;
int colors = ( bps < 8 ) ? 1 << bps : 256;
QImage * result = new QImage(
xsize,
ysize,
( bps > 1 ) ? 8 : 1,
colors,
( bps > 1 ) ? QImage::IgnoreEndian : QImage::BigEndian );
result->setNumColors( colors );
/*GDALRasterBand **/poBand_1 = m_Dataset_1->GetRasterBand( 1 );
if( imgInfo_1.ColorInterp == GCI_PaletteIndex )
{
GDALColorTableH gdalColorTable = GDALGetRasterColorTable( poBand_1 );
if( gdalColorTable )
{
int count = GDALGetColorEntryCount( gdalColorTable );
for( int i = 0; i < count; i++ )
{
const GDALColorEntry * colorEntry = GDALGetColorEntry( gdalColorTable, i
);
if( !colorEntry ) continue;
result->setColor( i, qRgb( colorEntry->c1, colorEntry->c2, colorEntry->c3
) );
}
}
}
else if( imgInfo_1.ColorInterp == GCI_GrayIndex )
{
float s = 255.0f / ( colors - 1 );
result->setNumColors( colors );
for( int i = 0; i < colors; ++i )
{
result->setColor( ( colors - 1 - i ), qRgb( ( int )( s * i + 0.5f ), (
int )( s * i + 0.5f ), ( int )( s * i + 0.5f ) ) );
}
}
else
qWarning( "Invalid pallete in single channel" );
poBand_1->RasterIO( GF_Read, x_off, y_off, xsize, ysize, pafScanline,
xsize, ysize, GDT_Byte, 1, xsize );
pafScanline_res = pafScanline;
poBand_11 = poBand_1;
//m_Dataset_1->RasterIO( GF_Read, x_off, y_off, xsize, ysize,
pafScanline, xsize, ysize, GDT_Byte, 1, 0, 0, 0, 0 );
for( int y = 0; y < ysize; ++y ){
QRgb *sLine = ( QRgb * )result->scanLine( y );
memcpy( sLine, pafScanline + y * xsize, xsize );
}
CPLFree( pafScanline );
canv->setBackgroundPixmap( QPixmap( *result ) );
canv->resize(result ->width(), result ->height());
delete result;
}//if( tiffinfo.RasterCount == 1 )
}
}
... and i want to save same area to bmp-file.
I do as is written in documentation:
void BIScrollView::saveToImage(QString trnsfFilename)
{
if( !trnsfFilename.isEmpty() )
{
const char *pszFormat = "BMP";
GDALDriver *poDriver;
char **papszMetadata;
poDriver = GetGDALDriverManager()->GetDriverByName(pszFormat);
if( poDriver == NULL )
{
qDebug("poDriver == NULL");
return;
}
papszMetadata = poDriver->GetMetadata();
if( CSLFetchBoolean( papszMetadata, GDAL_DCAP_CREATE, FALSE ) )
printf( "%s Create().\n", pszFormat );
if( CSLFetchBoolean( papszMetadata, GDAL_DCAP_CREATECOPY, FALSE ) )
printf( "%s CreateCopy().\n", pszFormat );
GDALDataset *poDstDS;
char **papszOptions = NULL;
poDstDS = poDriver->Create( trnsfFilename, 512, 512, 1, GDT_Byte,
papszOptions );
double adfGeoTransform[6] = { 444720, 30, 0, 3751320, 0, -30 };
OGRSpatialReference oSRS;
char *pszSRS_WKT = NULL;
GDALRasterBand *poBand;
GByte abyRaster[512*515];
poDstDS->SetGeoTransform( adfGeoTransform );
oSRS.SetUTM( 11, TRUE );
oSRS.SetWellKnownGeogCS( "NAD27" );
oSRS.exportToWkt( &pszSRS_WKT );
CPLFree( pszSRS_WKT );
poBand = poDstDS->GetRasterBand(1);
if (poBand->RasterIO( GF_Write, 0, 0, 512, 512, abyRaster, 512, 512,
GDT_Byte, 0, 0 ) == CE_Failure)
qDebug("no");
else
qDebug("yes");
delete poDstDS;
}
}
What should I write instead of abyRaster?
--
View this message in context: http://n2.nabble.com/How-to-save-picture-into-bmp-file-tp3566284p3566284.html
Sent from the GDAL - Dev mailing list archive at Nabble.com.
More information about the gdal-dev
mailing list