| 1 | #include "sopnamsp.h" | 
|---|
| 2 | #include "machdefs.h" | 
|---|
| 3 | #include <stdlib.h> | 
|---|
| 4 | #include <stdio.h> | 
|---|
| 5 |  | 
|---|
| 6 | #include "fmath.h" | 
|---|
| 7 | #include "srandgen.h" | 
|---|
| 8 | #include "perandom.h" | 
|---|
| 9 |  | 
|---|
| 10 | #include "cimage.h" | 
|---|
| 11 |  | 
|---|
| 12 | #include "dynccd.h" | 
|---|
| 13 |  | 
|---|
| 14 | /*! | 
|---|
| 15 | \class SOPHYA::DynCCD | 
|---|
| 16 | \ingroup NTools | 
|---|
| 17 |  | 
|---|
| 18 | Cette classe permet la specification des parametres | 
|---|
| 19 | definissant la dynamique du CCD, et doit etre utilise | 
|---|
| 20 | pour le calcul des images de bruit. | 
|---|
| 21 |  | 
|---|
| 22 | - TypNoise = 1 | 
|---|
| 23 | Bruit = Sqrt( (RONoise/Gain)**2 + ValPix/Gain ) | 
|---|
| 24 | - TypNoise = 2 : | 
|---|
| 25 | Bruit = Sqrt( RefSFond**2 + (ValPix-RefFond)/Gain ) | 
|---|
| 26 | - TypNoise = 0 | 
|---|
| 27 | Bruit = 1  (Constant pour tous les pixels) | 
|---|
| 28 | - TypNoise = 3 | 
|---|
| 29 | Bruit = Sqrt(Abs(ValPix)) | 
|---|
| 30 |  | 
|---|
| 31 | Les pixels hors dynamique sont marques (Bruit = 0) | 
|---|
| 32 | ( < MinADU ou > MaxADU) pour toutes valeurs de TypNoise. | 
|---|
| 33 | Gain est exprime en electron par ADU, RONoise en electron, | 
|---|
| 34 | Bruit, ValPix et RefFond en ADU. | 
|---|
| 35 |  | 
|---|
| 36 | \sa SOPHYA::Image | 
|---|
| 37 | */ | 
|---|
| 38 |  | 
|---|
| 39 |  | 
|---|
| 40 | /* ............................................................ */ | 
|---|
| 41 | /* :::::::::::::  methode de la classe  DynCCD :::::::::::::::: */ | 
|---|
| 42 | /* ............................................................ */ | 
|---|
| 43 |  | 
|---|
| 44 | /* --Methode-- */ | 
|---|
| 45 | //!  Creation d'un objet DynCCD ("typ" determine la methode de calcul du bruit) | 
|---|
| 46 | DynCCD::DynCCD(int_4 typ, r_8 min, r_8 max, r_8 g, | 
|---|
| 47 | r_8 ron, r_8 rf, r_8 rfs) | 
|---|
| 48 | { | 
|---|
| 49 | if ( (typ >= kConstantNoise) && (typ <= kSqrtADUNoise) )  TypNoise = typ; | 
|---|
| 50 | else  TypNoise = kConstantNoise; | 
|---|
| 51 | MinADU = min;  MaxADU = max; | 
|---|
| 52 | Gain = g;  RONoise = ron; | 
|---|
| 53 | RefFond = rf;  RefSFond = rfs; | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | /* --Methode-- */ | 
|---|
| 57 | //!     Modification des parametres de la dynamique | 
|---|
| 58 | void DynCCD::Set(int_4 typ, r_8 min, r_8 max, r_8 g, | 
|---|
| 59 | r_8 ron, r_8 rf, r_8 rfs) | 
|---|
| 60 | { | 
|---|
| 61 | if ( (typ >= kConstantNoise) && (typ <= kSqrtADUNoise) )  TypNoise = typ; | 
|---|
| 62 | MinADU = min;  MaxADU = max; | 
|---|
| 63 | Gain = g;  RONoise = ron; | 
|---|
| 64 | RefFond = rf;  RefSFond = rfs; | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | /* --Methode-- */ | 
|---|
| 68 | void DynCCD::Print() | 
|---|
| 69 | { | 
|---|
| 70 | printf("DynCCD: Type= %d Min/MaxADU= %g %g Gain/RoN= %g %g\n", | 
|---|
| 71 | TypNoise, MinADU, MaxADU, Gain, RONoise); | 
|---|
| 72 | if (TypNoise == 2) | 
|---|
| 73 | printf("... RefFond= %g  RefSFond= %g \n", RefFond, RefSFond); | 
|---|
| 74 | return; | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 | //!     Renvoie la valeur du bruit pour "pixel" en ADU. | 
|---|
| 78 | /*! | 
|---|
| 79 | Cette fonction calcule la valeur du bruit pour pixel | 
|---|
| 80 | - Si TypNoise = 1 : | 
|---|
| 81 | Bruit = Sqrt( (RONoise/Gain)**2 + ValPix/Gain ) | 
|---|
| 82 | - Si TypNoise = 2 : | 
|---|
| 83 | Bruit = Sqrt( RefSFond**2 + (ValPix-RefFond)/Gain ) | 
|---|
| 84 | - Si TypNoise = 0 | 
|---|
| 85 | Bruit = 1  (Constant pour tous les pixels) | 
|---|
| 86 | - Si TypNoise = 3 | 
|---|
| 87 | Bruit = Sqrt(Abs(PixelADU)) | 
|---|
| 88 |  | 
|---|
| 89 | Les pixels hors dynamique sont marques (Bruit = 0) | 
|---|
| 90 | ( < MinADU ou > MaxADU) pour tout valeur de TypNoise | 
|---|
| 91 | */ | 
|---|
| 92 | /* --Methode-- */ | 
|---|
| 93 | r_8 DynCCD::Noise(r_8 pixel) const | 
|---|
| 94 | { | 
|---|
| 95 | r_8 h,s,ronsq; | 
|---|
| 96 | r_8 fond; | 
|---|
| 97 |  | 
|---|
| 98 | if ( (pixel > MaxADU) || (pixel < MinADU) )   return(0.); | 
|---|
| 99 | if ( TypNoise == kConstantNoise)  return(1.); | 
|---|
| 100 | if ( TypNoise == kSqrtADUNoise )  return(sqrt(fabs(pixel))); | 
|---|
| 101 |  | 
|---|
| 102 | if ( TypNoise == kSigFondNoise) | 
|---|
| 103 | { fond = RefFond; | 
|---|
| 104 | ronsq = RefSFond * RefSFond; } | 
|---|
| 105 | else | 
|---|
| 106 | { fond = 0; | 
|---|
| 107 | ronsq = RONoise/Gain;  ronsq *= ronsq; } | 
|---|
| 108 |  | 
|---|
| 109 | h = (pixel>fond) ? (r_8)(pixel-fond) : 0.; | 
|---|
| 110 | s = ronsq+h/Gain; | 
|---|
| 111 | s = sqrt(s); | 
|---|
| 112 | return(s); | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 | /* --------------------------------------------------------------  */ | 
|---|
| 116 | /*      Quelques fonctions pour manipuler des images de bruit      */ | 
|---|
| 117 | /* --------------------------------------------------------------  */ | 
|---|
| 118 |  | 
|---|
| 119 |  | 
|---|
| 120 | /* Nouvelle-Fonction */ | 
|---|
| 121 | /*! | 
|---|
| 122 | Construit et renvoie l'image du bruit pour l'image "*pim" | 
|---|
| 123 |  | 
|---|
| 124 | Creation et Calcul d'une image de bruit a partir de l'image | 
|---|
| 125 | pim et de dynccd. Voir la methode DynCCD::Noise() pour la | 
|---|
| 126 | description du calcul | 
|---|
| 127 | */ | 
|---|
| 128 | template <class T> | 
|---|
| 129 | Image<T> NoiseImage(Image<T> const & pim, DynCCD const & dynccd) | 
|---|
| 130 |  | 
|---|
| 131 | { | 
|---|
| 132 | r_8 h,s,ronsq; | 
|---|
| 133 | r_8 fond, min,max; | 
|---|
| 134 | int_4 k, npix; | 
|---|
| 135 | r_8 minois, offnois; | 
|---|
| 136 |  | 
|---|
| 137 | npix = pim.XSize()*pim.YSize(); | 
|---|
| 138 | Image<T> nois(pim.XSize(), pim.YSize()); | 
|---|
| 139 | nois.SetOrg(pim.XOrg(), pim.YOrg()); | 
|---|
| 140 |  | 
|---|
| 141 | min = dynccd.MinADU;   max = dynccd.MaxADU; | 
|---|
| 142 | for(k=0; k<npix; k++)      { | 
|---|
| 143 | if ( (pim[k] <= max) && (pim[k] >= min) )  nois[k] = (T)1; | 
|---|
| 144 | else nois[k] = 0; | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 |  | 
|---|
| 148 | return(nois); | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 |  | 
|---|
| 152 | /* Nouvelle-Fonction */ | 
|---|
| 153 | //!     Calcule l'image du bruit et le rajoute a l'image originale | 
|---|
| 154 | template <class T> | 
|---|
| 155 | void ImgAddNoise(Image<T>& img, DynCCD const& dyn) | 
|---|
| 156 |  | 
|---|
| 157 | { | 
|---|
| 158 | int_4 nPix = img.XSize() * img.YSize(); | 
|---|
| 159 |  | 
|---|
| 160 | for (int_4 i=0; i<nPix; i++) | 
|---|
| 161 | img[i] += (T) (dyn.Noise(img[i])*NorRand()); | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 |  | 
|---|
| 165 |  | 
|---|
| 166 |  | 
|---|
| 167 |  | 
|---|
| 168 | #ifdef __CXX_PRAGMA_TEMPLATES__ | 
|---|
| 169 | #pragma define_template NoiseImage<uint_2> | 
|---|
| 170 | #pragma define_template NoiseImage<int_4> | 
|---|
| 171 | #pragma define_template NoiseImage<r_4> | 
|---|
| 172 |  | 
|---|
| 173 | #pragma define_template ImgAddNoise<uint_2> | 
|---|
| 174 | #pragma define_template ImgAddNoise<int_4> | 
|---|
| 175 | #pragma define_template ImgAddNoise<r_4> | 
|---|
| 176 | #endif | 
|---|
| 177 |  | 
|---|
| 178 | #if defined(ANSI_TEMPLATES) || defined(__GNU_TEMPLATES__) | 
|---|
| 179 | #if defined( __IBMCPP__ ) | 
|---|
| 180 | namespace SOPHYA { | 
|---|
| 181 | #endif | 
|---|
| 182 | template Image<uint_2> NoiseImage<uint_2>(Image<uint_2> const& , DynCCD const&); | 
|---|
| 183 | template Image< int_4> NoiseImage< int_4>(Image< int_4> const& , DynCCD const&); | 
|---|
| 184 | template Image<   r_4> NoiseImage<   r_4>(Image<   r_4> const& , DynCCD const&); | 
|---|
| 185 |  | 
|---|
| 186 | template void ImgAddNoise<uint_2>(Image<uint_2>&, DynCCD const&); | 
|---|
| 187 | template void ImgAddNoise< int_4>(Image< int_4>&, DynCCD const&); | 
|---|
| 188 | template void ImgAddNoise<   r_4>(Image<   r_4>&, DynCCD const&); | 
|---|
| 189 | #if defined( __IBMCPP__ ) | 
|---|
| 190 | } | 
|---|
| 191 | #endif | 
|---|
| 192 |  | 
|---|
| 193 | #endif | 
|---|