Advertisement

Dumb bitmap scaling problem...

Started by December 14, 2000 06:25 PM
0 comments, last by MTEKevin 24 years ago
i'm having a problem with scaling bitmaps...lemme show you the function i wrote that does this...

// scales a DIB to OpenGL-compatible format and inverts if necessary
// note: remember to delete [] dest after you're done with it
void rectify_texture(BYTE *src, UINT uiWidth, UINT uiHeight, BYTE **dest, UINT &uiNewWidth, UINT &uiNewHeight, bool tfInvert)
{
	float sample_rate_x, sample_rate_y;
	float sample_index_x, sample_index_y;
	UINT uiSrcPixelIndex, uiDestPixelIndex;
	UINT uiSrcBytesPerLine, uiDestBytesPerLine;
	BYTE *pDestBuffer;
	BYTE *pBuffer;
	UINT i, j;

	uiSrcBytesPerLine = uiWidth*3;	// 3 because bitmaps are 24-bit

	// invert if we have to (code by Andre' LaMothe)
	if(tfInvert)
	{
		// allocate the temporary buffer
		pBuffer = new BYTE[uiSrcBytesPerLine*uiHeight];

		if(!pBuffer)
			throw;

		// copy image to work area
		memcpy(pBuffer, src, uiSrcBytesPerLine*uiHeight);

		// flip vertically
		for(i = 0; i < uiHeight; ++i)
			memcpy(&src[((uiHeight-1) - i)*uiSrcBytesPerLine], &pBuffer[i*uiSrcBytesPerLine], uiSrcBytesPerLine);

		// release the memory
		delete [] pBuffer;
	}

	// scale to a power of 2
	for(uiNewWidth = 1; uiNewWidth < (GLuint)uiWidth; uiNewWidth<<=1)
		;

	for(uiNewHeight = 1; uiNewHeight < (GLuint)uiHeight; uiNewHeight<<=1)
		;

	uiDestBytesPerLine = uiNewWidth*3;

	pDestBuffer = new BYTE[uiDestBytesPerLine*uiNewHeight];

	if(!pDestBuffer)
		throw;

	sample_rate_x = (float)uiWidth/uiNewWidth;
	sample_rate_y = (float)uiHeight/uiNewHeight;
	sample_index_x = 0.0f;
	sample_index_y = 0.0f;

	// copy the bits into the new bitmap, while scaling it
	// (this algo isn't too great since it does no filtering)
	//if(uiWidth != uiNewWidth || uiHeight != uiNewHeight)
	{
		for(j = 0; j < uiNewHeight; ++j)
		{
			for(i = 0; i < uiNewWidth; ++i)
			{
				// (i, j) is now the location of the pixel we need (in pixels, not bytes)

				uiDestPixelIndex = i*3+j*uiDestBytesPerLine;
				uiSrcPixelIndex = (UINT)(sample_index_x*3+sample_index_y*uiSrcBytesPerLine);

				pDestBuffer[uiDestPixelIndex] = src[uiSrcPixelIndex];
				pDestBuffer[uiDestPixelIndex+1] = src[uiSrcPixelIndex+1];
				pDestBuffer[uiDestPixelIndex+2] = src[uiSrcPixelIndex+2];
				sample_index_x += sample_rate_x;
			}

			sample_index_y += sample_rate_y;
		}
	}
	//else
	//	memcpy(pDestBuffer, src, uiWidth*uiHeight*3);

	*dest = pDestBuffer;
}
note that this function is specifically for 24-bit bitmaps. what happens when i use this function is that the top half of the texture is the texture that i need, just scaled down and the bottom half is seemingly random garbage. i don't get it...can anybody help me? if you need more comments in the code or something, that'll be fine, but you should probably have at least an idea of what's going on if you know the answer to my problem... - MTE Kevin Edited by - MTEKevin on 12/14/00 6:28:08 PM Edited by - MTEKevin on 12/14/00 6:28:56 PM
sample_rate_x = (float)uiWidth/uiNewWidth;
sample_rate_y = (float)uiHeight/uiNewHeight;
???
Shouldn''t this be the opposite way around... i.e.:
sample_rate_x = (float)uiNewWidth/uiWidth;
sample_rate_y = (float)uiNewHeight/uiHeight;

Because if your new Width is to be 2x as large as your previous width then your sample_rate_x = 1/2... which is not right.

Try that and see...







Regards,
Nekosion
Regards,Nekosion

This topic is closed to new replies.

Advertisement