• Welcome to Theos PowerBasic Museum 2017.

Translating PB's CALL DWORD @@pthis[2] to C/C++, how?

Started by Patrice Terrier, April 06, 2013, 03:50:37 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Charles Pegge

Much of this is an exercise in C comprehension for Patrice and myself. We both need to understand C from the ground level.

Mike, do you recall we had a conversation about first-class functions and closures, on the thinBasic forum. I am still a little mystified by closures, so perhaps we could start a new topic on functional programming.

Patrice Terrier

#31
Mike--

It does not serve the same purpose, in GDImage it is used to create an image with variable opacity where the 256 level of gray are being used to assign a specific alpha channel to each pixel of an image, something totaly different that converting a whole image to gray. Indeed it works more like what OpenGL does when using GL_BLEND.

                   pBits = bm.bmBits
                   FOR y = 0 TO bm.bmHeight - 1
                       FOR x = 0 TO (bm.bmWidth - 1)
                           @pBits[3] = Rgb2Gray(RGB(@pBits[2],@pBits[1],@pBits[0]))
                           pBits = pBits + 4
                       NEXT
                   NEXT


As you can see from the above code only the alpha channel is changed, the other RGB components are left unchanged.
And you can understand now, why the Rgb2Gray must be as fast as possible.

This is something totaly different than this:

LONG_PTR UseGrayMatrix(IN LONG_PTR imgAttr) {
    ColorMatrix c, g;
    // Create the ImageAttributes object
    if (imgAttr == 0) { GdipCreateImageAttributes(imgAttr); }
    // Fill the color matrix
    // Red              Green               Blue                Alpha              W
    c.m[0][0] = 0.3f  ; c.m[1][0] = 0.3f  ; c.m[2][0] = 0.3f  ; c.m[3][0] = 0.0f ; c.m[4][0] = 0.0f; // Red
    c.m[0][1] = 0.59f ; c.m[1][1] = 0.59f ; c.m[2][1] = 0.59f ; c.m[3][1] = 0.0f ; c.m[4][1] = 0.0f; // Green
    c.m[0][2] = 0.11f ; c.m[1][2] = 0.11f ; c.m[2][2] = 0.11f ; c.m[3][2] = 0.0f ; c.m[4][2] = 0.0f; // Blue
    c.m[0][3] = 0.0f  ; c.m[1][3] = 0.0f  ; c.m[2][3] = 0.0f  ; c.m[3][3] = 1.0f ; c.m[4][3] = 0.0f; // Alpha
    c.m[0][4] = 0.0f  ; c.m[1][4] = 0.0f  ; c.m[2][4] = 0.0f  ; c.m[3][4] = 0.0f ; c.m[4][4] = 1.0f; // W
    // And set its color matrix
    GdipSetImageAttributesColorMatrix(imgAttr, ColorAdjustTypeDefault, TRUE, c, g, ColorMatrixFlagsDefault);
    return imgAttr;
}


Charles--
QuoteMuch of this is an exercise in C comprehension for Patrice and myself. We both need to understand C from the ground level.
So true, thank you :)
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Mike Stefanik

#32
Quote from: Charles Pegge on April 10, 2013, 07:09:12 AM
Mike, do you recall we had a conversation about first-class functions and closures, on the thinBasic forum. I am still a little mystified by closures, so perhaps we could start a new topic on functional programming.

Sure, but in the context of C++ though you kind of end out far into the weeds. C++11 introduced lambdas (anonymous functions) and closures that look like this:


int foo = 1;
for_each(v.begin(); v.end(); [&foo] (int bar) -> int
{
    return bar * foo++;
});
;

It does make things more convenient (for example, creating class delegates) but, to me, this just looks ugly. Other languages have a much cleaner implementation (such as C# and JavaScript). But perhaps it's just because I'm an old C programmer from days gone by and these are the new kids partying on my front lawn.

Edit: I'm just typing this off the top of my head, so hopefully I got the syntax right there (and it's somewhat complicated by the fact that not all of the current C++ compilers out there completely support the standard). Your mileage may vary, as they say.
Mike Stefanik
sockettools.com

James C. Fuller

I hear you Mike.
MinGW 4.8 has all of it I think.
This is one of the distro's I am using: http://nuwen.net/mingw.html
An MS employee's personal site.
Even though I am still early on in my c++ education I really like some of the new c11 stuff.
some of my favorites:
direct vector assigment, reference based loops and of course "auto"

With the nuwen distro this will compile with just this simple command line:
g++ v4.cpp -ov4.exe


James



#include <iostream>
#include <vector>
using namespace std;


int     main (void);


int main ()
{
    vector<string> s = {"one","two","three","four","five"};

    cout << "s contains" << endl;
    for(auto it = s.begin(); it != s.end(); ++it)
    {
        cout << " " << *it;
    }
    cout << endl;

    cout << "in reverse" << endl;
    for(auto rit = s.rbegin(); rit != s.rend(); ++rit)
    {
      cout <<" " << *rit;
    }

    cout << endl;
    cout << "range-based for loop" << endl;
    for ( auto it : s)
    {
      cout << " " << it;
    }
    cout << endl;
    return 0;
}