index cc97e08..7390330 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                                *
  *   Copyright (C) 2012 by CADS/UV Software Team,                          *
  *                         Indian Institute of Astrophysics                *
  *                         Bangalore 560034                                *
@@ -78,9 +79,8 @@
   5. jpeg quality factor
   6. pixel scaling operations moved to image.c
   7. Image resizing
   5. jpeg quality factor
   6. pixel scaling operations moved to image.c
   7. Image resizing
-  TODO
-      1. move fits read to image.c
-      2. specify output directory
+  8. moved fits read to image.c
+  9. Option to specify output directory
  *---------------------------------------------------------------------------*/
 
 #include "fits2jpeg.h"
  *---------------------------------------------------------------------------*/
 
 #include "fits2jpeg.h"
@@ -91,55 +91,80 @@ int optindex;
 /*--------------------------- Begin Main Progam -----------------------------*/
 int main(int argc, char *argv[], char *envp[])
 {
 /*--------------------------- Begin Main Progam -----------------------------*/
 int main(int argc, char *argv[], char *envp[])
 {
-    fitsfile *fptr;
-
-    int scale = 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;
     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] = "";
     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;
     char *tmpstr, *sptr;
 
     int opt;                                                   /* For getopt */
     extern char *optarg;
     extern int optindex;
-
+    mode_t mode = 0755;
     FILE *jpeg_file;
     FILE *jpeg_file;
-
     struct jpeg_error_mgr jerr;
     struct jpeg_compress_struct cinfo;
     JSAMPROW row_pointer[1];
     JSAMPLE *image_buffer;
     /*---------------- End of variable initialization -----------------------*/
 
     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 */
     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':
         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
             else
+            {
                 printwarn(strcat(optarg, " -- Unrecognized option"));
                 scale = 0;
                 printwarn(strcat(optarg, " -- Unrecognized option"));
                 scale = 0;
+            }
             break;
 
         case 'e':
             /* Need to preserve it for backward compatibility */
             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;
             else
                 printerro(strcat(optarg, " -- Unrecognized option"));
             break;
@@ -182,16 +207,21 @@ int main(int argc, char *argv[], char *envp[])
             break;
 
         case 'z' :
             break;
 
         case 'z' :
-
-            /* Do something to scale image
-             */
             zoomfact = atof(optarg);
             zoomfact = atof(optarg);
-            zoomfact = abs(zoomfact);
+            zoomfact = fabs(zoomfact);
             if (zoomfact < 0.01) zoomfact = 0.01;
             if (zoomfact > 4.0) zoomfact = 4.0;
             usrzoom = 1;
             break;
 
             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;
         case 'h':
             usage();
             break;
@@ -200,7 +230,7 @@ int main(int argc, char *argv[], char *envp[])
             printerro("You can try 'fits2jpeg -h' for valid options");
             break;
         }
             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 -------------------------*/
     if ((argc - optindex) == 0) usage();        /* Someone gave only options */
 
     /*---------------------- Begin processing files -------------------------*/
@@ -219,22 +249,18 @@ int main(int argc, char *argv[], char *envp[])
         strtok(jpeg_file_name, ".");
         strcat(jpeg_file_name, ".jpg");
 
         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 = 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);
 
 
         printf("INFO   : data input from %s\n", fits_file_name);
 
 
@@ -262,8 +288,6 @@ int main(int argc, char *argv[], char *envp[])
             for (i = 0; i <  npixels; ++i) image_buffer[i] ^= 0xff;
         }
 
             for (i = 0; i <  npixels; ++i) image_buffer[i] ^= 0xff;
         }
 
-        xdim = naxes[0];
-        ydim = naxes[1];
         if (usrzoom == 1)
         {
             printf("INFO   : Image zoom factor = %3.2f\n", zoomfact);
         if (usrzoom == 1)
         {
             printf("INFO   : Image zoom factor = %3.2f\n", zoomfact);
@@ -272,6 +296,7 @@ int main(int argc, char *argv[], char *envp[])
 
         /*Write out data into JPEG file*/
         jpeg_file = fopen(jpeg_file_name, "wb");/* Open JPEG file for writing*/
 
         /*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 */
         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 */