Sunday, May 10, 2009

Simple image output

Sometimes, it can be nice to have a dead-simple format for bitmap images. If I'm writing some low level code (say, in C) dealing with graphics, at times I will want a quick way to quickly output an image file. Let's say that I'm too lazy to bring in a graphics library that can write actual .pngs . The .bmp format, while simple, is a bit more complicated than a list of rgb values, because of alignment.

I wrote a little program that converts ".simple" images (which are a list of R,G,B bytes) into 24 bit .bmp files. So, your program simply writes a tiny header and three bytes per pixel, and simpletobmp.exe turns this into a .bmp.

This works well from Python, too, when the PIL isn't around.

Here is an example of how to draw a ".simple" image:
FILE * fout;
fout = fopen("test.simple","wb");
fputc('S', fout);
fputc('2', fout);
fputc('4', fout);
int x,y,width,height;
width = 512; height = 256;
fwrite(&width,sizeof(int), 1, fout); 
fwrite(&height,sizeof(int), 1, fout); 

for (y=0; y<height; y++)
{
  for (x=0; x<width; x++)
  {
    fputc( y%256 , fout); //Red
    fputc( x%256 , fout); //Green
    fputc( 0 , fout);          //Blue
  }
}
fclose(fout);


Or, in Python,

import array
fout = open('pyout.simple', 'wb')

chars = array.array('c') #char
chars.append('S')
chars.append('2')
chars.append('4')
chars.tofile(fout)

WIDTH=512; HEIGHT=256
ints = array.array('l') #signed long
ints.append(WIDTH)
ints.append(HEIGHT)
ints.tofile(fout)

bytes = array.array('B') #unsigned char
for y in range(HEIGHT):
 for x in range(WIDTH):
  bytes.append(y%256)
  bytes.append(x%256)
  bytes.append(0)

bytes.tofile(fout)
fout.close()
(This one draws a gradient in red and green. Change the fputc lines in the inner loop to draw the image you want. A common usage is to set up a 2d array of pixels, draw the picture into that array, and then output everything to a file).

Now, one can run
simpletobmp.exe o test.simple test.bmp
to get the image.

This is very similar to how in Linux, one can write a .ppm file, which is, literally, a brief header and list of rgb values. The .ppm format even accepts pixel information in human-readable ascii digits! Sounds ridiculous, but this type of thing can be useful.

Download:
Windows binary
Source LGPL license.

Simpletobmp uses the LGPL bmp_io library by John Burkardt.

Bit packing preprocessor

When working at lower-levels, sometimes it pays to conserve bits. Binary formats sometimes use all of the bits they are given - when I was working with MIDI, I came across this a lot. (If you have a full int, you might as well use all 32 of those guys.)

On a more practical note, 16bit color is often 5bits red, 6bits green, and 5bits blue. A color is stored as a 16 bit integer in the format rrrrrggggggbbbbb, where each letter represents a bit. It isn't difficult to write code to extract these bit fields, but to make it faster and more readable, I wrote some Python to write the C for me. The Python script takes a string like "00rrr000" and outputs C code with the appropriate shifts and masks.

Bit tools. Example usage:
> necessarybits(640)
10 bits are required to store values up to 640
2 ** 10 = 1024

> print tobinary(46)
00101110

> print frombinary('1100_1100') 
204

> pattern('00rrr000', True)
  r is a 3bit number
  Packing:
  assert(r<8);
  unsigned char packed |= r<<3;
  Unpacking:
  unsigned char r = (packed & 0x3f)>>3; //packed & 0b00111111

> pattern('00000bbb')
  b is a 3bit number
  Packing:
  unsigned char packed |= b;
  Unpacking:
  unsigned char b = packed & 0x7; //packed & 0b00000111


I find "00rrr000" to be a lot clearer than '(packed & 0x3f)>>3' .

Python source
(34 lines of quick/unpretty code)

Tuesday, March 31, 2009

Midi (part 4)

Today I released a new project, bmidi to wave.

It can be used to:

  • Turn midi files into great-sounding wav and mp3 files.
  • Play midi files, especially if you are unsatisfied with your current midi out quality.
  • Get information about SoundFont files and preview their voices.

You can find high quality SoundFonts online, and play each instrument in the midi with the SoundFont of your choice, resulting in the best possible sound. Use the mixer to fine-tune the volume and pan of each channel. Also, you can see a score view of notes in a particular track.

It is essentially a frontend for Timidity, the program that does the actual playback. Instead of having to edit the configuration files by hand, though, there is a gui.

This is a program I've been meaning to write for a long time. I first had the idea and initial designs in 2002, when midi files were more common. It didn't take very long to write.

Process management in Python is not too bad thanks to the subprocess module, which has a good interface. I also learned about Python threads through this project, primarily because one wants a responsive GUI while the song is playing and the time slider is moving.

Surprisingly, one of the more complicated parts of the program was allowing playback to start other places in the song. Timidity doesn't do this, and so I have to create a temporary truncated midi file for it to play. I would just chop off all events before a certain time, but because events like instrument change can occur at any time, all of the instruments would be wrong, let alone tempo and pitch bend. Tempo changes can occur at any time, and so it is not simple to correlate a midi tick with clock time.

Midi is a compact, but pretty well-designed format. Not many binary formats from 1982 are around today.

MS Paint Animation

I gave MS Paint the ability to make animations and save them as .avi files.

I wanted to write a "flipbook" program, where you could draw a series of frames and create a simple animation. I think this type of program could be fun for kids.

I was about to write the program in Pygame, but found myself re-implementing many of the standard bitmap editing tools. It's easy to make a rectangle and oval tool, but I didn't really feel like making the fill tool or selection tool. So, instead, I used MSPaint as part of the interface to the program. (This is completely a hack, and the resulting program isn't robust, but it was kind of interesting to do). See a video of how to use it - you can move from frame to frame, duplicate the current frame, and play the animation.

I'm trying to make this look like just one program. A lot is going on behind the scenes. The program is a c# app that, first, launches Paint. It has a window style that causes it to be on top of other windows. The program uses many Windows API SendKey calls to send key events to Paint. When you move from one frame to the next, it does the following:
  • Tell paint to select all (Ctrl A), and cut (Ctrl X)
  • Take that image from the clipboard and save it to a .png file
  • Open the next .png file in memory and put it in clipboard
  • Tell paint to paste (Ctrl V) and deselect (Esc)
The other operations are done with a similar series of events. I had to tune the timing; the c# program sleeps while waiting for Paint. The play preview actually opens up a borderless c# window that is positioned so that it appears above the image, and cycles through the images.

It ended up working. The Win api gives you almost too much to play with. Now I know that c# apps can send simulated keystrokes to other processes, for semi-practical purposes.

Friday, March 20, 2009

Fishsquish

I made an arcade-style game. You have to quickly out-maneuver your enemies and push blocks to squish them.

I was influenced by an old Mac shareware game, but I've added some twists. The blocks have numbers, and if you can add to 15, it will stun your enemies and give you points.

The game is written using Pygame. I've also ported it to the One Laptop Per Child XO.

I'm going to replace the graphics and sound effects; the ones there now are essentially placeholders. I'll also rewrite some of the levels.



Download. To play it, you need a recent version of Python, and for Pygame to be installed. (Sorry, if you don't already have Pygame you'll have to get it.) Works in Windows and Linux.

When the game is more complete I'll post a Windows binary.

Sunday, February 22, 2009

Midi (part 3) Tunescript

Here is another midi project done in my spare time. Tunescript is a musical toy, where you can enter a list of notes to create a song. Unlike other interfaces like this, though, tunescript supports a lot of features like multiple tracks and instruments, chords, percussion, accented notes, and even pitch bends. Also, I put much thought into the syntax.

For a lot of information, many examples, and to download, visit this page.

It is pretty fun to invent a domain specific language. Because the language doesn't (yet) have nested constructs, I don't need full parsing. I came up with a nice way to interpret the input. It works kind of like a finite state machine that is receiving a stream of instructions. For example, the character 'b' can mean either flat, as in 'Ab', or the note 'b', but there is no ambiguity, because the symbol 'A' causes a transition to a state that can accept the 'b'. Adding multiple tracks ended up being pretty simple, because I just use simple string operations to split the tracks.

In more detail, the core of my interpreter looks something like this. The use of the while loop isn't very good and should probably be made into a for loop, but for some reason I was thinking of gotos, perhaps because of the underlying finite-state-machine influence.

...main loop...
  while s!='':
    result, s = self.pullFullNote(s, track)
    if result: continue
    
    result, s = self.pullFullNoteSet(s, track)
    if result: continue
    
    result, s = self.pullFullModOctave(s, track)
    if result: continue
    
    #if i get here, i couldn't interpret something, throw an error.
  
def pullFullNote(self, s, track):
  if it is not a note,
    return False, s
  
  #otherwise, consume some of the characters from s
  next_s = s[2:]
  
  #add the note to the track
  self.trackobjects[track].addnote()...
  
  return True, next_s



What is nice is that this pattern can be followed repeatedly on smaller levels. The pullFullNote() can call pullPitch() or pullVolumeDuration() in just the same way, and those can themselves call pull functions. If pullVolumeDuration() doesn't see a match, it simply returns False with the original string given. Essentially, the benefit is that there is no need to be explicitly asking "can the next thing be a note?", because the pullNote style functions will smoothly drop through when the next thing is not a note. I don't think I'm explaining this well, but it is all there in interpretsyntax.py if you are interested.

Midi (part 2) Scoreview

For fun, I wrote a program that visualizes the contents of Midi files.
The "score view" is just a Tkinter canvas on which lines and ovals are drawn. None of the graphics are bitmaps (except the clefs), which means that there is freedom to quickly zoom in and out. The sharp signs are actually the text "#" drawn at that point. Writing a custom coordinate translation made this code so much easier. When I specify y coordinates, they are given in units where 2 units is the height of between staff lines. So, moving a note up or down just means incrementing or decrementing its position, and only the lowest level of code needs to know about the actual pixel coordinates.

This tool can be a useful way to explore the contents of a midi song. Besides showing the score for a track, it can also get channel information:

Also, one can view all of the midi events in a track, in a human-readable format:

For a lot of information, and to download, visit this page.

If you've been wondering, yes, the eventual goal is to create a midi editor. This project isn't high priority, though.