When drawing a bitmap, pixels come out misaligned.

Started by
2 comments, last by VanillaSnake21 5 years, 10 months ago

I'm working on a software rasterizer and I'm trying to get the text rending to work correctly. I'm producing the text as a collection of bitmaps, one bitmap for each letter. The bitmaps are generated by the FreeType library, which then passes it to my software blitter that draws them on my main texture. The issue is that when the text gets small the letters look garbled, some pixels come out missing and it just looks blurry and unreadable. However when I move the letters around by a few pixels they render correctly, sharp and crisp. I'm thinking that it's got something to do with pixel alignment, however I'm not sure exactly how my code is not aligning them already.

 

This is the overall picture of the project.

Main Texture that is the size of the screen -> this is my main drawing surface of the screen, I just access it through an array like  video_memory[x_coord + y_coord * pitch]

Various Small Bitmaps and Textures and plots -> all get drawn on the main bitmap through the same array access method

 

This is an except from this particular issue:

 


//I get the bitmap generated by the FreeType library
FT_Bitmap bmp = slot->bitmap;

//I convert the bitmap into my own bitmap format that the engine can handle
//Essentially just copies the buffer and width and height
BitmapFile* file = new BitmapFile(bmp.width, bmp.rows, bmp.buffer);

//I then draw the bitmap on my main texture
DrawBitmapWithClipping(video_mem, lpitch32, bitmap, 102, 100, NULL);

//this is the breakdown of the method
void DrawBitmapWithClipping(DWORD* dest, int destLPitch32, BitmapFile* source, int destPosX, int destPosY, RECT* sourceRegion)
{
		//... other code//

	//handles 8 bit bitmap (this is what FreeType is outputting)
	else if (byteCount == 1)
	{
		UCHAR* sourceStartMem = source->GetData() + (((offsetX + reg_offsetX) * byteCount) + ((offsetY + reg_offsetY)* image_width * byteCount));
		UCHAR* destStartMem = (UCHAR*)(dest)+(x1 + y1 *(destLPitch32 << 2));

		int numColumns = (x2 - x1) + 1;
		int numRows = (y2 - y1) + 1;

		for (int row = 0; row < numRows - 1; row++)
		{
			for (int column = 0; column < numColumns; column++)
			{
				UCHAR pixel[4];
				pixel[0] = sourceStartMem[column];

				
				if (pixel[0] != 0)
				{
					destStartMem[column * 3] = pixel[0];
					destStartMem[column * 3 + 1] = pixel[0];
					destStartMem[column * 3 + 2] = pixel[0];
					destStartMem[column * 3 + 3] = 255;
				}


			}

			destStartMem += destLPitch32 << 2;
			sourceStartMem += image_width;
		}
	}

}

 

I'd like to know how this misalignment happens and what can I do to fix it. Thanks.

 

The images included:

exmpl1 - the properly aligned letters, the code for it is expl1_ image

exmpl2 - the misaligned letters, code for it is in exmpl2_

As you can see I just changed the x-position by 1 pixel on each and they get misaligned

exmpl1.jpg

exmpl2.jpg

exmpl2_.jpg

expl1_.jpg

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

Advertisement
5 minutes ago, VanillaSnake21 said:

destStartMem[column * 3] = pixel[0];
destStartMem
[column * 3 + 1] = pixel[0];
destStartMem[column * 3 + 2] = pixel[0];
destStartMem[column * 3 + 3] = 255;

Shouldn't it be column * 4 + 0..3 ?

.:vinterberg:.

2 minutes ago, vinterberg said:

Shouldn't it be column * 4 + 0..3 ?

Yes I think that was the issue, i forgot to change this as my old code had RGB only, then I moved to RGBA. However now the letters come out with different colors, but it's probably something in the way I'm encoding the rgba value, I'm going to rework this whole code section. Thanks a bunch, I appreciate you spotting that!

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

This topic is closed to new replies.

Advertisement