1 #include <loader_common.h>
4 //TODO: add a color table.
5 //invert gray scale, Check if feh or qiv can do this.
6 // add option to check for datamin and datamax on the header
7 // make it more robust. datamax > datamin
8 // write the fits writer!
10 #define SWAP32(x) (x) = \
11 ((((x) & 0x000000ff ) << 24) |\
12 (((x) & 0x0000ff00 ) << 8) |\
13 (((x) & 0x00ff0000 ) >> 8) |\
14 (((x) & 0xff000000 ) >> 24))
17 load(ImlibImage * im, ImlibProgressFunction progress,
18 char progress_granularity, char immediate_load)
20 fprintf(stderr, "fits load function\n");
22 int w = 0, h = 0, alpha = 0;
29 //f = fopen(im->real_file, "rb");
30 if ( fits_open_file( &f, im->real_file, READONLY, &status) ) {
41 //char buf[256], buf2[256];
43 /* read the NAXIS1 and NAXIS2 keyword to get image size */
44 if ( fits_read_keys_lng( f, "NAXIS", 1, 2, naxes, &nfound, &status ) ){
45 fits_close_file(f, &status);
53 if (!fgets(buf, 255, f))
59 sscanf(buf, "%s %i %i %i", buf2, &w, &h, &alpha);
60 if (strcmp(buf2, "ARGB"))
66 if (!IMAGE_DIMENSIONS_OK(w, h))
68 fits_close_file(f, &status);
77 SET_FLAG(im->flags, F_HAS_ALPHA);
79 UNSET_FLAG(im->flags, F_HAS_ALPHA);
80 im->format = strdup("fits");
83 if (((!im->data) && (im->loader)) || (immediate_load) || (progress))
89 double *buffer, datamin, datamax, nullval = 0;
92 long *bin, nmax = 0, ndataok, sumpix = 0;
93 int nbin, k, kmin, mostfreq = 0;
95 char *envdmin, *envdmax;
97 /* must set the im->data member before callign progress function */
98 ptr = im->data = malloc(w * h * sizeof(DATA32));
100 buffer = malloc( nbuffer * sizeof( double ) );
104 fits_close_file(f, &status);
109 envdmin = getenv( "DATAMIN" );
110 envdmax = getenv( "DATAMAX" );
111 if( envdmin != NULL && envdmax != NULL ){
112 datamin = atof( envdmin );
113 datamax = atof( envdmax );
114 fprintf(stderr, "ENV:data min %g, max %g\n", datamin, datamax);}
118 datamax = -HUGE_VALF;
121 for (y = 0; y < h; y++)
123 fits_read_img( f, TDOUBLE, fpixel, nbuffer, &nullval,
124 buffer, &anynull, &status);
125 for( x = 0; x < w; x++ ){
126 if( buffer[ x ] > datamax )
127 datamax = buffer[ x ];
128 if( buffer[ x ] < datamin )
129 datamin = buffer[ x ];
133 fprintf(stderr, "data min %g, max %g\n", datamin, datamax);
135 nbin = 4 * sqrt( datamax - datamin );
136 if( nbin > 5000 ) nbin = 1000;
137 binw = ( datamax - datamin ) / nbin;
138 fprintf(stderr, "nbin and bin width:%d %g\n", nbin, binw);
139 bin = calloc( nbin, sizeof( long ) );
143 for (y = 0; y < h; y++)
145 fits_read_img( f, TDOUBLE, fpixel, nbuffer, &nullval,
146 buffer, &anynull, &status);
147 for( x = 0; x < w; x++ ){
148 for( k = 0; k < nbin; k++ ){
149 if( buffer[ x ] >= k * binw &&
150 buffer[ x ] < ( k + 1 ) * binw ){
158 //Find most frequent pixel value
159 for( k = 0; k < nbin; k++ ){
160 if( bin[ k ] > nmax ){
164 fprintf(stderr, "%ld ", bin[ k ] );
166 fprintf(stderr, "mostfreq and nmax = %d %ld ", mostfreq, nmax );
168 //Set new DATAMIN. look for FWHM on a log scale
169 nmax = log( nmax ) / 2;
170 datamin = ( mostfreq + 0.5 ) * binw;
171 for( k = mostfreq - 1; k >= 0; k-- ){
172 if( log( bin[ k ] ) < nmax ){
174 datamin = ( (kmin) + 0.5 ) * binw;
178 //Set new DATAMAX when we have 99.5% of the pixels accounted for
180 for( k = 0; k < kmin; k++)
182 //ndataok = h * w; //To get values closer to ds9's uncomment this
183 for( k = kmin; k < nbin; k++){
185 if( 1.0 * sumpix / ndataok > 0.997 )
188 datamax = ( ( k + 1 ) + 0.5 ) * binw;
191 // use the new datamin datamax values to rescale and create imlib im
194 for (y = 0; y < h; y++)
196 fits_read_img( f, TDOUBLE, fpixel, nbuffer, &nullval,
197 buffer, &anynull, &status);
199 for( x = w - 1; x >= 0; x-- ){
200 if( buffer[ x ] <= datamin )
203 if( buffer[ x ] >= datamax )
206 gray = ceil(( buffer[ x ] - datamin ) / ( datamax - datamin ) * 255 );
207 *ptr = (((( (0xff << 8) | gray) << 8) | gray) << 8) | gray ;
217 per = (char)((100 * y) / im->h);
218 if (((per - pper) >= progress_granularity) ||
222 if (!progress(im, per, 0, (y - l), im->w, l))
224 fits_close_file(f, &status);
233 fprintf(stderr, "data min %g, max %g\n", datamin, datamax);
236 fits_close_file(f, &status);
241 save(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity)
245 int y, pl = 0, alpha = 0;
249 /* no image data? abort */
252 f = fopen(im->real_file, "wb");
255 if (im->flags & F_HAS_ALPHA)
257 fprintf(f, "ARGB %i %i %i\n", im->w, im->h, alpha);
259 for (y = 0; y < im->h; y++)
261 fwrite(ptr, im->w, 4, f);
268 per = (char)((100 * y) / im->h);
269 if (((per - pper) >= progress_granularity) || (y == (im->h - 1)))
272 if (!progress(im, per, 0, (y - l), im->w, l))
288 formats(ImlibLoader * l)
290 char *list_formats[] = { "fits", "fts", "fit" };
295 l->num_formats = (sizeof(list_formats) / sizeof(char *));
296 l->formats = malloc(sizeof(char *) * l->num_formats);
297 for (i = 0; i < l->num_formats; i++)
298 l->formats[i] = strdup(list_formats[i]);