Browsed by
Month: November 2012

Convert an OpenCV 2 Image to an Allegro 5 Image In C/C++

Convert an OpenCV 2 Image to an Allegro 5 Image In C/C++

Just a quick sample for converting an OpenCV 2 image (Mat) to an Allegro 5 image (ALLEGRO_BITMAP).

First we need to setup some things and have places to store some stuff:

#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#include <cv.h>
#include <highgui.h>

cv::VideoCapture video([device number/filename]);
cv::Mat frame;
ALLEGRO_BITMAP *image = al_create_bitmap([width], [height]);

Next the guts:

video >> frame;
if ( !frame.empty() ) {
	for ( int y = 0; y < [height]; y++ ) {
		for ( int x = 0; x < [width]; x++ ) {
			cv::Vec3b &pixel =, x);
			al_put_pixel(x, y, al_map_rgb(pixel[2], pixel[1], pixel[0]));

A few notes:

  • OpenCV 2 does not often work in RGB unless you make it. It is typically the reverse, BGR. Unless you have a specific need I see no reason not to do the conversion on-the-fly as above.
  • This sample assumes everything is the same width, height, color depth, ect, so watch out for that. Allegro, in particular, may slow to a crawl if you do not watch your conversions.
  • I am very not happy with the performance of this so it does need some work in that respect. It does, however, work very well otherwise. My goal is to get my Atom-based netbook running this smoothly. The Raspberry Pi may be a pipe dream but I am going to try.
  • This was tested in Linux with hardware I know what to expect out of. If there is any chance your webcam/video/whatever may return something other than a 24-bit (uint8, uint8, uint8) BGR color space you will need to account for that. Both OpenCV and Allegro have a number of functions/macros for that kind of thing.

This is mostly for my own notes but I figured someone else might also be interested. None of this is meant to be complete but, if you are struggling like I was, this should be all you need to pass that hurdle. Give a man a fish… alright, back to my cold, week-old “chinese” food and root beer.

Update 2012.11.28
After some more experimentation (and a nudge in the right direction from Peter Wang) I have tweaked the guts and it now runs much, much faster:

video >> frame;
if ( !frame.empty() ) {
	for ( int y = 0; y < [height]; y++ ) {
		for ( int x = 0; x < [width]; x++ ) {
			uint32_t *ptr32 = (uint32_t *)region->data + x + y * (region->pitch / 4);
			*ptr32 = ([y * webcam_width * 3 + x * 3 + 0] << 16) | ([y * webcam_width * 3 + x * 3 + 1] << 8) | ([y * webcam_width * 3 + x * 3 + 2] << 0);

Encrypted Linux Mint 13 and Linux Mint 14 Install

Encrypted Linux Mint 13 and Linux Mint 14 Install

As an updated version of my previous article, here are the steps I took to get an encrypted Linux Mint 14 install. This article should also be used for Linux Mint 13 as I discovered some very minor issues with the old method (seriously, they were so minor I would not bother reinstalling if you were not already planning to).

So, zzzoooooommm:

  • Boot into any Linux Mint 13 or Linux Mint 14 installation disc.
  • Setup our storage:
    • In a terminal install and load the required tools: sudo apt-get install -y cryptsetup lvm2 && sudo modprobe dm-crypt
    • In a terminal partition sda: ( echo "o" ; echo "n" ; echo "p" ; echo "1" ; echo "" ; echo "+256M" ; echo "n" ; echo "p" ; echo "2" ; echo ""; echo ""; echo "w" ) | sudo fdisk /dev/sda
      • Some might call a 256MB /boot partition a bit excessive. Storage is cheap so it does not bother me too much but you could go down to ~64MB or so. Resizing an encrypted partition is not as easy as resizing an unencrypted one so if you are unsure ~128MB might be a better minimum.
      • This will wipe all of sda.
    • [Optional] In a terminal, if you are very paranoid, fill your encrypted partition with random garbage using one of these:
      • Much faster: sudo dd if=/dev/urandom of=/dev/sda2 bs=1M
      • More secure: sudo shred /dev/sda2
    • In a terminal create an encrypted LUKS device: sudo cryptsetup luksFormat --cipher aes-xts-plain64 --key-size 512 --verify-passphrase /dev/sda2
    • In a terminal open the encrypted device: sudo cryptsetup luksOpen /dev/sda2 system
      If you would like to do multiple installations you can replace system with virtually any name you like so long as you replace it with the same name throughout this article. I suggest you use the inteded hostname either way.
    • In a terminal create your encrypted logical volumes: sudo vgcreate system /dev/mapper/system && sudo lvcreate -n root -l 100%FREE system
    • In a terminal format the partitions: sudo mkfs.ext2 /dev/sda1 ; sudo mkfs.ext4 /dev/mapper/system-root
      • I always like to specify -m 0 for both filesystems (turns off the reserved blocks percentage). If everything explodes, for some reason, and I can not boot I can always get in with a LiveCD.
  • Install Linux Mint 14:
    • Open the Install Linux Mint shortcut on the desktop.
    • When asked about the the automatic partitioning select Something else.
    • Select /dev/sda1 and click the Change… button.
      • Under Use as select Ext2 file system.
      • Under Mount point select /boot.
    • Select /dev/mapper/system-root and click the Change… button.
      • Under Use as select Ext4 journaling file system.
      • Under Mount point select /.
    • Make sure /dev/sda is selected for Device for boot loader installation.
    • Click Install Now.
      • When it complains about the swap space just continue as we will do this later.
      • When it complains about the the existing partitions not being formatted just continue. We formatted them in a previous step but if you like you can do it again here to get the installer filesystem defaults.
    • [Optional] When you get to the Who are you screen check Log in automatically. Since you will need to enter a passphrase to unencrypt the disk there is no threat if the machine falls into the wrong hands (there are a lot of ninjas in my apartment). I recommend setting the user password to match the encrypted passphrase for simplicities sake.
    • When the installation is completed and you are prompted to restart select Continue Testing.
  • Ready new Linux Mint 14 installation:
    • In a terminal mount new installation: sudo mount /dev/mapper/system-root /mnt && sudo mount /dev/sda1 /mnt/boot && sudo mount -o bind /dev /mnt/dev && sudo mount -o bind /sys /mnt/sys && sudo mount -t proc proc /mnt/proc
    • In a terminal change into new installation: sudo chroot /mnt
    • In a terminal setup network resolution: echo -e "nameserver\nnameserver" >> /etc/resolv.conf
    • In a terminal install and load the required tools: apt-get install -y cryptsetup lvm2
    • In a terminal setup our configuration for booting: echo "system UUID=$(ls -l /dev/disk/by-uuid | grep sda2 | cut -d ' ' -f 9) none luks" > /etc/crypttab && update-initramfs -u -k all
    • [Optional] In a terminal add swap: dd if=/dev/zero of=/swap bs=1M count=$((`free -m | grep -e "^Mem:" | sed -e 's/^Mem: *//' -e 's/  *.*//'` * 2)) ; chown root:root /swap ; chmod 600 /swap ; mkswap /swap ; echo "/swap none swap sw 0 0" >> /etc/fstab
    • In a terminal change back to LiveCD: exit
    • In a terminal unmount new installation: sudo umount /mnt/proc ; sudo umount /mnt/sys ; sudo umount /mnt/dev ; sudo umount /mnt/boot && sudo umount /mnt
  • Reboot into your new, encrypted installation.

A few notes:

  • If you already have an installation you want to encrypt without a fresh install the steps should be very similar. You will need to setup GRUB 2 yourself as, per this article, the Linux Mint 14 installer does it for you. I have not experimented with it but it should not be too hard to figure out.
  • These instructions may very well work with other distros, too, but I have not tested that.
  • This can all probably be stuck into a script. Maybe I will some time down the road (hell, I do enough of these a month).
Compile Allegro 5.0.x on Linux Mint and Ubuntu

Compile Allegro 5.0.x on Linux Mint and Ubuntu

As a sister article to my Cross Compile Allegro 5 Programs in Linux for Windows post, here are the steps I took to get Allegro 5 installed on Linux Mint 13, Linux Mint 14, and Ubuntu 12.10:

  1. Download and extract the latest .tar.gz-compressed source.
  2. Install the required packages: sudo apt-get install -y cmake g++ freeglut3-dev libxcursor-dev libpng12-dev libjpeg-dev libfreetype6-dev libgtk2.0-dev libasound2-dev libpulse-dev libopenal-dev libflac-dev libdumb1-dev libvorbis-dev libphysfs-dev
    • [Note] Would be a good idea to do a sudo apt-get update first.
  3. Create a workspace: mkdir "build" && cd "build/"
  4. Create make files: cmake "../"
    • [Note] By default cmake will want to configure make for a release shared build. If you want a debug build you will need -DCMAKE_BUILD_TYPE=Debug or -DCMAKE_BUILD_TYPE=Profile for a profiling build.
  5. Compile: make
    • [Optional] By default make will not eat up all the processing power it can. Add -j# to change this behavior, where # is the number of job you would like to have running in parallel. If you machine is more or less idle the number of processors available should not hurt anything. If you are using your machine you might want to some half that number instead.
  6. Install to respective paths: sudo make install && sudo ldconfig
    • [Optional] Recommended if you are unsure as to why this step is optional.

If you want to compile an Allegro 5 C++ application– assuming you completed all the steps above and have g++ installed– you can run g++ [source file(s)] -o [output] `pkg-config --libs allegro-5.0`. There are, of course, many more Allegro 5 add-ons (check out pkg-config --list-all | grep allegro) but I will leave using those up to you to discover on your own.

As of this writing Allegro 5 v5.0.8 was the latest version.

Update 2012.11.28
Seems I already had some things installed from some other projects so I did not notice some missing dependencies. Thanks to weapon_S and sorry about that.