Main Page   Class Hierarchy   Data Structures   File List   Data Fields   Globals  

write_image.C

Go to the documentation of this file.
00001 
00002 //  WRITE_IMAGE:  Convert an array of real values a[m, n] into a color image.
00003 //                Assume that the numbers are already in the range [0,1].
00004 //
00005 //                This version is called from Fortran or C.
00006 //                For a UNIX pipe version, use make_image.
00007 
00008 #include "stdinc.h"
00009 
00010 // From make_header.C:
00011 
00012 void make_header(int m, int n, FILE* output_file, char* colormap_file);
00013 
00014 // The description and construction of the header for SUN raster
00015 // images is now in make_header.c
00016 
00017 #define BUFSIZE 4096
00018 
00019 static void write_line(float* a, int m, FILE* file,
00020                        int scale, float amin, float amax)
00021 {
00022     int i, modd = 0;
00023     unsigned char c[BUFSIZE];
00024     float afac, as[BUFSIZ];
00025 
00026     int cmin, cmax;
00027 
00028     if (m%2 != 0) modd = 1;
00029 
00030     // Options: no scaling, expect input data in range [0, 1)
00031     //          with scaling, will scale min to 0, max to 255
00032 
00033     if (!scale || amax <= amin)
00034         for (i = 0; i < m; i++) as[i] = 255.0*a[i];       // will vectorize
00035     else {
00036         afac = 255.0/(amax-amin);
00037         for (i = 0; i < m; i++) as[i] = afac*(a[i]-amin); // will vectorize
00038     }
00039 
00040     for (i = 0; i < m; i++)
00041         c[i] = (unsigned char) as[i]; // Won't vectorize
00042 
00043     fwrite(&c, 1, m, file);                     // may vectorize?
00044     if (modd) fwrite(&c, 1, 1, file);           // pad to 16-bit boundary.
00045 }
00046 
00047 static void get_limits(float* a, int n, float* amin, float* amax)
00048 {
00049     int i;
00050 
00051     *amin = 1.0e30;
00052     *amax = -1.e30;
00053     for (i = 0; i < n; i++) {
00054         if (a[i] < *amin) *amin = a[i];
00055         if (a[i] > *amax) *amax = a[i];
00056     }
00057 }
00058 
00059 void write_image(float* a, int m, int n, char* filename, int scale)
00060 
00061 // Write the contents of the 2-D array a to a file as a SUN raster image.
00062 // If no file is specified, use stdout.
00063 // Scale the data before writing if scale is set.
00064 
00065 {
00066     FILE* file;
00067     int file_open = 0;
00068     int j, mm, nn;
00069     float amin, amax;
00070 
00071     if (m > BUFSIZE) exit(1);
00072 
00073     if (filename != NULL) {
00074         if ((file = fopen(filename, "w")) == NULL) exit(1);
00075     } else
00076         file = stdout;
00077 
00078     // Make dimensions even (pad if necessary):
00079 
00080     mm = m;
00081     if (mm%2 != 0) mm++;
00082 
00083     nn = n;
00084     if (nn%2 != 0) nn++;
00085 
00086     make_header(mm, nn, file, NULL);
00087 
00088     // Scan 2-D array top to bottom, left to right.
00089 
00090 //    if (scale) {
00091         get_limits(a, m*n, &amin, &amax);
00092 //      fprintf(stderr, "%s: min = %f max = %f\n", filename, amin, amax);
00093 //    }
00094 
00095     // Note: j ordering goes from top to bottom...
00096 
00097     for (j = n - 1; j >= 0; j--) write_line(a+m*j, m, file,
00098                                             scale, amin, amax);
00099 
00100     if (nn != n) write_line(a, m, file,         // Pad to 16-bit boundary.
00101                             scale, amin, amax);
00102         
00103     if (filename != NULL) fclose(file);
00104 }
00105 
00106 
00107 //**********************************************************************
00108 //
00109 //              Fortran interfaces to the C++ routines:
00110 //
00111 //**********************************************************************
00112 
00113 // Need to take care of name mangling...  (Not done yet.)
00114 
00115 void write_image_f77(float* a, int* m, int* n, char* filename,
00116                      int* scale, long int nl)
00117 {
00118     int j, file_open = 0;
00119     char* f = NULL;
00120 
00121     for (j = 0; j < nl; j++)
00122         if (*(filename+j) > ' ') file_open = 1;
00123 
00124     if (file_open) {
00125 
00126         // Make a C string for the filename.
00127 
00128         if ((f = (char*)malloc(1+nl)) == NULL) exit(1);
00129 
00130         // Make a new string, since filename isn't null terminated...
00131 
00132         for (j = 0; j < nl; j++) *(f+j) = *(filename+j);
00133         *(f+nl) = '\0';
00134     }
00135 
00136     // Use the C routine to do the work.
00137 
00138     write_image(a, *m, *n, f, *scale);
00139 }
00140 
00141 // Some brain-dead Fortrans (e.g. on the Sun and the Cray!) want global
00142 // names to be uppercase or terminated with "_"...
00143 
00144 void WRITE_IMAGE_F77(float* a, int* m, int* n, char* filename,
00145                  int* scale, long int nl)
00146 {
00147     write_image_f77(a, m, n, filename, scale, nl);
00148 }
00149 
00150 void write_image_f77_(float* a, int* m, int* n, char* filename,
00151                  int* scale, long int nl)
00152 {
00153     write_image_f77(a, m, n, filename, scale, nl);
00154 }

Generated at Sun Feb 24 09:57:21 2002 for STARLAB by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001