C code – save array to TGA image file

This snippet will write a TGA image file for you. RBitmap is a struct which has a width * height, and data (simply an array of an array, each pixel being a 32 bit int)

TGA is one of the simplest image file formats out there, so it’s great if you want a quick & dirty way of saving an image to disk.

 // 32 bit image
 typedef struct {
 int width, height;
 int** data;
 } RBitmap;

int write_truecolor_tga( RBitmap* data ) {
 FILE *fp = fopen("out.tga", "w");
 if (fp == NULL) return 0;

// The image header
 char header[ 18 ] = { 0 }; // char = byte
 header[ 2 ] = 2; // truecolor
 header[ 12 ] = data->width & 0xFF;
 header[ 13 ] = (data->width >> 8) & 0xFF;
 header[ 14 ] = data->height & 0xFF;
 header[ 15 ] = (data->height >> 8) & 0xFF;
 header[ 16 ] = 24; // bits per pixel

fwrite((const char*)&header, 1, sizeof(header), fp);

// The image data is stored bottom-to-top, left-to-right
 for (int y = data->height -1; y >= 0; y--)
 for (int x = 0; x < data->width; x++)
 {
 char b = (data->data[x][y] & 0x0000FF);
 char g = (data->data[x][y] & 0x00FF00) >> 8;
 char r = (data->data[x][y] & 0xFF0000) >> 16;
 putc((int)(b & 0xFF),fp);
 putc((int)(g & 0xFF),fp);
 putc((int)(r & 0xFF),fp);
 }

// The file footer
 static const char footer[ 26 ] =
 "\0\0\0\0" // no extension area
 "\0\0\0\0" // no developer directory
 "TRUEVISION-XFILE" // yep, this is a TGA file
 ".";
 fwrite((const char*)&footer, 1, sizeof(footer), fp);

fclose(fp);
 return 1;
 }

Creating an email image using ImageMagick

Most of us hate spam, and we know that if we put a plaintext email address anywhere on the Internet, some evil person is going to find it.

I’ve been putting my emails in images for a long time, and thought I’d show you how I do it.

I’m using a Mac, and you may wonder how difficult it was to install ImageMagick, but it wasn’t hard at all, especially if you have Homebrew (I highly suggest you get this if you don’t have it already).

If you haven’t installed ImageMagick, and you’ve installed Homebrew, then all you need to do is type the following command into your Terminal:

brew install imagemagick

Run the following command once you’ve successfully installed ImageMagick:

brew install ghostscript

Now you should be able to use ImageMagick to create image-based emails by running the following line in Terminal:

echo -n "youremail@domain.com" | convert -background none -gravity center -size x20 -pointsize 15 label:@-  output.png

This will be the result:
Email address output using ImageMagick

What I normally do next is use the Email Address Encoder website to encode the email string “youremail@domain.com” so that I can put it into a clickable link.

<a href="mailto:&#121;&#111;&#117;&#114;&#101;&#109;&#097;&#105;&#108;&#064;&#100;&#111;&#109;&#097;&#105;&#110;&#046;&#099;&#111;&#109;"><img src="output.png" /></a>

The final outcome (click it):

I am currently using this in practice on my other site, http://veb.co.nz

Hope this helps someone out there! As always, if you find a bug in this mini-tutorial, or an even better way of doing things, please let me know.

Android device not showing up

If your android device is not showing up on Mac OS X when you try and run your application from Eclipse (or you can’t see your device in the list in the command line) you need to change the USB settings.

Firstly, make sure you’ve read this: running your application on a real device.

If that still doesn’t do anything, try the following:

Go to Settings > Storage > Options > USB computer connection and change the “Connect as” to “Camera (PTP)” not ”Media Device (MTP)”.

I have a Galaxy Nexus, and this worked perfectly for me. As soon as I checked the option, a window popped up on my computer with all my photos — but it meant I could see my device in Eclipse/adb and run the application on my phone, without having to use the emulator.

Hope this helps! I think the PTP option does the same trick as the old “Charge only” option Android used to have.

EDIT: I just noticed if I change my phone setting back to MTP, it will now show up. Both settings now work. So I guess if you’re having trouble, flick between the two settings and see what happens.

How to lock your Mac when AFK

To lock your mac (in the same way win+L does on windows), press control + shift + eject (⏏)

Alternatively, you can always set up a hot corner:

Desktop and Screensaver on Mac OS X

Desktop and Screensaver on Mac OS X

Next, click the “Screen Saver” tab, and on the lower left corner, you’ll see a button called “Hot Corners”.

Simply choose a corner where you’d like your screensaver to start:

Hot Corner Screensaver

Hot Corner Screensaver

This means if you simply move your mouse to the top left of your screen, the screensaver turns on and if you’ve set your screensaver to resume with a password, that’s a nice quick and effective way of locking your computer.