void cImage_LumRas::CalculShadeByDiff()
{

   mImShade.Resize(mAppli.mImGr.sz());

   std::string aNameOut = mDir+ "LumRas_"+StdPrefix(mName) + ".tif";
   Tiff_Im TifTest
           (
                 aNameOut.c_str(),
                 mIm.sz(),
                 // GenIm::u_int1,
                 GenIm::real4,
                 Tiff_Im::No_Compr,
                 Tiff_Im::BlackIsZero
           );

    Fonc_Num aFRas  =  FLoc(6,50,mIm);
    Fonc_Num aFStd  =  FLoc(6,50,mAppli.mImGr);
    Tiff_Im::Create8BFromFonc("Test-Ras.tif",mIm.sz(),aFRas*100);
    Tiff_Im::Create8BFromFonc("Test-Std.tif",mIm.sz(),aFStd*100);
// Fonc_Num     cImage_LumRas::FLoc(int aNbIter,int aSzW,Fonc_Num aF)

   ELISE_COPY(mImShade.all_pts(),(aFRas-aFStd),mImShade.out());

   ELISE_COPY
   (
      TifTest.all_pts(),
      // Max(0,Min(255,128 * (1 + 2*mImShade.in()))),
      mImShade.in(),
      TifTest.out()
   );

}
void cMMTP::ContAndBoucheTrou()
{
   int aDist32Close = 6;
   int aNbErod = 6;

   // 1- Quelques fitre morpho de base, pour calculer les points eligibles au bouche-trou
   int aLabelOut = 0;
   //int aLabelIn = 1;
   int aLabelClose = 2;
   int aLabelFront = 3;

   ELISE_COPY(mImMasqInit.all_pts(),mImMasqInit.in(),mImLabel.out());
   ELISE_COPY(mImLabel.border(2),aLabelOut,mImLabel.out());

      // 1.1 calcul des point dans le fermeture
   ELISE_COPY
   (
          select
          (
             mImLabel.all_pts(),
             close_32(mImLabel.in(0),aDist32Close) && (mImLabel.in()==aLabelOut)
          ),
          aLabelClose,
          mImLabel.out()
   );
   ELISE_COPY(mImLabel.border(2),aLabelOut,mImLabel.out());


      // 1.2 erosion de ces points
   Neighbourhood V4 = Neighbourhood::v4();
   Neighbourhood V8 = Neighbourhood::v8();
   Neigh_Rel aRelV4(V4);

   Liste_Pts_U_INT2 aLFront(2);
   ELISE_COPY
   (
          select
          (
             mImLabel.all_pts(),
             (mImLabel.in(0)==aLabelClose) &&  (aRelV4.red_max(mImLabel.in(0)==aLabelOut))
          ),
          aLabelFront,
          mImLabel.out() | aLFront
    );
    for (int aK=0 ; aK<aNbErod ; aK++)
    {
        Liste_Pts_U_INT2 aLNew(2);
        ELISE_COPY
        (
               dilate
               (
                  aLFront.all_pts(),
                  mImLabel.neigh_test_and_set(Neighbourhood::v4(),2,3,20)
               ),
               aLabelFront,
               aLNew
         );
         aLFront = aLNew;
    }
    ELISE_COPY(select(mImLabel.all_pts(),mImLabel.in()==aLabelFront),0,mImLabel.out());

    // Au cas ou on ferait un export premature
    ELISE_COPY(mImMasqFinal.all_pts(),mImLabel.in()!=0,mImMasqFinal.out());

    int aSomMaskF;
    ELISE_COPY(mImMasqFinal.all_pts(),mImLabel.in()==1,sigma(aSomMaskF));
    if (aSomMaskF < 100) return;
    // std::cout << "aSomMaskFaSomMaskF " << aSomMaskF << "\n";
   // 2- Dequantifiication, adaptee au image a trou

       Im2D_REAL4 aProfCont(mSzTiep.x,mSzTiep.y,0.0);
       {
           Im2D_INT2 aPPV = BouchePPV(mImProf,mImLabel.in()==1);

           ElImplemDequantifier aDeq(mSzTiep);
           aDeq.DoDequantif(mSzTiep,aPPV.in());
           ELISE_COPY(aProfCont.all_pts(),aDeq.ImDeqReelle(),aProfCont.out());

           ELISE_COPY(select(aProfCont.all_pts(),mImLabel.in()!=1),0,aProfCont.out());
       }

       
    //Im2D_REAL4 aImInterp(mSzTiep.x,mSzTiep.y);
    TIm2D<REAL4,REAL8> aTInterp(mContBT);

   // 3- Bouchage "fin" des trour par moinde L2
          // 3.1 Valeur initial

                 // Filtrage gaussien
    Fonc_Num aFMasq = (mImLabel.in(0)==1);
    Fonc_Num aFProf = (aProfCont.in(0) * aFMasq);
    for (int aK=0 ; aK<3 ; aK++)
    {
          aFMasq = rect_som(aFMasq,1) /9.0;
          aFProf = rect_som(aFProf,1) /9.0;
    }

    ELISE_COPY
    (
         mContBT.all_pts(),
         aFProf / Max(aFMasq,1e-9),
         mContBT.out()
    );
                 // On remet la valeur init au point ayant un valeur propre
    ELISE_COPY
    (
         select(mContBT.all_pts(),mImLabel.in()==1),
         aProfCont.in(),
         mContBT.out()
    );
             // Et rien en dehors de l'image
    ELISE_COPY
    (
         select(mContBT.all_pts(),mImLabel.in()==0),
         0,
         mContBT.out()
    );
  
  
  
  
       // 3.2 Iteration pour regulariser les points interpoles
    {
         std::vector<Pt2di> aVInterp;
         {
            Pt2di aP;
            for (aP.x=0 ; aP.x<mSzTiep.x ; aP.x++)
            {
                for (aP.y=0 ; aP.y<mSzTiep.y ; aP.y++)
                {
                   if (mTLab.get(aP)==aLabelClose)
                     aVInterp.push_back(aP);
                }
            }
         }

         for (int aKIter=0 ; aKIter<20 ; aKIter++)
         {
              std::vector<double> aVVals;
              for (int aKP=0 ; aKP<int(aVInterp.size()) ; aKP++)
              {
                   double aSom=0;
                   double aSomPds = 0;
                   Pt2di aPK = aVInterp[aKP];
                   for (int aKV=0 ; aKV<9 ; aKV++)
                   {
                         Pt2di aVois = aPK+TAB_9_NEIGH[aKV];
                         if (mTLab.get(aVois)!=0)
                         {
                             int aPds = PdsGaussl9NEIGH[aKV];
                             aSom +=  aTInterp.get(aVois) * aPds;
                             aSomPds += aPds;
                         }
                   }
                   ELISE_ASSERT(aSomPds!=0,"Assert P!=0");
                   aVVals.push_back(aSom/aSomPds);
              }
              for (int aKP=0 ; aKP<int(aVInterp.size()) ; aKP++)
              {
                 aTInterp.oset(aVInterp[aKP],aVVals[aKP]);
              }
         }
    }
    
/*
*/

#ifdef ELISE_X11
           if(0 && TheWTiePCor)
           {

              ELISE_COPY
              (
                   mImLabel.all_pts(),
                   mContBT.in()*7,
                   TheWTiePCor->ocirc()
              );
              TheWTiePCor->clik_in();
              
              ELISE_COPY
              (
                  mImLabel.all_pts(),
                  nflag_close_sym(flag_front4(mImLabel.in(0)==1)),
                  TheWTiePCor->out_graph(Line_St(TheWTiePCor->pdisc()(P8COL::black)))
              );
              TheWTiePCor->clik_in();

              ELISE_COPY
              (
                  mImLabel.all_pts(),
                  mImLabel.in(0),
                  TheWTiePCor->odisc()
              );
              TheWTiePCor->clik_in();
              ELISE_COPY
              (
                  mImLabel.all_pts(),
                  mImMasqFinal.in(0),
                  TheWTiePCor->odisc()
              );
              TheWTiePCor->clik_in();
           }
#endif
}
void  cMMTP::ConputeEnveloppe(const cComputeAndExportEnveloppe & aCAEE,const cXML_ParamNuage3DMaille & aCurNuage)
{

   mNameTargetEnv = mAppli.WorkDir() + TheDIRMergeEPI()  +  mAppli.PDV1()->Name() + "/NuageImProf_LeChantier_Etape_1.xml";

   mNameTargetEnv = aCAEE.NuageExport().ValWithDef(mNameTargetEnv);
   cXML_ParamNuage3DMaille aTargetNuage = StdGetFromSI(mNameTargetEnv,XML_ParamNuage3DMaille);
   mZoomTargetEnv = aTargetNuage.SsResolRef().Val();
   mSzTargetEnv =  aTargetNuage.NbPixel();
   double aZoomRel = mAppli.CurEtape()->DeZoomTer()/mZoomTargetEnv;

   mBoxOutEnv._p0 = round_ni(Pt2dr(mBoxOutGlob._p0) * aZoomRel);
   mBoxOutEnv._p1 = round_ni(Pt2dr(mBoxOutGlob._p1) * aZoomRel);
   mBoxInEnv._p0 = round_ni(Pt2dr(mBoxInGlob._p0) * aZoomRel);
   mBoxInEnv._p1 = round_ni(Pt2dr(mBoxInGlob._p1) * aZoomRel);



   ELISE_ASSERT(mP0Tiep==Pt2di(0,0),"Too lazy to handle box maping");


   double aPasPx =  mAppli.CurEtape()->GeomTer().PasPxRel0();
//=============== READ PARAMS  ====================
   double  aStepSsEch = aCAEE.SsEchFilter().Val();
   int     aSzFiltrer = aCAEE.SzFilter().Val();
   double  aProp = aCAEE.ParamPropFilter().Val();

   int     aDistProl = round_up(  ElMax(aCAEE.ProlResolCur().Val(),aCAEE.ProlResolCible().Val()/aZoomRel) /aStepSsEch);
   double  aDistCum =  (aCAEE.ProlDistAddMax().Val()  / (aPasPx*  aZoomRel));
   double aDistAdd =   (aCAEE.ProlDistAdd().Val()*aStepSsEch )  / (aPasPx);

   std::cout << "DIST CUM " << aDistCum << " DADD " << aDistAdd << "\n";


//===================================

    ElTimer aChrono;

    int     aSeuilNbV = 2 * (1+2*aSzFiltrer); // Au moins une bande de 2 pixel pour inferer qqch
    Pt2di aSzRed = round_up(Pt2dr(mSzTiep)/aStepSsEch);

    Im2D_Bits<1>    aMasqRed(aSzRed.x,aSzRed.y,0);
    TIm2DBits<1>    aTMR(aMasqRed);
/*
    TIm2D<INT2,INT> aPMaxRed(aSzRed);
    TIm2D<INT2,INT> aPMinRed(aSzRed);
*/
    TIm2D<REAL4,REAL> aPMaxRed(aSzRed);
    TIm2D<REAL4,REAL> aPMinRed(aSzRed);

    // Calcul du filtre de reduction
    Pt2di aPRed;
    for (aPRed.y = 0 ; aPRed.y<aSzRed.y ; aPRed.y++)
    {
        for (aPRed.x = 0 ; aPRed.x<aSzRed.x ; aPRed.x++)
        {
             Pt2di aPR1 = round_ni(Pt2dr(aPRed)*aStepSsEch);
             int anX0 = ElMax(0,aPR1.x-aSzFiltrer);
             int anX1 = ElMin(mSzTiep.x-1,aPR1.x+aSzFiltrer);
             int anY0 = ElMax(0,aPR1.y-aSzFiltrer);
             int anY1 = ElMin(mSzTiep.y-1,aPR1.y+aSzFiltrer);
             std::vector<REAL> aVVals;
             Pt2di aVoisR1;
             for (aVoisR1.x=anX0 ; aVoisR1.x<=anX1 ; aVoisR1.x++)
             {
                  for (aVoisR1.y=anY0 ; aVoisR1.y<=anY1 ; aVoisR1.y++)
                  {
                     if (mTImMasqInit.get(aVoisR1))
                        aVVals.push_back( mTCBT.get(aVoisR1));
                        // aVVals.push_back( mTImProf.get(aVoisR1));
                  }
             }
             if (int(aVVals.size()) >= aSeuilNbV)
             {
                  REAL4 aVMax = KthValProp(aVVals,aProp);
                  REAL4 aVMin = KthValProp(aVVals,1-aProp);
                  aPMaxRed.oset(aPRed,aVMax);
                  aPMinRed.oset(aPRed,aVMin);
                  aTMR.oset(aPRed,1);
                  ELISE_ASSERT(aVMin<=aVMax,"Mic>Max !!!! in BasicMMTiep");
             }
             else
             {
                  aPMaxRed.oset(aPRed,-32000);
                  aPMinRed.oset(aPRed, 32000);
             }
        }
    }
    //Tiff_Im::Create8BFromFonc("TDifInit.tif",aSzRed,Max(0,Min(255,Iconv(aPMaxRed._the_im.in()-aPMinRed._the_im.in()))));

    Im2D_Bits<1> aNewM(1,1);
    Im2D_REAL4  aNewMax = ProlongByCont (aNewM,aMasqRed,aPMaxRed._the_im,aDistProl,aDistAdd,aDistCum);
    Im2D_REAL4  aNewMin = ProlongByCont (aNewM,aMasqRed,aPMinRed._the_im,aDistProl,-aDistAdd,aDistCum);
    ELISE_COPY(select(aNewM.all_pts(),!aNewM.in()),0,aNewMax.out()|aNewMin.out());

    // fChCo = Virgule(FX,FY)/ (aStepSsEch * aZoomRel);
    // fMasq = aNewM.in(0)[fChCo];
    // fMasqBin = fMasq>0.5;


    mDilatPlani = ElMax(aCAEE.DilatPlaniCible().Val(),round_up(aCAEE.DilatPlaniCur().Val()*aZoomRel));
    mDilatAlti  = ElMax(aCAEE.DilatAltiCible ().Val(),round_up(aCAEE.DilatPlaniCur().Val()*aZoomRel));
    
    DoOneEnv(aNewMax,aNewM,true ,aTargetNuage,aCurNuage,aStepSsEch * aZoomRel);
    DoOneEnv(aNewMin,aNewM,false,aTargetNuage,aCurNuage,aStepSsEch * aZoomRel);


    Fonc_Num  aFMasqBin;
    Fonc_Num fChCo = Virgule(FX,FY)/ aZoomRel;

std::cout  << "ZRRRR  " << aZoomRel <<  " 1/Z " << (1/aZoomRel) 
           <<   " ;; " << mAppli.CurEtape()->DeZoomTer() << " , " << mZoomTargetEnv << "\n";
// Tiff_Im::CreateFromIm(mContBT,DirOfFile(mNameTargetEnv)+"CONTBT.tif");


    Fonc_Num aFoncProf = FoncChCoordWithMasq(mContBT.in(0),mImMasqFinal.in(0),fChCo,0,aFMasqBin);
    aFoncProf = ::AdaptDynOut(aFoncProf,aTargetNuage,aCurNuage);

    Tiff_Im aFileProf = FileEnv("Depth",false);
    ELISE_COPY(rectangle(mBoxOutEnv._p0,mBoxOutEnv._p1),trans(aFoncProf,-mBoxInEnv._p0),aFileProf.out());


    Tiff_Im aFileMasq = FileEnv("Masq",true);
    ELISE_COPY(rectangle(mBoxOutEnv._p0,mBoxOutEnv._p1),trans(aFMasqBin,-mBoxInEnv._p0),aFileMasq.out());


#ifdef ELISE_X11
   if (0 && TheWTiePCor)
   {
       ELISE_COPY(TheWTiePCor->all_pts(),aFMasqBin,TheWTiePCor->odisc());
       std::cout << "AAAAAAAAAAAAAAAAAAAAa\n";
       TheWTiePCor->clik_in();
       ELISE_COPY(TheWTiePCor->all_pts(),aFileMasq.in(),TheWTiePCor->odisc());
       std::cout << "bbBBbbBBBBBBBbbb\n";
       TheWTiePCor->clik_in();
   }
#endif
}