Gdiplus-lock up your bits

Source: Internet
Author: User

The bitmap class providesLockbitsAnd correspondingUnlockbitsMethods which enable you to fix a portion of the bitmap pixel data array in memory, access it directly and finally replace the bits in
Bitmap with the modified data.LockbitsReturnsBitmapdataClass that describes the layout and position of the data in the locked array.

The bitmapdata class contains the following important properties;

  • Scan0The address in memory of the fixed data array
  • StrideThe width, in bytes, of a single row of pixel data. this width is a multiple, or possiblysub-multiple, of the pixel dimensions of the image and may be padded out to include a few more bytes. i'll
    Explain why shortly.
  • PixelformatThe actual pixel format of the data. This is important for finding the right bytes
  • WidthThe width of the locked Image
  • HeightThe height of the locked Image

The relationship of scan0 and stride to the array in memory is shown in figure1.

Figure 1: the basic layout of a locked bitmap Array

The stride property, as shown in figure 1, holds the width of one row in bytes. the size of a row however may not be an exact multiple of the pixel size because for efficiency, the system ensures that the data is packed into rows that begin on a four byte Boundary
And are padded out to a multiple of four bytes. this means for example that a 24 bit per pixel image 17 pixels wide wowould have a stride of 52. the used data in each row wocould take up 3*17 = 51 bytes and the padding of 1 byte wocould expand each row to 52 bytes
Or 13*4 bytes. A 4 bppindexed image of 17 pixels wide wocould have a stride of 12. nine of the bytes, or more properly eight and a half, wocould contain data and the row wocould be padded out with a further 3 bytes to a 4 byte boundary.

The data carrying portion of the row, as has been suggested above, is laid out according to the pixel format. A 24 bit per pixel image containing RGB data wocould have a new Pixel every 3 bytes, a 32 bit per pixel rgba every four bytes. pixel formats that contain
More than one pixel per byte, such as the 4 bit per pixel indexed and 1 bit per pixel indexed, have to be processed carefully so that the pixel required is not confused with it's neigbour pixels in the same byte.

Finding the right byte.

Because the stride is the width of a row, to index any given row or Y coordinate you can multiply the stride by the Y coordinate to get the beginning of a particle row. finding the correct pixel within the row is possibly more difficult and depends on knowing
The layout of the pixel formats. The following examples show how to access a particle pixel for a given pixel format.

  • Format32bppargbGiven X and Y coordinates, the address of the first element in the pixel isScan0 + (y * STRIDE) + (x * 4). This points
    The blue byte. The following three bytes contain the green, red and Alpha bytes.

  • Format24bpprgbGiven X and Y coordinates, the address of the first element in the pixel isScan0 + (y * STRIDE) + (x * 3). This points to
    Blue byte which is followed by the green and the red.

  • Format8bppindexedGiven the X and Y coordinates the address of the byte isScan0 + (y * STRIDE) + x. This byte is the index into the image palette.

  • Format4bppindexedGiven X and Y coordinates the byte containing the pixel data is calculatedScan0 + (y * STRIDE) + (X/2). The corresponding
    Byte contains two pixels, the upper nibble is the leftmost and the lower nibble is the rightmost of two pixels. the four bits of the upper and lower nibble are used to select the color from the 16 color palette.

  • Format1bppindexedGiven the X and Y coordinates, the byte containing the pixel is calculatedScan0 + (y * STRIDE) + (X/8). The Byte contains
    8 bits, each bit is one pixel with the leftmost pixel in BIT 8 and the rightmost pixel in bit 0. The bits select from the two entry color palette.

    Instance program, read data

    Bool cimgprocessdlg: fillimagebuf (byte * pbuffer) {assert (pbuffer); If (! Pbuffer) return false; // idb_bitmap_ks is a bitmap resource. A 1024*576*8 image gdiplus: bitmap * pbitmap = bitmap: fromresource (afxfindresourcehandle (makeintresource (idb_bitmap_ks ), rt_bitmap), makeintresource (idb_bitmap_ks); int nwidth = pbitmap-> getwidth (); int nheight = pbitmap-> getheight (); rect (0, 0, nwidth, nheight); bitmapdata * pbitmapdata = new bitmapdata; pbitmap-> lockbits (& rect, imagelockmoderead, pbitmap-> getpixelformat (), pbitmapdata); byte * pbyte = (byte *) pbitmapdata-> scan0; int noffset = pbitmapdata-> stride-nwidth; // eight-bit grayscale image trace1 ("pbitmapdata-> stride = % d \ n", noffset ); for (int row = 0; row <nheight; row ++) {for (INT Col = 0; Col <nwidth; Col ++) {* pbuffer = pbyte [0]; // pbuffer ++; pbyte + = 1;} pbyte + = noffset;} pbitmap-> unlockbits (pbitmapdata); // clean: delete pbitmapdata; // Delete pbitmap must be released; return 0 ;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.