index 60420e2..5469733 100644 (file)
@@ -1,4 +1,5 @@
 /***************************************************************************
+ * This file is a part of CADS/UVS fits2jpeg conversion software           *
  *   Copyright (C) 2012 by CADS/UV Software Team,                          *
  *                         Indian Institute of Astrophysics                *
  *                         Bangalore 560034                                *
   Reks 28 June 2012
   1. rebranded to CADS/UVS
 
-  Reks 05-10 July 2012 v2.0 plans
+  Reks 05-10 July 2012 v2.0 
   1. Merged scale & operations to single flag
   2. added fits2jpeg.h, help/banner functions moved to messages.c
   3. Options to clip image min/max
   4. negate image option
   5. jpeg quality factor
   6. pixel scaling operations moved to image.c
-  7. Image resizing works for reducing size. segfaults for zoom > 1 :(
-  TODO
-      1. move fits read to image.c
-      2. specify output directory
-      3. Image resizing options
+  7. Image resizing
+  8. moved fits read to image.c
+  9. Option to specify output directory
+
+  Reks Oct/Nov 2012 v2.1
+  1. Fixed a segfault on zero/blank fits images
  *---------------------------------------------------------------------------*/
 
 #include "fits2jpeg.h"
@@ -92,54 +94,80 @@ int optindex;
 /*--------------------------- Begin Main Progam -----------------------------*/
 int main(int argc, char *argv[], char *envp[])
 {
-    fitsfile *fptr;
-
-    int scale = 0, process = 0, status = 0, jpgqual = 100, nfound, anynull;
-    unsigned int i = 0, j = 0, usrclip = 0, usrnegt = 0, usrzoom = 0;
-    long xdim = 0, ydim = 0, naxes[2], row_stride;
+    int scale = 0, jpgqual = 100, status = 0;
+    unsigned int i = 0, j = 0;
+    unsigned int customdir = 0, usrclip = 0, usrnegt = 0, usrzoom = 0;
+    long xdim = 0, ydim = 0, row_stride;
     unsigned long npixels = 0;
-    float datamin = 0.0, datamax = 0.0, nullval = 0.0, zoomfact = 1.0;
+    float datamin = 0.0, datamax = 0.0, zoomfact = 1.0;
     float *data;
     char jpeg_file_name[MAX_TEXT] = "", fits_file_name[MAX_TEXT] = "";
+    char output_dir[MAX_TEXT] = "";
     char *tmpstr, *sptr;
 
     int opt;                                                   /* For getopt */
     extern char *optarg;
     extern int optindex;
-
+    mode_t mode = 0755;
     FILE *jpeg_file;
-
     struct jpeg_error_mgr jerr;
     struct jpeg_compress_struct cinfo;
     JSAMPROW row_pointer[1];
     JSAMPLE *image_buffer;
     /*---------------- End of variable initialization -----------------------*/
 
-    PRINT_BANNER();        /* Header: Talks about program, version & purpose */
+    banner();              /* Header: Talks about program, version & purpose */
     set_signals();                                  /* Trap un-natural exits */
 
     /*-------------------- Parse command line inputs ------------------------*/
 
     if (argc < 2) usage();  /* People who don't give any arguments need help */
-    while ( ( opt = my_getopt ( argc, argv, "e:hnq:r:s:z:" ) ) != EOF )
+    while ( ( opt = my_getopt ( argc, argv, "d:e:hnq:r:s:z:" ) ) != EOF )
         switch ( opt )
         {
         case 's':
-            if      (strcmp(optarg, "linear")   ==0) scale = 0;
-            else if (strcmp(optarg, "sqroot")   ==0) scale = 1;
-            else if (strcmp(optarg, "square")   ==0) scale = 2;
-            else if (strcmp(optarg, "cubic")    ==0) scale = 3;
-            else if (strcmp(optarg, "log")      ==0) scale = 4;
-            else if (strcmp(optarg, "normalize")==0) scale = 5;
-            else if (strcmp(optarg, "equalize") ==0) scale = 6;
+            if      (strcmp(optarg, "linear") == 0) scale = 0;
+            else if (strcmp(optarg, "sqroot") == 0)
+            {
+                printinfo("Using square-root scale");
+                scale = 1;
+            }
+            else if (strcmp(optarg, "square") == 0)
+            {
+                printinfo("Using quadratic scale");
+                scale = 2;
+            }
+            else if (strcmp(optarg, "cubic") == 0)
+            {
+                printinfo("Using cubic scale");
+                scale = 3;
+            }
+            else if (strcmp(optarg, "log") == 0)
+            {
+                printinfo("Using log scale");
+                scale = 4;
+            }
+            else if (strcmp(optarg, "normalize") == 0)
+            {
+                printinfo("Performing histogram stretch (normalization)");
+                scale = 5;
+            }
+            else if (strcmp(optarg, "equalize") == 0)
+            {
+                printinfo("Performing Histogram Equalization");
+                scale = 6;
+            }
             else
+            {
+                printwarn(strcat(optarg, " -- Unrecognized option"));
                 scale = 0;
+            }
             break;
 
         case 'e':
             /* Need to preserve it for backward compatibility */
-            if      (strcmp(optarg, "normalize")==0) scale = 5;
-            else if (strcmp(optarg, "equalize") ==0) scale = 6;
+            if      (strcmp(optarg, "normalize") == 0) scale = 5;
+            else if (strcmp(optarg, "equalize") == 0) scale = 6;
             else
                 printerro(strcat(optarg, " -- Unrecognized option"));
             break;
@@ -165,7 +193,6 @@ int main(int argc, char *argv[], char *envp[])
                 sptr = strtok(tmpstr, ":");
                 datamin = atof(sptr);
             }
-            printinfo("User defined pixel range");
 
             /* Remember.. now we have user specified range */
             usrclip = 1;
@@ -183,14 +210,21 @@ int main(int argc, char *argv[], char *envp[])
             break;
 
         case 'z' :
-
-            /* Do something to scale image
-             */
             zoomfact = atof(optarg);
-            if (zoomfact < 0.0) zoomfact = 1.0;
+            zoomfact = fabs(zoomfact);
+            if (zoomfact < 0.01) zoomfact = 0.01;
+            if (zoomfact > 4.0) zoomfact = 4.0;
             usrzoom = 1;
             break;
 
+        case 'd':
+            strcpy(output_dir, optarg);
+            printf("INFO   : Output image directory is %s\n", output_dir);
+            customdir = 1;
+            status = make_tree(output_dir, mode);
+            if (status != 0) printerro("Unable to create output directory");
+            break;
+
         case 'h':
             usage();
             break;
@@ -199,7 +233,7 @@ int main(int argc, char *argv[], char *envp[])
             printerro("You can try 'fits2jpeg -h' for valid options");
             break;
         }
-
+    if (scale == 0) printinfo("Using linear scale");
     if ((argc - optindex) == 0) usage();        /* Someone gave only options */
 
     /*---------------------- Begin processing files -------------------------*/
@@ -218,22 +252,18 @@ int main(int argc, char *argv[], char *envp[])
         strtok(jpeg_file_name, ".");
         strcat(jpeg_file_name, ".jpg");
 
-        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"));
+        if (customdir == 1)
+        {
+            if (output_dir[strlen(output_dir) - 1] != '/')
+                strcat(output_dir, "/");
 
-        /* Read in data */
-        npixels = naxes[0] * naxes[1];
-        data = (float *) malloc(sizeof(float) * npixels);
+            strcat(output_dir, basename(jpeg_file_name));
+            strcpy(jpeg_file_name, output_dir);
+        }
 
-        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"));
+        read_fits(fits_file_name, &xdim, &ydim, &data);
+        npixels = xdim * ydim;
 
-        fits_close_file(fptr, &status);
-        /*Close data file*/
         printf("INFO   : data input from %s\n", fits_file_name);
 
 
@@ -243,6 +273,7 @@ int main(int argc, char *argv[], char *envp[])
         /* IF user has provided a range, fix them as min & max in image */
         if (usrclip == 1)
         {
+            printinfo("user defined pixel range");
             for (i = 0; i < npixels; ++i)
             {
                 if (data[i] > datamax) data[i] = datamax;
@@ -250,9 +281,7 @@ int main(int argc, char *argv[], char *envp[])
             } /*endfor*/
         }
 
-        /* Allocate image buffer */
-        image_buffer = (unsigned char *) malloc(sizeof(char) * npixels);
-        scale_pixels(scale, npixels, data, image_buffer);
+        scale_pixels(scale, npixels, data, &image_buffer);
 
         /* Before we write jpeg, check if there is any requirement to negate
          * the image (usrnegt = 1)                                           */
@@ -262,12 +291,15 @@ int main(int argc, char *argv[], char *envp[])
             for (i = 0; i <  npixels; ++i) image_buffer[i] ^= 0xff;
         }
 
-        xdim = naxes[0];
-        ydim = naxes[1];
-        if (usrzoom == 1) resize_image(&xdim, &ydim, zoomfact, image_buffer);
+        if (usrzoom == 1)
+        {
+            printf("INFO   : Image zoom factor = %3.2f\n", zoomfact);
+            resize_image(&xdim, &ydim, zoomfact, &image_buffer);
+        }
 
         /*Write out data into JPEG file*/
         jpeg_file = fopen(jpeg_file_name, "wb");/* Open JPEG file for writing*/
+        if (!jpeg_file) printerro("Unable to create output file");
         cinfo.err = jpeg_std_error(&jerr);             /* JPEG error handler */
         jpeg_create_compress(&cinfo);                 /* JPEG initialization */
         jpeg_stdio_dest(&cinfo, jpeg_file); /* Send compressed data to stdio */