00001
00002
00003
00004
00005
00006
00007
00008 #include "stdinc.h"
00009
00010
00011
00012 void make_header(int m, int n, FILE* output_file, char* colormap_file);
00013
00014
00015
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
00031
00032
00033 if (!scale || amax <= amin)
00034 for (i = 0; i < m; i++) as[i] = 255.0*a[i];
00035 else {
00036 afac = 255.0/(amax-amin);
00037 for (i = 0; i < m; i++) as[i] = afac*(a[i]-amin);
00038 }
00039
00040 for (i = 0; i < m; i++)
00041 c[i] = (unsigned char) as[i];
00042
00043 fwrite(&c, 1, m, file);
00044 if (modd) fwrite(&c, 1, 1, file);
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
00062
00063
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
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
00089
00090
00091 get_limits(a, m*n, &amin, &amax);
00092
00093
00094
00095
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,
00101 scale, amin, amax);
00102
00103 if (filename != NULL) fclose(file);
00104 }
00105
00106
00107
00108
00109
00110
00111
00112
00113
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
00127
00128 if ((f = (char*)malloc(1+nl)) == NULL) exit(1);
00129
00130
00131
00132 for (j = 0; j < nl; j++) *(f+j) = *(filename+j);
00133 *(f+nl) = '\0';
00134 }
00135
00136
00137
00138 write_image(a, *m, *n, f, *scale);
00139 }
00140
00141
00142
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 }