Drawing Pixels
I have noticed that this line of code is used often when drawing pixels could someone explain it to me?
void Draw_Pixels(UCHAR *video_buffer, int x, int y, int color, int lpitch)
{
video_buffer[x + y*lpitch] = color;
}
December 21, 2000 04:25 PM
OK,
Explain the following idiom:
video_buffer[x + y*lpitch] = color;
video_buffer is a pointer to the pixel data (which can be thought of as an array of pixels). In your example, its a UCHAR so 8 bits per pixel (256 color bitmap).
lpitch is the logical size of a horizontal row of pixels (not always the same as the width of the bitmap, in fact some cards have a negative pitch [scanlines reversed in memory] or pad scanlines to some length).
So, calculating (y * lpitch) gets you the offset to the start of the "Yth" line in the bitmap, and adding in "x", gets you to a specific pixel. By doing this inside a [ ] array index it is the same as:
*(video_buffer + (y * lpitch) + x) = color
If the bitmap is stored "normally" in memory, lpitch and width will be the same (in 8-bit mode), but you can''t assume this.
So, in a nutshell, that line of code plots a pixel of color "color" at "x", "y" in an 8-bit bitmap pointed to by "video_buffer" and which has a pitch of "lpitch".
If you reverse the order of the assignment, then it will read a pixel from a bitmap and assign it to color:
color = video_buffer[x + y*lpitch];
Hope this helps
Explain the following idiom:
video_buffer[x + y*lpitch] = color;
video_buffer is a pointer to the pixel data (which can be thought of as an array of pixels). In your example, its a UCHAR so 8 bits per pixel (256 color bitmap).
lpitch is the logical size of a horizontal row of pixels (not always the same as the width of the bitmap, in fact some cards have a negative pitch [scanlines reversed in memory] or pad scanlines to some length).
So, calculating (y * lpitch) gets you the offset to the start of the "Yth" line in the bitmap, and adding in "x", gets you to a specific pixel. By doing this inside a [ ] array index it is the same as:
*(video_buffer + (y * lpitch) + x) = color
If the bitmap is stored "normally" in memory, lpitch and width will be the same (in 8-bit mode), but you can''t assume this.
So, in a nutshell, that line of code plots a pixel of color "color" at "x", "y" in an 8-bit bitmap pointed to by "video_buffer" and which has a pitch of "lpitch".
If you reverse the order of the assignment, then it will read a pixel from a bitmap and assign it to color:
color = video_buffer[x + y*lpitch];
Hope this helps
Any information in a computer must be stored in some kind of memory, and that information is stored in binary coding system.
Now.
Lets say that what you see on the screen is a bunch of colored dots called pixels, and that every one of those pixels has a value witch gives its color.
And all the information that tells which color each pixel must have is stored in arrays of memory inside the computer's video card.
Now lets say that you set the video mode to be 640x480x8
That means that the monitor will display 640 pixels wide and 480 pixels high, and every one of those pixels will have a value in between 0 and 256. ( that is because a char can only hold values from 0 to 256, and you set the video mode to have 8 bits per pixel, witch is a single byte or a char)
Now lets say that your array is as follows:
Char video_buffer[ 640*480 ];
To display a white dot in the upper left corner you would say:
Video_buffer[0] = 255;
And to display a pixel anywhere in the screen you would say:
Video_buffer[x+y*lpitch] = color;
Now lets say that x is equal to 3. and that y is equal to 2.
So you want your pixel to be displayed at (2,3)
Now; think of lpitch as the width (witch is supposed to be 640, but some video cards will add a couple of more bytes per row, and lpitch is the exact value of a row, so always use lpitch), of a row in the array, ( witch is virtual because the memory is linear, that's it, in reality you have a line of 307200 bytes ).
Now if you solve what's inside the [].
X + ( Y * lpitch ) = 2 + ( 3 * lpitch ).
Lets say that lpitch is indeed 640, so X+ ( Y * lpitch ) = 2 + ( 3 * 640 ) = 1922.
So ( video_buffer[ 1922 ] = color; ) will display the pixel at (2,3)
if ( there''s a will )
return ( there''s a way );
Edited by - myself on December 21, 2000 11:59:40 PM
Now.
Lets say that what you see on the screen is a bunch of colored dots called pixels, and that every one of those pixels has a value witch gives its color.
And all the information that tells which color each pixel must have is stored in arrays of memory inside the computer's video card.
Now lets say that you set the video mode to be 640x480x8
That means that the monitor will display 640 pixels wide and 480 pixels high, and every one of those pixels will have a value in between 0 and 256. ( that is because a char can only hold values from 0 to 256, and you set the video mode to have 8 bits per pixel, witch is a single byte or a char)
Now lets say that your array is as follows:
Char video_buffer[ 640*480 ];
To display a white dot in the upper left corner you would say:
Video_buffer[0] = 255;
And to display a pixel anywhere in the screen you would say:
Video_buffer[x+y*lpitch] = color;
Now lets say that x is equal to 3. and that y is equal to 2.
So you want your pixel to be displayed at (2,3)
Now; think of lpitch as the width (witch is supposed to be 640, but some video cards will add a couple of more bytes per row, and lpitch is the exact value of a row, so always use lpitch), of a row in the array, ( witch is virtual because the memory is linear, that's it, in reality you have a line of 307200 bytes ).
Now if you solve what's inside the [].
X + ( Y * lpitch ) = 2 + ( 3 * lpitch ).
Lets say that lpitch is indeed 640, so X+ ( Y * lpitch ) = 2 + ( 3 * 640 ) = 1922.
So ( video_buffer[ 1922 ] = color; ) will display the pixel at (2,3)
if ( there''s a will )
return ( there''s a way );
Edited by - myself on December 21, 2000 11:59:40 PM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement