GDI+ 使用LockBits和指针加快处理速度

简介:

Lock up your bits

The Bitmap class provides the LockBits and corresponding UnlockBits methods which enable you to fix a portion of the bitmap pixel data array in memory, access it directly and finally replace the bits in the bitmap with the modified data. LockBitsreturns a BitmapData class that describes the layout and position of the data in the locked array.

The BitmapData class contains the following important properties;

  • Scan0 The address in memory of the fixed data array
  • Stride The 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.
  • PixelFormat The actual pixel format of the data. This is important for finding the right bytes
  • Width The width of the locked image
  • Height The height of the locked image

The relationship of Scan0 and Stride to the array in memory is shown in figure1.

lockin1.gif

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 would have a stride of 52. The used data in each row would take up 3*17 = 51 bytes and the padding of 1 byte would expand each row to 52 bytes or 13*4 bytes. A 4BppIndexed image of 17 pixels wide would have a stride of 12. Nine of the bytes, or more properly eight and a half,  would contain data and the row would 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 would 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 particular 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 particular pixel for a given pixel format.

  • Format32BppArgb Given X and Y coordinates,  the address of the first element in the pixel is Scan0+(y * stride)+(x*4). This Points to the blue byte. The following three bytes contain the green, red and alpha bytes.

  • Format24BppRgb Given X and Y coordinates, the address of the first element in the pixel is Scan0+(y*Stride)+(x*3). This points to the blue byte which is followed by the green and the red.

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

  • Format4BppIndexed Given X and Y coordinates the byte containing the pixel data is calculated as Scan0+(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 colour from the 16 colour palette.

  • Format1BppIndexed Given the X and Y coordinates, the byte containing the pixel is calculated by Scan0+(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 colour palette.

示例程序:

 

}


本文转自feisky博客园博客,原文链接:http://www.cnblogs.com/feisky/archive/2009/11/01/1593985.html,如需转载请自行联系原作者


相关文章
|
2月前
|
缓存 算法 编译器
C/C++编译器内存优化技术:内存优化关注程序对内存的访问和使用,以提高内存访问速度和减少内存占用。
C/C++编译器内存优化技术:内存优化关注程序对内存的访问和使用,以提高内存访问速度和减少内存占用。
44 0
|
2月前
|
C语言
指针与内存
指针与内存
17 0
|
5月前
|
存储 编译器 人机交互
深度剖析整形数据在内存中的存储
深度剖析整形数据在内存中的存储
54 0
|
6月前
|
缓存 网络协议 Linux
零拷贝技术:减少数据复制和上下文切换,提高网络传输效率(下)
本章节主要讨论了如何通过零拷贝技术来优化文件传输的性能。零拷贝技术主要通过减少用户态和内核态之间的上下文切换次数和数据拷贝次数来提高性能。具体来说,介绍了两种实现零拷贝的方式:mmap + write和sendfile。使用mmap + write可以减少一次数据拷贝过程,而使用sendfile系统调用可以进一步减少系统调用和数据拷贝次数。此外,还介绍了如果网卡支持SG-DMA技术,可以通过DMA将数据直接拷贝到网卡缓冲区,实现真正的零拷贝。通过这些优化方法,可以显著提高文件传输的性能。
零拷贝技术:减少数据复制和上下文切换,提高网络传输效率(下)
|
6月前
|
算法 网络协议 调度
零拷贝技术:减少数据复制和上下文切换,提高网络传输效率(上)
在本次讨论中,我们确实只是提到了DMA技术在文件传输过程中的重要作用,并对零拷贝技术进行了简要介绍。然而,网络传输中存在的问题和优化方法是一个庞大的话题,涉及到诸多方面。因此,我决定将这些问题的详细讨论留到下一篇文章中,以便更全面地探讨网络传输的优化。我希望通过这样的讨论,能够为读者提供有益的信息和思路,感谢大家的阅读和关注,期待在下一篇文章中与大家再次交流和分享关于网络传输的优化问题。
|
9月前
|
存储 C语言
内存,指针
内存,指针
40 0
|
10月前
|
存储 小程序 编译器
数据在内存中存储的现象
数据在内存中存储的现象
68 0
|
12月前
|
存储 C语言
内存的读写过程、现实模型及指针
内存的读写过程、现实模型及指针
116 0
内存的读写过程、现实模型及指针
|
编译器 C++
结构体的初步认识以及其内存的计算
结构体的初步认识以及其内存的计算
90 0
结构体的初步认识以及其内存的计算