Programming in PostScript

 

Calibration target created using PS. Corners are detected and at a known distances apart, allowing intrinsic camera parameters to be determined.

Recently I needed to design and create a calibration grid for calibrating a camera.  The calibration grid was going to be very simple, just composed of squares, but their locations needed to be precise.  I couldn’t just slap something together in power point and call it good enough.

After considering a few options, I decided to give writing my own PostScript (PS) document from scratch a try.  PS is a document standard best known for printers.  Almost all laser printers can print PS documents.  If you have ever looked at a PS document as raw text you know it’s ugly.  Turned out to not be all that bad to work with after all.

PS is in fact a real programming language and is Turing complete.  Someone has even coded up the game of life, which you can run on your favorite or not so favorite printer!  Once you get used to how PS loads everything from the stack, it’s not all that bad of a scripting language.

The two tutorials/documents which I found useful are:

Less than two hours after I started I now know the basics of PS and created the PS document below.  See in line comments for explanation of the code.

%!PS
% paper size
/pagewidth 8.5 72 mul def
/pageheight 10 72 mul def 
 
% ----- Define centimeter
/cm {28.346 mul} def
% ----- square width
/w {3 cm} def
/ww {w 2 mul} def
/r {w 2 div} def
% ----- Define procedure for drawing a box
/box {w 0 rlineto 0 w rlineto -1 w mul 0 rlineto closepath} def
 
% take in account the size of a square
/pagewidth pagewidth ww sub def
/pageheight pageheight w sub def
 
% draw all the boxes it can across a single row
/rowboxes {w ww pagewidth {newpath y moveto box fill} for} def
 
% increments the y variable and draws all the rows
w ww pageheight { /y exch def rowboxes } for
 
showpage