Improving FITS file / astrophotography support in GIMP
FITS (Flexible Image Transport System) is a data file used in scientific imaging, primarily Astronomy. The standard was created by NASA and the International Astronomical Union and is used to publish datasets such as Hubble and Chandra imagery, and the Sloan Digital Sky Survey. It's also one of the formats used by the Library of Congress to archive historical data. The format has been fairly stable for 20 years.
GIMP advertises itself as "supporting FITS files" but this is incomplete and clunky. Almost nothing has been done to improve this support for at least four years. There is no need to write a new import parser, existing well-maintained libraries (such as cfitsio) provide most of the features I will be discussing here. (There is also a Python port/wrapper called 'fitsio')
It's important to note that FITS is a scientific data format, and has capabilities such as storing multiple 'images' per file in 8, 16, 32 and 64 bit integer types, as well as floating point in single and double precision for single greyscale images. (Essentially "high dynamic range" support since 1993) A relatively recent extension to the format allows for 24, 48 and 96 bit RGB triplets. It's possible to store 'image cubes' with 3 or more dimensions for multispectral or time-series images. Each image can have extensive metadata attached, using standard or custom fields to indicate how the image maps to the sky, wavelength and bandwidth of the light, exposure settings, and many other issues relevant to using the data in a scientific context.
The format also has three compression types which are lossless for integer images, and 'scientifically lossy' for floating-point data. Formats such as JPEG are 'human lossy' and discard data that humans are insensitive to, such as smoothing away single-pixel dots as 'noise'. In astronomy, that single-pixel dot is called 'data' and might be the result of months of painstaking observation.
Note that there is very little need for GIMP to produce or edit FITS files... they are the output of specialized astronomy software or data teams and there is no real need to modify them... merely to load files for the purposes of viewing the imagery and converting them into more friendly formats for use in documents or on web sites.
Full details on the format and links to all major libraries are available here: https://fits.gsfc.nasa.gov/fits_home.html
Current Support
At the moment, GIMP will only load uncompressed greyscale FITS files in integer and floating point format. When opening a file with multiple images (known as HDU's) each image is instanced in a separate window. For FITS files that contain many image HDUs, the screen quickly fills up with (potentially hundreds of) windows. Each window is 'greyscale recurved' from min/black to max/white independently.
Description of the feature
These features are fairly essential, have minimal impact on the UI, and should be easily implementable by upgrading the library used to load FITS files:
- Support for compressed image formats, specifically HCOMPRESS, RICE, PLIO and GZIP
- Change the behavior to be equivalent to "open as layers" instead of new windows for each HDU / image plane.
- Optionally create 'layer groups' which correspond to the structure of the FITS file. (eg: one group per 3D image block)
- Allow all layers (or per group) to be 'recurved' using the same global minimum and maximum value so that relative pixel values across layers are equivalent.
- Name each layer with the HDU name, rather than the sequential number currently used in new windows.
- Optionally support the new 'packed integer' RGB formats
These features would require more extensive changes to UI, and are less important:
- Optionally show the file/HDU metadata in the Image...Metadata dialog.
- Optionally support colorization and merging of image layers on load, either from wavelength metadata in the file, or saved color profiles
- Optionally choose subsets of the images to import from a file, such as when there are multiple HDU's each containing an 3D image 'cube'. (Eg, to create an animation from one color plane, or to pick one 'frame' from a time series that might be gigabytes in size)
Implementing the compressed image formats is getting increasingly critical. A modern DSLR camera can easily generate 100MB per image uncompressed, and this can be reduced by 10 to 100 times(!) by the specialist lossless compressors in FITS. (astrophotography is mostly a smattering of bright points on a black background and compresses very well) I have included a set of small FITS images of the M13 globular cluster compressed in various ways. GIMP currently will only open the two uncompressed (integer and real) variants.
Fixing this one issue is probably the most critical need; I would expect that the vast majority of FITS image data is now being stored in compressed format, and GIMP is unable to open any of it.
Use Cases
The most common use of GIMP by astrophotographers is to create color images suitable for publication from a set of 'multispectral' data layers. (usually created by taking photographs through a series of color filters) In the simplest case this might be Red, Green and Blue exposures, but many instruments record infrared, UV, X-Ray, radio or other non-visible wavelengths. People studying the Sun use narrowband 'hydrogen line' filters to see specific parts of the solar atmosphere. Or the layers might have been created from a mostly-standard RGB camera with high dynamic range.
Doing this process manually is possible using the standard GIMP color recurve tools (and is much of the 'art' of creating good astro images) but gets tedious if you want to colorize many similar images, or merely want a preview. You can ignore the task of aligning the images... this is non-trivial and best handled by other specialist software, if it hasn't already been done by hardware means. Just assume that sets of images correspond spatially, and are the same size.
The simplest version of this would read the wavelength metadata from the FITS file and pick the closest RGB equivalent, as an alternative to the current greyscale result. Or to 'order' the wavelengths and map them to red/green/blue colored 'lighten only' layers (if there are three of them) or along the visible rainbow (spectral compression) if there are several. A single extra drop-down option in the existing import dialog box would be enough to support these extra behaviors.
You could even define your own 'custom' metadata tags (eg: "GIMP_COLOR", "GIMP_RANGE", "GIMP_GAMMACURVE" etc) which, if present, set those options. (with greyscale being the default) Users who are generating their own FITS files will be able to add those fields in their capture software, or use common command-line tools to update the metadata in bulk before loading into GIMP. Then they can tweak the layers as appropriate and save to human-oriented formats.
Another fairly automatic option would be to look for names like 'red' 'green' or 'blue' in the HDU title, or an option to treat image cubes with 3 'layers' as RGB images. (or spread them along the spectrum if there are more)
More sophisticated options involve storing the color mapping as preference sets in the UI dialog. (Astronomers will typically deal with data from a couple of common sources) Technically it's even possible to store these preferences in the FITS file as data tables, but there is no defined standard for that I am aware of. (You could invent one!)
This is a potentially endless rabbit-hole though, so don't fall in too far. So long as you get all the FITS 'layers' into a single GIMP image with some kind of basic colorization, you've saved the user a tremendous amount of work compared to what's already there.
For some example FITS files, take a look at the NASA GSFC page: https://fits.gsfc.nasa.gov/fits_samples.html