Have a look in the Free Pascal sources. (packages/fpimage), or in SWAG.
Note those FPC sources are more geared towards the Delphi dialect, but it can
show how to.
For a very simple example see:
http://www.stack.nl/~marcov/cntbmp.pp
It writes a bmp to standard output (for web purposes)
The baseunix unit used in the example afaik is only used for getting the
environment string without 255 char limit.