source: Sophya/branches/EA_Juin98/SophyaPI/PI/piimage.cc@ 1036

Last change on this file since 1036 was 107, checked in by ansari, 27 years ago

Suppression des PIScDrawer / remplacement par des PIDrawer Reza 22/05/98

File size: 14.3 KB
Line 
1// Affichage d'image R. Ansari 05/95
2// LAL (Orsay) / IN2P3-CNRS DAPNIA/SPP (Saclay) / CEA
3// Refonte Reza 10/95
4
5
6#include <stdlib.h>
7#include <stdio.h>
8#include <string>
9#include <string.h>
10
11#include "piimage.h"
12
13#include "imageop.h"
14
15
16/* --Methode-- */
17PIImage::PIImage(PIContainerGen *par, char *nom,
18 int sx, int sy, int px, int py) :
19 PIPixmap(par, nom, sx, sy, px, py)
20{
21
22img = NULL;
23aisup = false;
24curshow = true;
25winovis = true; // $CHECK$ Reza A enlever si possible 17/6/96
26
27
28zow = NULL;
29gvw = NULL;
30trtw = NULL;
31trtlb = txw = NULL;
32SetTextWin(NULL);
33gpixm = zpixm = pixm = NULL;
34xsgpxm = xszpxm = xspxm = 0;
35ysgpxm = yszpxm = yspxm = 0;
36lut = NULL;
37offx = offy = 0;
38xpav = ypav = 0;
39xpv0 = ypv0 = 0;
40xszpav = yszpav = 1;
41xmv0 = ymv0 = -1;
42dxmv = dymv = 0;
43xcurs = ycurs = -1;
44zoom = 1;
45zmgv = 1;
46
47SetColMap(CMAP_COLRJ32);
48
49SetBackgroundColor(PI_Black);
50
51ActivateButton(1);
52ActivateMove(1);
53ActivateButton(2);
54ActivateMove(2);
55ActivateButton(3);
56ActivateMove(3);
57
58}
59
60/* --Methode-- */
61PIImage::~PIImage()
62{
63if (pixm) delete pixm;
64if (zpixm) delete zpixm;
65if (txw) txw->SetLabel("");
66if (zow)
67 {
68 zow->SetPixmap(NULL,0,0);
69 zow->EraseWindow();
70 }
71if (gpixm) delete gpixm;
72if (gvw)
73 {
74 gvw->SetPixmap(NULL,0,0);
75 gvw->EraseWindow();
76 gvw->SetMsg(0); gvw->SetMsgParent(NULL);
77 gvw->SetUserData(NULL, 0);
78 }
79if (lut) delete lut;
80if (aisup) delete img; // Suppression automatique d'image
81if (trtw) delete trtw;
82}
83
84/* --Methode-- */
85void PIImage::Process(PIMessage msg, PIMsgHandler* sender, void* data)
86{
87if (UserMsg(msg) == (Msg()+626))
88 {
89 if (ModMsg(msg) == PIMsg_Click)
90 { int* pxy=(int *)data; SetPave(pxy[0]*zmgv, pxy[1]*zmgv); }
91 if (ModMsg(msg) == PIMsg_Resize)
92 { ComputeGloVPixmap(); SetGloVPixmap(); }
93 }
94else ReSend(msg, sender, data);
95return;
96}
97
98/* --Methode-- */
99void PIImage::SetImage(P2DArrayAdapter *pim, bool ad, bool refr)
100{
101
102offx = offy = 0;
103xpav = ypav = 0;
104xszpav = yszpav = 1;
105img = pim;
106if (gpixm) delete gpixm;
107gpixm = NULL;
108xsgpxm = ysgpxm = 0;
109zmgv = 1;
110
111if (img) {
112// SetSize(img->XSize(), img->YSize());
113 CenterPave();
114 float min=1., max=-1., moy, sig;
115 int nbnul, nbsat;
116 img->CheckDyn(min, max, moy, sig, nbnul, nbsat);
117 SetLut(min, max, kLutType_Lin, 1, refr);
118 aisup = ad;
119 }
120else aisup = false;
121return;
122}
123
124
125/* --Methode-- */
126void PIImage::SetZoomWin(PIPixmap * zw, bool refr)
127{
128zow = zw;
129if (zow && refr && img)
130 ComputeZoomPixmap();
131return;
132}
133
134/* --Methode-- */
135void PIImage::SetGloVWin(PIPixmap * zw, bool refr)
136{
137gvw = zw;
138if (gvw && img)
139 { ComputeGloVPixmap();
140 if (refr) SetGloVPixmap(); }
141return;
142}
143
144
145/* --Methode-- */
146void PIImage::SetTextWin(PILabel * tw, bool trw, int tx, int ty)
147{
148txw = tw;
149if (trw)
150 {
151 if (trtw) trtw->SetSize(tx, ty);
152 else
153 {
154 trtw = new PIWindow(this, "PixVal", PIWK_transient, tx, ty, 0, 0);
155 trtw->SetAutoDelChilds(true);
156 trtlb = new PILabel(trtw, "PixVal", tx, ty, 0, 0);
157 trtlb->SetBinding(PIBK_fixed,PIBK_fixed, PIBK_fixed,PIBK_fixed);
158 }
159 }
160else
161 if (trtw) { delete trtw; trtw = NULL; trtlb = NULL; }
162
163return;
164}
165
166/* --Methode-- */
167void PIImage::ShowCursor(bool sc)
168{
169if (curshow == sc) return;
170curshow = sc;
171if (curshow) DrawCursor(mWGrC);
172else DrawCursor(mWGrC, -1, -1);
173return;
174}
175
176/* --Methode-- */
177void PIImage::SetLut(float min, float max, int typlut, int lauto, bool refr)
178{
179if (lut) delete(lut);
180if (lauto && img)
181 {
182 int nbsig = lauto;
183 float fracmax = 1. - 0.05*nbsig;
184 img->ComputeLut_PicHisto(min, max, nbsig, fracmax);
185 }
186
187lut = new LUT(min, max, NbCol(), typlut);
188
189ComputeGloVPixmap();
190if (refr) Apply();
191return;
192}
193
194
195/* --Methode-- */
196void PIImage::SetLut(LUT *clut, bool refr)
197{
198LUT * olut = lut;
199lut = new LUT(clut->Min(), clut->Max(), NbCol(), clut->Type());
200if (olut) delete olut;
201
202ComputeGloVPixmap();
203if (refr) Apply();
204return;
205}
206
207/* --Methode-- */
208void PIImage::SetZoomF(float fzm, bool refr)
209{
210int zm;
211
212if (fzm < 0.01) return;
213if (fzm < 1.) zm = (int)(-(1./fzm));
214else zm = (int)(fzm+0.25);
215SetZoom(zm, refr);
216return;
217}
218
219/* --Methode-- */
220void PIImage::SetZoom(int zm, bool refr)
221{
222if ((zm == 0) || (zm == -1) ) zoom = 1;
223else if (zm > 20) zoom = 20;
224else if (zm < -20) zoom = -20;
225else zoom = zm;
226if (img)
227 { SetOffsetFromPave();
228 if (refr) Apply(); }
229return;
230}
231
232/* --Methode-- */
233float PIImage::GetZoomF()
234{
235if (zoom >= 1) return((float)zoom);
236else return(-1./(float)zoom);
237}
238
239
240/* --Methode-- */
241void PIImage::SetOffset(int ox, int oy, bool refr)
242{
243if (img == NULL) {offx = offy = 0; return; }
244if (ox < 0) ox = 0;
245if (ox >= (img->XSize()-1)) ox = img->XSize()-2;
246if (oy < 0) oy = 0;
247if (oy >= (img->YSize()-1)) oy = img->YSize()-2;
248offx = ox; offy = oy;
249CenterPave();
250if (refr) Apply();
251return;
252}
253
254
255/* --Methode-- */
256void PIImage::SetPave(int x, int y, bool refr, bool cent)
257{
258int xc, yc;
259
260if (img == NULL)
261 { offx = offy = 0;
262 xpav = ypav = 0;
263 return; }
264
265if ( (x >= (img->XSize()-1)) || ( x < 0) ||
266 (y >= (img->YSize()-1)) || ( y < 0) ) return;
267
268if ( (x == xpav) && (y == ypav) ) return;
269xpav = x; ypav = y;
270
271if ( PosImg2W(xpav,ypav,&xc, &yc) || cent) // Il faut recenter l'image
272 { SetOffsetFromPave();
273 if (refr) Apply(true); }
274else if (refr)
275 { Apply(false); if (curshow) DrawCursor(mWGrC); }
276
277return;
278}
279
280
281/* --Methode-- */
282void PIImage::SetColMap(CMapId cmap, bool refr)
283{
284PIPixmap::SetColMap(cmap, refr);
285if (zow) zow->SetColMap(cmap, refr);
286if (gvw) gvw->SetColMap(cmap, refr);
287return;
288}
289
290
291/* --Methode-- */
292void PIImage::Apply(bool mw)
293{
294if (img == NULL) return;
295if (mw)
296 {
297 ComputePixmap();
298 Send(Msg(), PIMsg_Active);
299 }
300if (zow != NULL) ComputeZoomPixmap();
301if (gvw != NULL)
302 if ( (gvw->UserData() != this) || (mw) ) SetGloVPixmap();
303
304if (txw || trtlb)
305 {
306 char buff[256];
307 float x,y;
308 img->XYCoord(xpav, ypav, x, y);
309 sprintf(buff,"X= %g , Y= %g PixVal= %g (%d,%d)",
310 x, y , (*img)(xpav, ypav), xpav, ypav );
311 if (txw) txw->SetLabel((string)buff);
312 if (trtlb) trtlb->SetLabel((string)buff);
313 }
314
315return;
316}
317
318/* --Methode-- */
319void PIImage::Resize()
320{
321// printf("PIImage::Resize\n");
322PIPixmap::Resize();
323CenterPave();
324Apply();
325return;
326}
327
328/* --Methode-- */
329void PIImage::Draw(PIGraphic* g, int x0, int y0, int dx, int dy)
330{
331PIPixmap::Draw(g, x0, y0, dx, dy);
332winovis = false; // $CHECK$ Reza A enlever si possible 17/6/96
333if (curshow)
334 {
335 int xc,yc;
336 PosImg2W(xpav, ypav, &xc, &yc);
337 if ((xc >= x0) && (xc < x0+dx) &&
338 (yc >= y0) && (yc < y0+dy) ) DrawCursor(g, xc, yc);
339 }
340
341return;
342}
343
344/* --Methode-- */
345void PIImage::But1Press(int x, int y)
346{
347int xp, yp;
348
349Send(Msg(), PIMsg_Active);
350if (img == NULL) return;
351if ( PosW2Img(x, y, &xp, &yp) ) return;
352SelPointerShape(PI_CrossPointer);
353xpav = xp; ypav = yp;
354ShowTrTxW();
355Apply(false);
356if (curshow) DrawCursor(mWGrC, -1,-1);
357return;
358}
359
360
361/* --Methode-- */
362void PIImage::Ptr1Move(int x, int y)
363{
364int xp, yp;
365if (img == NULL) return;
366if ( PosW2Img(x, y, &xp, &yp) ) return;
367xpav = xp; ypav = yp;
368Apply(false);
369return;
370}
371
372
373/* --Methode-- */
374void PIImage::But1Release(int /*x*/, int /*y*/)
375{
376SelPointerShape(PI_ArrowPointer);
377if (curshow) DrawCursor(mWGrC);
378if (trtw) trtw->Hide();
379return;
380}
381
382
383/* --Methode-- */
384void PIImage::But2Press(int x, int y)
385{
386
387Send(Msg(), PIMsg_Active);
388if (img == NULL) return;
389if ( PosW2Img(x, y, &xpv0, &ypv0) ) { xmv0 = -1; return; }
390xmv0 = x; ymv0 = y; dxmv = dymv = 0;
391if (curshow) DrawCursor(mWGrC,-1,-1);
392mWGrC->SelGOMode(PI_GOXOR);
393if (GetColMapId() == CMAP_COLRJ32)
394mWGrC->SelForeground(PI_Yellow);
395else mWGrC->SelForeground(PI_Magenta);
396mWGrC->SelLine(PI_NormalLine);
397ShowTrTxW();
398return;
399}
400
401/* --Methode-- */
402void PIImage::Ptr2Move(int x, int y)
403{
404int xp, yp;
405if (img == NULL) return;
406if (xmv0 < 0) return;
407if ( PosW2Img(x, y, &xp, &yp) ) return;
408mWGrC->DrawBox(xmv0, ymv0, dxmv, dymv);
409dxmv = x-xmv0; dymv = y-ymv0;
410mWGrC->DrawBox(xmv0, ymv0, dxmv, dymv);
411if (txw || trtlb)
412 {
413 char buff[256];
414 float x,y;
415 img->XYCoord(xp, yp, x, y);
416 sprintf(buff,"X= %g , Y= %g PixVal= %g (C= %d,%d)",
417 x, y , (*img)(xp, yp), (xp+xpv0)/2, (yp+ypv0)/2 );
418 if (txw) txw->SetLabel((string)buff);
419 if (trtlb) trtlb->SetLabel((string)buff);
420 }
421return;
422}
423
424/* --Methode-- */
425void PIImage::But2Release(int x, int y)
426{
427int xp2, yp2;
428
429if (img == NULL) return;
430if (xmv0 < 0) return;
431mWGrC->DrawBox(xmv0, ymv0, dxmv, dymv);
432mWGrC->SelGOMode(PI_GOCopy);
433mWGrC->SelForeground(PI_White);
434if ( PosW2Img(x, y, &xp2, &yp2) )
435 PosW2Img(xmv0+dxmv, ymv0+dymv, &xp2, &yp2);
436xpav = (xpv0+xp2)/2;
437ypav = (ypv0+yp2)/2;
438xszpav = ((xp2-xpv0) > 0) ? (xp2-xpv0) : 1;
439yszpav = ((yp2-ypv0) > 0) ? (yp2-ypv0) : 1;
440
441Apply(false);
442if (curshow) DrawCursor(mWGrC);
443if (trtw) trtw->Hide();
444return;
445}
446
447
448/* --Methode-- */
449void PIImage::But3Press(int x, int y)
450{
451int xp, yp;
452
453Send(Msg(), PIMsg_Active);
454if (img == NULL) return;
455if ( PosW2Img(x, y, &xp, &yp) ) { xmv0 = -1; return; }
456if (curshow) DrawCursor(mWGrC,-1,-1);
457ShowTrTxW();
458SelPointerShape(PI_TDLRArrowPointer);
459xmv0 = xp;
460ymv0 = yp;
461return;
462}
463
464
465/* --Methode-- */
466void PIImage::Ptr3Move(int x, int y)
467{
468int xp, yp;
469if (img == NULL) return;
470if (xmv0 < 0) return;
471if ( PosW2Img(x, y, &xp, &yp) ) return;
472if (txw || trtlb)
473 {
474 char buff[256];
475 sprintf(buff,"DelX= %d , DelY= %d ", xp-xmv0, yp-ymv0);
476 if (txw) txw->SetLabel((string)buff);
477 if (trtlb) trtlb->SetLabel((string)buff);
478 }
479}
480
481/* --Methode-- */
482void PIImage::But3Release(int x, int y)
483{
484int xp, yp;
485
486SelPointerShape(PI_ArrowPointer);
487if (trtw) trtw->Hide();
488if (img == NULL) return;
489if (xmv0 < 0) return;
490if ( PosW2Img(x, y, &xp, &yp) ) return;
491
492offx -= (xp-xmv0);
493offy -= (yp-ymv0);
494
495if (offx < 0) offx = 0;
496if (offy < 0) offy = 0;
497if (offx >= img->XSize()) offx = img->XSize();
498if (offy >= img->YSize()) offy = img->YSize();
499xpav = xmv0; ypav = ymv0;
500xmv0 = ymv0 = -1;
501Apply();
502return;
503}
504
505
506/* --Methode-- */
507void PIImage::ComputePixmap()
508{
509unsigned int xwsz, ywsz;
510
511if (img == NULL) return;
512xwsz = this->XSize();
513ywsz = this->YSize();
514
515pixm = img->ComputePixmap(lut, offx, offy, zoom, xwsz, ywsz, pixm, &xspxm, &yspxm);
516SetPixmap(pixm, xspxm, yspxm);
517// Refresh();
518return;
519}
520
521
522/* --Methode-- */
523void PIImage::ComputeZoomPixmap()
524{
525unsigned int xwsz, ywsz;
526int ofx, ofy;
527int zm;
528
529if (zow == NULL) return;
530if (img == NULL) return;
531
532xwsz = zow->XSize();
533ywsz = zow->YSize();
534
535zm = zoom+4;
536if ( (zm==-1) || (zm == 0) ) zm = 1;
537if (zm > 0)
538 {
539 ofx = xpav-(xwsz/2/zm);
540 ofy = ypav-(ywsz/2/zm);
541 }
542else
543 {
544 ofx = xpav+(xwsz*zm/2);
545 ofy = ypav+(ywsz*zm/2);
546 }
547if (ofx < 0) ofx = 0;
548if (ofy < 0) ofy = 0;
549
550zpixm = img->ComputePixmap(lut, ofx, ofy, zm, xwsz, ywsz, zpixm, &xszpxm, &yszpxm);
551zow->SetColMap(GetColMapId(), false);
552zow->SetPixmap(zpixm, xszpxm, yszpxm);
553zow->SetUserData((void *)this, 0);
554// zow->Refresh();
555return;
556
557}
558
559
560/* --Methode-- */
561void PIImage::ComputeGloVPixmap()
562{
563unsigned int xwsz, ywsz;
564int zmx,zmy;
565
566if (gvw == NULL) return;
567if (img == NULL) return;
568
569xwsz = gvw->XSize();
570ywsz = gvw->YSize();
571
572zmx = img->XSize()/xwsz;
573zmy = img->YSize()/ywsz;
574
575if (zmx < zmy) zmx = zmy;
576if (zmx < 1) zmx = 1;
577zmgv = zmx;
578if (zmx > 1) zmx = -zmx;
579gpixm = img->ComputePixmap(lut, 0, 0, zmx, xwsz, ywsz, gpixm, &xsgpxm, &ysgpxm);
580printf("DEBUG-ComputeGloVPixmap() %d-%d , (%d,%d) , %d-%d \n", xwsz, ywsz, zmx, zmy, xsgpxm, ysgpxm);
581return;
582}
583
584/* --Methode-- */
585void PIImage::SetGloVPixmap()
586{
587float zm;
588int x,y, dx,dy;
589
590if (zoom > 0) zm = zmgv*zoom;
591else zm = (float)zmgv/(float)(-zoom);
592x = offx/zmgv+1;
593y = offy/zmgv+1;
594dx = (int) ( (float)XSize()/zm-1. );
595dy = (int) ( (float)YSize()/zm-1. );
596
597
598gvw->ElDelAll();
599gvw->WindowGraphic()->SelForeground(PI_Yellow);
600gvw->WindowGraphic()->SelLine(PI_NormalLine);
601gvw->ElAddRect(x,y,dx,dy);
602gvw->SetColMap(GetColMapId(), false);
603gvw->SetPixmap(gpixm, xsgpxm, ysgpxm);
604gvw->SetUserData((void *)this, 0);
605gvw->SetMsg(Msg()+626);
606gvw->SetMsgParent(this);
607
608return;
609}
610
611/* --Methode-- */
612void PIImage::DrawCursor(PIGraphicGen* g)
613{
614int xc, yc;
615if (PosImg2W(xpav, ypav, &xc, &yc)) DrawCursor(g, -1, -1);
616else DrawCursor(g, xc, yc);
617return;
618}
619
620#define CURSHSIZE 25
621
622/* --Methode-- */
623void PIImage::DrawCursor(PIGraphicGen* g, int xc, int yc)
624{
625
626if ((xcurs>=0) && (ycurs>=0))
627 PIPixmap::Draw(g, xcurs-CURSHSIZE, ycurs-CURSHSIZE, CURSHSIZE*2, CURSHSIZE*2);
628
629if ((xc >= 0) && (yc >= 0))
630 {
631 xcurs = xc; ycurs = yc;
632 if (winovis) return; // $CHECK$ Reza A enlever si possible 17/6/96
633 if (zoom < 3) g->SelLine(PI_NormalLine);
634 else g->SelLine(PI_ThickLine);
635 if ( (GetColMapId() == CMAP_GREY32) || (GetColMapId() == CMAP_GREYINV32) )
636 g->SelForeground(PI_Red);
637 else g->SelForeground(PI_White);
638 g->SelGOMode(PI_GOCopy);
639 g->DrawLine(xcurs-CURSHSIZE, ycurs, xcurs-4, ycurs);
640 g->DrawLine(xcurs+4, ycurs, xcurs+CURSHSIZE, ycurs);
641 g->DrawLine(xcurs, ycurs-CURSHSIZE, xcurs, ycurs-4);
642 g->DrawLine(xcurs, ycurs+4, xcurs, ycurs+CURSHSIZE);
643 }
644else { xcurs = ycurs = -1; }
645
646return;
647}
648
649
650/* --Methode-- */
651void PIImage::ShowTrTxW()
652{
653if (trtw)
654 {
655 int tpx, tpy;
656 GetScreenPos(tpx, tpy);
657 tpy -= (trtw->YSize()+5);
658 if (tpy < 0) tpy = 0;
659 trtw->SetPos(tpx, tpy);
660 trtw->Show();
661 }
662
663return;
664}
665
666/* --Methode-- */
667int PIImage::PosW2Img(int xiw, int yiw, int * xp, int * yp)
668{
669
670if (zoom > 0)
671 {
672 *xp = xiw/zoom+offx;
673 *yp = yiw/zoom+offy;
674 }
675else
676 {
677 *xp = offx-xiw*zoom;
678 *yp = offy-yiw*zoom;
679 }
680
681if ( (*xp >= 0) && (*xp < img->XSize() ) &&
682 (*yp >= 0) && (*yp < img->YSize() ) ) return(0);
683else return(1);
684}
685
686
687/* --Methode-- */
688int PIImage::PosImg2W(int xp, int yp, int * xiw, int * yiw)
689{
690if (zoom > 0)
691 {
692 *xiw = (xp-offx)*zoom+(zoom/2);
693 *yiw = (yp-offy)*zoom+(zoom/2);
694 }
695else
696 {
697 *xiw = (offx-xp)/zoom;
698 *yiw = (offy-yp)/zoom;
699 }
700
701if (*xiw > XSize()) *xiw = -1;
702if (*yiw > YSize()) *yiw = -1;
703
704if ((*xiw < 0) || (*yiw < 0)) return(1);
705else return(0);
706}
707
708
709/* --Methode-- */
710void PIImage::SetOffsetFromPave()
711{
712if (zoom > 0)
713 {
714 offx = xpav-XSize()/2/zoom;
715 offy = ypav-YSize()/2/zoom;
716 }
717else
718 {
719 offx = xpav+XSize()/2*zoom;
720 offy = ypav+YSize()/2*zoom;
721 }
722
723if (offx < 0) offx = 0;
724if (offx >= (img->XSize()-1)) offx = img->XSize()-2;
725if (offy < 0) offy = 0;
726if (offy >= (img->YSize()-1)) offy = img->YSize()-2;
727
728return;
729}
730
731/* --Methode-- */
732void PIImage::CenterPave()
733{
734int xwsz, ywsz;
735
736if (zoom > 0)
737 { xwsz = XSize()/zoom;
738 ywsz = YSize()/zoom; }
739else
740 { xwsz = -(XSize()*zoom);
741 ywsz = -(YSize()*zoom); }
742
743if (xwsz > (img->XSize()-offx)) xwsz = (img->XSize()-offx);
744if (ywsz > (img->YSize()-offy)) ywsz = (img->YSize()-offy);
745xpav = offx+xwsz/2;
746ypav = offx+ywsz/2;
747xszpav = ((xwsz+ywsz)/20) ;
748if (xszpav < 1) xszpav = 1;
749yszpav = xszpav;
750
751return;
752}
Note: See TracBrowser for help on using the repository browser.