|
-
Jul 29th, 2003, 08:55 PM
#15
Thread Starter
Fanatic Member
finally... i just got home from work... here's the source code to the dll
jsgfx.cpp
Code:
#include <windows.h>
// The Representation of a 32 bit color table entry
#pragma pack(push)
#pragma pack(1)
typedef struct ssBGR {
unsigned char b;
unsigned char g;
unsigned char r;
unsigned char pad;
} sBGR;
typedef sBGR *pBGR;
#pragma pack(pop)
//prototypes
void __stdcall SoftBlt(HDC hdcDest, int nLeft, int nTop, int nWidth, int nHeight, HDC hdcSrc, HDC hdcSrcMask);
pBGR MyGetDibBits(HDC hdc, HBITMAP hBmp, int nWidth, int nHeight);
int MySetDibBits(HDC hdc, HBITMAP hBmp, int nWidth, int nHeight, pBGR buf);
sBGR Pixelate(sBGR dest,sBGR source,BYTE Alpha);
void __stdcall SoftBlt(HDC hdcDest, int nLeft, int nTop, int nWidth, int nHeight, HDC hdcSrc, HDC hdcSrcMask)
{
//STEP 1 - 3
HDC hdcTMPDest = CreateCompatibleDC(hdcDest);
HBITMAP hbTMPDest = CreateCompatibleBitmap(hdcDest, nWidth, nHeight);
SelectObject(hdcTMPDest, hbTMPDest);
BitBlt(hdcTMPDest, 0, 0, nWidth, nHeight, hdcDest, nLeft, nTop, SRCCOPY);
pBGR dibDest = MyGetDibBits(hdcTMPDest, (HBITMAP) GetCurrentObject(hdcTMPDest, OBJ_BITMAP), nWidth, nHeight);
pBGR dibSource = MyGetDibBits(hdcSrc, (HBITMAP) GetCurrentObject(hdcSrc, OBJ_BITMAP), nWidth, nHeight);
pBGR dibMask = MyGetDibBits(hdcSrcMask, (HBITMAP) GetCurrentObject(hdcSrcMask, OBJ_BITMAP), nWidth, nHeight);
//STEP 4
for (int y = 0; y < nHeight; ++y)
{
for (int x = 0; x < nWidth; ++x)
{
sBGR bgrDest = dibDest[y * nWidth + x];
sBGR bgrSource = dibSource[y * nWidth + x];
sBGR bgrMask = dibMask[y * nWidth + x];
unsigned char mask = ((bgrMask.b + bgrMask.g + bgrMask.r) / 3);
//Blend function
//bgrDest.r = 255;
//bgrDest.g = 0;
//bgrDest.b = 0;
bgrDest = Pixelate(bgrDest, bgrSource, mask);
//bgrDest.r = 255 - bgrDest.r;
//bgrDest.g = 255 - bgrDest.g;
//bgrDest.b = 255 - bgrDest.b;
dibDest[y * nWidth + x] = bgrDest;
}
}
//STEP 5
HDC hdcTmp = CreateCompatibleDC(hdcDest);
HBITMAP hbTmp = CreateCompatibleBitmap(hdcDest, nWidth, nHeight);
SelectObject(hdcTmp, hbTmp);
//STEP 6
MySetDibBits(hdcTmp, hbTmp, nWidth, nHeight, dibDest);
//STEP 7
BitBlt(hdcDest, nLeft, nTop, nWidth, nHeight, hdcTmp, 0, 0, SRCCOPY);
//Step 8
DeleteObject(hbTMPDest);
DeleteObject(hbTmp);
DeleteDC(hdcTMPDest);
DeleteDC(hdcTmp);
free(dibDest);
free(dibSource);
free(dibMask);
}
sBGR Pixelate(sBGR dest,sBGR source,BYTE Alpha)
{
float FinalRed,FinalGreen,FinalBlue;
FinalRed = (Alpha/255.0f*(float)source.r) + ((1.0f-Alpha/255.0f)*(float)dest.r);
FinalGreen= (Alpha/255.0f*(float)source.g) + ((1.0f-Alpha/255.0f)*(float)dest.g);
FinalBlue = (Alpha/255.0f*(float)source.b) + ((1.0f-Alpha/255.0f)*(float)dest.b);
int nFinalRed,nFinalGreen,nFinalBlue;
if(FinalRed>=FinalGreen && FinalRed>=FinalBlue)
{
if(FinalRed>255.0f)
{
nFinalRed=255;
nFinalGreen=(int)(FinalGreen/FinalRed*255.0f);
nFinalBlue =(int)(FinalBlue/FinalRed*255.0f);
}
else
{
nFinalRed =(int)(FinalRed);
nFinalGreen=(int)(FinalGreen);
nFinalBlue =(int)(FinalBlue);
}
}
else if(FinalGreen>=FinalRed && FinalGreen>=FinalBlue)
{
if(FinalGreen>255.0f)
{
nFinalRed =(int)(FinalRed/FinalGreen*255.0f);
nFinalGreen=255;
nFinalBlue =(int)(FinalBlue/FinalGreen*255.0f);
}
else
{
nFinalRed =(int)(FinalRed);
nFinalGreen=(int)(FinalGreen);
nFinalBlue =(int)(FinalBlue);
}
}
else if(FinalBlue>=FinalRed && FinalBlue>=FinalGreen)
{
if(FinalBlue>255.0f)
{
nFinalRed =(int)(FinalRed/FinalBlue*255.0f);
nFinalGreen=(int)(FinalGreen/FinalBlue*255.0f);
nFinalBlue=255;
}
else
{
nFinalRed =(int)(FinalRed);
nFinalGreen=(int)(FinalGreen);
nFinalBlue =(int)(FinalBlue);
}
}
sBGR tempBGR;
tempBGR.r=nFinalRed;
tempBGR.g=nFinalGreen;
tempBGR.b=nFinalBlue;
return tempBGR;
}
pBGR MyGetDibBits(HDC hdc, HBITMAP hBmp, int nWidth, int nHeight)
{
BITMAPINFO bi;
BOOL bRes;
pBGR buf;
bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
bi.bmiHeader.biWidth = nWidth;
bi.bmiHeader.biHeight = - nHeight;
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = 32;
bi.bmiHeader.biCompression = BI_RGB;
bi.bmiHeader.biSizeImage = nWidth * 4 * nHeight;
bi.bmiHeader.biClrUsed = 0;
bi.bmiHeader.biClrImportant = 0;
// buf = new sBGR[nWidth * nHeight];
buf = (pBGR) malloc(nWidth * 4 * nHeight);
bRes = GetDIBits(hdc, hBmp, 0, nHeight, buf, &bi, DIB_RGB_COLORS);
if (!bRes) {
// delete [] buf;
free(buf);
buf = 0;
}
return buf;
}
int MySetDibBits(HDC hdc, HBITMAP hBmp, int nWidth, int nHeight, pBGR buf)
{
BITMAPINFO bi;
bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
bi.bmiHeader.biWidth = nWidth ;
bi.bmiHeader.biHeight = - nHeight ;
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = 32;
bi.bmiHeader.biCompression = BI_RGB;
bi.bmiHeader.biSizeImage = nWidth * 4 * nHeight;
bi.bmiHeader.biClrUsed = 0;
bi.bmiHeader.biClrImportant = 0;
return ( SetDIBits(hdc , hBmp ,0, nHeight ,buf,&bi,DIB_RGB_COLORS) );
}
jsgfx.def
Code:
LIBRARY "jsgfx.dll"
EXPORTS
SoftBlt
VB Declare (you'll need to change the path for the Lib keyword as appropriate)
Code:
Private Declare Function SoftBlt Lib "H:\Microsoft Visual Studio\MyProjects\jsgfx\Debug\jsgfx.dll" (ByVal hdcDest As Long, ByVal nLeft As Long, ByVal nTop As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hdcSrc As Long, ByVal hdcSrcMask As Long ) As Long
if you use a control's hdc, the control's AutoRedraw property will need to be set to true, and you'll need to call the update method for the destination control after calling SoftBlit
the function does not currently produce a properly Alpha Blended image. Only SoftBlit is exported.
UPDATE!!!
I fixed the above code. Now it does work as advertised. See also http://www.vbforums.com/showthread.php?postid=1488575 for more information.
Last edited by agent; Jul 29th, 2003 at 11:32 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|