Image synthesis

xiaoxiao2021-03-06  50

5.4 Image Synthesis 01-9-17 02:19:54

If you want to achieve an animation, such as a mouse run from the left side of the screen, the general book is called: First, make a mouse picture, and then draw a black and white mouse mask picture. First, the screen is processed by a mask diagram, and then processes the mouse picture with a mask, and finally put the treated mice on the screen, and the three Bitblt functions are handled before and after. And so that the process will flash significantly on the screen. To stop flashing, you must first make a compatible DC, first copy the screen picture to the compatible DC, and then copy the mouse after compatibility with the DC. It is necessary to use five Bitblt functions before and after. The picture is relatively small, if the picture is very big, that speed is simply "Today, this computer, the human face is red, and the people don't know where, the mouse is still slowly crawling." Is there any other solution?

How to achieve transparent bitmap

1. The most stupid approach: Direct the SETPIXEL function directly on the screen.

2. General methods: use the Bitblt function to make at least three operations.

3. The fastest way: write the screen directly.

4. The highest price ratio: write the data buffer directly.

Discussion in four ways:

1. For programmers who first engage in image processing, it seems that the way to paint in the past is the first choice. However, it has proven that this is the slowest method in the world, the most ideal animation effect of it is "a drop of last year. I have a tear, and today it streams to the mouth. "

2. Bitblt: The previous introduction is therefore, no longer introduced here.

3. Direct Write Screen: This method is used in this way. Programming in a Windows environment, which can only hang WING in the Windows 3.1 environment can be implemented, and the function created application () or use MicrosoftDirectX functions in Win95 or NT. achieve. This way we will introduce them. Not discussed here.

4. Write the data buffer: This method is smaller for the environment, no external software, compatible with 3.1 and 95, NT, speed can also, its principle is similar to the direct write screen, and can be convenient to directly write The screen is going. We will introduce this method here.

Read and write data buffer:

Everyone may still remember the class mydib introduced in the previous, there are two parameters, one is the BMP information head, one is the BMP data area, whether everyone can imagine how to modify the BMP data area, then display the image? ? This data area is the data buffer we have to use.

Transparent bitmap

To achieve a transparent bitmap, you must first have two pictures, one as a source diagram, one as a destination bitmap, the programmer should post the source chart to the destination chart, and to indicate what color to block the off For this reason, we add a function on the class mydib (MyDIB * Dib, Int x1, int y1, int x2, int y2, int x3, int y3, byte r1, byte g1, byte b1, byte r2, byte G2, BYTE B2), this function is a bit similar to the Bitblt function, it means: copy the data in the self-buffer to the buffer of class DIB, where from RGB (R1, G1, B1) to RGB The color of (R2, G2, B2) is transparent color, X1, Y1, X2, Y2, X3, Y3 are the target coordinate, copy range, source coordinate, and the meaning is the same as Bitblt. During the implementation of the show function, we first calculate the source data to be changed, the target data address, and then analyze if the data color to be copied belongs to the color, if it is a screen color, no copy data, otherwise copy.

Another transparent bitmap method

Transparent color is a relatively simple implementation, but sometimes there is another way of implementation, which is to specify the color index mode, we can specify a certain number to a certain number in a palette to a certain number transparent color . Therefore, in Class MyDIB, add a function show (MyDIB * DIB, INT X1, INT Y1, INT X2, INT Y2, INT X3, INT Y3, REGISTER BYTE X, Register Byte Y), the principle of this function and the previous The way is similar, but it is only four parameters than the previous way, and it is changed from color specified transparencies to specify transparent colors with color index.

Transparent bitmap refresh speed

Change the speed of the data buffer mode, or Bitblt speed fast? If the Bitblt is fast, the previous heart is not a rolling Yangtze River, for this, we must use an instance to analyze, establish a dialog-based program named TP, join the source program mybmp.cpp and mybmp. h Add #include "mybmp.h" in the TPDLG.H file header, add two member variables BMP1 and BMP2 in class CTPDLG. When the window is initialized, set the timer, open the file "1.BMP", "2.BMP", complete the copy and refresh process, compile and run the program during the timer message response process. We can see a "allTIME" parameter that shows that the refresh 256 bitmap needs to be about 20_21 seconds. Now Note Frequency Function BMP1.SHOW ((MYDIB *) & BMP2, 0, 0, 640, 480, 0, 0, 0, 0, 0, I, I, I), and then Refresh 256 positions It takes about 15_16 seconds, which is the time required to use the function StretchDibits. It can be seen that a transparent bitmap completion time is equivalent to a four bitblt time, the three Bitblt time (difference effects) of the Bitblt method, and the five Bitblt time (good effect) is much better. Of course, this is much better than the direct written screen.

Now replace the bottom function to BMP1.SHOW ((MyDib *) & bmp2, 0, 0, 640, 480, 0, 0, 0, i), we are not surprised to see the current time to refresh 256 bitmap is 16_17 seconds, almost It is believed that the read / write time of the buffer has been ignored.

Figure 5.3

Case Analysis

In this example, we have to achieve an animation, a background is a great good person, with a dog in front of it ran to run. The material needs five pictures, including one, four animation. We analyze its implementation:

In class cmoviedlg, we first define five images with statement BMP BMP [5], and then define a temporary picture with statement MYDIB TEMP. After the dialog initialization process function, the five bitmaps are read, and the timer is one hundred milliseconds, and the operation procedure in the timer response function is as follows: First write the background to the temporary picture, and then the puppy penetrate white Write Into the temporary picture, and finally write the temporary picture on the screen.

Figure 5.4

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

New Post(0)