Installing node-canvas on Windows

There’s an article here that describes the process, but it’s a bit vague in some areas and didn’t work for me.

If you’re on 64-bit Windows, you will need the 64-bit version of GTK which comes bundled with Cairo if you get the all-in-one package. Somewhere under the mile of text on this page you should find a link to it; here’s a direct link for version 2.22.

It’s a zip. Extract it to c:/GTK. I don’t even what to consider what’s involved making it work from a different location.

You also need node-gyp. Install it via

npm install -g node-gyp

Once you’ve got all the dependencies you can attempt to install node-canvas. “CD” into your project directory and then run

npm install canvas --msvs_version=2012

Adjust the version number for whatever version of Visual Studio you have. The Wiki says to use VC++ 2010 Express which is also a bitch to find on Microsoft’s website as they push you towards 2013. Here’s a direct link which may or not work.

Even after installing VS2010 though, npm/gyp wouldn’t pick it up automatically, which is why I had to specify the version manually. Even then 2010 didn’t work, but 2012 did, so, whatever.

If you get some linker errors, you probably have an old version of Cairo.

If you get something about cairo-features, it’s probably the same deal. Those can be fixed by opening the .sln in Visual Studio and updating the include dirs to include C:/GTK/includes/cairo, but then you’ll be left with a new error, and they’ll probably never end, so just try different versions of the GTK bundle until you find one that works. Maybe try 32-bit if the 64-bit one doesn’t work for you (32 didn’t work for me).

Using OpenGL with Qt: A Tutorial with Code Examples and Screenshots

My last tutorial on this subject was written almost thee years ago, and it’s still the most popular article on this blog, yet I’m not sure even I could follow it. Thus, I’m going through the steps again with the latest versions of NetBeans (7.1.2) and Qt (4.8.1), this time taking screenshots along the way.

Read more Using OpenGL with Qt: A Tutorial with Code Examples and Screenshots

Qt + OpenGL Code Example

This article is from August 2009; please see the updated and more detailed version here.

I wanted to develop a game using OpenGL but I was having trouble deciding on a windowing library. Somebody suggested I try Qt, so I decided to give it another shot. I heard it was good, but the editor it came with was so foreign and unintuitive. Unfortunately, this overshadowed what a beautiful library Qt really is! It takes a little getting used to, but it really is well designed.
Read more Qt + OpenGL Code Example

Posted in

Draw an Unfilled Circle

OpenGL doesn’t seem to have any functions for drawing an unfilled circle, so you can use this code instead. It uses lines, so you can adjust the line thickness with glLineWidth and anti-alias it with glEnable(GL_LINE_SMOOTH) if you want.

void drawCircle(GLfloat x, GLfloat y, GLfloat r) {
    static const double inc = M_PI / 12;
    static const double max = 2 * M_PI;
    glBegin(GL_LINE_LOOP);
    for(double d = 0; d < max; d += inc) {
        glVertex2f(cos(d) * r + x, sin(d) * r + y);
    }
    glEnd();
}

If you want a filled circle, I would suggest you just set glPointSize to whatever size you want, and then plot a single point with glBegin(GL_POINTS) where ever you want your circle. Otherwise, you can use the above code with GL_POLYGON if you want more control. If you want a smoother circle, decrease the size of “inc”.

Human-readable file size in C

I did a quick search on Google and couldn’t find any code that did this in C/C++, so here’s my contribution for the day. Just remember the allocate enough space in the buffer — about 10 chars should be enough.

char* readable_fs(double size/*in bytes*/, char *buf) {
    int i = 0;
    const char* units[] = {"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
    while (size > 1024) {
        size /= 1024;
        i++;
    }
    sprintf(buf, "%.*f %s", i, size, units[i]);
    return buf;
}

// usage
struct stat info;
char buf[10];
lstat("somefile.txt", &info);
printf("File size: %s\n", readable_fs(info.st_size, buf));
Posted in

Draw IplImage

OpenCV stores images in a data structure called IplImage. They provide methods for rendering it to the screen, but if you want to use OpenGL instead (which should be faster and gives you more flexibility), I wrote the following code. It should be a little faster than loading the IplImage into a texture first, and then drawing it (if you have to do this every frame).

void DrawIplImage(IplImage *image, int x = 0, int y = 0, GLfloat xZoom = 1.0f, GLfloat yZoom = 1.0f)
{
    GLenum format;
        switch(image->nChannels) {
            case 1:
                format = GL_LUMINANCE;
                break;
            case 2:
                format = GL_LUMINANCE_ALPHA;
                break;
            case 3:
                format = GL_BGR;
                break;
            default:
                return;
        }
   
        glRasterPos2i(x, y);
        glPixelZoom(xZoom, yZoom);
    glDrawPixels(image->width, image->height, format, GL_UNSIGNED_BYTE, image->imageData);
}

It should be pretty straight forward to use. Handles IplImages with 1 or 3 layers. Should work with 2 layers too, but I haven’t needed or found an example of this.

Get frame count from AVI

Here’s a little code snippet I wrote to get the number of frames in an AVI video file.

int getFrameCount(const char *filename) {
    // only works on AVIs
    int frameCount;
    char size[4];
    ifstream fin(filename, ios::in|ios::binary);
    if(!fin) {
        cerr << "Could not open " << filename << endl;
        exit(EXIT_FAILURE);
    }
    fin.seekg(0x30, ios::beg); // number of frames is stored at this location
    fin.read(size, 4);
    frameCount = size[0] | size[1]<<8 | size[2]<<16 | size[3]<<24;
    fin.close();
    return frameCount;
}

Use it however you like. I’m using it because OpenCV’s cvGetCaptureProperty doesn’t seem to work.