1 | // Classe image generique
|
---|
2 | // E.Aubourg , E. Lesquoy 01-03/95
|
---|
3 | // Modifs R. Ansari 04/95
|
---|
4 |
|
---|
5 | // LAL (Orsay) / IN2P3-CNRS DAPNIA/SPP (Saclay) / CEA
|
---|
6 |
|
---|
7 | // $Id: rzimage.cc,v 1.6 1999-10-25 16:40:03 ansari Exp $
|
---|
8 |
|
---|
9 | #include "machdefs.h"
|
---|
10 | #include <stdlib.h>
|
---|
11 | #include <stdio.h>
|
---|
12 | #include <string.h>
|
---|
13 | #include "rzimage.h"
|
---|
14 | #include "dvlist.h"
|
---|
15 | #include "generalfit.h"
|
---|
16 |
|
---|
17 |
|
---|
18 | /* ......... Methodes de la classe RzVect ......... */
|
---|
19 | //++
|
---|
20 | // Class RzVect<T>
|
---|
21 | // Lib Images++
|
---|
22 | // include rzvect.h
|
---|
23 | //
|
---|
24 | // Cette classe permet de construire des vecteurs de types T
|
---|
25 | // "(short, int, float, double, ...)". Cette classe est utilisee
|
---|
26 | // par la classe *RzImage*.
|
---|
27 | //--
|
---|
28 |
|
---|
29 | //++
|
---|
30 | // Titre Constructeurs
|
---|
31 | //--
|
---|
32 | //++
|
---|
33 | // RzVect<T>(int n)
|
---|
34 | // Constructeur avec allocation d'espace pour "n" elements
|
---|
35 | // "RzVect<uint_1>, RzVect<uint_2>, RzVect<int_2>, RzVect<int_4>"
|
---|
36 | // " RzVect<r_4>, RzVect<r_8>"
|
---|
37 | // RzVect<T>(int n, T* data)
|
---|
38 | // Constructeur avec specification du tableau de stockage des elements
|
---|
39 | // *Attention* "data" doit etre alloue a la bonne taille ("n")
|
---|
40 | // ~RzVect<T>()
|
---|
41 | // Destructeur - Libere l'espace de stockage des elements si l'objet a
|
---|
42 | // ete construit par le premier constructeur
|
---|
43 | //
|
---|
44 | //--
|
---|
45 |
|
---|
46 | /* --Methode-- */
|
---|
47 | template <class T>
|
---|
48 | RzVect<T>::RzVect(int n)
|
---|
49 | : p(new T[n]), nElt(n), nImgRef(1), shareable(1), ownTable(1)
|
---|
50 | {END_CONSTRUCTOR}
|
---|
51 |
|
---|
52 | /* --Methode-- */
|
---|
53 | template <class T>
|
---|
54 | RzVect<T>::RzVect(int n, T* data)
|
---|
55 | : p(data), nElt(n), nImgRef(0), shareable(0), ownTable(0)
|
---|
56 | {END_CONSTRUCTOR}
|
---|
57 |
|
---|
58 |
|
---|
59 | /* --Methode-- */
|
---|
60 | template <class T>
|
---|
61 | RzVect<T>::~RzVect()
|
---|
62 | {
|
---|
63 | if (ownTable) delete[] p;
|
---|
64 | #ifdef DEBUG
|
---|
65 | p = (T*) -1;
|
---|
66 | #endif
|
---|
67 | }
|
---|
68 |
|
---|
69 | //++
|
---|
70 | // Titre Acces aux elements
|
---|
71 | //--
|
---|
72 | //++
|
---|
73 | // T& operator[](int i)
|
---|
74 | // Acces a l'element "i" (lecture/ecriture)
|
---|
75 | // int IElem(int k)
|
---|
76 | // Valeur converti en entier "(int)" de l'element "k" (lecture)
|
---|
77 | // float FElem(int k)
|
---|
78 | // Valeur converti en reel "(float)" de l'element "k" (lecture)
|
---|
79 | // double DElem(int k)
|
---|
80 | // Valeur converti en reel "(double)" de l'element "k" (lecture)
|
---|
81 | //--
|
---|
82 |
|
---|
83 | /* .............. Classe RzImage ............... */
|
---|
84 |
|
---|
85 | //++
|
---|
86 | // Class RzImage
|
---|
87 | // Lib Images++
|
---|
88 | // include rzimage.h
|
---|
89 | //
|
---|
90 | // Cette classe permet de gerer les donnees correspondantes a une image.
|
---|
91 | // Une image est un tableau rectangulaire de valeurs de pixels.
|
---|
92 | // *RzImage* offre la possibilite de creer des images de valeurs de
|
---|
93 | // pixels entier ou flottant "(unsigned char uint_1, short int_2,"
|
---|
94 | // "unsigned short uint_2, int int_4, float r_4, double r_8)"
|
---|
95 | // les objets de la classe RzImage sont faiblement types. La classe
|
---|
96 | // *Image<T>* qui derive de RzImage permet la creation d'objets
|
---|
97 | // types. Chaque image peut contenir un objet *DVList* permettant
|
---|
98 | // a l'utilisateur d'associer un ensemble de valeurs a l'image.
|
---|
99 | //--
|
---|
100 | //++
|
---|
101 | // Links Parents
|
---|
102 | // PPersist
|
---|
103 | //--
|
---|
104 | //++
|
---|
105 | // Links Descendants
|
---|
106 | // Image<T>
|
---|
107 | // FitsImage<T>
|
---|
108 | //--
|
---|
109 | //++
|
---|
110 | // Links Voir aussi
|
---|
111 | // DVList
|
---|
112 | //--
|
---|
113 |
|
---|
114 | //++
|
---|
115 | // Titre Constructeurs
|
---|
116 | //--
|
---|
117 | //++
|
---|
118 | // RzImage()
|
---|
119 | // Creation d'une image vide, non typee.
|
---|
120 | // RzImage(PBaseDataTypes dType, int sizx, int sizy, -
|
---|
121 | // int imgId = 0, char const * imgName = 0)
|
---|
122 | // Creation d'une image de taille "sizx * sizy" pixels , de type "dType".
|
---|
123 | // (dType = kuint_1, kuint_2, kint_2, kint_4, kr_4, kr_8).
|
---|
124 | // "imgId" et "imgName" permettent d'indiquer une identification optionnelle.
|
---|
125 | // RzImage(const RzImage&, int sharePixels=0)
|
---|
126 | // Constructeur par copie. Permet de partager le tableau des pixels (RzVect)
|
---|
127 | // si "sharePixels" non nul (<> 0)
|
---|
128 | // RzImage(char *flnm)
|
---|
129 | // Les RzImage etant des objets persistants (*PPersist*), ils peuvent etre
|
---|
130 | // instancies a partir d'un fichier (nom = "flnm") ecrit par la methode Write()
|
---|
131 | // ~RzImage()
|
---|
132 | // Destructeur.
|
---|
133 | // operator= (RzImage const &)
|
---|
134 | // Copie les valeurs de pixels d'une image a l'autre.
|
---|
135 | // Si la 1ere image a son propre tableau de pixel, il faut que
|
---|
136 | // les deux images aient le meme type de pixels et la meme taille.
|
---|
137 | // Sinon, une exception ("typeMismatchErr" ou "sizeMismatchErr") est
|
---|
138 | // generee.
|
---|
139 | //--
|
---|
140 |
|
---|
141 | /* --Methode-- */
|
---|
142 | RzImage::RzImage(PBaseDataTypes dType)
|
---|
143 | : dataType(dType)
|
---|
144 | {
|
---|
145 | voidP = NULL;
|
---|
146 | isFits = 0;
|
---|
147 | siz_x = siz_y = 0;
|
---|
148 | vect.u2 = 0;
|
---|
149 | mInfo = NULL;
|
---|
150 | SetNameId(0, "");
|
---|
151 | SetOrg(0, 0);
|
---|
152 | SetPxSize();
|
---|
153 | SetAtt();
|
---|
154 | END_CONSTRUCTOR
|
---|
155 | }
|
---|
156 |
|
---|
157 | /* --Methode-- */
|
---|
158 | RzImage::RzImage(PBaseDataTypes dType, int sizx, int sizy, int imgId, char const* imgName)
|
---|
159 | : dataType(dType)
|
---|
160 | {
|
---|
161 | isFits = 0;
|
---|
162 | siz_x = siz_y = 0;
|
---|
163 | vect.u2 = 0;
|
---|
164 | voidP = NULL;
|
---|
165 | mInfo = NULL;
|
---|
166 | Allocate(dType, sizx, sizy);
|
---|
167 | SetNameId(imgId, imgName);
|
---|
168 | SetOrg(0, 0);
|
---|
169 | SetPxSize();
|
---|
170 | SetAtt();
|
---|
171 | END_CONSTRUCTOR
|
---|
172 | }
|
---|
173 |
|
---|
174 | /* --Methode-- */
|
---|
175 | RzImage::RzImage(const RzImage& src, int sharePixels)
|
---|
176 | : dataType(kpbdt_unknown)
|
---|
177 | {
|
---|
178 | isFits = 0;
|
---|
179 | siz_x = siz_y = 0;
|
---|
180 | vect.u2 = 0;
|
---|
181 | voidP = NULL;
|
---|
182 | mInfo = NULL;
|
---|
183 | if (sharePixels) {
|
---|
184 | ImgVectP vs = src.vect;
|
---|
185 | Allocate(src.PixelType(), src.XSize(), src.YSize(), &vs);
|
---|
186 | }
|
---|
187 | else {
|
---|
188 | Allocate(src.PixelType(), src.XSize(), src.YSize(), NULL);
|
---|
189 | memcpy(voidP, src.voidP, siz_x*siz_y*DataSize(dataType));
|
---|
190 | }
|
---|
191 |
|
---|
192 | SetNameId(src.id, src.name);
|
---|
193 | SetOrg(src.XOrg(), src.YOrg());
|
---|
194 | SetPxSize(src.XPxSize(), src.YPxSize());
|
---|
195 | SetAtt(src.nbNul, src.nbSat,
|
---|
196 | src.minPix, src.maxPix, src.moyPix, src.sigPix,
|
---|
197 | src.fond, src.sigmaFond);
|
---|
198 | END_CONSTRUCTOR
|
---|
199 | }
|
---|
200 |
|
---|
201 | /* --Methode-- */
|
---|
202 | RzImage::RzImage(char *flnm)
|
---|
203 | : dataType(kpbdt_unknown)
|
---|
204 | {
|
---|
205 | isFits = 0;
|
---|
206 | vect.u2 = 0;
|
---|
207 | voidP = NULL;
|
---|
208 | mInfo = NULL;
|
---|
209 | PInPersist s(flnm);
|
---|
210 | Read(s);
|
---|
211 | END_CONSTRUCTOR
|
---|
212 | }
|
---|
213 |
|
---|
214 | /* --Methode-- */
|
---|
215 | RzImage::~RzImage()
|
---|
216 | {
|
---|
217 | if (mInfo) delete mInfo;
|
---|
218 | DeAllocPixels();
|
---|
219 | }
|
---|
220 |
|
---|
221 | /* --Methode-- */
|
---|
222 | RzImage& RzImage::operator = (const RzImage& src)
|
---|
223 | {
|
---|
224 |
|
---|
225 | if (PixelType() >= 0)
|
---|
226 | {
|
---|
227 | if (src.PixelType() != PixelType())
|
---|
228 | THROW(typeMismatchErr);
|
---|
229 | if (siz_x != src.siz_x || siz_y != src.siz_y)
|
---|
230 | THROW(sizeMismatchErr);
|
---|
231 | }
|
---|
232 | else Allocate(src.PixelType(), src.siz_x, src.siz_y);
|
---|
233 |
|
---|
234 | memcpy(voidP, src.voidP, siz_x*siz_y*DataSize(dataType));
|
---|
235 | SetNameId(src.id, src.name);
|
---|
236 | SetOrg(src.XOrg(), src.YOrg());
|
---|
237 | SetPxSize(src.XPxSize(), src.YPxSize());
|
---|
238 | SetAtt(src.nbNul, src.nbSat,
|
---|
239 | src.minPix, src.maxPix, src.moyPix, src.sigPix,
|
---|
240 | src.fond, src.sigmaFond);
|
---|
241 |
|
---|
242 | return(*this);
|
---|
243 | }
|
---|
244 |
|
---|
245 | /* --Methode-- */
|
---|
246 | void RzImage::Allocate(PBaseDataTypes dType, int sizx, int sizy, ImgVectP* pvpsh)
|
---|
247 | // ATTENTION : Si pvpsh non nul, on partage les pixels
|
---|
248 | {
|
---|
249 | DeAllocPixels(); // EA 090998 -- memory leak...
|
---|
250 | siz_x = sizx;
|
---|
251 | siz_y = sizy;
|
---|
252 | if ((int)dataType<=0) dataType = dType; // Sinon ERREUR. Exception?
|
---|
253 | switch (dataType) {
|
---|
254 | case kuint_1:
|
---|
255 | vect.u1 = NULL;
|
---|
256 | if (pvpsh)
|
---|
257 | if (pvpsh->u1)
|
---|
258 | { ASSERT(pvpsh->u1->Shareable());
|
---|
259 | vect.u1 = pvpsh->u1; vect.u1->NImgRef()++; }
|
---|
260 | if (!vect.u1) vect.u1 = new RzVect<uint_1> (siz_x * siz_y);
|
---|
261 | voidP = vect.u1->p;
|
---|
262 | IVal_p = &RzImage::IVal_u1;
|
---|
263 | FVal_p = &RzImage::FVal_u1;
|
---|
264 | DVal_p = &RzImage::DVal_u1;
|
---|
265 | break;
|
---|
266 | case kuint_2:
|
---|
267 | vect.u2 = NULL;
|
---|
268 | if (pvpsh)
|
---|
269 | if (pvpsh->u2)
|
---|
270 | { ASSERT(pvpsh->u2->Shareable());
|
---|
271 | vect.u2 = pvpsh->u2; vect.u2->NImgRef()++; }
|
---|
272 | if (!vect.u2) vect.u2 = new RzVect<uint_2> (siz_x * siz_y);
|
---|
273 | voidP = vect.u2->p;
|
---|
274 | IVal_p = &RzImage::IVal_u2;
|
---|
275 | FVal_p = &RzImage::FVal_u2;
|
---|
276 | DVal_p = &RzImage::DVal_u2;
|
---|
277 | break;
|
---|
278 | case kint_2:
|
---|
279 | vect.i2 = NULL;
|
---|
280 | if (pvpsh)
|
---|
281 | if (pvpsh->i2)
|
---|
282 | { ASSERT(pvpsh->i2->Shareable());
|
---|
283 | vect.i2 = pvpsh->i2; vect.i2->NImgRef()++; }
|
---|
284 | if (!vect.i2) vect.i2 = new RzVect<int_2> (siz_x * siz_y);
|
---|
285 | voidP = vect.i2->p;
|
---|
286 | IVal_p = &RzImage::IVal_i2;
|
---|
287 | FVal_p = &RzImage::FVal_i2;
|
---|
288 | DVal_p = &RzImage::DVal_i2;
|
---|
289 | break;
|
---|
290 | case kint_4:
|
---|
291 | vect.i4 = NULL;
|
---|
292 | if (pvpsh)
|
---|
293 | if (pvpsh->i4)
|
---|
294 | { ASSERT(pvpsh->i4->Shareable());
|
---|
295 | vect.i4 = pvpsh->i4; vect.i4->NImgRef()++; }
|
---|
296 | if (!vect.i4) vect.i4 = new RzVect<int_4> (siz_x * siz_y);
|
---|
297 | voidP = vect.i4->p;
|
---|
298 | IVal_p = &RzImage::IVal_i4;
|
---|
299 | FVal_p = &RzImage::FVal_i4;
|
---|
300 | DVal_p = &RzImage::DVal_i4;
|
---|
301 | break;
|
---|
302 | case kr_4:
|
---|
303 | vect.r4 = NULL;
|
---|
304 | if (pvpsh)
|
---|
305 | if (pvpsh->r4)
|
---|
306 | { ASSERT(pvpsh->r4->Shareable());
|
---|
307 | vect.r4 = pvpsh->r4; vect.r4->NImgRef()++; }
|
---|
308 | if (!vect.r4) vect.r4 = new RzVect<r_4> (siz_x * siz_y);
|
---|
309 | voidP = vect.r4->p;
|
---|
310 | IVal_p = &RzImage::IVal_r4;
|
---|
311 | FVal_p = &RzImage::FVal_r4;
|
---|
312 | DVal_p = &RzImage::DVal_r4;
|
---|
313 | break;
|
---|
314 | case kr_8:
|
---|
315 | vect.r8 = NULL;
|
---|
316 | if (pvpsh)
|
---|
317 | if (pvpsh->r8)
|
---|
318 | { ASSERT(pvpsh->r8->Shareable());
|
---|
319 | vect.r8 = pvpsh->r8; vect.r8->NImgRef()++; }
|
---|
320 | if (!vect.r8) vect.r8 = new RzVect<r_8> (siz_x * siz_y);
|
---|
321 | voidP = vect.r8->p;
|
---|
322 | IVal_p = &RzImage::IVal_r8;
|
---|
323 | FVal_p = &RzImage::FVal_r8;
|
---|
324 | DVal_p = &RzImage::DVal_r8;
|
---|
325 | break;
|
---|
326 | default:
|
---|
327 | printf("RzImage::Allocate Error - Unsupported DataType= %d \n", (int)dataType);
|
---|
328 | break;
|
---|
329 | }
|
---|
330 | }
|
---|
331 |
|
---|
332 | /* --Methode-- */
|
---|
333 | void RzImage::SetPixels(PBaseDataTypes dType, int sizx, int sizy, void* data)
|
---|
334 | {
|
---|
335 | if (voidP != data) DeAllocPixels();
|
---|
336 | siz_x = sizx;
|
---|
337 | siz_y = sizy;
|
---|
338 | // if (dataType<0) (int&) dataType = (int)dType; // Sinon ERREUR. Exception?
|
---|
339 | if (dataType<0) dataType = dType; // Sinon ERREUR. Exception?
|
---|
340 | switch (dataType) {
|
---|
341 | case kuint_1:
|
---|
342 | vect.u1 = new RzVect<uint_1> (siz_x * siz_y, (uint_1*)data);
|
---|
343 | voidP = vect.u1->p;
|
---|
344 | IVal_p = &RzImage::IVal_u1;
|
---|
345 | FVal_p = &RzImage::FVal_u1;
|
---|
346 | DVal_p = &RzImage::DVal_u1;
|
---|
347 | break;
|
---|
348 | case kuint_2:
|
---|
349 | vect.u2 = new RzVect<uint_2> (siz_x * siz_y, (uint_2*)data);
|
---|
350 | voidP = vect.u2->p;
|
---|
351 | IVal_p = &RzImage::IVal_u2;
|
---|
352 | FVal_p = &RzImage::FVal_u2;
|
---|
353 | DVal_p = &RzImage::DVal_u2;
|
---|
354 | break;
|
---|
355 | case kint_2:
|
---|
356 | vect.i2 = new RzVect<int_2> (siz_x * siz_y, (int_2*)data);
|
---|
357 | voidP = vect.i2->p;
|
---|
358 | IVal_p = &RzImage::IVal_i2;
|
---|
359 | FVal_p = &RzImage::FVal_i2;
|
---|
360 | DVal_p = &RzImage::DVal_i2;
|
---|
361 | break;
|
---|
362 | case kint_4:
|
---|
363 | vect.i4 = new RzVect<int_4> (siz_x * siz_y, (int_4*)data);
|
---|
364 | voidP = vect.i4->p;
|
---|
365 | IVal_p = &RzImage::IVal_i4;
|
---|
366 | FVal_p = &RzImage::FVal_i4;
|
---|
367 | DVal_p = &RzImage::DVal_i4;
|
---|
368 | break;
|
---|
369 | case kr_4:
|
---|
370 | vect.r4 = new RzVect<r_4> (siz_x * siz_y, (r_4*)data);
|
---|
371 | voidP = vect.r4->p;
|
---|
372 | IVal_p = &RzImage::IVal_r4;
|
---|
373 | FVal_p = &RzImage::FVal_r4;
|
---|
374 | DVal_p = &RzImage::DVal_r4;
|
---|
375 | break;
|
---|
376 | case kr_8:
|
---|
377 | vect.r8 = new RzVect<r_8> (siz_x * siz_y, (r_8*)data);
|
---|
378 | voidP = vect.r8->p;
|
---|
379 | IVal_p = &RzImage::IVal_r8;
|
---|
380 | FVal_p = &RzImage::FVal_r8;
|
---|
381 | DVal_p = &RzImage::DVal_r8;
|
---|
382 | break;
|
---|
383 | default:
|
---|
384 | printf("RzImage::SetPixels Error - Unsupported DataType= %d \n", (int)dataType);
|
---|
385 | break;
|
---|
386 | }
|
---|
387 | }
|
---|
388 |
|
---|
389 | /* --Methode-- */
|
---|
390 | void RzImage::DeAllocPixels()
|
---|
391 | {
|
---|
392 | if (!voidP) return;
|
---|
393 | switch (dataType) {
|
---|
394 | case kuint_1:
|
---|
395 | if (! (--vect.u1->NImgRef()))
|
---|
396 | delete vect.u1;
|
---|
397 | vect.u1 = NULL;
|
---|
398 | break;
|
---|
399 | case kuint_2:
|
---|
400 | if (! (--vect.u2->NImgRef()))
|
---|
401 | delete vect.u2;
|
---|
402 | vect.u2 = NULL;
|
---|
403 | break;
|
---|
404 | case kint_2:
|
---|
405 | if (! (--vect.i2->NImgRef()))
|
---|
406 | delete vect.i2;
|
---|
407 | vect.i2 = NULL;
|
---|
408 | break;
|
---|
409 | case kint_4:
|
---|
410 | if (! (--vect.i4->NImgRef()))
|
---|
411 | delete vect.i4;
|
---|
412 | vect.i4 = NULL;
|
---|
413 | break;
|
---|
414 | case kr_4:
|
---|
415 | if (! (--vect.r4->NImgRef()))
|
---|
416 | delete vect.r4;
|
---|
417 | vect.r4 = NULL;
|
---|
418 | break;
|
---|
419 | case kr_8:
|
---|
420 | if (! (--vect.r8->NImgRef()))
|
---|
421 | delete vect.r8;
|
---|
422 | vect.r8 = NULL;
|
---|
423 | break;
|
---|
424 | default:
|
---|
425 | printf("RzImage::DeAllocPixels Error - Unsupported DataType= %d \n", (int)dataType);
|
---|
426 | break;
|
---|
427 | }
|
---|
428 |
|
---|
429 | voidP = NULL;
|
---|
430 | siz_x = siz_y = 0;
|
---|
431 | vect.u2 = NULL;
|
---|
432 | }
|
---|
433 |
|
---|
434 | //++
|
---|
435 | // Titre Informations globales
|
---|
436 | //--
|
---|
437 | //++
|
---|
438 | // int_4 XSize(), YSize()
|
---|
439 | // Taille de l'image en X, Y
|
---|
440 | // int_4 XOrg(), YOrg()
|
---|
441 | // Origines (X,Y) de l'image. Cette origine permet en particulier
|
---|
442 | // de conserver l'information de decalage, quand l'image est
|
---|
443 | // extraite d'une image plus grande. Les valeurs de l'origine
|
---|
444 | // peuvent etre modifiees par la methode "SetOrg()"
|
---|
445 | // void SetOrg(int_4 orgx, int_4 orgy)
|
---|
446 | // Modifie l'origine de l'image
|
---|
447 | // float XPxSize() YPxSize()
|
---|
448 | // Taille des pixels (X,Y) de l'image. Peuvent etre modifiees
|
---|
449 | // par la methode "SetPxSize()"
|
---|
450 | // void SetPxSize()
|
---|
451 | // PBaseDataTypes PixelType()
|
---|
452 | // Type des pixels de l'image "(kint_2, kint_4, kr_4, ...)"
|
---|
453 | // char * PixelNomType()
|
---|
454 | // Nom du Type des pixels de l'image. Chaine de caracteres = kint_2, kint_4, ...
|
---|
455 | // char * Nom()
|
---|
456 | // Nom de l'image. "Ident()" retourne l' "id" associe a l'image
|
---|
457 | // void SetNameId(int imgid = 0, char const * nom = NULL)
|
---|
458 | // Permet de modifier l'identificateur et le nom d'une image
|
---|
459 | // void SetAtt(int nbnul = -1, int nbsat = -1, -
|
---|
460 | // float minpix = 0, float maxpix = -1., -
|
---|
461 | // float moypix = 0, float sigpix = -1., -
|
---|
462 | // float fnd = -1., float sigfnd = -1.)
|
---|
463 | // Des donnees membres du type reels (float)
|
---|
464 | // "fond, sigmaFond, minPix, moyPix,sigPix" et entier (int) "nbNul, nbSat"
|
---|
465 | // sont associees a chaque image. Leurs valeurs peuvent etre modifiees
|
---|
466 | // a l'aide de la methode "SetAtt()" ou la methode "CheckDyn()"
|
---|
467 | // int CheckDyn(double min=-9.e19, double max=9.e19)
|
---|
468 | // Cette methode retourne le nombre de pixels hors dynamique "( <min ou >max )"
|
---|
469 | // Met a jour les donnees membres "min/maxPix nbNul/Sat".
|
---|
470 | // Calcule aussi la moyenne et le sigma des pixels -> "moyPix, sigPix"
|
---|
471 | //--
|
---|
472 |
|
---|
473 | //++
|
---|
474 | // Titre I/O
|
---|
475 | //--
|
---|
476 | //++
|
---|
477 | // void Write(string const& flnm)
|
---|
478 | // void Save(char *flnm)
|
---|
479 | // Ecrit l'image en format PPF (PPersist) dans le fichier "flnm"
|
---|
480 | // void Read(string const& flnm)
|
---|
481 | // Lit le fichier "flnm" (au format PPF)
|
---|
482 | //--
|
---|
483 |
|
---|
484 | /* --Methode-- */
|
---|
485 | DVList& RzImage::Info()
|
---|
486 | {
|
---|
487 | if (mInfo == NULL) mInfo = new DVList;
|
---|
488 | return(*mInfo);
|
---|
489 | }
|
---|
490 |
|
---|
491 | /* --Methode-- */
|
---|
492 | void RzImage::SetOrg(int_4 orgx, int_4 orgy)
|
---|
493 | {
|
---|
494 | org_x = orgx; org_y = orgy;
|
---|
495 | return;
|
---|
496 | }
|
---|
497 |
|
---|
498 | /* --Methode-- */
|
---|
499 | void RzImage::SetPxSize(float szx, float szy)
|
---|
500 | {
|
---|
501 | if (szx <= 0.) szx = 1.;
|
---|
502 | if (szy <= 0.) szy = 1.;
|
---|
503 | pxsz_x = szx; pxsz_y = szy;
|
---|
504 | return;
|
---|
505 | }
|
---|
506 |
|
---|
507 | /* --Methode-- */
|
---|
508 | void RzImage::SetAtt(int nbnul, int nbsat,
|
---|
509 | float minpix, float maxpix,
|
---|
510 | float moypix, float sigpix,
|
---|
511 | float fnd, float sigfnd)
|
---|
512 | {
|
---|
513 | nbNul = nbnul; nbSat = nbsat;
|
---|
514 | minPix = minpix; maxPix = maxpix;
|
---|
515 | moyPix = moypix; sigPix = sigpix;
|
---|
516 | fond = fnd; sigmaFond = sigfnd;
|
---|
517 | return;
|
---|
518 | }
|
---|
519 |
|
---|
520 |
|
---|
521 | /* --Methode-- */
|
---|
522 | void RzImage::SetNameId(int imgid, char const * nom)
|
---|
523 | {
|
---|
524 | id = imgid;
|
---|
525 | if (nom) { strncpy(name, nom, 32); name[31] = '\0'; }
|
---|
526 | else name[0] = '\0';
|
---|
527 | return;
|
---|
528 | }
|
---|
529 |
|
---|
530 | /* --Methode-- */
|
---|
531 | void RzImage::Save(char *flnm)
|
---|
532 | {
|
---|
533 | POutPersist s(flnm);
|
---|
534 | Write(s);
|
---|
535 | }
|
---|
536 |
|
---|
537 | /* --Methode-- */
|
---|
538 | void RzImage::WriteSelf(POutPersist& s) const
|
---|
539 |
|
---|
540 | /* Ecriture de la structure image ds un fichier de maniere */
|
---|
541 | /* brute - (Dump memoire) */
|
---|
542 | /* Pour utilisation par PPersist::Write() */
|
---|
543 |
|
---|
544 | {
|
---|
545 | char buf[256];
|
---|
546 | unsigned long sz;
|
---|
547 | int i,j;
|
---|
548 | int_4 itab[5];
|
---|
549 |
|
---|
550 | sprintf(buf,"\nRzImage(%s)[%d*%d] Org: %d %d PxSz: %g %g (Id=%d Nom=%s)\n",
|
---|
551 | PixelNomType(),siz_x, siz_y, org_x, org_y, pxsz_x, pxsz_y, id, name);
|
---|
552 | i = strlen(buf);
|
---|
553 | sprintf(buf+i,"NbSat,Nul=%d %d Min,Max= %g %g Moy,Sig= %g %g Fond,Sig= %g %g\n",
|
---|
554 | nbSat, nbNul, minPix, maxPix, moyPix, sigPix, fond, sigmaFond);
|
---|
555 | i += strlen(buf+i);
|
---|
556 | for(j=i; j<255; j++) buf[j] = ' '; buf[255] = '\0';
|
---|
557 | s.PutBytes(buf, 256);
|
---|
558 |
|
---|
559 | itab[0] = (int)dataType;
|
---|
560 | itab[1] = siz_x; itab[2] = siz_y;
|
---|
561 | itab[3] = itab[4] = 0;
|
---|
562 | if (mInfo) itab[3] = 1; // Has DVList as info
|
---|
563 | s.PutI4s(itab, 5);
|
---|
564 |
|
---|
565 | s.PutR4(fond);
|
---|
566 | s.PutR4(sigmaFond);
|
---|
567 | s.PutI4(nbNul);
|
---|
568 | s.PutI4(nbSat);
|
---|
569 | s.PutR4s(&minPix, 4); // minPix -> sigPix
|
---|
570 |
|
---|
571 | s.PutI4(org_x);
|
---|
572 | s.PutI4(org_y);
|
---|
573 | s.PutR4(pxsz_x);
|
---|
574 | s.PutR4(pxsz_y);
|
---|
575 | s.PutI4(isFits);
|
---|
576 | s.PutBytes(name, 32);
|
---|
577 | s.PutI4(id);
|
---|
578 |
|
---|
579 | sz = XSize()*YSize();
|
---|
580 | switch (dataType) {
|
---|
581 | case kuint_1:
|
---|
582 | s.PutBytes(voidP, sz);
|
---|
583 | break;
|
---|
584 | case kuint_2:
|
---|
585 | s.PutU2s((uint_2*)voidP, sz);
|
---|
586 | break;
|
---|
587 | case kint_2:
|
---|
588 | s.PutI2s((int_2*)voidP, sz);
|
---|
589 | break;
|
---|
590 | case kint_4:
|
---|
591 | s.PutI4s((int_4*)voidP, sz);
|
---|
592 | break;
|
---|
593 | case kr_4:
|
---|
594 | s.PutR4s((r_4*)voidP, sz);
|
---|
595 | break;
|
---|
596 | case kr_8:
|
---|
597 | s.PutR8s((r_8*)voidP, sz);
|
---|
598 | break;
|
---|
599 | default:
|
---|
600 | printf("RzImage::WriteSelf Error - Unsupported DataType= %d \n", (int)dataType);
|
---|
601 | break;
|
---|
602 | }
|
---|
603 |
|
---|
604 | if (mInfo) s << (*mInfo);
|
---|
605 | return;
|
---|
606 | }
|
---|
607 |
|
---|
608 |
|
---|
609 | /* --Methode-- */
|
---|
610 | void RzImage::ReadSelf(PInPersist& s)
|
---|
611 | /* Relecture de fichier cree par WriteSelf() */
|
---|
612 | /* Pour utilisation par PPersist::Read() */
|
---|
613 | {
|
---|
614 | char buf[256];
|
---|
615 | unsigned long sz;
|
---|
616 | int_4 itab[5];
|
---|
617 | int_4 opt;
|
---|
618 |
|
---|
619 | s.GetBytes(buf, 256);
|
---|
620 | s.GetI4s(itab, 5);
|
---|
621 | opt = (int_4)PixelType();
|
---|
622 | if ((opt > 0) && (opt != itab[0])) {
|
---|
623 | cerr << "RzImage::ReadSelf() Image type (=" << opt
|
---|
624 | << ") mismatch (type in file=" << itab[0] << ")" << endl;
|
---|
625 | THROW(typeMismatchErr); }
|
---|
626 | Allocate((PBaseDataTypes)itab[0], itab[1], itab[2]);
|
---|
627 |
|
---|
628 | s.GetR4(fond);
|
---|
629 | s.GetR4(sigmaFond);
|
---|
630 | s.GetI4(nbNul);
|
---|
631 | s.GetI4(nbSat);
|
---|
632 | s.GetR4s(&minPix, 4); // minPix -> sigPix
|
---|
633 |
|
---|
634 | s.GetI4(org_x);
|
---|
635 | s.GetI4(org_y);
|
---|
636 | s.GetR4(pxsz_x);
|
---|
637 | s.GetR4(pxsz_y);
|
---|
638 | s.GetI4(isFits);
|
---|
639 | s.GetBytes(name, 32);
|
---|
640 | s.GetI4(id);
|
---|
641 |
|
---|
642 | sz = XSize()*YSize();
|
---|
643 | switch (dataType) {
|
---|
644 | case kuint_1:
|
---|
645 | s.GetBytes(voidP, sz);
|
---|
646 | break;
|
---|
647 | case kuint_2:
|
---|
648 | s.GetU2s((uint_2*)voidP, sz);
|
---|
649 | break;
|
---|
650 | case kint_2:
|
---|
651 | s.GetI2s((int_2*)voidP, sz);
|
---|
652 | break;
|
---|
653 | case kint_4:
|
---|
654 | s.GetI4s((int_4*)voidP, sz);
|
---|
655 | break;
|
---|
656 | case kr_4:
|
---|
657 | s.GetR4s((r_4*)voidP, sz);
|
---|
658 | break;
|
---|
659 | case kr_8:
|
---|
660 | s.GetR8s((r_8*)voidP, sz);
|
---|
661 | break;
|
---|
662 | default:
|
---|
663 | printf("RzImage::ReadSelf Error - Unsupported DataType= %d \n", (int)dataType);
|
---|
664 | break;
|
---|
665 | }
|
---|
666 |
|
---|
667 | if (itab[3]) // has info
|
---|
668 | { if (mInfo == NULL) mInfo = new DVList;
|
---|
669 | s >> (*mInfo); }
|
---|
670 |
|
---|
671 | return;
|
---|
672 | }
|
---|
673 |
|
---|
674 |
|
---|
675 | //++
|
---|
676 | // Titre Impression
|
---|
677 | //--
|
---|
678 | //++
|
---|
679 | // void Print(ostream& os)
|
---|
680 | // Imprime sur le flot "os" les informations globales concernant l'image
|
---|
681 | // void Print()
|
---|
682 | // Imprime (sur cout) les informations globales concernant l'image
|
---|
683 | // ostream& operator << (ostream& s, RzImage const & img)
|
---|
684 | // sortie sur flot "s" (Appel a "Print(s)").
|
---|
685 | //--
|
---|
686 |
|
---|
687 | /* --Methode-- */
|
---|
688 | void RzImage::Print(ostream& os) const
|
---|
689 | {
|
---|
690 | char buff[256];
|
---|
691 | os << " ---- RzImage::Print() ----- \n";
|
---|
692 | sprintf(buff, "RzImage(%s)[%d*%d] Org: %d %d PxSz= %g %g (Id=%d Nom=%s)\n",
|
---|
693 | PixelNomType(),siz_x, siz_y, org_x, org_y, pxsz_x, pxsz_y, id, name);
|
---|
694 | os << buff;
|
---|
695 | sprintf(buff, "NbSat,Nul=%d %d Min,Max= %g %g Moy,Sig= %g %g Fond,Sig= %g %g\n",
|
---|
696 | nbSat, nbNul, minPix, maxPix, moyPix, sigPix, fond, sigmaFond);
|
---|
697 | os << buff;
|
---|
698 | if (mInfo) mInfo->Print(os);
|
---|
699 | os << endl;
|
---|
700 | }
|
---|
701 |
|
---|
702 | /* --Methode-- */
|
---|
703 | int RzImage::CheckDyn(double min, double max)
|
---|
704 |
|
---|
705 | /* Cette methode retourne le nombre de pixels hors dynamique */
|
---|
706 | /* ds le pave ( <min ou >max ) */
|
---|
707 | /* Met a jour la structure image Min/MaxPix nbNul/Sat */
|
---|
708 | /* Calcule aussi la moyenne et le sigma des pixels, met a jour */
|
---|
709 | /* moypix et sigpix dans la structure */
|
---|
710 |
|
---|
711 | {
|
---|
712 | float pixv=0;
|
---|
713 | double dv;
|
---|
714 |
|
---|
715 | float minadu = PutInRange(min, pixv);
|
---|
716 | float maxadu = PutInRange(max, pixv);
|
---|
717 | float minpix = PutInRange(maxadu+1, pixv);
|
---|
718 | float maxpix = PutInRange(minadu-1, pixv);
|
---|
719 |
|
---|
720 | double m = 0.;
|
---|
721 | double s = 0.;
|
---|
722 | int n9 = 0;
|
---|
723 | int n0 = 0;
|
---|
724 | int n = XSize()*YSize();
|
---|
725 | for(int i=0; i<n; i++)
|
---|
726 | {
|
---|
727 | pixv = FValue(i);
|
---|
728 | if (pixv <= maxadu && pixv >= minadu)
|
---|
729 | {
|
---|
730 | if (pixv < minpix) minpix = pixv;
|
---|
731 | else if (pixv > maxpix) maxpix = pixv;
|
---|
732 | dv = (double)pixv;
|
---|
733 | m += dv; s += (dv*dv);
|
---|
734 | }
|
---|
735 | else
|
---|
736 | {
|
---|
737 | if (pixv > maxadu) n9++;
|
---|
738 | if (pixv < minadu) n0++;
|
---|
739 | }
|
---|
740 | }
|
---|
741 |
|
---|
742 | nbNul = n0;
|
---|
743 | nbSat = n9;
|
---|
744 | minPix = minpix;
|
---|
745 | maxPix = maxpix;
|
---|
746 |
|
---|
747 | n -= (n0+n9);
|
---|
748 | if (n > 0)
|
---|
749 | { dv = (double)n; m /= dv; s = s/dv - m*m;
|
---|
750 | if (s>0.) s = sqrt(s); else s = 0.;
|
---|
751 | moyPix = m; sigPix = s; }
|
---|
752 | else { moyPix = 0.; sigPix = -1.; }
|
---|
753 |
|
---|
754 | return(n0+n9);
|
---|
755 | }
|
---|
756 |
|
---|
757 | ////////////////////////////////////////////////////////////////
|
---|
758 | //++
|
---|
759 | RzImage* RzImage::FitResidus(GeneralFit& gfit)
|
---|
760 | //
|
---|
761 | // Retourne une RzImage R4 contenant les residus du fit ``gfit''.
|
---|
762 | //--
|
---|
763 | {
|
---|
764 | if(XSize()<=0 || YSize()<=0) return NULL;
|
---|
765 | GeneralFunction* f = gfit.GetFunction();
|
---|
766 | if(f==NULL) return NULL;
|
---|
767 | Vector par = gfit.GetParm();
|
---|
768 | RzImage* img = new RzImage(DataType((r_4)0.),XSize(),YSize());
|
---|
769 | img->SetOrg(XOrg(),YOrg());
|
---|
770 | img->SetPxSize(XPxSize(),YPxSize());
|
---|
771 | r_4* ptr = (r_4 *) img->VoidP();
|
---|
772 | for(int i=0;i<XSize();i++) {
|
---|
773 | double x[2];
|
---|
774 | x[0] = XOrg()+(i+0.5)*XPxSize();
|
---|
775 | for(int j=0;j<YSize();j++) {
|
---|
776 | x[1] = YOrg()+(j+0.5)*YPxSize();
|
---|
777 | *(ptr+i+j*img->siz_x) = FValue(i,j) - (float) f->Value(x,par.Data());
|
---|
778 | }
|
---|
779 | }
|
---|
780 | return img;
|
---|
781 | }
|
---|
782 |
|
---|
783 | //++
|
---|
784 | RzImage* RzImage::FitFunction(GeneralFit& gfit)
|
---|
785 | //
|
---|
786 | // Retourne RzImage R4 contenant la fonction du fit ``gfit''.
|
---|
787 | //--
|
---|
788 | {
|
---|
789 | if(XSize()<=0 || YSize()<=0) return NULL;
|
---|
790 | GeneralFunction* f = gfit.GetFunction();
|
---|
791 | if(f==NULL) return NULL;
|
---|
792 | Vector par = gfit.GetParm();
|
---|
793 | RzImage* img = new RzImage(DataType((r_4)0.),XSize(),YSize());
|
---|
794 | img->SetOrg(XOrg(),YOrg());
|
---|
795 | img->SetPxSize(XPxSize(),YPxSize());
|
---|
796 | r_4* ptr = (r_4 *) img->VoidP();
|
---|
797 | for(int i=0;i<XSize();i++) {
|
---|
798 | double x[2];
|
---|
799 | x[0] = XOrg()+(i+0.5)*XPxSize();
|
---|
800 | for(int j=0;j<YSize();j++) {
|
---|
801 | x[1] = YOrg()+(j+0.5)*YPxSize();
|
---|
802 | *(ptr+i+j*img->siz_x) = (float) f->Value(x,par.Data());
|
---|
803 | }
|
---|
804 | }
|
---|
805 | return img;
|
---|
806 | }
|
---|
807 |
|
---|
808 | //++
|
---|
809 | // Titre Acces aux pixels
|
---|
810 | // *Attention:* Les methodes IValue() FValue() et DValue() sont lentes.
|
---|
811 | // Ne pas les utiliser dans les parties du code ou la vitesse de traitement
|
---|
812 | // est critique.
|
---|
813 | //--
|
---|
814 | //++
|
---|
815 | // void * VoidP()
|
---|
816 | // Renvoie un pointeur void sur le tableau des pixels. Doit etre converti
|
---|
817 | // dans le bon type suivant le contenu de l'image.
|
---|
818 | // int IValue(int i, int j)
|
---|
819 | // Renvoie la valeur du pixel "(i,j)" converti en int
|
---|
820 | // float FValue(int i, int j)
|
---|
821 | // Renvoie la valeur du pixel "(i,j)" converti en float
|
---|
822 | // double DValue(int i, int j)
|
---|
823 | // Renvoie la valeur du pixel "(i,j)" converti en double
|
---|
824 | // int IValue(int k)
|
---|
825 | // Renvoie la valeur du pixel numero "k" converti en int
|
---|
826 | // float FValue(int k)
|
---|
827 | // Renvoie la valeur du pixel numero "k" converti en float
|
---|
828 | // double DValue(int k)
|
---|
829 | // envoie la valeur du pixel numero "k" converti en double
|
---|
830 | //
|
---|
831 | // Les pixels sont rangees ligne apres ligne
|
---|
832 | //| {(1,1), (2,1), ... (m, 1), (1,2), ... , (m,2), .... (m,n)}
|
---|
833 | //--
|
---|
834 |
|
---|
835 | /* --Methodes-- */
|
---|
836 | int RzImage::IVal_u1(int k) const {return vect.u1->IElem(k);}
|
---|
837 | int RzImage::IVal_u2(int k) const {return vect.u2->IElem(k);}
|
---|
838 | int RzImage::IVal_i2(int k) const {return vect.i2->IElem(k);}
|
---|
839 | int RzImage::IVal_i4(int k) const {return vect.i4->IElem(k);}
|
---|
840 | int RzImage::IVal_r4(int k) const {return vect.r4->IElem(k);}
|
---|
841 | int RzImage::IVal_r8(int k) const {return vect.r8->IElem(k);}
|
---|
842 |
|
---|
843 | float RzImage::FVal_u1(int k) const {return vect.u1->FElem(k);}
|
---|
844 | float RzImage::FVal_u2(int k) const {return vect.u2->FElem(k);}
|
---|
845 | float RzImage::FVal_i2(int k) const {return vect.i2->FElem(k);}
|
---|
846 | float RzImage::FVal_i4(int k) const {return vect.i4->FElem(k);}
|
---|
847 | float RzImage::FVal_r4(int k) const {return vect.r4->FElem(k);}
|
---|
848 | float RzImage::FVal_r8(int k) const {return vect.r8->FElem(k);}
|
---|
849 |
|
---|
850 | double RzImage::DVal_u1(int k) const {return vect.u1->DElem(k);}
|
---|
851 | double RzImage::DVal_u2(int k) const {return vect.u2->DElem(k);}
|
---|
852 | double RzImage::DVal_i2(int k) const {return vect.i2->DElem(k);}
|
---|
853 | double RzImage::DVal_i4(int k) const {return vect.i4->DElem(k);}
|
---|
854 | double RzImage::DVal_r4(int k) const {return vect.r4->DElem(k);}
|
---|
855 | double RzImage::DVal_r8(int k) const {return vect.r8->DElem(k);}
|
---|
856 |
|
---|
857 |
|
---|
858 |
|
---|
859 | // ******** INSTANCES
|
---|
860 | #if defined(__xlC)
|
---|
861 | void instancetemprzimg(int n)
|
---|
862 | {
|
---|
863 | /* Cette fonction sert uniquement a forcer le compilo a instancier les
|
---|
864 | classes/fonctions template */
|
---|
865 |
|
---|
866 | RzVect<uint_1> rzvu1(n);
|
---|
867 | RzVect<uint_2> rzvu2(n);
|
---|
868 | RzVect<int_2> rzvi2(n);
|
---|
869 | RzVect<int_4> rzvi4(n);
|
---|
870 | RzVect<r_4> rzvr4(n);
|
---|
871 | RzVect<r_8> rzvr8(n);
|
---|
872 | return;
|
---|
873 | }
|
---|
874 |
|
---|
875 | #endif
|
---|
876 |
|
---|
877 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
878 | #pragma define_template RzVect<uint_1>
|
---|
879 | #pragma define_template RzVect<uint_2>
|
---|
880 | #pragma define_template RzVect<int_2>
|
---|
881 | #pragma define_template RzVect<int_4>
|
---|
882 | #pragma define_template RzVect<r_4>
|
---|
883 | #pragma define_template RzVect<r_8>
|
---|
884 | #endif
|
---|
885 |
|
---|
886 | #ifdef GNU_TEMPLATES
|
---|
887 | template class RzVect<uint_1>;
|
---|
888 | template class RzVect<uint_2>;
|
---|
889 | template class RzVect<int_2>;
|
---|
890 | template class RzVect<int_4>;
|
---|
891 | template class RzVect<r_4>;
|
---|
892 | template class RzVect<r_8>;
|
---|
893 | #endif
|
---|
894 |
|
---|
895 | #if defined(ANSI_TEMPLATES)
|
---|
896 | template class RzVect<uint_1>;
|
---|
897 | template class RzVect<uint_2>;
|
---|
898 | template class RzVect<int_2>;
|
---|
899 | template class RzVect<int_4>;
|
---|
900 | template class RzVect<r_4>;
|
---|
901 | template class RzVect<r_8>;
|
---|
902 | #endif
|
---|