Converting PICTs to PNGs

Posted by Brian on February 13, 2010

I recently had the task of finding a way to properly convert thousands of files from the ancient Macintosh PICT image format to a more modern image format. In this case, the desired output was files in the PNG image format.

As you may have noticed, support for the PICT image format is drying up. Starting with Mac OS X v10.5, functionality for rendering PICT images was not available for 64-bit applications. This is really obvious in Snow Leopard, with Preview not being able to open PICT images unless the application is run in 32-bit mode. Even Adobe has indicated that they will begin dropping support for the PICT file format in Photoshop.

Alpha Channel Complications

The biggest challenge I had with converting the PICT images was to find something that would properly handle the alpha channel of the images. While many of the PICT images had a straight (unassociated) alpha channel, a good number of them had an alpha channel that was premultiplied (associated) with a black or white background.

An image with a premultiplied alpha channel is one that has had the color pixels mixed with a background color in proportion to the alpha channel value for that pixel. The best explanation of this that I have found online thus far is Alpha Matting and Premultiplication.

The PICT image format does not include a way to specify if the alpha channel is premultiplied, so most applications either rely on the user to indicate this when the image is loaded or they try to guess based on certain things that are found in the pixel data.

I tested a number of batch image converters, including the sips command line utility in 32-bit mode and the powerful GraphicConverter. Unfortunately, none of these would detect that the alpha channel was premultiplied and would convert it as a straight alpha channel.

The three PNG images below are an example of what would happen when PICT images were converted with an application that would not properly detect the alpha channel. The first image had a straight alpha channel and appears correctly. The second two images were premultiplied over black and white and thus the pixel colors have shifted.

Straight Alpha

Premultiplied over Black

Premultiplied over White

The pict2png Solution

I decided to create my own solution in the form of a simple command line utility written in the C programming language. When given a specified folder, it will find all the PICT images contained within it and convert them to PNG images.

Images with a premultiplied alpha channels are detected and the pixel data adjusted to result in straight alpha channels as required by the PNG image format. Here are the basic steps by which this takes place:

  1. Find the most prevalent background color (pixels that are completely transparent).
  2. Check to see if the partially transparent pixels have color values that would indicate that they have been premultiplied with the alpha channel.
  3. If the image appears to have a premultiplied alpha channel, then correct this by applying the formula: Fg = (Comp - ((1 - A) * Bg)) / A

The formula used to undo the premultiplied alpha channel is the reverse of the standard image composition formula: Comp = (Fg * A) + ((1 - A) * Bg)

An iMac (Early 2009) that I tested pict2png on was able to convert 1000 PICT images in just under a minute. Apple’s new Grand Central Dispatch technology is used to speed up the conversion process, so pict2png requires Mac OS X v10.6 Snow Leopard.

If you wish to give it a try, you may download pict2png from the following location.

pict2png_1.0.4.pkg (26 KB)

Please Note: The software is provided ‘as-is’, without any expressed or implied warranty. In no event will the author be held liable for any damages arising from its use. See the license that applies to the software for more details.

Installing ImageMagick

The pict2png software requires that ImageMagick be installed in the /usr/local folder. A script to install ImageMagick can be found at http://github.com/masterkain/ImageMagick-sl, but needs to be modified in order for the installed ImageMagick to work with pict2png. Simply adjust the line that has the ImageMagick configure arguments to disable support for OpenMP, which conflicts with Grand Central Dispatch:

IMAGEMAGICK_ARGUMENTS="--disable-static --with-modules --without-perl --without-magick-plus-plus --with-quantum-depth=8 --disable-openmp"

To make things even easier for those who wish to try out pict2png, I have built a copy of ImageMagick 6.5.8-10 and its required packages for Snow Leopard or later.

ImageMagick_SL_6.5.8-10.pkg (36 MB)

Further Improvements

If you would like to help improve the pict2png software, or would like to report an issue with it, please visit the repository on GitHub:

http://github.com/brianwells/pict2png

The one thing that is slowing pict2png down is the ImageMagick library, which can only load one image at a time thanks to their use of global locking for many operations. It should be possible to write out the PNG images by using libpng directly instead of going through ImageMagick. However, reading PICT images directly might be a bit trickier.