Write an efficient image processing program v1.0 with VB write

zhaozj2021-02-16  55

First, why is this slow? Since the ancient world (seems to be exaggerated), some people have complained that the VB program is slow. In particular, image processing is considered to be a vB's penalty area. It is also that the data on the market's image processing on the market is to speak the formula, and then directly painted with PSET (or API function setpixel) (at least I have seen this). The effect is done, but the speed is slowly outrageous: a translucent merger of a 640 * 480 image is taken for 10 seconds; in Photoshop, as long as a transparency of the layer is set, the translucent effect is immediately present. No wonder some people say VB's gossip.

But this does not mean that VB cannot write a high-speed image processing program, and the speed is slow because there is no correct method.

Starting from VB5, it is possible to compile into an EXE file with this unit code, so there is no problem with code execution speed. So what is the slow speed? Pset and SetPixel! PSET converts the coordinates of floating point into pixel units, and then handed over to SetPixel processing. And setpixel, coordinate system transformation, critical area judgment, match color matches the closest of the device support, and finally addressed according to different color formats to write the color to its bit. After so many layers, the speed is not slow.

So how can I improve the processing speed? With DIB, you can use the memory where the bitmap is located, and the speed can be greatly improved. Take a look now

Example program, this is just a simple color demo.

CPU: Celeron 333; Memory: PC100 (very old concept) SDRAM, 128MB; Unit: millisecond

Windows 98Windows XP Description VB_PSet1,199.4553786.1885 use PSet painting in VB VB_SetPixel872.3621451.3712 use VB_DIB8.22188.2226 SetPixelV painting in VB using DIBSection analog hands painted in VB_DIB_Ptr9.67839.4420 VB VB use DIB painting of VC (Debug) 6.68966 .6503VC Write (Debug Version) VC (Release) 3.27363.6247VC Write (Release Version) From this table:

1.vc is twice as fast than VB_DIB, VB_DIB_PTR, because the array of SafeArray structures is slower than the real pointer, but it is not 70 ~ 100 times of some people.

2. VB_DIB_PTR is slower than VB_DIB, because the analog pointer is originally an array of SafeArray structures, and the analog pointer needs to operate two arrays, so the speed is slower;

3. True difference is 70 ~ 100 times is VB_PSET and VB_SETPIXEL, especially VB_PSETs, 145 times the VB_DIB, in Windows 98.

The above can be discussed, the speed is that setPixel is very inefficient, not the problem of VB. Although VC is indeed faster, I wrote this article is not to discuss the speed limit (otherwise this article will be renamed "how to use

Compilation of high-speed image processing programs "), but to tell you how to write image processing programs that can be processed in VB.

Second, DIB structure Before Windows 3.0, the Windows system uses DDB (devices related bitmap). DDB does not have a palette, the color of the display is dependent on hardware, and it is very inconvenient to handle color. So Microsoft redefines the BMP file format (BMP 3.0) in Windows 3.0, so that it supports device-free bitmap - that is, DIB. Today, the BMP version number has risen to 5.0 (Windows NT 4.0, Windows 95 defines BMP 4.0, Windows 98, Windows 2000 defines BMP 5.0), but the basic structure is not changed - still BMP file header and DIB consists of:

BMP file BitmapFileHeaderbmp file head DibbitMapInfoHeader bitmap information head bitmapinforgbquad [] palette bitmap data

(

# 代 代 代 代 代 项目 项目 (= 0) project)

BMP file header - BitmapfileHeader

Prototype definition: typedef struct tagbitmapfileHeader {// BMFH

Word bftype;

DWORD BFSIZE;

Word bfReserved1;

Word bfReserved2;

DWORD BFOFFBITS;

} BitmapfileHeader;

VB declaration: Type BitmapFileHeader

BFTYPE (0 to 1) as Byte

BFSIZE AS Long

BFRESERVED1 AS INTEGER

BFRESERVED2 AS INTEGER

Bfoffbits as long

End Type

Description: The type of bftype indicates that the "BM" bFSize # indicates the size of the file, including bitmapfileHeaderbfreserved1 reserved, = 0bfReServed2 reserved, = 0BFFFBITS # From the file header to the offset byte number of file heads

File Information Head - BitmapInfoHeader

Prototype Definition: TypedEf struct tagbitmapinfoheader {// bmih

DWORD Bisize;

Long biwidth;

Long biheight;

Word biplanes;

Word bibitcount;

DWORD BICOMPRESSION;

DWORD BisizeImage;

Long Bixpelspermeter;

Long biypelspermeter;

DWORD BICLRUSED;

DWORD BICLRIMPORTANT;

} BitmapInfoHeader;

VB declaration: Type BitmapInfoHeader

Bisize as long

BIWIDTH AS Long

Biheight As Long

BIPLANES AS INTEGER

BIBITCOUNT AS INTEGER

BiCompression As Long

BisizeImage as long

Bixpelspermeter As Long

Biypelspermeter As Long

BICLRUSED AS Long

BICLRIMPORTANT AS Long

End Type

Description: The size of the BisizeBitmapInfoHeader structure. BMP has multiple versions, depending on Bisize. B. Z3.0: BitmapInfoHeader (= 40) BMP4.0: Bitmapv4Header (= 108) BMP5.0: Bitmapv5Header (= 124) Biwidth bitmap height, unit is pixel Biheight bit The width of the figure is the number of positions of the pixel BIPLANES device. Now is the number of color digits of 1biBitcount images 0: When bicompression = bi_jpeg must be 0 (BMP 5.0) 1: Monochrome bit Figure 4:16 Color bit Figure 8: 256 Collection Figure 16: Enhance the color bitmap, default 555 Format 24: Real color bitmap 32:32 bitmap, by default, Windows does not process up to 8 bits, you can use it as its own alpha channel BiCompression compression mode BI_RGB: no compression Bi_RLE8: stroke encoding compression, Bibitcount must be equal to 8 BI_RLE4: BiBITCount must be equal to 4 bi_bitfields: specify the RGB mask, BIBITCount must be equal to 16,32 bi_jpeg: JPEG compression (BMP 5.0) BI_PNG: PNG Compression (BMP 5.0) BisizeImage # actual bitmap data occupied (BiXPRESSION = Bi_RGB can be omitted) The horizontal resolution of the Bixpelspermeter # target, the unit is the vertical resolution of the number of pixels per meter, and the unit is the number of pixels per meter biclrused # used in the number of colors ( It is only valid when BIBITCOUNT is equal to 1, 4, 8). If the item is 0, the number of color numbers is 2 ^ bibitCountBiRIMPORTANT # important colors. If the item is 0, it means that all colors are important palettes.

There is only a palette when BiBitcount is equal to 1, 4, and 8. The palette is actually an array, and the number of elements is determined by BIBITCOUNT and BICLRUSED.

Prototype definition: typedef struct tagrgbquad {// RGBQ

BYTE RGBBLUE;

BYTE RGBGREEN;

BYTE RGBRED;

BYTE RGBRESERVED;

} RGBQUAD;

VB Declaration: Private Type Rgbquad

Rgbblue as byte

Rgbgreen as byte

Rgbred as byte

RGBRESERVED AS BYTE

End Type

Description: RGBBLUE Blue Component RGBGreen Green Component RGBred Red Component RGBRESERVED # 保, = 0

Bitmap data

◆ Scanning: The image data of a row is called a scanning line. The length of a scan line must be a multiple (byte), if not, you need to make up. Calculation formula: linebytes = (BiWidth * BibitCount 31) and & hffffffe0) / 8 Since the BMP settor believes that the mathematical coordinate system is more important, the scanning line of DIB is in reverse storage (relative to the screen coordinate system), That is, the first line on the screen is the last line of DIB bitmap data. ◆ 1 bit color: represents one pixel with 1 bits, so one byte can represent 8 pixels. The coordinates start from the leftmost (highest), not the lowest in general. In the form of memory, the placement is as follows: byte 0 ... bits 76543210 pixels 01234567 ◆ 4 bit color: A 4-bit represents a pixel, so one byte can represent two pixels. The coordinates start from the leftmost (highest), not the lowest in general. The placement form is as follows:

Byte 0 ... Bit 76543210 pixel 01 pixel bit 32103210 ◆ 8 bit color: A pixel is represented by 8 bits, so one byte can only represent one pixel. The placement form is as follows:

Byte 01 ... pixel 01 ◆ 16 bit color: represents a pixel with 16 bits, so the two bytes can represent 1 pixels. By default, the 16-bit DIB is 555 format, the highest bit is invalid (this is a gospel for VB because VB does not have 16-bit non-symbolic types). The placed form in memory is as follows (PC is the low byte):

Byte 0123 ... Bit 76543210765432107654321076543210 pixel 01RGBGBXRGBXRGRGB bit 21043210043210432104321004321043 ◆ 24 bit color: 24 bits represent one pixel, three bytes may represent 1 pixel. Note that its order is BGR, not a traditional RGB. The placement form is as follows:

Byte 012345 ... pixel 01RGBBGRBGR ◆ 32 bit color: A pixel is represented by 32 bits, so four bytes can represent 1 pixels. Note that most of the GDI functions do not process Alpha channels (Alphablend support). The placement form is as follows:

Byte 01234567 ... pixel 01RGBBGRABGRA

Third, DIB access function

SetDibitStodevice

Prototype definition: int setDibitStodevice

HDC HDC, // Handle to Device Context

INT XDEST, // X-Coordinate of Upper-LEFT CORNER OF DEST. RECT.

INT YDEST, // Y-Coordinate of Upper-Left Corner of Dest. Rect.

DWORD dwidth, // source reccTangle Width

DWORD dwheight, // Source Rectangle Height

INT XSRC, // X-Coordinate of Lower-Left Corner of Source Rect.

INT YSRC, // Y-Coordinate of Lower-Left Corner of Source Rect.

Uint ustartscan, //first scan line in array

UINT CSCANLINES, // Number of Scan LinesConst Void * LPVBITS, / / ​​Address of Array with Dib Bits

Const BitmapInfo * lpbmi, // address of structure with bitmap info.

Uint fucoloruse // RGB or PALETTE INDEXES

);

VB statement: Declare Function SetDIBitsToDevice Lib "gdi32.dll" (ByVal hDC As Long, ByVal XDest As Long, ByVal YDest As Long, ByVal dwWidth As Long, ByVal dwHeight As Long, ByVal XSrc As Long, ByVal YSrc As Long, ByVal uStartScan As long, Byval Cscanlines As long, byval fucoloruse as long) AS Long Description: Copy a full or part of the data that is unrelated to the device to a device. This function defines a target rectangle in the device to receive bitmap data. It also defines a source rectangle in DIB to extract data return values ​​from it: If the function is executed, return the number of scan lines to be copied; if returns constant GDI_ERROR, indicates an error parameter: HDC a handle of a device scene. This scene is used to receive bitmap data XDest specifying the left upper left corner X coordinate YDEST of the drawing area. The left upper corner Y coordinate dwidth specifies the height dwheight of the drawing area DWHEIGHT specifies the width XSRC rectangle of the drawing area XSRC rectangle in the DIB's starting point X coordinate YSRC rectangle The starting point y coordinate the number of the first scan line in USTARTSCANLPVBITS. If the Biheight field of the LPBMI bitmapinfoheader is positive, then this scan line will calculate from the bottom of the bitmap; if it is a negative number, the number of scan lines that CSCANLES want to copy from the top Point points to a pointer to a buffer. This buffer contains bitmap data in DIB format; this format is a structure of LPBMI points to BitmapInfo (compatible with BMP4 / 5), which describes the format and color of DIB, and describes Fucoloruse.

The DIB_PAL_COLORS color table is an integer array containing the index dib_rgb_colors color table related to the color palette currently selected in the HDC device scene, contains RGB color.

Stretchdibits

Prototype definition: int StretchDibits (

HDC HDC, // Handle to Device Context

INT xDest, // x-coordinate of Upper-Left Corner of Dest. Rectangle

Int YDEST, // Y-Coordinate of Upper-Left Corner of Dest. Rectangle

Int NDestWidth, // Width of Destination Rectangle

Int NDestheight, // HEIGHT OF Destination Rectangle

INT xsrc, // x-coordinate of Upper-Left Corner of Source Rectangle

INT YSRC, // Y-Coordinate of Upper-Left Corner of Source Rectangleint NsrcWidth, // Width of Source Rectangle

INT NSRCHEIGHT, // HEIGHT OF SOURCE Rectangle

Const void * lpbits, // address of bitmap bits

Const BitmapInfo * LPBITSINFO, / / ​​Address of Bitmap Data

Uint iusage, // usage flags

DWORD DWROP // Raster Operation Code

);

VB statement: Declare Function StretchDIBits Lib "gdi32" (ByVal hDC As Long, ByVal XDest As Long, ByVal YDest As Long, ByVal nDestWidth As Long, ByVal nDestHeight As Long, ByVal XSrc As Long, ByVal YSrc As Long, ByVal nSrcWidth As Long , Byval NSRCHEIGHT As long, LPBITS AS ANY, LPBITSINFO ANY, BYVAL WUSAGE AS Long Description: Create a bitmap-related bitmap-related bitmap according to a bitmap-independent bitmap: execution Successfully returned the bitmap section, zero indicates failure parameters: HDC a device scene handle, the device scene defines the left upper left corner XDest to create the configuration information of the device to create a drawing area to specify the left upper left corner of the drawing area. Y Coordinate NDestWidth Specifies the Height NDestHeight of Drawing Area NDestHeight Specifies the Width XSRC Rectangle of Drawing Area XSRC Rectangles in DIB The starting point X coordinate YSRC rectangle in DIB The starting point Y coordinate NsrcWidth specifies the left upper corner X coordinate nsrcheight of the in-situ diagram area NSRCHEight specified in place diagram The upper left corner Y coordinate LPBITS of the area is pointed to a buffer. This buffer contains bitmap data in DIB format; this format is a structure in which LPBITSINFO specified by lpbitsInfo points to BitmapInfo (a ANY to be compatible with BMP4 / 5), a structure for DIB's format and color.

The DIB_PAL_COLORS color table is an integer array containing the index dib_rgb_colors color table related to the palette currently selected in the HDC device scene, contains RGB color dwrop's raster operations.

CreatedIBITMAP

Prototype definition: HBitmap CreatedIbitmap

HDC HDC, // Handle to Device Context

Const BitMapInfoheader * lpbmih, // Pointer to Bitmap Size and Format Data

DWORD FDWINIT, // Initialization Flag

Const void * lpbinit, // Pointer to Initialization Data

Const BitmapInfo * lpbmi, // Pointer to Bitmap Color-Format Data

Uint Fuusage // Color-Data usage

);

VB Declaration: Declare Function Createdibitmap LIB "GDI32" (Byval HDC As Long, LPBMIH As Any, Byval Fdwinit As Long, LPBINIT AS ANY, LPBMI AS ANY, BYVAL Fuusage As Long) AS Long Description: A bit diagram with an installation All or some data copied directly to a device. This function defines a target rectangle in the device to receive bitmap data. It also defines a source rectangle in DIB to extract data return values ​​from it: the number of execution returns the number of scan lines, and zero means fail. Getlasterror parameters are set: HDC handle of a device scene. This scene is used to receive bitmap data LPBMIHbitMapInfoHeader (in a compatible BMP4 / 5, ANY), a structure fdwinit describing DIB's format should be initialized if you should initialize bitmap data, then set to zero. If set to CBM_INIT, it is indicated that the bitmap is initialized to the bitmap according to the LPBINIT and LPBMI parameters to the pointer to a buffer. This buffer contains bitmap data in DIB format; this format is a structure fuusagedib_pal_colors color to bitmapInfo specified by LPBMI points to BitMapInfo (ANY to compatible BMP4 / 5), format and color to DIB. The table is an integer array, which contains the index dib_rgb_colors color table related to the palette currently selected in the HDC device scene contains RGB color.

Createdibsection

Prototype definition: HBitmap Createdibsection

HDC HDC, // Handle to Device Context

Const BitmapInfo * lpbmi,

// Pointer to Structure Containing Bitmap Size, Format, And Color Data

UINT IUSAGE, // Color Data Type Indicator: RGB VALUES OR PALETTE INDEXES

Void * ppvbits, // Pointer to Variable To Receive a Pointer to the bitmap's bit value

Handle Hsection, // Optional Handle To a file mapping object

DWord dwoffset // offset to the bitmap bit value forin the file mapping object

);

VB statement: Declare Function CreateDIBSection Lib "gdi32" (ByVal hDC As Long, lpbmi As Any, ByVal iUsage As Long, ByRef ppvBits As Long, ByVal hSection As Long, ByVal dwOffset As Long) As Long Description: CreateDIBSection to create a special DIB, called Dib Item, and then return a handle of a GDI bitmap. It provides the best feature of DIB and GDI bitmaps. This way we can directly access the DIB's memory, which can use the bitmap sector and memory device environment, we can even call the GDI function in the DIB to draw the return value: perform successfully returns the handle of the DibSECTION bitmap, zero means failure. Getlasterror parameters are set: HDC handle of a device scene. If DW is set to DIB_PAL_COLORS, then the DIB color table will initialize the color of the logical palette to point to BitmapInfo (in compatible BMP4 / 5 is a ANY), this structure initializes the configuration of the bitmap to be created. Data IUSAGEDIB_PAL_COLORS color table is an integer array, which contains index dib_rgb_colors color tables related to the color palette currently selected HDC device scene contains RGB color PPVBITS to get the memory address of the DibSECTION data hSECTION pointing to a file mapping object. Optional handles, the bitmap will be created. If set to zero, Windows automatically assigns memory dwoffset, such as specifying the handle, using this parameter to point the offset in the file mapping object.

GetDibits

Prototype definition: int getDibits

HDC HDC, // Handle to Device Context

Hbitmap HBMP, // Handle to Bitmap

Uint uStartscan, //first scan line to set in destination bitmap

Uint Cscanlines, // Number of Scan Lines To Copy

LPVOID LPVBITS, / / ​​Address of Array for Bitmap Bits

LPbitMapInfo lpbmi, // address of structure with bitmap data

Uint uusage // RGB or Palette Index

);

VB Statement: Declare Function GetDibits LIB "GDI32" (Byval HBMP As Long, Byval Cscanlines As Long, Byval Cscanlines As long, LPVBITS ANY, LPBMI AS ANY, BYVAL UUSAGE AS long): This function DIB bitmap data is obtained by the GDI bitmap using the application-to-memory. With this function, the format of the DIB can be controlled, and the number of bits of the color can be made, and it can be specified whether it is compressed. If a compressed method must be called, the function must be called twice, once in order to get the required memory, another in order to obtain the bitmap data return value: The number of scan lines is returned, and zero means failed. Getlasterror parameters are set: HDC defines the handle of the handle of the HBMP source bitmap for the device for bitmap Hbitmap's configuration information. Ustartscan wants to copy the number of scan lines to the first scan line in the DIB. Quantity LPVBITS points to a pointer to a buffer. This buffer contains bitmap data described in DIB format; this format is the LPBMI specified by LPBMI points to BitmapInfo (a ANY is compatible with BMP4 / 5). One structure for DIB's format and color. In the BitMapInfoHeader structure, all fields from Bisize to BiCompression must initialize the uusagedib_pal_colors color table is an integer array, which contains the index dib_rgb_colors color table related to the color palette currently selected in the HDC device scene contains RGB colors.

SetDIBITS

Prototype definition: int setDibits

HDC HDC, // Handle to Device Context

Hbitmap HBMP, // Handle to Bitmap

Uint ustartscan, // starting scan line

Uint Cscanlines, // Number of Scan Lines

Const void * lpvbits, // array of bitmap bits

Const BitmapInfo * lpbmi, // address of structure with bitmap data

UINT uusage // type of color indexes to us

);

VB Statement: Declare Function SetDibits LIB "GDI32" (Byval HBMP As Long, Byval Cscanlines As Long, Byval Cscanlines As Long, LPVBITS ANY, LPBMI AS Any, BYVAL UUSAGE AS Long) AS Long Description: Will come from The binary bit with the device-independent bitmap is copied to a bitmap related to the device. Return value: the execution is successful, returns the number of scan lines, and zero means fail. Getlasterror parameters are set: HDC defines the handle of the handle of the HBMP source bitmap for the device for bitmap Hbitmap's configuration information. Ustartscan wants to copy the number of scan lines to the first scan line in the DIB. Quantity LPVBITS points to a pointer to a buffer. This buffer contains bitmap data described in DIB format; this format is the LPBMI specified by LPBMI points to BitmapInfo (a ANY is compatible with BMP4 / 5). One structure for DIB's format and color. In the BitMapInfoHeader structure, all fields from Bisize to BiCompression must initialize the uusagedib_pal_colors color table is an integer array, which contains the index dib_rgb_colors color table related to the color palette currently selected in the HDC device scene contains RGB colors.

GetDibcolortable

Prototype Definition: UINT GETDIBCOLORTABLE (

HDC HDC, // Handle To Device Context Whose Dib is of Interest

Uint uStartIndex, // Color Table Index of First Entries To Retrieve

Uint Centries, // Number of Color Table Entries To Retrieve

Rgbquad * Pcolors // Pointer to Buffer That Receives Color Table Entries

);

VB Declaration: Declare Function GetDibcolortable LIB "GDI32" (BYVAL USTARTINDEX As Long, Byval Centries As Long, PCOLORS AS RGBQUAD) AS Long Description: Net Color Table Information Netherly Nether Color Table Information Netherly Nether The number of color entries and zero expires. Getlasterror parameters will be set: HDC has been selected for a DibSECTION object's device scenario USTARTINDEX color table to retrieve the first entry of the index CENTRIES to retrieve the number of items to retrieve the number of items of the PCOLORS This structure is used to load color table information Entry

SetDibcolortable

Prototype definition: uint setdibcolortable

HDC HDC, // Handle To Device Context Whose Dib is of Interest

Uint uStartIndex, // color Table Index of First Entries TO SET

Uint Centries, // Number of Color Table Entries To Set

Const Rgbquad * Pcolors // Pointer TO Array of Color Table Entries

);

VB Declaration: Declare Function SetDibcolortable LIB "GDI32" (Byval HDC As Long, BYVAL CENTRIES AS Long, PCOLORS AS RGBQUAD) AS LONG Description: Color table information is obtained from the Dibsection of the selected device scene: take The number of color entries and zero expires. Getlasterror parameters will be set: HDC has been selected for a DibSECTION object's device scenario USTARTINDEX color table to retrieve the first entry of the index CENTRIES to retrieve the number of items to retrieve the number of items of the PCOLORS This structure is used to load color table information When you use the DIB write image handler, you should first clarify that the DIB is not an image processing algorithm, but a drawing method. The processing algorithm is higher than the DIB high, manage coordinates and colors; and DIB is just to draw. Therefore, the efficiency of the processing algorithm is the key to the speed.

Drawing images with DIB is not more than drawing the difference between Pset / SetPixel, it is just a change in the coordinate operation. Many people know that the pointer is a dangerous thing, because it can access memory directly, if the pointer is not careful, Windows immediately reports general protection errors. Therefore, be careful when the address is calculated, pay attention to keep it at any time, because the incidence of illegal operation at this time is very high, otherwise the hard work code is not blamed, don't blame, I don't want to blame.

Ok, start now!

Since the processing algorithm is guided, the VB_PSET is now explained. All code is in frmmain.frm. The code of other procedures can not be seen, and now you will focus on "Drawit", which is drawn.

In fact, my demo program is quite simple: R component is extended in the horizontal direction, and the g component is extended in the vertical direction, and the B component is scrolling from right left. what? ! What does "and & hff" mean? ! This is the foundation ... (slightly & hffff). "& H" means a hexadecimal number, while And represents a bit bit. & HFF is a binary "1111 1111", just covering the low 8 bits, then uses and the bit position and, only the low 8 bits, 8 digits required to RGB components are just in line (for "(J K) and & Hff "For rolling effects).

If not understand

Two paragraphs of Then Goto

it is good! Open VB_DIB now. (Since 24 bits can specify RGB components directly, this is 24-bit DIB)

Have a little fainting in the "DIB structure" in front of it? In fact, DIB is nothing, is a BitmapInfo structure that states bitmap information and a data buffer that stores bitmap data. Top multiple uses setDibitStodevice, so VB_DIB is just more setdicstodevice compared to VB_PSET, BitmapInfoHeader (24-bit DIB has no adjustment Sloth, so use BitMapInfoHeader to in line with some constants. Since this demo program does not need to change the image size and color depth, you can use the variable as the form-level variable, and then initial in Form_Load. Since DIB does not apply for a resource to the system (the memory allocation of the array is VB, it will be automatically released), so it is not necessary to write release code.

Now look at Drawit.

1. Although the coordinates can be mapped by the address by point, this efficiency is too low, and the continuity of coordinate processing can be optimized. 2. Since I use an array here, I use the (array element) index instead of the address.

3. At the beginning, you should pay attention to DIB is in advance, set the index to the index of the first line of the first pixel.

4. Since the RGB order of DIB is B, G, R, the order of "MAPDATA (Curidx ?) Is 2, 1, 0.

5. After setting up a color of a pixel, pay attention to the index of the index to the next pixel.

6. Since DIB is inversely stored, it is moved to the next scan line is "lineidx = lineidx - linebytes"

"Well, the code understands, press F5 to run and see the effect."

"Hey? How much is it not fast?"

This is because the program is operated in the VB environment, and the interpretation method is low to perform the image processing requires a large-scale cycle and a large number of arithmetic operations, so it is necessary to compile (native code) EXE Run . At this point, you should pay attention to compilation optimization, you can increase the "advanced optimization" all hook, speed can be increased by about 20%.

5. Although using DibSecion and analog pointers Although there is a getDibits / setDibits function, Data exchange between DIBs and GDI bitmaps is still very inconvenient, and the GDI function needs to be invoked during the special handling process. And even if you are not afraid of trouble, this is very low. So Windows provides us with DibSecion. Dibsection is a special DIB, which can operate directly to the bitmap data directly like DIB, which can also be selected to draw DC, which can be drawn with GDI functions, very flexible. However, use DibSecion under VB, because create a DibSection with CREATEDIBSECTION, the address of bitmap data is obtained, and VB does not have a pointer.

Fortunately, the array analog pointers of the SafearRay structure can be used under VB. With regard to the principles, methods, methods of simulation pointers, more information on the Internet, such as

Adambear article "

VB really can't think of the four: VB pointer Sunflower Collection SafeArray. But these articles are just general applications, can't change the address like a real pointer (they are all modified). In image processing Due to frequent point operations, the function of "change the address like the real pointer" is important.

In fact, "I will change the address like the real pointer" without technical difficulty (for people who have learned to simulate pointers), I want to get: I don't want to get: use an array (set up pDataPtr) to control the address of the analog pointer (set up PDATA), This can use the PDATAPTR to control the address, access data with PDATA.

Since two simulation pointers need to be established, the code is more annoying, so I want to write a "analog pointer code generator" (in (imgtest.zip) Makesaptr directory). Although there are many kinds of simulated pointers established, most of them need temporary variables to save the address, not suitable for bulk generation, so I only change this method (refer to the "simulated pointer code generator" automatically generated). This method may cause confusion to people who learn to simulate pointers by watching adambear, because adambear is not explained when the value returned by Varptrarray is the address of the SafeArray address: Varptrarray returns the value of VB storage array information. The address of the variable can make the array with another Safearray by changing the value of the variable.

Now look at VB_DIB_PTR: 1. In order to demonstrate Dibsection, like Hbitmap, I created a DC in Form_Load, and select DibSecion to DC. At the same time, I wrote release code in Form_unload.

2. In Drawit, pay attention to the code of the processing section does not have much difference with VB_DIB, but only reform index is changed to the address calculation.

转载请注明原文地址:https://www.9cbs.com/read-27468.html

New Post(0)