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

Martin Chapman chapmanm at pixia.com
Thu Jan 12 22:11:13 EST 2012


An easy way to handle this would be to create a 3 band bitmap using the
windows gdi, in other words the bitmapinfo bits should be 24 (8 bits for
each band).  Then use the single band from the source image for every band
.  Red, green and blue.  Otherwise you will have to handle palletted like
Joaquim states.

Gonna look something like this if you have four bands:

// number of target output bands
Int numBands = 3;

// create new buffer to hold 3 band data
BYTE* targetBytes = new BYTE[stride * sourceRect.height * numBands]; 
memset(targetBytes, 0, stride * sourceRect.height * numBands);

// get pointers to first scanline (row) of each data buffer
UCHAR* sourceScan0 = (UCHAR*) sourceBytes;
UCHAR* targetScan0 = (UCHAR*) targetBytes;

// color table
GDALColorEntry colorEntry;
GDALColorTable* colorTable = band->GetColorTable();

// loop through each row of data
for (int y = 0; y < sourceRect.height; y++) 
{
	// loop through each pixel in paletted image and set the rgb
values of the new buffer
	for (UINT x = 0; x < stride; x++) 
	{
		// get color entry
		colorTable->GetColorEntryAsRGB((int) sourceScan0[0],
&colorEntry);

		// transfer pixels - note windows wants BGR
		targetScan0[0] = (UCHAR) colorEntry.c3;
		targetScan0[1] = (UCHAR) colorEntry.c2;
		targetScan0[2] = (UCHAR) colorEntry.c1;
		//targetScan0[3] = (UCHAR) colorEntry.c4;    // add this
if you have four bands

		// increment scanline pointers to the next pixel
		targetScan0 += numBands;
		sourceScan0 += 1;
	}
}

BITMAPINFO bmi;
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth  = (LONG) stride;
bmi.bmiHeader.biHeight = (LONG) - sourceRect.height;  // added the
negative to flip the bitmap in my case
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = (numBands * 8);  // 8 bits times each band, in
this case it is 3, 8 is only good for 8 bit data though, if you have 16
bit then change 8 to 16
bmi.bmiHeader.biCompression = BI_RGB; 
bmi.bmiHeader.biSizeImage = (DWORD) (stride * sourceRect.height *
numBands) 
bmi.bmiHeader.biXPelsPerMeter = 0;
bmi.bmiHeader.biYPelsPerMeter = 0;
bmi.bmiHeader.biClrUsed = 0;
bmi.bmiHeader.biClrImportant = 0;

SetStretchBltMode(memoryDC, HALFTONE);
POINT brushOrg;
SetBrushOrgEx(memoryDC, drawRect.x, drawRect.y, &brushOrg);

StretchDIBits(memoryDC,  drawRect.x, drawRect.y, drawRect.width,
drawRect.height, 0, 0, sourceRect.width, sourceRect.height, targetBytes,
&bmi, DIB_RGB_COLORS, SRCCOPY);

SetStretchBltMode(memoryDC, stretchBltMode);
SetBrushOrgEx(memoryDC, brushOrg.x, brushOrg.y, NULL);

delete [] targetBytes;
targetBytes = NULL;

By the way, I just wrote this quick so there may be bugs.  If so, let me
know and I will compile it and fix it.

Marty

-----Original Message-----
From: gdal-dev-bounces at lists.osgeo.org
[mailto:gdal-dev-bounces at lists.osgeo.org] On Behalf Of Joaquim Luis
Sent: Wednesday, January 11, 2012 10:11 AM
To: Timur Chabayev
Cc: gdal-dev at lists.osgeo.org
Subject: Re: [gdal-dev] Problem w\images with one raster band

On 11-01-2012 08:11, Timur Chabayev wrote:
> Hi,
>
> I'd like to use GDAL in my program to open raster images (MFC), but I
have one problem - when I try to open images with one raster band, these
image are drawn gray (or, in few cases, almost black). I' ve looked for a
solution, but haven't found any. 
>
>   Thanks in advance.

Single band images need an associated color palette (some prefer to call
it color map). Some programs just generate one default palette, that can
be gray, when the file doesn't provide it. Maybe that's what is happening
with you, or maybe the file has one that applied to band in question
produces a dark image. Basically you must use one software that allows you
to change the palette if the one you get doesn't please you.

Joaquim
_______________________________________________
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