Sign in to follow this  
cebugdev

Manualy plot a pixel on a bitmap file

Recommended Posts

cebugdev    635
hi how can i manually plot a single pixel to a bitmap RGB file? for example i have a unsigned char* pData that handles the data value RGB of the BMP file and i want to place a RED color on location X,Y of the bitmap. how can i do it? X would be the width and Y would be the height of the Bitmap file.

Share this post


Link to post
Share on other sites
Evil Steve    2017

unsigned char r, g, b; // Your RGB colour
int x, y; // Your pixel position to plot at

unsigned char* pPixelStart = pData + bitmapHeader.biWidthBytes * y + (bitmapHeader.biBitsPixel/8)*x;
*pPixelStart++ = b;
*pPixelStart++ = g;
*pPixelStart = r;


I think...

Bitmap files can have padding at the end of them, since each scanline needs to be DWORD (or WORD, I can't recall offhand) aligned. Pixels in a bitmap file are in BGR order, and this code assumes that it's dealing with a 24-bit bitmap.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Well, first there are a couple of things you have to know. First and foremost, what format is the bitmap in? Is it 16 bit, 24 bit, or 32 bit? Is it BGR or RGB? Since I can't answer your question completely without it, I'll just give you an example, using a standard 24 bit Windows bitmap (BGR).

The next thing you need is (a) the dimensions of the bitmap (width and height), and (b) the position of the pixel (x,y). This is easily attainable, so I leave that to you.

The last thing you'll need is to understand how the data is stored. Again, if this is a standard Windows bitmap, the first "row" of data is actually the bottom row of the image.

Now for implementation. Since the 24 bit data type doesn't really exist, I'm going to fake it.


typedef struct
{
unsigned char blue;
unsigned char green;
unsigned char red;
} BGR24, *pBGR24;



Now, we implement a draw pixel function:


void drawPixel(unsigned char *data,int width,int height,int x,int y,pBGR24 pColor)
{
/* Typecast for easy pixel access */
pBGR24 pBMP = (pBGR24)data;

/* Calculate memory position, in terms of BGR24s */
int memPos=(height-y-1)*width + x;

/* Now, set the color */
pBMP[memPos].blue=pColor->blue;
pBMP[memPos].green=pColor->green;
pBMP[memPos].red=pColor->red;
}



This should get you started. And if your pixels come out wrong, you might need RGB format, maybe it's 16 or 32 bits, or maybe the picture isn't inverted. Like I said, there are too many formats to post them all.

Share this post


Link to post
Share on other sites
cebugdev    635
Quote:
Original post by Anonymous Poster
Well, first there are a couple of things you have to know. First and foremost, what format is the bitmap in? Is it 16 bit, 24 bit, or 32 bit? Is it BGR or RGB? Since I can't answer your question completely without it, I'll just give you an example, using a standard 24 bit Windows bitmap (BGR).

The next thing you need is (a) the dimensions of the bitmap (width and height), and (b) the position of the pixel (x,y). This is easily attainable, so I leave that to you.

The last thing you'll need is to understand how the data is stored. Again, if this is a standard Windows bitmap, the first "row" of data is actually the bottom row of the image.

Now for implementation. Since the 24 bit data type doesn't really exist, I'm going to fake it.

*** Source Snippet Removed ***

Now, we implement a draw pixel function:

*** Source Snippet Removed ***

This should get you started. And if your pixels come out wrong, you might need RGB format, maybe it's 16 or 32 bits, or maybe the picture isn't inverted. Like I said, there are too many formats to post them all.



im using a 24 bit bitmap..
is this the right function? i really need this for my work

Share this post


Link to post
Share on other sites
Nychold    292
It should work, but you will need to test it. The basic concept is to multiply the correct y position by the width of the image, then add the x in. This gives you the position in the image, then change the pixel. The Windows BMP are inverted to better fit the Cartesian coordinates system, where (0,0) is the lower left corner rather than being the upper left corner, like in standard graphics.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this