Results 1 to 5 of 5

Thread: VB --> C DLL code doesn't work...

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325

    Question VB --> C DLL code doesn't work...

    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.

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325

    Sorry...

    When pasted it code was indented... But now, what a disaster!!!!

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You need to use [ code ] [ /code ] tags.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Remove the __declspec( dllexport ) and write a .def file instead.
    And there are macros to extract the colors from a COLORREF, you don't need your own function.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325
    Code:
    Learnt:D
    I know about GetRValue,GetGValue,GetBValue, but ColorCodeToRGB does the same job. But this isn't the problem.

    1) Let's strip GetRed,GetGrn and GetBlu functions that doesn't do nothing.(they are called nowhere in that code!!!).
    2) I can call my DLL perfecty from VB, so i won't write a .def.

    The problem is : VB code works great but it's slow. I want to write the same code in C and put into a DLL to get more speed. Right?!? So I translated in C but it doesn't work. Did I make a good translation?!? I'm new to C++ so I can't make the simplest program for testing that code (e.g. two picturebox)


    Pls Help...
    Thanks, Christmas

    Here you are the indented code

    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 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
    
    #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;
    }
    
    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;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width