I'm using Magick++ to read FITS files. I have lots of test images from Fitswork, PixInsight, ImageTOOLScs and ImageMagick itself.
Currently I'm testing the 16 bit signed format (BITPIX=16). In this case you normally have (like e. g. PixInsight does)
BZERO=32768
BSCALE=1
This achieves that the signed integer value contained in the FITS file is added to 32768 and then multiplicated with 1. The result is an unsigned 16 bit integer (0..65535) that can be used to represent the image data internally in a software.
For debugging purposes I created an image containing 256 gray values distributed from 0..65535. After the following code (simplified)
Code: Select all
Image img;
img.read(path);
Magick::Pixels* view = Pixels(img);
int width = img.columns();
int height = img.rows();
PixelPacket* pixels = view->get(0,0,width, height);
As said, my test FITS image contains values from -32768 to 32767 which I verified using a hex editor. When reading this test image with the above code, the array contains no values below 0x5555. The values are distributed from 0x5555 (which was black in my test image) to 0xFFFF. This makes the image considerably brighter (less contrast). This occurs independent from other tags in the header. Googling this problem I found that people simply stretch FITS images after reading them to compensate this bug. But there's loss of data (brightness resolution). In addition it is not guaranteed that stretching the image generates the same image as the original image.
My current workaround is that I make a temporary copy of the image, verify that it is of type BITPIX=16 and then patch the FITS header by setting BZERO=21846 (0xAAAA / 2) and removing any DATAMIN or DATAMAX entries. When I read in such a patched image with the above code, the image data is all well. This works with images from PixInsight, Fitswork and ImageMagick itself (ImageMagick cannot read in correctly its own FITS files for BITPIX>=16). But this is just a workaround, I'm sure there are cases where it does not work (for example if BSCALE is different from 1 in the input image).
Its very simple to reproduce this bug. Convert any 16 bit TIF image to FITS (with ImageMagick), then convert it back to TIF and it will be lighter. Reason is the above described problem. But be sure not to use IrfanView, because IrfanView stretches images. Use Fitswork, Photoshop or - best - a hex editor. The lowest value will be 0x5555.
Kind regards
Stefan