File: wcs_align.c

package info (click to toggle)
fitscut 1.4.4-4
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 924 kB
  • ctags: 465
  • sloc: ansic: 6,024; sh: 767; makefile: 132
file content (398 lines) | stat: -rw-r--r-- 12,176 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/* -*- mode:C; indent-tabs-mode:nil; tab-width:8; c-basic-offset:8; -*-
 *
 * Functions for remapping an image based on WCS info
 *
 * Author: William Jon McCann
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Id: wcs_align.c,v 1.7 2004/04/21 21:28:47 mccannwj Exp $
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
#include <errno.h>
#include "getopt.h"
#include <signal.h>
#include <ctype.h>

#include "fitscut.h"
#include <wcstools/wcs.h>
#include "wcs_align.h"

#ifdef  STDC_HEADERS
#include <stdlib.h>
#else   /* Not STDC_HEADERS */
extern void exit ();
extern char *malloc ();
#endif  /* STDC_HEADERS */

#ifdef  HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
 
#ifdef DMALLOC
#include <dmalloc.h>
#define DMALLOC_FUNC_CHECK 1
#endif

void
wcs_initialize_channel (FitsCutImage *Image, int k)
{
        double xpix, ypix;

        xpix = Image->x0[k] + Image->ncols[k] / 2.0;
        ypix = Image->y0[k] + Image->nrows[k] / 2.0;
  
        if (Image->header[k] != NULL) {
                struct WorldCoor *wcs;

                wcs = wcsinit (Image->header[k]);
                if (iswcs (wcs)) {
                        /*
                          struct WorldCoor *wcs_new;
                          double            cra, cdec;
                        */

                        Image->wcs[k] = wcs;
#if 0      
                        if (verbose)
                                wcscent (wcs);
      
                        pix2wcs (wcs, xpix, ypix, &cra, &cdec);

                        fitscut_message (2,"\tNew center [%f,%f] -> %f %f\n",
                                         xpix, ypix, cra, cdec);
      
                        /*wcsshift (wcs,cra,cdec,"FK5");*/
                        wcs_new = wcskinit (Image->ncols[k], Image->nrows[k],
                                            wcs->ctype[0],wcs->ctype[1],
                                            xpix, ypix, cra, cdec, wcs->cd,
                                            wcs->cdelt[0], wcs->cdelt[1],
                                            wcs->rot, wcs->equinox, wcs->radecsys);
                        if (verbose)
                                wcscent (wcs_new);
                        wcsfree (wcs);
                        Image->wcs[k] = wcs_new;
#endif
                }
        }
}

void
wcs_initialize (FitsCutImage *Image)
{
        int k;

        for (k = 0; k < Image->channels; k++)
                wcs_initialize_channel (Image, k);
}

void
wcs_align_first_channel (FitsCutImage *Image, int k)
{
  
        if (Image->wcs[0] == NULL)
                return;

        if (iswcs(Image->wcs[0]) == 0)
                return;

        wcs_remap_channel (Image, Image->wcs[0], k);
}

void
wcs_align_first (FitsCutImage *Image)
{
        int k;
  
        if (Image->wcs[0] == NULL)
                return;

        if (iswcs (Image->wcs[0]) == 0)
                return;

        for (k = 1; k < Image->channels; k++) {
                fitscut_message (2, "\t\tremapping channel %d\n", k);
                wcs_remap_channel (Image, Image->wcs[0], k);
        }
}

int
wcs_remap_channel (FitsCutImage *Image, struct WorldCoor *wcs_out, int channel)
{
        struct WorldCoor *wcs_in;
        int wpin, hpin;
        int offscl;
        int iin, iout, jin, jout;
        int iout1, iout2, jout1, jout2;

        double xout, yout, xin, yin, xpos, ypos, dpixi;
        double xmin, xmax, ymin, ymax;

        int nbytes;
        int bitpix,bitpix_out;
        int ncols_in, nrows_in;
        int ncols_out, nrows_out;

        char *image_out, *image;

        image = (char *) Image->data[channel];
        wcs_in = Image->wcs[channel];
        bitpix = -32;
        bitpix_out = -32;
        ncols_in = Image->ncols[channel];
        nrows_in = Image->nrows[channel];

        /* Allocate space for output image */
        ncols_out = Image->ncols[channel];
        nrows_out = Image->nrows[channel];

        fitscut_message (3, "\t\tCreating temp image [%d,%d]\n", ncols_out, nrows_out);

        nbytes = ncols_out * nrows_out * sizeof (float);
        image_out = (char *) malloc (nbytes);
        memset (image_out, 0, nbytes);

        /* Set input WCS output coordinate system to output coordinate system*/
        wcs_in->sysout = wcs_out->syswcs;
        strcpy (wcs_in->radecout, wcs_out->radecsys);
        wpin = wcs_in->nxpix;
        hpin = wcs_in->nypix;

        /* Set output WCS output coordinate system to input coordinate system*/
        wcs_out->sysout = wcs_in->syswcs;

        /* Find limiting edges of input image in output image */
        pix2wcs (wcs_in, 1.0, 1.0, &xpos, &ypos);
        wcs2pix (wcs_out, xpos, ypos, &xout, &yout, &offscl);
        xmin = xout;
        xmax = xout;
        ymin = yout;
        ymax = yout;
        pix2wcs (wcs_in, 1.0, (double)hpin, &xpos, &ypos);
        wcs2pix (wcs_out, xpos, ypos, &xout, &yout, &offscl);
        if (xout < xmin) xmin = xout;
        if (xout > xmax) xmax = xout;
        if (yout < ymin) ymin = yout;
        if (yout > ymax) ymax = yout;
        pix2wcs (wcs_in, (double)wpin, 1.0, &xpos, &ypos);
        wcs2pix (wcs_out, xpos, ypos, &xout, &yout, &offscl);
        if (xout < xmin) xmin = xout;
        if (xout > xmax) xmax = xout;
        if (yout < ymin) ymin = yout;
        if (yout > ymax) ymax = yout;
        pix2wcs (wcs_in, (double)wpin, (double)hpin, &xpos, &ypos);
        wcs2pix (wcs_out, xpos, ypos, &xout, &yout, &offscl);
        if (xout < xmin) xmin = xout;
        if (xout > xmax) xmax = xout;
        if (yout < ymin) ymin = yout;
        if (yout > ymax) ymax = yout;
        iout1 = (int) (ymin + 0.5);
        if (iout1 < 1) iout1 = 1;
        iout2 = (int) (ymax + 0.5);
        if (iout2 > nrows_out) iout2 = nrows_out;
        jout1 = (int) (xmin + 0.5);
        if (jout1 < 1) jout1 = 1;
        jout2 = (int) (xmax + 0.5);
        if (jout2 > ncols_out) jout2 = ncols_out;

        fitscut_message (3, "REMAP: Output x: %d-%d, y: %d-%d\n",
                         jout1, jout2, iout1, iout2);

        /* Loop through vertical pixels (output image lines) */
        for (iout = iout1; iout <= iout2; iout++) {
                yout = (double) iout;

                /* Loop through horizontal pixels (output image columns) */
                for (jout = jout1; jout <= jout2; jout++) {
                        xout = (double) jout;

                        /* Get WCS coordinates of this pixel in output image */
                        pix2wcs (wcs_out, xout, yout, &xpos, &ypos);

                        /* Get image coordinates of this subpixel in input image */
                        wcs2pix (wcs_in, xpos, ypos, &xin, &yin, &offscl);
                        if (!offscl) {
                                iin = (int) (yin + 0.5);
                                jin = (int) (xin + 0.5);

                                /* Read pixel from input */
                                dpixi = getpix (image, bitpix, ncols_in,
                                                nrows_in, 0.0, 1.0, jin, iin);

                                /* Write pixel to output */
                                addpix (image_out, bitpix_out, ncols_out,
                                        nrows_out, 0.0, 1.0, jout, iout, dpixi);
                        }
                }
        }

        free (Image->data[channel]);
        Image->data[channel] = (float *) image_out;

        return (0);
}

/* GETPIX -- Get pixel from 2D image of any numeric type */

double
getpix (char   *image,		/* Image array as 1-D vector */
        int	bitpix,		/* FITS bits per pixel */
        /*  16 = short, -16 = unsigned short, 32 = int */
        /* -32 = float, -64 = double */
        int	w,		/* Image width in pixels */
        int	h,		/* Image height in pixels */
        double  bzero,		/* Zero point for pixel scaling */
        double  bscale,		/* Scale factor for pixel scaling */
        int	x,		/* Zero-based horizontal pixel number */
        int	y)		/* Zero-based vertical pixel number */
        
{
        short *im2;
        int *im4;
        unsigned short *imu;
        float *imr;
        double *imd;
        double dpix;

        /* Return 0 if coordinates are not inside image */
        if (x < 0 || x >= w)
                return (0.0);
        if (y < 0 || y >= h)
                return (0.0);

        /* Extract pixel from appropriate type of array */
        switch (bitpix) {

	case 8:
                dpix = (double) image[(y*w) + x];
                break;

	case 16:
                im2 = (short *)image;
                dpix = (double) im2[(y*w) + x];
                break;

	case 32:
                im4 = (int *)image;
                dpix = (double) im4[(y*w) + x];
                break;

	case -16:
                imu = (unsigned short *)image;
                dpix = (double) imu[(y*w) + x];
                break;

	case -32:
                imr = (float *)image;
                dpix = (double) imr[(y*w) + x];
                break;

	case -64:
                imd = (double *)image;
                dpix = imd[(y*w) + x];
                break;

	default:
                dpix = 0.0;
	}
        return (bzero + (bscale * dpix));
}

/* ADDPIX -- Add pixel value into 2D image of any numeric type */

void
addpix (char   *image,
        int	bitpix,		/* Number of bits per pixel */
        /*  16 = short, -16 = unsigned short, 32 = int */
        /* -32 = float, -64 = double */
        int	w,		/* Image width in pixels */
        int	h,		/* Image height in pixels */
        double  bzero,		/* Zero point for pixel scaling */
        double  bscale,		/* Scale factor for pixel scaling */
        int	x,		/* Zero-based horizontal pixel number */
        int	y,		/* Zero-based vertical pixel number */
        double	dpix)		/* Value to add to pixel */
        
{
        short *im2;
        int *im4;
        unsigned short *imu;
        float *imr;
        double *imd;
        int ipix;

        /* Return if coordinates are not inside image */
        if (x < 0 || x >= w)
                return;
        if (y < 0 || y >= h)
                return;

        dpix = (dpix - bzero) / bscale;
        ipix = (y * w) + x;

        switch (bitpix) {

	case 8:
                if (dpix < 0)
                        image[ipix] = image[ipix] + (char) (dpix - 0.5);
                else
                        image[ipix] = image[ipix] + (char) (dpix + 0.5);
                break;

	case 16:
                im2 = (short *)image;
                if (dpix < 0)
                        im2[ipix] = im2[ipix] + (short) (dpix - 0.5);
                else
                        im2[ipix] = im2[ipix] + (short) (dpix + 0.5);
                break;

	case 32:
                im4 = (int *)image;
                if (dpix < 0)
                        im4[ipix] = im4[ipix] + (int) (dpix - 0.5);
                else
                        im4[ipix] = im4[ipix] + (int) (dpix + 0.5);
                break;

	case -16:
                imu = (unsigned short *)image;
                if (dpix > 0)
                        imu[ipix] = imu[ipix] + (unsigned short) (dpix + 0.5);
                break;

	case -32:
                imr = (float *)image;
                imr[ipix] = imr[ipix] + (float) dpix;
                break;

	case -64:
                imd = (double *)image;
                imd[ipix] = imd[ipix] + dpix;
                break;

	}
        return;
}