source: Sophya/trunk/SophyaLib/NTools/dynccd.cc@ 2631

Last change on this file since 2631 was 2615, checked in by cmv, 21 years ago

using namespace sophya enleve de machdefs.h, nouveau sopnamsp.h cmv 10/09/2004

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