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