// Affichage d'image R. Ansari 05/95 // LAL (Orsay) / IN2P3-CNRS DAPNIA/SPP (Saclay) / CEA // Refonte Reza 10/95 #include #include #include #include #include "piimage.h" #include "imageop.h" /* --Methode-- */ PIImage::PIImage(PIContainerGen *par, char *nom, int sx, int sy, int px, int py) : PIPixmap(par, nom, sx, sy, px, py) { img = NULL; aisup = false; curshow = true; winovis = true; // $CHECK$ Reza A enlever si possible 17/6/96 zow = NULL; gvw = NULL; trtw = NULL; trtlb = txw = NULL; SetTextWin(NULL); gpixm = zpixm = pixm = NULL; xsgpxm = xszpxm = xspxm = 0; ysgpxm = yszpxm = yspxm = 0; lut = NULL; offx = offy = 0; xpav = ypav = 0; xpv0 = ypv0 = 0; xszpav = yszpav = 1; xmv0 = ymv0 = -1; dxmv = dymv = 0; xcurs = ycurs = -1; zoom = 1; zmgv = 1; SetColMap(CMAP_COLRJ32); SetBackgroundColor(PI_Black); ActivateButton(1); ActivateMove(1); ActivateButton(2); ActivateMove(2); ActivateButton(3); ActivateMove(3); } /* --Methode-- */ PIImage::~PIImage() { if (pixm) delete pixm; if (zpixm) delete zpixm; if (txw) txw->SetLabel(""); if (zow) { zow->SetPixmap(NULL,0,0); zow->EraseWindow(); } if (gpixm) delete gpixm; if (gvw) { gvw->SetPixmap(NULL,0,0); gvw->EraseWindow(); gvw->SetMsg(0); gvw->SetMsgParent(NULL); gvw->SetUserData(NULL, 0); } if (lut) delete lut; if (aisup) delete img; // Suppression automatique d'image if (trtw) delete trtw; } /* --Methode-- */ void PIImage::Process(PIMessage msg, PIMsgHandler* sender, void* data) { if (UserMsg(msg) == (Msg()+626)) { if (ModMsg(msg) == PIMsg_Click) { int* pxy=(int *)data; SetPave(pxy[0]*zmgv, pxy[1]*zmgv); } if (ModMsg(msg) == PIMsg_Resize) { ComputeGloVPixmap(); SetGloVPixmap(); } } else ReSend(msg, sender, data); return; } /* --Methode-- */ void PIImage::SetImage(P2DArrayAdapter *pim, bool ad, bool refr) { offx = offy = 0; xpav = ypav = 0; xszpav = yszpav = 1; img = pim; if (gpixm) delete gpixm; gpixm = NULL; xsgpxm = ysgpxm = 0; zmgv = 1; if (img) { // SetSize(img->XSize(), img->YSize()); CenterPave(); float min=1., max=-1., moy, sig; int nbnul, nbsat; img->CheckDyn(min, max, moy, sig, nbnul, nbsat); SetLut(min, max, kLutType_Lin, 1, refr); aisup = ad; } else aisup = false; return; } /* --Methode-- */ void PIImage::SetZoomWin(PIPixmap * zw, bool refr) { zow = zw; if (zow && refr && img) ComputeZoomPixmap(); return; } /* --Methode-- */ void PIImage::SetGloVWin(PIPixmap * zw, bool refr) { gvw = zw; if (gvw && img) { ComputeGloVPixmap(); if (refr) SetGloVPixmap(); } return; } /* --Methode-- */ void PIImage::SetTextWin(PILabel * tw, bool trw, int tx, int ty) { txw = tw; if (trw) { if (trtw) trtw->SetSize(tx, ty); else { trtw = new PIWindow(this, "PixVal", PIWK_transient, tx, ty, 0, 0); trtw->SetAutoDelChilds(true); trtlb = new PILabel(trtw, "PixVal", tx, ty, 0, 0); trtlb->SetBinding(PIBK_fixed,PIBK_fixed, PIBK_fixed,PIBK_fixed); } } else if (trtw) { delete trtw; trtw = NULL; trtlb = NULL; } return; } /* --Methode-- */ void PIImage::ShowCursor(bool sc) { if (curshow == sc) return; curshow = sc; if (curshow) DrawCursor(mWGrC); else DrawCursor(mWGrC, -1, -1); return; } /* --Methode-- */ void PIImage::SetLut(float min, float max, int typlut, int lauto, bool refr) { if (lut) delete(lut); if (lauto && img) { int nbsig = lauto; float fracmax = 1. - 0.05*nbsig; img->ComputeLut_PicHisto(min, max, nbsig, fracmax); } lut = new LUT(min, max, NbCol(), typlut); ComputeGloVPixmap(); if (refr) Apply(); return; } /* --Methode-- */ void PIImage::SetLut(LUT *clut, bool refr) { LUT * olut = lut; lut = new LUT(clut->Min(), clut->Max(), NbCol(), clut->Type()); if (olut) delete olut; ComputeGloVPixmap(); if (refr) Apply(); return; } /* --Methode-- */ void PIImage::SetZoomF(float fzm, bool refr) { int zm; if (fzm < 0.01) return; if (fzm < 1.) zm = (int)(-(1./fzm)); else zm = (int)(fzm+0.25); SetZoom(zm, refr); return; } /* --Methode-- */ void PIImage::SetZoom(int zm, bool refr) { if ((zm == 0) || (zm == -1) ) zoom = 1; else if (zm > 20) zoom = 20; else if (zm < -20) zoom = -20; else zoom = zm; if (img) { SetOffsetFromPave(); if (refr) Apply(); } return; } /* --Methode-- */ float PIImage::GetZoomF() { if (zoom >= 1) return((float)zoom); else return(-1./(float)zoom); } /* --Methode-- */ void PIImage::SetOffset(int ox, int oy, bool refr) { if (img == NULL) {offx = offy = 0; return; } if (ox < 0) ox = 0; if (ox >= (img->XSize()-1)) ox = img->XSize()-2; if (oy < 0) oy = 0; if (oy >= (img->YSize()-1)) oy = img->YSize()-2; offx = ox; offy = oy; CenterPave(); if (refr) Apply(); return; } /* --Methode-- */ void PIImage::SetPave(int x, int y, bool refr, bool cent) { int xc, yc; if (img == NULL) { offx = offy = 0; xpav = ypav = 0; return; } if ( (x >= (img->XSize()-1)) || ( x < 0) || (y >= (img->YSize()-1)) || ( y < 0) ) return; if ( (x == xpav) && (y == ypav) ) return; xpav = x; ypav = y; if ( PosImg2W(xpav,ypav,&xc, &yc) || cent) // Il faut recenter l'image { SetOffsetFromPave(); if (refr) Apply(true); } else if (refr) { Apply(false); if (curshow) DrawCursor(mWGrC); } return; } /* --Methode-- */ void PIImage::SetColMap(CMapId cmap, bool refr) { PIPixmap::SetColMap(cmap, refr); if (zow) zow->SetColMap(cmap, refr); if (gvw) gvw->SetColMap(cmap, refr); return; } /* --Methode-- */ void PIImage::Apply(bool mw) { if (img == NULL) return; if (mw) { ComputePixmap(); Send(Msg(), PIMsg_Active); } if (zow != NULL) ComputeZoomPixmap(); if (gvw != NULL) if ( (gvw->UserData() != this) || (mw) ) SetGloVPixmap(); if (txw || trtlb) { char buff[256]; float x,y; img->XYCoord(xpav, ypav, x, y); sprintf(buff,"X= %g , Y= %g PixVal= %g (%d,%d)", x, y , (*img)(xpav, ypav), xpav, ypav ); if (txw) txw->SetLabel((string)buff); if (trtlb) trtlb->SetLabel((string)buff); } return; } /* --Methode-- */ void PIImage::Resize() { // printf("PIImage::Resize\n"); PIPixmap::Resize(); CenterPave(); Apply(); return; } /* --Methode-- */ void PIImage::Draw(PIGraphic* g, int x0, int y0, int dx, int dy) { PIPixmap::Draw(g, x0, y0, dx, dy); winovis = false; // $CHECK$ Reza A enlever si possible 17/6/96 if (curshow) { int xc,yc; PosImg2W(xpav, ypav, &xc, &yc); if ((xc >= x0) && (xc < x0+dx) && (yc >= y0) && (yc < y0+dy) ) DrawCursor(g, xc, yc); } return; } /* --Methode-- */ void PIImage::But1Press(int x, int y) { int xp, yp; Send(Msg(), PIMsg_Active); if (img == NULL) return; if ( PosW2Img(x, y, &xp, &yp) ) return; SelPointerShape(PI_CrossPointer); xpav = xp; ypav = yp; ShowTrTxW(); Apply(false); if (curshow) DrawCursor(mWGrC, -1,-1); return; } /* --Methode-- */ void PIImage::Ptr1Move(int x, int y) { int xp, yp; if (img == NULL) return; if ( PosW2Img(x, y, &xp, &yp) ) return; xpav = xp; ypav = yp; Apply(false); return; } /* --Methode-- */ void PIImage::But1Release(int /*x*/, int /*y*/) { SelPointerShape(PI_ArrowPointer); if (curshow) DrawCursor(mWGrC); if (trtw) trtw->Hide(); return; } /* --Methode-- */ void PIImage::But2Press(int x, int y) { Send(Msg(), PIMsg_Active); if (img == NULL) return; if ( PosW2Img(x, y, &xpv0, &ypv0) ) { xmv0 = -1; return; } xmv0 = x; ymv0 = y; dxmv = dymv = 0; if (curshow) DrawCursor(mWGrC,-1,-1); mWGrC->SelGOMode(PI_GOXOR); if (GetColMapId() == CMAP_COLRJ32) mWGrC->SelForeground(PI_Yellow); else mWGrC->SelForeground(PI_Magenta); mWGrC->SelLine(PI_NormalLine); ShowTrTxW(); return; } /* --Methode-- */ void PIImage::Ptr2Move(int x, int y) { int xp, yp; if (img == NULL) return; if (xmv0 < 0) return; if ( PosW2Img(x, y, &xp, &yp) ) return; mWGrC->DrawBox(xmv0, ymv0, dxmv, dymv); dxmv = x-xmv0; dymv = y-ymv0; mWGrC->DrawBox(xmv0, ymv0, dxmv, dymv); if (txw || trtlb) { char buff[256]; float x,y; img->XYCoord(xp, yp, x, y); sprintf(buff,"X= %g , Y= %g PixVal= %g (C= %d,%d)", x, y , (*img)(xp, yp), (xp+xpv0)/2, (yp+ypv0)/2 ); if (txw) txw->SetLabel((string)buff); if (trtlb) trtlb->SetLabel((string)buff); } return; } /* --Methode-- */ void PIImage::But2Release(int x, int y) { int xp2, yp2; if (img == NULL) return; if (xmv0 < 0) return; mWGrC->DrawBox(xmv0, ymv0, dxmv, dymv); mWGrC->SelGOMode(PI_GOCopy); mWGrC->SelForeground(PI_White); if ( PosW2Img(x, y, &xp2, &yp2) ) PosW2Img(xmv0+dxmv, ymv0+dymv, &xp2, &yp2); xpav = (xpv0+xp2)/2; ypav = (ypv0+yp2)/2; xszpav = ((xp2-xpv0) > 0) ? (xp2-xpv0) : 1; yszpav = ((yp2-ypv0) > 0) ? (yp2-ypv0) : 1; Apply(false); if (curshow) DrawCursor(mWGrC); if (trtw) trtw->Hide(); return; } /* --Methode-- */ void PIImage::But3Press(int x, int y) { int xp, yp; Send(Msg(), PIMsg_Active); if (img == NULL) return; if ( PosW2Img(x, y, &xp, &yp) ) { xmv0 = -1; return; } if (curshow) DrawCursor(mWGrC,-1,-1); ShowTrTxW(); SelPointerShape(PI_TDLRArrowPointer); xmv0 = xp; ymv0 = yp; return; } /* --Methode-- */ void PIImage::Ptr3Move(int x, int y) { int xp, yp; if (img == NULL) return; if (xmv0 < 0) return; if ( PosW2Img(x, y, &xp, &yp) ) return; if (txw || trtlb) { char buff[256]; sprintf(buff,"DelX= %d , DelY= %d ", xp-xmv0, yp-ymv0); if (txw) txw->SetLabel((string)buff); if (trtlb) trtlb->SetLabel((string)buff); } } /* --Methode-- */ void PIImage::But3Release(int x, int y) { int xp, yp; SelPointerShape(PI_ArrowPointer); if (trtw) trtw->Hide(); if (img == NULL) return; if (xmv0 < 0) return; if ( PosW2Img(x, y, &xp, &yp) ) return; offx -= (xp-xmv0); offy -= (yp-ymv0); if (offx < 0) offx = 0; if (offy < 0) offy = 0; if (offx >= img->XSize()) offx = img->XSize(); if (offy >= img->YSize()) offy = img->YSize(); xpav = xmv0; ypav = ymv0; xmv0 = ymv0 = -1; Apply(); return; } /* --Methode-- */ void PIImage::ComputePixmap() { unsigned int xwsz, ywsz; if (img == NULL) return; xwsz = this->XSize(); ywsz = this->YSize(); pixm = img->ComputePixmap(lut, offx, offy, zoom, xwsz, ywsz, pixm, &xspxm, &yspxm); SetPixmap(pixm, xspxm, yspxm); // Refresh(); return; } /* --Methode-- */ void PIImage::ComputeZoomPixmap() { unsigned int xwsz, ywsz; int ofx, ofy; int zm; if (zow == NULL) return; if (img == NULL) return; xwsz = zow->XSize(); ywsz = zow->YSize(); zm = zoom+4; if ( (zm==-1) || (zm == 0) ) zm = 1; if (zm > 0) { ofx = xpav-(xwsz/2/zm); ofy = ypav-(ywsz/2/zm); } else { ofx = xpav+(xwsz*zm/2); ofy = ypav+(ywsz*zm/2); } if (ofx < 0) ofx = 0; if (ofy < 0) ofy = 0; zpixm = img->ComputePixmap(lut, ofx, ofy, zm, xwsz, ywsz, zpixm, &xszpxm, &yszpxm); zow->SetColMap(GetColMapId(), false); zow->SetPixmap(zpixm, xszpxm, yszpxm); zow->SetUserData((void *)this, 0); // zow->Refresh(); return; } /* --Methode-- */ void PIImage::ComputeGloVPixmap() { unsigned int xwsz, ywsz; int zmx,zmy; if (gvw == NULL) return; if (img == NULL) return; xwsz = gvw->XSize(); ywsz = gvw->YSize(); zmx = img->XSize()/xwsz; zmy = img->YSize()/ywsz; if (zmx < zmy) zmx = zmy; if (zmx < 1) zmx = 1; zmgv = zmx; if (zmx > 1) zmx = -zmx; gpixm = img->ComputePixmap(lut, 0, 0, zmx, xwsz, ywsz, gpixm, &xsgpxm, &ysgpxm); printf("DEBUG-ComputeGloVPixmap() %d-%d , (%d,%d) , %d-%d \n", xwsz, ywsz, zmx, zmy, xsgpxm, ysgpxm); return; } /* --Methode-- */ void PIImage::SetGloVPixmap() { float zm; int x,y, dx,dy; if (zoom > 0) zm = zmgv*zoom; else zm = (float)zmgv/(float)(-zoom); x = offx/zmgv+1; y = offy/zmgv+1; dx = (int) ( (float)XSize()/zm-1. ); dy = (int) ( (float)YSize()/zm-1. ); gvw->ElDelAll(); gvw->WindowGraphic()->SelForeground(PI_Yellow); gvw->WindowGraphic()->SelLine(PI_NormalLine); gvw->ElAddRect(x,y,dx,dy); gvw->SetColMap(GetColMapId(), false); gvw->SetPixmap(gpixm, xsgpxm, ysgpxm); gvw->SetUserData((void *)this, 0); gvw->SetMsg(Msg()+626); gvw->SetMsgParent(this); return; } /* --Methode-- */ void PIImage::DrawCursor(PIGraphicGen* g) { int xc, yc; if (PosImg2W(xpav, ypav, &xc, &yc)) DrawCursor(g, -1, -1); else DrawCursor(g, xc, yc); return; } #define CURSHSIZE 25 /* --Methode-- */ void PIImage::DrawCursor(PIGraphicGen* g, int xc, int yc) { if ((xcurs>=0) && (ycurs>=0)) PIPixmap::Draw(g, xcurs-CURSHSIZE, ycurs-CURSHSIZE, CURSHSIZE*2, CURSHSIZE*2); if ((xc >= 0) && (yc >= 0)) { xcurs = xc; ycurs = yc; if (winovis) return; // $CHECK$ Reza A enlever si possible 17/6/96 if (zoom < 3) g->SelLine(PI_NormalLine); else g->SelLine(PI_ThickLine); if ( (GetColMapId() == CMAP_GREY32) || (GetColMapId() == CMAP_GREYINV32) ) g->SelForeground(PI_Red); else g->SelForeground(PI_White); g->SelGOMode(PI_GOCopy); g->DrawLine(xcurs-CURSHSIZE, ycurs, xcurs-4, ycurs); g->DrawLine(xcurs+4, ycurs, xcurs+CURSHSIZE, ycurs); g->DrawLine(xcurs, ycurs-CURSHSIZE, xcurs, ycurs-4); g->DrawLine(xcurs, ycurs+4, xcurs, ycurs+CURSHSIZE); } else { xcurs = ycurs = -1; } return; } /* --Methode-- */ void PIImage::ShowTrTxW() { if (trtw) { int tpx, tpy; GetScreenPos(tpx, tpy); tpy -= (trtw->YSize()+5); if (tpy < 0) tpy = 0; trtw->SetPos(tpx, tpy); trtw->Show(); } return; } /* --Methode-- */ int PIImage::PosW2Img(int xiw, int yiw, int * xp, int * yp) { if (zoom > 0) { *xp = xiw/zoom+offx; *yp = yiw/zoom+offy; } else { *xp = offx-xiw*zoom; *yp = offy-yiw*zoom; } if ( (*xp >= 0) && (*xp < img->XSize() ) && (*yp >= 0) && (*yp < img->YSize() ) ) return(0); else return(1); } /* --Methode-- */ int PIImage::PosImg2W(int xp, int yp, int * xiw, int * yiw) { if (zoom > 0) { *xiw = (xp-offx)*zoom+(zoom/2); *yiw = (yp-offy)*zoom+(zoom/2); } else { *xiw = (offx-xp)/zoom; *yiw = (offy-yp)/zoom; } if (*xiw > XSize()) *xiw = -1; if (*yiw > YSize()) *yiw = -1; if ((*xiw < 0) || (*yiw < 0)) return(1); else return(0); } /* --Methode-- */ void PIImage::SetOffsetFromPave() { if (zoom > 0) { offx = xpav-XSize()/2/zoom; offy = ypav-YSize()/2/zoom; } else { offx = xpav+XSize()/2*zoom; offy = ypav+YSize()/2*zoom; } if (offx < 0) offx = 0; if (offx >= (img->XSize()-1)) offx = img->XSize()-2; if (offy < 0) offy = 0; if (offy >= (img->YSize()-1)) offy = img->YSize()-2; return; } /* --Methode-- */ void PIImage::CenterPave() { int xwsz, ywsz; if (zoom > 0) { xwsz = XSize()/zoom; ywsz = YSize()/zoom; } else { xwsz = -(XSize()*zoom); ywsz = -(YSize()*zoom); } if (xwsz > (img->XSize()-offx)) xwsz = (img->XSize()-offx); if (ywsz > (img->YSize()-offy)) ywsz = (img->YSize()-offy); xpav = offx+xwsz/2; ypav = offx+ywsz/2; xszpav = ((xwsz+ywsz)/20) ; if (xszpav < 1) xszpav = 1; yszpav = xszpav; return; }