# Bitmap rendering problem

## Recommended Posts

Crypter    748
Hello everyone, I have the following routine that renders a 4 bit bitmap image to display. I added extra comments to make it easier to read.
void
VidRenderImage (IN PBYTE Image,
IN WORD x,
IN WORD y,
IN WORD w,
IN WORD h) {

PBYTE pImage = Image;
DWORD height=0;

while (h--) {	//pixels per height

DWORD CurrentPixel = 0;

while (CurrentPixel < w) {

//! convert the pixel into a byte (2 pixels per byte)
WORD CurrentWidthByte = CurrentPixel / 2;

//! grab the pixel
BYTE pixel = pImage [CurrentWidthByte];

//! two pixels per byte..mask out the pixel we are on
if ( (CurrentPixel % 2) == 0)
pixel >>= 4;
else
pixel &= 0x0f;

//! plot pixel
VidPlotPixel (x + CurrentPixel , y + height , pixel);

//! move to next pixel
CurrentPixel++;
}

//! go to next row
pImage += w/2; //w/2 as w is in pixels not bytes; 2 pixels per byte
height++;
}
}


The code does seem to work, but only if the width of the image is 196. If it is 195, 197, or other widths, I seem to run into a problem with the image. I am not quite sure what the problem is at the moment. This will be better explained by viewing an image. These is the image being rendered at different widths by the above code.
The image on the bottom is what is happening when it fails. I can fix it by adding a "pImage += 2" right after "pImage += w/2" but this will only work for that specific width. Other widths seem to require a "pImage += 3" or pImage += 4" or similar to "fix" them. This is hackish. I know I am missing something in the above code to resolve this, but i do not know what it is. *additional info: These are bitmap resources. The resolutions are listed from the files themselves and are not hard coded. i.e., the code gets the width and height from the bitmap itself. Does anyone see anything wrong? Thanks for any help or suggestions [smile]

##### Share on other sites
swiftcoder    18433
What happens when you have an odd number of pixels per-row? Given that you store 2 pixels per byte, I would assume you need to add one to width if it is an odd number.

##### Share on other sites
AAA    196
If I recall correctly, bitmaps scan lines have a 32bit pitch. So a line must be a multiple of 4 bytes. If the width of the image is not a multiple of 4 then you need to read in the extra padding and ignore that data.

##### Share on other sites
Crypter    748
Quote:
 Original post by AAAIf I recall correctly, bitmaps scan lines have a 32bit pitch. So a line must be a multiple of 4 bytes. If the width of the image is not a multiple of 4 then you need to read in the extra padding and ignore that data.

It was an alignment problem and is fixed now. Thank you both for you suggestions and help!

Now if only I can figure out the way around MSPaints palettes.. [grin]

## Create an account

Register a new account