var c = /****   
#include <windows.h> 
 
/*
该函数原作者 Jean-Edouard Lachand-Robert 
http://www.codeguru.com/member.php/Jean-Edouard+Lachand+Robert/
*/

#include <math.h>
__declspec(dllexport) HRGN BitmapToRegion (HBITMAP hBmp, BOOL autoTransparent,COLORREF cTransparentColor , COLORREF cTransparentColor2, COLORREF cStepTolerance )
{ 
	HRGN hRgn = NULL; 
	//FILE * stdout2;
	//freopen_s(&stdout2,"CONOUT$","w+t",stdout); 
	
	if (hBmp)
	{
		// Create a memory DC inside which we will scan the bitmap content
		HDC hMemDC = CreateCompatibleDC(NULL);
		if (hMemDC)
		{
			// Get bitmap size
			BITMAP bm;
			GetObject(hBmp, sizeof(bm), &bm);

			// Create a 32 bits depth bitmap and select it into the memory DC 
			BITMAPINFOHEADER RGB32BITSBITMAPINFO = {	
					sizeof(BITMAPINFOHEADER),	// biSize 
					bm.bmWidth,					// biWidth; 
					bm.bmHeight,				// biHeight; 
					1,							// biPlanes; 
					32,							// biBitCount 
					BI_RGB,						// biCompression; 
					0,							// biSizeImage; 
					0,							// biXPelsPerMeter; 
					0,							// biYPelsPerMeter; 
					0,							// biClrUsed; 
					0							// biClrImportant; 
			};
			VOID * pbits32; 
			HBITMAP hbm32 = CreateDIBSection(hMemDC, (BITMAPINFO *)&RGB32BITSBITMAPINFO, DIB_RGB_COLORS, &pbits32, NULL, 0);
			if (hbm32)
			{
				HBITMAP holdBmp = (HBITMAP)SelectObject(hMemDC, hbm32);

				// Create a DC just to copy the bitmap into the memory DC
				HDC hDC = CreateCompatibleDC(hMemDC);
				if (hDC)
				{
					// Get how many bytes per row we have for the bitmap bits (rounded up to 32 bits)
					BITMAP bm32;
					GetObject(hbm32, sizeof(bm32), &bm32);
					while (bm32.bmWidthBytes % 4)
						bm32.bmWidthBytes++;

					// Copy the bitmap into the memory DC
					HBITMAP holdBmp = (HBITMAP)SelectObject(hDC, hBmp);
					BitBlt(hMemDC, 0, 0, bm.bmWidth, bm.bmHeight, hDC, 0, 0, SRCCOPY);

					// For better performances, we will use the ExtCreateRegion() function to create the
					// region. This function take a RGNDATA structure on entry. We will add rectangles by
					// amount of ALLOC_UNIT number in this structure.
					#define ALLOC_UNIT	100
					DWORD maxRects = ALLOC_UNIT;
					HANDLE hData = GlobalAlloc(GMEM_MOVEABLE, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects));
					RGNDATA *pData = (RGNDATA *)GlobalLock(hData);
					pData->rdh.dwSize = sizeof(RGNDATAHEADER);
					pData->rdh.iType = RDH_RECTANGLES;
					pData->rdh.nCount = pData->rdh.nRgnSize = 0;
					SetRect(&pData->rdh.rcBound, MAXLONG, MAXLONG, 0, 0);

					// Keep on hand highest and lowest values for the "transparent" pixels
					BYTE lrHead = GetRValue(cTransparentColor);
					BYTE lgHead = GetGValue(cTransparentColor);
					BYTE lbHead = GetBValue(cTransparentColor);
					BYTE lrPrev = lrHead;
					BYTE lgPrev = lgHead;
					BYTE lbPrev = lbHead;
					BYTE hrHead = min(0xff, GetRValue(cTransparentColor2));
					BYTE hgHead = min(0xff, GetGValue(cTransparentColor2));
					BYTE hbHead = min(0xff, GetBValue(cTransparentColor2));
					BYTE hrStep = min(0xff, GetRValue(cStepTolerance));
					BYTE hgStep = min(0xff, GetGValue(cStepTolerance));
					BYTE hbStep = min(0xff, GetBValue(cStepTolerance));
					 
					// Scan each bitmap row from bottom to top (the bitmap is inverted vertically)
					BYTE *p32 = (BYTE *)bm32.bmBits + (bm32.bmHeight - 1) * bm32.bmWidthBytes;
					BYTE *pbyte = p32;
					if( autoTransparent  ){
						BYTE *p = (BYTE *)bm32.bmBits;
						lbHead = *pbyte++;
						hbHead = *p++;
						lgHead = *pbyte++;
						hgHead = *p++;
						lrHead = *pbyte++;
						hrHead = *p++;
					}
					
					BYTE temp;
					if( lbHead > hbHead ){
						temp = lbHead;
						lbHead =hbHead;
						hbHead =temp;
					} 
					if( lgHead > hgHead ){
						temp = lgHead;
						lgHead =hgHead;
						hgHead =temp;
					} 
					if( lrHead > hrHead ){
						temp = lrHead;
						lrHead =hrHead;
						hrHead =temp;
					}  
					
					int y;
					BYTE r,g,b;  
					int ncr = cStepTolerance ? 2 : 0;
					for ( y = 0; y < bm.bmHeight; y++)
					{ 
						int x;
						for ( x = 0; x < bm.bmWidth; x++)
						{ 
							int x0 = x;
							LONG *p = (LONG *)p32 + x;
						
							while (x < bm.bmWidth)
							{   
							 	if( cStepTolerance ){
									pbyte = (BYTE *)p;
									b = *pbyte++; 
									if ( abs( b - lbPrev ) <= (hbStep)  )
									{ 
										g = *pbyte++;
										if ( abs( g - lgPrev ) <= (hgStep) )
										{
											r = *pbyte++;
											if ( abs( r - lrPrev ) <= (hrStep) ){ 
												lrPrev = r;
												lgPrev = g;
												lbPrev = b;
												break;
											}
										}  
									} 
								}
							  
								pbyte = (BYTE *)p;
								b = *pbyte++; 
								if ( b <=  hbHead+hbStep  && b+hbStep >=  lbHead )
								{ 
									g = *pbyte++;
									if ( g <= hgHead+hgStep  && g+hgStep >=  lgHead )
									{
										r = *pbyte++;
										if ( r <= hrHead+hrStep  && r+hrStep >=  lrHead )
										{
											lrPrev = r;
											lgPrev = g;
											lbPrev = b;
											break;
										}
									}  
								} 
								 
								p++;
								x++;
							}  
							
							if ( x > x0 + ncr ) {
								// Add the pixels (x0, y) to (x, y+1) as a new rectangle in the region
								if (pData->rdh.nCount >= maxRects)
								{
									GlobalUnlock(hData);
									maxRects += ALLOC_UNIT;
									hData = GlobalReAlloc(hData, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects), GMEM_MOVEABLE);
									pData = (RGNDATA *)GlobalLock(hData);
								}
								RECT *pr = (RECT *)&pData->Buffer;
								SetRect(&pr[pData->rdh.nCount], x0, y, x, y+1);
								if (x0 < pData->rdh.rcBound.left)
									pData->rdh.rcBound.left = x0;
								if (y < pData->rdh.rcBound.top)
									pData->rdh.rcBound.top = y;
								if (x > pData->rdh.rcBound.right)
									pData->rdh.rcBound.right = x;
								if (y+1 > pData->rdh.rcBound.bottom)
									pData->rdh.rcBound.bottom = y+1;
								pData->rdh.nCount++;

								// On Windows98, ExtCreateRegion() may fail if the number of rectangles is too
								// large (ie: > 4000). Therefore, we have to create the region by multiple steps.
								if (pData->rdh.nCount == 2000)
								{
									HRGN h = ExtCreateRegion(NULL, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects), pData);
									if (hRgn)
									{
										CombineRgn(hRgn, hRgn, h, RGN_OR);
										DeleteObject(h);
									}
									else
										hRgn = h;
									pData->rdh.nCount = 0;
									SetRect(&pData->rdh.rcBound, MAXLONG, MAXLONG, 0, 0);
								}
							}
						}

						// Go to next row (remember, the bitmap is inverted vertically)
						p32 -= bm32.bmWidthBytes;
					}

					// Create or extend the region with the remaining rectangles
					HRGN h = ExtCreateRegion(NULL, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects), pData);
					if (hRgn)
					{
						CombineRgn(hRgn, hRgn, h, RGN_OR);
						DeleteObject(h);
					}
					else
						hRgn = h;

					// Clean up
					SelectObject(hDC, holdBmp);
					DeleteDC(hDC);
					
					GlobalUnlock(hData);
					GlobalFree(hData);
				}

				DeleteObject(SelectObject(hMemDC, holdBmp));
			}

			DeleteDC(hMemDC);
		}	
	}

	return hRgn;
}
****/

import console;
import tcc;  
vm = tcc(); 
  
vm.addLib( 
	"user32",
	"kernel32",
	"gdi32"
)

vm.onError = function(msg){
 	console.log( msg )
}
vm.output( "/c.dll", c  )//编译C源码,生成DLL  
vm.close();  
   
