-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfitsiodemosaicbayer.c
1676 lines (1535 loc) · 65.4 KB
/
fitsiodemosaicbayer.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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
C. Moos
Template: fitsslpitbayer.c from Mischa Schirmer
Revise: 5 01.07.2020
Interpolationsmethoden
fitsiodemosaicbayer.c
Changes 1.3: black edge reduced
Changes 1.4: Edge smoothed, Error in PPG last interpolation in row and column was wrong
*/
/*
Changes 1.4 Rev3: Usage Text missleading: 2.21 1.00 1.51 1.00 = RGBg
Changes 1.5 Rev1: Added median filter
Added L[ab] export
200 exchanged with FILEMAX
changes 1.6 Rev1: make header interpolate.h
removed fitstools.h, not c99 conform
did not compile!! retun to source file without header interpolate.h
added colorsystems.h
changes 1.7 : -q 3 WCAPI added
diff, sum, median3x3, fmedian3x3,
fill_intensities5x5[_2], fill_intensities7x7
filterm added
-t option added for generating test-patterns, not really a feature, mismatches other than RGGB, not complete
Rev 2:
qfits_header_add for history information added
switch -q 3 in usage added
Rev 3:
second try :declarations in demosaic.h
still doesn not compile, gave up
will deliver _demosaic.c, _demosaic.h, _myfitstools.c, _myfitstools.h for packaging by mischa
Rev 4:
fill_intensities5x5 and fill_intensities5x5_2 combined to fill_intensities5x5
open optimizations: treating the outer frame,
fill_intensities_2 integration in fill_intensities
-m with pass > 2 is very slow
-d despeckle generates sometimes crosses
for fmedian3x3 maximum memory consumption is about 9ximage!!HEAVY didn't find a way to reduce.
Maybe some functions are already in fitstools included, didn't look for e.g fitslaplace, fitsmedian
sometimes functions works with reference to image, sometimes they don't (sum and diff versus median3x3 and fmedian3x3)
despeckle seems to become obsolete, related to success of artifact reducing bei fmedian, then median3x3 is obsolete too
CIE-Lab has to proofe needs, maximum level is reduced to about 400 max, due to a 8 Bit model, should be watched further
-t for other than RGGB does not work properly
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
//#include "eclipse.h"
#include "fitsio.h"
#include "fitsiotools.h"
//#include "myfitstools.h"//
//#include "demosaic.h" doesnt compile because of fitstools.h
#include "colorsystems.h" // CIEL*a*b
#define RED 1
#define GREEN1 2 // in blue line
#define BLUE 3
#define GREEN2 4 // in red line
// compiled with
// gcc -o fitsiodemosaicbayer fitsiodemosaicbayer.c colorsystems.c -L. -lcfitsio -lm //-lnsl
void usage(int i, char *argv[])
{
if (i == 0) {
fprintf(stderr,"\n");
fprintf(stderr," Version 1.7 Rev 5 (2020-07-01) fitsio\n\n");
fprintf(stderr," Author: Carsten Moos\n\n");
fprintf(stderr," USAGE: %s \n", argv[0]);
fprintf(stderr," -i input_image \n");
fprintf(stderr," -p pattern (available: RGGB, GRBG, GBRG, BGGR)\n");
fprintf(stderr," -q 0 for bilinear Interpolation\n");
fprintf(stderr," -q 1 for gradient Interpolation\n");
fprintf(stderr," -q 2 for PPG Interpolation\n");
fprintf(stderr," -q 3 for WACP Interpolation\n");
fprintf(stderr," -c 2.21 1.00 1.51 1.006 for bayerfilter RGBG colorbalance\n");
fprintf(stderr," -o 256 for Offsetcorrection before RGBG colorbalance\n");
fprintf(stderr," -m 2 for 1-5 times artifact filter 3x3 of crominannce\n");
fprintf(stderr," -d 1000 for despeckle above level 1000 filter 1x3 of crominannce\n");
fprintf(stderr," -l with export of Luminance CIEL*a*b\n");
fprintf(stderr," -t with generates RAW testimage: R=50 G=101[2] B=75\n");
fprintf(stderr," PURPOSE: Demosaics a bayer matrix fits image into\n");
fprintf(stderr," three monochrome fits images for RGB.\n\n");
exit(1);
}
}
// sollte nach library demosaic.c,
//laesst sich aber mit der fitstools.h compilieren ??
// in demosaic.c
float hue_transit(float l1,float l2,float l3,float v1,float v3)
{
// for PPG
//printf("hue_transit: l1 %5.1f l2 %5.1f l3 %5.1f v1 %5.1f v3 %5.1f\n",l1,l2,l3,v1,v3);
if((l1<l2 && l2<l3) || (l1> l2 && l2 > l3))
return(v1+(v3-v1) * (l2-l1)/(l3-l1));
else
return((v1+v3)/2.0+(l2*2.0-l1-l3)/4.0);
}
int direction(float N, float E, float W, float S)
{
// for PPG
if (N < E && W < S)
if ( N < W)
return(1);
else
return(3);
else
if (E < S)
return (2);
else
return (4);
}
void fill_intensities5x5(int k,int n, float *in,float array[6][6]){
// array is 6x6, but used for use 5x5 without null index
// k is actual pointer position in image
// n is images width
// array is local reference to filtermatrix
// input image pointer maybe usable with fill_intensities() as replacement
/*
FILL_INTENSITIES: fills an array of a givel center (k) for better access to surrounding
elements
I11 I12 I13 I14 I15
I21 I22 I23 I24 I25
I31 .. I35
I41 .. I45
I51 I52 I53 I54 I55
*/
int i,j;
for (j=1;j<=5;j++){ // rows
for(i=1;i<=5;i++){ // cols
array[j][i]=in[k + (i-3) + (j-3)*n];
k=k;
}
}
}
//void fill_intensities7x7(long k,int n,image_t *in,float array[8][8]){
void fill_intensities7x7(long k,int n,float *in,float array[8][8]){
// array is 8x8, but used for use 7x7 without null index
// k is actual pointer position in image
// n is images width
// array is local reference to filtermatrix
/*
FILL_INTENSITIES: fills an array of a givel center (k) for better access to surrounding
elements
I11 I12 I13 I14 I15 I16 I17
I21 I22 I23 I24 I25 I26 I27
I31 .. I37
I41 .. I47
I51 .. I57
I61 .. I67
I71 I72 I73 I74 I75 I76 I77
*/
int i,j;
for (j=1;j<=7;j++){ // rows
for(i=1;i<=7;i++){ // cols
//array[j][i]=in->data[k+ (i-4) + (j-4)*n];
array[j][i]=in[k+ (i-4) + (j-4)*n];
}
}
}
void despeckle (int m,int n,int filterdirection ,float level,float *in, float *out){
// m is image height
// n is image width
// filterdirection can be 1,2,3 or 4 for experimental
// level is a threshold, only pixelvalues above will be filtered
// in is a reference pointer to input_image
// out is a reference pointer to out_image
/*
DESPECKLE: median filter of 3 pixelvalues (3 cells)
to avoid hotpixel.
Filter is rather weak, but sometimes crosses appear.
*/
int k,filtersize=1;
int j,jt, i,it;
int cell;
long counter=0;
float filter[9];
//printf("despeckle filter, ");
for (j=0; j<m; j++) { //row
for (i=0; i<n; i++) { // col
if (filterdirection <1 || filterdirection >5){
printf("no direction recognized, copying");
out[i+n*j]=in[i+n*j];
}
else
{
k = 0; cell=0;
if ( in[i+n*j]>level ){
for (jt=j-filtersize;jt<=j+filtersize;jt++) {
for (it=i-filtersize;it<=i+filtersize;it++) {
if (it>=0 && jt>=0 && it<n && jt<m) {
cell=it-i+2+(jt-j+2-1)*3;
if (filterdirection==1 && (cell ==3 || cell==5 || cell==7) ){
filter[k] = in[it+n*jt];
k++;
}
if (filterdirection==2 && (cell ==1 || cell==5 || cell==9) ){
filter[k] = in[it+n*jt];
k++;
}
if (filterdirection==3 && (cell ==2 || cell==5 || cell==8) ){
filter[k] = in[it+n*jt];
k++;
}
if (filterdirection==4 && (cell ==4 || cell==5 || cell==6) ){
filter[k] = in[it+n*jt];
k++;
}
}
}//it
}//jt
qsort(filter, k, sizeof(float), compare);
if (k>1) { counter++;
if (k % 2 == 0)
out[i+n*j] = 0.5*(filter[k/2-1] + filter[k/2]);
else
out[i+n*j] = filter[k/2];
}
else // k=1
{out[i+n*j]=in[i+n*j]; counter++;}
}// below level
else
out[i+n*j]=in[i+n*j];
}
}// i
}//j
}
void filter_m(int m, int n, float *in,float *out){
// m is image height
// n is image width
// in is reference to input image
// out is reference to output image
/*
FILTER_M:
convolve input image with Laplacian operator LP
1 9 1
1/11 * 9 -40 9
1 9 1
*/
int j,i,r,s;
float I[6][6],OP[6][6]={ {1, 9, 1},{ 9,-40, 9},{ 1, 9, 1} }, weight;
int T;
T=15; // experimental, suggestion of Lu & Tan
weight=0;
for (j=0; j<m; j++) { //row
for (i=0; i<n; i++) { // col
if (i>2&&i<n-2&&j>2&&j<m-2){
//f=*(in[i-1+ n*(j-1)] +in[i+ n*(j-1)]*9 +in[i+1+ n*(j-1)]+
// in[i-+n*j]*9 +in[i+ n*j]*40 + in[i+1+n*j]*9 +
//in[i-1+ n*(j+1)] + in[i+ n*(j+1)]*9 + in[i+1+ n*(j+1)]);
for (r=1;r<=3;r++){
for(s=1;s<=3;s++){
weight+=I[r][s]*OP[r][s];
}
}
weight=weight/11.0;
if (weight>T) out[i+n*j]=1;
else out[i+n*j]=0;
}
}
}
}
void median3x3(int m, int n ,float *in ){
// m is image height
// n is image width
// in is refernce to input image, will be replaced by results
/*
MEDIAN3x3: does a median of surrounding 3x3 fields
*/
float *out;
int k,filtersize=1;
int j,jt, i,it;
out= (float*) calloc(m*n, sizeof(float));
float filter[9];
for (j=0; j<m; j++) { //row
for (i=0; i<n; i++) { // col
k = 0;
for (jt=j-filtersize;jt<=j+filtersize;jt++) {
for (it=i-filtersize;it<=i+filtersize;it++) {
if (it>=0 && jt>=0 && it<n && jt<m) {
filter[k] = in[it+n*jt];
k++;
}
}//it
}//jt
qsort(filter, k, sizeof(float), compare);
if (k>1) {
if (k % 2 == 0)
out[i+n*j] = 0.5*(filter[k/2-1] + filter[k/2]);
else
out[i+n*j] = filter[k/2];
}
else // outside
out[i+n*j]=in[i+n*j];
}// i
// if ((j%100)==0) printf(".");
}//j
//copy out nach in
//printf("copy to out, ");
for (j=0; j<m; j++) { //row
for (i=0; i<n; i++) { // col
in[i+n*j]=out[i+n*j];
}
}
//printf("done\n");
free(out);
}
void fmedian3x3(int m, int n ,float *in ,float *mask ){
// m is image height
// n is image width
// in is refernce to input image, will be replaced by results
// mask is refernce to a pixelmask, same size as in
/*
FMEDIAN3x3: does a median of surrounding 3x3 fields if local mask is 1
is for artifact reduction, Lu & Tan
*/
float *out;
int k,filtersize=1;
int j,jt, i,it;
out= (float*) calloc(m*n, sizeof(float));
float filter[9];
for (j=0; j<m; j++) { //row
for (i=0; i<n; i++) { // col
if (mask){
k = 0;
for (jt=j-filtersize;jt<=j+filtersize;jt++) {
for (it=i-filtersize;it<=i+filtersize;it++) {
if (it>=0 && jt>=0 && it<n && jt<m) {
filter[k] = in[it+n*jt];
k++;
}
}//it
}//jt
qsort(filter, k, sizeof(float), compare);
if (k>1) {
if (k % 2 == 0)
out[i+n*j] = 0.5*(filter[k/2-1] + filter[k/2]);
else
out[i+n*j] = filter[k/2];
}
else // outside
out[i+n*j]=in[i+n*j];
}//masked 1
else out[i+n*j]=in[i+n*j];
}// i
}//j
//copy out nach in
for (j=0; j<m; j++) { //row
for (i=0; i<n; i++) { // col
in[i+n*j]=out[i+n*j];
}
}
free(out);
}
void diff(int m,int n,float *in1,float *in2,float *out){
// m is image height
// n is image width
// in1 is refernce to input image1
// in2 is refernce to input image2, will be subtracted from image1
// out is refernce to output
int i, j;
//printf("diff ");
for (j=0; j<m; j++) { //row
for (i=0; i<n; i++) { // col
out[i+n*j]=in1[i+n*j]-in2[i+n*j];
}
}
//printf("done\n");
}
void sum(int m,int n,float *in1,float *in2, float *out,float factor){
// m is image height
// n is image width
// in1 is refernce to input image1
// in2 is refernce to input image2, will be subtracted from image1
// out is refernce to output
// factor is multiplicator to weight the sum, could mostly be 1.0
int i, j;
//printf("sum ");
for (j=0; j<m; j++) { //row
for (i=0; i<n; i++) { // col
out[i+j*n]=(in1[i+j*n]+in2[i+j*n])*factor;
}
}
}
//
// end for export to library demosaic.c
void printerror( int status); // fitsio
void writeoutputfile(char *name, fitsfile *old , fitsfile *new, float *data,long *w_npixels, int *w_status); // fitsio
int main(int argc, char *argv[])
{
//******************************************************
// Demosaics a bayer matrix fits image (CFA)
//******************************************************
int n, m, ns, ms, flag_q, flag_l, flag_t, flag_d,median, length;
float R=1.0,G=1.0,g=1.0,B=1.0,o=0.0,level; // Colorbalance of 4 Colorfilters
int xoffset,yoffset; // for color determing
long i, j, k1;
float H,V; // for Gradient horizontal, vertical
float DN,DE,DW,DS,dne,dnw; // for PPG
float *channel1, *channel2, *channel3, *L=NULL;
char input_image[FILEMAX], pattern[FILEMAX], *tmp6;
char out1[FILEMAX], out2[FILEMAX], out3[FILEMAX], out4[FILEMAX];
//image_t *image_in, *image_out1, *image_out2, *image_out3, *image_out4=NULL;
//qfits_header *header;
// new for libchange
struct image_t {
int lx;
int ly;
float *data;
};
int status=0, nkeys, keypos, nfound, anynull; // fitsio
char card[FLEN_CARD]; // fitsio
fitsfile *fptr,*ofptr1,*ofptr2,*ofptr3,*ofptr4; // fitsio
long naxes[2]; // fitsio
long nbuffer, fpixel, npixels; // fitsio
float nullval; // fitsio
struct image_t *image_in, iimg1; // fitsio
char buffer[30]; // for History information
// print usage if no arguments were given
// default: Linear interpolation
flag_q = 0;
flag_l = 0;
flag_d = 0;
flag_t = 0;
level= 0;
median = 0;
k1=0;
if (argc==1) usage(0, argv);
for (i=1; i<argc; i++) {
if (argv[i][0] == '-') {
switch(tolower((int)argv[i][1])) {
case 'i': strcpy(input_image,argv[++i]);
break;
case 'p': strcpy(pattern,argv[++i]);
break;
case 'q': flag_q = atoi(argv[++i]);
break;
case 'c': R = atof(argv[++i]);
G = atof(argv[++i]);
B = atof(argv[++i]);
g = atof(argv[++i]);
break;
case 'o': o = atof(argv[++i]);
break;
case 'm': median = atoi(argv[++i]);
break;
case 'd': level = atof(argv[++i]);flag_d=1;
break;
case 'l': flag_l = 1;
break;
case 't': flag_t = 1;
break;
}
}
}
if (! (strcmp(pattern,"RGGB") == 0 || strcmp(pattern,"GRBG") == 0 ||
strcmp(pattern,"GBRG") == 0 || strcmp(pattern,"BGGR") == 0)) {
printf("\nBayer pattern not recognised. Nothing will be done.\n\n");
exit (0);
}
// read the FITS header and the data block
// checkfile(input_image);
if (fits_open_file(&fptr,input_image,READONLY, &status)){
printerror( status );}
//header = qfits_header_read(input_image);
if (fits_read_keys_lng(fptr,"NAXIS",1,2,naxes,&nfound, &status)){
printerror( status );}
//image_in = image_load(input_image);
npixels=naxes[0]*naxes[1];
fpixel=1;
nullval=0;
//image_in1->data[0] =(float *) malloc (npixels * sizeof (float));
iimg1.data =(float *) malloc (npixels * sizeof (float));
iimg1.data[npixels-1]=0;
if (fits_read_img(fptr,TFLOAT,fpixel,npixels,&nullval,iimg1.data, &anynull,&status)){
printerror( status );}
// for new lib
iimg1.lx=naxes[0];
iimg1.ly=naxes[1];
// Uebergabe
image_in=&iimg1;
n = image_in->lx;
m = image_in->ly;
// chop the last row / column of pixels if the image dimensions are uneven
if ( n % 2 != 0) n = n - 1;
if ( m % 2 != 0) m = m - 1;
ns = n / 2;
ms = m / 2;
// the file name prefix
length = strlen(input_image) - 5; // Oh har, hardcodiertes .fits
// tmp6 = strndup(input_image, length);
input_image[length]='\0';
tmp6=input_image;
strcpy(out1, tmp6);
strcpy(out2, tmp6);
strcpy(out3, tmp6);
strcpy(out4, tmp6);
// cut all pattern to RGGB
if (strcmp(pattern,"RGGB") == 0){
xoffset=0;
yoffset=0;
}
if (strcmp(pattern,"GRBG") == 0){
xoffset=1;
yoffset=0;
}
if (strcmp(pattern,"GBRG") == 0){
xoffset=0;
yoffset=1;
}
if (strcmp(pattern,"BGGR") == 0){
xoffset=1;
yoffset=1;
}
/*
RGRGRGRGR
gBgBgBgBg
RGRGRGRGR
gBgBgBgBg
*/
/*
// only for testing and distinguishing the output files
if (median) {
strcat(out1,".F");
strcat(out2,".F");
strcat(out3,".F");
strcat(out4,".F");
}
if (flag_d) {
strcat(out1,".D");
strcat(out2,".D");
strcat(out3,".D");
strcat(out4,".D");
}
*/
strcat(out1,".R.fits");
strcat(out2,".G.fits");
strcat(out3,".B.fits");
strcat(out4,".L.fits");
channel1 = (float*) calloc(n*m, sizeof(float)); //red
channel2 = (float*) calloc(n*m, sizeof(float)); //green
channel3 = (float*) calloc(n*m, sizeof(float)); //blue
sprintf(buffer,"Bayerpattern used: %s",pattern);
//qfits_header_add(header,"HISTORY",buffer,"","");
// correct Offset & correct colors
if (!(R==1.0 && G==1.0 && B==1.0 && o==0.0))
{
printf("subtracts offset:%4.2f\ncolorbalance with sensormultipliers of R:G:B:G = %4.2f:%4.2f:%4.2f:%4.2f\n",o,R,G,B,g);
sprintf(buffer,"Correct offset: %4.2f ",o);
//qfits_header_add(header,"HISTORY",buffer,"","");
sprintf(buffer,"Correct colorbalance R:G:B:G = %4.2f:%4.2f:%4.2f:%4.2f",R,G,B,g);
//qfits_header_add(header,"HISTORY",buffer,"","");
for (j=0; j<m; j++) { //Zeilen
for (i=0; i<n; i++) { // Spalten
if(j==0||j>=m-1||i==0|| i>=n-1) { // Rand oben unten links rechts
// don't care here
k1++;
}
else{
if ((j+yoffset)%2 ==0 && (i+xoffset)%2 ==0){ // Rotes Feld
image_in->data[(i+n*j)] = R*(image_in->data[(i+n*j)]-o);
k1++;
}
if ((j+yoffset)%2 ==1 && (i+xoffset)%2 ==0){ // gruenes Feld1, rot oben
image_in->data[(i+n*j)]= G*(image_in->data[(i+n*j)]-o);
k1++;
}
if ((j+yoffset)%2 ==0 && (i+xoffset)%2 ==1){ // gruenes Feld 2 blau oben
image_in->data[(i+n*j)]= g*(image_in->data[(i+n*j)]-o);
k1++;;
}
if ((j+yoffset)%2 ==1 && (i+xoffset)%2 ==1){ // blaues Feld
image_in->data[(i+n*j)] = B*(image_in->data[(i+n*j)]-o);
k1++;
}
}
}
}
}
k1=0;
if (flag_t){
printf("creating testmap.fits ...");
strcpy(out1, "testmap.fits");
//image_out1 = image_new(600,400);
npixels=600*400;
for (j=yoffset; j<(400+yoffset); j++){ //rows
for(i=xoffset;i<(600+xoffset);i++){ // cols
if ((j+yoffset)%2 ==0 && (i+xoffset)%2 ==0) channel1[k1]=50; // rotes Feld
if ((j+yoffset)%2 ==1 && (i+xoffset)%2 ==0) channel1[k1]=100; // grunes Feld
if ((j+yoffset)%2 ==0 && (i+xoffset)%2 ==1) channel1[k1]=101; // grunes Feld
if ((j+yoffset)%2 ==1 && (i+xoffset)%2 ==1) channel1[k1]=75; // blaues Feld
k1++;
}
}
//image_save_fits_hdrdump(image_out1, out1, header, BPP_IEEE_FLOAT);
writeoutputfile(out1, fptr , ofptr1, channel1, &npixels, &status);
printf("done!\n");
exit (0);
}
k1 = 0; // red
if (flag_q == 0){
//qfits_header_add(header,"HISTORY","Interpolation: bilinear","","");
printf("Using Bilinear\n");
for (j=0; j<m; j++) { //Zeilen
for (i=0; i<n; i++) { // Spalten
if(j==0||j>=m-1||i==0|| i>=n-1) { // Rand oben unten links rechts
/*channel1[k1]=image_in->data[(i+n*j)];
channel2[k1]=image_in->data[(i+n*j)];
channel3[k1]=image_in->data[(i+n*j)];*/
if(!xoffset && !yoffset){ // RGGB
if ((j+yoffset)%2 ==0 && (i+xoffset)%2 ==0){ // rotes Feld
channel1[k1] = image_in->data[(i+n*j)];
channel2[k1] = image_in->data[(i+1+n*j)];
channel3[k1] = image_in->data[(i+1+n+n*j)];
}
if ((j+yoffset)%2 ==1 && (i+xoffset)%2 ==0){ // gruenes Feld1, rot oben
channel1[k1] = image_in->data[(i-n+n*j)];
channel2[k1] = image_in->data[(i+n*j)];
channel3[k1] = image_in->data[(i+1+n*j)];
}
if ((j+yoffset)%2 ==0 && (i+xoffset)%2 ==1){ // gruenes Feld 2 blau oben
channel1[k1] = image_in->data[(i-1+n*j)];
channel2[k1] = image_in->data[(i+n*j)];
channel3[k1] = image_in->data[(i+n+n*j)];
}
if ((j+yoffset)%2 ==1 && (i+xoffset)%2 ==1){ // blaues Feld
channel1[k1] = image_in->data[(i-1-n+n*j)];
channel2[k1] = image_in->data[(i-1+n*j)];
channel3[k1] = image_in->data[(i+n*j)];
}
}
if(xoffset && !yoffset){ //GRBG
if ((j+yoffset)%2 ==0 && (i+xoffset)%2 ==0){ // rotes Feld
channel1[k1] = image_in->data[(i+n*j)];
channel2[k1] = image_in->data[(i-1+n*j)];
channel3[k1] = image_in->data[(i-1+n+n*j)];
}
if ((j+yoffset)%2 ==1 && (i+xoffset)%2 ==0){ // gruenes Feld1, rot oben
channel1[k1] = image_in->data[(i-n+n*j)];
channel2[k1] = image_in->data[(i+n*j)];
channel3[k1] = image_in->data[(i-1+n*j)];
}
if ((j+yoffset)%2 ==0 && (i+xoffset)%2 ==1){ // gruenes Feld 2 blau oben
channel1[k1] = image_in->data[(i+1+n*j)];
channel2[k1] = image_in->data[(i+n*j)];
channel3[k1] = image_in->data[(i+n+n*j)];
}
if ((j+yoffset)%2 ==1 && (i+xoffset)%2 ==1){ // blaues Feld
channel1[k1] = image_in->data[(i+1-n+n*j)];
channel2[k1] = image_in->data[(i+1+n*j)];
channel3[k1] = image_in->data[(i+n*j)];
}
}
if(!xoffset && yoffset){ //GBRG
if ((j+yoffset)%2 ==0 && (i+xoffset)%2 ==0){ // rotes Feld
channel1[k1] = image_in->data[(i+n*j)];
channel2[k1] = image_in->data[(i-n+n*j)];
channel3[k1] = image_in->data[(i+1-n+n*j)];
}
if ((j+yoffset)%2 ==1 && (i+xoffset)%2 ==0){ // gruenes Feld1, rot oben
channel1[k1] = image_in->data[(i+n+n*j)];
channel2[k1] = image_in->data[(i+n*j)];
channel3[k1] = image_in->data[(i+1+n*j)];
}
if ((j+yoffset)%2 ==0 && (i+xoffset)%2 ==1){ // gruenes Feld 2 blau oben
channel1[k1] = image_in->data[(i-1+n*j)];
channel2[k1] = image_in->data[(i+n*j)];
channel3[k1] = image_in->data[(i-n+n*j)];
}
if ((j+yoffset)%2 ==1 && (i+xoffset)%2 ==1){ // blaues Feld
channel1[k1] = image_in->data[(i-1+n+n*j)];
channel2[k1] = image_in->data[(i-1+n*j)];
channel3[k1] = image_in->data[(i+n*j)];
}
}
if(xoffset && yoffset){ //BGGR
if ((j+yoffset)%2 ==0 && (i+xoffset)%2 ==0){ // rotes Feld
channel1[k1] = image_in->data[(i+n*j)];
channel2[k1] = image_in->data[(i-1+n*j)];
channel3[k1] = image_in->data[(i-1-n+n*j)];
}
if ((j+yoffset)%2 ==1 && (i+xoffset)%2 ==0){ // gruenes Feld1, rot oben
channel1[k1] = image_in->data[(i+n+n*j)];
channel2[k1] = image_in->data[(i+n*j)];
channel3[k1] = image_in->data[(i-1+n*j)];
}
if ((j+yoffset)%2 ==0 && (i+xoffset)%2 ==1){ // gruenes Feld 2 blau oben
channel1[k1] = image_in->data[(i+1+n*j)];
channel2[k1] = image_in->data[(i+n*j)];
channel3[k1] = image_in->data[(i-n+n*j)];
}
if ((j+yoffset)%2 ==1 && (i+xoffset)%2 ==1){ // blaues Feld
channel1[k1] = image_in->data[(i+1+n+n*j)];
channel2[k1] = image_in->data[(i+1+n*j)];
channel3[k1] = image_in->data[(i+n*j)];
}
}
k1++;
}
else{
if ((j+yoffset)%2 ==0 && (i+xoffset)%2 ==0){ // rotes Feld
channel1[k1] = image_in->data[(i+n*j)];
channel2[k1]=(image_in->data[(i-1+n*j)]+ image_in->data[(i+1+n*j)]
+image_in->data[(i-n+n*j)] + image_in->data[(i+n+n*j)]) / 4.0;
channel3[k1] = (image_in->data[(i-n-1+n*j)] + image_in->data[(i-n+1+n*j)]+
image_in->data[(i+n-1+n*j)] + image_in->data[(i+n+1+n*j)]) /4.0;
k1++;
}
if ((j+yoffset)%2 ==1 && (i+xoffset)%2 ==0){ // gruenes Feld1, rot oben
channel1[k1] = (image_in->data[(i-n+n*j)]+image_in->data[(i+n+n*j)])/2.0;
channel2[k1] = image_in->data[(i+n*j)];
channel3[k1] = (image_in->data[(i-1+n*j)]+image_in->data[(i+1+n*j)])/2.0;
k1++;
}
if ((j+yoffset)%2 ==0 && (i+xoffset)%2 ==1){ // gruenes Feld 2 blau oben
channel1[k1] = (image_in->data[(i-1+n*j)]+image_in->data[(i+1+n*j)])/2.0;
channel2[k1] = image_in->data[(i+n*j)];
channel3[k1] = (image_in->data[(i-n+n*j)]+image_in->data[(i+n+n*j)])/2.0;
k1++;;
}
if ((j+yoffset)%2 ==1 && (i+xoffset)%2 ==1){ // blaues Feld
channel1[k1] = (image_in->data[(i-n-1+n*j)] + image_in->data[(i-n+1+n*j)]+
image_in->data[(i+n-1+n*j)] + image_in->data[(i+n+1+n*j)]) /4.0;
channel2[k1]=(image_in->data[(i-1+n*j)]+ image_in->data[(i+1+n*j)]
+image_in->data[(i-n+n*j)] + image_in->data[(i+n+n*j)]) / 4.0;
channel3[k1] = image_in->data[(i+n*j)];
k1++;
}
}
}
}
}
// Gradienten Mode; refers to: grafics.cs.msa.ru/en/publications/text/gc2004lk.pdf (A. Lukin
// and D. Kubasov)
if (flag_q == 1){
printf("Using Gradient\n");
//qfits_header_add(header,"HISTORY","Interpolation: gradient","","");
for (j=0; j<m; j++) { //Zeilen
for (i=0; i<n; i++) { // Spalten
if(j==0||j>=m-1||i==0|| i>=n-1) { // Rand oben unten links rechts
/*channel1[k1]=image_in->data[(i+n*j)];
channel2[k1]=image_in->data[(i+n*j)];
channel3[k1]=image_in->data[(i+n*j)];*/
if(!xoffset && !yoffset){ // RGGB
if ((j+yoffset)%2 ==0 && (i+xoffset)%2 ==0){ // rotes Feld
channel1[k1] = image_in->data[(i+n*j)];
channel2[k1] = image_in->data[(i+1+n*j)];
channel3[k1] = image_in->data[(i+1+n+n*j)];
}
if ((j+yoffset)%2 ==1 && (i+xoffset)%2 ==0){ // gruenes Feld1, rot oben
channel1[k1] = image_in->data[(i-n+n*j)];
channel2[k1] = image_in->data[(i+n*j)];
channel3[k1] = image_in->data[(i+1+n*j)];
}
if ((j+yoffset)%2 ==0 && (i+xoffset)%2 ==1){ // gruenes Feld 2 blau oben
channel1[k1] = image_in->data[(i-1+n*j)];
channel2[k1] = image_in->data[(i+n*j)];
channel3[k1] = image_in->data[(i+n+n*j)];
}
if ((j+yoffset)%2 ==1 && (i+xoffset)%2 ==1){ // blaues Feld
channel1[k1] = image_in->data[(i-1-n+n*j)];
channel2[k1] = image_in->data[(i-1+n*j)];
channel3[k1] = image_in->data[(i+n*j)];
}
}
if(xoffset && !yoffset){ //GRBG
if ((j+yoffset)%2 ==0 && (i+xoffset)%2 ==0){ // rotes Feld
channel1[k1] = image_in->data[(i+n*j)];
channel2[k1] = image_in->data[(i-1+n*j)];
channel3[k1] = image_in->data[(i-1+n+n*j)];
}
if ((j+yoffset)%2 ==1 && (i+xoffset)%2 ==0){ // gruenes Feld1, rot oben
channel1[k1] = image_in->data[(i-n+n*j)];
channel2[k1] = image_in->data[(i+n*j)];
channel3[k1] = image_in->data[(i-1+n*j)];
}
if ((j+yoffset)%2 ==0 && (i+xoffset)%2 ==1){ // gruenes Feld 2 blau oben
channel1[k1] = image_in->data[(i+1+n*j)];
channel2[k1] = image_in->data[(i+n*j)];
channel3[k1] = image_in->data[(i+n+n*j)];
}
if ((j+yoffset)%2 ==1 && (i+xoffset)%2 ==1){ // blaues Feld
channel1[k1] = image_in->data[(i+1-n+n*j)];
channel2[k1] = image_in->data[(i+1+n*j)];
channel3[k1] = image_in->data[(i+n*j)];
}
}
if(!xoffset && yoffset){ //GBRG
if ((j+yoffset)%2 ==0 && (i+xoffset)%2 ==0){ // rotes Feld
channel1[k1] = image_in->data[(i+n*j)];
channel2[k1] = image_in->data[(i-n+n*j)];
channel3[k1] = image_in->data[(i+1-n+n*j)];
}
if ((j+yoffset)%2 ==1 && (i+xoffset)%2 ==0){ // gruenes Feld1, rot oben
channel1[k1] = image_in->data[(i+n+n*j)];
channel2[k1] = image_in->data[(i+n*j)];
channel3[k1] = image_in->data[(i+1+n*j)];
}
if ((j+yoffset)%2 ==0 && (i+xoffset)%2 ==1){ // gruenes Feld 2 blau oben
channel1[k1] = image_in->data[(i-1+n*j)];
channel2[k1] = image_in->data[(i+n*j)];
channel3[k1] = image_in->data[(i-n+n*j)];
}
if ((j+yoffset)%2 ==1 && (i+xoffset)%2 ==1){ // blaues Feld
channel1[k1] = image_in->data[(i-1+n+n*j)];
channel2[k1] = image_in->data[(i-1+n*j)];
channel3[k1] = image_in->data[(i+n*j)];
}
}
if(xoffset && yoffset){ //BGGR
if ((j+yoffset)%2 ==0 && (i+xoffset)%2 ==0){ // rotes Feld
channel1[k1] = image_in->data[(i+n*j)];
channel2[k1] = image_in->data[(i-1+n*j)];
channel3[k1] = image_in->data[(i-1-n+n*j)];
}
if ((j+yoffset)%2 ==1 && (i+xoffset)%2 ==0){ // gruenes Feld1, rot oben
channel1[k1] = image_in->data[(i+n+n*j)];
channel2[k1] = image_in->data[(i+n*j)];
channel3[k1] = image_in->data[(i-1+n*j)];
}
if ((j+yoffset)%2 ==0 && (i+xoffset)%2 ==1){ // gruenes Feld 2 blau oben
channel1[k1] = image_in->data[(i+1+n*j)];
channel2[k1] = image_in->data[(i+n*j)];
channel3[k1] = image_in->data[(i-n+n*j)];
}
if ((j+yoffset)%2 ==1 && (i+xoffset)%2 ==1){ // blaues Feld
channel1[k1] = image_in->data[(i+1+n+n*j)];
channel2[k1] = image_in->data[(i+1+n*j)];
channel3[k1] = image_in->data[(i+n*j)];
}
}
k1++;
}
else{
if ((j+yoffset)%2 ==0 && (i+xoffset)%2 ==0){ // rotes Feld
channel1[k1] = image_in->data[(i+n*j)];
// Gradients determing for green
H=abs(image_in->data[(i-1+n*j)] - image_in->data[(i+1+n*j)]);
V=abs(image_in->data[(i-n+n*j)] - image_in->data[(i+n+n*j)]);
if ( H>=V )
channel2[k1]=(image_in->data[(i-n+n*j)] + image_in->data[(i+n+n*j)]) / 2.0;
else
channel2[k1]=(image_in->data[(i-1+n*j)] + image_in->data[(i+1+n*j)]) / 2.0;
// Gradients determing for blue H=L (H=high to Right down)
H=abs(image_in->data[(i-n-1+n*j)] - image_in->data[(i+1+n*j)]);
V=abs(image_in->data[(i+n-1+n*j)] - image_in->data[(i-n+1+n*j)]);
if (H>=V)
channel3[k1] = (image_in->data[(i+n-1+n*j)] + image_in->data[(i-n+1+n*j)]) /2.0;
else
channel3[k1]=(image_in->data[(i-n-1+n*j)] + image_in->data[(i+n+1+n*j)]) /2.0;
k1++;
}
if ((j+yoffset)%2 ==1 && (i+xoffset)%2 ==0){ // gruenes Feld1, rot oben
channel1[k1] = (image_in->data[(i-n+n*j)]+image_in->data[(i+n+n*j)])/2.0;
channel2[k1] = image_in->data[(i+n*j)];
channel3[k1] = (image_in->data[(i-1+n*j)]+image_in->data[(i+1+n*j)])/2.0;
k1++;
}
if ((j+yoffset)%2 ==0 && (i+xoffset)%2 ==1){ // gruenes Feld 2 blau oben
channel1[k1] = (image_in->data[(i-1+n*j)]+image_in->data[(i+1+n*j)])/2.0;
channel2[k1] = image_in->data[(i+n*j)];
channel3[k1] = (image_in->data[(i-n+n*j)]+image_in->data[(i+n+n*j)])/2.0;
k1++;;
}
if ((j+yoffset)%2 ==1 && (i+xoffset)%2 ==1){ // blaues Feld
// Gradients determing for Red H=L (H=high to Right down)
H=abs(image_in->data[(i-n-1+n*j)] - image_in->data[(i+1+n*j)]);
V=abs(image_in->data[(i+n-1+n*j)] - image_in->data[(i-n+1+n*j)]);
if (H>=V)
channel1[k1] = (image_in->data[(i+n-1+n*j)] + image_in->data[(i-n+1+n*j)]) /2.0;
else
channel1[k1]=(image_in->data[(i-n-1+n*j)] + image_in->data[(i+n+1+n*j)]) /2.0;
// Gradients determing for green
H=abs(image_in->data[(i-1+n*j)] - image_in->data[(i+1+n*j)]);
V=abs(image_in->data[(i-n+n*j)] - image_in->data[(i+n+n*j)]);
if ( H>=V )
channel2[k1]=(image_in->data[(i-n+n*j)] + image_in->data[(i+n+n*j)]) / 2.0;
else
channel2[k1]=(image_in->data[(i-1+n*j)] + image_in->data[(i+1+n*j)]) / 2.0;
channel3[k1] = image_in->data[(i+n*j)];
k1++;
}
}
}
}
}
// PPG, web.cecs.pdx.edu/~cklin/demosaic/ (Chuan-Kai Lin)
if (flag_q == 2){
printf("Using Pixel Grouping\n");
//qfits_header_add(header,"HISTORY","Interpolation: PPG","","");
// Calculation the green values at red and blue pixels
for (j=0; j<m; j++) { //Zeilen
for (i=0; i<n; i++) { // Spalten
//if(j<=2||j>=m-2||i<=2|| i>=n-2) { // 3 Reihen Rand oben unten links rechts
if(j<=2||j>=m-3||i<=2|| i>=n-3) {
channel1[k1]=image_in->data[(i+n*j)]; // alle gleich gemacht mit aktueller Farbe
channel2[k1]=image_in->data[(i+n*j)];
channel3[k1]=image_in->data[(i+n*j)];
k1++;
}
else{
//Gradients calculation for green values at red or blue pixels
DN=abs(image_in->data[(i-2*n+n*j)]-image_in->data[(i+n*j)])*2.0+abs(image_in->data[(i-n+n*j)]-image_in->data[(i+n+n*j)]);
DE=abs(image_in->data[(i+n*j)]-image_in->data[(i+2+n*j)])*2.0+abs(image_in->data[(i-1+n*j)]-image_in->data[(i+1+n*j)]);
DW=abs(image_in->data[(i-2+n*j)]-image_in->data[(i+n*j)])*2.0+abs(image_in->data[(i-1+n*j)]-image_in->data[(i+1+n*j)]);
DS=abs(image_in->data[(i+n*j)]-image_in->data[(i+2*n+n*j)])*2.0+abs(image_in->data[(i-n+n*j)]-image_in->data[(i+n+n*j)]);
if ((j+yoffset)%2 ==0 && (i+xoffset)%2 ==0){ // rotes Feld
channel1[k1] = image_in->data[(i+n*j)];
switch (direction(DN,DE,DW,DS)){
case 1: channel2[k1]=(image_in->data[(i-n+n*j)]*3.0+image_in->data[(i+n*j)]
+image_in->data[(i+n+n*j)] - image_in->data[(i-2*n+n*j)]) / 4.0;break;
case 2: channel2[k1]=(image_in->data[(i+1+n*j)]*3.0+image_in->data[(i+n*j)]
+image_in->data[(i-1+n*j)] - image_in->data[(i+2+n*j)]) / 4.0;break;
case 3: channel2[k1]=(image_in->data[(i-1+n*j)]*3.0+image_in->data[(i+n*j)]
+image_in->data[(i+1+n*j)] - image_in->data[(i-2+n*j)]) / 4.0;break;