forked from cbassa/sattools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deproject.c
325 lines (271 loc) · 7.32 KB
/
deproject.c
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "qfits.h"
#include <cpgplot.h>
#include <wcslib/cel.h>
#include <jpeglib.h>
#include <getopt.h>
struct image {
int naxis1,naxis2,naxis3;
float *z;
float zmin,zmax;
double ra0,de0;
float avg,std;
float x0,y0;
float a[3],b[3],xrms,yrms;
float exptime;
double mjd;
char nfd[32];
int cospar;
};
struct jpeg_image {
int nx,ny,nz;
float *z;
};
struct jpeg_image read_jpg(char *filename);
void write_jpg(char *filename,struct jpeg_image img);
struct image read_fits(char *filename,int pnum);
void forward(double ra0,double de0,double ra,double de,double *x,double *y);
void reverse(double ra0,double de0,double x,double y,double *ra,double *de);
int main(int argc,char *argv[])
{
int i,j,k,l,m;
struct image img;
struct jpeg_image jpg,out;
double rx,ry,ra,de,rx0,ry0;
double x,y,d;
double drx=-10.0,dry=10.0;
double ra0=237.0,de0=12.5;
int arg=0;
char *fitsfile,*jpgfile,*outfile;
// Decode options
while ((arg=getopt(argc,argv,"j:f:o:R:D:s:"))!=-1) {
switch(arg) {
case 'j':
jpgfile=optarg;
break;
case 'f':
fitsfile=optarg;
break;
case 'o':
outfile=optarg;
break;
case 'R':
ra0=atof(optarg);
break;
case 'D':
de0=atof(optarg);
break;
case 's':
dry=atof(optarg);
drx=-dry;
break;
default:
return 0;
}
}
// Read image
img=read_fits(fitsfile,0);
jpg=read_jpg(jpgfile);
out.nx=3000;
out.ny=6000;
out.nz=3;
/*
img.x0*=4.0;
img.y0*=4.0;
img.a[1]/=4.0;
img.a[2]/=4.0;
img.b[1]/=4.0;
img.b[2]/=4.0;
*/
out.z=(float *) malloc(sizeof(float)*out.nx*out.ny*out.nz);
for (i=0;i<out.nx;i++) {
for (j=0;j<out.ny;j++) {
// Set rx,ry
rx=drx*(float) (i-0.5*out.nx);
ry=dry*(float) (j-0.5*out.ny);
// Obtain ra/dec for output image
reverse(ra0,de0,rx,ry,&ra,&de);
// Obtain rx/ry for input image
forward(img.ra0,img.de0,ra,de,&rx0,&ry0);
// Compute pixel position
d=img.a[1]*img.b[2]-img.a[2]*img.b[1];
x=(+rx0*img.b[2]-ry0*img.a[2])/d+img.x0;
y=(-rx0*img.b[1]+ry0*img.a[1])/d+img.y0;
// Fill image
for (k=0;k<jpg.nz;k++) {
l=out.nz*(i+out.nx*(out.ny-j-1))+k;
m=jpg.nz*((int) x+jpg.nx*(int) (jpg.ny-y-1))+k;
if (x>0.0 && x<jpg.nx && y>0.0 && y<jpg.ny)
out.z[l]=jpg.z[m];
else
out.z[l]=0.0;
}
}
}
// Dump
write_jpg(outfile,out);
// Free
free(img.z);
free(jpg.z);
free(out.z);
return 0;
}
// Read fits image
struct image read_fits(char *filename,int pnum)
{
int i,j,k,l,m;
qfitsloader ql;
char key[FITS_LINESZ+1] ;
struct image img;
float s1,s2,avg,std;
// Set plane
ql.xtnum = 0;
ql.pnum = pnum;
// Set loadtype
ql.ptype = PTYPE_FLOAT;
// Set filename
ql.filename=filename;
// Image size
img.naxis1=atoi(qfits_query_hdr(filename,"NAXIS1"));
img.naxis2=atoi(qfits_query_hdr(filename,"NAXIS2"));
// MJD
img.mjd=atof(qfits_query_hdr(filename,"MJD-OBS"));
strcpy(img.nfd,qfits_query_hdr(filename,"DATE-OBS"));
img.exptime=atof(qfits_query_hdr(filename,"EXPTIME"));
// COSPAR ID
img.cospar=atoi(qfits_query_hdr(filename,"COSPAR"));
// Transformation
img.mjd=atof(qfits_query_hdr(filename,"MJD-OBS"));
img.ra0=atof(qfits_query_hdr(filename,"CRVAL1"));
img.de0=atof(qfits_query_hdr(filename,"CRVAL2"));
img.x0=atof(qfits_query_hdr(filename,"CRPIX1"));
img.y0=atof(qfits_query_hdr(filename,"CRPIX2"));
img.a[0]=0.0;
img.a[1]=3600.0*atof(qfits_query_hdr(filename,"CD1_1"));
img.a[2]=3600.0*atof(qfits_query_hdr(filename,"CD1_2"));
img.b[0]=0.0;
img.b[1]=3600.0*atof(qfits_query_hdr(filename,"CD2_1"));
img.b[2]=3600.0*atof(qfits_query_hdr(filename,"CD2_2"));
img.xrms=3600.0*atof(qfits_query_hdr(filename,"CRRES1"));
img.yrms=3600.0*atof(qfits_query_hdr(filename,"CRRES2"));
// Initialize load
if (qfitsloader_init(&ql) != 0)
printf("Error initializing data loading\n");
// Test load
if (qfits_loadpix(&ql) != 0)
printf("Error loading actual data\n");
// Allocate image memory
img.z=(float *) malloc(sizeof(float) * img.naxis1*img.naxis2);
// Fill z array
for (i=0,l=0,m=0;i<img.naxis1;i++) {
for (j=0;j<img.naxis2;j++) {
img.z[l]=ql.fbuf[l];
l++;
}
}
// Get levels
for (i=0,s1=0.0,s2=0.0;i<img.naxis1*img.naxis2;i++) {
s1+=img.z[i];
s2+=img.z[i]*img.z[i];
}
img.avg=s1/(float) (img.naxis1*img.naxis2);
img.std=sqrt(s2/(float) (img.naxis1*img.naxis2)-img.avg*img.avg);
img.zmin=img.avg-4.0*img.std;
img.zmax=img.avg+12.0*img.std;
return img;
}
struct jpeg_image read_jpg(char *filename)
{
int i=0,j,k,l,m;
unsigned long location=0;
struct jpeg_image img;
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
JSAMPROW row_pointer[1];
unsigned char *raw_image=NULL;
FILE *file;
// Open file
file=fopen(filename,"rb");
if (!file)
perror("Error opening file");
// Get header info, decompress
cinfo.err=jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo,file);
jpeg_read_header(&cinfo,TRUE);
jpeg_start_decompress(&cinfo);
// Allocate memory
raw_image=(unsigned char *) malloc(cinfo.output_width*cinfo.output_height*cinfo.num_components);
// Read image, one scan at a time
row_pointer[0]=(unsigned char *) malloc(cinfo.output_width*cinfo.num_components);
while(cinfo.output_scanline<cinfo.image_height) {
jpeg_read_scanlines(&cinfo,row_pointer,1);
for(i=0;i<cinfo.image_width*cinfo.num_components;i++)
raw_image[location++]=row_pointer[0][i];
}
// wrap up decompression, destroy objects, free pointers and close open files
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
// Copy image to image struct
img.nx=cinfo.image_width;
img.ny=cinfo.image_height;
img.nz=cinfo.num_components;
img.z=(float *) malloc(sizeof(float)*img.nx*img.ny*img.nz);
// Fill image
for (i=0;i<img.nx;i++) {
for (j=0;j<img.ny;j++) {
for (k=0;k<img.nz;k++) {
l=img.nz*(i+img.nx*j)+k;
img.z[l]=(float) raw_image[l];
}
}
}
// Free allocated memory
free(row_pointer[0]);
free(raw_image);
// Close file
fclose(file);
return img;
}
// Write jpg
void write_jpg(char *filename,struct jpeg_image img)
{
int i,j,k,l,m;
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
JSAMPROW row_pointer[1];
FILE *outfile;
unsigned char *raw_image=NULL;
outfile=fopen(filename,"wb");
cinfo.err=jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
jpeg_stdio_dest(&cinfo,outfile);
cinfo.image_width=img.nx;
cinfo.image_height=img.ny;
cinfo.input_components=3;
cinfo.in_color_space=JCS_RGB;
jpeg_set_defaults(&cinfo);
jpeg_start_compress(&cinfo,TRUE);
// Allocate memory
raw_image=(unsigned char *) malloc(cinfo.image_width*cinfo.image_height*cinfo.input_components);
// Fill image
for (i=0;i<img.nx;i++) {
for (j=0;j<img.ny;j++) {
for (k=0;k<img.nz;k++) {
l=img.nz*(i+img.nx*j)+k;
raw_image[l]=(unsigned char) img.z[l];
}
}
}
while(cinfo.next_scanline<cinfo.image_height) {
row_pointer[0]=&raw_image[cinfo.next_scanline*cinfo.image_width*cinfo.input_components];
jpeg_write_scanlines(&cinfo,row_pointer,1);
}
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
fclose(outfile);
return;
}