index f5f6c46..819158b 100644 (file)
 #include "fits2jpeg.h"
 
 /*---------------------------------------------------------------------------*
+ * READ_FITS: To reads data from a fits image file.. (isn't that obvious?)
+ *---------------------------------------------------------------------------*/
+void read_fits(char * fits_file_name, long * xdim, long * ydim, float ** data)
+{
+    fitsfile *fptr;
+    int status = 0, nfound, anynull;
+    long naxes[2];
+    long npixels;
+    float nullval = 0.0;
+
+    fits_open_file(&fptr, fits_file_name, READONLY, &status);
+    fits_read_keys_lng(fptr, "NAXIS", 1, 2, naxes, &nfound, &status);
+    if (status)
+        printerro(strcat(fits_file_name, " <-- Failed to open the file"));
+
+    /* Read in data */
+    npixels = naxes[0] * naxes[1];
+    (*data) = malloc(sizeof(float) * npixels);
+
+    nullval = 0;
+    if (fits_read_img(fptr, TFLOAT, 1, npixels, &nullval, (*data), &anynull,
+        &status))
+        printerro(strcat(fits_file_name, " has no valid fits image data"));
+
+    *xdim = naxes[0];
+    *ydim = naxes[1];
+
+    fits_close_file(fptr, &status);
+}
+
+/*---------------------------------------------------------------------------*
  * SCALE_PIXELS: Changes the pixel scale to linear/log/sqroot/etc..
  *---------------------------------------------------------------------------*/
 void scale_pixels(int scale, unsigned int npixels, float *data,
@@ -51,6 +82,9 @@ void scale_pixels(int scale, unsigned int npixels, float *data,
     /* the dynamic range is reduced to 255 for jpeg                         */
     scl_data = (datamax - datamin)/(float)JMAXVAL;
 
+    /* we will end up with segfaults if scl_data = 0                        */
+    if (scl_data == 0) scl_data = 1;
+
     for (i = 0; i < npixels; ++i)
         data[i] = (data[i] - datamin)/scl_data;
 
@@ -89,39 +123,32 @@ void scale_pixels(int scale, unsigned int npixels, float *data,
     switch (scale)
     {
         case 1 :                                              /* Square root */
-            printinfo("Using square-root scale");
             scl_data = sqrt((float)JMAXVAL)/(float)JMAXVAL;
             for (i = 0; i < npixels; ++i)
                 (*image_buffer)[i] = (int)(sqrt(data[i])/scl_data);
             break;
 
         case 2 :                                                   /* Square */
-            printinfo("Using quadratic scale");
             scl_data = pow((float)JMAXVAL,2)/(float)JMAXVAL;
             for (i = 0; i < npixels; ++i)
                 (*image_buffer)[i] = (int)abs((pow(data[i],2) - 1.0)/scl_data);
             break;
 
         case 3 :                                                    /* Cubic */
-            printinfo("Using cubic scale");
             scl_data = pow((float)JMAXVAL,3)/(float)JMAXVAL;
             for (i = 0; i < npixels; ++i)
                 (*image_buffer)[i] = (int)abs((pow(data[i],3) - 1.0)/scl_data);
             break;
 
         case 4 :                                                      /* log */
-            printinfo("Using log scale");
             scl_data = log(1.0 + (float)JMAXVAL)/(float)JMAXVAL;
             for (i = 0; i < npixels; ++i)
                 (*image_buffer)[i] = (int)((log(abs(data[i]) + 1.0))/scl_data);
             break;
 
-        case 5 :
-            /* contrast stretch */
-            printinfo("Performing histogram stretch (normalization)");
-
+        case 5 :                                         /* contrast stretch */
             /* We need to go through the cumulative histogram to pick the
-             *  appropriate values for datamin and datamax               */
+             *  appropriate values for datamin and datamax                   */
             i = 0;
             while (i < JMAXVAL)
             {
@@ -155,14 +182,11 @@ void scale_pixels(int scale, unsigned int npixels, float *data,
             }
             break;
 
-        case 6 :
-            /* histogram equalization */
-            printinfo("Performing Histogram Equalization");
+        case 6 :                                   /* histogram equalization */
             for (i = 0; i <  npixels; ++i)
                 (*image_buffer)[i] = cumhist[(*image_buffer)[i]] * JMAXVAL;
             break;
         default :
-            printinfo("Using linear scale");
             break;
     }
 }