DOS version of FITS compression software

  • Overview
  • Getting the software (executable and source code)
  • Using the software
  • Changes made in the port to DOS
  • Overview: People who have attempted compressing FITS images using "standard" compression tools such as gzip, PKZip, WinZip, etc. generally get frustratingly poor compression ratios, and usually decide compression is more trouble than it's worth. This was certainly my attitude, until I became aware of the hcompress image compression package, developed at the Space Telescope Science Institute. For a full description, plus the original source code, look here: 

    hcompress allows the user to select lossless or lossy compression, with control over the amount of loss. The basic algorithm is used as the compression scheme for RealSky and DSS images; in the case of RealSky, a compression ratio of about 100:1 was accepted, while in DSS, a lesser ratio (resulting in better image quality) of about 10:1 was accepted.

    I've ported this program to DOS, and compiled it as a Win32 console application. I've tried it out on several images collected from different sources, and it appears to work well.

    Getting the software (executable and source code): If you want the source code, you should first get the original source code from STScI. All I did was to modify two of the original .c files, and supply a make file for the DOS version. I was able to use everything else "as is".

    You can click here to download the changes to the source code, plus the hcomp and hdecomp executables (about 75 KBytes). Those uninterested in the source code can ignore it; the executables will work just fine "as is". In general, they work as described at the STScI site, but I did have to make some changes to keep Bill Gates happy, as described below.

    Using the software: To compress a FITS file such as, say, '', you could run the following from the DOS prompt:


    The above would create a losslessly-compressed version, '23oct00.cpf'. The compression ratio will depend largely on how noisy the image is. On the images I've tested, it's ranged from about 1.6:1 to as good as 2:1. To decompress it, you would run:

    hdecomp 23oct00.cpf

    Since lossless compression was used, the original '' would be created, byte for byte.

    hcomp has additional options to set the amount of (lossy) compression and the name of the output file. It can be run as

    hcomp -s scale -o output_filename input_filename

    For example,

    hcomp -s 10 -o \comp_img\23oct00.cpf

    to apply mild compression, with the resulting file put in the \comp_img directory. See the STScI documentation for a full discussion of '-s'. Basically, '-s' sets a 'scale' factor; 0 indicates lossless compression, higher values indicate greater loss (and greater compression). Figuring out the right value to use appears to involve some trial and error.

    Aside from the above (new, DOS-only) capabilities, hcomp and hdecomp still have the options that were available in the STScI versions. Files other than FITS images can still be compressed. However, I'm assuming that it's unlikely anyone will compress non-FITS images, so FITS input (and output) are now the default.

    Changes made in the port to DOS: I made as few alterations as possible, and as it turned out, few were needed. Much to my surprise, the code compiled and ran immediately under DOS, with no changes at all. Unfortunately, it produced garbage results.

    There were two reasons for this. First, the file I/O was being done in text mode, not binary mode. (For some idiotic reason, this is the default in DOS.) Second, hcomp and hdecomp relied heavily on sending data through the standard input and output, something that gives grief in DOS-land (partly because these are always opened in text mode, and you can't evade this.) If you look for places where #ifdef DOS appears in my revised versions of hcomp.c and hdecomp.c, you'll see that I was able to evade both restrictions. I also had to make sure that the input and output of both programs could run to user-specified files, instead of through stdin and stdout.

    At present, that's all I've done. But parts of this code appear similar to the H-transform code used in Getimage, and I ported (and heavily revised) that code in my Get_DSS software. The versions in Get_DSS are a lot faster than those in Getimage, and I may eventually move them over to hcomp and hdecomp.

    (24 Nov 2002: Update:) The STScI documentation notes that no effort was made to compress the FITS headers. It's not too important for large images, but it's a problem for small images. I modified the code to allow for some very simple compacting of FITS headers (they're mostly spaces, after all, and could be compressed trivially.)

    Also, if you look in hdecomp.c, you'll see an add_contrast_data() function. This adds a basic histogram to the header, giving brightness values at 1% intervals. This is the same sort of basic histogram provided by the Get_DSS code, and serves the same purpose of making it a little easier to do contrast/brightness adjustment.