Under the embedded system, due to the limitations of the embedded system itself, it is very difficult to implement the functions and effects that can be easily implemented under Windows. Moreover, under WinCE, because Microsoft can ensure that the WinCE OS operating system is small enough, reduce many Windows components, only the core part of the OS is. In this case, the WinCE operating system can be reduced to a small enough. However, this approach to Microsoft has brought great inconvenience to our programmers. It caused the original We'm to write to the letter, and the API came with the API changed so unfamiliar, we had to be free, find an alternative method to achieve the same function. At the same time, this also requires our programmers to understand more and more.
For semi-transparent, the translation of embedded type is a problem that plagues many embedded programmers. For the author, I have been looking for a large number of domestic, foreign websites, and I have consulted dozens of years have many years of embedded work. Experience personnel, but no one can give me a good solution. Most of the current implementation methods are directly written hardware or direct write memory such a underlying method. Later, under their own efforts, the relatively perfect solution solved the problem on the application layer and achieved high efficiency and translucent effects.
Let us first talk about the normal translucent implementation methods under WINDOW.
Conventional translucent implementation methods are:
1: First get the color value of the pixel point of the target DC, split this color value into R, G, and B value (assuming to R1, G1, B1),
2: Receive the color value of the source DC this position pixel point, and also split this color value into R, G, and B value (assuming R2, G2, B2),
3: Then, the R, G, and B values of these two colors are calculated as follows.
NEW = Source * Alpha (1 - alpha) * DEST;
Also calculate: newR = r1 * alpha (1 - alpha) * R2;
NEWG = G1 * Alpha (1 - alpha) * G2; newb = b1 * alpha (1 - alpha) * b2; 4: Finally, the new new NEWR, NewG, newb value synthesized the color of the final display value.
Here, all pixel points in the translucent area are processed 4 steps, which will involve a large number of calculations. But this is not the most time consuming. The most time consuming is the color value of the pixel point and the pixel value of a pixel point. Getpixel () and setpixel (). Test under Wince4.2 simulator, as follows
For (int i = 0; i <100; i )
{
For (int N = 0; n <100; n )
{
PDC-> GetPixel (i, n);
}
}
It takes 4 seconds, which is 10,000 GetPixel operations for 4 seconds, which is only a half-translucent area of 100, long 100.
So, ultimately we need to replace this getPixel and setpixel functions in some alternative way. Under Windows, you can read all pixel points over to get all the pixel points to analyze the getDibits and getBitmapBits.
But in embedded, unfortunately, the dead Microsoft gives these two functions to DEL, so this must also ask us to find another alternative way. After fine research, the BMP structure is studied, and after the DIB device is independent, it finds a method to achieve translucent features, and can have high drawing efficiency in the embedded system. Operating system: WinCE 4.2, Emulator, X86 Simulator.
Programming environment: Embedded Visual C , SP4, Standardsdk / * ------------------------------------- ------------------------ 2004-12-1 Ricky Huang Junfeng Writing Function: Picture of a semi-transparent area in display DC Pictures in: CDC * PDSTDC Target DC CRECT RTTRAN The translucent area ColorRef CLREF CLREF CLRSRC with the target area requires an Optimized place 1: Incoming transparency as a parameter here half translation, no conventional split method, in order to speed up In order to facilitate the convenience of the floating point operation, it is also very convenient to change the transparency of 50% transparency here. It is also very convenient to change to any transparency. For a detailed modification, please see another article about Alpha. 2: Use rgbquad * prgb instead of byte * pbtpixel; a plurality of values are performed at a time. 3: Embed an MMX assembly instruction to speed up the computation speed.
-------------------------------------------------- ------------ * / Void DrawsemitransparentRect (CDC * PDSTDC, CRECT RTTRTRAN, ColorRef CLRSRC) {Int nsaveddc = pdstdc-> savedc (); // Save Original DC INT NWIDTH = RTTRAN.WIDTH ( ); // translucent area INT NHEIGHT = RTTRAN.HEIGHT (); // Translucent area height // copy screen, new DC to copy target area CDC BMPDC; bmpdc.createcompatibleDC (PDSTDC); CBITMAP BMP; bmp.CreateCompatibleBitmap (pDstDC, nWidth, nHeight); CBitmap * pOldBmp = (CBitmap *) bmpDC.SelectObject (& bmp); bmpDC.BitBlt (0, 0, nWidth, nHeight, pDstDC, rtTran.left, rtTran.top, SRCCOPY) ; // Get graphical data in BMPDC to the Byte array HDC HDIBDC = CreateCompatibleDC (null); BitmapInfo HDR; ZerMemory (& HDR, SIZEOF (BitmapInfo); hdr.bmiheade r.biSize = sizeof (BITMAPINFOHEADER); hdr.bmiHeader.biWidth = nWidth; hdr.bmiHeader.biHeight = nHeight; hdr.bmiHeader.biPlanes = 1; hdr.bmiHeader.biBitCount = 32; / * Here unified with 32-bit pattern, The reason is that the 32-bit graphic structure is relatively fixed, and each pixel is represented by 4 bytes. No additional bytes are required at the end of each row.
And the 24-bit graphics, you need to add additional bytes at the end of each row to ensure that the number of bytes per line is 4 integer times. If you want to reduce the overhead and bytes, you can use 24-bit graphics, but will be more Judgment Add an additional own code.
* / BYTE * pbtPixels = NULL; HBITMAP hDIBitmap = CreateDIBSection (hDIBDC, (BITMAPINFO *) & hdr, DIB_RGB_COLORS, (void **) & pbtPixels, NULL, 0); HBITMAP hOldBmp = (HBITMAP) SelectObject (hDIBDC, hDIBitmap); BitBlt ( HDIBDC, 0, 0, NWIDTH, NHEIGHT, BMPDC, 0, 0, Srccopy; SelectObject (HDIBDC, HoldBMP); // For the Byte array, Alpha mixed int npixelsize = 4; // Each pixel point with 4 bytes To store Byte btsr = get strs (clrsrc); byte btsg = getgvalue (clrsrc); byte btsb = getBvalue (CLRSRC); for (int i = 0; i