Fitspng

Fitspng is an utility intended to convert of images in astronomical FITS format into PNG format.

Introduction

FITS format is a general purpose astronomical format which stores measurements of physical quantities. FITS images represents an angular distribution of captured photon events, or intensity of light. Fitspng provides visualisation of FITS images by converting them to PNG format.

Tone mapping

Fitspng applies a global tone mapping technique to transform of a wide dynamical range of FITS files (naturally including HDR) to a limited range of, so called, modern displaying devices and image formats.

The tone mapping includes two steps: a pre-scaling (normalisation), and optional application of an intensity transfer function,

Pre-scaling maps linearly the captured counts, related to optical intensities I (I ∈ ℝ, I ≥ 0 ), to the range 0 ≤ t ≤ 1 by the two parameter transformation:

t = (I - B) / s.

As the parameters, B – black level, and s – sensitivity, has been selected.

B sets a background level in original data. It corresponds to zero in transformed values, and finally to the black colour.

The sensitivity s > 0 adjusts a range of FITS values visible on PNG pictures. It simulates the artificial sensitivity of a detector: high, or low, values leads to a dim, or highlight, images. Sensitivity operates like a gain of an electronic amplifier of a detector. This parameter is a reciprocal quantity to ISO value well-known in classical photography.

The pre-scale transformation should be followed by a cut-off, which assigns all values below the level B to be black, and over the interval to be white:

0 ≤ t ≤ 1

ITT scaling applies an intensity transfer function f(t) (realised by an intensity transfer table (ITT) in the past) on the pre-scaled data t:

τ = f(t).

Various ITT functions are introduced in ITT functions paragraph.

The described tone mapping is applied on all pixels of a frame; one is applied on values for grey-scale, and on the luminosity channel of colour images.

Both B, s parameters can be leaved unspecified: they are estimated by the included machine algorithm described in Tone parameters estimation. A manual setup of the pre-scale parameters is available in two modes:

The absolute scaling is suitable for fine tune by hand, while the relative scaling for some batch processing.

Gamma correction

Finally, a light-sensitivity model, specific to a displaying device, sRGB is applied onto τ values by the colour space specific gamma function Γ(τ):

p = { 255 Γ(τ), 0 ≤ p ≤ 255 },

where p is the final value stored in PNG.

Tone parameters estimation

A reliable initial setup of the scaling parameters B, s is crucial for proper function of Fitspng. A novel approach on base of quantiles of an empirical distribution function has been developed.

Background level is estimated as 25% quantile QB of empirical CDF of observed pixels. It is close to CDF of Normal distribution for the sky, a star free background. The black level is claimed as the quantile:

B = QB(¼)

The pixels included in determination of the CDF are selected from a grid covering full frame (over 30 thousands of data points). The one-quarter choice is a result of empirical experiments.

Light upper level is estimated as 95% quantile of the empirical CDF constructed on base of pixels with values above ≥ 3 D of the background CDF. The selected pixels contains high fraction of star light and another star-gazing objects, which provides the range for observed intensity values.

s0 = QL(95%)

Parameter D is a quantile estimation of dispersion on base of ¼ and ¾ of QB quantiles (QN(½) ≐ 0.6745 is 50% quantile of Normal distribution):

D = [QB(¾) - QB(¼)] / [2 · QN(½)]

Relative parameters q, v are defined by the way:

The relative parameter q ∈ (0,1) is the quantile of QB(q). It is set on ¼ for grey images. Colour images has both B and QB(q) set such way to have guarantied B ≥ 0; CIE 1931 XYZ values are positive by definition.

Common properties of QB(q) are: q = ½ is arithmetic mean, q = ¼ (standard background level) is the mean of absolute deviations under the mean, q=0 or q=1 are minimal and maximal values.

The parameter 0.001 < v < 1000 (mostly) adjusts relative slope against to the pre-defined value s0.

The empirical CDF for a perfectly ordinary picture of the sky, a starry image having its principal part covered by a noise due background, is plotted on the graph below. The vertical axis shows quantiles, the horizontal axis of picture values. The background noise follows Normal distribution up to Q = 0.8; the star light is getting importance over the background above the level.

cdf

The empirical CDF of a perfectly ordinary picture of the sky, see text for description.

histogram

The histogram of the same frame.

ITT functions

Intensity transfer (ITT) functions maps input and output intensities with non-linear weights. It can help to emphasise, or to surprise, desired objects on images. The choice of "right" function depends on taken scene, on photographers intention, and it is so unique as every photography is unique.

Fitspng implements these ITT functions:

Line function does nothing, so it is very universal, fast and the preferable first choice.

S/N like function is very rough approximation of signal-to-noise ratio which offers a good image contrast. For pure counts, the ratio is proportional to reciprocal of relative error of observed values as t / √t, but the square root has singularity in zero. Asinh is a safe approximation of square root for 0 ≤ t ≤ 10.

Function square emphasises details in large diffuse faint areas.

Function tanh simulates logistics curve with a smooth saturation.

Function asinh offers the most wide dynamical range. It keeps faint details white the bright objects are still visible.

Photo emulates the gradation curve of the classical photographic emulsion having a low dynamical range.

Itt

Colour tuning

Colour FITS frames, as defined by Munipack, are recognised.

Saturation The colour saturation of images should be reduced when the relative saturation parameter has value S < 1, or enhanced when S > 1. The saturation of the final frame is computed by formula

S c,

with the chroma c = √[(u-uw)2 + (v-vw)2] specifying the white-point distance in CIELUV colour-space.

Nite vision The human eye sensitivity in high and low light conditions can be artificially simulated by greying of faint parts of images. The night vision has shifted spectral sensitivity into short wavelengths which, together with its narrow broadband, simulates Purkyně effect.

The blend of photopic (daily) and scotopic visions (darkness) is modelled by the weighting function

w = ½ {1 + tanh [(Y - Ym) / wm]},

with night-day vision threshold Ym, and width of wm of the mesopic vision region. The function has been selected by heuristic. There is no a proof of validity.

To derive composition of colour and scotopic images, CIE 1931 XYZ values are converted into monochromatic scotopic channel S

S = 0.3617 Z + 1.1821 Y - 0.8050 X.

Mesopic intensity Ym is a compound of intensities Y and S:

Yn = wY + (1-w)S = Yw + Sw,       with Yw = Yw, Sw = S(1-w).

Colours are derived by chromaticity coordinates xy in xyY colour space (CIE 1931 XYZ, sec. CIE xy chromaticity diagram and the CIE xyY colour space) by a blend of full colours with scotopic channel. Scotopic vision is in degrees of grey having chromatic coordinates of the white-point xw,yw:

xn = [(x/y) Yw + (xw/yw) Sw] / D

yn = Yn / D,

where the denominator is D = Yw / y + Sw / yw.

The scotopic imaging is possible under conditions Ym ≥ 0 and wm > 0. Pictures becomes fully coloured for Ym → 0, or complete in night vision for Ym → ∞. The mesopic regime parameters Ym, wm should be determined empirically by a qualified estimate.

Image resize

An output image can be scaled-down by an integer factor. Every pixel of the scaled image is computed as the arithmetical mean of a square with side given by this factor. The photometric information is preserved while a noise is suppressed during the re-scaling. This sufficient fast method provides high quality images.

Exif information

FITS header meta-information is stored as an EXIF information of PNG files: the EXIF text strings has free format and no formalised form.

Fitspng invocation

Synopsis

fitspng [options] file(s)

Command line options

-r q, v

Relative pre-scaling mode: q, v (0 ≤ q ≤ 1) represents quantiles of a background (the sky) and v relative sensitivity. See Tone parameters estimation.

Parameters can be omitted leaving default values in row. It is specially useful for colour images where q should have its value slightly over zero. The exact text form is "-r ,v" or "-r q,", and should have the comma included to support unambiguous interpretation.

If a colour image is processed, q is determined as the quantile for zero intensity, so it is good idea to leave it unset; just use "-r ,v".

-l B, s
Absolute pre-scaling mode: B, s are directly used for scaling of the output frame. This setup completely disables the internal parameters estimation. See Tone mapping.
-f [line | asinh | snlike | sqr | tanh | photo]
Select a function, see ITT functions section.
-S S
The colour saturation (Colour FITS only).
-n Ym, wm
When used, switch-on, and setup, mode which emulates human night vision (Colour FITS only).
-s s
Shrink image: scale-down the size of the image by the specified factor s > 1: every output pixel is the arithmetic mean of s2 input pixels.
-o filename

Specify an output file name for a single file input only.

If this switch is omitted, the output filename is determined by modification of the input filename(s): suffixes, like *.fits, are replaced by *.png, and the directory path is removed: /space/image.fits is converted to image.png. The approach leaves original data untouched, results are stored in the current working directory (see also Examples).

-B [8|16]

The bit depth of the output PNG frame: 8 (default) or 16 bites per pixel.

A post-processing can be affected by the choice; most utilities doesn't work with 16-bit colour depth correctly.

-v, --verbose
describes what Fitspng currently does.
-h, --help, --version
prints summary of options; the current version.

Examples of usage

Convert a FITS image to PNG:

$ fitspng grey.fits

Convert a FITS image to PNG thumbnail:

$ fitspng -s 10 grey.fits

Emulate the classical photography sensitivity function (gradation curve):

$ fitspng -f photo photo.fits

Create a semi-grey image:

$ fitspng -S 0.2 -o reduced.png colour.fits

Emulate night vision:

$ fitspng -n 1e6,1 -o scotopic.png colour.fits

Generate thumbnails of FITS files in /space storing files under the current directory:

  fitspng -s 10 /space/*.fits

The same result should be emulated with help of shell scripting (providing powerful file name manipulations):

  $ for FITS in /space/*.fits; do
      NAME=$(basename $FITS);
      PNG=${NAME%fits}png;
      fitspng -o $PNG $FITS;
  done

Fitspng has no parallel run support included; however, the execution time on multiprocessor systems should be significantly reduced with help of some external utility:

  $ ls /space/*.fits | xargs -n 1 -P 0 fitspng

Gallery

IMG_5952

#1, Colour image in sRGB

IMG_5952

#2, ITT photography tone applied

IMG_5952

#3, Sensitivity increased

IMG_5952

#4, Sensitivity decreased

IMG_5952

#5, Reduced colour saturation

IMG_5952

#6, Enhanced colour saturation

IMG_5952

#7, Vanished colour saturation

IMG_5952

#8, Night vision

Whole gallery is generated by processing of the reference raw photo IMG_5952.CR2:

$ rawtran IMG_5952.CR2
$ fitspng                   -s 10 -o IMG_5952.png       IMG_5952.fits #1
$ fitspng -f photo -l 0,4e3 -s 10 -o IMG_5952_photo.png IMG_5952.fits #2
$ fitspng -r 0,2            -s 10 -o IMG_5952_r02.png   IMG_5952.fits #3
$ fitspng -r 0,0.5          -s 10 -o IMG_5952_r05.png   IMG_5952.fits #4
$ fitspng -S 0.5            -s 10 -o IMG_5952_S05.png   IMG_5952.fits #5
$ fitspng -S 1.5            -s 10 -o IMG_5952_S15.png   IMG_5952.fits #6
$ fitspng -S 0              -s 10 -o IMG_5952_S0.png    IMG_5952.fits #7
$ fitspng -n 3e10,1         -s 10 -o IMG_5952_nite.png  IMG_5952.fits #8

Download and installation

The tar-ball, or the development repository, is freely available under GPL-3 licence. Both cfitsio and libpng libraries, including files required for development (headers, static libraries), are necessary for building.

A recommended way of installation under Unix-like system is:

$ tar zxf fitspng-U.V.tar.gz
$ cd fitspng/
$ ./configure CFLAGS="-O2 -DNDEBUG"
$ make
# make install

The final step should be executed by root. All files are installed into /usr/local tree. It would be nice to keep the source directory, if uninstall is a desired action.

See also

Munipack is a general utility working with FITS images.

Development notes can be found in my blog.

License

Fitspng is free software licensed under the GNU General Public License. This gives you the freedom to use and modify Fitspng to suit your needs.