Xmas79
Jan 9th, 2002, 08:54 AM
Hello, I just "translated" an algorithm from VB to C. I put it in a DLL but it doesn't work. I'm doing this because I think in C is faster... It's an algorithm to shrink a picture. Surely better than StretchBlt API. Found in this forum...
Can anyone have a look at both codes, pls?!?
-----------------------------
VB Code
-----------------------------
Public Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Public Declare Function SetPixelV Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
Public Function ShrinkPict(DestDC As Long, SourceDC As Long, fx1 As Integer, fx2 As Integer, fy1 As Integer, fy2 As Integer, tx1 As Integer, tx2 As Integer, ty1 As Integer, ty2 As Integer)
Dim yScale As Single
Dim xScale As Single
Dim tx As Integer
Dim ty As Integer
Dim RedAvg As Long
Dim GrnAvg As Long
Dim BluAvg As Long
Dim x As Integer
Dim y As Integer
Dim BaseCol As Long
Dim R As Long
Dim G As Long
Dim B As Long
xScale = (tx2 - tx1) / (fx2 - fx1)
yScale = (ty2 - ty1) / (fy2 - fy1)
For ty = ty1 To ty2 - 1
Y1 = Int((ty - ty1) / yScale + fy1)
Y2 = Int((ty + 1 - ty1) / yScale + fy1) - 1
For tx = tx1 To tx2 - 1
X1 = Int((tx - tx1) / xScale + fx1)
X2 = Int((tx + 1 - tx1) / xScale + fx1) - 1
RedAvg = 0: GrnAvg = 0: BluAvg = 0
For y = Y1 To Y2
For x = X1 To X2
BaseCol = GetPixel(SourceDC, x, y)
ColorCodeToRGB BaseCol, R, G, B
RedAvg = RedAvg + R
GrnAvg = GrnAvg + G
BluAvg = BluAvg + B
Next x
Next y
If RedAvg > 0 Then
RedAvg = RedAvg / (X2 - X1 + 1) / (Y2 - Y1 + 1)
Else
RedAvg = 0
End If
If GrnAvg > 0 Then
GrnAvg = GrnAvg / (X2 - X1 + 1) / (Y2 - Y1 + 1)
Else
GrnAvg = 0
End If
If BluAvg > 0 Then
BluAvg = BluAvg / (X2 - X1 + 1) / (Y2 - Y1 + 1)
Else
BluAvg = 0
End If
SetPixelV DestDC, tx, ty, RGB(RedAvg, GrnAvg, BluAvg)
Next tx
Next ty
End Function
Public Function GetRed(RGBCol As Long) As Long
GetRed = RGBCol And &HFF
If GetRed > 255 Then GetRed = 255
If GetRed < 0 Then GetRed = 0
End Function
Public Function GetGrn(RGBCol As Long) As Long
GetGrn = (RGBCol And &HFF00)
If GetGrn <> 0 Then GetGrn = GetGrn / &HFF
If GetGrn > 255 Then GetGrn = 255
If GetGrn < 0 Then GetGrn = 0
End Function
Public Function GetBlu(RGBCol As Long) As Long
GetBlu = (RGBCol And &HFF0000)
If GetBlu <> 0 Then GetBlu = GetBlu / &HFFFF
If GetBlu > 255 Then GetBlu = 255
If GetBlu < 0 Then GetBlu = 0
End Function
Public Function ColorCodeToRGB(lColorCode As Long, iRed As Long, iGreen As Long, iBlue As Long) As Boolean
' 1996/01/16 Return the individual colors for lColorCode.
' 1996/07/15 Use Tip 171: Determining RGB Color Values, MSDN July 1996.
' Enter with:
' lColorCode contains the color to be converted
'
' Return:
' iRed contains the red component
' iGreen the green component
' iBlue the blue component
'
Dim lColor As Long
lColor = lColorCode 'work long
iRed = lColor Mod &H100 'get red component
lColor = lColor \ &H100 'divide
iGreen = lColor Mod &H100 'get green component
lColor = lColor \ &H100 'divide
iBlue = lColor Mod &H100 'get blue component
ColorCodeToRGB = True
End Function
-----------------------------
My translation into DLL
-----------------------------
#define WIN32_LEAN_AND_MEAN
#include <windows.h>;
#pragma warning(once : 4244 4700);
long GetRed(long RGBCol);
long GetGrn(long RGBCol);
long GetBlu(long RGBCol);
bool ColorCodeToRGB(long *lColorCode,long *iRed,long *iGreen,long *iBlue);
extern "C" __declspec( dllexport ) long __stdcall ShrinkPict(long DestDC,long SourceDC,long fx1,long fx2,long fy1,long fy2,long tx1,long tx2,long ty1,long ty2)
{
float yScale,xScale;
long tx,ty,RedAvg,GrnAvg,BluAvg,x,y,BaseCol,R,G,B,X1,X2,Y1,Y2;
xScale=(float)(tx2-tx1)/(float)(fx2-fx1);
yScale=(float)(ty2-ty1)/(float)(fy2-fy1);
for(ty=ty1;ty<=(ty2-1);ty++)
{
Y1=(float)((ty-ty1)/yScale+fy1);
Y2=(float)((ty+1-ty1)/yScale+fy1)-1;
for(tx=tx1;tx<=(tx2-1);tx++)
{
X1=(float)((tx-tx1)/xScale+fx1);
X2=(float)((tx+1-tx1)/xScale+fx1)-1;
RedAvg=0;
GrnAvg=0;
BluAvg=0;
for(y=Y1;y<=Y2;y++)
{
for(x=X1;x<=X2;x++)
{
BaseCol=GetPixel((HDC)SourceDC,x,y);
ColorCodeToRGB(&BaseCol,&R,&G,&B);
RedAvg=RedAvg+R;
GrnAvg=GrnAvg+G;
BluAvg=BluAvg+B;
}
}
if(RedAvg>0) RedAvg=RedAvg/(X2-X1+1)/(Y2-Y1+1); else RedAvg=0;
if(GrnAvg>0) GrnAvg=GrnAvg/(X2-X1+1)/(Y2-Y1+1); else GrnAvg=0;
if(BluAvg>0) BluAvg=BluAvg/(X2-X1+1)/(Y2-Y1+1); else BluAvg=0;
SetPixelV((HDC)DestDC,tx,ty,RGB(RedAvg,GrnAvg,BluAvg));
}
}
return 0;
}
long GetRed(long RGBCol)
{
long Color;
Color=RGBCol & 0xFF;
if(Color>255) Color=255;
else if(Color<0) Color=0;
return Color;
}
long GetGrn(long RGBCol)
{
long Color;
Color=(RGBCol & 0xFF00);
if(Color!=0)
{
Color=Color/0xFF;
if(Color>255) Color=255;
else if(Color<0) Color=0;
}
return Color;
}
long GetBlu(long RGBCol)
{
long Color;
Color=(RGBCol & 0xFF0000);
if(Color!=0)
{
Color=Color/0xFFFF;
if(Color>255) Color=255;
else if(Color<0) Color=0;
}
return Color;
}
bool ColorCodeToRGB(long *lColorCode,long *iRed,long *iGreen,long *iBlue)
{
long lColor;
lColor=*lColorCode;
*iRed=lColor % 0x100;
lColor=lColor / 0x100;
*iGreen=lColor % 0x100;
lColor=lColor / 0x100;
*iBlue=lColor % 0x100;
return true;
}
-----------------------------
Usage
-----------------------------
ShrinkPict Picture2.hdc, Picture1.hdc, 0, Picture1.ScaleWidth, 0, Picture1.ScaleHeight, 0, Picture2.ScaleWidth, 0, Picture2.ScaleHeight
-----------------------------
End of code
-----------------------------
It enters in loop and never stops...
Thanks a lot,
Christmas.
Can anyone have a look at both codes, pls?!?
-----------------------------
VB Code
-----------------------------
Public Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Public Declare Function SetPixelV Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
Public Function ShrinkPict(DestDC As Long, SourceDC As Long, fx1 As Integer, fx2 As Integer, fy1 As Integer, fy2 As Integer, tx1 As Integer, tx2 As Integer, ty1 As Integer, ty2 As Integer)
Dim yScale As Single
Dim xScale As Single
Dim tx As Integer
Dim ty As Integer
Dim RedAvg As Long
Dim GrnAvg As Long
Dim BluAvg As Long
Dim x As Integer
Dim y As Integer
Dim BaseCol As Long
Dim R As Long
Dim G As Long
Dim B As Long
xScale = (tx2 - tx1) / (fx2 - fx1)
yScale = (ty2 - ty1) / (fy2 - fy1)
For ty = ty1 To ty2 - 1
Y1 = Int((ty - ty1) / yScale + fy1)
Y2 = Int((ty + 1 - ty1) / yScale + fy1) - 1
For tx = tx1 To tx2 - 1
X1 = Int((tx - tx1) / xScale + fx1)
X2 = Int((tx + 1 - tx1) / xScale + fx1) - 1
RedAvg = 0: GrnAvg = 0: BluAvg = 0
For y = Y1 To Y2
For x = X1 To X2
BaseCol = GetPixel(SourceDC, x, y)
ColorCodeToRGB BaseCol, R, G, B
RedAvg = RedAvg + R
GrnAvg = GrnAvg + G
BluAvg = BluAvg + B
Next x
Next y
If RedAvg > 0 Then
RedAvg = RedAvg / (X2 - X1 + 1) / (Y2 - Y1 + 1)
Else
RedAvg = 0
End If
If GrnAvg > 0 Then
GrnAvg = GrnAvg / (X2 - X1 + 1) / (Y2 - Y1 + 1)
Else
GrnAvg = 0
End If
If BluAvg > 0 Then
BluAvg = BluAvg / (X2 - X1 + 1) / (Y2 - Y1 + 1)
Else
BluAvg = 0
End If
SetPixelV DestDC, tx, ty, RGB(RedAvg, GrnAvg, BluAvg)
Next tx
Next ty
End Function
Public Function GetRed(RGBCol As Long) As Long
GetRed = RGBCol And &HFF
If GetRed > 255 Then GetRed = 255
If GetRed < 0 Then GetRed = 0
End Function
Public Function GetGrn(RGBCol As Long) As Long
GetGrn = (RGBCol And &HFF00)
If GetGrn <> 0 Then GetGrn = GetGrn / &HFF
If GetGrn > 255 Then GetGrn = 255
If GetGrn < 0 Then GetGrn = 0
End Function
Public Function GetBlu(RGBCol As Long) As Long
GetBlu = (RGBCol And &HFF0000)
If GetBlu <> 0 Then GetBlu = GetBlu / &HFFFF
If GetBlu > 255 Then GetBlu = 255
If GetBlu < 0 Then GetBlu = 0
End Function
Public Function ColorCodeToRGB(lColorCode As Long, iRed As Long, iGreen As Long, iBlue As Long) As Boolean
' 1996/01/16 Return the individual colors for lColorCode.
' 1996/07/15 Use Tip 171: Determining RGB Color Values, MSDN July 1996.
' Enter with:
' lColorCode contains the color to be converted
'
' Return:
' iRed contains the red component
' iGreen the green component
' iBlue the blue component
'
Dim lColor As Long
lColor = lColorCode 'work long
iRed = lColor Mod &H100 'get red component
lColor = lColor \ &H100 'divide
iGreen = lColor Mod &H100 'get green component
lColor = lColor \ &H100 'divide
iBlue = lColor Mod &H100 'get blue component
ColorCodeToRGB = True
End Function
-----------------------------
My translation into DLL
-----------------------------
#define WIN32_LEAN_AND_MEAN
#include <windows.h>;
#pragma warning(once : 4244 4700);
long GetRed(long RGBCol);
long GetGrn(long RGBCol);
long GetBlu(long RGBCol);
bool ColorCodeToRGB(long *lColorCode,long *iRed,long *iGreen,long *iBlue);
extern "C" __declspec( dllexport ) long __stdcall ShrinkPict(long DestDC,long SourceDC,long fx1,long fx2,long fy1,long fy2,long tx1,long tx2,long ty1,long ty2)
{
float yScale,xScale;
long tx,ty,RedAvg,GrnAvg,BluAvg,x,y,BaseCol,R,G,B,X1,X2,Y1,Y2;
xScale=(float)(tx2-tx1)/(float)(fx2-fx1);
yScale=(float)(ty2-ty1)/(float)(fy2-fy1);
for(ty=ty1;ty<=(ty2-1);ty++)
{
Y1=(float)((ty-ty1)/yScale+fy1);
Y2=(float)((ty+1-ty1)/yScale+fy1)-1;
for(tx=tx1;tx<=(tx2-1);tx++)
{
X1=(float)((tx-tx1)/xScale+fx1);
X2=(float)((tx+1-tx1)/xScale+fx1)-1;
RedAvg=0;
GrnAvg=0;
BluAvg=0;
for(y=Y1;y<=Y2;y++)
{
for(x=X1;x<=X2;x++)
{
BaseCol=GetPixel((HDC)SourceDC,x,y);
ColorCodeToRGB(&BaseCol,&R,&G,&B);
RedAvg=RedAvg+R;
GrnAvg=GrnAvg+G;
BluAvg=BluAvg+B;
}
}
if(RedAvg>0) RedAvg=RedAvg/(X2-X1+1)/(Y2-Y1+1); else RedAvg=0;
if(GrnAvg>0) GrnAvg=GrnAvg/(X2-X1+1)/(Y2-Y1+1); else GrnAvg=0;
if(BluAvg>0) BluAvg=BluAvg/(X2-X1+1)/(Y2-Y1+1); else BluAvg=0;
SetPixelV((HDC)DestDC,tx,ty,RGB(RedAvg,GrnAvg,BluAvg));
}
}
return 0;
}
long GetRed(long RGBCol)
{
long Color;
Color=RGBCol & 0xFF;
if(Color>255) Color=255;
else if(Color<0) Color=0;
return Color;
}
long GetGrn(long RGBCol)
{
long Color;
Color=(RGBCol & 0xFF00);
if(Color!=0)
{
Color=Color/0xFF;
if(Color>255) Color=255;
else if(Color<0) Color=0;
}
return Color;
}
long GetBlu(long RGBCol)
{
long Color;
Color=(RGBCol & 0xFF0000);
if(Color!=0)
{
Color=Color/0xFFFF;
if(Color>255) Color=255;
else if(Color<0) Color=0;
}
return Color;
}
bool ColorCodeToRGB(long *lColorCode,long *iRed,long *iGreen,long *iBlue)
{
long lColor;
lColor=*lColorCode;
*iRed=lColor % 0x100;
lColor=lColor / 0x100;
*iGreen=lColor % 0x100;
lColor=lColor / 0x100;
*iBlue=lColor % 0x100;
return true;
}
-----------------------------
Usage
-----------------------------
ShrinkPict Picture2.hdc, Picture1.hdc, 0, Picture1.ScaleWidth, 0, Picture1.ScaleHeight, 0, Picture2.ScaleWidth, 0, Picture2.ScaleHeight
-----------------------------
End of code
-----------------------------
It enters in loop and never stops...
Thanks a lot,
Christmas.