-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimrotate.c
497 lines (441 loc) · 15.4 KB
/
imrotate.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
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/** @file imrotate.c
*/
#include <math.h>
#include "CommandLineInterface/CLIcore.h"
#include "COREMOD_memory/COREMOD_memory.h"
// ==========================================
// Forward declaration(s)
// ==========================================
imageID basic_rotate(const char *__restrict ID_name,
const char *__restrict IDout_name,
float angle);
// ==========================================
// Command line interface wrapper function(s)
// ==========================================
static errno_t image_basic_rotate_cli()
{
if(CLI_checkarg(1, 4) + CLI_checkarg(2, 3) + CLI_checkarg(3, 1) == 0)
{
basic_rotate(data.cmdargtoken[1].val.string,
data.cmdargtoken[2].val.string,
data.cmdargtoken[3].val.numf);
return CLICMD_SUCCESS;
}
else
{
return CLICMD_INVALID_ARG;
}
}
// ==========================================
// Register CLI command(s)
// ==========================================
errno_t imrotate_addCLIcmd()
{
RegisterCLIcommand("rotateim",
__FILE__,
image_basic_rotate_cli,
"rotate 2D image",
"<image in> <output image> <angle>",
"rotateim imin imout 230",
"long basic_rotate(const char *ID_name, const char "
"*ID_out_name, float angle)");
return RETURN_SUCCESS;
}
imageID basic_rotate(const char *__restrict ID_name,
const char *__restrict IDout_name,
float angle)
{
imageID ID, IDout;
uint32_t naxes[2];
ID = image_ID(ID_name);
naxes[0] = data.image[ID].md[0].size[0];
naxes[1] = data.image[ID].md[0].size[1];
create_2Dimage_ID(IDout_name, naxes[0], naxes[1], &IDout);
for(uint32_t jj = 0; jj < naxes[1]; jj++)
for(uint32_t ii = 0; ii < naxes[0]; ii++)
{
long iis = (long)(naxes[0] / 2 + (ii - naxes[0] / 2) * cos(angle) +
(jj - naxes[1] / 2) * sin(angle));
long jjs = (long)(naxes[1] / 2 - (ii - naxes[0] / 2) * sin(angle) +
(jj - naxes[1] / 2) * cos(angle));
if((iis > 0) && (jjs > 0) && (iis < naxes[0]) && (jjs < naxes[1]))
{
data.image[IDout].array.F[jj * naxes[0] + ii] =
data.image[ID].array.F[jjs * naxes[0] + iis];
}
}
return (IDout);
}
imageID basic_rotate90(const char *__restrict ID_name,
const char *__restrict ID_out_name)
{
imageID ID;
imageID IDout;
uint32_t naxes[2];
ID = image_ID(ID_name);
naxes[0] = data.image[ID].md[0].size[0];
naxes[1] = data.image[ID].md[0].size[1];
create_2Dimage_ID(ID_out_name, naxes[1], naxes[0], &IDout);
for(uint32_t jj = 0; jj < naxes[0]; jj++)
for(uint32_t ii = 0; ii < naxes[1]; ii++)
{
uint32_t iis = jj;
uint32_t jjs = naxes[1] - ii - 1;
data.image[IDout].array.F[jj * naxes[1] + ii] =
data.image[ID].array.F[jjs * naxes[0] + iis];
}
return IDout;
}
imageID basic_rotate_int(const char *__restrict ID_name,
const char *__restrict ID_out_name,
long nbstep)
{
float angle;
imageID ID;
imageID IDout;
uint32_t naxes[2];
ID = image_ID(ID_name);
naxes[0] = data.image[ID].md[0].size[0];
naxes[1] = data.image[ID].md[0].size[1];
create_2Dimage_ID(ID_out_name, naxes[0], naxes[1], &IDout);
for(int i = 0; i < nbstep; i++)
{
angle = M_PI * i / nbstep;
for(uint32_t jj = 0; jj < naxes[1]; jj++)
for(uint32_t ii = 0; ii < naxes[0]; ii++)
{
long iis =
(long)(naxes[0] / 2 + (ii - naxes[0] / 2) * cos(angle) +
(jj - naxes[1] / 2) * sin(angle));
long jjs =
(long)(naxes[1] / 2 + (ii - naxes[0] / 2) * sin(angle) -
(jj - naxes[1] / 2) * cos(angle));
if((iis > 0) && (jjs > 0) && (iis < naxes[0]) &&
(jjs < naxes[1]))
{
data.image[IDout].array.F[jj * naxes[0] + ii] +=
data.image[ID].array.F[jjs * naxes[0] + iis];
}
}
}
return IDout;
}
/* rotation that keeps photometry - angle is in radians */
imageID basic_rotate2(const char *__restrict ID_name_in,
const char *__restrict ID_name_out,
float angle)
{
imageID ID_in;
imageID ID_out, ID_wout;
uint32_t naxes[2];
uint32_t naxes2[2];
uint64_t nelements;
float rotangle;
float *pixcorner_x;
float *pixcorner_y;
float x, y;
uint32_t NB_step = 20;
uint32_t i, j;
float f1, f2, f3, f4, f5, f6, f7, f8, f9;
/* float *f1a;
float *f2a;
float *f3a;
float *f4a;
float *f5a;
float *f6a;
float *f7a;
float *f8a;
float *f9a;*/
int *f1a;
int *f2a;
int *f3a;
int *f4a;
int *f5a;
int *f6a;
int *f7a;
int *f8a;
int *f9a;
float pixcx, pixcy;
float total;
int xint, yint;
float ccos, ssin;
printf("rotating %s by %f radians ...\n", ID_name_in, angle);
fflush(stdout);
rotangle = angle;
while(rotangle < 0)
{
rotangle += 2.0 * M_PI;
}
while(rotangle > 2 * M_PI)
{
rotangle -= 2.0 * M_PI;
}
/* now the angle is between 0 and 2*PI */
while(rotangle > (M_PI / 2))
{
basic_rotate90(ID_name_in, "tmprot");
delete_image_ID(ID_name_in, DELETE_IMAGE_ERRMODE_WARNING);
copy_image_ID("tmprot", ID_name_in, 0);
delete_image_ID("tmprot", DELETE_IMAGE_ERRMODE_WARNING);
rotangle -= M_PI / 2.0;
}
/* now the angle is between 0 and PI/2 */
ID_in = image_ID(ID_name_in);
naxes[0] = data.image[ID_in].md[0].size[0];
naxes[1] = data.image[ID_in].md[0].size[1];
nelements = naxes[0] * naxes[1];
printf("creating temporary arrays\n");
fflush(stdout);
f1a = (int *) calloc(NB_step * NB_step, sizeof(int));
if(f1a == NULL)
{
PRINT_ERROR("calloc returns NULL pointer");
abort();
}
f2a = (int *) calloc(NB_step * NB_step, sizeof(int));
if(f2a == NULL)
{
PRINT_ERROR("calloc returns NULL pointer");
abort();
}
f3a = (int *) calloc(NB_step * NB_step, sizeof(int));
if(f3a == NULL)
{
PRINT_ERROR("calloc returns NULL pointer");
abort();
}
f4a = (int *) calloc(NB_step * NB_step, sizeof(int));
if(f4a == NULL)
{
PRINT_ERROR("calloc returns NULL pointer");
abort();
}
f5a = (int *) calloc(NB_step * NB_step, sizeof(int));
if(f5a == NULL)
{
PRINT_ERROR("calloc returns NULL pointer");
abort();
}
f6a = (int *) calloc(NB_step * NB_step, sizeof(int));
if(f6a == NULL)
{
PRINT_ERROR("calloc returns NULL pointer");
abort();
}
f7a = (int *) calloc(NB_step * NB_step, sizeof(int));
if(f7a == NULL)
{
PRINT_ERROR("calloc returns NULL pointer");
abort();
}
f8a = (int *) calloc(NB_step * NB_step, sizeof(int));
if(f8a == NULL)
{
PRINT_ERROR("calloc returns NULL pointer");
abort();
}
f9a = (int *) calloc(NB_step * NB_step, sizeof(int));
if(f9a == NULL)
{
PRINT_ERROR("calloc returns NULL pointer");
abort();
}
printf("filling up calibration array ... ");
fflush(stdout);
total = 1.0 / NB_step / NB_step;
ccos = cos(rotangle);
ssin = sin(rotangle);
for(uint32_t ii = 0; ii < NB_step; ii++)
for(uint32_t jj = 0; jj < NB_step; jj++)
{
pixcx = 1.0 * ii / NB_step;
pixcy = 1.0 * jj / NB_step;
for(i = 0; i < NB_step; i++)
for(j = 0; j < NB_step; j++)
{
x = pixcx + 1.0 * (0.5 + i) / NB_step * ccos -
1.0 * (0.5 + j) / NB_step * ssin;
y = pixcy + 1.0 * (0.5 + i) / NB_step * ssin +
1.0 * (0.5 + j) / NB_step * ccos;
if(x < 0)
{
if(y < 1)
{
f1a[jj * NB_step + ii]++;
}
else
{
if(y > 2)
{
f7a[jj * NB_step + ii]++;
}
else
{
f4a[jj * NB_step + ii]++;
}
}
}
else
{
if(x > 1)
{
if(y < 1)
{
f3a[jj * NB_step + ii]++;
}
else
{
if(y > 2)
{
f9a[jj * NB_step + ii]++;
}
else
{
f6a[jj * NB_step + ii]++;
}
}
}
else
{
if(y < 1)
{
f2a[jj * NB_step + ii]++;
}
else
{
if(y > 2)
{
f8a[jj * NB_step + ii]++;
}
else
{
f5a[jj * NB_step + ii]++;
}
}
}
}
}
}
printf("done\n");
fflush(stdout);
pixcorner_x = (float *) malloc(sizeof(float) * nelements);
if(pixcorner_x == NULL)
{
C_ERRNO = errno;
PRINT_ERROR("malloc() error");
exit(0);
}
pixcorner_y = (float *) malloc(sizeof(float) * nelements);
if(pixcorner_y == NULL)
{
C_ERRNO = errno;
PRINT_ERROR("malloc() error");
exit(0);
}
for(uint32_t jj = 0; jj < naxes[1]; jj++)
for(uint32_t ii = 0; ii < naxes[0]; ii++)
{
pixcorner_x[jj * naxes[0] + ii] =
ii * ccos - jj * ssin + ssin * naxes[1] + 1.0;
pixcorner_y[jj * naxes[0] + ii] = ii * ssin + jj * ccos;
}
naxes2[0] =
(long)(sin(rotangle) * naxes[1] + cos(rotangle) * naxes[0] + 2.0);
naxes2[1] =
(long)(cos(rotangle) * naxes[1] + sin(rotangle) * naxes[0] + 2.0);
create_2Dimage_ID(ID_name_out, naxes2[0], naxes2[1], &ID_out);
create_2Dimage_ID("wtmp", naxes2[0], naxes2[1], &ID_wout);
for(uint32_t jj = 0; jj < naxes[1]; jj++)
for(uint32_t ii = 0; ii < naxes[0]; ii++)
{
x = pixcorner_x[jj * naxes[0] + ii];
y = pixcorner_y[jj * naxes[0] + ii];
/*printf("%ld %ld %d %d %f %f %f %f\n",ii,jj,i,j,pixcorner_x[jj*naxes[0]+ii],pixcorner_y[jj*naxes[0]+ii],x,y);*/
xint = (int)(((x + 3 * naxes[0]) - (int)(x + 3 * naxes[0])) *
NB_step);
yint = (int)((y - (int) y) * NB_step);
/* printf("%d %d\n",xint,yint);*/
f1 = 1.0 * f1a[yint * NB_step + xint];
f2 = 1.0 * f2a[yint * NB_step + xint];
f3 = 1.0 * f3a[yint * NB_step + xint];
f4 = 1.0 * f4a[yint * NB_step + xint];
f5 = 1.0 * f5a[yint * NB_step + xint];
f6 = 1.0 * f6a[yint * NB_step + xint];
f7 = 1.0 * f7a[yint * NB_step + xint];
f8 = 1.0 * f8a[yint * NB_step + xint];
f9 = 1.0 * f9a[yint * NB_step + xint];
data.image[ID_out]
.array.F[((long) y) * naxes2[0] + ((long)(x)) - 1] +=
f1 * data.image[ID_in].array.F[jj * naxes[0] + ii];
data.image[ID_out].array.F[((long) y) * naxes2[0] + ((long)(x))] +=
f2 * data.image[ID_in].array.F[jj * naxes[0] + ii];
data.image[ID_out]
.array.F[((long) y) * naxes2[0] + ((long)(x)) + 1] +=
f3 * data.image[ID_in].array.F[jj * naxes[0] + ii];
data.image[ID_out]
.array.F[((long) y + 1) * naxes2[0] + ((long)(x)) - 1] +=
f4 * data.image[ID_in].array.F[jj * naxes[0] + ii];
data.image[ID_out]
.array.F[((long) y + 1) * naxes2[0] + ((long)(x))] +=
f5 * data.image[ID_in].array.F[jj * naxes[0] + ii];
data.image[ID_out]
.array.F[((long) y + 1) * naxes2[0] + ((long)(x)) + 1] +=
f6 * data.image[ID_in].array.F[jj * naxes[0] + ii];
data.image[ID_out]
.array.F[((long) y + 2) * naxes2[0] + ((long)(x)) - 1] +=
f7 * data.image[ID_in].array.F[jj * naxes[0] + ii];
data.image[ID_out]
.array.F[((long) y + 2) * naxes2[0] + ((long)(x))] +=
f8 * data.image[ID_in].array.F[jj * naxes[0] + ii];
data.image[ID_out]
.array.F[((long) y + 2) * naxes2[0] + ((long)(x)) + 1] +=
f9 * data.image[ID_in].array.F[jj * naxes[0] + ii];
data.image[ID_wout]
.array.F[((long) y) * naxes2[0] + ((long)(x)) - 1] += f1;
data.image[ID_wout]
.array.F[((long) y) * naxes2[0] + ((long)(x))] += f2;
data.image[ID_wout]
.array.F[((long) y) * naxes2[0] + ((long)(x)) + 1] += f3;
data.image[ID_wout]
.array.F[((long) y + 1) * naxes2[0] + ((long)(x)) - 1] += f4;
data.image[ID_wout]
.array.F[((long) y + 1) * naxes2[0] + ((long)(x))] += f5;
data.image[ID_wout]
.array.F[((long) y + 1) * naxes2[0] + ((long)(x)) + 1] += f6;
data.image[ID_wout]
.array.F[((long) y + 2) * naxes2[0] + ((long)(x)) - 1] += f7;
data.image[ID_wout]
.array.F[((long) y + 2) * naxes2[0] + ((long)(x))] += f8;
data.image[ID_wout]
.array.F[((long) y + 2) * naxes2[0] + ((long)(x)) + 1] += f9;
}
for(uint32_t jj = 0; jj < naxes2[1]; jj++)
for(uint32_t ii = 0; ii < naxes2[0]; ii++)
{
if(data.image[ID_wout].array.F[jj * naxes2[0] + ii] >
(0.9 * NB_step * NB_step))
{
data.image[ID_out].array.F[jj * naxes2[0] + ii] /=
data.image[ID_wout].array.F[jj * naxes2[0] + ii];
}
else
{
data.image[ID_out].array.F[jj * naxes2[0] + ii] *= total;
}
}
delete_image_ID("wtmp", DELETE_IMAGE_ERRMODE_WARNING);
free(pixcorner_x);
free(pixcorner_y);
free(f1a);
free(f2a);
free(f3a);
free(f4a);
free(f5a);
free(f6a);
free(f7a);
free(f8a);
free(f9a);
printf("done\n");
fflush(stdout);
return ID_out;
}