"Go" process an image using the LockBits method

Source: Internet
Author: User

Many image processing tasks are instantly the simplest type of file conversion, such as converting from 32-bit depth to 8-bit depth, and getting a pixel array directly is much more efficient than using methods such as GetPixel and SetPixel.

You may find that Dotnet uses a hosting mechanism, and in most cases Microsoft will recommend you to use managed code for convenience and security. In practice, it is rare to manipulate data blocks in memory directly, although image processing is one of the few things that can happen because the inefficient use of managed code is unbearable, especially for large images, where we discuss a new approach.

How to use unmanaged code is language-dependent, in C # we can invoke pointers through the unsafe keyword, which directly manipulate the in-memory bitmap data; VB uses the method in the Marshal class, which results in a partial loss of performance and therefore less efficiency than the former.

Lock Bit Stream

The bitmap class uses the LockBits and Unlockbits methods to store the bitmap's data matrix in memory, manipulate it directly, and finally replace the original data in the bitmap with the modified data. LockBits returns the position and distribution of the BitmapData class with the described data in the locked matrix.

The BitmapData class includes several important attributes:

    • Scan0: The address of the data matrix in memory.

    • Stride: The line width in the data matrix, in bytes. Several bytes may be extended, which is described later.

    • PixelFormat: Pixel format, which is important for locating bytes in a matrix.

    • Width: The breadth of the bitmap.

    • Height: The width of the bitmap.

See the specific relationship:

As shown, the Stride property represents the line width of the bitmap data matrix, in bytes. For efficiency reasons, the line width of a matrix is not just an integer multiple of the number of pixels per line, and the system will often encapsulate it as an integer multiple of 4. For example, for an image with a 24-bit depth of 17 pixels, its Stride property is 52, the amount of data per row is 17*3=51, and the system automatically encapsulates a byte, so its stride is 52byte (or 13*4byte). For a 4-bit index graph with a width of 17 pixels, the STRIDE is 12, where 9byte (exactly 8.5 bytes) is used to record data information, and each line is automatically added 3 (3.5) byte to guarantee that it is an integer multiple of 4.

The distribution of specific data varies by its pixel format. A 24-bit deep image contains a set of RGB information every 3 bytes, and a 32-bit deep image contains a set of Rgba information every 4 bytes. Pixel format, which contains multiple pixels per byte, such as a 4-bit indexed image or a 1-bit indexed image, must be carefully processed to ensure that neighboring byte in the same byte is not confused.

accurate positioning of pointers

    • 32-bit RGB: Assuming X, Y is the coordinate of the pixel in the bitmap, its address in memory is scan0+y*stride+x*4. The pointer points to blue, followed by the green, red, and alpha components.

    • 24-bit rgb:scan0+y*stride+x*3. At this point the pointer is blue and then green and red, respectively.

    • 8-bit index: scan0+y*stride+x. The current pointer points to the color palette of the image.

    • 4-bit index: scan0+y*stride+ (X/2). The current pointer refers to a byte that consists of two pixels, a high-and low-index 16-tone color dial, where the high point represents the left pixel, and the low position represents the right pixel.

    • 1-bit index: SCAN0+Y*STRIDE+X/8. Each digit in the byte referred to by the current pointer represents the index color of a pixel, the palette is two colors, the leftmost pixel is 8, and the rightmost pixel is 0.

using iterators between pixels

The following example sets the blue component of all pixels in a 32-bit deep image to the maximum (255):

BitmapData BMD=BM. LockBits (New Rectangle (0, 0, ten, ten), System.Drawing.Imaging.ImageLockMode.ReadOnly, BM. PixelFormat);

int pixelsize=4;

for (int y=0; Y<bmd. Height; y++)

{

byte* row= (BYTE *) BMD. scan0+ (Y*bmd. Stride);

for (int x=0; X<bmd. Width; X + +)

{

row[x*pixelsize]=255;

}

}

Processing 4-bit index graph, high and low bits should be processed separately, the code is as follows:

int offset = (Y * BMD. Stride) + (x >> 1);

byte Currentbyte = ((BYTE *) BMD. SCAN0) [offset];

if ((x&1) = = 1)

{

Currentbyte &= 0xF0;

Currentbyte |= (Byte) (ColorIndex & 0x0F);

}

Else

{

Currentbyte &= 0x0F;

Currentbyte |= (Byte) (ColorIndex << 4);

}

((BYTE *) BMD. SCAN0) [Offset]=currentbyte;

Code to process 1-bit indexes:

byte* p= (byte*) BMD. Scan0.topointer ();

int Index=y*bmd. Stride+ (x>>3);

byte mask= (Byte) (0x80>> (x&0x7));

if (pixel)

P[index]|=mask;

Else

P[index]&= (Byte) (MASK^0XFF);

Finally, after all the processing has been done, the horse should not forget to use the unlockbits command to unlock.

The original is here http://blog.sina.com.cn/s/blog_4e3e2ce4010009on.html

"Go" process an image using the LockBits method

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.