Junk Pixels in BMP Drawing

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
singerng
Posts: 21
Joined: Sun Jan 20, 2013 6:27 pm

Junk Pixels in BMP Drawing

Post by singerng »

I'm trying to implement some basic code to draw bitmaps (BMP images) in my OS. Here's the function I'm using:

Code: Select all

void draw_bmp_32(framebuffer_t *fb, char *fname)
{
	inode_t *file = fs_open(fname);

	bmp_header_t header;

	fs_read(file, &header, 0, sizeof(bmp_header_t));

	if(header.header_field != 0x4D42) panic("Invalid BMP header field: %04X.\n", header.header_field);
	if(header.color_planes != 1) panic("Invalid BMP # color planes: %d.\n", header.color_planes);
	if(header.bpp != 32) panic("Invalid BMP bytes-per-pixel (must be 32): %d.\n", header.bpp);

	uint32_t file_offset = header.pixeldata_start;
	uint32_t fb_offset = 0;
	uint32_t row_size = floor(header.bpp * header.width + 31, 32) * 4;

	for (uint32_t row = 0; row < abs(header.height); row++)
	{
		fs_read(file, fb->buffer + fb_offset, file_offset, header.bpp / 8 * header.width);
		file_offset += row_size;
		fb_offset += fb->pitch;
	}
}
I'm drawing some random shapes that I happened to have lying around in a BMP. I've attached the image that was actually drawn and the image that should've been drawn. The image that was actually drawn had junk pixels in it. Interestingly, the junk pixels are different when I run it in Bochs and when I run it on my laptop. Does anybody have any ideas why this could be happening? I think it might have something to do with the bit orders, but I'm not sure.

Thanks,
Noah
Attachments
How it's supposed to look
How it's supposed to look
boot screen.jpg (3.11 KiB) Viewed 1877 times
On real hardware (my laptop)
On real hardware (my laptop)
IMG_0281.jpeg (27.97 KiB) Viewed 1881 times
On Bochs
On Bochs
IMG_0280.jpeg (34.18 KiB) Viewed 1881 times
User avatar
ScropTheOSAdventurer
Member
Member
Posts: 86
Joined: Sun Aug 25, 2013 5:47 pm
Location: Nebraska, USA

Re: Junk Pixels in BMP Drawing

Post by ScropTheOSAdventurer »

Now, I know NOTHING on this bitmap drawing stuff. But I can safely say you are probably not giving enough information. How is this function called? How is fs_read supposed to be called? What is your framebuffer structure like exactly?
"Procrastination is the art of keeping up with yesterday."
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Junk Pixels in BMP Drawing

Post by Combuster »

One of the obvious issues here is that you forgot bitmap coordinates are not screen coordinates - instead they follow the same direction as how you would plot a function in math classes.

The other issue is possibly caused by having a bitmap covering only a part of the screen (and you neglecting to clear the rest of it in advance).
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Bender
Member
Member
Posts: 449
Joined: Wed Aug 21, 2013 3:53 am
Libera.chat IRC: bender|
Location: Asia, Singapore

Re: Junk Pixels in BMP Drawing

Post by Bender »

Is it my brain.exe or the image rendered is inverted? Another bug or a feature?
"In a time of universal deceit - telling the truth is a revolutionary act." -- George Orwell
(R3X Runtime VM)(CHIP8 Interpreter OS)
singerng
Posts: 21
Joined: Sun Jan 20, 2013 6:27 pm

Re: Junk Pixels in BMP Drawing

Post by singerng »

Sorry for not being specific enough on my first try. framebuffer_t is a cross-platform structure I use for framebuffers in my graphics code that contains, among other things, the framebuffer's data region as a void*, and the framebuffer's width, height, bpp, and pitch. fs_read() takes an Inode, buffer, offset, and length. My bitmap drawing function, for now, just takes a framebuffer and a file name.

Right now the image is drawn upside-down. I'm going to fix that issue but I was first testing with this code that draws it upside-down. Finally, the image is the same size as the screen, for testing purposes. The screen has already been cleared.
User avatar
zhiayang
Member
Member
Posts: 368
Joined: Tue Dec 27, 2011 7:57 am
Libera.chat IRC: zhiayang

Re: Junk Pixels in BMP Drawing

Post by zhiayang »

A couple of points:

1. I don't know how your fs_read works internally, but why would you make repeated calls to that per row? Unless you have filesystem caching, in which case ignore this point.

2. Combuster has already pointed out, as with OpenGL and math, the 0,0 on bitmaps is usually on the bottom left instead. However, there's some quirk or something, where if the row is negative it changes the direction of the bitmap. I don't know if it's negative makes it top-left or bottom-left, but either way you can't just abs() away the height.

3. Again, I don't know the internals of your system, but one of the possible causes of the junk could be your disk reading -- it might be returning junk in the buffer or something. Either way, your actual bitmap drawing looks correct (apart from the inverted thing), but we can't tell for sure.


EDIT: Another thing I would check is your bitmap file itself. It might be compressed. It might help if you provided the original bitmap image.
singerng
Posts: 21
Joined: Sun Jan 20, 2013 6:27 pm

Re: Junk Pixels in BMP Drawing

Post by singerng »

1) I know that the image is currently flipped and that I can't simply use abs() of the height. I just haven't implemented that feature yet (I was testing it with the simpler version first).

2) It seems a very real possibility that something is wrong with my filesystem drivers. I will do more testing on this. I can dump pixel values on the screen/in the framebuffer and in the image and compare them. Specifically, I want to see a hexdump of the junk pixel data. There are LBA out of range messages in Bochs.

3) I can't upload the original image because it's too big, however, I checked and the image data doesn't seem to be compressed. This is substantiated by the fact that it displays differently on my two different devices I've been testing it on.

Thanks,
Noah
User avatar
zhiayang
Member
Member
Posts: 368
Joined: Tue Dec 27, 2011 7:57 am
Libera.chat IRC: zhiayang

Re: Junk Pixels in BMP Drawing

Post by zhiayang »

If you're getting LBA out of range errors, then you definitely want to check your filesystem code.

EDIT: Well if you want to see the pixel data, just dump it? It should also be trivial to make a user space program to dump the data from the original image.
Post Reply