Extensions to PIL 1.0

Those extension enable PIL to handle Radiance image files and the data within. Radiance is a synthetic image generation software package written by Greg Ward-Larson (no, not the Greg Ward known to most of the Python community). This is the most advanced and reliable suite of programs for lighting simulation inside and outsite of the built environment. Check the Radiance homepage or www.schorsch.com for more information.

All of this is experimental and only tested on my Linux system so far (SuSE 6.2). One of the next steps will be to try all this on NT and Solaris. The first step is floating point color support, which is the basis for importing and exporting Radiance format files, and mapping of images from floating point color to other formats (currently only RGB).

All my modifications and extensions to PIL are Copyright © Georg Mischler 1999. License to use and modify and redistribute them for any purpose is hereby given, provided that the copyright information is included in an appropriate way in the redistributed package. Of course, I'd appreciate if you send any enhancements of fixes back to me.

To install, apply all the patches and activate one or several of the related #defines in Image.h.

activates floating point color support.
activates Radiance format image file support.
activates a module to convert Radiance image data to other formats.

Floating Point Color

#define WITH_FLOATCOL in Image.h. This creates a new image type RGBf, with
  im->bands = 3;
  im->pixelsize = 12;
  im->linesize = xsize * 12;
  im->type = IMAGING_TYPE_FLOAT32X;
  im->image32f = (FLOAT32 **) im->image;
I had to put image32f at the end of ImagingMemoryInstance, since tkImaging.c seems to depend on the layout of that struct - eeek!

After this, you can create floating point color images of type RGBf, and apply a few operations on them. At the moment, those are getpixel(), transpose(Image.FLIP_XXXX_YYYY), and copy(). Allmost everything else is very likely to dump core!

Radiance Codec

#define WITH_RADIANCE in Image.h and add the object files RadianceDecode.c and RadianceEncode.c to libImaging/Makefile. You also need the RadianceImagePlugin.py in your PIL directory.

Use of this plugin is straightforward as with most others. Im.info will hold the information from the file header. Images are read from old style chunk encoded format and and new style byte encoded format transparently. Written files are byte encoded, unless they have a width of less than 8 pixels or more than 32767 pixels, in which case they will be stored in plain format without any encoding (ie. uncompressed).

The plugin will currently _accept() all files, although newer files usually have the string "#?RADIANCE" at their beginning. Files created with older versions of Radiance don't have this header, and can't reliably be recognized based on the 16 bytes that Image.py provides for that purpose. I'm not quite sure what to do abou this. The most elegant solution would be to give the _accept() function of each plugin a file object instead of a string, so that they can decide how much data they need locally. RadianceImagePlugin.py would require up to a dozen or more lines of text read from the file to make sure it is valid in a reliable way.

I'm still looking for a good way to support loading partial files, but PIL makes this a little difficult. It reads the file data in chunks and doesn't tell the the decoder if there is more to come or not for each of those chunks. There may be a hack possible, to guess this from the size of the chunk we get in each turn, but I'll have to check out how realiable this approach will work. We want to see the contents of partially rendered files as well, don't we?

Floating Point Image Converter

#include WITH_RADIANCE_CONVERT in Image.h and add the file RadianceConvert.c ray/color.c, ray/tonemap.c and ray/spec_rgb.c to the libImaging/Makefile.

The files in the ray/ subdirectory are from the Radiance distribution. Radiance can be downloaded from The Radiance Homepage for free, but you are not allowed to redistribute it to anyone else without a written permission by the Regents of the University of California.

Don't ask me if this makes any sense, but it still keeps me from including those files here, so that you'll have to get them there yourself if you want to try this out. The full package has roughly 10 MB. you only need a few files for this PIL extension here to work, all of which reside in the ray/src/common/ directory of the distribution and go to Imaging-1.0/libImaging/ray/:

Since Radiance images store their values in radiance (sic!) units, instead of "brightness" as other formats, we need some way to convert the radiometric information we got into photometric values first, and then into RGB luma (or whatever) to display on screen. Those who need more information about this radiometry/photometry stuff can check out The Lighting Design Glossary).

This module is very experimental, since I have yet to understand most of what happens in the conversion process. More features probably to come.


Since I haven't fully understood the workings of Makefile.pre.in and Setup.in as used in Python and (partly) PIL, I'll just provide the patches to PIL 1.0 and my additional files, excluding those parts that origin in the Radiance distribution (only the converter needs external files).

The patches and my additional files are available for download (pilray.tgz 82054 bytes). The archive will expand into a directory named "pilray".

The RadianceImagePlugin.py and RadianceImageConvert.py files go into your PIL subdirectory.

The file pyradconv.c goes into the main Imaging directory. It is #included into _imaging.c, which is not very elegant, but turned out to be the simplest way to do it, since it needs much information that is not exported from _imaging.c, while most of it's own contents must be visible there.

The files RadianceConvert.h, RadianceConvert.c, RadianceEncode.c and RadianceDecode.c go into the libImaging directory.

The patch files must be applied to the equally named files throughout the package.

I have included a small example image named "alberta.pic". You will find more example Radiance format images in the ray/lib[/lib] directory of the Radiance distribution, or in the dowload section of The Radiance Homepage.

I'd appreciate any comments on all of the above and whatever I might have forgotten!

Georg Mischler

back to schorsch's quarters

All Mongolian Recipes | The Mongolian Yurt | All of Mongolia: Adventure Travel