Easily converting an image into C++ / Obj C code..

If you need a way to convert simply a small image into C++ / Obj C code that can be compiled by GCC or any other modern compiler there are a few utilities out there. In my case I was not happy with the results, so I wrote a few lines of code to do this.

What I needed is a simple code that fill an array of char that can then be used by OpenGL or custom pixel processing code. It’s so easy to load an image into an NSBitmapImageRep. Once you have this there are a few accessor like pixelsWide, bitmapData … that lets you play with the pixels and it’s then very easy to generate some text that will be C++ / Obj C compliant.

Here is a small 16 by 16 image converted into code with my utility:

int gImageWidth = 16;
int gImageHeight = 16;
int gImageBits = 24;
unsigned char gImagePixels[] = {
// line 0
0xf2,0xf1,0xf1,0xf1,0xf1,0xf1,0xf1,0xf1,0xf1,0xf1,0xf1,0xf1,
0xf1,0xf1,0xf1,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,
0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xef,0xf0,0xf0,0xef,0xef,0xef,
0xef,0xef,0xef,0xef,0xef,0xef,0xef,0xef,0xef,0xee,0xee,0xee,

// line 1
0xe5,0xe5,0xe5,0xe5,0xe5,0xe5,0xe4,0xe4,0xe4,0xe4,0xe4,0xe4,
0xe4,0xe4,0xe4,0xe4,0xe4,0xe4,0xe4,0xe3,0xe4,0xe3,0xe3,0xe3,
0xe3,0xe3,0xe3,0xe3,0xe3,0xe3,0xe3,0xe3,0xe3,0xe2,0xe2,0xe2,
0xe2,0xe2,0xe2,0xe2,0xe2,0xe2,0xe2,0xe2,0xe2,0xe1,0xe1,0xe1,

// line 2
0xd7,0xd7,0xd7,0xd7,0xd7,0xd7,0xd7,0xd7,0xd7,0xd6,0xd6,0xd6,
0xd6,0xd6,0xd6,0xd6,0xd6,0xd6,0xd6,0xd6,0xd6,0xd5,0xd5,0xd5,
0xd5,0xd5,0xd5,0xd5,0xd5,0xd5,0xd4,0xd5,0xd5,0xd4,0xd4,0xd4,
0xd4,0xd4,0xd4,0xd4,0xd4,0xd4,0xd3,0xd3,0xd4,0xd3,0xd3,0xd3,

And here is the code of the small command line utility:

#import Cocoa/Cocoa.h

int main (int argc, const char * argv[])
{
if (argc >=3)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString * src_file = [[NSString alloc] initWithFormat: @”%s”, argv[1]];
NSData * raw = [NSData dataWithContentsOfFile: src_file];

if (raw)
{
NSBitmapImageRep *rep = [NSBitmapImageRep imageRepWithData: raw];
if (rep)
{
FILE * out = fopen (argv[2], “w”);
if (out)
{
int width = [rep pixelsWide];
int height = [rep pixelsHigh];
int bits = [rep bitsPerPixel];
int rowbytes = [rep bytesPerRow];
unsigned char * src = [rep bitmapData];

fprintf (out, “int gImageWidth = %ld;\n”, width);
fprintf (out, “int gImageHeight = %ld;\n”, height);
fprintf (out, “int gImageBits = %ld;\n”, bits);
fprintf (out, “unsigned char gImagePixels[] = {\n”);
for (int v=0; v

Preview of interface to drive a network of players

I am toying with the idea of a network of players synchronized to a central coordinator since a few months and I have already shared those movies:

New synch experiments at WWDC

Exploring an idea about distributing content on a network of players…

Our goal at ArKaos is to build a new range of products based on such architecture but we want to move forward step by steps and the first public step will be a simple interface to drive a limited pool of players connected to the same network.

Here you can preview what will look like the interface. This application is not even alpha code but I am happy to share it with those of you that are curious.

I used the opportunity when working on this project to experiment with the cocoa tools of Mac OS X. I am a big fan of cross platform programming and I worked with wxWidgets since a few years. Unfortunately because Apple did obsolete carbon we are now looking for new ways of creating our interfaces. I am happy that just within a few days I was able to create this already complex interface without writing too much code. In the end it’s true, interface builder rules and cocoa is a great idea!

Ok now to my prototype, the application is still useless but it demonstrate how to write a simple cue player. You can create list of events (cues) and assign them to computer keys. Then the idea is that when you will press those computer keys those players listening to the network events will start playing those video loops.

So at this time what can be done here is:
– importing static pictures to the cells on the left by drag and dropping from the finder.
– playing with the + / – buttons of the cue editor to add and remove steps of a cue.
– drag and drop visuals from the left cells to the cells of a cue.
– you can edit the layer position, start time and duration of a cue step. You need at this time to use the enter key to validate a new time or duration.

Here is a simple picture to show the interface in action:

Coordinator

Coordinator

Just drag and drop a few pictures on the left cells, create a few cues steps with the + and – buttons. The final prototype will be able to play movie loops across 3 zones of maximum 6 projectors. It will be possible to stack 4 layers of visuals on each zones. This is why by example the popup says z1l1, it means zone 1 layer 1.

I made a quick build if you want to play with this preview app, for Mas OS X only at the moment, download it here:

2009_08_CocoaCoordinator.dmg (208 KB)

Perfectly fluid animations…

At ArKaos we work a lot on the fluidity of the animations produced by our softwares. To do so we multi thread a lot the software and make the best use of the graphical acceleration.

It looks like on the Mac, under 10.5.6 at least, it’s very difficult to have perfectly fluid animations on one monitor while continuing to update a software interface on another monitor. Unfortunately it’s what we want to achieve in all our softwares.

I was able to reduce the problem by creating a simple application that draw a band in one window at 60 FPS. As soon as the system is slowing down you see clearly the speed of the animation being irregular. Because the code is very light, uses no texture, I was expecting this code to run very smoothly.

The function that is called for every frame is as simple as this one:

void display1(void)
{
static int framecount = 0;

glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glLoadIdentity (); /* clear the matrix */
/* viewing transformation */
float x_pos = – ((framecount++ % 60) – 30.0) / 8.0;
gluLookAt (x_pos, 0.0, 5.0, x_pos, 0.0, 0.0, 0.0, 1.0, 0.0);
glScalef (1.0, 2.0, 1.0); /* modeling transformation */

glColor3f(1,1,1);
glBegin(GL_POLYGON);
glVertex2f(-0.5,3);
glVertex2f(-0.5,-3);
glVertex2f(0.5,-3);
glVertex2f(0.5,3);
glEnd();

glFinish ();
}

It simply draw a band like this that cycle in the window:

band

band

Because framecount is incremented for each new frame it has a very visible impact when you miss a frame, this is what I want to see.

Here is the result of sharking the application, untitled and untitled copy are the 2 instances running. You see that the whole machine is almost idle, but very clearly on this picture after 6 frames you see that the time between 2 ticks on the untitled line start to double:

Sharking 60fps animations

Sharking 60fps animations

I have the feeling that the problem is in the WindowServer process but it’s out of my scope to understand what is wrong there.

If you are curious my very simple GLUT sample is here GlutSample.zip

Once you unzip it just run band1 and band2 and move one window on a second monitor you should see the animation become irregular.

Here is the shark file GlutSample.mshark