32 #define POSIX_SOURCE 1
33 #include "sinfo_vltPort.h"
43 #include "sinfo_detlin.h"
44 #include "sinfo_recipes.h"
45 #include "sinfo_fit_curve.h"
80 sinfo_new_fit_intensity_course(cpl_imagelist * flatStack,
85 cpl_imagelist * ret_iml ;
95 cpl_image* img_tmp=NULL;
96 sx=cpl_image_get_size_x(cpl_imagelist_get(flatStack,0));
97 sy=cpl_image_get_size_y(cpl_imagelist_get(flatStack,0));
98 sz=cpl_imagelist_get_size(flatStack);
100 stats=(Stats**) cpl_calloc(sz,
sizeof(Stats*)) ;
102 if ( NULL == flatStack )
115 ret_iml = cpl_imagelist_new();
116 for ( z = 0 ; z < order+1 ; z++ )
118 img_tmp=cpl_image_new(sx,sy,CPL_TYPE_FLOAT);
119 cpl_imagelist_set(ret_iml,img_tmp,z);
122 for ( z = 0 ; z < sz ; z++ )
125 sinfo_new_image_stats_on_rectangle(cpl_imagelist_get(flatStack,z),
132 if ( stats[z] == NULL )
136 cpl_imagelist_delete(ret_iml) ;
144 for ( i = 0 ; i < sx*sy ; i++ )
148 if ( NULL == ( points = (dpoint*) cpl_calloc(sz,
sizeof(dpoint)) ) )
151 cpl_imagelist_delete(ret_iml) ;
155 for ( z = 0 ; z < sz ; z++ )
157 if(NULL==(img_tmp = cpl_imagelist_get(flatStack,z))) {
159 cpl_imagelist_delete(ret_iml) ;
163 psrcdata=cpl_image_get_data_float(img_tmp);
164 points[z].x = (double)stats[z]->cleanmean ;
165 points[z].y = (double)psrcdata[i] ;
170 if ( NULL == ( coeffs = sinfo_fit_1d_poly(order, points, sz, NULL) ) )
173 for ( z = 0 ; z < order+1 ; z++ )
175 presdata=cpl_image_get_data_float(cpl_imagelist_get(ret_iml,z));
181 for ( z = 0 ; z < order+1 ; z++ )
183 if(NULL==(img_tmp = cpl_imagelist_get(ret_iml,z))) {
185 cpl_imagelist_delete(ret_iml) ;
188 presdata=cpl_image_get_data_float(img_tmp);
189 presdata[i] = coeffs[z] ;
197 for ( z = 0 ; z < sz ; z++ )
199 cpl_free (stats[z]) ;
229 cpl_image * sinfo_new_search_bad_pixels( cpl_imagelist * coeffs,
230 double threshSigmaFactor,
231 double nonlinearThresh,
241 cpl_image * img_res ;
242 cpl_image* img_src=NULL;
244 float* psrcdata=NULL;
245 float* presdata=NULL;
247 if ( NULL == coeffs )
252 if ( threshSigmaFactor <= 0. )
257 if ( nonlinearThresh <= 0. )
264 sz=cpl_imagelist_get_size(coeffs);
273 img_src=cpl_imagelist_get(coeffs,1);
274 sx=cpl_image_get_size_x(img_src);
275 sy=cpl_image_get_size_y(img_src);
278 if ( NULL == (img_res = cpl_image_new(sx, sy,CPL_TYPE_FLOAT)) )
289 stats = sinfo_new_image_stats_on_rectangle(img_src,
296 cpl_image_delete(img_res) ;
301 psrcdata=cpl_image_get_data_float(img_src);
302 presdata=cpl_image_get_data_float(img_res);
303 for ( i = 0 ; i < (int) sx*sy ; i++ )
306 if ( isnan(psrcdata[i]) )
310 else if ( stats->cleanmean - psrcdata[i] >
311 threshSigmaFactor*stats->cleanstdev )
332 for ( z = 2 ; z < sz ; z++ )
334 img_src=cpl_imagelist_get(coeffs,z);
335 sx=cpl_image_get_size_x(img_src);
336 sy=cpl_image_get_size_y(img_src);
338 psrcdata=cpl_image_get_data_float(img_src);
339 stats = sinfo_new_image_stats_on_rectangle(img_src, loReject,
340 hiReject, 0, 0, sx-1, sy-1) ;
344 cpl_image_delete(img_res) ;
347 presdata=cpl_image_get_data_float(img_res);
348 for ( i = 0 ; i < (int) sx*sy ; i++ )
350 if ( presdata[i] == 1. &&
351 (fabs(psrcdata[i] - stats->cleanmean) >
352 threshSigmaFactor*stats->cleanstdev ||
353 fabs(psrcdata[i]) > nonlinearThresh ) )
388 cpl_image * sinfo_new_search_bad_pixels_via_noise(cpl_imagelist * darks,
389 float threshSigmaFactor,
402 cpl_image* img_src=NULL;
404 float* psrcdata=NULL;
415 if ( threshSigmaFactor <= 0. )
420 if ( loReject < 0. || hiReject < 0. || (loReject + hiReject) >= 100. )
426 lz=cpl_imagelist_get_size(darks);
432 img_src=cpl_imagelist_get(darks,0);
434 lx = cpl_image_get_size_x(img_src) ;
435 ly = cpl_image_get_size_y(img_src) ;
437 low_n = (int)(loReject/100. *(
float)lz) ;
438 high_n = (int)(hiReject/100. *(
float)lz) ;
439 if (NULL == (bp_map = cpl_image_new (lx, ly,CPL_TYPE_FLOAT) ) )
444 pbpdata=cpl_image_get_data(bp_map);
445 if (NULL == (spectrum = (
float*) cpl_calloc(lz,
sizeof(
float)) ) )
450 for ( row = 0 ; row < ly ; row++ ) {
452 for ( col = 0 ; col < lx ; col++ ) {
454 for ( z = 0 ; z < lz ; z++ ) {
455 img_src=cpl_imagelist_get(darks,z);
456 psrcdata=cpl_image_get_data(img_src);
457 spectrum[z] = psrcdata[col+lx*row] ;
459 sinfo_pixel_qsort(spectrum, lz) ;
463 for ( i = low_n ; i < lz - high_n ; i++ ) {
464 pix_sum += (double)spectrum[i] ;
465 sqr_sum += ((double)spectrum[i]*(
double)spectrum[i]) ;
469 pix_sum /= (double)n ;
470 sqr_sum /= (double)n ;
472 pbpdata[col+lx*row] = (float)sqrt(sqr_sum - pix_sum*pix_sum) ;
476 if ( NULL == (stats = sinfo_new_image_stats_on_rectangle (bp_map, loReject,
477 hiReject, 200, 200, 800, 800) ) )
480 cpl_image_delete (bp_map) ;
486 for ( row = 0 ; row < ly ; row++ ) {
487 for ( col = 0 ; col < lx ; col++ ) {
488 if (pbpdata[col+lx*row] >
489 stats->cleanmean+threshSigmaFactor*stats->cleanstdev ||
490 pbpdata[col+lx*row] <
491 stats->cleanmean-threshSigmaFactor*stats->cleanstdev)
493 pbpdata[col+lx*row] = 0. ;
497 pbpdata[col+lx*row] = 1. ;
515 int sinfo_new_count_bad_pixels (cpl_image * bad )
518 int sx=cpl_image_get_size_x(bad);
519 int sy=cpl_image_get_size_y(bad);
520 float* pbpdata=cpl_image_get_data(bad);
523 for ( i = 0 ; i < (int) sx*sy ; i++ )
525 if ( pbpdata[i] == 0 || isnan(pbpdata[i]) )
561 cpl_image * sinfo_new_abs_dist_image(cpl_image * im,
float fmedian )
567 pixelvalue median_dist ;
568 pixelvalue* pix_dist=NULL ;
584 image = cpl_image_duplicate ( im ) ;
594 pdata = cpl_image_get_data(im);
595 lx=cpl_image_get_size_x(im);
596 ly=cpl_image_get_size_y(im);
597 pix_dist=(pixelvalue*)cpl_calloc(lx*ly,
sizeof(pixelvalue)) ;
599 for ( i = 0 ; i < (int) lx*ly ; i++ )
602 if ( isnan(pdata[i]) )
608 value = (pixelvalue * )cpl_calloc ( 8,
sizeof ( pixelvalue * ) ) ;
609 position = (
int * ) cpl_calloc ( 8,
sizeof (
int * ) ) ;
615 position[0] = i + lx - 1 ;
616 position[1] = i + lx ;
617 position[2] = i + lx + 1 ;
618 position[3] = i + 1 ;
619 position[4] = i - lx + 1 ;
620 position[5] = i - lx ;
621 position[6] = i - lx - 1 ;
622 position[7] = i - 1 ;
630 if ( i >= 0 && i < lx )
632 position[4] += 2 * lx ;
633 position[5] += 2 * lx ;
634 position[6] += 2 * lx ;
636 else if ( i >= ((
int) lx*ly - lx ) && i < (
int) lx*ly )
638 position[0] -= 2 * lx ;
639 position[1] -= 2 * lx ;
640 position[2] -= 2 * lx ;
642 else if ( i % lx == 0 )
648 else if ( i % lx == lx - 1 )
662 for ( j = 0 ; j < nposition ; j ++ )
664 if ( !isnan(pdata[position[j]]) )
666 value[n] = pdata[position[j]] ;
672 if ( nposition <= 1 )
682 for ( n = 0 ; n < nposition ; n++ )
684 dist += (pdata[i] - value[n])*(pdata[i] - value[n]) ;
686 dist = sqrt(dist)/(float) nposition ;
689 sum += (double)dist ;
690 sum2 += (double)dist * (
double)dist ;
696 stdev = sqrt(sum2 - sum*sum) ;
698 median_dist = sinfo_new_median(pix_dist, m) ;
700 for ( i = 0 ; i < (int) lx*ly ; i++ )
703 if ( isnan(pdata[i]) )
709 value = (pixelvalue * )cpl_calloc ( 8,
sizeof ( pixelvalue * ) ) ;
710 position = (
int * ) cpl_calloc ( 8,
sizeof (
int * ) ) ;
716 position[0] = i + lx - 1 ;
717 position[1] = i + lx ;
718 position[2] = i + lx + 1 ;
719 position[3] = i + 1 ;
720 position[4] = i - lx + 1 ;
721 position[5] = i - lx ;
722 position[6] = i - lx - 1 ;
723 position[7] = i - 1 ;
731 if ( i >= 0 && i < lx )
733 position[4] += 2 * lx ;
734 position[5] += 2 * lx ;
735 position[6] += 2 * lx ;
737 else if ( i >= ((
int) lx*ly - lx ) && i < (
int) lx*ly )
739 position[0] -= 2 * lx ;
740 position[1] -= 2 * lx ;
741 position[2] -= 2 * lx ;
743 else if ( i % lx == 0 )
749 else if ( i % lx == lx - 1 )
763 for ( j = 0 ; j < nposition ; j ++ )
765 if ( !isnan(pdata[position[j]]) )
767 value[n] = pdata[position[j]] ;
773 if ( nposition <= 1 )
783 for ( n = 0 ; n < nposition ; n++ )
785 dist += (pdata[i] - value[n])*(pdata[i] - value[n]) ;
787 dist = sqrt(dist)/(float) nposition ;
807 else if ( fmedian < 0 &&
808 fabs ( median_dist - dist ) >= -fmedian*stdev )
812 else if ( fmedian > 0 &&
813 fabs ( median_dist - dist ) >=
814 fmedian*stdev * sqrt(fabs(dist)) )
821 cpl_free (position) ;
826 cpl_free (position) ;
866 cpl_image * sinfo_new_local_median_image( cpl_image * im,
878 int llx, lly, urx, ury ;
890 if ( half_box_size < 0 )
896 image = cpl_image_duplicate ( im ) ;
897 lx=cpl_image_get_size_x(im);
898 ly=cpl_image_get_size_y(im);
899 pidata=cpl_image_get_data(im);
900 podata=cpl_image_get_data(image);
905 for ( i = 0 ; i < (int) lx*ly ; i++ )
908 if ( isnan(pidata[i]) )
914 llx = i%lx - half_box_size ;
915 if ( llx < 0 ) llx = 0 ;
916 lly = i%ly - half_box_size ;
917 if ( lly < 0 ) lly = 0 ;
918 urx = i%lx + half_box_size ;
919 if ( urx >= lx ) urx = lx - 1 ;
920 ury = i%ly + half_box_size ;
921 if ( ury >= ly ) ury = ly - 1 ;
923 if ( NULL == (stats = sinfo_new_image_stats_on_rectangle (im, loReject,
924 hiReject, llx, lly, urx, ury)) )
932 value = (pixelvalue * )cpl_calloc ( 8,
sizeof ( pixelvalue * ) ) ;
933 position = (
int * ) cpl_calloc ( 8,
sizeof (
int * ) ) ;
939 position[0] = i + lx - 1 ;
940 position[1] = i + lx ;
941 position[2] = i + lx + 1 ;
942 position[3] = i + 1 ;
943 position[4] = i - lx + 1 ;
944 position[5] = i - lx ;
945 position[6] = i - lx - 1 ;
946 position[7] = i - 1 ;
954 if ( i >= 0 && i < lx )
956 position[4] += 2 * lx ;
957 position[5] += 2 * lx ;
958 position[6] += 2 * lx ;
960 else if ( i >= ((
int) lx*ly - lx ) && i < (
int) lx*ly )
962 position[0] -= 2 * lx ;
963 position[1] -= 2 * lx ;
964 position[2] -= 2 * lx ;
966 else if ( i % lx == 0 )
972 else if ( i % lx == lx - 1 )
986 for ( j = 0 ; j < nposition ; j ++ )
988 if ( !isnan(pidata[position[j]]) )
990 value[n] = pidata[position[j]] ;
996 if ( nposition <= 1 )
1000 cpl_free(position) ;
1007 sinfo_pixel_qsort( value, nposition ) ;
1008 if ( nposition % 2 == 1 )
1010 median = value [ nposition/2 ] ;
1014 median = ( value [nposition/2 - 1] + value [nposition/2] ) / 2. ;
1029 podata[i] = median ;
1031 else if ( fmedian < 0 &&
1032 fabs ( median - pidata[i] ) >= -fmedian * stats->cleanstdev)
1034 podata[i] = median ;
1036 else if ( fmedian > 0 &&
1037 fabs ( median - pidata[i] ) >= fmedian * sqrt(fabs(median)) )
1039 podata[i] = median ;
1044 cpl_free (position) ;
1050 cpl_free (position) ;
1081 cpl_image * sinfo_new_mean_image_in_spec( cpl_image * im,
float fmedian )
1084 pixelvalue * value ;
1100 image = cpl_image_duplicate ( im ) ;
1101 lx=cpl_image_get_size_x(im);
1102 ly=cpl_image_get_size_y(im);
1103 pidata=cpl_image_get_data(im);
1104 podata=cpl_image_get_data(image);
1110 for ( i = 0 ; i < (int) lx*ly ; i++ )
1113 if ( isnan(pidata[i]) )
1120 value = (pixelvalue * )cpl_calloc ( 4,
sizeof ( pixelvalue * ) ) ;
1121 position = (
int * ) cpl_calloc ( 4,
sizeof (
int * ) ) ;
1127 position[0] = i + lx ;
1128 position[1] = i + 2*lx ;
1129 position[2] = i - lx ;
1130 position[3] = i - 2*lx ;
1138 if ( i >= 0 && i < lx )
1140 position[2] += 2 * lx ;
1141 position[3] += 4 * lx ;
1143 else if ( i >= ((
int) lx*ly - lx ) && i < (
int) lx*ly )
1145 position[0] -= 2 * lx ;
1146 position[1] -= 4 * lx ;
1156 for ( j = 0 ; j < nposition ; j ++ )
1158 if ( !isnan(pidata[position[j]]) )
1160 value[n] = pidata[position[j]] ;
1166 if ( nposition < 1 )
1170 cpl_free(position) ;
1176 for ( n = 0 ; n < nposition ; n++ )
1180 mean /= (float) nposition ;
1194 else if ( fmedian < 0 &&
1195 fabs ( mean - pidata[i] ) >= -fmedian )
1199 else if ( fmedian > 0 &&
1200 fabs ( mean - pidata[i] ) >= fmedian * sqrt(fabs(mean)) )
1207 cpl_free (position) ;
1212 cpl_free (position) ;
#define sinfo_msg_error(...)
Print an error message.
#define sinfo_msg_warning(...)
Print an warning message.