/* The copyright in this software is being made available under the BSD
 * License, included below. This software may be subject to other third party
 * and contributor rights, including patent rights, and no such rights are
 * granted under this license.  
 *
 * Copyright (c) 2010-2012, ITU/ISO/IEC
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  * Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
 *    be used to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */



/*
* In addition to the orignal authors, this software was further modified by the following parties:
*
* Authors:
*      Lu  Yu,    yul@zju.edu.cn (Zhejiang University)
*      Yin Zhao,  zhao87099664@163.com  (Zhejiang University)
*      <new contributor name>, <email>, <affiliation>
*
* The related parities retain full right to their code for their own purpose, assign or donate the cooresponding
* code to another party and to inhibit third parties from using the code for products that do not conform 
* to MPEG-related and/or ISO/IEC International Standards. 
*
*/


#include "ViewSynDef.h"
#include "PictureResample.h"
#if POZNAN_NONLINEAR_DEPTH
extern "C"
{
  #include "nonlinear_depth.h"
}
#endif
#include "Visbd.h"

#if NTT_SUBPEL_VSP_M24796
#if NTT_VSP_HARMONIZATION_M25195
#define INTERPOLATE_RIGHT_SIDE3(x, y, i) {\
                p0 = CLIP((x)-2, 0, y);\
                p1 = CLIP((x)-1, 0, y);\
                p2 = CLIP((x)  , 0, y);\
                p3 = CLIP((x)+1, 0, y);\
                p4 = CLIP((x)+2, 0, y);\
                p5 = CLIP((x)+3, 0, y);\
                x_far[i]  = p3;\
                x_near[i] = p2;      }
#define INTERPOLATE_LEFT_SIDE3(x, y, i) {\
                p0 = CLIP((x)-3, 0, y);\
                p1 = CLIP((x)-2, 0, y);\
                p2 = CLIP((x)-1, 0, y);\
                p3 = CLIP((x)  , 0, y);\
                p4 = CLIP((x)+1, 0, y);\
                p5 = CLIP((x)+2, 0, y);\
                x_far[i]  = p2;\
                x_near[i] = p3;    }
#endif // NTT_VSP_HARMONIZATION_M25195
#define INTERPOLATE_RIGHT_SIDE(x, y) {\
                p0 = CLIP((x)-2, 0, y);\
                p1 = CLIP((x)-1, 0, y);\
                p2 = CLIP((x)  , 0, y);\
                p3 = CLIP((x)+1, 0, y);\
                p4 = CLIP((x)+2, 0, y);\
                p5 = CLIP((x)+3, 0, y);\
                x_far  = p3;\
                x_near = p2;      }
#define INTERPOLATE_LEFT_SIDE(x, y) {\
                p0 = CLIP((x)-3, 0, y);\
                p1 = CLIP((x)-2, 0, y);\
                p2 = CLIP((x)-1, 0, y);\
                p3 = CLIP((x)  , 0, y);\
                p4 = CLIP((x)+1, 0, y);\
                p5 = CLIP((x)+2, 0, y);\
                x_far  = p2;\
                x_near = p3;    }
#define AVC_HALF_PEL(pt) ((*(PixelValueMul20+*(pt+p2)) + *(PixelValueMul20+*(pt+p3)) - *(PiXelValueMul5+*(pt+p1)) - *(PiXelValueMul5+*(pt+p4)) + *(pt+p0)+ *(pt+p5)+16)>>5)
#endif
#if NOKIA_CHROMA_BILINEAR
#define BILINEAR1(pt, near, far) ( ((*(pt+near))*3 + (*(pt+far))   + 2) >> 2 )
#define BILINEAR2(pt, near, far) ( ((*(pt+near))   + (*(pt+far))   + 1) >> 1 )
#define BILINEAR3(pt, near, far) ( ((*(pt+near))   + (*(pt+far))*3 + 2) >> 2 )
#endif

CViewInterpolation1D ViewSyn1DMode;

CViewInterpolation1D::CViewInterpolation1D()
{
  int i=0;

  Width = 1024;
  Height = 768;
#if NTT_SUBPEL_VSP_M24796
  SubPelOption = 4;
  UpsampleRefs = 1;
#else
  SubPelOption = 2;
  UpsampleRefs = 2;
#endif
#if VSP_OPTIMIZATION
  MergingOption = 0;
#else
  MergingOption = 2;
#endif
  SplattingOption = 2;

  FrameNumber=0;

#if NOKIA_DISP_CALC_B0150
  DisparityLUTL=NULL;
  DisparityLUTR=NULL;
#else
  FocalLength = 1800;
  LTranslation[LEFTVIEW] = 50;
  LTranslation[RGHTVIEW] = -50;

  duPrincipal[LEFTVIEW] = duPrincipal[RGHTVIEW] = 0;
  Znear[LEFTVIEW] = 40;
  Zfar[LEFTVIEW] = 120;
  Znear[RGHTVIEW] = 40;
  Zfar[RGHTVIEW] = 120;
#endif
  DepthThreshold = 75;
  HoleCountThreshold = 30;
  BoundaryGrowth = 40;
  TemporalImprovementOption = 1; // Zhejiang, May, 4
  WarpEnhancementOption = 1;
  CleanNoiseOption = 1;
  SplattingDepthThreshold = 255;

  MemAllocated=false;

  for (i = 0; i < 2; i++)
  {
    BoundaryMask[i] = NULL;
    RefView[i][0]= NULL; //!<Y
    RefView[i][1]=NULL;  //!<U
    RefView[i][2]=NULL;  //!<V
    RefDepth[i] = NULL;
    RefViewLast[i] = NULL; //Zhejiang
    RefDepthLast[i] = NULL;
  }
  for (i = 0; i < 3; i++)
  {
    Mask[i] = NULL;
    Dmap[i] = NULL;
  }
  for (i = 0; i < 5; i++)
  {
    SynY[i] = NULL;
    SynU[i] = NULL;
    SynV[i] = NULL;
  }

#if NTT_SUBPEL_VSP_M24796
  for(i=0;i<PIXEL_VALUE_RANGE;++i)
  {
    PiXelValueMul5[i]=i*5;
    PixelValueMul20[i]=i*20;
  }
#endif
}

CViewInterpolation1D::~CViewInterpolation1D()
{
  if(MemAllocated)
    FreeMem();
}

#if POZNAN_NONLINEAR_DEPTH
void CViewInterpolation1D::SetNonLinearDepth(int nonlinear_depth_num, char nonlinear_depth_points[256])
{      
   nonlinear_depth_init_lut ( nonlinear_depth_lut_backward, nonlinear_depth_num, nonlinear_depth_points, 0 );
   nonlinear_depth_init_lut ( nonlinear_depth_lut_forward,  nonlinear_depth_num, nonlinear_depth_points, 1 );
};
#endif
#if NOKIA_DISP_CALC_B0150
int  CViewInterpolation1D::InitViewInterpolation1D(ViewSynParams *pViewSynParams)
#else
int  CViewInterpolation1D::InitViewInterpolation1D(ViewSynParams *pViewSynParams,CameraParameters* pCamParams)
#endif
{
#if POZNAN_NONLINEAR_DEPTH
   SetNonLinearDepth(pViewSynParams->NonlinearDepthNum, pViewSynParams->NonlinearDepthPoints);
#endif
   SetWidth(pViewSynParams->SourceWidth);
   SetHeight(pViewSynParams->SourceHeight);
#if NOKIA_DISP_CALC_B0150
   SetDisparityLUTL(pViewSynParams->disparity_lut_left);
   SetDisparityLUTR(pViewSynParams->disparity_lut_right);
#else
   SetZnearL(pViewSynParams->ZLeftNearestDepthValue);
   SetZfarL(pViewSynParams->ZLeftFarthestDepthValue);
   SetZnearR(pViewSynParams->ZRightNearestDepthValue);
   SetZfarR(pViewSynParams->ZRightFarthestDepthValue);
   SetFocalLength(pCamParams[0].m_fIntrinsicMatrix[0][0]);
   SetLTranslationLeft(pCamParams[1].m_fTranslationVector[0]
   -pCamParams[0].m_fTranslationVector[0]);
   SetLTranslationRight(pCamParams[1].m_fTranslationVector[0]
   -pCamParams[2].m_fTranslationVector[0]);
   SetduPrincipalLeft(pCamParams[1].m_fIntrinsicMatrix[0][2]
   -pCamParams[0].m_fIntrinsicMatrix[0][2]);
   SetduPrincipalRight(pCamParams[1].m_fIntrinsicMatrix[0][2]
   -pCamParams[2].m_fIntrinsicMatrix[0][2]);
#endif

#if NTT_SUBPEL_VSP_M24796
   SubPelOption=4;
   UpsampleRefs=1;
#else
   SubPelOption=2;
   UpsampleRefs=2;
#endif
   SplattingOption=2;
   
   BoundaryGrowth=40;
#if VSP_OPTIMIZATION
   SynDirection=pViewSynParams->SynDirection;
   MergingOption=0;
#else
   MergingOption = 2;
#endif
   DepthThreshold=75;
   HoleCountThreshold=30;
   TemporalImprovementOption=0;
   WarpEnhancementOption= 1;

   return 1;
}
#if QC_VSP_Asymmetric_Resolution
  int  CViewInterpolation1D::AllocMem( Boolean bMixedRes)
#else
  int  CViewInterpolation1D::AllocMem()
#endif
{
  int i;
#if !NOKIA_DISP_CALC_B0150
  double left_weight_db=0.0;
#endif

  if(MemAllocated)
    return 1;

  Width2 = Width*UpsampleRefs;

#if (!QCOM_VSP || (ZJU_VSP && BOUNDARY_DETECT_ATM02))
  //BoundaryMask[*] is now the size of Width2*Height (previously, Width*Height)

  // When splattingOption==2 or CleanNoiseOption==1
  if (SplattingOption == 2 || CleanNoiseOption == 1)
  {
#if VSP_OPTIMIZATION
    if(((SynDirection==0)||(SynDirection==2))&&(BoundaryMask[0]==NULL)){
#if QC_VSP_Asymmetric_Resolution
      if( bMixedRes )
        get_mem2D(&(BoundaryMask[0]),Height/2,Width2/2);
      else
#endif
        get_mem2D(&(BoundaryMask[0]),Height,Width2);
    }
    if(((SynDirection==1)||(SynDirection==2))&&(BoundaryMask[1]==NULL)) {
#if QC_VSP_Asymmetric_Resolution
      if ( bMixedRes )
        get_mem2D(&(BoundaryMask[1]),Height/2,Width2/2);
      else
#endif
      get_mem2D(&(BoundaryMask[1]),Height,Width2);
    }
#else
    for (i = 0; i < 2; i++)
    {
      if (BoundaryMask[i] == NULL)
      {
#if QC_VSP_Asymmetric_Resolution
      if ( bMixedRes )
        get_mem2D(&(BoundaryMask[i]),Height/2,Width2/2);
      else
#endif
        get_mem2D(&(BoundaryMask[i]),Height,Width2);
      }
    }
#endif
  }
#endif


#if NTT_SUBPEL_VSP_M24796
#if VSP_OPTIMIZATION
  if(SynDirection==2)
  {
#if !QCOM_VSP
    for (i = 0; i < 3; i++)
    {
      if (Mask[i] == NULL)
      {
        get_mem2D(&(Mask[i]),Height,Width);
      }
    }
#endif
    for (i = 0; i < 4; i++)
    {
      if (SynY[i] == NULL)
      {
        get_mem2D(&(SynY[i]),Height,Width);
      }
#if QC_VSP_Asymmetric_Resolution
      if ( bMixedRes )
      {
        if (SynU[i] == NULL)
        {
          get_mem2D(&(SynU[i]),Height/2,Width/2);
        }
        if (SynV[i] == NULL)
        {
          get_mem2D(&(SynV[i]),Height/2,Width/2);
        }
        if (Dmap[i] == NULL)
        {
          get_mem2D(&(Dmap[i]),Height/2,Width/2);
        }
      }
      else
#endif
      {
        if (SynU[i] == NULL)
        {
          get_mem2D(&(SynU[i]),Height,Width);
        }
        if (SynV[i] == NULL)
        {
          get_mem2D(&(SynV[i]),Height,Width);
        }
        if (Dmap[i] == NULL)
        {
          get_mem2D(&(Dmap[i]),Height,Width);
        }
      }
    }
  }
#else
  for (i = 0; i < 3; i++)
  {
    if (Mask[i] == NULL)
    {
      get_mem2D(&(Mask[i]),Height,Width);
    }
  }
  for (i = 0; i < 4; i++)
  {
    if (SynY[i] == NULL)
    {
      get_mem2D(&(SynY[i]),Height,Width);
    }
    if (SynU[i] == NULL)
    {
#if QC_VSP_Asymmetric_Resolution
      if(bMixedRes)
      get_mem2D(&(SynU[i]),Height/2,Width/2);
      else
#endif
      get_mem2D(&(SynU[i]),Height,Width);
    }
    if (SynV[i] == NULL)
    {
#if QC_VSP_Asymmetric_Resolution
      if(bMixedRes)
        get_mem2D(&(SynV[i]),Height/2,Width/2);
      else
#endif
      get_mem2D(&(SynV[i]),Height,Width);
    }
    if (Dmap[i] == NULL)
    {
#if QC_VSP_Asymmetric_Resolution
      if(bMixedRes)
        get_mem2D(&(Dmap[i]),Height/2,Width/2);
      else
#endif
      get_mem2D(&(Dmap[i]),Height,Width);
    }
  }
#endif

  get_mem2D(&(SynY[FINLVIEW]),Height,Width);
#if (QC_VSP_Asymmetric_Resolution) // NTT
  if(bMixedRes)
    get_mem2D(&(Dmap[FINLVIEW]),Height/2,Width/2);
  else
#endif
    get_mem2D(&(Dmap[FINLVIEW]),Height,Width);

  if(SynDirection==2)
  {
#if QC_VSP_Asymmetric_Resolution
    if(bMixedRes)
    {
      get_mem2D(&(SynU[FINLVIEW]),Height/2,Width/2); // unnecessary?
      get_mem2D(&(SynV[FINLVIEW]),Height/2,Width/2); // unnecessary?
    }
    else
#endif
    {
      get_mem2D(&(SynU[FINLVIEW]),Height,Width);
      get_mem2D(&(SynV[FINLVIEW]),Height,Width);
    }
  }
#if QC_VSP_Asymmetric_Resolution// NTT
  else if(!bMixedRes)
#else
  else
#endif
  {
    get_mem2D(&(SynU[FINLVIEW]),Height,Width);
    get_mem2D(&(SynV[FINLVIEW]),Height,Width);
  }

#if VSP_OPTIMIZATION
  if(SynDirection==2)
  {
    for (i = 0; i < 2; i++)
    {
#if QC_VSP_Asymmetric_Resolution
      if (RefView[i][1] == NULL && !bMixedRes)
#else
      if (RefView[i][1] == NULL)
#endif
      {
        get_mem2D(&(RefView[i][1]),Height,Width);
        get_mem2D(&(RefView[i][2]),Height,Width);
      }
    }
  }
  else if(SynDirection==1)
  {
#if QC_VSP_Asymmetric_Resolution
    if(!RefView[1][1] && !bMixedRes)
#else
    if(!RefView[1][0])
#endif
    {
      get_mem2D(&(RefView[1][1]),Height,Width);
      get_mem2D(&(RefView[1][2]),Height,Width);
    }
  }
  else if(SynDirection==0)
  {
#if QC_VSP_Asymmetric_Resolution
    if(!RefView[0][1] && !bMixedRes)
#else
    if(!RefView[0][1])
#endif
    {
      get_mem2D(&(RefView[0][1]),Height,Width);
      get_mem2D(&(RefView[0][2]),Height,Width);
    }
  }
#else // (VSP_OPTIMIZATION)
  for (i = 0; i < 2; i++)
  {
#if QC_VSP_Asymmetric_Resolution
    if (RefView[i][1] == NULL && !bMixedRes)
#else
    if (RefView[i][1] == NULL)
#endif
    {
      get_mem2D(&(RefView[i][1]),Height,Width);
      get_mem2D(&(RefView[i][2]),Height,Width);
    }
  }
#endif // (VSP_OPTIMIZATION)

#else // NTT_SUBPEL_VSP_M24796

#if VSP_OPTIMIZATION
  if(SynDirection==2)
  {
#if !QCOM_VSP
    for (i = 0; i < 3; i++)
    {
      if (Mask[i] == NULL)
      {
        get_mem2D(&(Mask[i]),Height,Width2*SubPelOption);
      }
    }
#endif
    for (i = 0; i < 4; i++)
    {
      if (SynY[i] == NULL)
      {
        get_mem2D(&(SynY[i]),Height,Width2*SubPelOption);
      }
#if QC_VSP_Asymmetric_Resolution
      if ( bMixedRes )
      {
        if (SynU[i] == NULL)
        {
          get_mem2D(&(SynU[i]),Height/2,Width2*SubPelOption/2);
        }
        if (SynV[i] == NULL)
        {
          get_mem2D(&(SynV[i]),Height/2,Width2*SubPelOption/2);
        }
        if (Dmap[i] == NULL)
        {
          get_mem2D(&(Dmap[i]),Height/2,Width2*SubPelOption/2);
        }
      }else{
#endif
        if (SynU[i] == NULL)
        {
          get_mem2D(&(SynU[i]),Height,Width2*SubPelOption);
        }
        if (SynV[i] == NULL)
        {
          get_mem2D(&(SynV[i]),Height,Width2*SubPelOption);
        }
        if (Dmap[i] == NULL)
        {
          get_mem2D(&(Dmap[i]),Height,Width2*SubPelOption);
        }
#if QC_VSP_Asymmetric_Resolution
      }
#endif
    }
  }
  else if(SynDirection==0)
  {
#if !QCOM_VSP
    if(!Mask[0])
      get_mem2D(&(Mask[0]),Height,Width2*SubPelOption);
#endif
    if(!SynY[0])
      get_mem2D(&(SynY[0]),Height,Width2*SubPelOption);

#if QC_VSP_Asymmetric_Resolution
    if ( bMixedRes )
    {
      if(!SynU[0])
        get_mem2D(&(SynU[0]),Height/2,Width2*SubPelOption/2);
      if(!SynV[0])
        get_mem2D(&(SynV[0]),Height/2,Width2*SubPelOption/2);
      if(!Dmap[0])
        get_mem2D(&(Dmap[0]),Height/2,Width2*SubPelOption/2);
    }else{
#endif
      if(!SynU[0])
        get_mem2D(&(SynU[0]),Height,Width2*SubPelOption);
      if(!SynV[0])
        get_mem2D(&(SynV[0]),Height,Width2*SubPelOption);
      if(!Dmap[0])
        get_mem2D(&(Dmap[0]),Height,Width2*SubPelOption);
#if QC_VSP_Asymmetric_Resolution
    }
#endif
  }
  else if(SynDirection==1)
  {
#if !QCOM_VSP
    if(Mask[1]==NULL)
      get_mem2D(&(Mask[1]),Height,Width2*SubPelOption);
#endif
    if(!SynY[1])
      get_mem2D(&(SynY[1]),Height,Width2*SubPelOption);
#if QC_VSP_Asymmetric_Resolution
    if ( bMixedRes )
    {
      if(!SynU[1])
        get_mem2D(&(SynU[1]),Height/2,Width2*SubPelOption/2);
      if(!SynV[1])
        get_mem2D(&(SynV[1]),Height/2,Width2*SubPelOption/2);
      if(!Dmap[1])
        get_mem2D(&(Dmap[1]),Height/2,Width2*SubPelOption/2);
    }else{
#endif
      if(!SynU[1])
        get_mem2D(&(SynU[1]),Height,Width2*SubPelOption);
      if(!SynV[1])
        get_mem2D(&(SynV[1]),Height,Width2*SubPelOption);
      if(!Dmap[1])
        get_mem2D(&(Dmap[1]),Height,Width2*SubPelOption);
#if QC_VSP_Asymmetric_Resolution
    }
#endif
  }
  get_mem2D(&(SynY[3]),Height,Width2*SubPelOption);
#if QC_VSP_Asymmetric_Resolution
  if(bMixedRes){
    get_mem2D(&(SynU[3]),Height/2,Width2*SubPelOption/2);
    get_mem2D(&(SynV[3]),Height/2,Width2*SubPelOption/2);
    get_mem2D(&(Dmap[3]),Height/2,Width2*SubPelOption/2);
  }
  else
  {
#endif
    get_mem2D(&(SynU[3]),Height,Width2*SubPelOption);
    get_mem2D(&(SynV[3]),Height,Width2*SubPelOption);
    get_mem2D(&(Dmap[3]),Height,Width2*SubPelOption);
#if QC_VSP_Asymmetric_Resolution
  }
#endif
#else
  for (i = 0; i < 3; i++)
  {
    if (Mask[i] == NULL)
    {
      get_mem2D(&(Mask[i]),Height,Width2*SubPelOption);
    }
  }
  for (i = 0; i < 4; i++)
  {
    if (SynY[i] == NULL)
    {
      get_mem2D(&(SynY[i]),Height,Width2*SubPelOption);
    }
    if (SynU[i] == NULL)
    {
#if QC_VSP_Asymmetric_Resolution
      if(bMixedRes)
      get_mem2D(&(SynU[i]),Height/2,Width2*SubPelOption/2);
      else
#endif
      get_mem2D(&(SynU[i]),Height,Width2*SubPelOption);
    }
    if (SynV[i] == NULL)
    {
#if QC_VSP_Asymmetric_Resolution
      if(bMixedRes)
        get_mem2D(&(SynV[i]),Height/2,Width2*SubPelOption/2);
      else
#endif
      get_mem2D(&(SynV[i]),Height,Width2*SubPelOption);
    }
    if (Dmap[i] == NULL)
    {
#if QC_VSP_Asymmetric_Resolution
      if(bMixedRes)
        get_mem2D(&(Dmap[i]),Height/2,Width2*SubPelOption/2);
      else
#endif
      get_mem2D(&(Dmap[i]),Height,Width2*SubPelOption);
    }
  }
#endif
  get_mem2D(&(SynY[FINLVIEW]),Height,Width2);
#if QC_VSP_Asymmetric_Resolution
  if(bMixedRes){
    get_mem2D(&(SynU[FINLVIEW]),Height/2,Width2/2);
    get_mem2D(&(SynV[FINLVIEW]),Height/2,Width2/2);
    get_mem2D(&(Dmap[FINLVIEW]),Height/2,Width2/2);
  }else{
#endif
    get_mem2D(&(SynU[FINLVIEW]),Height,Width2);
    get_mem2D(&(SynV[FINLVIEW]),Height,Width2);
    get_mem2D(&(Dmap[FINLVIEW]),Height,Width2);
#if QC_VSP_Asymmetric_Resolution
  }
#endif

#if VSP_OPTIMIZATION
  if(SynDirection==2)
  {
    for (i = 0; i < 2; i++)
    {
      if (RefView[i][0] == NULL)
      {
        get_mem2D(&(RefView[i][0]),Height,Width2);
#if QC_VSP_Asymmetric_Resolution
        if(bMixedRes){
          get_mem2D(&(RefView[i][1]),Height/2,Width2/2);
          get_mem2D(&(RefView[i][2]),Height/2,Width2/2);
        }else{
#endif
          get_mem2D(&(RefView[i][1]),Height,Width2);
          get_mem2D(&(RefView[i][2]),Height,Width2);
#if QC_VSP_Asymmetric_Resolution
        }
#endif
      }
      if (RefDepth[i] == NULL)
      {
#if QC_VSP_Asymmetric_Resolution
        if(bMixedRes)
          get_mem2D(&(RefDepth[i]),Height/2,Width2/2);
        else
#endif
        get_mem2D(&(RefDepth[i]),Height,Width2);
      }
    }
  }
  else if(SynDirection==1)
  {
    if(!RefView[1][0])
    {
      get_mem2D(&(RefView[1][0]),Height,Width2);
#if QC_VSP_Asymmetric_Resolution
      if(bMixedRes){
        get_mem2D(&(RefView[1][1]),Height/2,Width2/2);
        get_mem2D(&(RefView[1][2]),Height/2,Width2/2);
      }else{
#endif
      get_mem2D(&(RefView[1][1]),Height,Width2);
      get_mem2D(&(RefView[1][2]),Height,Width2);
#if QC_VSP_Asymmetric_Resolution
      }
#endif
    }
    if(!RefDepth[1]){
#if QC_VSP_Asymmetric_Resolution
    if(bMixedRes)
      get_mem2D(&(RefDepth[1]),Height/2,Width2/2);
    else
#endif
      get_mem2D(&(RefDepth[1]),Height,Width2);
    }
  }
  else if(SynDirection==0)
  {
    if(!RefView[0][0])
    {
      get_mem2D(&(RefView[0][0]),Height,Width2);
#if QC_VSP_Asymmetric_Resolution
      if(bMixedRes){
        get_mem2D(&(RefView[0][1]),Height/2,Width2/2);
        get_mem2D(&(RefView[0][2]),Height/2,Width2/2);
      }else{
#endif
        get_mem2D(&(RefView[0][1]),Height,Width2);
        get_mem2D(&(RefView[0][2]),Height,Width2);
#if QC_VSP_Asymmetric_Resolution
      }
#endif
    }
    if(!RefDepth[0]){ //original was RefDepth[1], is it a bug?
#if QC_VSP_Asymmetric_Resolution
      if(bMixedRes)
        get_mem2D(&(RefDepth[0]),Height/2, Width2/2);
      else
#endif
      get_mem2D(&(RefDepth[0]),Height,Width2);

    }
  }
#else
  for (i = 0; i < 2; i++)
  {
    if (RefView[i][0] == NULL)
    {
      get_mem2D(&(RefView[i][0]),Height,Width2);
#if QC_VSP_Asymmetric_Resolution
      if(bMixedRes){
        get_mem2D(&(RefView[i][1]),Height/2,Width2/2);
        get_mem2D(&(RefView[i][2]),Height/2,Width2/2);
      }else{
#endif
        get_mem2D(&(RefView[i][1]),Height,Width2);
        get_mem2D(&(RefView[i][2]),Height,Width2);
#if QC_VSP_Asymmetric_Resolution
      }
#endif
    }
    if (RefDepth[i] == NULL)
    {
#if QC_VSP_Asymmetric_Resolution
      if(bMixedRes)
        get_mem2D(&(RefDepth[i]),Height/2,Width2/2);
      else
#endif
      get_mem2D(&(RefDepth[i]),Height,Width2);
    }
  }
#endif

#endif  // NTT_SUBPEL_VSP_M24796


  //Alloc Memory for TIM and cleaning boundary noise, Zhejiang
  if(TemporalImprovementOption)
  {
    for(i=0; i<2; i++)
    {
      if (RefViewLast[i] == NULL) 
      {
        get_mem2D(&(RefViewLast[i]),Height,Width);
      }
      if (RefDepthLast[i] == NULL)
      {
#if QC_VSP_Asymmetric_Resolution
        if(bMixedRes)
          get_mem2D(&(RefDepthLast[i]), Height/2, Width/2);
        else
#endif
        get_mem2D(&(RefDepthLast[i]),Height,Width);
      }
    }
  }

#if VSP_OPTIMIZATION
#if NOKIA_DISP_CALC_B0150
  WeightLeft=abs(DisparityLUTL[0][255]<<16)/(abs(DisparityLUTL[0][255])+abs(DisparityLUTR[0][255]));
#else
  left_weight_db=fabs(LTranslation[RGHTVIEW]/(fabs(LTranslation[LEFTVIEW])+fabs(LTranslation[RGHTVIEW])));
  WeightLeft=(int)(65536*left_weight_db);
#endif

  WeightRight=65536-WeightLeft;

  for(i=0;i<PIXEL_VALUE_RANGE;++i)
  {
#if NOKIA_DISP_CALC_B0150
    WeightLeftTable[i]=WeightLeft*i;
    WeightRightTable[i]=WeightRight*i;

#if NTT_SUBPEL_VSP_M24796
    int db_disparity, db_disparity_subpel;
#if (QC_VSP_Asymmetric_Resolution & !NTT_VSP_HARMONIZATION_M25195) // NTT
    int db_disparity_uv_subpel;
#endif
#endif
#else
    double z;
    WeightLeftTable[i]=WeightLeft*i;
    WeightRightTable[i]=WeightRight*i;
#if POZNAN_NONLINEAR_DEPTH
    int in = nonlinear_depth_lut_backward[i];
#else
    int in = i;
#endif
#if NTT_SUBPEL_VSP_M24796
    double db_disparity, db_disparity_subpel;
#if (QC_VSP_Asymmetric_Resolution & !NTT_VSP_HARMONIZATION_M25195) // NTT
    double db_disparity_uv_subpel;
#endif
#endif

    z=1.0/( (in/255.0) * (1/Znear[0] - 1/Zfar[0]) + (1/Zfar[0]) );
#endif
#if NTT_SUBPEL_VSP_M24796
#if NOKIA_DISP_CALC_B0150
    db_disparity = -DisparityLUTL[0][i]/4;
    assert( SubPelOption==2 || SubPelOption==4 );
    db_disparity_subpel = (SubPelOption==4) ? (-DisparityLUTL[0][i]) : (-DisparityLUTL[1][i]);
#else
    db_disparity        = (FocalLength*LTranslation[0]/z)-duPrincipal[0];
    db_disparity_subpel = db_disparity*SubPelOption;
#endif
#if (QC_VSP_Asymmetric_Resolution & !NTT_VSP_HARMONIZATION_M25195) // NTT
    if(bMixedRes)
    {
#if NOKIA_DISP_CALC_B0150
      db_disparity_uv_subpel = (SubPelOption==4)? (-DisparityLUTL[1][i]) : db_disparity;
      DisparityLookupTable[0][i].targetUV      = db_disparity/2;
      DisparityLookupTable[0][i].referenceUV   = abs(db_disparity_uv_subpel)-abs(DisparityLookupTable[0][i].targetUV)*SubPelOption;
#else
      db_disparity_uv_subpel = db_disparity_subpel/2;
      DisparityLookupTable[0][i].targetUV      = (int)(db_disparity/2);
      DisparityLookupTable[0][i].referenceUV   = (int)(fabs(db_disparity_uv_subpel)+0.5)-abs(DisparityLookupTable[0][i].targetUV)*SubPelOption;
#endif
      if(DisparityLookupTable[0][i].referenceUV==SubPelOption)
      {
        DisparityLookupTable[0][i].referenceUV = 0;
        DisparityLookupTable[0][i].targetUV += (db_disparity<0?-1:1);
      }
      if(SubPelOption==2) DisparityLookupTable[0][i].referenceUV *= 2;
#if NOKIA_DISP_CALC_B0150
      DisparityLookupTable[0][i].referenceY[0] = db_disparity - DisparityLookupTable[0][i].targetUV*2;
      DisparityLookupTable[0][i].referenceY[1] = abs(db_disparity_subpel)-abs(db_disparity)*SubPelOption;
#else
      DisparityLookupTable[0][i].referenceY[0] = ((int)db_disparity) - DisparityLookupTable[0][i].targetUV*2;
      DisparityLookupTable[0][i].referenceY[1] = (int)(fabs(db_disparity_subpel)+0.5)-abs(((int)db_disparity))*SubPelOption;
#endif
      if(DisparityLookupTable[0][i].referenceY[1]==SubPelOption)
      {
        DisparityLookupTable[0][i].referenceY[1] = 0;
        DisparityLookupTable[0][i].referenceY[0] += (db_disparity<0?-1:1);
      }
      if(SubPelOption==2) DisparityLookupTable[0][i].referenceY[1] *= 2;
    }
    else
#endif
    {
#if NOKIA_DISP_CALC_B0150
      DisparityLookupTable[0][i].integer    = db_disparity;
      DisparityLookupTable[0][i].fractional = abs(db_disparity_subpel)-abs(DisparityLookupTable[0][i].integer)*SubPelOption;
#else
      DisparityLookupTable[0][i].integer    = (int)db_disparity;
      DisparityLookupTable[0][i].fractional = (int)(fabs(db_disparity_subpel)+0.5)-abs(DisparityLookupTable[0][i].integer)*SubPelOption;
#endif
      if(DisparityLookupTable[0][i].fractional==SubPelOption) {
        DisparityLookupTable[0][i].fractional = 0;
        DisparityLookupTable[0][i].integer += (db_disparity<0?-1:1);
      }
      if(SubPelOption==2) DisparityLookupTable[0][i].fractional *= 2;
#if NTT_VSP_HARMONIZATION_M25195
      db_disparity        /= 2;
      db_disparity_subpel /= 2;
      DisparityLookupTable[0][i].integerUV    = (int)db_disparity;
      DisparityLookupTable[0][i].fractionalUV = (int)(fabs(db_disparity_subpel)+0.5)-abs(DisparityLookupTable[0][i].integerUV)*4;
      if(DisparityLookupTable[0][i].fractionalUV==SubPelOption)
      {
        DisparityLookupTable[0][i].fractionalUV = 0;
        DisparityLookupTable[0][i].integerUV += (db_disparity<0?-1:1);
      }
      if(SubPelOption==2) DisparityLookupTable[0][i].fractionalUV *= 2;
#endif
    }
#else
    DisparityLookupTable[0][i].db_disparity=(FocalLength*LTranslation[0]/z)-duPrincipal[0];
    DisparityLookupTable[0][i].db_disparity_mul2=DisparityLookupTable[0][i].db_disparity*2;
    DisparityLookupTable[0][i].db_disparity_mul4=DisparityLookupTable[0][i].db_disparity*4;
    DisparityLookupTable[0][i].int_disparity=(int)floor(DisparityLookupTable[0][i].db_disparity+0.5);
#endif

#if !NOKIA_DISP_CALC_B0150
    z=1.0/( (in/255.0) * (1/Znear[1] - 1/Zfar[1]) + (1/Zfar[1]) );
#endif
#if NTT_SUBPEL_VSP_M24796
#if NOKIA_DISP_CALC_B0150
    db_disparity = -DisparityLUTR[0][i]/4;
    db_disparity_subpel = (SubPelOption==4) ? (-DisparityLUTR[0][i]) : (-DisparityLUTR[1][i]);
#else
    db_disparity        = (FocalLength*LTranslation[1]/z)-duPrincipal[1];
    db_disparity_subpel = db_disparity*SubPelOption;
#endif
#if (QC_VSP_Asymmetric_Resolution & !NTT_VSP_HARMONIZATION_M25195) // NTT
    if(bMixedRes)
    {
#if NOKIA_DISP_CALC_B0150
      db_disparity_uv_subpel = (SubPelOption==4)? (-DisparityLUTR[1][i]) : db_disparity;
      DisparityLookupTable[1][i].targetUV      = db_disparity/2;
      DisparityLookupTable[1][i].referenceUV   = abs(db_disparity_uv_subpel)-abs(DisparityLookupTable[1][i].targetUV)*SubPelOption;
#else
      db_disparity_uv_subpel = db_disparity_subpel/2;
      DisparityLookupTable[1][i].targetUV      = (int)(db_disparity/2);
      DisparityLookupTable[1][i].referenceUV   = (int)(fabs(db_disparity_uv_subpel)+0.5)-abs(DisparityLookupTable[1][i].targetUV)*SubPelOption;
#endif
      if(DisparityLookupTable[1][i].referenceUV==SubPelOption)
      {
        DisparityLookupTable[1][i].referenceUV = 0;
        DisparityLookupTable[1][i].targetUV += (db_disparity<0?-1:1);
      }
      if(SubPelOption==2) DisparityLookupTable[1][i].referenceUV *= 2;
#if NOKIA_DISP_CALC_B0150
      DisparityLookupTable[1][i].referenceY[0] = db_disparity - DisparityLookupTable[1][i].targetUV*2;
      DisparityLookupTable[1][i].referenceY[1] = abs(db_disparity_subpel)-abs(db_disparity)*SubPelOption;
#else
      DisparityLookupTable[1][i].referenceY[0] = ((int)db_disparity) - DisparityLookupTable[1][i].targetUV*2;
      DisparityLookupTable[1][i].referenceY[1] = (int)(fabs(db_disparity_subpel)+0.5)-abs(((int)db_disparity))*SubPelOption;
#endif
      if(DisparityLookupTable[1][i].referenceY[1]==SubPelOption)
      {
        DisparityLookupTable[1][i].referenceY[1] = 0;
        DisparityLookupTable[1][i].referenceY[0] += (db_disparity<0?-1:1);
      }
      if(SubPelOption==2) DisparityLookupTable[1][i].referenceY[1] *= 2;
    }
    else
#endif
    {
#if NOKIA_DISP_CALC_B0150
      DisparityLookupTable[1][i].integer    = db_disparity;
      DisparityLookupTable[1][i].fractional = abs(db_disparity_subpel)-abs(DisparityLookupTable[1][i].integer)*SubPelOption;
#else
      DisparityLookupTable[1][i].integer    = (int)db_disparity;
      DisparityLookupTable[1][i].fractional = (int)(fabs(db_disparity_subpel)+0.5)-abs(DisparityLookupTable[1][i].integer)*SubPelOption;
#endif
      if(DisparityLookupTable[1][i].fractional==SubPelOption) {
        DisparityLookupTable[1][i].fractional = 0;
        DisparityLookupTable[1][i].integer += (db_disparity<0?-1:1);
      }
      if(SubPelOption==2) DisparityLookupTable[1][i].fractional *= 2;
#if NTT_VSP_HARMONIZATION_M25195
      db_disparity        /= 2;
      db_disparity_subpel /= 2;
      DisparityLookupTable[1][i].integerUV    = (int)db_disparity;
      DisparityLookupTable[1][i].fractionalUV = (int)(fabs(db_disparity_subpel)+0.5)-abs(DisparityLookupTable[1][i].integerUV)*4;
      if(DisparityLookupTable[1][i].fractionalUV==SubPelOption)
      {
        DisparityLookupTable[1][i].fractionalUV = 0;
        DisparityLookupTable[1][i].integerUV += (db_disparity<0?-1:1);
      }
      if(SubPelOption==2) DisparityLookupTable[1][i].fractionalUV *= 2;
#endif
    }
#else
    DisparityLookupTable[1][i].db_disparity=(FocalLength*LTranslation[1]/z)-duPrincipal[1];
    DisparityLookupTable[1][i].db_disparity_mul2=DisparityLookupTable[1][i].db_disparity*2;
    DisparityLookupTable[1][i].db_disparity_mul4=DisparityLookupTable[1][i].db_disparity*4;
    DisparityLookupTable[1][i].int_disparity=(int)floor(DisparityLookupTable[1][i].db_disparity+0.5);
#endif
  }

#else
  WeightLeft = fabs(LTranslation[RGHTVIEW]/(fabs(LTranslation[LEFTVIEW])+fabs(LTranslation[RGHTVIEW])));
  WeightRight = 1-WeightLeft ;
#endif

  MemAllocated=true;

  return 0;
}

void CViewInterpolation1D::FreeMem()
{
  int i;

  if(!MemAllocated)
    return ;
  for (i = 0; i < 2; i++)
  {
#if (!QCOM_VSP || (ZJU_VSP && BOUNDARY_DETECT_ATM02))
    if (BoundaryMask[i]) 
    {
      free_mem2D(BoundaryMask[i]);
      BoundaryMask[i]=NULL;
    }
#endif
      if (RefView[i][0]) 
      {
        free_mem2D(RefView[i][0]);
        RefView[i][0]=NULL;
      }
      if (RefView[i][1]) 
      {
        free_mem2D(RefView[i][1]);
        RefView[i][1]=NULL;
      }
      if (RefView[i][2]) 
      {
        free_mem2D(RefView[i][2]);
        RefView[i][2]=NULL;
      }
      if (RefDepth[i]) 
      {
        free_mem2D(RefDepth[i]);
        RefDepth[i]=NULL;
      }
      if(TemporalImprovementOption)
      {
        if (RefViewLast[i]) 
        {
          free_mem2D(RefViewLast[i]); //Zhejiang
          RefViewLast[i]=NULL;
        }
        if (RefDepthLast[i]) 
        {
          free_mem2D(RefDepthLast[i]);
          RefDepthLast[i]=NULL;
        }
      }
  }
#if !QCOM_VSP
  for (i = 0; i < 3; i++)
  {
    if (Mask[i])
    {
      free_mem2D(Mask [i]);
      Mask[i]=NULL;
    }
  }
#endif
  for (i = 0; i < 5; i++)
  {
    if (SynY[i]) 
    {
      free_mem2D(SynY[i]);
      SynY[i]=NULL;
    }
    if (SynU[i]) 
    {
      free_mem2D(SynU[i]);
      SynU[i]=NULL;
    }
    if (SynV[i]) 
    {
      free_mem2D(SynV[i]);
      SynV[i]=NULL;
    }
    if (Dmap[i]) 
    {
      free_mem2D(Dmap[i]);
      Dmap[i]=NULL;
    }
  }
  MemAllocated=false;

}
void CViewInterpolation1D::SetSubPelOption(int sSubPelOption)
{
   SubPelOption=sSubPelOption;
}


#if (ZJU_VSP && BOUNDARY_DETECT_ATM02)
void CViewInterpolation1D::ViewGen_1LWH_splatting(int ViewId)
{

  int i;
#if NTT_SUBPEL_VSP_M24796
  int width_warped = Width;
#else
  int width_warped = Width2 * SubPelOption; //width of the warped image (in the target view), related to UpsampleRefs and SubPelOption
  int Width2_minus1 = Width2 - 1;           //width of the original image (in the original view) - 1
#endif
  int width_warped_minus1 = width_warped-1;
  int width_warped_half   = width_warped>>1;//half width of the warped image
  int x2_s, x2_floor, x2_ceil;
  int x_flip = 0;
  int warping_valid_flag = 0;
  byte** splatting_frame = NULL;
  byte*  splatting_line  = NULL;
#if NTT_SUBPEL_VSP_M24796
  int mem_id = SynDirection==2 ? ViewId : FINLVIEW;
#endif


#if VSP_OPTIMIZATION
  if(SynDirection==2)
  {
    for (i = 0; i < 3; i++)
    {
#if NTT_SUBPEL_VSP_M24796
      memset(SynY[i][0], 0,         Width*Height*sizeof(imgpel));
      memset(SynU[i][0], 0,         Width*Height*sizeof(imgpel));
      memset(SynV[i][0], 0,         Width*Height*sizeof(imgpel));
      memset(Dmap[i][0], 0,         Width*Height*sizeof(byte));
#else
      memset(SynY[i][0], 0,         Width2*Height*SubPelOption*sizeof(imgpel));
      memset(SynU[i][0], 0,         Width2*Height*SubPelOption*sizeof(imgpel));
      memset(SynV[i][0], 0,         Width2*Height*SubPelOption*sizeof(imgpel));
      memset(Dmap[i][0], 0,         Width2*Height*SubPelOption*sizeof(byte));
#endif
    }
  }
  else if(SynDirection==1)
  {
#if NTT_SUBPEL_VSP_M24796
    memset(SynU[FINLVIEW][0], 0,    Width*Height*sizeof(imgpel));
    memset(SynV[FINLVIEW][0], 0,    Width*Height*sizeof(imgpel));
#else
    memset(SynY[1][0], 0,          Width2*Height*SubPelOption*sizeof(imgpel));
    memset(SynU[1][0], 0,          Width2*Height*SubPelOption*sizeof(imgpel));
    memset(SynV[1][0], 0,          Width2*Height*SubPelOption*sizeof(imgpel));
    memset(Dmap[1][0], 0,          Width2*Height*SubPelOption*sizeof(byte));
#endif
  }
  else if(SynDirection==0)
  {
#if NTT_SUBPEL_VSP_M24796
    memset(SynU[FINLVIEW][0], 0,    Width*Height*sizeof(imgpel));
    memset(SynV[FINLVIEW][0], 0,    Width*Height*sizeof(imgpel));
#else
    memset(SynY[0][0], 0,         Width2*Height*SubPelOption*sizeof(imgpel));
    memset(SynU[0][0], 0,         Width2*Height*SubPelOption*sizeof(imgpel));
    memset(SynV[0][0], 0,         Width2*Height*SubPelOption*sizeof(imgpel));
    memset(Dmap[0][0], 0,         Width2*Height*SubPelOption*sizeof(byte));
#endif
  }
#endif // (VSP_OPTIMIZATION)

  /*------------- Original Boundary-aware processing --------------*/
  //find out the max and min z value in current frame
  if(SynDirection!=1)
    FindDepthMaxMin(RefDepth[LEFTVIEW], LEFTVIEW); //left view must have a similar depth structure as that of the right view
  else
    FindDepthMaxMin(RefDepth[RGHTVIEW], RGHTVIEW);

  {
    double es = 0.15; //EdgeStrength
    int diff_z= Maxz-Minz;
    int th=(int)(es*diff_z /UpsampleRefs+1); //depth edge threshold = 15% * z_value_range / UpsampleRefs

    if(SynDirection==1)
    {
      //!<synthesize from right
#if NTT_SUBPEL_VSP_M24796
      memset(BoundaryMask[1][0],NONE,Width*Height*sizeof(byte));
      DetectBoundary(BoundaryMask[1],  RefDepth[1], Width, Height, th);
#else
      memset(BoundaryMask[1][0],NONE,Width2*Height*sizeof(byte));
      DetectBoundary(BoundaryMask[1],  RefDepth[1], Width2, Height, th);
#endif
      splatting_frame = BoundaryMask[1];
    }
    else if(SynDirection==0)
    {
      //!<synthesize from left
#if NTT_SUBPEL_VSP_M24796
      memset(BoundaryMask[0][0],NONE,Width*Height*sizeof(byte));
      DetectBoundary(BoundaryMask[0],  RefDepth[0], Width, Height, th);
#else
      memset(BoundaryMask[0][0],NONE,Width2*Height*sizeof(byte));
      DetectBoundary(BoundaryMask[0],  RefDepth[0], Width2, Height, th);
#endif
      splatting_frame = BoundaryMask[0];
    }
  }
  //-----------end of determination of pixels using splatting

  if(ViewId == LEFTVIEW)
    WarpToRight = 1;
  else
    WarpToRight = 0;

  //start to warp each pixel
  if( WarpToRight == 0) // right view-- from right 2 left
  {
    int x, y, xLast, idx2 = 0, x2;
    imgpel* ref_Y=NULL;
    imgpel* ref_U=NULL;
    imgpel* ref_V=NULL;
    imgpel* ref_d=NULL;
    imgpel* dmap=NULL;
    imgpel* syn_Y=NULL;
    imgpel* syn_U=NULL;
    imgpel* syn_V=NULL;
    imgpel* pDepth = NULL; //rev
#if NTT_SUBPEL_VSP_M24796
    int  disp_integer;
    int  disp_fractional;
    int  p0, p1, p2, p3, p4, p5;
    int  x_far, x_near;
    imgpel half_pel[3];
#else
    double  disp;
#endif
    imgpel  crData;

    for (y = 0; y < Height; ++y)
    {
      ref_Y=RefView[ViewId][0][y];
      ref_U=RefView[ViewId][1][y];
      ref_V=RefView[ViewId][2][y];
      ref_d=RefDepth[ViewId][y];
#if NTT_SUBPEL_VSP_M24796
      syn_Y=SynY[mem_id][y];
      syn_U=SynU[mem_id][y];
      syn_V=SynV[mem_id][y];
      dmap =Dmap[mem_id][y];
      memset(syn_Y, 0, Width*sizeof(imgpel));
#else
      dmap=Dmap[ViewId][y];
      syn_Y=SynY[ViewId][y];
      syn_U=SynU[ViewId][y];
      syn_V=SynV[ViewId][y];
#endif
      pDepth = dmap;  //rev
      memset(pDepth, 0, width_warped*sizeof(imgpel)); //maybe set to "-1", as pixels may have 0 depth value; could be a bug
      xLast = 0;
      splatting_line = splatting_frame[y];

#if NTT_SUBPEL_VSP_M24796
      for (x = 0; x < Width;  ++x)
#else
      for (x = 0; x < Width2;  ++x)
#endif
      {
        //get disparity
        crData = *(ref_d+x);  //depth of current pixel being warped
#if VSP_OPTIMIZATION
#if NTT_SUBPEL_VSP_M24796
        disp_integer    = DisparityLookupTable[ViewId][crData].integer;
        disp_fractional = DisparityLookupTable[ViewId][crData].fractional;
#else
        if(UpsampleRefs==2)
          disp=DisparityLookupTable[ViewId][crData].db_disparity_mul2;
        else if(UpsampleRefs==4)
          disp=DisparityLookupTable[ViewId][crData].db_disparity_mul4;
        else  
          disp=DisparityLookupTable[ViewId][crData].db_disparity;
#endif // NTT_SUBPEL_VSP_M24796
#else
#if NTT_SUBPEL_VSP_M24796
        {
          double z;
#if POZNAN_NONLINEAR_DEPTH
          int d = nonlinear_depth_lut_backward[crData];
#else
          int d = crData;
#endif
          double db_disparity, db_disparity_subpel;

          z = 1.0/( (d/255.0) * (1/Znear[ViewId] - 1/Zfar[ViewId]) + (1/Zfar[ViewId]) );
          
          db_disparity        = (FocalLength*LTranslation[ViewId]/z)-duPrincipal[ViewId];
          db_disparity_subpel = db_disparity*SubPelOption;

          disp_integer    = (int)db_disparity;
          disp_fractional = (int)(fabs(db_disparity_subpel)+0.5)-abs(disp_integer)*SubPelOption;
          if(disp_fractional==SubPelOption) {
            disp_fractional = 0;
            disp_integer += (db_disparity<0?-1:1);
          }
          if(SubPelOption==2) disp_fractional *= 2;
        }
#endif // NTT_SUBPEL_VSP_M24796
#endif

        /* --------- splatting/warping, start --------------*/
        warping_valid_flag = 0;
        if(splatting_line[x] == NONE)
        {
#if NTT_SUBPEL_VSP_M24796
          x2 = x - disp_integer;
#else
          x2 = (int)((x - disp) * SubPelOption + 0.5);
#endif
          x2_s = x2;
          if(x2 < width_warped) //since x2 >=0 always valid in this warping direction
          {
            if(pDepth[x2] <= crData) //to handle the cases: depth value = 0;
            {
              idx2 = x2;
#if NTT_SUBPEL_VSP_M24796
              if(disp_fractional)
              {
                if(disp_fractional<0)
                  INTERPOLATE_RIGHT_SIDE(x, width_warped_minus1)
                else
                  INTERPOLATE_LEFT_SIDE(x, width_warped_minus1)

                half_pel[0]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y),0,255);
#if !(NOKIA_CHROMA_BILINEAR)
                half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_U),0,255);
                half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_V),0,255);
#endif

                if(disp_fractional==1)
                {
                  *(syn_Y+idx2) = ((*(ref_Y+x_near)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR1(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR1(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = ((*(ref_U+x_near)+half_pel[1]+1)>>1);
                  *(syn_V+idx2) = ((*(ref_V+x_near)+half_pel[2]+1)>>1);
#endif
                }
                else if(disp_fractional==2)
                {
                  *(syn_Y+idx2) = half_pel[0];
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR2(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR2(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = half_pel[1];
                  *(syn_V+idx2) = half_pel[2];
#endif
                }
                else
                {
                  *(syn_Y+idx2) = ((*(ref_Y+x_far)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR3(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR3(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = ((*(ref_U+x_far)+half_pel[1]+1)>>1);
                  *(syn_V+idx2) = ((*(ref_V+x_far)+half_pel[2]+1)>>1);
#endif
                }
              }
              else
#endif  // NTT_SUBPEL_VSP_M24796
              {
                *(syn_Y+idx2)=*(ref_Y+x);
                *(syn_U+idx2)=*(ref_U+x);
                *(syn_V+idx2)=*(ref_V+x);
              }
              *(dmap+idx2) =*(ref_d+x);
              warping_valid_flag = 1;
            }
          }

        }
        else
        {
#if NTT_SUBPEL_VSP_M24796
          x2_floor = x - disp_integer;
          x2_ceil  = x2_floor + 1;
#else
          x2_floor = (int)((x - disp) * SubPelOption);
          x2_ceil  = x2_floor + 1;
#endif

          x2 = x2_floor;
          x2_s = x2_ceil;

          if(x2_ceil < width_warped) //since x2 >=0 always valid in this warping direction
          {
            if(pDepth[x2_ceil] < crData) //note: adding "=" in splatting may cause slight difference
            {
              idx2 = x2_ceil;
#if NTT_SUBPEL_VSP_M24796
              if(disp_fractional)
              {
                if(disp_fractional>0)
                  INTERPOLATE_RIGHT_SIDE(x, width_warped_minus1)
                else
                  INTERPOLATE_LEFT_SIDE(x, width_warped_minus1)

                half_pel[0]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y),0,255);
#if !(NOKIA_CHROMA_BILINEAR)
                half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_U),0,255);
                half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_V),0,255);
#endif

                if(disp_fractional==1)
                {
                  *(syn_Y+idx2) = ((*(ref_Y+x_far)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR3(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR3(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = ((*(ref_U+x_far)+half_pel[1]+1)>>1);
                  *(syn_V+idx2) = ((*(ref_V+x_far)+half_pel[2]+1)>>1);
#endif
                }
                else if(disp_fractional==2)
                {
                  *(syn_Y+idx2) = half_pel[0];
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR2(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR2(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = half_pel[1];
                  *(syn_V+idx2) = half_pel[2];
#endif
                }
                else
                {
                  *(syn_Y+idx2) = ((*(ref_Y+x_near)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR1(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR1(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = ((*(ref_U+x_near)+half_pel[1]+1)>>1);
                  *(syn_V+idx2) = ((*(ref_V+x_near)+half_pel[2]+1)>>1);
#endif
                }
              }
              else
              {
                int x_neighbor = CLIP(x+1, 0, width_warped_minus1);
                *(syn_Y+idx2)=*(ref_Y+x_neighbor);
                *(syn_U+idx2)=*(ref_U+x_neighbor);
                *(syn_V+idx2)=*(ref_V+x_neighbor);
              }
#else // NTT_SUBPEL_VSP_M24796
              *(syn_Y+idx2)=*(ref_Y+x);
              *(syn_U+idx2)=*(ref_U+x);
              *(syn_V+idx2)=*(ref_V+x);
#endif // NTT_SUBPEL_VSP_M24796
              *(dmap+idx2) =*(ref_d+x);
              warping_valid_flag = 1;
            }
          }

          if(x2_floor < width_warped) //since x2 >=0 always valid in this warping direction
          {
            if(pDepth[x2_floor] < crData)
            {
              idx2 = x2_floor;
#if NTT_SUBPEL_VSP_M24796
              if(disp_fractional)
              {
                if(disp_fractional<0)
                  INTERPOLATE_RIGHT_SIDE(x, width_warped_minus1)
                else
                  INTERPOLATE_LEFT_SIDE(x, width_warped_minus1)

                half_pel[0]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y),0,255);
#if !(NOKIA_CHROMA_BILINEAR)
                half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_U),0,255);
                half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_V),0,255);
#endif

                if(disp_fractional==1)
                {
                  *(syn_Y+idx2) = ((*(ref_Y+x_near)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR1(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR1(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = ((*(ref_U+x_near)+half_pel[1]+1)>>1);
                  *(syn_V+idx2) = ((*(ref_V+x_near)+half_pel[2]+1)>>1);
#endif
                }
                else if(disp_fractional==2)
                {
                  *(syn_Y+idx2) = half_pel[0];
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR2(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR2(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = half_pel[1];
                  *(syn_V+idx2) = half_pel[2];
#endif
                }
                else
                {
                  *(syn_Y+idx2) = ((*(ref_Y+x_far)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR3(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR3(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = ((*(ref_U+x_far)+half_pel[1]+1)>>1);
                  *(syn_V+idx2) = ((*(ref_V+x_far)+half_pel[2]+1)>>1);
#endif
                }
              }
              else
#endif // NTT_SUBPEL_VSP_M24796
              {
                *(syn_Y+idx2)=*(ref_Y+x);
                *(syn_U+idx2)=*(ref_U+x);
                *(syn_V+idx2)=*(ref_V+x);
              }
              *(dmap+idx2) =*(ref_d+x);
              warping_valid_flag = 1;
            }
          }
        }
        /* --------- splatting/warping, end --------------*/

        if(warping_valid_flag) //filling the holes between two valid pixel projections
        {

          if(xLast >= x2)
          {
            int cnt, use, fill;
            if(xLast > x2) 
              memset((void*)&pDepth[idx2+1], 0, (xLast-x2));
            if(x2!=0 && pDepth[x2-1] == 0)
            {
              for(cnt = 0; cnt < x2; cnt++)
              {
                if(pDepth[x2-1-cnt]!= 0 ) 
                  break;
              }
              if(cnt == x2)
                use = x2;
              else if( pDepth[x2-cnt-1]>pDepth[x2])
                use = x2;
              else
                use = x2-1-cnt;
              for(fill = (x2-cnt); fill < x2; fill++)
              {
                *(syn_Y+fill) =*(syn_Y+use);
                *(syn_U+fill) =*(syn_U+use);
                *(syn_V+fill) =*(syn_V+use);
                *(dmap +fill) =*(dmap +use);
              }
            }
          }//this code for the case (if(xLast > x2)) may never be used actually - Yin
          else
          { //xLast < x2
            int fill;
            if( !(x2!=0 && xLast== (x2-1)) )
            {
              if(xLast == 0)
              {
                for(fill = xLast; fill < x2; fill++)
                {
                  *(syn_Y+fill)=*(syn_Y+x2);
                  *(syn_U+fill)=*(syn_U+x2);
                  *(syn_V+fill)=*(syn_V+x2);
                  *(dmap+fill) =*(dmap +x2);
                }
              }
              else
              {
                if(pDepth[xLast] > pDepth[x2])
                {
                  for(fill = (xLast+1); fill < x2; fill++)
                  {
                    *(syn_Y+fill)=*(syn_Y+x2);
                    *(syn_U+fill)=*(syn_U+x2);
                    *(syn_V+fill)=*(syn_V+x2);
                    *(dmap+fill) =*(dmap+x2);
                  }
                }
                else
                {
                  for(fill = (xLast+1); fill < x2; fill++)
                  {
                    *(syn_Y+fill)=*(syn_Y+xLast);
                    *(syn_U+fill)=*(syn_U+xLast);
                    *(syn_V+fill)=*(syn_V+xLast);
                    *(dmap+fill) =*(dmap+xLast);
                  }
                }
              }
            }
          }//end x2>=imgWidth
          xLast = x2_s; //rev
        }
      }//end x loop
      if(xLast != width_warped)
      {
        int fill;
        for(fill = xLast+1; fill < width_warped; fill++)
        {
          *(syn_Y+fill)=*(syn_Y+xLast);
          *(syn_U+fill)=*(syn_U+xLast);
          *(syn_V+fill)=*(syn_V+xLast);
          *(dmap+fill) =*(dmap+xLast);
        }
      }
      if(pDepth[0] == 0)
      {
        *(syn_Y+0) =*(syn_Y+1);
        *(syn_U+0) =*(syn_U+1);
        *(syn_V+0) =*(syn_V+1);
        *(dmap +0) =*(dmap +1);
      }
    }//end y loop
  }
  else  //if( WarpToRight == 1) 
  {
    int x, y, xLast, idx2 = 0, x2;
    imgpel* ref_Y=NULL;
    imgpel* ref_U=NULL;
    imgpel* ref_V=NULL;
    imgpel* ref_d=NULL;
    imgpel* dmap=NULL;
    imgpel* syn_Y=NULL;
    imgpel* syn_U=NULL;
    imgpel* syn_V=NULL;
    imgpel* pDepth = NULL; //rev
#if NTT_SUBPEL_VSP_M24796
    int  disp_integer;
    int  disp_fractional;
    int  p0, p1, p2, p3, p4, p5;
    int  x_far, x_near;
    imgpel half_pel[3];
#else
    double  disp;
#endif
    imgpel  crData;

    for (y = 0; y < Height; ++y)
    {
      ref_Y=RefView[ViewId][0][y];
      ref_U=RefView[ViewId][1][y];
      ref_V=RefView[ViewId][2][y];
      ref_d=RefDepth[ViewId][y];
#if NTT_SUBPEL_VSP_M24796
      syn_Y=SynY[mem_id][y];
      syn_U=SynU[mem_id][y];
      syn_V=SynV[mem_id][y];
      dmap =Dmap[mem_id][y];
      memset(syn_Y, 0, Width*sizeof(imgpel));
#else
      dmap=Dmap[ViewId][y];
      syn_Y=SynY[ViewId][y];
      syn_U=SynU[ViewId][y];
      syn_V=SynV[ViewId][y];
#endif

      pDepth = dmap; //rev
      memset(pDepth, 0, width_warped*sizeof(imgpel));
      xLast = 0;
      splatting_line = splatting_frame[y];

#if NTT_SUBPEL_VSP_M24796
      for(x = 0; x < Width;  ++x)
#else
      for(x = 0; x < Width2;  ++x)
#endif
      {
        //get disparity
#if NTT_SUBPEL_VSP_M24796
        x_flip = width_warped_minus1-x; //rev
#else
        x_flip = Width2_minus1-x; //rev
#endif
        crData = *(ref_d+ x_flip);
        
#if NTT_SUBPEL_VSP_M24796
#if VSP_OPTIMIZATION
        disp_integer    = DisparityLookupTable[ViewId][crData].integer;
        disp_fractional = DisparityLookupTable[ViewId][crData].fractional;
#else
        {
          double z;
#if POZNAN_NONLINEAR_DEPTH
          int d = nonlinear_depth_lut_backward[crData];
#else
          int d = crData;
#endif
          double db_disparity, db_disparity_subpel;

          z = 1.0/( (d/255.0) * (1/Znear[ViewId] - 1/Zfar[ViewId]) + (1/Zfar[ViewId]) );
          
          db_disparity        = (FocalLength*LTranslation[ViewId]/z)-duPrincipal[ViewId];
          db_disparity_subpel = db_disparity*SubPelOption;

          disp_integer    = (int)db_disparity;
          disp_fractional = (int)(fabs(db_disparity_subpel)+0.5)-abs(disp_integer)*SubPelOption;
          if(disp_fractional==SubPelOption) {
            disp_fractional = 0;
            disp_integer += (db_disparity<0?-1:1);
          }
          if(SubPelOption==2) disp_fractional *= 2;
        }
#endif // (VSP_OPTIMIZATION)
#else // NTT_SUBPEL_VSP_M24796
        if(UpsampleRefs==2)
          disp= DisparityLookupTable[ViewId][crData].db_disparity_mul2;
        else if(UpsampleRefs==4)
          disp= DisparityLookupTable[ViewId][crData].db_disparity_mul4;
        else  
          disp= DisparityLookupTable[ViewId][crData].db_disparity;
#endif // NTT_SUBPEL_VSP_M24796


        /* --------- splatting/warping, start --------------*/
        warping_valid_flag = 0;
        if(splatting_line[x_flip] == NONE)
        {
#if NTT_SUBPEL_VSP_M24796
          x2 = x + disp_integer;
#else
          x2 = (int)((x + disp) * SubPelOption + 0.5);
#endif
          x2_s = x2;
          if(x2 < width_warped) //since x2 >=0 always valid in this warping direction
          {
            if(pDepth[x2] <= crData)
            {
              idx2 = x2;
#if NTT_SUBPEL_VSP_M24796
              if(disp_fractional)
              {
                if(disp_fractional<0)
                  INTERPOLATE_LEFT_SIDE(x_flip, width_warped_minus1)
                else
                  INTERPOLATE_RIGHT_SIDE(x_flip, width_warped_minus1)

                half_pel[0]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y),0,255);
#if !(NOKIA_CHROMA_BILINEAR)
                half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_U),0,255);
                half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_V),0,255);
#endif

                if(disp_fractional==1)
                {
                  *(syn_Y+idx2) = ((*(ref_Y+x_near)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR1(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR1(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = ((*(ref_U+x_near)+half_pel[1]+1)>>1);
                  *(syn_V+idx2) = ((*(ref_V+x_near)+half_pel[2]+1)>>1);
#endif
                }
                else if(disp_fractional==2)
                {
                  *(syn_Y+idx2) = half_pel[0];
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR2(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR2(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = half_pel[1];
                  *(syn_V+idx2) = half_pel[2];
#endif
                }
                else
                {
                  *(syn_Y+idx2) = ((*(ref_Y+x_far)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR3(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR3(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = ((*(ref_U+x_far)+half_pel[1]+1)>>1);
                  *(syn_V+idx2) = ((*(ref_V+x_far)+half_pel[2]+1)>>1);
#endif
                }
              }
              else
#endif // NTT_SUBPEL_VSP_M24796
              {
                *(syn_Y+idx2)=*(ref_Y+x_flip); //flipping meanwhile
                *(syn_U+idx2)=*(ref_U+x_flip);
                *(syn_V+idx2)=*(ref_V+x_flip);
              }
              *(dmap+idx2) =*(ref_d+x_flip);
              warping_valid_flag = 1;
            }
          }
        }
        else
        {
#if NTT_SUBPEL_VSP_M24796
          x2_floor = x + disp_integer;
          x2_ceil  = x2_floor + 1;
#else
          x2_floor = (int)((x + disp) * SubPelOption);
          x2_ceil  = x2_floor + 1;
#endif

          x2 = x2_floor;
          x2_s = x2_ceil;

          if(x2_ceil < width_warped)
          {
            if(pDepth[x2_ceil] < crData)
            {
              idx2 = x2_ceil;
#if NTT_SUBPEL_VSP_M24796
              if(disp_fractional)
              {
                if(disp_fractional>0)
                  INTERPOLATE_LEFT_SIDE(x_flip, width_warped_minus1)
                else
                  INTERPOLATE_RIGHT_SIDE(x_flip, width_warped_minus1)

                half_pel[0]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y),0,255);
#if !(NOKIA_CHROMA_BILINEAR)
                half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_U),0,255);
                half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_V),0,255);
#endif

                if(disp_fractional==1)
                {
                  *(syn_Y+idx2) = ((*(ref_Y+x_far)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR3(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR3(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = ((*(ref_U+x_far)+half_pel[1]+1)>>1);
                  *(syn_V+idx2) = ((*(ref_V+x_far)+half_pel[2]+1)>>1);
#endif
                }
                else if(disp_fractional==2)
                {
                  *(syn_Y+idx2) = half_pel[0];
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR2(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR2(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = half_pel[1];
                  *(syn_V+idx2) = half_pel[2];
#endif
                }
                else
                {
                  *(syn_Y+idx2) = ((*(ref_Y+x_near)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR1(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR1(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = ((*(ref_U+x_near)+half_pel[1]+1)>>1);
                  *(syn_V+idx2) = ((*(ref_V+x_near)+half_pel[2]+1)>>1);
#endif
                }
              }
              else
              {
                int x_neighbor = CLIP(x_flip-1, 0, width_warped_minus1);
                *(syn_Y+idx2)=*(ref_Y+x_neighbor);
                *(syn_U+idx2)=*(ref_U+x_neighbor);
                *(syn_V+idx2)=*(ref_V+x_neighbor);
              }
#else
              *(syn_Y+idx2)=*(ref_Y+x_flip);
              *(syn_U+idx2)=*(ref_U+x_flip);
              *(syn_V+idx2)=*(ref_V+x_flip);
#endif // NTT_SUBPEL_VSP_M24796
              *(dmap+idx2) =*(ref_d+x_flip);
              warping_valid_flag = 1;
            }
          }

          if(x2_floor < width_warped)
          {
            if(pDepth[x2_floor] < crData)
            {
              idx2 = x2_floor;
#if NTT_SUBPEL_VSP_M24796
              if(disp_fractional)
              {
                if(disp_fractional<0)
                  INTERPOLATE_LEFT_SIDE(x_flip, width_warped_minus1)
                else
                  INTERPOLATE_RIGHT_SIDE(x_flip, width_warped_minus1)

                half_pel[0]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y),0,255);
#if !(NOKIA_CHROMA_BILINEAR)
                half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_U),0,255);
                half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_V),0,255);
#endif

                if(disp_fractional==1)
                {
                  *(syn_Y+idx2) = ((*(ref_Y+x_near)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR1(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR1(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = ((*(ref_U+x_near)+half_pel[1]+1)>>1);
                  *(syn_V+idx2) = ((*(ref_V+x_near)+half_pel[2]+1)>>1);
#endif
                }
                else if(disp_fractional==2)
                {
                  *(syn_Y+idx2) = half_pel[0];
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR2(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR2(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = half_pel[1];
                  *(syn_V+idx2) = half_pel[2];
#endif
                }
                else
                {
                  *(syn_Y+idx2) = ((*(ref_Y+x_far)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR3(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR3(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = ((*(ref_U+x_far)+half_pel[1]+1)>>1);
                  *(syn_V+idx2) = ((*(ref_V+x_far)+half_pel[2]+1)>>1);
#endif
                }
              }
              else
#endif
              {
                *(syn_Y+idx2)=*(ref_Y+x_flip);
                *(syn_U+idx2)=*(ref_U+x_flip);
                *(syn_V+idx2)=*(ref_V+x_flip);
              }
              *(dmap+idx2) =*(ref_d+x_flip);
              warping_valid_flag = 1;
            }
          }
        }
        /* --------- splatting/warping, start --------------*/

        if(warping_valid_flag)
        {
          if(xLast >= x2)
          {
            int cnt, use, fill;
            //assert(xLast==x2);
            if(xLast > x2) 
              memset((void*)&pDepth[idx2+1], 0, (xLast-x2));
            if(x2!=0 && pDepth[x2-1] == 0)
            {
              for(cnt = 0; cnt < x2; cnt++)
              {
                if(pDepth[x2-1-cnt]!= 0) 
                  break;
              }
              if(cnt == x2)
                use = x2;
              else if( pDepth[x2-cnt-1]>pDepth[x2])
                use = x2;
              else
                use = x2-1-cnt;
              for(fill = (x2-cnt); fill < x2; fill++)
              {
                *(syn_Y+fill) =*(syn_Y+use);
                *(syn_U+fill) =*(syn_U+use);
                *(syn_V+fill) =*(syn_V+use);
                *(dmap +fill) =*(dmap +use);
              }
            }
          } 
          else
          { //xLast < x2
            int fill;
            if( !(x2!=0 && xLast== (x2-1)) )
            {
              if(xLast == 0)
              {
                for(fill = xLast; fill < x2; fill++)
                {
                  *(syn_Y+fill)=*(syn_Y+x2);
                  *(syn_U+fill)=*(syn_U+x2);
                  *(syn_V+fill)=*(syn_V+x2);
                  *(dmap+fill) =*(dmap +x2);
                }
              }else
              {
                if(pDepth[xLast] > pDepth[x2])
                {
                  for(fill = (xLast+1); fill < x2; fill++)
                  {
                    *(syn_Y+fill)=*(syn_Y+x2);
                    *(syn_U+fill)=*(syn_U+x2);
                    *(syn_V+fill)=*(syn_V+x2);
                    *(dmap+fill) =*(dmap+x2);
                  }
                }else
                {
                  for(fill = (xLast+1); fill < x2; fill++)
                  {
                    *(syn_Y+fill)=*(syn_Y+xLast);
                    *(syn_U+fill)=*(syn_U+xLast);
                    *(syn_V+fill)=*(syn_V+xLast);
                    *(dmap+fill) =*(dmap+xLast);
                  }
                }
              }
            }
          }//end x2>=imgWidth
          xLast = x2_s; //rev
        }

      }//end x loop
      if(xLast != width_warped)
      {
        int fill;
        for(fill = xLast+1; fill < width_warped; fill++)
        {
          *(syn_Y+fill)=*(syn_Y+xLast);
          *(syn_U+fill)=*(syn_U+xLast);
          *(syn_V+fill)=*(syn_V+xLast);
          *(dmap+fill) =*(dmap+xLast);
        }
      }
      if(pDepth[0] == 0)
      {
        *(syn_Y+0) =*(syn_Y+1);
        *(syn_U+0) =*(syn_U+1);
        *(syn_V+0) =*(syn_V+1);
        *(dmap +0) =*(dmap +1);
      }
      //flip
      for(x=0; x< width_warped_half; x++)
      {
        imgpel temp = *(syn_Y+x);
        int pos = width_warped_minus1-x;
        *(syn_Y+x) = *(syn_Y+ pos);
        *(syn_Y+ pos) = temp;

        temp = *(syn_U+x);
        *(syn_U+x) = *(syn_U+ pos);
        *(syn_U+ pos) = temp;

        temp = *(syn_V+x);
        *(syn_V+x) = *(syn_V+ pos);
        *(syn_V+ pos) = temp;

        temp = *(dmap+x);
        *(dmap+x) = *(dmap+ pos);
        *(dmap+ pos) = temp;
      }
    }//end y loop
  }//end RIGHT_VIEW


#if NTT_SUBPEL_VSP_M24796
  if(SynDirection==2)
#endif
  {
    int y;
    for(y = 0; y < Height; ++y)
    {
      imgpel* syn_hlf_Y=SynY[HLFLVIEW][y];
      imgpel* syn_hlf_U=SynU[HLFLVIEW][y];
      imgpel* syn_hlf_V=SynV[HLFLVIEW][y];
      imgpel* syn_hlf_D=Dmap[HLFLVIEW][y];
      imgpel* syn_merge_Y=SynY[ViewId][y];
      imgpel* syn_merge_U=SynU[ViewId][y];
      imgpel* syn_merge_V=SynV[ViewId][y];
      imgpel* syn_merge_D=Dmap[ViewId][y];

      memcpy(syn_hlf_Y,syn_merge_Y,width_warped*sizeof(imgpel));
      memcpy(syn_hlf_U,syn_merge_U,width_warped*sizeof(imgpel));
      memcpy(syn_hlf_V,syn_merge_V,width_warped*sizeof(imgpel));
      memcpy(syn_hlf_D,syn_merge_D,width_warped*sizeof(imgpel));
    }
  }
}

#if QC_VSP_Asymmetric_Resolution
#if NTT_VSP_HARMONIZATION_M25195
void CViewInterpolation1D::ViewGen_1LWH_splatting420(int ViewId)
{
  int i, x, y;
  int WidthUV = Width>>1;
  int WidthUV_minus1 = WidthUV - 1;
  byte** splatting_frame = NULL;
  byte*  splatting_line  = NULL;
  int WidthLuma_minus1 = Width-1;
  int mem_id = SynDirection==2 ? ViewId : FINLVIEW;

  int xLastLuma, xLastChroma, fill;
  int dispY_integer, dispUV_integer;
  int dispY_fractional, dispUV_fractional;
  int warping_valid_flag_Y=0, warping_valid_flag_UV=0;
  int x2Chroma, x2Chroma_s;
  int x2Luma, x2Luma_s, x2Luma_m;

  imgpel* ref_Y=NULL;
  imgpel* ref_Y_Rownxt=NULL;

  imgpel* ref_U=NULL;
  imgpel* ref_V=NULL;
  imgpel* ref_d=NULL;
  imgpel* dmap=NULL;
  
  imgpel* syn_Y=NULL;
  imgpel* syn_Y_Rownxt=NULL;
  imgpel* syn_U=NULL;
  imgpel* syn_V=NULL;
  imgpel* pDepth = NULL; //rev
  int  xLuma;
  int  p0, p1, p2, p3, p4, p5;
  int  x_far[3], x_near[3];
  imgpel half_pel[6];
  imgpel  crData;

  if(SynDirection==2)
  {
    for (i = 0; i < 3; i++)
    {
      memset(SynY[i][0], 0,         Width*Height*SubPelOption*sizeof(imgpel));
      memset(SynU[i][0], 0,         Width*Height/4*SubPelOption*sizeof(imgpel));
      memset(SynV[i][0], 0,         Width*Height/4*SubPelOption*sizeof(imgpel));
      memset(Dmap[i][0], 0,         Width*Height/4*SubPelOption*sizeof(byte));
    }
  }

  /*------------- Original Boundary-aware processing --------------*/
  //find out the max and min z value in current frame
  if(SynDirection!=1)
    FindDepthMaxMin420(RefDepth[LEFTVIEW], LEFTVIEW); //left view must have a similar depth structure as that of the right view
  else
    FindDepthMaxMin420(RefDepth[RGHTVIEW], RGHTVIEW);

  {
    double es = 0.15; //EdgeStrength
    int diff_z= Maxz-Minz;
    int th=(int)(es*diff_z /UpsampleRefs+1); //depth edge threshold = 15% * z_value_range / UpsampleRefs

    if(SynDirection==1)
    {
      //!<synthesize from right
      memset(BoundaryMask[1][0],NONE,Width*Height/4*sizeof(byte));
      DetectBoundary(BoundaryMask[1],  RefDepth[1], Width/2, Height/2, th);
      splatting_frame = BoundaryMask[1];
    }
    else if(SynDirection==0)
    {
      //!<synthesize from left
      memset(BoundaryMask[0][0],NONE,Width*Height/4*sizeof(byte));
      DetectBoundary(BoundaryMask[0],  RefDepth[0], Width/2, Height/2, th);
      splatting_frame = BoundaryMask[0];
    }
  }
  //-----------end of determination of pixels using splatting

  if(ViewId == LEFTVIEW)
    WarpToRight = 1;
  else
    WarpToRight = 0;

  //start to warp each pixel
  if( WarpToRight == 0) // right view-- from right 2 left
  {
    for (y = 0; y < Height/2; ++y)
    {
      ref_Y=RefView[ViewId][0][2*y];
      ref_Y_Rownxt=RefView[ViewId][0][2*y+1];

      ref_U=RefView[ViewId][1][y];
      ref_V=RefView[ViewId][2][y];
      ref_d=RefDepth[ViewId][y];

      syn_Y=SynY[mem_id][2*y];
      syn_Y_Rownxt=SynY[mem_id][2*y+1];
      syn_U=SynU[mem_id][y];
      syn_V=SynV[mem_id][y];
      dmap=Dmap[mem_id][y];
      memset(syn_Y, 0, Width*sizeof(imgpel));
      memset(syn_Y_Rownxt, 0, Width*sizeof(imgpel));
      memset(syn_U, 0, WidthUV*sizeof(imgpel));
      memset(syn_V, 0, WidthUV*sizeof(imgpel));

      pDepth = dmap;  //rev
      memset(pDepth, 0, WidthUV*sizeof(imgpel));
      xLastLuma = Width;
      xLastChroma = WidthUV;
      splatting_line = splatting_frame[y];

      for (x = WidthUV_minus1; x >=0;  x--)
      {
        //get disparity
        crData = *(ref_d+x);  //depth of current pixel being warped

        xLuma = (x<<1);
#if (VSP_OPTIMIZATION)
        dispY_integer     = DisparityLookupTable[ViewId][crData].integer;
        dispY_fractional  = DisparityLookupTable[ViewId][crData].fractional;
        dispUV_integer    = DisparityLookupTable[ViewId][crData].integerUV;
        dispUV_fractional = DisparityLookupTable[ViewId][crData].fractionalUV;
#else
        {
#if POZNAN_NONLINEAR_DEPTH
          int in = nonlinear_depth_lut_backward[crData];
#else
          int in = crData;
#endif
          double z, db_disparity, db_disparity_subpel;

          z=1.0/( (in/255.0) * (1/Znear[ViewId] - 1/Zfar[ViewId]) + (1/Zfar[ViewId]) );
          db_disparity        = (FocalLength*LTranslation[ViewId]/z)-duPrincipal[ViewId];
          db_disparity_subpel = db_disparity*SubPelOption;
          dispY_integer    = (int)db_disparity;
          dispY_fractional = (int)(fabs(db_disparity_subpel)+0.5)-abs(dispY_integer)*SubPelOption;
          if(dispY_fractional==SubPelOption) {
            dispY_fractional = 0;
            dispY_integer += (db_disparity<0?-1:1);
          }
          if(SubPelOption==2) dispY_fractional *= 2;
          db_disparity        /= 2;
          db_disparity_subpel /= 2;
          dispUV_integer    = (int)db_disparity;
          dispUV_fractional = (int)(fabs(db_disparity_subpel)+0.5)-abs(dispUV_integer)*4;
          if(dispUV_fractional==SubPelOption)
          {
            dispUV_fractional = 0;
            dispUV_integer += (db_disparity<0?-1:1);
          }
          if(SubPelOption==2) dispUV_fractional *= 2;
        }
#endif // (VSP_OPTIMIZATION)

        /* --------- splatting/warping, start --------------*/
        warping_valid_flag_Y = warping_valid_flag_UV = 0;
        if(splatting_line[x] == NONE)
        {
          // Luma
          x2Luma = xLuma - dispY_integer;
          x2Luma_s = x2Luma + 1;
          if(x2Luma_s < Width)
          {
            warping_valid_flag_Y = 1;
            if(dispY_fractional)
            {
              if(dispY_fractional<0)
              {
                INTERPOLATE_RIGHT_SIDE3(xLuma,   WidthLuma_minus1, 0)
                half_pel[0] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)       , 0, 255);
                half_pel[2] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt), 0, 255);
                INTERPOLATE_RIGHT_SIDE3(xLuma+1, WidthLuma_minus1, 1)
                half_pel[1] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)       , 0, 255);
                half_pel[3] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt), 0, 255);
              }
              else
              {
                INTERPOLATE_LEFT_SIDE3(xLuma,   WidthLuma_minus1, 0)
                half_pel[0] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)       , 0, 255);
                half_pel[2] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt), 0, 255);
                INTERPOLATE_LEFT_SIDE3(xLuma+1, WidthLuma_minus1, 1)
                half_pel[1] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)       , 0, 255);
                half_pel[3] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt), 0, 255);
              }
              if(dispY_fractional==1)
              {
                *(syn_Y       +x2Luma  ) = ((*(ref_Y       +x_near[0])+half_pel[0]+1)>>1);
                *(syn_Y       +x2Luma_s) = ((*(ref_Y       +x_near[1])+half_pel[1]+1)>>1);
                *(syn_Y_Rownxt+x2Luma  ) = ((*(ref_Y_Rownxt+x_near[0])+half_pel[2]+1)>>1);
                *(syn_Y_Rownxt+x2Luma_s) = ((*(ref_Y_Rownxt+x_near[1])+half_pel[3]+1)>>1);
              }
              else if(dispY_fractional==2)
              {
                *(syn_Y       +x2Luma  ) = half_pel[0];
                *(syn_Y       +x2Luma_s) = half_pel[1];
                *(syn_Y_Rownxt+x2Luma  ) = half_pel[2];
                *(syn_Y_Rownxt+x2Luma_s) = half_pel[3];
              }
              else
              {
                *(syn_Y       +x2Luma  ) = ((*(ref_Y       +x_far[0])+half_pel[0]+1)>>1);
                *(syn_Y       +x2Luma_s) = ((*(ref_Y       +x_far[1])+half_pel[1]+1)>>1);
                *(syn_Y_Rownxt+x2Luma  ) = ((*(ref_Y_Rownxt+x_far[0])+half_pel[2]+1)>>1);
                *(syn_Y_Rownxt+x2Luma_s) = ((*(ref_Y_Rownxt+x_far[1])+half_pel[3]+1)>>1);
              }
            }
            else
            {
              *(syn_Y       +x2Luma  )=*(ref_Y       +xLuma);
              *(syn_Y       +x2Luma_s)=*(ref_Y       +xLuma+1);
              *(syn_Y_Rownxt+x2Luma  )=*(ref_Y_Rownxt+xLuma);
              *(syn_Y_Rownxt+x2Luma_s)=*(ref_Y_Rownxt+xLuma+1);
            }
          }
          else if(x2Luma < Width)
          {
            warping_valid_flag_Y = 1;
            x2Luma_s = x2Luma;
            if(dispY_fractional)
            {
              if(dispY_fractional<0)
                INTERPOLATE_RIGHT_SIDE3(xLuma, WidthLuma_minus1, 0)
              else
                INTERPOLATE_LEFT_SIDE3(xLuma, WidthLuma_minus1, 0)
              half_pel[0] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)         , 0, 255);
              half_pel[2] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt)  , 0, 255);
              if(dispY_fractional==1)
              {
                *(syn_Y       +x2Luma  ) = ((*(ref_Y       +x_near[0])+half_pel[0]+1)>>1);
                *(syn_Y_Rownxt+x2Luma  ) = ((*(ref_Y_Rownxt+x_near[0])+half_pel[2]+1)>>1);
              }
              else if(dispY_fractional==2)
              {
                *(syn_Y       +x2Luma  ) = half_pel[0];
                *(syn_Y_Rownxt+x2Luma  ) = half_pel[2];
              }
              else
              {
                *(syn_Y       +x2Luma  ) = ((*(ref_Y       +x_far[0])+half_pel[0]+1)>>1);
                *(syn_Y_Rownxt+x2Luma  ) = ((*(ref_Y_Rownxt+x_far[0])+half_pel[2]+1)>>1);
              }
            }
            else
            {
              *(syn_Y       +x2Luma  )=*(ref_Y       +xLuma);
              *(syn_Y_Rownxt+x2Luma  )=*(ref_Y_Rownxt+xLuma);
            }
          }

          // Chroma
          x2Chroma = x2Chroma_s =  x - dispUV_integer;
          if(x2Chroma < WidthUV)
          {
            warping_valid_flag_UV = 1;
            if(dispUV_fractional)
            {
              if(dispUV_fractional<0)
                INTERPOLATE_RIGHT_SIDE3(x, WidthUV_minus1, 0)
              else
                INTERPOLATE_LEFT_SIDE3(x, WidthUV_minus1, 0)
#if !(NOKIA_CHROMA_BILINEAR)
              half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_U),0,255);
              half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_V),0,255);
#endif
              if(dispUV_fractional==1)
              {
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+x2Chroma) = BILINEAR1(ref_U, x_near[0], x_far[0]);
                *(syn_V+x2Chroma) = BILINEAR1(ref_V, x_near[0], x_far[0]);
#else
                *(syn_U+x2Chroma) = ((*(ref_U+x_near[0])+half_pel[1]+1)>>1);
                *(syn_V+x2Chroma) = ((*(ref_V+x_near[0])+half_pel[2]+1)>>1);
#endif
              }
              else if(dispUV_fractional==2)
              {
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+x2Chroma) = BILINEAR2(ref_U, x_near[0], x_far[0]);
                *(syn_V+x2Chroma) = BILINEAR2(ref_V, x_near[0], x_far[0]);
#else
                *(syn_U+x2Chroma) = half_pel[1];
                *(syn_V+x2Chroma) = half_pel[2];
#endif
              }
              else
              {
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+x2Chroma) = BILINEAR3(ref_U, x_near[0], x_far[0]);
                *(syn_V+x2Chroma) = BILINEAR3(ref_V, x_near[0], x_far[0]);
#else
                *(syn_U+x2Chroma) = ((*(ref_U+x_far[0])+half_pel[1]+1)>>1);
                *(syn_V+x2Chroma) = ((*(ref_V+x_far[0])+half_pel[2]+1)>>1);
#endif
              }
            }
            else
            {
              *(syn_U+x2Chroma)=*(ref_U+x);
              *(syn_V+x2Chroma)=*(ref_V+x);
            }
            *(dmap+x2Chroma) = *(ref_d+x);
          }
        }
        else
        {
          // Luma
          x2Luma   = xLuma - dispY_integer;
          x2Luma_m = x2Luma + 1;
          x2Luma_s = x2Luma + 2;

          if(x2Luma_s < Width)
          {
            warping_valid_flag_Y = 1;
            if(dispY_fractional)
            {
              if(dispY_fractional<0)
              {
                INTERPOLATE_RIGHT_SIDE3(xLuma,   WidthLuma_minus1, 0)
                half_pel[0] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)       , 0, 255); 
                half_pel[3] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt), 0, 255);
                INTERPOLATE_RIGHT_SIDE3(xLuma+1, WidthLuma_minus1, 1)
                half_pel[1] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)       , 0, 255); 
                half_pel[4] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt), 0, 255);
                INTERPOLATE_RIGHT_SIDE3(xLuma+2, WidthLuma_minus1, 2)
                half_pel[2] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)       , 0, 255); 
                half_pel[5] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt), 0, 255);
              }
              else
              {
                INTERPOLATE_LEFT_SIDE3(xLuma,   WidthLuma_minus1, 0)
                half_pel[0] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)       , 0, 255); 
                half_pel[3] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt), 0, 255);
                INTERPOLATE_LEFT_SIDE3(xLuma+1, WidthLuma_minus1, 1)
                half_pel[1] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)       , 0, 255); 
                half_pel[4] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt), 0, 255);
                INTERPOLATE_LEFT_SIDE3(xLuma+2, WidthLuma_minus1, 2)
                half_pel[2] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)       , 0, 255); 
                half_pel[5] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt), 0, 255);
              }

              if(dispY_fractional==1)
              {
                *(syn_Y       +x2Luma  ) = ((*(ref_Y       +x_near[0])+half_pel[0]+1)>>1);
                *(syn_Y       +x2Luma_m) = ((*(ref_Y       +x_near[1])+half_pel[1]+1)>>1);
                *(syn_Y       +x2Luma_s) = ((*(ref_Y       +x_near[2])+half_pel[2]+1)>>1);
                *(syn_Y_Rownxt+x2Luma  ) = ((*(ref_Y_Rownxt+x_near[0])+half_pel[3]+1)>>1);
                *(syn_Y_Rownxt+x2Luma_m) = ((*(ref_Y_Rownxt+x_near[1])+half_pel[4]+1)>>1);
                *(syn_Y_Rownxt+x2Luma_s) = ((*(ref_Y_Rownxt+x_near[2])+half_pel[5]+1)>>1);
              }
              else if(dispY_fractional==2)
              {
                *(syn_Y       +x2Luma  ) = half_pel[0];
                *(syn_Y       +x2Luma_m) = half_pel[1];
                *(syn_Y       +x2Luma_s) = half_pel[2];
                *(syn_Y_Rownxt+x2Luma  ) = half_pel[3];
                *(syn_Y_Rownxt+x2Luma_m) = half_pel[4];
                *(syn_Y_Rownxt+x2Luma_s) = half_pel[5];
              }
              else
              {
                *(syn_Y       +x2Luma  ) = ((*(ref_Y       +x_far[0])+half_pel[0]+1)>>1);
                *(syn_Y       +x2Luma_m) = ((*(ref_Y       +x_far[1])+half_pel[1]+1)>>1);
                *(syn_Y       +x2Luma_s) = ((*(ref_Y       +x_far[2])+half_pel[2]+1)>>1);
                *(syn_Y_Rownxt+x2Luma  ) = ((*(ref_Y_Rownxt+x_far[0])+half_pel[3]+1)>>1);
                *(syn_Y_Rownxt+x2Luma_m) = ((*(ref_Y_Rownxt+x_far[1])+half_pel[4]+1)>>1);
                *(syn_Y_Rownxt+x2Luma_s) = ((*(ref_Y_Rownxt+x_far[2])+half_pel[5]+1)>>1);
              }
            }
            else
            {
              int x_neighbor = CLIP(xLuma+2, 0, WidthLuma_minus1);
              *(syn_Y       +x2Luma  ) = *(ref_Y       +xLuma     );
              *(syn_Y       +x2Luma_m) = *(ref_Y       +xLuma+1   );
              *(syn_Y       +x2Luma_s) = *(ref_Y       +x_neighbor);
              *(syn_Y_Rownxt+x2Luma  ) = *(ref_Y_Rownxt+xLuma     );
              *(syn_Y_Rownxt+x2Luma_m) = *(ref_Y_Rownxt+xLuma+1   );
              *(syn_Y_Rownxt+x2Luma_s) = *(ref_Y_Rownxt+x_neighbor);
            }
          }
          else if(x2Luma_m < Width)
          {
            x2Luma_s = x2Luma_m;
            warping_valid_flag_Y = 1;
            if(dispY_fractional)
            {
              if(dispY_fractional<0)
              {
                INTERPOLATE_RIGHT_SIDE3(xLuma, WidthLuma_minus1, 0)
                half_pel[0] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)       , 0, 255); 
                half_pel[3] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt), 0, 255);
                INTERPOLATE_RIGHT_SIDE3(xLuma+1, WidthLuma_minus1, 1)
                half_pel[1] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)       , 0, 255); 
                half_pel[4] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt), 0, 255);
              }
              else
              {
                INTERPOLATE_LEFT_SIDE3(xLuma, WidthLuma_minus1, 0)
                half_pel[0] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)       , 0, 255); 
                half_pel[3] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt), 0, 255);
                INTERPOLATE_LEFT_SIDE3(xLuma+1, WidthLuma_minus1, 1)
                half_pel[1] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)       , 0, 255); 
                half_pel[4] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt), 0, 255);
              }

              if(dispY_fractional==1)
              {
                *(syn_Y       +x2Luma  ) = ((*(ref_Y       +x_near[0])+half_pel[0]+1)>>1);
                *(syn_Y       +x2Luma_m) = ((*(ref_Y       +x_near[1])+half_pel[1]+1)>>1);
                *(syn_Y_Rownxt+x2Luma  ) = ((*(ref_Y_Rownxt+x_near[0])+half_pel[3]+1)>>1);
                *(syn_Y_Rownxt+x2Luma_m) = ((*(ref_Y_Rownxt+x_near[1])+half_pel[4]+1)>>1);
              }
              else if(dispY_fractional==2)
              {
                *(syn_Y       +x2Luma  ) = half_pel[0];
                *(syn_Y       +x2Luma_m) = half_pel[1];
                *(syn_Y_Rownxt+x2Luma  ) = half_pel[3];
                *(syn_Y_Rownxt+x2Luma_m) = half_pel[4];
              }
              else
              {
                *(syn_Y       +x2Luma  ) = ((*(ref_Y       +x_far[0])+half_pel[0]+1)>>1);
                *(syn_Y       +x2Luma_m) = ((*(ref_Y       +x_far[1])+half_pel[1]+1)>>1);
                *(syn_Y_Rownxt+x2Luma  ) = ((*(ref_Y_Rownxt+x_far[0])+half_pel[3]+1)>>1);
                *(syn_Y_Rownxt+x2Luma_m) = ((*(ref_Y_Rownxt+x_far[1])+half_pel[4]+1)>>1);
              }
            }
            else
            {
              *(syn_Y       +x2Luma  ) = *(ref_Y       +xLuma      );
              *(syn_Y       +x2Luma_m) = *(ref_Y       +xLuma+1    );
              *(syn_Y_Rownxt+x2Luma  ) = *(ref_Y_Rownxt+xLuma      );
              *(syn_Y_Rownxt+x2Luma_m) = *(ref_Y_Rownxt+xLuma+1    );
            }
          }
          else if(x2Luma < Width)
          {
            x2Luma_s = x2Luma_m = x2Luma;
            warping_valid_flag_Y = 1;
            if(dispY_fractional)
            {
              if(dispY_fractional<0)
              {
                INTERPOLATE_RIGHT_SIDE3(xLuma, WidthLuma_minus1, 0)
                half_pel[0] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)       , 0, 255); 
                half_pel[3] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt), 0, 255);
              }
              else
              {
                INTERPOLATE_LEFT_SIDE3(xLuma, WidthLuma_minus1, 0)
                half_pel[0] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)       , 0, 255); 
                half_pel[3] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt), 0, 255);
              }

              if(dispY_fractional==1)
              {
                *(syn_Y       +x2Luma  ) = ((*(ref_Y       +x_near[0])+half_pel[0]+1)>>1);
                *(syn_Y_Rownxt+x2Luma  ) = ((*(ref_Y_Rownxt+x_near[0])+half_pel[3]+1)>>1);
              }
              else if(dispY_fractional==2)
              {
                *(syn_Y       +x2Luma  ) = half_pel[0];
                *(syn_Y_Rownxt+x2Luma  ) = half_pel[3];
              }
              else
              {
                *(syn_Y       +x2Luma  ) = ((*(ref_Y       +x_far[0])+half_pel[0]+1)>>1);
                *(syn_Y_Rownxt+x2Luma  ) = ((*(ref_Y_Rownxt+x_far[0])+half_pel[3]+1)>>1);
              }
            }
            else
            {
              *(syn_Y       +x2Luma  ) = *(ref_Y       +xLuma      );
              *(syn_Y_Rownxt+x2Luma  ) = *(ref_Y_Rownxt+xLuma      );
            }
          }

          // Chroma
          x2Chroma   = x - dispUV_integer;
          x2Chroma_s = x2Chroma + 1;

          if(x2Chroma_s < WidthUV)
          {
            warping_valid_flag_UV = 1;
            if(dispUV_fractional)
            {
              if(dispUV_fractional<0)
              {
                INTERPOLATE_RIGHT_SIDE3(x,   WidthUV_minus1, 0)
#if !(NOKIA_CHROMA_BILINEAR)
                half_pel[0] = (imgpel)CLIP(AVC_HALF_PEL(ref_U), 0, 255); 
                half_pel[1] = (imgpel)CLIP(AVC_HALF_PEL(ref_V), 0, 255);
#endif
                INTERPOLATE_RIGHT_SIDE3(x+1, WidthUV_minus1, 1)
#if !(NOKIA_CHROMA_BILINEAR)
                half_pel[2] = (imgpel)CLIP(AVC_HALF_PEL(ref_U), 0, 255); 
                half_pel[3] = (imgpel)CLIP(AVC_HALF_PEL(ref_V), 0, 255);
#endif
              }
              else
              {
                INTERPOLATE_LEFT_SIDE3(x,   WidthUV_minus1, 0)
#if !(NOKIA_CHROMA_BILINEAR)
                half_pel[0] = (imgpel)CLIP(AVC_HALF_PEL(ref_U), 0, 255); 
                half_pel[1] = (imgpel)CLIP(AVC_HALF_PEL(ref_V), 0, 255);
#endif
                INTERPOLATE_LEFT_SIDE3(x+1, WidthUV_minus1, 1)
#if !(NOKIA_CHROMA_BILINEAR)
                half_pel[2] = (imgpel)CLIP(AVC_HALF_PEL(ref_U), 0, 255); 
                half_pel[3] = (imgpel)CLIP(AVC_HALF_PEL(ref_V), 0, 255);
#endif
              }

              if(dispUV_fractional==1)
              {
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+x2Chroma  ) = BILINEAR1(ref_U, x_near[0], x_far[0]);
                *(syn_V+x2Chroma  ) = BILINEAR1(ref_V, x_near[0], x_far[0]);
                *(syn_U+x2Chroma_s) = BILINEAR1(ref_U, x_near[1], x_far[1]);
                *(syn_V+x2Chroma_s) = BILINEAR1(ref_V, x_near[1], x_far[1]);
#else
                *(syn_U+x2Chroma  ) = ((*(ref_U+x_near[0])+half_pel[0]+1)>>1);
                *(syn_V+x2Chroma  ) = ((*(ref_V+x_near[0])+half_pel[1]+1)>>1);
                *(syn_U+x2Chroma_s) = ((*(ref_U+x_near[1])+half_pel[2]+1)>>1);
                *(syn_V+x2Chroma_s) = ((*(ref_V+x_near[1])+half_pel[3]+1)>>1);
#endif
              }
              else if(dispUV_fractional==2)
              {
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+x2Chroma  ) = BILINEAR2(ref_U, x_near[0], x_far[0]);
                *(syn_V+x2Chroma  ) = BILINEAR2(ref_V, x_near[0], x_far[0]);
                *(syn_U+x2Chroma_s) = BILINEAR2(ref_U, x_near[1], x_far[1]);
                *(syn_V+x2Chroma_s) = BILINEAR2(ref_V, x_near[1], x_far[1]);
#else
                *(syn_U+x2Chroma  ) = half_pel[0];
                *(syn_V+x2Chroma  ) = half_pel[1];
                *(syn_U+x2Chroma_s) = half_pel[2];
                *(syn_V+x2Chroma_s) = half_pel[3];
#endif
              }
              else
              {
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+x2Chroma  ) = BILINEAR3(ref_U, x_near[0], x_far[0]);
                *(syn_V+x2Chroma  ) = BILINEAR3(ref_V, x_near[0], x_far[0]);
                *(syn_U+x2Chroma_s) = BILINEAR3(ref_U, x_near[1], x_far[1]);
                *(syn_V+x2Chroma_s) = BILINEAR3(ref_V, x_near[1], x_far[1]);
#else
                *(syn_U+x2Chroma  ) = ((*(ref_U+x_far[0])+half_pel[0]+1)>>1);
                *(syn_V+x2Chroma  ) = ((*(ref_V+x_far[0])+half_pel[1]+1)>>1);
                *(syn_U+x2Chroma_s) = ((*(ref_U+x_far[1])+half_pel[2]+1)>>1);
                *(syn_V+x2Chroma_s) = ((*(ref_V+x_far[1])+half_pel[3]+1)>>1);
#endif
              }
            }
            else
            {
              int x_neighbor = CLIP(x+1, 0, WidthUV_minus1);
              *(syn_U+x2Chroma  ) = *(ref_U+x         );
              *(syn_V+x2Chroma  ) = *(ref_V+x         );
              *(syn_U+x2Chroma_s) = *(ref_U+x_neighbor);
              *(syn_V+x2Chroma_s) = *(ref_V+x_neighbor);
            }
            {
              int x_neighbor = CLIP(x+1, 0, WidthUV_minus1);
              *(dmap+x2Chroma  ) = *(ref_d+x         );
              *(dmap+x2Chroma_s) = *(ref_d+x_neighbor);
            }
          }
          else if(x2Chroma < WidthUV)
          {
            x2Chroma_s = x2Chroma;
            warping_valid_flag_UV = 1;
            if(dispUV_fractional)
            {
              if(dispUV_fractional<0)
              {
                INTERPOLATE_RIGHT_SIDE3(x, WidthUV_minus1, 0)
#if !(NOKIA_CHROMA_BILINEAR)
                half_pel[1] = (imgpel)CLIP(AVC_HALF_PEL(ref_U), 0, 255); 
                half_pel[2] = (imgpel)CLIP(AVC_HALF_PEL(ref_V), 0, 255);
#endif
              }
              else
              {
                INTERPOLATE_LEFT_SIDE3(x, WidthUV_minus1, 0)
#if !(NOKIA_CHROMA_BILINEAR)
                half_pel[1] = (imgpel)CLIP(AVC_HALF_PEL(ref_U), 0, 255); 
                half_pel[2] = (imgpel)CLIP(AVC_HALF_PEL(ref_V), 0, 255);
#endif
              }

              if(dispUV_fractional==1)
              {
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+x2Chroma  ) = BILINEAR1(ref_U, x_near[0], x_far[0]);
                *(syn_V+x2Chroma  ) = BILINEAR1(ref_V, x_near[0], x_far[0]);
#else
                *(syn_U+x2Chroma  ) = ((*(ref_U+x_near[0])+half_pel[0]+1)>>1);
                *(syn_V+x2Chroma  ) = ((*(ref_V+x_near[0])+half_pel[1]+1)>>1);
#endif
              }
              else if(dispUV_fractional==2)
              {
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+x2Chroma  ) = BILINEAR2(ref_U, x_near[0], x_far[0]);
                *(syn_V+x2Chroma  ) = BILINEAR2(ref_V, x_near[0], x_far[0]);
#else
                *(syn_U+x2Chroma  ) = half_pel[0];
                *(syn_V+x2Chroma  ) = half_pel[1];
#endif
              }
              else
              {
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+x2Chroma  ) = BILINEAR3(ref_U, x_near[0], x_far[0]);
                *(syn_V+x2Chroma  ) = BILINEAR3(ref_V, x_near[0], x_far[0]);
#else
                *(syn_U+x2Chroma  ) = ((*(ref_U+x_far [0])+half_pel[0]+1)>>1);
                *(syn_V+x2Chroma  ) = ((*(ref_V+x_far [0])+half_pel[1]+1)>>1);
#endif
              }
            }
            else
            {
              *(syn_U+x2Chroma  ) = *(ref_U+x         );
              *(syn_V+x2Chroma  ) = *(ref_V+x         );
            }
            *(dmap+x2Chroma  ) = *(ref_d+x         );
          }
        }
        /* --------- splatting/warping, end --------------*/

        /* --------- filling the holes between two valid pixel projections, start --------------*/
        if(warping_valid_flag_Y)
        {
          if(xLastLuma > x2Luma_s)
          {
            for(fill=xLastLuma-1; fill>x2Luma_s; fill--)
            {
              *(syn_Y        + fill) = *(syn_Y        + x2Luma_s);
              *(syn_Y_Rownxt + fill) = *(syn_Y_Rownxt + x2Luma_s);
            }
          }
          xLastLuma = x2Luma;
        }
        if(warping_valid_flag_UV)
        {
          if(xLastChroma > x2Chroma_s)
          {
            for(fill=xLastChroma-1; fill>x2Chroma_s; fill--)
            {
              *(syn_U+fill) = *(syn_U+x2Chroma_s);
              *(syn_V+fill) = *(syn_V+x2Chroma_s);
              *(dmap +fill) = *(dmap +x2Chroma_s);
            }
          }
          xLastChroma = x2Chroma;
        }
        /* --------- filling the holes between two valid pixel projections, end --------------*/

      }//end x loop

      if(xLastLuma != 0)
      {
        for(fill=xLastLuma-1; fill>=0; fill--)
        {
          *(syn_Y        + fill) = *(syn_Y        + xLastLuma);
          *(syn_Y_Rownxt + fill) = *(syn_Y_Rownxt + xLastLuma);
        }
      }
      if(xLastChroma!=0)
      {
        for(fill=xLastChroma-1; fill>=0; fill--)
        {
          *(syn_U + fill) = *(syn_U + xLastChroma);
          *(syn_V + fill) = *(syn_V + xLastChroma);
          *(dmap  + fill) = *(dmap  + xLastChroma);
        }
      }
    }//end y loop
  }
  else  //if( WarpToRight == 1) 
  {
    for (y = 0; y < Height/2; ++y)
    {
      ref_Y       =RefView[ViewId][0][2*y];
      ref_Y_Rownxt=RefView[ViewId][0][2*y+1];
      ref_U       =RefView[ViewId][1][y];
      ref_V       =RefView[ViewId][2][y];
      ref_d       =RefDepth[ViewId][y];

      syn_Y       =SynY[mem_id][2*y];
      syn_Y_Rownxt=SynY[mem_id][2*y+1];
      syn_U       =SynU[mem_id][y];
      syn_V       =SynV[mem_id][y];
      dmap        =Dmap[mem_id][y];
      memset(syn_Y,        0, Width *sizeof(imgpel));
      memset(syn_Y_Rownxt, 0, Width *sizeof(imgpel));
      memset(syn_U,        0, WidthUV*sizeof(imgpel));
      memset(syn_V,        0, WidthUV*sizeof(imgpel));

      pDepth = dmap;  //rev
      memset(pDepth, 0, WidthUV*sizeof(imgpel));
      xLastLuma = xLastChroma = -1;
      splatting_line = splatting_frame[y];

      for (x = 0; x <WidthUV;  ++x)
      {
        //get disparity
        crData = *(ref_d+x);  //depth of current pixel being warped

        xLuma = (x<<1);
#if (VSP_OPTIMIZATION)
        dispY_integer     = DisparityLookupTable[ViewId][crData].integer;
        dispY_fractional  = DisparityLookupTable[ViewId][crData].fractional;
        dispUV_integer    = DisparityLookupTable[ViewId][crData].integerUV;
        dispUV_fractional = DisparityLookupTable[ViewId][crData].fractionalUV;
#else
        {
#if POZNAN_NONLINEAR_DEPTH
          int in = nonlinear_depth_lut_backward[crData];
#else
          int in = crData;
#endif
          double z, db_disparity, db_disparity_subpel;

          z=1.0/( (in/255.0) * (1/Znear[ViewId] - 1/Zfar[ViewId]) + (1/Zfar[ViewId]) );
          db_disparity        = (FocalLength*LTranslation[ViewId]/z)-duPrincipal[ViewId];
          db_disparity_subpel = db_disparity*SubPelOption;
          dispY_integer    = (int)db_disparity;
          dispY_fractional = (int)(fabs(db_disparity_subpel)+0.5)-abs(dispY_integer)*SubPelOption;
          if(dispY_fractional==SubPelOption) {
            dispY_fractional = 0;
            dispY_integer += (db_disparity<0?-1:1);
          }
          if(SubPelOption==2) dispY_fractional *= 2;
          db_disparity        /= 2;
          db_disparity_subpel /= 2;
          dispUV_integer    = (int)db_disparity;
          dispUV_fractional = (int)(fabs(db_disparity_subpel)+0.5)-abs(dispUV_integer)*4;
          if(dispUV_fractional==SubPelOption)
          {
            dispUV_fractional = 0;
            dispUV_integer += (db_disparity<0?-1:1);
          }
          if(SubPelOption==2) dispUV_fractional *= 2;
        }
#endif // (VSP_OPTIMIZATION)

        /* --------- splatting/warping, start --------------*/
        warping_valid_flag_Y = warping_valid_flag_UV = 0;
        if(splatting_line[x] == NONE)
        {
          // Luma
          x2Luma = xLuma - dispY_integer;
          x2Luma_s = x2Luma + 1;

          if(x2Luma >= 0)
          {
            warping_valid_flag_Y = 1;
            if(dispY_fractional)
            {
              if(dispY_fractional>0)
              {
                INTERPOLATE_RIGHT_SIDE3(xLuma,   WidthLuma_minus1, 0)
                half_pel[0] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)         , 0, 255);
                half_pel[2] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt)  , 0, 255);
                INTERPOLATE_RIGHT_SIDE3(xLuma+1, WidthLuma_minus1, 1)
                half_pel[1] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)         , 0, 255);
                half_pel[3] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt)  , 0, 255);
              }
              else
              {
                INTERPOLATE_LEFT_SIDE3(xLuma,   WidthLuma_minus1, 0)
                half_pel[0] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)         , 0, 255);
                half_pel[2] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt)  , 0, 255);
                INTERPOLATE_LEFT_SIDE3(xLuma+1, WidthLuma_minus1, 1)
                half_pel[1] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)         , 0, 255);
                half_pel[3] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt)  , 0, 255);
              }
              if(dispY_fractional==1)
              {
                *(syn_Y       +x2Luma  ) = ((*(ref_Y       +x_near[0])+half_pel[0]+1)>>1);
                *(syn_Y       +x2Luma_s) = ((*(ref_Y       +x_near[1])+half_pel[1]+1)>>1);
                *(syn_Y_Rownxt+x2Luma  ) = ((*(ref_Y_Rownxt+x_near[0])+half_pel[2]+1)>>1);
                *(syn_Y_Rownxt+x2Luma_s) = ((*(ref_Y_Rownxt+x_near[1])+half_pel[3]+1)>>1);
              }
              else if(dispY_fractional==2)
              {
                *(syn_Y       +x2Luma  ) = half_pel[0];
                *(syn_Y       +x2Luma_s) = half_pel[1];
                *(syn_Y_Rownxt+x2Luma  ) = half_pel[2];
                *(syn_Y_Rownxt+x2Luma_s) = half_pel[3];
              }
              else
              {
                *(syn_Y       +x2Luma  ) = ((*(ref_Y       +x_far[0])+half_pel[0]+1)>>1);
                *(syn_Y       +x2Luma_s) = ((*(ref_Y       +x_far[1])+half_pel[1]+1)>>1);
                *(syn_Y_Rownxt+x2Luma  ) = ((*(ref_Y_Rownxt+x_far[0])+half_pel[2]+1)>>1);
                *(syn_Y_Rownxt+x2Luma_s) = ((*(ref_Y_Rownxt+x_far[1])+half_pel[3]+1)>>1);
              }
            }
            else
            {
              *(syn_Y       +x2Luma  )=*(ref_Y       +xLuma);
              *(syn_Y       +x2Luma_s)=*(ref_Y       +xLuma+1);
              *(syn_Y_Rownxt+x2Luma  )=*(ref_Y_Rownxt+xLuma);
              *(syn_Y_Rownxt+x2Luma_s)=*(ref_Y_Rownxt+xLuma+1);
            }
          }
          else if(x2Luma_s >0)
          {
            warping_valid_flag_Y = 1;
            x2Luma = x2Luma_s;
            if(dispY_fractional)
            {
              if(dispY_fractional>0)
                INTERPOLATE_RIGHT_SIDE3(xLuma+1, WidthLuma_minus1, 0)
              else
                INTERPOLATE_LEFT_SIDE3(xLuma+1, WidthLuma_minus1, 0)
              half_pel[0] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)         , 0, 255);
              half_pel[2] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt)  , 0, 255);
              if(dispY_fractional==1)
              {
                *(syn_Y       +x2Luma_s) = ((*(ref_Y       +x_near[0])+half_pel[0]+1)>>1);
                *(syn_Y_Rownxt+x2Luma_s) = ((*(ref_Y_Rownxt+x_near[0])+half_pel[2]+1)>>1);
              }
              else if(dispY_fractional==2)
              {
                *(syn_Y       +x2Luma_s) = half_pel[0];
                *(syn_Y_Rownxt+x2Luma_s) = half_pel[2];
              }
              else
              {
                *(syn_Y       +x2Luma_s) = ((*(ref_Y       +x_far[0])+half_pel[0]+1)>>1);
                *(syn_Y_Rownxt+x2Luma_s) = ((*(ref_Y_Rownxt+x_far[0])+half_pel[2]+1)>>1);
              }
            }
            else
            {
              *(syn_Y       +x2Luma_s)=*(ref_Y       +xLuma+1);
              *(syn_Y_Rownxt+x2Luma_s)=*(ref_Y_Rownxt+xLuma+1);
            }
          }

          // Chroma
          x2Chroma = x2Chroma_s =  x - dispUV_integer;
          if(x2Chroma >= 0)
          {
            warping_valid_flag_UV = 1;
            if(dispUV_fractional)
            {
              if(dispUV_fractional>0)
                INTERPOLATE_RIGHT_SIDE3(x, WidthUV_minus1, 0)
              else
                INTERPOLATE_LEFT_SIDE3(x, WidthUV_minus1, 0)
#if !(NOKIA_CHROMA_BILINEAR)
              half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_U),0,255);
              half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_V),0,255);
#endif
              if(dispUV_fractional==1)
              {
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+x2Chroma  ) = BILINEAR1(ref_U, x_near[0], x_far[0]);
                *(syn_V+x2Chroma  ) = BILINEAR1(ref_V, x_near[0], x_far[0]);
#else
                *(syn_U+x2Chroma) = ((*(ref_U+x_near[0])+half_pel[1]+1)>>1);
                *(syn_V+x2Chroma) = ((*(ref_V+x_near[0])+half_pel[2]+1)>>1);
#endif
              }
              else if(dispUV_fractional==2)
              {
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+x2Chroma  ) = BILINEAR2(ref_U, x_near[0], x_far[0]);
                *(syn_V+x2Chroma  ) = BILINEAR2(ref_V, x_near[0], x_far[0]);
#else
                *(syn_U+x2Chroma) = half_pel[1];
                *(syn_V+x2Chroma) = half_pel[2];
#endif
              }
              else
              {
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+x2Chroma  ) = BILINEAR3(ref_U, x_near[0], x_far[0]);
                *(syn_V+x2Chroma  ) = BILINEAR3(ref_V, x_near[0], x_far[0]);
#else
                *(syn_U+x2Chroma) = ((*(ref_U+x_far[0])+half_pel[1]+1)>>1);
                *(syn_V+x2Chroma) = ((*(ref_V+x_far[0])+half_pel[2]+1)>>1);
#endif
              }
            }
            else
            {
              *(syn_U+x2Chroma)=*(ref_U+x);
              *(syn_V+x2Chroma)=*(ref_V+x);
            }
            *(dmap+x2Chroma) = *(ref_d+x);
          }
        }
        else // if(splatting_line[x] == NONE)
        {
          // Luma
          x2Luma_m = xLuma - dispY_integer;
          x2Luma_s = x2Luma_m + 1;
          x2Luma   = x2Luma_m - 1;

          if(x2Luma >= 0)
          {
            warping_valid_flag_Y = 1;
            if(dispY_fractional)
            {
              if(dispY_fractional>0)
              {
                INTERPOLATE_RIGHT_SIDE3(xLuma-1, WidthLuma_minus1, 0)
                half_pel[0] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)       , 0, 255); 
                half_pel[3] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt), 0, 255);
                INTERPOLATE_RIGHT_SIDE3(xLuma  , WidthLuma_minus1, 1)
                half_pel[1] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)       , 0, 255); 
                half_pel[4] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt), 0, 255);
                INTERPOLATE_RIGHT_SIDE3(xLuma+1, WidthLuma_minus1, 2)
                half_pel[2] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)       , 0, 255); 
                half_pel[5] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt), 0, 255);
              }
              else
              {
                INTERPOLATE_LEFT_SIDE3(xLuma-1, WidthLuma_minus1, 0)
                half_pel[0] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)       , 0, 255); 
                half_pel[3] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt), 0, 255);
                INTERPOLATE_LEFT_SIDE3(xLuma  , WidthLuma_minus1, 1)
                half_pel[1] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)       , 0, 255); 
                half_pel[4] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt), 0, 255);
                INTERPOLATE_LEFT_SIDE3(xLuma+1, WidthLuma_minus1, 2)
                half_pel[2] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)       , 0, 255); 
                half_pel[5] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt), 0, 255);
              }

              if(dispY_fractional==1)
              {
                *(syn_Y       +x2Luma  ) = ((*(ref_Y       +x_near[0])+half_pel[0]+1)>>1);
                *(syn_Y       +x2Luma_m) = ((*(ref_Y       +x_near[1])+half_pel[1]+1)>>1);
                *(syn_Y       +x2Luma_s) = ((*(ref_Y       +x_near[2])+half_pel[2]+1)>>1);
                *(syn_Y_Rownxt+x2Luma  ) = ((*(ref_Y_Rownxt+x_near[0])+half_pel[3]+1)>>1);
                *(syn_Y_Rownxt+x2Luma_m) = ((*(ref_Y_Rownxt+x_near[1])+half_pel[4]+1)>>1);
                *(syn_Y_Rownxt+x2Luma_s) = ((*(ref_Y_Rownxt+x_near[2])+half_pel[5]+1)>>1);
              }
              else if(dispY_fractional==2)
              {
                *(syn_Y       +x2Luma  ) = half_pel[0];
                *(syn_Y       +x2Luma_m) = half_pel[1];
                *(syn_Y       +x2Luma_s) = half_pel[2];
                *(syn_Y_Rownxt+x2Luma  ) = half_pel[3];
                *(syn_Y_Rownxt+x2Luma_m) = half_pel[4];
                *(syn_Y_Rownxt+x2Luma_s) = half_pel[5];
              }
              else
              {
                *(syn_Y       +x2Luma  ) = ((*(ref_Y       +x_far[0])+half_pel[0]+1)>>1);
                *(syn_Y       +x2Luma_m) = ((*(ref_Y       +x_far[1])+half_pel[1]+1)>>1);
                *(syn_Y       +x2Luma_s) = ((*(ref_Y       +x_far[2])+half_pel[2]+1)>>1);
                *(syn_Y_Rownxt+x2Luma  ) = ((*(ref_Y_Rownxt+x_far[0])+half_pel[3]+1)>>1);
                *(syn_Y_Rownxt+x2Luma_m) = ((*(ref_Y_Rownxt+x_far[1])+half_pel[4]+1)>>1);
                *(syn_Y_Rownxt+x2Luma_s) = ((*(ref_Y_Rownxt+x_far[2])+half_pel[5]+1)>>1);
              }
            }
            else
            {
              int x_neighbor = CLIP(xLuma-1, 0, WidthLuma_minus1);
              *(syn_Y       +x2Luma  ) = *(ref_Y       +x_neighbor);
              *(syn_Y       +x2Luma_m) = *(ref_Y       +xLuma     );
              *(syn_Y       +x2Luma_s) = *(ref_Y       +xLuma+1   );
              *(syn_Y_Rownxt+x2Luma  ) = *(ref_Y_Rownxt+x_neighbor);
              *(syn_Y_Rownxt+x2Luma_m) = *(ref_Y_Rownxt+xLuma     );
              *(syn_Y_Rownxt+x2Luma_s) = *(ref_Y_Rownxt+xLuma+1   );
            }
          }
          else if(x2Luma_m >= 0)
          {
            x2Luma = x2Luma_m;
            warping_valid_flag_Y = 1;
            if(dispY_fractional)
            {
              if(dispY_fractional>0)
              {
                INTERPOLATE_RIGHT_SIDE3(xLuma  , WidthLuma_minus1, 1)
                half_pel[1] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)       , 0, 255); 
                half_pel[4] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt), 0, 255);
                INTERPOLATE_RIGHT_SIDE3(xLuma+1, WidthLuma_minus1, 2)
                half_pel[2] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)       , 0, 255); 
                half_pel[5] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt), 0, 255);
              }
              else
              {
                INTERPOLATE_LEFT_SIDE3(xLuma  , WidthLuma_minus1, 1)
                half_pel[1] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)       , 0, 255); 
                half_pel[4] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt), 0, 255);
                INTERPOLATE_LEFT_SIDE3(xLuma+1, WidthLuma_minus1, 2)
                half_pel[2] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)       , 0, 255); 
                half_pel[5] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt), 0, 255);
              }

              if(dispY_fractional==1)
              {
                *(syn_Y       +x2Luma_m) = ((*(ref_Y       +x_near[1])+half_pel[1]+1)>>1);
                *(syn_Y       +x2Luma_s) = ((*(ref_Y       +x_near[2])+half_pel[2]+1)>>1);
                *(syn_Y_Rownxt+x2Luma_m) = ((*(ref_Y_Rownxt+x_near[1])+half_pel[4]+1)>>1);
                *(syn_Y_Rownxt+x2Luma_s) = ((*(ref_Y_Rownxt+x_near[2])+half_pel[5]+1)>>1);
              }
              else if(dispY_fractional==2)
              {
                *(syn_Y       +x2Luma_m) = half_pel[1];
                *(syn_Y       +x2Luma_s) = half_pel[2];
                *(syn_Y_Rownxt+x2Luma_m) = half_pel[4];
                *(syn_Y_Rownxt+x2Luma_s) = half_pel[5];
              }
              else
              {
                *(syn_Y       +x2Luma_m) = ((*(ref_Y       +x_far[1])+half_pel[1]+1)>>1);
                *(syn_Y       +x2Luma_s) = ((*(ref_Y       +x_far[2])+half_pel[2]+1)>>1);
                *(syn_Y_Rownxt+x2Luma_m) = ((*(ref_Y_Rownxt+x_far[1])+half_pel[4]+1)>>1);
                *(syn_Y_Rownxt+x2Luma_s) = ((*(ref_Y_Rownxt+x_far[2])+half_pel[5]+1)>>1);
              }
            }
            else
            {
              *(syn_Y       +x2Luma_m) = *(ref_Y       +xLuma     );
              *(syn_Y       +x2Luma_s) = *(ref_Y       +xLuma+1   );
              *(syn_Y_Rownxt+x2Luma_m) = *(ref_Y_Rownxt+xLuma     );
              *(syn_Y_Rownxt+x2Luma_s) = *(ref_Y_Rownxt+xLuma+1   );
            }
          }
          else if(x2Luma_s >= 0)
          {
            x2Luma = x2Luma_m = x2Luma_s;
            warping_valid_flag_Y = 1;
            if(dispY_fractional)
            {
              if(dispY_fractional>0)
              {
                INTERPOLATE_RIGHT_SIDE3(xLuma+1, WidthLuma_minus1, 2)
                half_pel[2] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)       , 0, 255); 
                half_pel[5] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt), 0, 255);
              }
              else
              {
                INTERPOLATE_LEFT_SIDE3(xLuma+1, WidthLuma_minus1, 2)
                half_pel[2] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)       , 0, 255); 
                half_pel[5] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt), 0, 255);
              }

              if(dispY_fractional==1)
              {
                *(syn_Y       +x2Luma_s) = ((*(ref_Y       +x_near[2])+half_pel[2]+1)>>1);
                *(syn_Y_Rownxt+x2Luma_s) = ((*(ref_Y_Rownxt+x_near[2])+half_pel[5]+1)>>1);
              }
              else if(dispY_fractional==2)
              {
                *(syn_Y       +x2Luma_s) = half_pel[2];
                *(syn_Y_Rownxt+x2Luma_s) = half_pel[5];
              }
              else
              {
                *(syn_Y       +x2Luma_s) = ((*(ref_Y       +x_far[2])+half_pel[2]+1)>>1);
                *(syn_Y_Rownxt+x2Luma_s) = ((*(ref_Y_Rownxt+x_far[2])+half_pel[5]+1)>>1);
              }
            }
            else
            {
              *(syn_Y       +x2Luma_s) = *(ref_Y       +xLuma+1   );
              *(syn_Y_Rownxt+x2Luma_s) = *(ref_Y_Rownxt+xLuma+1   );
            }
          }

          // Chroma
          x2Chroma_s = x - dispUV_integer;
          x2Chroma   = x2Chroma_s - 1;

          if(x2Chroma >= 0)
          {
            warping_valid_flag_UV = 1;
            if(dispUV_fractional)
            {
              if(dispUV_fractional>0)
              {
                INTERPOLATE_RIGHT_SIDE3(x-1, WidthUV_minus1, 0)
#if !(NOKIA_CHROMA_BILINEAR)
                half_pel[0] = (imgpel)CLIP(AVC_HALF_PEL(ref_U), 0, 255); 
                half_pel[1] = (imgpel)CLIP(AVC_HALF_PEL(ref_V), 0, 255);
#endif
                INTERPOLATE_RIGHT_SIDE3(x  , WidthUV_minus1, 1)
#if !(NOKIA_CHROMA_BILINEAR)
                half_pel[2] = (imgpel)CLIP(AVC_HALF_PEL(ref_U), 0, 255); 
                half_pel[3] = (imgpel)CLIP(AVC_HALF_PEL(ref_V), 0, 255);
#endif
              }
              else
              {
                INTERPOLATE_LEFT_SIDE3(x-1, WidthUV_minus1, 0)
#if !(NOKIA_CHROMA_BILINEAR)
                half_pel[0] = (imgpel)CLIP(AVC_HALF_PEL(ref_U), 0, 255); 
                half_pel[1] = (imgpel)CLIP(AVC_HALF_PEL(ref_V), 0, 255);
#endif
                INTERPOLATE_LEFT_SIDE3(x  , WidthUV_minus1, 1)
#if !(NOKIA_CHROMA_BILINEAR)
                half_pel[2] = (imgpel)CLIP(AVC_HALF_PEL(ref_U), 0, 255); 
                half_pel[3] = (imgpel)CLIP(AVC_HALF_PEL(ref_V), 0, 255);
#endif
              }

              if(dispUV_fractional==1)
              {
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+x2Chroma  ) = BILINEAR1(ref_U, x_near[0], x_far[0]);
                *(syn_V+x2Chroma  ) = BILINEAR1(ref_V, x_near[0], x_far[0]);
                *(syn_U+x2Chroma_s) = BILINEAR1(ref_U, x_near[1], x_far[1]);
                *(syn_V+x2Chroma_s) = BILINEAR1(ref_V, x_near[1], x_far[1]);
#else
                *(syn_U+x2Chroma  ) = ((*(ref_U+x_near[0])+half_pel[0]+1)>>1);
                *(syn_V+x2Chroma  ) = ((*(ref_V+x_near[0])+half_pel[1]+1)>>1);
                *(syn_U+x2Chroma_s) = ((*(ref_U+x_near[1])+half_pel[2]+1)>>1);
                *(syn_V+x2Chroma_s) = ((*(ref_V+x_near[1])+half_pel[3]+1)>>1);
#endif
              }
              else if(dispUV_fractional==2)
              {
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+x2Chroma  ) = BILINEAR2(ref_U, x_near[0], x_far[0]);
                *(syn_V+x2Chroma  ) = BILINEAR2(ref_V, x_near[0], x_far[0]);
                *(syn_U+x2Chroma_s) = BILINEAR2(ref_U, x_near[1], x_far[1]);
                *(syn_V+x2Chroma_s) = BILINEAR2(ref_V, x_near[1], x_far[1]);
#else
                *(syn_U+x2Chroma  ) = half_pel[0];
                *(syn_V+x2Chroma  ) = half_pel[1];
                *(syn_U+x2Chroma_s) = half_pel[2];
                *(syn_V+x2Chroma_s) = half_pel[3];
#endif
              }
              else
              {
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+x2Chroma  ) = BILINEAR3(ref_U, x_near[0], x_far[0]);
                *(syn_V+x2Chroma  ) = BILINEAR3(ref_V, x_near[0], x_far[0]);
                *(syn_U+x2Chroma_s) = BILINEAR3(ref_U, x_near[1], x_far[1]);
                *(syn_V+x2Chroma_s) = BILINEAR3(ref_V, x_near[1], x_far[1]);
#else
                *(syn_U+x2Chroma  ) = ((*(ref_U+x_far[0])+half_pel[0]+1)>>1);
                *(syn_V+x2Chroma  ) = ((*(ref_V+x_far[0])+half_pel[1]+1)>>1);
                *(syn_U+x2Chroma_s) = ((*(ref_U+x_far[1])+half_pel[2]+1)>>1);
                *(syn_V+x2Chroma_s) = ((*(ref_V+x_far[1])+half_pel[3]+1)>>1);
#endif
              }
            }
            else
            {
              int x_neighbor = CLIP(x-1, 0, WidthUV_minus1);
              *(syn_U+x2Chroma  ) = *(ref_U+x_neighbor);
              *(syn_V+x2Chroma  ) = *(ref_V+x_neighbor);
              *(syn_U+x2Chroma_s) = *(ref_U+x         );
              *(syn_V+x2Chroma_s) = *(ref_V+x         );
            }
            {
              int x_neighbor = CLIP(x-1, 0, WidthUV_minus1);
              *(dmap+x2Chroma  ) = *(ref_d+x_neighbor);
              *(dmap+x2Chroma_s) = *(ref_d+x         );
            }
          }
          else if(x2Chroma_s >= 0)
          {
            x2Chroma = x2Chroma_s;
            warping_valid_flag_UV = 1;
            if(dispUV_fractional)
            {
              if(dispUV_fractional>0)
              {
                INTERPOLATE_RIGHT_SIDE3(x, WidthUV_minus1, 0)
#if !(NOKIA_CHROMA_BILINEAR)
                half_pel[1] = (imgpel)CLIP(AVC_HALF_PEL(ref_U), 0, 255); 
                half_pel[2] = (imgpel)CLIP(AVC_HALF_PEL(ref_V), 0, 255);
#endif
              }
              else
              {
                INTERPOLATE_LEFT_SIDE3(x, WidthUV_minus1, 0)
#if !(NOKIA_CHROMA_BILINEAR)
                half_pel[1] = (imgpel)CLIP(AVC_HALF_PEL(ref_U), 0, 255); 
                half_pel[2] = (imgpel)CLIP(AVC_HALF_PEL(ref_V), 0, 255);
#endif
              }

              if(dispUV_fractional==1)
              {
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+x2Chroma_s) = BILINEAR1(ref_U, x_near[0], x_far[0]);
                *(syn_V+x2Chroma_s) = BILINEAR1(ref_V, x_near[0], x_far[0]);
#else
                *(syn_U+x2Chroma_s) = ((*(ref_U+x_near[0])+half_pel[0]+1)>>1);
                *(syn_V+x2Chroma_s) = ((*(ref_V+x_near[0])+half_pel[1]+1)>>1);
#endif
              }
              else if(dispUV_fractional==2)
              {
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+x2Chroma_s) = BILINEAR2(ref_U, x_near[0], x_far[0]);
                *(syn_V+x2Chroma_s) = BILINEAR2(ref_V, x_near[0], x_far[0]);
#else
                *(syn_U+x2Chroma_s) = half_pel[0];
                *(syn_V+x2Chroma_s) = half_pel[1];
#endif
              }
              else
              {
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+x2Chroma_s) = BILINEAR3(ref_U, x_near[0], x_far[0]);
                *(syn_V+x2Chroma_s) = BILINEAR3(ref_V, x_near[0], x_far[0]);
#else
                *(syn_U+x2Chroma_s) = ((*(ref_U+x_far [0])+half_pel[0]+1)>>1);
                *(syn_V+x2Chroma_s) = ((*(ref_V+x_far [0])+half_pel[1]+1)>>1);
#endif
              }
            }
            else
            {
              *(syn_U+x2Chroma_s) = *(ref_U+x);
              *(syn_V+x2Chroma_s) = *(ref_V+x);
            }
            *(dmap+x2Chroma_s) = *(ref_d+x);
          }
        }
        /* --------- splatting/warping, end --------------*/


        /* --------- filling the holes between two valid pixel projections, start --------------*/
        if(warping_valid_flag_Y)
        {
          if(xLastLuma < x2Luma)
          {
            for(fill=xLastLuma+1; fill<x2Luma; fill++)
            {
              *(syn_Y        + fill) = *(syn_Y        + x2Luma);
              *(syn_Y_Rownxt + fill) = *(syn_Y_Rownxt + x2Luma);
            }
          }
          xLastLuma = x2Luma_s;
        }
        if(warping_valid_flag_UV)
        {
          if(xLastChroma < x2Chroma)
          {
            for(fill=xLastChroma+1; fill<x2Chroma; fill++)
            {
              *(syn_U+fill) = *(syn_U+x2Chroma);
              *(syn_V+fill) = *(syn_V+x2Chroma);
              *(dmap +fill) = *(dmap +x2Chroma);
            }
          }
          xLastChroma = x2Chroma_s;
        }
        /* --------- filling the holes between two valid pixel projections, end --------------*/

      }//end x loop

      if(xLastLuma != Width)
      {
        for(fill=xLastLuma+1; fill<Width; fill++)
        {
          *(syn_Y        + fill) = *(syn_Y        + xLastLuma);
          *(syn_Y_Rownxt + fill) = *(syn_Y_Rownxt + xLastLuma);
        }
      }
      if(xLastChroma!=WidthUV)
      {
        for(fill=xLastChroma+1; fill<WidthUV; fill++)
        {
          *(syn_U + fill) = *(syn_U + xLastChroma);
          *(syn_V + fill) = *(syn_V + xLastChroma);
          *(dmap  + fill) = *(dmap  + xLastChroma);
        }
      }
    }//end y loop
  }

  if(SynDirection==2)
  {
    for(y = 0; y < Height/2; ++y)
    {
      imgpel* syn_hlf_Y=SynY[HLFLVIEW][2*y];
      imgpel* syn_hlf_Y_Rownxt=SynY[HLFLVIEW][2*y+1];

      imgpel* syn_hlf_U=SynU[HLFLVIEW][y];
      imgpel* syn_hlf_V=SynV[HLFLVIEW][y];
      imgpel* syn_hlf_D=Dmap[HLFLVIEW][y];

      imgpel* syn_merge_Y=SynY[ViewId][2*y];
      imgpel* syn_merge_Y_Rownxt=SynY[ViewId][2*y+1];

      imgpel* syn_merge_U=SynU[ViewId][y];
      imgpel* syn_merge_V=SynV[ViewId][y];
      imgpel* syn_merge_D=Dmap[ViewId][y];

      memcpy(syn_hlf_Y,syn_merge_Y,WidthUV*2*sizeof(imgpel));
      memcpy(syn_hlf_Y_Rownxt,syn_merge_Y_Rownxt,WidthUV*2*sizeof(imgpel));

      memcpy(syn_hlf_U,syn_merge_U,WidthUV*sizeof(imgpel));
      memcpy(syn_hlf_V,syn_merge_V,WidthUV*sizeof(imgpel));
      memcpy(syn_hlf_D,syn_merge_D,WidthUV*sizeof(imgpel));
    }
  }
}


void CViewInterpolation1D::ViewGen_1LWH420(int ViewId)
{
  int i, x, y;
  int WidthUV = Width>>1;
  int WidthUV_minus1 = WidthUV - 1;
  int WidthLuma_minus1 = Width-1;
  int mem_id = SynDirection==2 ? ViewId : FINLVIEW;

  int xLastLuma, xLastChroma, fill;
  int dispY_integer, dispUV_integer;
  int dispY_fractional, dispUV_fractional;
  int warping_valid_flag_Y=0, warping_valid_flag_UV=0;
  int x2Chroma, x2Chroma_s;
  int x2Luma, x2Luma_s;

  imgpel* ref_Y=NULL;
  imgpel* ref_Y_Rownxt=NULL;

  imgpel* ref_U=NULL;
  imgpel* ref_V=NULL;
  imgpel* ref_d=NULL;
  imgpel* dmap=NULL;
  
  imgpel* syn_Y=NULL;
  imgpel* syn_Y_Rownxt=NULL;
  imgpel* syn_U=NULL;
  imgpel* syn_V=NULL;
  imgpel* pDepth = NULL; //rev
  int  xLuma;
  int  p0, p1, p2, p3, p4, p5;
  int  x_far[3], x_near[3];
  imgpel half_pel[6];
  imgpel  crData;

  if(SynDirection==2)
  {
    for (i = 0; i < 3; i++)
    {
      memset(SynY[i][0], 0,         Width*Height*SubPelOption*sizeof(imgpel));
      memset(SynU[i][0], 0,         Width*Height/4*SubPelOption*sizeof(imgpel));
      memset(SynV[i][0], 0,         Width*Height/4*SubPelOption*sizeof(imgpel));
      memset(Dmap[i][0], 0,         Width*Height/4*SubPelOption*sizeof(byte));
    }
  }

  if(ViewId == LEFTVIEW)
    WarpToRight = 1;
  else
    WarpToRight = 0;

  //start to warp each pixel
  if( WarpToRight == 0) // right view-- from right 2 left
  {
    for (y = 0; y < Height/2; ++y)
    {
      ref_Y=RefView[ViewId][0][2*y];
      ref_Y_Rownxt=RefView[ViewId][0][2*y+1];

      ref_U=RefView[ViewId][1][y];
      ref_V=RefView[ViewId][2][y];
      ref_d=RefDepth[ViewId][y];

      syn_Y=SynY[mem_id][2*y];
      syn_Y_Rownxt=SynY[mem_id][2*y+1];
      syn_U=SynU[mem_id][y];
      syn_V=SynV[mem_id][y];
      dmap=Dmap[mem_id][y];
      memset(syn_Y, 0, Width*sizeof(imgpel));
      memset(syn_Y_Rownxt, 0, Width*sizeof(imgpel));
      memset(syn_U, 0, WidthUV*sizeof(imgpel));
      memset(syn_V, 0, WidthUV*sizeof(imgpel));

      pDepth = dmap;  //rev
      memset(pDepth, 0, WidthUV*sizeof(imgpel));
      xLastLuma = Width;
      xLastChroma = WidthUV;

      for (x = WidthUV_minus1; x >=0;  x--)
      {
        //get disparity
        crData = *(ref_d+x);  //depth of current pixel being warped

        xLuma = (x<<1);
#if (VSP_OPTIMIZATION)
        dispY_integer     = DisparityLookupTable[ViewId][crData].integer;
        dispY_fractional  = DisparityLookupTable[ViewId][crData].fractional;
        dispUV_integer    = DisparityLookupTable[ViewId][crData].integerUV;
        dispUV_fractional = DisparityLookupTable[ViewId][crData].fractionalUV;
#else
        {
#if POZNAN_NONLINEAR_DEPTH
          int in = nonlinear_depth_lut_backward[crData];
#else
          int in = crData;
#endif
          double z, db_disparity, db_disparity_subpel;

          z=1.0/( (in/255.0) * (1/Znear[ViewId] - 1/Zfar[ViewId]) + (1/Zfar[ViewId]) );
          db_disparity        = (FocalLength*LTranslation[ViewId]/z)-duPrincipal[ViewId];
          db_disparity_subpel = db_disparity*SubPelOption;
          dispY_integer    = (int)db_disparity;
          dispY_fractional = (int)(fabs(db_disparity_subpel)+0.5)-abs(dispY_integer)*SubPelOption;
          if(dispY_fractional==SubPelOption) {
            dispY_fractional = 0;
            dispY_integer += (db_disparity<0?-1:1);
          }
          if(SubPelOption==2) dispY_fractional *= 2;
          db_disparity        /= 2;
          db_disparity_subpel /= 2;
          dispUV_integer    = (int)db_disparity;
          dispUV_fractional = (int)(fabs(db_disparity_subpel)+0.5)-abs(dispUV_integer)*4;
          if(dispUV_fractional==SubPelOption)
          {
            dispUV_fractional = 0;
            dispUV_integer += (db_disparity<0?-1:1);
          }
          if(SubPelOption==2) dispUV_fractional *= 2;
        }
#endif // (VSP_OPTIMIZATION)

        /* --------- warping, start --------------*/
        warping_valid_flag_Y = warping_valid_flag_UV = 0;
        {
          // Luma
          x2Luma = xLuma - dispY_integer;
          x2Luma_s = x2Luma + 1;
          if(x2Luma_s < Width)
          {
            warping_valid_flag_Y = 1;
            if(dispY_fractional)
            {
              if(dispY_fractional<0)
              {
                INTERPOLATE_RIGHT_SIDE3(xLuma,   WidthLuma_minus1, 0)
                half_pel[0] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)       , 0, 255);
                half_pel[2] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt), 0, 255);
                INTERPOLATE_RIGHT_SIDE3(xLuma+1, WidthLuma_minus1, 1)
                half_pel[1] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)       , 0, 255);
                half_pel[3] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt), 0, 255);
              }
              else
              {
                INTERPOLATE_LEFT_SIDE3(xLuma,   WidthLuma_minus1, 0)
                half_pel[0] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)       , 0, 255);
                half_pel[2] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt), 0, 255);
                INTERPOLATE_LEFT_SIDE3(xLuma+1, WidthLuma_minus1, 1)
                half_pel[1] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)       , 0, 255);
                half_pel[3] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt), 0, 255);
              }
              if(dispY_fractional==1)
              {
                *(syn_Y       +x2Luma  ) = ((*(ref_Y       +x_near[0])+half_pel[0]+1)>>1);
                *(syn_Y       +x2Luma_s) = ((*(ref_Y       +x_near[1])+half_pel[1]+1)>>1);
                *(syn_Y_Rownxt+x2Luma  ) = ((*(ref_Y_Rownxt+x_near[0])+half_pel[2]+1)>>1);
                *(syn_Y_Rownxt+x2Luma_s) = ((*(ref_Y_Rownxt+x_near[1])+half_pel[3]+1)>>1);
              }
              else if(dispY_fractional==2)
              {
                *(syn_Y       +x2Luma  ) = half_pel[0];
                *(syn_Y       +x2Luma_s) = half_pel[1];
                *(syn_Y_Rownxt+x2Luma  ) = half_pel[2];
                *(syn_Y_Rownxt+x2Luma_s) = half_pel[3];
              }
              else
              {
                *(syn_Y       +x2Luma  ) = ((*(ref_Y       +x_far[0])+half_pel[0]+1)>>1);
                *(syn_Y       +x2Luma_s) = ((*(ref_Y       +x_far[1])+half_pel[1]+1)>>1);
                *(syn_Y_Rownxt+x2Luma  ) = ((*(ref_Y_Rownxt+x_far[0])+half_pel[2]+1)>>1);
                *(syn_Y_Rownxt+x2Luma_s) = ((*(ref_Y_Rownxt+x_far[1])+half_pel[3]+1)>>1);
              }
            }
            else
            {
              *(syn_Y       +x2Luma  )=*(ref_Y       +xLuma);
              *(syn_Y       +x2Luma_s)=*(ref_Y       +xLuma+1);
              *(syn_Y_Rownxt+x2Luma  )=*(ref_Y_Rownxt+xLuma);
              *(syn_Y_Rownxt+x2Luma_s)=*(ref_Y_Rownxt+xLuma+1);
            }
          }
          else if(x2Luma < Width)
          {
            warping_valid_flag_Y = 1;
            x2Luma_s = x2Luma;
            if(dispY_fractional)
            {
              if(dispY_fractional<0)
                INTERPOLATE_RIGHT_SIDE3(xLuma, WidthLuma_minus1, 0)
              else
                INTERPOLATE_LEFT_SIDE3(xLuma, WidthLuma_minus1, 0)
              half_pel[0] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)         , 0, 255);
              half_pel[2] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt)  , 0, 255);
              if(dispY_fractional==1)
              {
                *(syn_Y       +x2Luma  ) = ((*(ref_Y       +x_near[0])+half_pel[0]+1)>>1);
                *(syn_Y_Rownxt+x2Luma  ) = ((*(ref_Y_Rownxt+x_near[0])+half_pel[2]+1)>>1);
              }
              else if(dispY_fractional==2)
              {
                *(syn_Y       +x2Luma  ) = half_pel[0];
                *(syn_Y_Rownxt+x2Luma  ) = half_pel[2];
              }
              else
              {
                *(syn_Y       +x2Luma  ) = ((*(ref_Y       +x_far[0])+half_pel[0]+1)>>1);
                *(syn_Y_Rownxt+x2Luma  ) = ((*(ref_Y_Rownxt+x_far[0])+half_pel[2]+1)>>1);
              }
            }
            else
            {
              *(syn_Y       +x2Luma  )=*(ref_Y       +xLuma);
              *(syn_Y_Rownxt+x2Luma  )=*(ref_Y_Rownxt+xLuma);
            }
          }

          // Chroma
          x2Chroma = x2Chroma_s =  x - dispUV_integer;
          if(x2Chroma < WidthUV)
          {
            warping_valid_flag_UV = 1;
            if(dispUV_fractional)
            {
              if(dispUV_fractional<0)
                INTERPOLATE_RIGHT_SIDE3(x, WidthUV_minus1, 0)
              else
                INTERPOLATE_LEFT_SIDE3(x, WidthUV_minus1, 0)
#if !(NOKIA_CHROMA_BILINEAR)
              half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_U),0,255);
              half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_V),0,255);
#endif
              if(dispUV_fractional==1)
              {
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+x2Chroma) = BILINEAR1(ref_U, x_near[0], x_far[0]);
                *(syn_V+x2Chroma) = BILINEAR1(ref_V, x_near[0], x_far[0]);
#else
                *(syn_U+x2Chroma) = ((*(ref_U+x_near[0])+half_pel[1]+1)>>1);
                *(syn_V+x2Chroma) = ((*(ref_V+x_near[0])+half_pel[2]+1)>>1);
#endif
              }
              else if(dispUV_fractional==2)
              {
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+x2Chroma) = BILINEAR2(ref_U, x_near[0], x_far[0]);
                *(syn_V+x2Chroma) = BILINEAR2(ref_V, x_near[0], x_far[0]);
#else
                *(syn_U+x2Chroma) = half_pel[1];
                *(syn_V+x2Chroma) = half_pel[2];
#endif
              }
              else
              {
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+x2Chroma) = BILINEAR3(ref_U, x_near[0], x_far[0]);
                *(syn_V+x2Chroma) = BILINEAR3(ref_V, x_near[0], x_far[0]);
#else
                *(syn_U+x2Chroma) = ((*(ref_U+x_far[0])+half_pel[1]+1)>>1);
                *(syn_V+x2Chroma) = ((*(ref_V+x_far[0])+half_pel[2]+1)>>1);
#endif
              }
            }
            else
            {
              *(syn_U+x2Chroma)=*(ref_U+x);
              *(syn_V+x2Chroma)=*(ref_V+x);
            }
            *(dmap+x2Chroma) = *(ref_d+x);
          }
        }
        /* --------- warping, end --------------*/

        /* --------- filling the holes between two valid pixel projections, start --------------*/
        if(warping_valid_flag_Y)
        {
          if(xLastLuma > x2Luma_s)
          {
            for(fill=xLastLuma-1; fill>x2Luma_s; fill--)
            {
              *(syn_Y        + fill) = *(syn_Y        + x2Luma_s);
              *(syn_Y_Rownxt + fill) = *(syn_Y_Rownxt + x2Luma_s);
            }
          }
          xLastLuma = x2Luma;
        }
        if(warping_valid_flag_UV)
        {
          if(xLastChroma > x2Chroma_s)
          {
            for(fill=xLastChroma-1; fill>x2Chroma_s; fill--)
            {
              *(syn_U+fill) = *(syn_U+x2Chroma_s);
              *(syn_V+fill) = *(syn_V+x2Chroma_s);
              *(dmap +fill) = *(dmap +x2Chroma_s);
            }
          }
          xLastChroma = x2Chroma;
        }
        /* --------- filling the holes between two valid pixel projections, end --------------*/

      }//end x loop

      if(xLastLuma != 0)
      {
        for(fill=xLastLuma-1; fill>=0; fill--)
        {
          *(syn_Y        + fill) = *(syn_Y        + xLastLuma);
          *(syn_Y_Rownxt + fill) = *(syn_Y_Rownxt + xLastLuma);
        }
      }
      if(xLastChroma!=0)
      {
        for(fill=xLastChroma-1; fill>=0; fill--)
        {
          *(syn_U + fill) = *(syn_U + xLastChroma);
          *(syn_V + fill) = *(syn_V + xLastChroma);
          *(dmap  + fill) = *(dmap  + xLastChroma);
        }
      }
    }//end y loop
  }
  else  //if( WarpToRight == 1) 
  {
    for (y = 0; y < Height/2; ++y)
    {
      ref_Y       =RefView[ViewId][0][2*y];
      ref_Y_Rownxt=RefView[ViewId][0][2*y+1];
      ref_U       =RefView[ViewId][1][y];
      ref_V       =RefView[ViewId][2][y];
      ref_d       =RefDepth[ViewId][y];

      syn_Y       =SynY[mem_id][2*y];
      syn_Y_Rownxt=SynY[mem_id][2*y+1];
      syn_U       =SynU[mem_id][y];
      syn_V       =SynV[mem_id][y];
      dmap        =Dmap[mem_id][y];
      memset(syn_Y,        0, Width *sizeof(imgpel));
      memset(syn_Y_Rownxt, 0, Width *sizeof(imgpel));
      memset(syn_U,        0, WidthUV*sizeof(imgpel));
      memset(syn_V,        0, WidthUV*sizeof(imgpel));

      pDepth = dmap;  //rev
      memset(pDepth, 0, WidthUV*sizeof(imgpel));
      xLastLuma = xLastChroma = -1;

      for (x = 0; x <WidthUV;  ++x)
      {
        //get disparity
        crData = *(ref_d+x);  //depth of current pixel being warped

        xLuma = (x<<1);
#if (VSP_OPTIMIZATION)
        dispY_integer     = DisparityLookupTable[ViewId][crData].integer;
        dispY_fractional  = DisparityLookupTable[ViewId][crData].fractional;
        dispUV_integer    = DisparityLookupTable[ViewId][crData].integerUV;
        dispUV_fractional = DisparityLookupTable[ViewId][crData].fractionalUV;
#else
        {
#if POZNAN_NONLINEAR_DEPTH
          int in = nonlinear_depth_lut_backward[crData];
#else
          int in = crData;
#endif
          double z, db_disparity, db_disparity_subpel;

          z=1.0/( (in/255.0) * (1/Znear[ViewId] - 1/Zfar[ViewId]) + (1/Zfar[ViewId]) );
          db_disparity        = (FocalLength*LTranslation[ViewId]/z)-duPrincipal[ViewId];
          db_disparity_subpel = db_disparity*SubPelOption;
          dispY_integer    = (int)db_disparity;
          dispY_fractional = (int)(fabs(db_disparity_subpel)+0.5)-abs(dispY_integer)*SubPelOption;
          if(dispY_fractional==SubPelOption) {
            dispY_fractional = 0;
            dispY_integer += (db_disparity<0?-1:1);
          }
          if(SubPelOption==2) dispY_fractional *= 2;
          db_disparity        /= 2;
          db_disparity_subpel /= 2;
          dispUV_integer    = (int)db_disparity;
          dispUV_fractional = (int)(fabs(db_disparity_subpel)+0.5)-abs(dispUV_integer)*4;
          if(dispUV_fractional==SubPelOption)
          {
            dispUV_fractional = 0;
            dispUV_integer += (db_disparity<0?-1:1);
          }
          if(SubPelOption==2) dispUV_fractional *= 2;
        }
#endif // (VSP_OPTIMIZATION)

        /* --------- warping, start --------------*/
        warping_valid_flag_Y = warping_valid_flag_UV = 0;
        {
          // Luma
          x2Luma = xLuma - dispY_integer;
          x2Luma_s = x2Luma + 1;

          if(x2Luma >= 0)
          {
            warping_valid_flag_Y = 1;
            if(dispY_fractional)
            {
              if(dispY_fractional>0)
              {
                INTERPOLATE_RIGHT_SIDE3(xLuma,   WidthLuma_minus1, 0)
                half_pel[0] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)         , 0, 255);
                half_pel[2] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt)  , 0, 255);
                INTERPOLATE_RIGHT_SIDE3(xLuma+1, WidthLuma_minus1, 1)
                half_pel[1] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)         , 0, 255);
                half_pel[3] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt)  , 0, 255);
              }
              else
              {
                INTERPOLATE_LEFT_SIDE3(xLuma,   WidthLuma_minus1, 0)
                half_pel[0] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)         , 0, 255);
                half_pel[2] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt)  , 0, 255);
                INTERPOLATE_LEFT_SIDE3(xLuma+1, WidthLuma_minus1, 1)
                half_pel[1] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)         , 0, 255);
                half_pel[3] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt)  , 0, 255);
              }
              if(dispY_fractional==1)
              {
                *(syn_Y       +x2Luma  ) = ((*(ref_Y       +x_near[0])+half_pel[0]+1)>>1);
                *(syn_Y       +x2Luma_s) = ((*(ref_Y       +x_near[1])+half_pel[1]+1)>>1);
                *(syn_Y_Rownxt+x2Luma  ) = ((*(ref_Y_Rownxt+x_near[0])+half_pel[2]+1)>>1);
                *(syn_Y_Rownxt+x2Luma_s) = ((*(ref_Y_Rownxt+x_near[1])+half_pel[3]+1)>>1);
              }
              else if(dispY_fractional==2)
              {
                *(syn_Y       +x2Luma  ) = half_pel[0];
                *(syn_Y       +x2Luma_s) = half_pel[1];
                *(syn_Y_Rownxt+x2Luma  ) = half_pel[2];
                *(syn_Y_Rownxt+x2Luma_s) = half_pel[3];
              }
              else
              {
                *(syn_Y       +x2Luma  ) = ((*(ref_Y       +x_far[0])+half_pel[0]+1)>>1);
                *(syn_Y       +x2Luma_s) = ((*(ref_Y       +x_far[1])+half_pel[1]+1)>>1);
                *(syn_Y_Rownxt+x2Luma  ) = ((*(ref_Y_Rownxt+x_far[0])+half_pel[2]+1)>>1);
                *(syn_Y_Rownxt+x2Luma_s) = ((*(ref_Y_Rownxt+x_far[1])+half_pel[3]+1)>>1);
              }
            }
            else
            {
              *(syn_Y       +x2Luma  )=*(ref_Y       +xLuma);
              *(syn_Y       +x2Luma_s)=*(ref_Y       +xLuma+1);
              *(syn_Y_Rownxt+x2Luma  )=*(ref_Y_Rownxt+xLuma);
              *(syn_Y_Rownxt+x2Luma_s)=*(ref_Y_Rownxt+xLuma+1);
            }
          }
          else if(x2Luma_s >0)
          {
            warping_valid_flag_Y = 1;
            x2Luma = x2Luma_s;
            if(dispY_fractional)
            {
              if(dispY_fractional>0)
                INTERPOLATE_RIGHT_SIDE3(xLuma+1, WidthLuma_minus1, 0)
              else
                INTERPOLATE_LEFT_SIDE3(xLuma+1, WidthLuma_minus1, 0)
              half_pel[0] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y)         , 0, 255);
              half_pel[2] = (imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt)  , 0, 255);
              if(dispY_fractional==1)
              {
                *(syn_Y       +x2Luma_s) = ((*(ref_Y       +x_near[0])+half_pel[0]+1)>>1);
                *(syn_Y_Rownxt+x2Luma_s) = ((*(ref_Y_Rownxt+x_near[0])+half_pel[2]+1)>>1);
              }
              else if(dispY_fractional==2)
              {
                *(syn_Y       +x2Luma_s) = half_pel[0];
                *(syn_Y_Rownxt+x2Luma_s) = half_pel[2];
              }
              else
              {
                *(syn_Y       +x2Luma_s) = ((*(ref_Y       +x_far[0])+half_pel[0]+1)>>1);
                *(syn_Y_Rownxt+x2Luma_s) = ((*(ref_Y_Rownxt+x_far[0])+half_pel[2]+1)>>1);
              }
            }
            else
            {
              *(syn_Y       +x2Luma_s)=*(ref_Y       +xLuma+1);
              *(syn_Y_Rownxt+x2Luma_s)=*(ref_Y_Rownxt+xLuma+1);
            }
          }

          // Chroma
          x2Chroma = x2Chroma_s =  x - dispUV_integer;
          if(x2Chroma >= 0)
          {
            warping_valid_flag_UV = 1;
            if(dispUV_fractional)
            {
              if(dispUV_fractional>0)
                INTERPOLATE_RIGHT_SIDE3(x, WidthUV_minus1, 0)
              else
                INTERPOLATE_LEFT_SIDE3(x, WidthUV_minus1, 0)
#if !(NOKIA_CHROMA_BILINEAR)
              half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_U),0,255);
              half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_V),0,255);
#endif
              if(dispUV_fractional==1)
              {
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+x2Chroma) = BILINEAR1(ref_U, x_near[0], x_far[0]);
                *(syn_V+x2Chroma) = BILINEAR1(ref_V, x_near[0], x_far[0]);
#else
                *(syn_U+x2Chroma) = ((*(ref_U+x_near[0])+half_pel[1]+1)>>1);
                *(syn_V+x2Chroma) = ((*(ref_V+x_near[0])+half_pel[2]+1)>>1);
#endif
              }
              else if(dispUV_fractional==2)
              {
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+x2Chroma) = BILINEAR2(ref_U, x_near[0], x_far[0]);
                *(syn_V+x2Chroma) = BILINEAR2(ref_V, x_near[0], x_far[0]);
#else
                *(syn_U+x2Chroma) = half_pel[1];
                *(syn_V+x2Chroma) = half_pel[2];
#endif
              }
              else
              {
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+x2Chroma) = BILINEAR3(ref_U, x_near[0], x_far[0]);
                *(syn_V+x2Chroma) = BILINEAR3(ref_V, x_near[0], x_far[0]);
#else
                *(syn_U+x2Chroma) = ((*(ref_U+x_far[0])+half_pel[1]+1)>>1);
                *(syn_V+x2Chroma) = ((*(ref_V+x_far[0])+half_pel[2]+1)>>1);
#endif
              }
            }
            else
            {
              *(syn_U+x2Chroma)=*(ref_U+x);
              *(syn_V+x2Chroma)=*(ref_V+x);
            }
            *(dmap+x2Chroma) = *(ref_d+x);
          }
        }
        /* --------- warping, end --------------*/


        /* --------- filling the holes between two valid pixel projections, start --------------*/
        if(warping_valid_flag_Y)
        {
          if(xLastLuma < x2Luma)
          {
            for(fill=xLastLuma+1; fill<x2Luma; fill++)
            {
              *(syn_Y        + fill) = *(syn_Y        + x2Luma);
              *(syn_Y_Rownxt + fill) = *(syn_Y_Rownxt + x2Luma);
            }
          }
          xLastLuma = x2Luma_s;
        }
        if(warping_valid_flag_UV)
        {
          if(xLastChroma < x2Chroma)
          {
            for(fill=xLastChroma+1; fill<x2Chroma; fill++)
            {
              *(syn_U+fill) = *(syn_U+x2Chroma);
              *(syn_V+fill) = *(syn_V+x2Chroma);
              *(dmap +fill) = *(dmap +x2Chroma);
            }
          }
          xLastChroma = x2Chroma_s;
        }
        /* --------- filling the holes between two valid pixel projections, end --------------*/

      }//end x loop

      if(xLastLuma != Width)
      {
        for(fill=xLastLuma+1; fill<Width; fill++)
        {
          *(syn_Y        + fill) = *(syn_Y        + xLastLuma);
          *(syn_Y_Rownxt + fill) = *(syn_Y_Rownxt + xLastLuma);
        }
      }
      if(xLastChroma!=WidthUV)
      {
        for(fill=xLastChroma+1; fill<WidthUV; fill++)
        {
          *(syn_U + fill) = *(syn_U + xLastChroma);
          *(syn_V + fill) = *(syn_V + xLastChroma);
          *(dmap  + fill) = *(dmap  + xLastChroma);
        }
      }
    }//end y loop
  }

  if(SynDirection==2)
  {
    for(y = 0; y < Height/2; ++y)
    {
      imgpel* syn_hlf_Y=SynY[HLFLVIEW][2*y];
      imgpel* syn_hlf_Y_Rownxt=SynY[HLFLVIEW][2*y+1];

      imgpel* syn_hlf_U=SynU[HLFLVIEW][y];
      imgpel* syn_hlf_V=SynV[HLFLVIEW][y];
      imgpel* syn_hlf_D=Dmap[HLFLVIEW][y];

      imgpel* syn_merge_Y=SynY[ViewId][2*y];
      imgpel* syn_merge_Y_Rownxt=SynY[ViewId][2*y+1];

      imgpel* syn_merge_U=SynU[ViewId][y];
      imgpel* syn_merge_V=SynV[ViewId][y];
      imgpel* syn_merge_D=Dmap[ViewId][y];

      memcpy(syn_hlf_Y,syn_merge_Y,WidthUV*2*sizeof(imgpel));
      memcpy(syn_hlf_Y_Rownxt,syn_merge_Y_Rownxt,WidthUV*2*sizeof(imgpel));

      memcpy(syn_hlf_U,syn_merge_U,WidthUV*sizeof(imgpel));
      memcpy(syn_hlf_V,syn_merge_V,WidthUV*sizeof(imgpel));
      memcpy(syn_hlf_D,syn_merge_D,WidthUV*sizeof(imgpel));
    }
  }
}

#else // NTT_VSP_HARMONIZATION_M25195
void CViewInterpolation1D::ViewGen_1LWH_splatting420(int ViewId)
{

  int i;
#if NTT_SUBPEL_VSP_M24796
  int width_warped = Width>>1; //width of the warped image (in the target view), related to UpsampleRefs and SubPelOption
#else
  int width_warped = Width2 * SubPelOption/2; //width of the warped image (in the target view), related to UpsampleRefs and SubPelOption
  int Width2_minus1 = Width2/2 - 1;           //width of the original image (in the original view) - 1
#endif
  int width_warped_minus1 = width_warped-1;
  int width_warped_half   = width_warped>>1;//half width of the warped image
  int x2_s, x2_floor, x2_ceil;
  int x_flip = 0;
  int warping_valid_flag = 0;
  byte** splatting_frame = NULL;
  byte*  splatting_line  = NULL;
#if NTT_SUBPEL_VSP_M24796
  int WidthUV = Width>>1;
  int WidthUV_minus1 = WidthUV-1;
  int WidthLuma_minus1 = Width-1;
  int x2Luma_s, x2Luma_floor, x2Luma_ceil;
  int mem_id = SynDirection==2 ? ViewId : FINLVIEW;
#endif


#if VSP_OPTIMIZATION
  if(SynDirection==2)
  {
    for (i = 0; i < 3; i++)
    {
#if NTT_SUBPEL_VSP_M24796
      memset(SynY[i][0], 0,         Width*Height*sizeof(imgpel));
      memset(SynU[i][0], 0,         Width*Height/4*sizeof(imgpel));
      memset(SynV[i][0], 0,         Width*Height/4*sizeof(imgpel));
      memset(Dmap[i][0], 0,         Width*Height/4*sizeof(byte));
#else
      memset(SynY[i][0], 0,         Width2*Height*SubPelOption*sizeof(imgpel));
      memset(SynU[i][0], 0,         Width2*Height/4*SubPelOption*sizeof(imgpel));
      memset(SynV[i][0], 0,         Width2*Height/4*SubPelOption*sizeof(imgpel));
      memset(Dmap[i][0], 0,         Width2*Height/4*SubPelOption*sizeof(byte));
#endif
    }
  }
#if !NTT_SUBPEL_VSP_M24796
  else if(SynDirection==1)
  {
    memset(SynY[1][0], 0,          Width2*Height*SubPelOption*sizeof(imgpel));
    memset(SynU[1][0], 0,          Width2*Height/4*SubPelOption*sizeof(imgpel));
    memset(SynV[1][0], 0,          Width2*Height/4*SubPelOption*sizeof(imgpel));
    memset(Dmap[1][0], 0,          Width2*Height/4*SubPelOption*sizeof(byte));
  }
  else if(SynDirection==0)
  {
    memset(SynY[0][0], 0,         Width2*Height*SubPelOption*sizeof(imgpel));
    memset(SynU[0][0], 0,         Width2*Height/4*SubPelOption*sizeof(imgpel));
    memset(SynV[0][0], 0,         Width2*Height/4*SubPelOption*sizeof(imgpel));
    memset(Dmap[0][0], 0,         Width2*Height/4*SubPelOption*sizeof(byte));
  }
#endif // !NTT_SUBPEL_VSP_M24796
#endif

  /*------------- Original Boundary-aware processing --------------*/
  //find out the max and min z value in current frame
  if(SynDirection!=1)
    FindDepthMaxMin420(RefDepth[LEFTVIEW], LEFTVIEW); //left view must have a similar depth structure as that of the right view
  else
    FindDepthMaxMin420(RefDepth[RGHTVIEW], RGHTVIEW);
  {
    double es = 0.15; //EdgeStrength
    int diff_z= Maxz-Minz;
#if NTT_SUBPEL_VSP_M24796
    int th=(int)(es*diff_z+1); //depth edge threshold = 15% * z_value_range / UpsampleRefs
#else
    int th=(int)(es*diff_z /UpsampleRefs+1); //depth edge threshold = 15% * z_value_range / UpsampleRefs
#endif

    if(SynDirection==1)
    {
      //!<synthesize from right
#if NTT_SUBPEL_VSP_M24796
      memset(BoundaryMask[1][0],NONE,Width*Height/4*sizeof(byte));
      DetectBoundary(BoundaryMask[1],  RefDepth[1], Width/2, Height/2, th);
#else
      memset(BoundaryMask[1][0],NONE,Width2*Height/4*sizeof(byte));
      DetectBoundary(BoundaryMask[1],  RefDepth[1], Width2/2, Height/2, th);
#endif
      splatting_frame = BoundaryMask[1];
    }
    else if(SynDirection==0)
    {
      //!<synthesize from left
#if NTT_SUBPEL_VSP_M24796
      memset(BoundaryMask[0][0],NONE,Width*Height/4*sizeof(byte));
      DetectBoundary(BoundaryMask[0],  RefDepth[0], Width/2, Height/2, th);
#else
      memset(BoundaryMask[0][0],NONE,Width2*Height/4*sizeof(byte));
      DetectBoundary(BoundaryMask[0],  RefDepth[0], Width2/2, Height/2, th);
#endif
      splatting_frame = BoundaryMask[0];
    }
  }
  //-----------end of determination of pixels using splatting

  if(ViewId == LEFTVIEW)
    WarpToRight = 1;
  else
    WarpToRight = 0;
  //start to warp each pixel
  if( WarpToRight == 0) // right view-- from right 2 left
  {
    int x, y, xLast, idx2 = 0, x2, idx2Luma;
    imgpel* ref_Y=NULL;
    imgpel* ref_Y_Rownxt=NULL;

    imgpel* ref_U=NULL;
    imgpel* ref_V=NULL;
    imgpel* ref_d=NULL;
    imgpel* dmap=NULL;
    
    imgpel* syn_Y=NULL;
    imgpel* syn_Y_Rownxt=NULL;

    imgpel* syn_U=NULL;
    imgpel* syn_V=NULL;
    imgpel* pDepth = NULL; //rev
#if NTT_SUBPEL_VSP_M24796
    int  xLuma, x2Luma, idx2LumaN;
    int  disp_integer;
    int  disp_fractional;
    int  dispUV_integer;
    int  dispUV_fractional;
    int  p0, p1, p2, p3, p4, p5;
    int  x_far, x_near;
    imgpel half_pel[4];
#else
    double  disp;
#endif // NTT_SUBPEL_VSP_M24796
    imgpel  crData;

    for (y = 0; y < Height/2; ++y)
    {
      ref_Y=RefView[ViewId][0][2*y];
      ref_Y_Rownxt=RefView[ViewId][0][2*y+1];

      ref_U=RefView[ViewId][1][y];
      ref_V=RefView[ViewId][2][y];
      ref_d=RefDepth[ViewId][y];

#if NTT_SUBPEL_VSP_M24796
      syn_Y=SynY[mem_id][2*y];
      syn_Y_Rownxt=SynY[mem_id][2*y+1];
      syn_U=SynU[mem_id][y];
      syn_V=SynV[mem_id][y];
      dmap=Dmap[mem_id][y];
      memset(syn_Y, 0, Width*sizeof(imgpel));
      memset(syn_Y_Rownxt, 0, Width*sizeof(imgpel));
      memset(syn_U, 0, WidthUV*sizeof(imgpel));
      memset(syn_V, 0, WidthUV*sizeof(imgpel));
#else
      syn_Y=SynY[ViewId][2*y];
      syn_Y_Rownxt=SynY[ViewId][2*y+1];
      syn_U=SynU[ViewId][y];
      syn_V=SynV[ViewId][y];
      dmap=Dmap[ViewId][y];
#endif // NTT_SUBPEL_VSP_M24796

      pDepth = dmap;  //rev
      memset(pDepth, 0, width_warped*sizeof(imgpel)); //maybe set to "-1", as pixels may have 0 depth value; could be a bug
      xLast = 0;
      splatting_line = splatting_frame[y];

#if NTT_SUBPEL_VSP_M24796
      for (x = 0; x < WidthUV;  ++x)
#else
      for (x = 0; x < Width2/2;  ++x)
#endif
      {
        //get disparity
        crData = *(ref_d+x);  //depth of current pixel being warped

#if NTT_SUBPEL_VSP_M24796
#if VSP_OPTIMIZATION
        xLuma = (x<<1) + DisparityLookupTable[ViewId][crData].referenceY[0];
        disp_integer      = DisparityLookupTable[ViewId][crData].targetUV*2;
        disp_fractional   = DisparityLookupTable[ViewId][crData].referenceY[1];
        dispUV_integer    = DisparityLookupTable[ViewId][crData].targetUV;
        dispUV_fractional = DisparityLookupTable[ViewId][crData].referenceUV;
#else
        {
#if POZNAN_NONLINEAR_DEPTH
          int in = nonlinear_depth_lut_backward[crData];
#else
          int in = crData;
#endif
          double z, db_disparity, db_disparity_subpel, db_disparity_uv_subpel;

          z=1.0/( (in/255.0) * (1/Znear[ViewId] - 1/Zfar[ViewId]) + (1/Zfar[ViewId]) );
          db_disparity        = (FocalLength*LTranslation[ViewId]/z)-duPrincipal[ViewId];
          db_disparity_subpel = db_disparity*SubPelOption;
          db_disparity_uv_subpel = db_disparity_subpel/2;
          dispUV_integer      = (int)(db_disparity/2);
          dispUV_fractional   = (int)(fabs(db_disparity_uv_subpel)+0.5)-abs(dispUV_integer)*SubPelOption;
          if(dispUV_fractional==SubPelOption)
          {
            dispUV_fractional = 0;
            dispUV_integer += (db_disparity<0?-1:1);
          }
          if(SubPelOption==2) dispUV_fractional *= 2;
          disp_integer = dispUV_integer*2;
          xLuma = (x<<1) + ((int)db_disparity) - disp_integer;
          disp_fractional = (int)(fabs(db_disparity_subpel)+0.5)-abs(((int)db_disparity))*SubPelOption;
          if(disp_fractional==SubPelOption)
          {
            disp_fractional = 0;
            xLuma += (db_disparity<0?-1:1);
          }
          if(SubPelOption==2) disp_fractional *= 2;
        }
#endif
#else // NTT_SUBPEL_VSP_M24796
#if VSP_OPTIMIZATION
        if(UpsampleRefs==2)
          disp=DisparityLookupTable[ViewId][crData].db_disparity_mul2;
        else if(UpsampleRefs==4)
          disp=DisparityLookupTable[ViewId][crData].db_disparity_mul4;
        else
          disp=DisparityLookupTable[ViewId][crData].db_disparity;
        disp=(disp>=0) ?((disp+1)/2) :((disp-1)/2);
#endif
#endif // NTT_SUBPEL_VSP_M24796

        /* --------- splatting/warping, start --------------*/
        warping_valid_flag = 0;
        if(splatting_line[x] == NONE)
        {
#if NTT_SUBPEL_VSP_M24796
          x2      = x2_s      = x     - dispUV_integer;
          x2Luma  = x2Luma_s  = (x<<1)- disp_integer;
#else
          x2 = (int)((x - disp) * SubPelOption + 0.5);
          x2_s = x2;
#endif
          if(x2 < width_warped) //since x2 >=0 always valid in this warping direction
          {
            if(pDepth[x2] <= crData) //to handle the cases: depth value = 0;
            {
              idx2 = x2;
#if NTT_SUBPEL_VSP_M24796
              idx2Luma  = x2Luma;
              idx2LumaN = idx2Luma<WidthLuma_minus1 ? (idx2Luma+1) : WidthLuma_minus1;
              if(disp_fractional)
              {
                if(disp_fractional<0)
                  INTERPOLATE_RIGHT_SIDE(xLuma, WidthLuma_minus1)
                else
                  INTERPOLATE_LEFT_SIDE(xLuma, WidthLuma_minus1)
                half_pel[0]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y),0,255);
                half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y+1),0,255);
                half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt),0,255);
                half_pel[3]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt+1),0,255);
                if(disp_fractional==1)
                {
                  *(syn_Y       +idx2Luma ) = ((*(ref_Y       +x_near  )+half_pel[0]+1)>>1);
                  *(syn_Y       +idx2LumaN) = ((*(ref_Y       +x_near+1)+half_pel[1]+1)>>1);
                  *(syn_Y_Rownxt+idx2Luma ) = ((*(ref_Y_Rownxt+x_near  )+half_pel[2]+1)>>1);
                  *(syn_Y_Rownxt+idx2LumaN) = ((*(ref_Y_Rownxt+x_near+1)+half_pel[3]+1)>>1);
                }
                else if(disp_fractional==2)
                {
                  *(syn_Y       +idx2Luma ) = half_pel[0];
                  *(syn_Y       +idx2LumaN) = half_pel[1];
                  *(syn_Y_Rownxt+idx2Luma ) = half_pel[2];
                  *(syn_Y_Rownxt+idx2LumaN) = half_pel[3];
                }
                else
                {
                  *(syn_Y       +idx2Luma ) = ((*(ref_Y       +x_far  )+half_pel[0]+1)>>1);
                  *(syn_Y       +idx2LumaN) = ((*(ref_Y       +x_far+1)+half_pel[1]+1)>>1);
                  *(syn_Y_Rownxt+idx2Luma ) = ((*(ref_Y_Rownxt+x_far  )+half_pel[2]+1)>>1);
                  *(syn_Y_Rownxt+idx2LumaN) = ((*(ref_Y_Rownxt+x_far+1)+half_pel[3]+1)>>1);
                }
              }
              else
              {
                *(syn_Y       +idx2Luma )=*(ref_Y       +xLuma);
                *(syn_Y       +idx2LumaN)=*(ref_Y       +xLuma+1);
                *(syn_Y_Rownxt+idx2Luma )=*(ref_Y_Rownxt+xLuma);
                *(syn_Y_Rownxt+idx2LumaN)=*(ref_Y_Rownxt+xLuma+1);
              }

              if(dispUV_fractional)
              {
                if(dispUV_fractional<0)
                  INTERPOLATE_RIGHT_SIDE(x, WidthUV_minus1)
                else
                  INTERPOLATE_LEFT_SIDE(x, WidthUV_minus1)
#if !(NOKIA_CHROMA_BILINEAR)
                half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_U),0,255);
                half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_V),0,255);
#endif
                if(dispUV_fractional==1)
                {
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR1(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR1(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = ((*(ref_U+x_near)+half_pel[1]+1)>>1);
                  *(syn_V+idx2) = ((*(ref_V+x_near)+half_pel[2]+1)>>1);
#endif
                }
                else if(dispUV_fractional==2)
                {
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR2(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR2(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = half_pel[1];
                  *(syn_V+idx2) = half_pel[2];
#endif
                }
                else
                {
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR3(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR3(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = ((*(ref_U+x_far)+half_pel[1]+1)>>1);
                  *(syn_V+idx2) = ((*(ref_V+x_far)+half_pel[2]+1)>>1);
#endif
                }
              }
              else
              {
                *(syn_U+idx2)=*(ref_U+x);
                *(syn_V+idx2)=*(ref_V+x);
              }
#else
              idx2Luma = (idx2<<1); //

              *(syn_Y+idx2Luma)=*(ref_Y+(2*x));
              *(syn_Y+idx2Luma+1)=*(ref_Y+(2*x)+1);
              *(syn_Y_Rownxt+idx2Luma)=*(ref_Y_Rownxt+(2*x));
              *(syn_Y_Rownxt+idx2Luma+1)=*(ref_Y_Rownxt+(2*x)+1);
              
              *(syn_U+idx2)=*(ref_U+x);
              *(syn_V+idx2)=*(ref_V+x);
#endif
              *(dmap+idx2) =*(ref_d+x);
              warping_valid_flag = 1;
            }
          }

        }
        else
        {
#if NTT_SUBPEL_VSP_M24796
          x2      = x2_floor  = x - dispUV_integer;
          x2_s    = x2_ceil   = x2_floor + 1;
          x2Luma  = x2Luma_floor = (x<<1) - disp_integer;
          x2Luma_s= x2Luma_ceil  = x2Luma_floor + 2;
#else
          x2_floor = (int)((x - disp) * SubPelOption);
          x2_ceil  = x2_floor + 1;

          x2 = x2_floor;
          x2_s = x2_ceil;
#endif

          if(x2_ceil < width_warped) //since x2 >=0 always valid in this warping direction
          {
            if(pDepth[x2_ceil] < crData) //note: adding "=" in splatting may cause slight difference
            {
              idx2 = x2_ceil;
#if NTT_SUBPEL_VSP_M24796
              idx2Luma  = x2Luma_ceil;
              idx2LumaN = idx2Luma<WidthLuma_minus1 ? (idx2Luma+1) : WidthLuma_minus1;
              if(disp_fractional)
              {
                if(disp_fractional>0)
                  INTERPOLATE_RIGHT_SIDE(xLuma, WidthLuma_minus1)
                else
                  INTERPOLATE_LEFT_SIDE(xLuma, WidthLuma_minus1)
                half_pel[0]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y),0,255);
                half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y+1),0,255);
                half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt),0,255);
                half_pel[3]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt+1),0,255);
                if(disp_fractional==1)
                {
                  *(syn_Y       +idx2Luma ) = ((*(ref_Y       +x_far  )+half_pel[0]+1)>>1);
                  *(syn_Y       +idx2LumaN) = ((*(ref_Y       +x_far+1)+half_pel[1]+1)>>1);
                  *(syn_Y_Rownxt+idx2Luma ) = ((*(ref_Y_Rownxt+x_far  )+half_pel[2]+1)>>1);
                  *(syn_Y_Rownxt+idx2LumaN) = ((*(ref_Y_Rownxt+x_far+1)+half_pel[3]+1)>>1);
                }
                else if(disp_fractional==2)
                {
                  *(syn_Y       +idx2Luma ) = half_pel[0];
                  *(syn_Y       +idx2LumaN) = half_pel[1];
                  *(syn_Y_Rownxt+idx2Luma ) = half_pel[2];
                  *(syn_Y_Rownxt+idx2LumaN) = half_pel[3];
                }
                else
                {
                  *(syn_Y       +idx2Luma ) = ((*(ref_Y       +x_near  )+half_pel[0]+1)>>1);
                  *(syn_Y       +idx2LumaN) = ((*(ref_Y       +x_near+1)+half_pel[1]+1)>>1);
                  *(syn_Y_Rownxt+idx2Luma ) = ((*(ref_Y_Rownxt+x_near  )+half_pel[2]+1)>>1);
                  *(syn_Y_Rownxt+idx2LumaN) = ((*(ref_Y_Rownxt+x_near+1)+half_pel[3]+1)>>1);
                }
              }
              else
              {
                int x_neighbor = CLIP(xLuma+2, 0, WidthLuma_minus1);
                *(syn_Y       +idx2Luma )=*(ref_Y       +xLuma+1);
                *(syn_Y       +idx2LumaN)=*(ref_Y       +x_neighbor);
                *(syn_Y_Rownxt+idx2Luma )=*(ref_Y_Rownxt+xLuma+1);
                *(syn_Y_Rownxt+idx2LumaN)=*(ref_Y_Rownxt+x_neighbor);
              }

              if(dispUV_fractional)
              {
                if(dispUV_fractional>0)
                  INTERPOLATE_RIGHT_SIDE(x, WidthUV_minus1)
                else
                  INTERPOLATE_LEFT_SIDE(x, WidthUV_minus1)
#if !(NOKIA_CHROMA_BILINEAR)
                half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_U),0,255);
                half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_V),0,255);
#endif
                if(dispUV_fractional==1)
                {
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR3(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR3(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = ((*(ref_U+x_far)+half_pel[1]+1)>>1);
                  *(syn_V+idx2) = ((*(ref_V+x_far)+half_pel[2]+1)>>1);
#endif
                }
                else if(dispUV_fractional==2)
                {
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR2(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR2(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = half_pel[1];
                  *(syn_V+idx2) = half_pel[2];
#endif
                }
                else
                {
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR1(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR1(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = ((*(ref_U+x_near)+half_pel[1]+1)>>1);
                  *(syn_V+idx2) = ((*(ref_V+x_near)+half_pel[2]+1)>>1);
#endif
                }
              }
              else
              {
                int x_neighbor = CLIP(x+1, 0, WidthUV_minus1);
                *(syn_U+idx2)=*(ref_U+x_neighbor);
                *(syn_V+idx2)=*(ref_V+x_neighbor);
              }
#else
              idx2Luma = (idx2<<1); //

              *(syn_Y+idx2Luma)=*(ref_Y+(2*x));
              *(syn_Y+idx2Luma+1)=*(ref_Y+(2*x)+1);
              *(syn_Y_Rownxt+idx2Luma)=*(ref_Y_Rownxt+(2*x));
              *(syn_Y_Rownxt+idx2Luma+1)=*(ref_Y_Rownxt+(2*x)+1);

              *(syn_U+idx2)=*(ref_U+x);
              *(syn_V+idx2)=*(ref_V+x);
#endif
              *(dmap+idx2) =*(ref_d+x);
              warping_valid_flag = 1;
            }
          }

          if(x2_floor < width_warped) //since x2 >=0 always valid in this warping direction
          {
            if(pDepth[x2_floor] < crData)
            {
              idx2 = x2_floor;
#if NTT_SUBPEL_VSP_M24796
              idx2Luma  = x2Luma_floor;
              idx2LumaN = idx2Luma<WidthLuma_minus1 ? (idx2Luma+1) : WidthLuma_minus1;
              if(disp_fractional)
              {
                if(disp_fractional<0)
                  INTERPOLATE_RIGHT_SIDE(xLuma, WidthLuma_minus1)
                else
                  INTERPOLATE_LEFT_SIDE(xLuma, WidthLuma_minus1)
                half_pel[0]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y),0,255);
                half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y+1),0,255);
                half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt),0,255);
                half_pel[3]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt+1),0,255);
                if(disp_fractional==1)
                {
                  *(syn_Y       +idx2Luma ) = ((*(ref_Y       +x_near  )+half_pel[0]+1)>>1);
                  *(syn_Y       +idx2LumaN) = ((*(ref_Y       +x_near+1)+half_pel[1]+1)>>1);
                  *(syn_Y_Rownxt+idx2Luma ) = ((*(ref_Y_Rownxt+x_near  )+half_pel[2]+1)>>1);
                  *(syn_Y_Rownxt+idx2LumaN) = ((*(ref_Y_Rownxt+x_near+1)+half_pel[3]+1)>>1);
                }
                else if(disp_fractional==2)
                {
                  *(syn_Y       +idx2Luma ) = half_pel[0];
                  *(syn_Y       +idx2LumaN) = half_pel[1];
                  *(syn_Y_Rownxt+idx2Luma ) = half_pel[2];
                  *(syn_Y_Rownxt+idx2LumaN) = half_pel[3];
                }
                else
                {
                  *(syn_Y       +idx2Luma ) = ((*(ref_Y       +x_far  )+half_pel[0]+1)>>1);
                  *(syn_Y       +idx2LumaN) = ((*(ref_Y       +x_far+1)+half_pel[1]+1)>>1);
                  *(syn_Y_Rownxt+idx2Luma ) = ((*(ref_Y_Rownxt+x_far  )+half_pel[2]+1)>>1);
                  *(syn_Y_Rownxt+idx2LumaN) = ((*(ref_Y_Rownxt+x_far+1)+half_pel[3]+1)>>1);
                }
              }
              else
              {
                *(syn_Y       +idx2Luma )=*(ref_Y       +xLuma);
                *(syn_Y       +idx2LumaN)=*(ref_Y       +xLuma+1);
                *(syn_Y_Rownxt+idx2Luma )=*(ref_Y_Rownxt+xLuma);
                *(syn_Y_Rownxt+idx2LumaN)=*(ref_Y_Rownxt+xLuma+1);
              }

              if(dispUV_fractional)
              {
                if(dispUV_fractional<0)
                  INTERPOLATE_RIGHT_SIDE(x, WidthUV_minus1)
                else
                  INTERPOLATE_LEFT_SIDE(x, WidthUV_minus1)
#if !(NOKIA_CHROMA_BILINEAR)
                half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_U),0,255);
                half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_V),0,255);
#endif
                if(dispUV_fractional==1)
                {
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR1(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR1(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = ((*(ref_U+x_near)+half_pel[1]+1)>>1);
                  *(syn_V+idx2) = ((*(ref_V+x_near)+half_pel[2]+1)>>1);
#endif
                }
                else if(dispUV_fractional==2)
                {
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR2(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR2(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = half_pel[1];
                  *(syn_V+idx2) = half_pel[2];
#endif
                }
                else
                {
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR3(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR3(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = ((*(ref_U+x_far)+half_pel[1]+1)>>1);
                  *(syn_V+idx2) = ((*(ref_V+x_far)+half_pel[2]+1)>>1);
#endif
                }
              }
              else
              {
                *(syn_U+idx2)=*(ref_U+x);
                *(syn_V+idx2)=*(ref_V+x);
              }
#else
              idx2Luma = (idx2<<1); //

              *(syn_Y+idx2Luma)=*(ref_Y+(2*x));
              *(syn_Y+idx2Luma+1)=*(ref_Y+(2*x)+1);
              *(syn_Y_Rownxt+idx2Luma)=*(ref_Y_Rownxt+(2*x));
              *(syn_Y_Rownxt+idx2Luma+1)=*(ref_Y_Rownxt+(2*x)+1);

              *(syn_U+idx2)=*(ref_U+x);
              *(syn_V+idx2)=*(ref_V+x);
#endif
              *(dmap+idx2) =*(ref_d+x);
              warping_valid_flag = 1;
            }
          }
        }
        /* --------- splatting/warping, end --------------*/
        if(warping_valid_flag) //filling the holes between two valid pixel projections
        {
          if(xLast >= x2)
          {
            int cnt, use, fill, luma_use;
            if(xLast > x2) 
              memset((void*)&pDepth[idx2+1], 0, (xLast-x2));
            if(x2!=0 && pDepth[x2-1] == 0)
            {
              for(cnt = 0; cnt < x2; cnt++)
              {
                if(pDepth[x2-1-cnt]!= 0 ) 
                  break;
              }
              if(cnt == x2)
              {
                use = x2;
                luma_use = use<<1;
              }
              else if( pDepth[x2-cnt-1]>pDepth[x2])
              {
                use = x2;
                luma_use = use<<1;
              }
              else
              {
                use = x2-1-cnt;
                luma_use = (use<<1)+1;
              }
              for(fill = (x2-cnt); fill < x2; fill++)
              {
                *(syn_Y+(fill*2)) =*(syn_Y+luma_use);
                *(syn_Y+(fill*2)+1) =*(syn_Y+luma_use);
                *(syn_Y_Rownxt+(fill*2)) =*(syn_Y_Rownxt+luma_use);
                *(syn_Y_Rownxt+(fill*2)+1) =*(syn_Y_Rownxt+luma_use);

                *(syn_U+fill) =*(syn_U+use);
                *(syn_V+fill) =*(syn_V+use);
                *(dmap +fill) =*(dmap +use);
              }
            }
          }
          else
          { //xLast < x2
            int fill;
            if( !(x2!=0 && xLast== (x2-1)) )
            {
              if(xLast == 0)
              {
                for(fill = xLast; fill < x2; fill++)
                {
                  *(syn_Y+(fill*2))=*(syn_Y+(x2*2));
                  *(syn_Y+(fill*2)+1)=*(syn_Y+(x2*2));
                  *(syn_Y_Rownxt+(fill*2))=*(syn_Y_Rownxt+(x2*2));
                  *(syn_Y_Rownxt+(fill*2)+1)=*(syn_Y_Rownxt+(x2*2));

                  *(syn_U+fill)=*(syn_U+x2);
                  *(syn_V+fill)=*(syn_V+x2);
                  *(dmap+fill) =*(dmap +x2);
                }
              }
              else
              {
                if(pDepth[xLast] > pDepth[x2])
                {
                  for(fill = (xLast+1); fill < x2; fill++)
                  {
                    *(syn_Y+(fill*2))=*(syn_Y+(x2*2));
                    *(syn_Y+(fill*2)+1)=*(syn_Y+(x2*2));
                    *(syn_Y_Rownxt+(fill*2))=*(syn_Y_Rownxt+(x2*2));
                    *(syn_Y_Rownxt+(fill*2)+1)=*(syn_Y_Rownxt+(x2*2));

                    *(syn_U+fill)=*(syn_U+x2);
                    *(syn_V+fill)=*(syn_V+x2);
                    *(dmap+fill) =*(dmap+x2);
                  }
                }
                else
                {
                  for(fill = (xLast+1); fill < x2; fill++)
                  {
                    *(syn_Y+(fill*2))=*(syn_Y+(xLast*2)+1);
                    *(syn_Y+(fill*2)+1)=*(syn_Y+(xLast*2+1));
                    *(syn_Y_Rownxt+(fill*2))=*(syn_Y_Rownxt+(xLast*2)+1);
                    *(syn_Y_Rownxt+(fill*2)+1)=*(syn_Y_Rownxt+(xLast*2)+1);


                    *(syn_U+fill)=*(syn_U+xLast);
                    *(syn_V+fill)=*(syn_V+xLast);
                    *(dmap+fill) =*(dmap+xLast);
                  }
                }
              }
            }
          }//end x2>=imgWidth
          xLast = x2_s; //rev
        }
      }//end x loop

      if(xLast != width_warped)
      {
        int fill;
        for(fill = xLast+1; fill < width_warped; fill++)
        {
          *(syn_Y+(fill*2))=*(syn_Y+(xLast*2)+1);
          *(syn_Y+(fill*2)+1)=*(syn_Y+(xLast*2)+1);
          *(syn_Y_Rownxt+(fill*2))=*(syn_Y_Rownxt+(xLast*2)+1);
          *(syn_Y_Rownxt+(fill*2)+1)=*(syn_Y_Rownxt+(xLast*2)+1);

          *(syn_U+fill)=*(syn_U+xLast);
          *(syn_V+fill)=*(syn_V+xLast);
          *(dmap+fill) =*(dmap+xLast);
        }
      }
      if(pDepth[0] == 0)
      {
        *(syn_Y+0) =*(syn_Y+1*2);
        *(syn_Y+0+1) =*(syn_Y+1*2);
        *(syn_Y_Rownxt+0) =*(syn_Y_Rownxt+1*2);
        *(syn_Y_Rownxt+0+1) =*(syn_Y_Rownxt+1*2);

        *(syn_U+0) =*(syn_U+1);
        *(syn_V+0) =*(syn_V+1);
        *(dmap +0) =*(dmap +1);
      }
    }//end y loop
  }
  else  //if( WarpToRight == 1) 
  {
    int x, y, xLast, idx2 = 0, x2, idx2Luma;
    imgpel* ref_Y=NULL;
    imgpel* ref_Y_Rownxt=NULL;

    imgpel* ref_U=NULL;
    imgpel* ref_V=NULL;
    imgpel* ref_d=NULL;
    imgpel* dmap=NULL;

    imgpel* syn_Y=NULL;
    imgpel* syn_Y_Rownxt=NULL;

    imgpel* syn_U=NULL;
    imgpel* syn_V=NULL;
    imgpel* pDepth = NULL; //rev
#if NTT_SUBPEL_VSP_M24796
    int  xLuma, x2Luma, idx2LumaN;
    int  disp_integer;
    int  disp_fractional;
    int  dispUV_integer;
    int  dispUV_fractional;
    int  p0, p1, p2, p3, p4, p5;
    int  x_far, x_near;
    imgpel half_pel[4];
#else
    double  disp;
#endif
    imgpel  crData;

    for (y = 0; y < Height/2; ++y)
    {
      ref_Y=RefView[ViewId][0][2*y];
      ref_Y_Rownxt=RefView[ViewId][0][2*y+1];

      ref_U=RefView[ViewId][1][y];
      ref_V=RefView[ViewId][2][y];
      ref_d=RefDepth[ViewId][y];

#if NTT_SUBPEL_VSP_M24796
      syn_Y=SynY[mem_id][2*y];
      syn_Y_Rownxt=SynY[mem_id][2*y+1];
      syn_U=SynU[mem_id][y];
      syn_V=SynV[mem_id][y];
      dmap=Dmap[mem_id][y];
      memset(syn_Y, 0, Width2*sizeof(imgpel));
      memset(syn_Y_Rownxt, 0, Width2*sizeof(imgpel));
      memset(syn_U, 0, WidthUV*sizeof(imgpel));
      memset(syn_V, 0, WidthUV*sizeof(imgpel));
#else
      syn_Y=SynY[ViewId][2*y];
      syn_Y_Rownxt=SynY[ViewId][2*y+1];
      syn_U=SynU[ViewId][y];
      syn_V=SynV[ViewId][y];
      dmap=Dmap[ViewId][y];
#endif

      pDepth = dmap; //rev
      memset(pDepth, 0, width_warped*sizeof(imgpel));
      xLast = 0;
      splatting_line = splatting_frame[y];

#if NTT_SUBPEL_VSP_M24796
      for(x = 0; x < WidthUV;  ++x)
#else
      for(x = 0; x < Width2/2;  ++x)
#endif
      {
        //get disparity
#if NTT_SUBPEL_VSP_M24796
        x_flip = WidthUV_minus1-x; //rev
#else
        x_flip = Width2_minus1-x; //rev
#endif
        crData = *(ref_d+ x_flip);

#if NTT_SUBPEL_VSP_M24796
#if VSP_OPTIMIZATION
        xLuma = (x_flip<<1) + DisparityLookupTable[ViewId][crData].referenceY[0];
        disp_integer      = DisparityLookupTable[ViewId][crData].targetUV*2;
        disp_fractional   = DisparityLookupTable[ViewId][crData].referenceY[1];
        dispUV_integer    = DisparityLookupTable[ViewId][crData].targetUV;
        dispUV_fractional = DisparityLookupTable[ViewId][crData].referenceUV;
#else
        {
#if POZNAN_NONLINEAR_DEPTH
          int in = nonlinear_depth_lut_backward[crData];
#else
          int in = crData;
#endif
          double z, db_disparity, db_disparity_subpel, db_disparity_uv_subpel;

          z=1.0/( (in/255.0) * (1/Znear[ViewId] - 1/Zfar[ViewId]) + (1/Zfar[ViewId]) );
          db_disparity        = (FocalLength*LTranslation[ViewId]/z)-duPrincipal[ViewId];
          db_disparity_subpel = db_disparity*SubPelOption;
          db_disparity_uv_subpel = db_disparity_subpel/2;
          dispUV_integer      = (int)(db_disparity/2);
          dispUV_fractional   = (int)(fabs(db_disparity_uv_subpel)+0.5)-abs(dispUV_integer)*SubPelOption;
          if(dispUV_fractional==SubPelOption)
          {
            dispUV_fractional = 0;
            dispUV_integer += (db_disparity<0?-1:1);
          }
          if(SubPelOption==2) dispUV_fractional *= 2;
          disp_integer = dispUV_integer*2;
          xLuma = (x_flip<<1) + ((int)db_disparity) - disp_integer;
          disp_fractional = (int)(fabs(db_disparity_subpel)+0.5)-abs(((int)db_disparity))*SubPelOption;
          if(disp_fractional==SubPelOption)
          {
            disp_fractional = 0;
            xLuma += (db_disparity<0?-1:1);
          }
          if(SubPelOption==2) disp_fractional *= 2;
        }
#endif
#else // NTT_SUBPEL_VSP_M24796
        if(UpsampleRefs==2)
          disp=DisparityLookupTable[ViewId][crData].db_disparity_mul2;
        else if(UpsampleRefs==4)
          disp=DisparityLookupTable[ViewId][crData].db_disparity_mul4;
        else
          disp=DisparityLookupTable[ViewId][crData].db_disparity;
        
          disp=(disp>=0) ?((disp+1)/2) :((disp-1)/2);
#endif // NTT_SUBPEL_VSP_M24796

        /* --------- splatting/warping, start --------------*/
        warping_valid_flag = 0;
        if(splatting_line[x_flip] == NONE)
        {
#if NTT_SUBPEL_VSP_M24796
          x2      = x2_s      = x     + dispUV_integer;
          x2Luma  = x2Luma_s  = (x<<1)+ disp_integer;
#else
          x2 = (int)((x + disp) * SubPelOption + 0.5);
          x2_s = x2;
#endif
          if(x2 < width_warped) //since x2 >=0 always valid in this warping direction
          {
            if(pDepth[x2] <= crData)
            {
              idx2 = x2;
#if NTT_SUBPEL_VSP_M24796
              idx2Luma  = x2Luma;
              idx2LumaN = idx2Luma<WidthLuma_minus1 ? (idx2Luma+1) : WidthLuma_minus1;;
              if(disp_fractional)
              {
                if(disp_fractional>0)
                  INTERPOLATE_RIGHT_SIDE(xLuma, WidthLuma_minus1)
                else
                  INTERPOLATE_LEFT_SIDE(xLuma, WidthLuma_minus1)
                half_pel[0]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y+1),0,255);
                half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y),0,255);
                half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt+1),0,255);
                half_pel[3]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt),0,255);
                if(disp_fractional==1)
                {
                  *(syn_Y       +idx2Luma ) = ((*(ref_Y       +x_near+1  )+half_pel[0]+1)>>1);
                  *(syn_Y       +idx2LumaN) = ((*(ref_Y       +x_near)+half_pel[1]+1)>>1);
                  *(syn_Y_Rownxt+idx2Luma ) = ((*(ref_Y_Rownxt+x_near+1  )+half_pel[2]+1)>>1);
                  *(syn_Y_Rownxt+idx2LumaN) = ((*(ref_Y_Rownxt+x_near)+half_pel[3]+1)>>1);
                }
                else if(disp_fractional==2)
                {
                  *(syn_Y       +idx2Luma ) = half_pel[0];
                  *(syn_Y       +idx2LumaN) = half_pel[1];
                  *(syn_Y_Rownxt+idx2Luma ) = half_pel[2];
                  *(syn_Y_Rownxt+idx2LumaN) = half_pel[3];
                }
                else
                {
                  *(syn_Y       +idx2Luma ) = ((*(ref_Y       +x_far+1  )+half_pel[0]+1)>>1);
                  *(syn_Y       +idx2LumaN) = ((*(ref_Y       +x_far)+half_pel[1]+1)>>1);
                  *(syn_Y_Rownxt+idx2Luma ) = ((*(ref_Y_Rownxt+x_far+1  )+half_pel[2]+1)>>1);
                  *(syn_Y_Rownxt+idx2LumaN) = ((*(ref_Y_Rownxt+x_far)+half_pel[3]+1)>>1);
                }
              }
              else
              {
                *(syn_Y       +idx2Luma )=*(ref_Y       +xLuma+1);
                *(syn_Y       +idx2LumaN)=*(ref_Y       +xLuma);
                *(syn_Y_Rownxt+idx2Luma )=*(ref_Y_Rownxt+xLuma+1);
                *(syn_Y_Rownxt+idx2LumaN)=*(ref_Y_Rownxt+xLuma);
              }

              if(dispUV_fractional)
              {
                if(dispUV_fractional<0)
                  INTERPOLATE_RIGHT_SIDE(x_flip, WidthUV_minus1)
                else
                  INTERPOLATE_LEFT_SIDE(x_flip, WidthUV_minus1)
#if !(NOKIA_CHROMA_BILINEAR)
                half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_U),0,255);
                half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_V),0,255);
#endif
                if(dispUV_fractional==1)
                {
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR1(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR1(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = ((*(ref_U+x_near)+half_pel[1]+1)>>1);
                  *(syn_V+idx2) = ((*(ref_V+x_near)+half_pel[2]+1)>>1);
#endif
                }
                else if(dispUV_fractional==2)
                {
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR2(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR2(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = half_pel[1];
                  *(syn_V+idx2) = half_pel[2];
#endif
                }
                else
                {
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR3(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR3(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = ((*(ref_U+x_far)+half_pel[1]+1)>>1);
                  *(syn_V+idx2) = ((*(ref_V+x_far)+half_pel[2]+1)>>1);
#endif
                }
              }
              else
              {
                *(syn_U+idx2)=*(ref_U+x_flip);
                *(syn_V+idx2)=*(ref_V+x_flip);
              }
#else
              idx2Luma = (idx2<<1);
              *(syn_Y+idx2Luma)=*(ref_Y+(2*x_flip)+1); //flipping meanwhile
              *(syn_Y+idx2Luma+1)=*(ref_Y+(2*x_flip)); //flipping meanwhile
              *(syn_Y_Rownxt+idx2Luma)=*(ref_Y_Rownxt+(2*x_flip)+1); //flipping meanwhile
              *(syn_Y_Rownxt+idx2Luma+1)=*(ref_Y_Rownxt+(2*x_flip)); //flipping meanwhile

              *(syn_U+idx2)=*(ref_U+x_flip);
              *(syn_V+idx2)=*(ref_V+x_flip);
#endif
              *(dmap+idx2) =*(ref_d+x_flip);
              warping_valid_flag = 1;
            }
          }

        }
        else
        {
#if NTT_SUBPEL_VSP_M24796
          x2      = x2_floor  = x + dispUV_integer;
          x2_s    = x2_ceil   = x2_floor + 1;
          x2Luma  = x2Luma_floor = (x<<1)+ disp_integer;
          x2Luma_s= x2Luma_ceil  = x2Luma_floor + 2;
#else
          x2_floor = (int)((x + disp) * SubPelOption);
          x2_ceil  = x2_floor + 1;

          x2 = x2_floor;
          x2_s = x2_ceil;
#endif

          if(x2_ceil < width_warped)
          {
            if(pDepth[x2_ceil] < crData)
            {
              idx2 = x2_ceil;
#if NTT_SUBPEL_VSP_M24796
              idx2Luma  = x2Luma_ceil;
              idx2LumaN = idx2Luma<WidthLuma_minus1 ? (idx2Luma+1) : WidthLuma_minus1;
              if(disp_fractional)
              {
                if(disp_fractional<0)
                  INTERPOLATE_RIGHT_SIDE(xLuma, WidthLuma_minus1)
                else
                  INTERPOLATE_LEFT_SIDE(xLuma, WidthLuma_minus1)
                half_pel[0]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y+1),0,255);
                half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y),0,255);
                half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt+1),0,255);
                half_pel[3]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt),0,255);
                if(disp_fractional==1)
                {
                  *(syn_Y       +idx2Luma ) = ((*(ref_Y       +x_far+1  )+half_pel[0]+1)>>1);
                  *(syn_Y       +idx2LumaN) = ((*(ref_Y       +x_far)+half_pel[1]+1)>>1);
                  *(syn_Y_Rownxt+idx2Luma ) = ((*(ref_Y_Rownxt+x_far+1  )+half_pel[2]+1)>>1);
                  *(syn_Y_Rownxt+idx2LumaN) = ((*(ref_Y_Rownxt+x_far)+half_pel[3]+1)>>1);
                }
                else if(disp_fractional==2)
                {
                  *(syn_Y       +idx2Luma ) = half_pel[0];
                  *(syn_Y       +idx2LumaN) = half_pel[1];
                  *(syn_Y_Rownxt+idx2Luma ) = half_pel[2];
                  *(syn_Y_Rownxt+idx2LumaN) = half_pel[3];
                }
                else
                {
                  *(syn_Y       +idx2Luma ) = ((*(ref_Y       +x_near+1  )+half_pel[0]+1)>>1);
                  *(syn_Y       +idx2LumaN) = ((*(ref_Y       +x_near)+half_pel[1]+1)>>1);
                  *(syn_Y_Rownxt+idx2Luma ) = ((*(ref_Y_Rownxt+x_near+1  )+half_pel[2]+1)>>1);
                  *(syn_Y_Rownxt+idx2LumaN) = ((*(ref_Y_Rownxt+x_near)+half_pel[3]+1)>>1);
                }
              }
              else
              {
                int x_neighbor = CLIP(xLuma-2, 0, WidthLuma_minus1+1);
                *(syn_Y       +idx2Luma )=*(ref_Y       +xLuma);
                *(syn_Y       +idx2LumaN)=*(ref_Y       +x_neighbor+1);
                *(syn_Y_Rownxt+idx2Luma )=*(ref_Y_Rownxt+xLuma);
                *(syn_Y_Rownxt+idx2LumaN)=*(ref_Y_Rownxt+x_neighbor);
              }

              if(dispUV_fractional)
              {
                if(dispUV_fractional<0)
                  INTERPOLATE_RIGHT_SIDE(x_flip, WidthUV_minus1)
                else
                  INTERPOLATE_LEFT_SIDE(x_flip, WidthUV_minus1)
#if !(NOKIA_CHROMA_BILINEAR)
                half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_U),0,255);
                half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_V),0,255);
#endif
                if(dispUV_fractional==1)
                {
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR3(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR3(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = ((*(ref_U+x_far)+half_pel[1]+1)>>1);
                  *(syn_V+idx2) = ((*(ref_V+x_far)+half_pel[2]+1)>>1);
#endif
                }
                else if(dispUV_fractional==2)
                {
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR2(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR2(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = half_pel[1];
                  *(syn_V+idx2) = half_pel[2];
#endif
                }
                else
                {
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR1(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR1(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = ((*(ref_U+x_near)+half_pel[1]+1)>>1);
                  *(syn_V+idx2) = ((*(ref_V+x_near)+half_pel[2]+1)>>1);
#endif
                }
              }
              else
              {
                int x_neighbor = CLIP(x_flip-1, 0, WidthUV_minus1);
                *(syn_U+idx2)=*(ref_U+x_neighbor);
                *(syn_V+idx2)=*(ref_V+x_neighbor);
              }
#else
              idx2Luma = (idx2<<1); 

              *(syn_Y+idx2Luma)=*(ref_Y+(2*x_flip)+1);
              *(syn_Y+idx2Luma+1)=*(ref_Y+(2*x_flip));
              *(syn_Y_Rownxt+idx2Luma)=*(ref_Y_Rownxt+(2*x_flip)+1);
              *(syn_Y_Rownxt+idx2Luma+1)=*(ref_Y_Rownxt+(2*x_flip));

              *(syn_U+idx2)=*(ref_U+x_flip);
              *(syn_V+idx2)=*(ref_V+x_flip);
#endif
              *(dmap+idx2) =*(ref_d+x_flip);
              warping_valid_flag = 1;
            }
          }

          if(x2_floor < width_warped)
          {
            if(pDepth[x2_floor] < crData)
            {
              idx2 = x2_floor;
#if NTT_SUBPEL_VSP_M24796
              idx2Luma  = x2Luma_floor;
              idx2LumaN = idx2Luma<WidthLuma_minus1 ? (idx2Luma+1) : WidthLuma_minus1;
              if(disp_fractional)
              {
                if(disp_fractional>0)
                  INTERPOLATE_RIGHT_SIDE(xLuma, WidthLuma_minus1)
                else
                  INTERPOLATE_LEFT_SIDE(xLuma, WidthLuma_minus1)
                half_pel[0]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y+1),0,255);
                half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y),0,255);
                half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt+1),0,255);
                half_pel[3]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt),0,255);
                if(disp_fractional==1)
                {
                  *(syn_Y       +idx2Luma ) = ((*(ref_Y       +x_near+1  )+half_pel[0]+1)>>1);
                  *(syn_Y       +idx2LumaN) = ((*(ref_Y       +x_near)+half_pel[1]+1)>>1);
                  *(syn_Y_Rownxt+idx2Luma ) = ((*(ref_Y_Rownxt+x_near+1  )+half_pel[2]+1)>>1);
                  *(syn_Y_Rownxt+idx2LumaN) = ((*(ref_Y_Rownxt+x_near)+half_pel[3]+1)>>1);
                }
                else if(disp_fractional==2)
                {
                  *(syn_Y       +idx2Luma ) = half_pel[0];
                  *(syn_Y       +idx2LumaN) = half_pel[1];
                  *(syn_Y_Rownxt+idx2Luma ) = half_pel[2];
                  *(syn_Y_Rownxt+idx2LumaN) = half_pel[3];
                }
                else
                {
                  *(syn_Y       +idx2Luma ) = ((*(ref_Y       +x_far+1  )+half_pel[0]+1)>>1);
                  *(syn_Y       +idx2LumaN) = ((*(ref_Y       +x_far)+half_pel[1]+1)>>1);
                  *(syn_Y_Rownxt+idx2Luma ) = ((*(ref_Y_Rownxt+x_far+1  )+half_pel[2]+1)>>1);
                  *(syn_Y_Rownxt+idx2LumaN) = ((*(ref_Y_Rownxt+x_far)+half_pel[3]+1)>>1);
                }
              }
              else
              {
                *(syn_Y       +idx2Luma )=*(ref_Y       +xLuma+1);
                *(syn_Y       +idx2LumaN)=*(ref_Y       +xLuma);
                *(syn_Y_Rownxt+idx2Luma )=*(ref_Y_Rownxt+xLuma+1);
                *(syn_Y_Rownxt+idx2LumaN)=*(ref_Y_Rownxt+xLuma);
              }

              if(dispUV_fractional)
              {
                if(dispUV_fractional>0)
                  INTERPOLATE_RIGHT_SIDE(x_flip, WidthUV_minus1)
                else
                  INTERPOLATE_LEFT_SIDE(x_flip, WidthUV_minus1)
#if !(NOKIA_CHROMA_BILINEAR)
                half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_U),0,255);
                half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_V),0,255);
#endif
                if(dispUV_fractional==1)
                {
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR1(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR1(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = ((*(ref_U+x_near)+half_pel[1]+1)>>1);
                  *(syn_V+idx2) = ((*(ref_V+x_near)+half_pel[2]+1)>>1);
#endif
                }
                else if(dispUV_fractional==2)
                {
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR2(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR2(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = half_pel[1];
                  *(syn_V+idx2) = half_pel[2];
#endif
                }
                else
                {
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR1(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR1(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = ((*(ref_U+x_far)+half_pel[1]+1)>>1);
                  *(syn_V+idx2) = ((*(ref_V+x_far)+half_pel[2]+1)>>1);
#endif
                }
              }
              else
              {
                *(syn_U+idx2)=*(ref_U+x_flip);
                *(syn_V+idx2)=*(ref_V+x_flip);
              }
#else
              idx2Luma = (idx2<<1); //

              *(syn_Y+idx2Luma)=*(ref_Y+(2*x_flip)+1);
              *(syn_Y+idx2Luma+1)=*(ref_Y+(2*x_flip));
              *(syn_Y_Rownxt+idx2Luma)=*(ref_Y_Rownxt+(2*x_flip)+1);
              *(syn_Y_Rownxt+idx2Luma+1)=*(ref_Y_Rownxt+(2*x_flip));

              *(syn_U+idx2)=*(ref_U+x_flip);
              *(syn_V+idx2)=*(ref_V+x_flip);
#endif
              *(dmap+idx2) =*(ref_d+x_flip);
              warping_valid_flag = 1;
            }
          }
        }
        /* --------- splatting/warping, start --------------*/
        if(warping_valid_flag)
        {
          if(xLast >= x2)
          {
            int cnt, use, fill, luma_use;
            //assert(xLast==x2);
            if(xLast > x2) 
              memset((void*)&pDepth[idx2+1], 0, (xLast-x2));
            if(x2!=0 && pDepth[x2-1] == 0)
            {
              for(cnt = 0; cnt < x2; cnt++)
              {
                if(pDepth[x2-1-cnt]!= 0) 
                  break;
              }
              if(cnt == x2)
              {
                use = x2;
                luma_use = x2<<1;
              }
              else if( pDepth[x2-cnt-1]>pDepth[x2])
              {
                use = x2;
                luma_use = x2<<1;
              }
              else
              {
                use = x2-1-cnt;
                luma_use = (use<<1)+1;
              }
              for(fill = (x2-cnt); fill < x2; fill++)
              {
                *(syn_Y+(fill*2)) =*(syn_Y+luma_use);
                *(syn_Y+(fill*2)+1) =*(syn_Y+luma_use);
                *(syn_Y_Rownxt+(fill*2)) =*(syn_Y_Rownxt+luma_use);
                *(syn_Y_Rownxt+(fill*2)+1) =*(syn_Y_Rownxt+luma_use);

                *(syn_U+fill) =*(syn_U+use);
                *(syn_V+fill) =*(syn_V+use);
                *(dmap +fill) =*(dmap +use);
              }
            }
          } 
          else
          { //xLast < x2
            int fill;
            if( !(x2!=0 && xLast== (x2-1)) )
            {
              if(xLast == 0)
              {
                for(fill = xLast; fill < x2; fill++)
                {
                  *(syn_Y+(fill*2))=*(syn_Y+(x2*2));
                  *(syn_Y+(fill*2)+1)=*(syn_Y+(x2*2));
                  *(syn_Y_Rownxt+(fill*2))=*(syn_Y_Rownxt+(x2*2));
                  *(syn_Y_Rownxt+(fill*2)+1)=*(syn_Y_Rownxt+(x2*2));

                  *(syn_U+fill)=*(syn_U+x2);
                  *(syn_V+fill)=*(syn_V+x2);
                  *(dmap+fill) =*(dmap +x2);
                }
              }else
              {
                if(pDepth[xLast] > pDepth[x2])
                {
                  for(fill = (xLast+1); fill < x2; fill++)
                  {
                    *(syn_Y+(fill*2))=*(syn_Y+(x2*2));
                    *(syn_Y+(fill*2)+1)=*(syn_Y+(x2*2));
                    *(syn_Y_Rownxt+(fill*2))=*(syn_Y_Rownxt+(x2*2));
                    *(syn_Y_Rownxt+(fill*2)+1)=*(syn_Y_Rownxt+(x2*2));

                    *(syn_U+fill)=*(syn_U+x2);
                    *(syn_V+fill)=*(syn_V+x2);
                    *(dmap+fill) =*(dmap+x2);
                  }
                }else
                {
                  for(fill = (xLast+1); fill < x2; fill++)
                  {
                    *(syn_Y+(fill*2))=*(syn_Y+(xLast*2)+1);
                    *(syn_Y+(fill*2)+1)=*(syn_Y+(xLast*2)+1);
                    *(syn_Y_Rownxt+(fill*2))=*(syn_Y_Rownxt+(xLast*2)+1);
                    *(syn_Y_Rownxt+(fill*2)+1)=*(syn_Y_Rownxt+(xLast*2)+1);

                    *(syn_U+fill)=*(syn_U+xLast);
                    *(syn_V+fill)=*(syn_V+xLast);
                    *(dmap+fill) =*(dmap+xLast);
                  }
                }
              }
            }
          }//end x2>=imgWidth
          xLast = x2_s; //rev
        }
      }//end x loop
      if(xLast != width_warped)
      {
        int fill;
        for(fill = xLast+1; fill < width_warped; fill++)
        {
          *(syn_Y+(fill*2))=*(syn_Y+(xLast*2)+1);
          *(syn_Y+(fill*2)+1)=*(syn_Y+(xLast*2)+1);
          *(syn_Y_Rownxt+(fill*2))=*(syn_Y_Rownxt+(xLast*2)+1);
          *(syn_Y_Rownxt+(fill*2)+1)=*(syn_Y_Rownxt+(xLast*2)+1);

          *(syn_U+fill)=*(syn_U+xLast);
          *(syn_V+fill)=*(syn_V+xLast);
          *(dmap+fill) =*(dmap+xLast);
        }
      }
      if(pDepth[0] == 0)
      {
        *(syn_Y+0) =*(syn_Y+1*2);
        *(syn_Y+0+1) =*(syn_Y+1*2);
        *(syn_Y_Rownxt+0) =*(syn_Y_Rownxt+1*2);
        *(syn_Y_Rownxt+0+1) =*(syn_Y_Rownxt+1*2);

        *(syn_U+0) =*(syn_U+1);
        *(syn_V+0) =*(syn_V+1);
        *(dmap +0) =*(dmap +1);
      }
      //flip

      for(x=0; x< width_warped; x++)
      {
        imgpel temp;
        int pos = width_warped*2-1-x;

        temp = *(syn_Y+x);
        *(syn_Y+x) = *(syn_Y+ pos);
        *(syn_Y+ pos) = temp;

        temp = *(syn_Y_Rownxt+x);
        *(syn_Y_Rownxt+x) = *(syn_Y_Rownxt+ pos);
        *(syn_Y_Rownxt+ pos) = temp;

      }
      for(x=0; x< width_warped_half; x++)
      {
        imgpel temp;
        int pos = width_warped_minus1-x;

        temp = *(syn_U+x);
        *(syn_U+x) = *(syn_U+ pos);
        *(syn_U+ pos) = temp;

        temp = *(syn_V+x);
        *(syn_V+x) = *(syn_V+ pos);
        *(syn_V+ pos) = temp;

        temp = *(dmap+x);
        *(dmap+x) = *(dmap+ pos);
        *(dmap+ pos) = temp;
      }

    }//end y loop
  }//end RIGHT_VIEW


#if NTT_SUBPEL_VSP_M24796
  if(SynDirection==2)
#endif
  {
    int y;
    for(y = 0; y < Height/2; ++y)
    {
      imgpel* syn_hlf_Y=SynY[HLFLVIEW][2*y];
      imgpel* syn_hlf_Y_Rownxt=SynY[HLFLVIEW][2*y+1];

      imgpel* syn_hlf_U=SynU[HLFLVIEW][y];
      imgpel* syn_hlf_V=SynV[HLFLVIEW][y];
      imgpel* syn_hlf_D=Dmap[HLFLVIEW][y];

      imgpel* syn_merge_Y=SynY[ViewId][2*y];
      imgpel* syn_merge_Y_Rownxt=SynY[ViewId][2*y+1];

      imgpel* syn_merge_U=SynU[ViewId][y];
      imgpel* syn_merge_V=SynV[ViewId][y];
      imgpel* syn_merge_D=Dmap[ViewId][y];

      memcpy(syn_hlf_Y,syn_merge_Y,width_warped*2*sizeof(imgpel));
      memcpy(syn_hlf_Y_Rownxt,syn_merge_Y_Rownxt,width_warped*2*sizeof(imgpel));

      memcpy(syn_hlf_U,syn_merge_U,width_warped*sizeof(imgpel));
      memcpy(syn_hlf_V,syn_merge_V,width_warped*sizeof(imgpel));
      memcpy(syn_hlf_D,syn_merge_D,width_warped*sizeof(imgpel));
    }
  }
}

void CViewInterpolation1D::ViewGen_1LWH420(int ViewId)
{
  int i;
#if (VSP_OPTIMIZATION)
  int WidthUV = Width/2;
  int WidthUV_minus1 = Width/2-1;
  int WidthLuma_minus1 = Width-1;
  int mem_id = SynDirection==2 ? ViewId : FINLVIEW;
#endif

#if VSP_OPTIMIZATION
  if(SynDirection==2)
  {
    for (i = 0; i < 3; i++)
    {
#if NTT_SUBPEL_VSP_M24796
      memset(SynY[i][0], 0,         Width*Height*sizeof(imgpel));
      memset(SynU[i][0], 0,         Width*Height/4*sizeof(imgpel));
      memset(SynV[i][0], 0,         Width*Height/4*sizeof(imgpel));
      memset(Dmap[i][0], 0,         Width*Height/4*sizeof(byte));
#else
      memset(SynY[i][0], 0,         Width2*Height*SubPelOption*sizeof(imgpel));
      memset(SynU[i][0], 0,         Width2*Height/4*SubPelOption*sizeof(imgpel));
      memset(SynV[i][0], 0,         Width2*Height/4*SubPelOption*sizeof(imgpel));
      memset(Dmap[i][0], 0,         Width2*Height/4*SubPelOption*sizeof(byte));
#endif
    }
  }
#if !NTT_SUBPEL_VSP_M24796
  else if(SynDirection==1)
  {
    memset(SynY[1][0], 0,         Width2*Height*SubPelOption*sizeof(imgpel));
    memset(SynU[1][0], 0,         Width2*Height/4*SubPelOption*sizeof(imgpel));
    memset(SynV[1][0], 0,         Width2*Height/4*SubPelOption*sizeof(imgpel));
    memset(Dmap[1][0], 0,         Width2*Height/4*SubPelOption*sizeof(byte));
  }
  else if(SynDirection==0)
  {
    memset(SynY[0][0], 0,         Width2*Height*SubPelOption*sizeof(imgpel));
    memset(SynU[0][0], 0,         Width2*Height/4*SubPelOption*sizeof(imgpel));
    memset(SynV[0][0], 0,         Width2*Height/4*SubPelOption*sizeof(imgpel));
    memset(Dmap[0][0], 0,         Width2*Height/4*SubPelOption*sizeof(byte));
  }
#endif
#endif

  if(ViewId == LEFTVIEW)
    WarpToRight = 1;
  else
    WarpToRight = 0;

  //start to warp each pixel
  if( WarpToRight == 0) // right view-- from right 2 left
  {
    int x, y, xLast, idx2, x2, idx2Luma;
    imgpel* ref_Y=NULL;
    imgpel* ref_Y_Rownxt=NULL;

    imgpel* ref_U=NULL;
    imgpel* ref_V=NULL;
    imgpel* ref_d=NULL;
    imgpel* dmap=NULL;
    //imgpel* mask=NULL;

    imgpel* syn_Y=NULL;
    imgpel* syn_Y_Rownxt=NULL;

    imgpel* syn_U=NULL;
    imgpel* syn_V=NULL;
#if NTT_SUBPEL_VSP_M24796
    int  xLuma, x2Luma, idx2LumaN;
    int  disp_integer;
    int  disp_fractional;
    int  dispUV_integer;
    int  dispUV_fractional;
    int  p0, p1, p2, p3, p4, p5;
    int  x_far, x_near;
    imgpel half_pel[4];
    imgpel* pDepth = (imgpel*)calloc(WidthUV, sizeof(imgpel)); //same
#else
    imgpel* pDepth = (imgpel*)calloc(Width2*SubPelOption/2, sizeof(imgpel)); //same 
    //imgpel* pDepth = (imgpel*)malloc(Width2*SubPelOption*sizeof(imgpel)); //same 
    double  disp; // shoulde be revised to int?
#endif
    imgpel  crData;

    for (y = 0; y < Height/2; ++y)
    {
      ref_Y=RefView[ViewId][0][2*y];
      ref_Y_Rownxt=RefView[ViewId][0][2*y+1];

      ref_U=RefView[ViewId][1][y];
      ref_V=RefView[ViewId][2][y];
      ref_d=RefDepth[ViewId][y];
#if NTT_SUBPEL_VSP_M24796
      syn_Y=SynY[mem_id][2*y];
      syn_Y_Rownxt=SynY[mem_id][2*y+1];
      syn_U=SynU[mem_id][y];
      syn_V=SynV[mem_id][y];
      dmap=Dmap[mem_id][y];
      memset(syn_Y, 0, Width*sizeof(imgpel));
      memset(syn_Y_Rownxt, 0, Width*sizeof(imgpel));
      memset(syn_U, 0, WidthUV*sizeof(imgpel));
      memset(syn_V, 0, WidthUV*sizeof(imgpel));
      memset(dmap, 0, WidthUV*sizeof(imgpel));
      memset(pDepth, 0, WidthUV*sizeof(imgpel));
#else
      //mask=Mask[ViewId][y];
      syn_Y=SynY[ViewId][2*y];
      syn_Y_Rownxt=SynY[ViewId][2*y+1];
      syn_U=SynU[ViewId][y];
      syn_V=SynV[ViewId][y];
      dmap=Dmap[ViewId][y];
      memset(pDepth, 0, Width2*SubPelOption/2*sizeof(imgpel));
#endif

      xLast = 0;

#if NTT_SUBPEL_VSP_M24796
      for (x = 0; x < WidthUV;  ++x)
#else
      for (x = 0; x < Width2/2;  ++x)
#endif
      {
        //get disparity
        crData = *(ref_d+x);
#if NTT_SUBPEL_VSP_M24796
#if VSP_OPTIMIZATION
        xLuma = (x<<1) + DisparityLookupTable[ViewId][crData].referenceY[0];
        disp_integer      = DisparityLookupTable[ViewId][crData].targetUV*2;
        disp_fractional   = DisparityLookupTable[ViewId][crData].referenceY[1];
        dispUV_integer    = DisparityLookupTable[ViewId][crData].targetUV;
        dispUV_fractional = DisparityLookupTable[ViewId][crData].referenceUV;
#else
        {
#if POZNAN_NONLINEAR_DEPTH
          int in = nonlinear_depth_lut_backward[crData];
#else
          int in = crData;
#endif
          double z, db_disparity, db_disparity_subpel, db_disparity_uv_subpel;

          z=1.0/( (in/255.0) * (1/Znear[ViewId] - 1/Zfar[ViewId]) + (1/Zfar[ViewId]) );
          db_disparity        = (FocalLength*LTranslation[ViewId]/z)-duPrincipal[ViewId];
          db_disparity_subpel = db_disparity*SubPelOption;
          db_disparity_uv_subpel = db_disparity_subpel/2;
          dispUV_integer      = (int)(db_disparity/2);
          dispUV_fractional   = (int)(fabs(db_disparity_uv_subpel)+0.5)-abs(dispUV_integer)*SubPelOption;
          if(dispUV_fractional==SubPelOption)
          {
            dispUV_fractional = 0;
            dispUV_integer += (db_disparity<0?-1:1);
          }
          if(SubPelOption==2) dispUV_fractional *= 2;
          disp_integer = dispUV_integer*2;
          xLuma = (x<<1) + ((int)db_disparity) - disp_integer;
          disp_fractional = (int)(fabs(db_disparity_subpel)+0.5)-abs(((int)db_disparity))*SubPelOption;
          if(disp_fractional==SubPelOption)
          {
            disp_fractional = 0;
            xLuma += (db_disparity<0?-1:1);
          }
          if(SubPelOption==2) disp_fractional *= 2;
        }
#endif
#else // NTT_SUBPEL_VSP_M24796
#if VSP_OPTIMIZATION
        if(UpsampleRefs==2)
          disp=DisparityLookupTable[ViewId][*(ref_d+x)].db_disparity_mul2;
        else if(UpsampleRefs==4)
          disp=DisparityLookupTable[ViewId][*(ref_d+x)].db_disparity_mul4;
        else  
          disp=DisparityLookupTable[ViewId][*(ref_d+x)].db_disparity;

        // now the width of U, V are half of before, disparity should be halfed too
          disp=(disp>=0) ?((disp+1)/2) :((disp-1)/2);

#endif
#endif // NTT_SUBPEL_VSP_M24796
        
#if NTT_SUBPEL_VSP_M24796
        x2      = x     - dispUV_integer;
        x2Luma  = (x<<1)- disp_integer;
#else
        x2 = (int)((x - disp) * SubPelOption + 0.5);
#endif

#if NTT_SUBPEL_VSP_M24796
        if(x2 < WidthUV && x2 >=0)
#else
        if(x2 < Width2*SubPelOption/2 && x2 >=0)
#endif
        {
          if( x2 <0)
            printf("x2 error %2d", x2);
          if(pDepth[x2] < crData)
          {
#if NTT_SUBPEL_VSP_M24796
            idx2 = x2;
            idx2Luma  = x2Luma;
            idx2LumaN = idx2Luma<WidthLuma_minus1 ? (idx2Luma+1) : WidthLuma_minus1;
            if(disp_fractional)
            {
              if(disp_fractional<0)
                INTERPOLATE_RIGHT_SIDE(xLuma, WidthLuma_minus1)
              else
                INTERPOLATE_LEFT_SIDE(xLuma, WidthLuma_minus1)
              half_pel[0]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y),0,255);
              half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y+1),0,255);
              half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt),0,255);
              half_pel[3]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt+1),0,255);
              if(disp_fractional==1)
              {
                *(syn_Y       +idx2Luma ) = ((*(ref_Y       +x_near  )+half_pel[0]+1)>>1);
                *(syn_Y       +idx2LumaN) = ((*(ref_Y       +x_near+1)+half_pel[1]+1)>>1);
                *(syn_Y_Rownxt+idx2Luma ) = ((*(ref_Y_Rownxt+x_near  )+half_pel[2]+1)>>1);
                *(syn_Y_Rownxt+idx2LumaN) = ((*(ref_Y_Rownxt+x_near+1)+half_pel[3]+1)>>1);
              }
              else if(disp_fractional==2)
              {
                *(syn_Y       +idx2Luma ) = half_pel[0];
                *(syn_Y       +idx2LumaN) = half_pel[1];
                *(syn_Y_Rownxt+idx2Luma ) = half_pel[2];
                *(syn_Y_Rownxt+idx2LumaN) = half_pel[3];
              }
              else
              {
                *(syn_Y       +idx2Luma ) = ((*(ref_Y       +x_far  )+half_pel[0]+1)>>1);
                *(syn_Y       +idx2LumaN) = ((*(ref_Y       +x_far+1)+half_pel[1]+1)>>1);
                *(syn_Y_Rownxt+idx2Luma ) = ((*(ref_Y_Rownxt+x_far  )+half_pel[2]+1)>>1);
                *(syn_Y_Rownxt+idx2LumaN) = ((*(ref_Y_Rownxt+x_far+1)+half_pel[3]+1)>>1);
              }
            }
            else
            {
              *(syn_Y       +idx2Luma )=*(ref_Y       +xLuma);
              *(syn_Y       +idx2LumaN)=*(ref_Y       +xLuma+1);
              *(syn_Y_Rownxt+idx2Luma )=*(ref_Y_Rownxt+xLuma);
              *(syn_Y_Rownxt+idx2LumaN)=*(ref_Y_Rownxt+xLuma+1);
            }

            if(dispUV_fractional)
            {
              if(dispUV_fractional<0)
                INTERPOLATE_RIGHT_SIDE(x, WidthUV_minus1)
              else
                INTERPOLATE_LEFT_SIDE(x, WidthUV_minus1)
#if !(NOKIA_CHROMA_BILINEAR)
              half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_U),0,255);
              half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_V),0,255);
#endif
              if(dispUV_fractional==1)
              {
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+idx2) = BILINEAR1(ref_U, x_near, x_far);
                *(syn_V+idx2) = BILINEAR1(ref_V, x_near, x_far);
#else
                *(syn_U+idx2) = ((*(ref_U+x_near)+half_pel[1]+1)>>1);
                *(syn_V+idx2) = ((*(ref_V+x_near)+half_pel[2]+1)>>1);
#endif
              }
              else if(dispUV_fractional==2)
              {
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+idx2) = BILINEAR2(ref_U, x_near, x_far);
                *(syn_V+idx2) = BILINEAR2(ref_V, x_near, x_far);
#else
                *(syn_U+idx2) = half_pel[1];
                *(syn_V+idx2) = half_pel[2];
#endif
              }
              else
              {
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+idx2) = BILINEAR3(ref_U, x_near, x_far);
                *(syn_V+idx2) = BILINEAR3(ref_V, x_near, x_far);
#else
                *(syn_U+idx2) = ((*(ref_U+x_far)+half_pel[1]+1)>>1);
                *(syn_V+idx2) = ((*(ref_V+x_far)+half_pel[2]+1)>>1);
#endif
              }
            }
            else
            {
              *(syn_U+idx2)=*(ref_U+x);
              *(syn_V+idx2)=*(ref_V+x);
            }
#else
            idx2 = (int)((x - disp) * SubPelOption + 0.5);
            idx2Luma = (idx2<<1); //

            *(syn_Y+idx2Luma)=*(ref_Y+(2*x));
            *(syn_Y+idx2Luma+1)=*(ref_Y+(2*x)+1);
            *(syn_Y_Rownxt+idx2Luma)=*(ref_Y_Rownxt+(2*x));
            *(syn_Y_Rownxt+idx2Luma+1)=*(ref_Y_Rownxt+(2*x)+1);

            *(syn_U+idx2)=*(ref_U+x);
            *(syn_V+idx2)=*(ref_V+x);
#endif
            *(dmap+idx2) =*(ref_d+x);
            pDepth[x2] = crData;

            if(xLast >= x2)
            {
              int cnt, use, fill, luma_use;
              if(xLast > x2) 
                memset((void*)&pDepth[idx2+1], 0, (xLast-x2));
              if(x2!=0 && pDepth[x2-1] == 0)
              {
                for(cnt = 0; cnt < x2; cnt++)
                {
                  if(pDepth[x2-1-cnt]!= 0 ) 
                    break;
                }
                if(cnt == x2)
                {
                  use = x2;
                  luma_use = use<<1;
                }
                else if( pDepth[x2-cnt-1]>pDepth[x2])
                {
                  use = x2;
                  luma_use = use<<1;
                }
                else
                {
                  use = x2-1-cnt;
                  luma_use = (use<<1)+1;
                }
                for(fill = (x2-cnt); fill < x2; fill++)
                {
                  *(syn_Y+(fill*2)) =*(syn_Y+luma_use);
                  *(syn_Y+(fill*2)+1) =*(syn_Y+luma_use);
                  *(syn_Y_Rownxt+(fill*2)) =*(syn_Y_Rownxt+luma_use);
                  *(syn_Y_Rownxt+(fill*2)+1) =*(syn_Y_Rownxt+luma_use);

                  *(syn_U+fill) =*(syn_U+use);
                  *(syn_V+fill) =*(syn_V+use);
                  *(dmap +fill) =*(dmap +use);
                }
              }
            } 
            else
            { //xLast < x2
              int fill;
              if( !(x2!=0 && xLast== (x2-1)) )
              {
                if(xLast == 0)
                {
                  for(fill = xLast; fill < x2; fill++)
                  {
                    *(syn_Y+(fill*2))=*(syn_Y+(x2*2));
                    *(syn_Y+(fill*2)+1)=*(syn_Y+(x2*2));
                    *(syn_Y_Rownxt+(fill*2))=*(syn_Y_Rownxt+(x2*2));
                    *(syn_Y_Rownxt+(fill*2)+1)=*(syn_Y_Rownxt+(x2*2));


                    *(syn_U+fill)=*(syn_U+x2);
                    *(syn_V+fill)=*(syn_V+x2);
                    *(dmap+fill) =*(dmap +x2);
                  }
                }
                else
                {
                  if(pDepth[xLast] > pDepth[x2])
                  {
                    for(fill = (xLast+1); fill < x2; fill++)
                    {
                      *(syn_Y+(fill*2))=*(syn_Y+(x2*2));
                      *(syn_Y+(fill*2)+1)=*(syn_Y+(x2*2));
                      *(syn_Y_Rownxt+(fill*2))=*(syn_Y_Rownxt+(x2*2));
                      *(syn_Y_Rownxt+(fill*2)+1)=*(syn_Y_Rownxt+(x2*2));

                      *(syn_U+fill)=*(syn_U+x2);
                      *(syn_V+fill)=*(syn_V+x2);
                      *(dmap+fill) =*(dmap+x2);
                    }
                  }
                  else
                  {
                    for(fill = (xLast+1); fill < x2; fill++)
                    {
                      *(syn_Y+(fill*2))=*(syn_Y+(xLast*2)+1);
                      *(syn_Y+(fill*2)+1)=*(syn_Y+(xLast*2+1));
                      *(syn_Y_Rownxt+(fill*2))=*(syn_Y_Rownxt+(xLast*2)+1);
                      *(syn_Y_Rownxt+(fill*2)+1)=*(syn_Y_Rownxt+(xLast*2)+1);

                      *(syn_U+fill)=*(syn_U+xLast);
                      *(syn_V+fill)=*(syn_V+xLast);
                      *(dmap+fill) =*(dmap+xLast);
                    }
                  }
                }
              }
            }//end x2>=imgWidth
            xLast = x2;

          }//end pDepth<crData

        }
      }//end x loop
#if NTT_SUBPEL_VSP_M24796
      if(xLast != WidthUV)
#else
      if(xLast != Width2 * SubPelOption/2)
#endif
      {
        int fill;
#if NTT_SUBPEL_VSP_M24796
        for(fill = xLast+1; fill < WidthUV; fill++)
#else
        for(fill = xLast+1; fill < Width2*SubPelOption/2; fill++)
#endif
        {
          *(syn_Y+(fill*2))=*(syn_Y+(xLast*2)+1);
          *(syn_Y+(fill*2)+1)=*(syn_Y+(xLast*2)+1);
          *(syn_Y_Rownxt+(fill*2))=*(syn_Y_Rownxt+(xLast*2)+1);
          *(syn_Y_Rownxt+(fill*2)+1)=*(syn_Y_Rownxt+(xLast*2)+1);

          *(syn_U+fill)=*(syn_U+xLast);
          *(syn_V+fill)=*(syn_V+xLast);
          *(dmap+fill) =*(dmap+xLast);
        }
      }
      if(pDepth[0] == 0)
      {
        *(syn_Y+0) =*(syn_Y+1*2);
        *(syn_Y+0+1) =*(syn_Y+1*2);
        *(syn_Y_Rownxt+0) =*(syn_Y_Rownxt+1*2);
        *(syn_Y_Rownxt+0+1) =*(syn_Y_Rownxt+1*2);

        *(syn_U+0) =*(syn_U+1);
        *(syn_V+0) =*(syn_V+1);
        *(dmap +0) =*(dmap +1);
      }
    }//end y loop
    if(pDepth)
      free(pDepth);

  }
  else
  {
    int x, y, xLast, idx2, x2, idx2Luma, x_flip;
    imgpel* ref_Y=NULL;
    imgpel* ref_Y_Rownxt=NULL;

    imgpel* ref_U=NULL;
    imgpel* ref_V=NULL;
    imgpel* ref_d=NULL;
    imgpel* dmap=NULL;

    imgpel* syn_Y=NULL;
    imgpel* syn_Y_Rownxt=NULL;

    imgpel* syn_U=NULL;
    imgpel* syn_V=NULL;
#if NTT_SUBPEL_VSP_M24796
    int  xLuma, x2Luma, idx2LumaN;
    int  disp_integer;
    int  disp_fractional;
    int  dispUV_integer;
    int  dispUV_fractional;
    int  p0, p1, p2, p3, p4, p5;
    int  x_far, x_near;
    imgpel half_pel[4];
    imgpel* pDepth = (imgpel*)calloc(WidthUV, sizeof(imgpel)); //same 
#else
    imgpel* pDepth = (imgpel*)calloc(Width2*SubPelOption/2, sizeof(imgpel)); //same 
    double  disp; // shoulde be revised to int?
#endif
    imgpel  crData;

    for (y = 0; y < Height/2; ++y)
    {
      ref_Y=RefView[ViewId][0][2*y];
      ref_Y_Rownxt=RefView[ViewId][0][2*y+1];

      ref_U=RefView[ViewId][1][y];
      ref_V=RefView[ViewId][2][y];
      ref_d=RefDepth[ViewId][y];
#if NTT_SUBPEL_VSP_M24796
      syn_Y=SynY[mem_id][2*y];
      syn_Y_Rownxt=SynY[mem_id][2*y+1];
      syn_U=SynU[mem_id][y];
      syn_V=SynV[mem_id][y];
      dmap=Dmap[mem_id][y];
      memset(syn_Y, 0, Width*sizeof(imgpel));
      memset(syn_Y_Rownxt, 0, Width*sizeof(imgpel));
      memset(syn_U, 0, WidthUV*sizeof(imgpel));
      memset(syn_V, 0, WidthUV*sizeof(imgpel));
      memset(pDepth, 0, WidthUV*sizeof(imgpel));
      memset(dmap, 0, WidthUV*sizeof(imgpel));
#else
      syn_Y=SynY[ViewId][2*y];
      syn_Y_Rownxt=SynY[ViewId][2*y+1];
      syn_U=SynU[ViewId][y];
      syn_V=SynV[ViewId][y];
      dmap=Dmap[ViewId][y];
      memset(pDepth, 0, Width2*SubPelOption/2*sizeof(imgpel));
#endif
      xLast = 0;

#if NTT_SUBPEL_VSP_M24796
      for(x = 0; x < WidthUV;  ++x)
#else
      for(x = 0; x < Width2/2;  ++x)
#endif
      {
        //get disparity
#if NTT_SUBPEL_VSP_M24796
        x_flip = WidthUV_minus1-x;
#else
        x_flip = Width2/2-1-x;
#endif
        crData = *(ref_d+(x_flip));

#if NTT_SUBPEL_VSP_M24796
#if VSP_OPTIMIZATION
        xLuma = (x_flip<<1) + DisparityLookupTable[ViewId][crData].referenceY[0];
        disp_integer      = DisparityLookupTable[ViewId][crData].targetUV*2;
        disp_fractional   = DisparityLookupTable[ViewId][crData].referenceY[1];
        dispUV_integer    = DisparityLookupTable[ViewId][crData].targetUV;
        dispUV_fractional = DisparityLookupTable[ViewId][crData].referenceUV;
#else
        {
#if POZNAN_NONLINEAR_DEPTH
          int in = nonlinear_depth_lut_backward[crData];
#else
          int in = crData;
#endif
          double z, db_disparity, db_disparity_subpel, db_disparity_uv_subpel;

          z=1.0/( (in/255.0) * (1/Znear[ViewId] - 1/Zfar[ViewId]) + (1/Zfar[ViewId]) );
          db_disparity        = (FocalLength*LTranslation[ViewId]/z)-duPrincipal[ViewId];
          db_disparity_subpel = db_disparity*SubPelOption;
          db_disparity_uv_subpel = db_disparity_subpel/2;
          dispUV_integer      = (int)(db_disparity/2);
          dispUV_fractional   = (int)(fabs(db_disparity_uv_subpel)+0.5)-abs(dispUV_integer)*SubPelOption;
          if(dispUV_fractional==SubPelOption)
          {
            dispUV_fractional = 0;
            dispUV_integer += (db_disparity<0?-1:1);
          }
          if(SubPelOption==2) dispUV_fractional *= 2;
          disp_integer = dispUV_integer*2;
          xLuma = (x_flip<<1) + ((int)db_disparity) - disp_integer;
          disp_fractional = (int)(fabs(db_disparity_subpel)+0.5)-abs(((int)db_disparity))*SubPelOption;
          if(disp_fractional==SubPelOption)
          {
            disp_fractional = 0;
            xLuma += (db_disparity<0?-1:1);
          }
          if(SubPelOption==2) disp_fractional *= 2;
        }
#endif
        x2      = x     + dispUV_integer;
        x2Luma  = (x<<1)+ disp_integer;
#else // NTT_SUBPEL_VSP_M24796
#if VSP_OPTIMIZATION
        if(UpsampleRefs==2)
          disp= 0 - DisparityLookupTable[ViewId][*(ref_d+(x_flip))].db_disparity_mul2;
        else if(UpsampleRefs==4)
          disp= 0 - DisparityLookupTable[ViewId][*(ref_d+(x_flip))].db_disparity_mul4;
        else  
          disp= 0 - DisparityLookupTable[ViewId][*(ref_d+(x_flip))].db_disparity;
        disp=(disp>=0) ?((disp+1)/2) :((disp-1)/2);
#endif
        x2 = (int)((x - disp) * SubPelOption + 0.5);
#endif // NTT_SUBPEL_VSP_M24796

#if NTT_SUBPEL_VSP_M24796
        if(x2 < WidthUV && x2 >= 0)
#else
        if(x2 < Width2*SubPelOption/2 && x2 >= 0)
#endif
        {
          if( x2 <0)
            printf("x2 error %2d", x2);
          if(pDepth[x2] < crData)
          {
#if NTT_SUBPEL_VSP_M24796
            idx2 = x2;
            idx2Luma  = x2Luma;
            idx2LumaN = idx2Luma<WidthLuma_minus1 ? (idx2Luma+1) : WidthLuma_minus1;;
            if(disp_fractional)
            {
              if(disp_fractional>0)
                INTERPOLATE_RIGHT_SIDE(xLuma, WidthLuma_minus1)
              else
                INTERPOLATE_LEFT_SIDE(xLuma, WidthLuma_minus1)
              half_pel[0]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y+1),0,255);
              half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y),0,255);
              half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt+1),0,255);
              half_pel[3]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y_Rownxt),0,255);
              if(disp_fractional==1)
              {
                *(syn_Y       +idx2Luma ) = ((*(ref_Y       +x_near+1  )+half_pel[0]+1)>>1);
                *(syn_Y       +idx2LumaN) = ((*(ref_Y       +x_near)+half_pel[1]+1)>>1);
                *(syn_Y_Rownxt+idx2Luma ) = ((*(ref_Y_Rownxt+x_near+1  )+half_pel[2]+1)>>1);
                *(syn_Y_Rownxt+idx2LumaN) = ((*(ref_Y_Rownxt+x_near)+half_pel[3]+1)>>1);
              }
              else if(disp_fractional==2)
              {
                *(syn_Y       +idx2Luma ) = half_pel[0];
                *(syn_Y       +idx2LumaN) = half_pel[1];
                *(syn_Y_Rownxt+idx2Luma ) = half_pel[2];
                *(syn_Y_Rownxt+idx2LumaN) = half_pel[3];
              }
              else
              {
                *(syn_Y       +idx2Luma ) = ((*(ref_Y       +x_far+1  )+half_pel[0]+1)>>1);
                *(syn_Y       +idx2LumaN) = ((*(ref_Y       +x_far)+half_pel[1]+1)>>1);
                *(syn_Y_Rownxt+idx2Luma ) = ((*(ref_Y_Rownxt+x_far+1  )+half_pel[2]+1)>>1);
                *(syn_Y_Rownxt+idx2LumaN) = ((*(ref_Y_Rownxt+x_far)+half_pel[3]+1)>>1);
              }
            }
            else
            {
              *(syn_Y       +idx2Luma )=*(ref_Y       +xLuma+1);
              *(syn_Y       +idx2LumaN)=*(ref_Y       +xLuma);
              *(syn_Y_Rownxt+idx2Luma )=*(ref_Y_Rownxt+xLuma+1);
              *(syn_Y_Rownxt+idx2LumaN)=*(ref_Y_Rownxt+xLuma);
            }

            if(dispUV_fractional)
            {
              if(dispUV_fractional<0)
                INTERPOLATE_RIGHT_SIDE(x_flip, WidthUV_minus1)
              else
                INTERPOLATE_LEFT_SIDE(x_flip, WidthUV_minus1)
#if !(NOKIA_CHROMA_BILINEAR)
              half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_U),0,255);
              half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_V),0,255);
#endif
              if(dispUV_fractional==1)
              {
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+idx2) = BILINEAR1(ref_U, x_near, x_far);
                *(syn_V+idx2) = BILINEAR1(ref_V, x_near, x_far);
#else
                *(syn_U+idx2) = ((*(ref_U+x_near)+half_pel[1]+1)>>1);
                *(syn_V+idx2) = ((*(ref_V+x_near)+half_pel[2]+1)>>1);
#endif
              }
              else if(dispUV_fractional==2)
              {
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+idx2) = BILINEAR2(ref_U, x_near, x_far);
                *(syn_V+idx2) = BILINEAR2(ref_V, x_near, x_far);
#else
                *(syn_U+idx2) = half_pel[1];
                *(syn_V+idx2) = half_pel[2];
#endif
              }
              else
              {
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+idx2) = BILINEAR3(ref_U, x_near, x_far);
                *(syn_V+idx2) = BILINEAR3(ref_V, x_near, x_far);
#else
                *(syn_U+idx2) = ((*(ref_U+x_far)+half_pel[1]+1)>>1);
                *(syn_V+idx2) = ((*(ref_V+x_far)+half_pel[2]+1)>>1);
#endif
              }
            }
            else
            {
              *(syn_U+idx2)=*(ref_U+x_flip);
              *(syn_V+idx2)=*(ref_V+x_flip);
            }
#else
            idx2 = (int)((x - disp) * SubPelOption + 0.5);
            idx2Luma = (idx2<<1);

            *(syn_Y+idx2Luma)=*(ref_Y+(2*x_flip)+1); //flipping meanwhile
            *(syn_Y+idx2Luma+1)=*(ref_Y+(2*x_flip)); //flipping meanwhile
            *(syn_Y_Rownxt+idx2Luma)=*(ref_Y_Rownxt+(2*x_flip)+1); //flipping meanwhile
            *(syn_Y_Rownxt+idx2Luma+1)=*(ref_Y_Rownxt+(2*x_flip)); //flipping meanwhile

            *(syn_U+idx2)=*(ref_U+x_flip);
            *(syn_V+idx2)=*(ref_V+x_flip);
#endif
            *(dmap+idx2) =*(ref_d+x_flip);
            pDepth[x2] = crData;

            if(xLast >= x2)
            {
              int cnt, use, fill, luma_use;
              if(xLast > x2) 
                memset((void*)&pDepth[idx2+1], 0, (xLast-x2));
              if(x2!=0 && pDepth[x2-1] == 0)
              {
                for(cnt = 0; cnt < x2; cnt++)
                {
                  if(pDepth[x2-1-cnt]!= 0) 
                    break;
                }
                if(cnt == x2)
                {
                  use = x2;
                  luma_use = x2<<1;
                }
                else if( pDepth[x2-cnt-1]>pDepth[x2])
                {
                  use = x2;
                  luma_use = x2<<1;
                }
                else
                {
                  use = x2-1-cnt;
                  luma_use = (use<<1)+1;
                }
                for(fill = (x2-cnt); fill < x2; fill++)
                {
                  *(syn_Y+(fill*2)) =*(syn_Y+luma_use);
                  *(syn_Y+(fill*2)+1) =*(syn_Y+luma_use);
                  *(syn_Y_Rownxt+(fill*2)) =*(syn_Y_Rownxt+luma_use);
                  *(syn_Y_Rownxt+(fill*2)+1) =*(syn_Y_Rownxt+luma_use);

                  *(syn_U+fill) =*(syn_U+use);
                  *(syn_V+fill) =*(syn_V+use);
                  *(dmap +fill) =*(dmap +use);
                }
              }
            } 
            else
            { //xLast < x2
              int fill;
              if( !(x2!=0 && xLast== (x2-1)) )
              {
                if(xLast == 0)
                {
                  for(fill = xLast; fill < x2; fill++)
                  {
                    *(syn_Y+(fill*2))=*(syn_Y+(x2*2));
                    *(syn_Y+(fill*2)+1)=*(syn_Y+(x2*2));
                    *(syn_Y_Rownxt+(fill*2))=*(syn_Y_Rownxt+(x2*2));
                    *(syn_Y_Rownxt+(fill*2)+1)=*(syn_Y_Rownxt+(x2*2));

                    *(syn_U+fill)=*(syn_U+x2);
                    *(syn_V+fill)=*(syn_V+x2);
                    *(dmap+fill) =*(dmap +x2);
                  }
                }else
                {
                  if(pDepth[xLast] > pDepth[x2])
                  {
                    for(fill = (xLast+1); fill < x2; fill++)
                    {
                      *(syn_Y+(fill*2))=*(syn_Y+(x2*2));
                      *(syn_Y+(fill*2)+1)=*(syn_Y+(x2*2));
                      *(syn_Y_Rownxt+(fill*2))=*(syn_Y_Rownxt+(x2*2));
                      *(syn_Y_Rownxt+(fill*2)+1)=*(syn_Y_Rownxt+(x2*2));

                      *(syn_U+fill)=*(syn_U+x2);
                      *(syn_V+fill)=*(syn_V+x2);
                      *(dmap+fill) =*(dmap+x2);
                    }
                  }else
                  {
                    for(fill = (xLast+1); fill < x2; fill++)
                    {
                      *(syn_Y+(fill*2))=*(syn_Y+(xLast*2)+1);
                      *(syn_Y+(fill*2)+1)=*(syn_Y+(xLast*2)+1);
                      *(syn_Y_Rownxt+(fill*2))=*(syn_Y_Rownxt+(xLast*2)+1);
                      *(syn_Y_Rownxt+(fill*2)+1)=*(syn_Y_Rownxt+(xLast*2)+1);

                      *(syn_U+fill)=*(syn_U+xLast);
                      *(syn_V+fill)=*(syn_V+xLast);
                      *(dmap+fill) =*(dmap+xLast);
                    }
                  }
                }
              }
            }//end x2>=imgWidth
            xLast = x2;

          }//end pDepth<crData

        }
      }//end x loop
#if NTT_SUBPEL_VSP_M24796
      if(xLast != WidthUV)
#else
      if(xLast != Width2*SubPelOption/2)
#endif
      {
        int fill;
#if NTT_SUBPEL_VSP_M24796
        for(fill = xLast+1; fill < WidthUV; fill++)
#else
        for(fill = xLast+1; fill < Width2*SubPelOption/2; fill++)
#endif
        {
          *(syn_Y+(fill*2))=*(syn_Y+(xLast*2)+1);
          *(syn_Y+(fill*2)+1)=*(syn_Y+(xLast*2)+1);
          *(syn_Y_Rownxt+(fill*2))=*(syn_Y_Rownxt+(xLast*2)+1);
          *(syn_Y_Rownxt+(fill*2)+1)=*(syn_Y_Rownxt+(xLast*2)+1);


          *(syn_U+fill)=*(syn_U+xLast);
          *(syn_V+fill)=*(syn_V+xLast);
          *(dmap+fill) =*(dmap+xLast);
        }
      }
      if(pDepth[0] == 0)
      {
        *(syn_Y+0) =*(syn_Y+1*2);
        *(syn_Y+0+1) =*(syn_Y+1*2);
        *(syn_Y_Rownxt+0) =*(syn_Y_Rownxt+1*2);
        *(syn_Y_Rownxt+0+1) =*(syn_Y_Rownxt+1*2);

        *(syn_U+0) =*(syn_U+1);
        *(syn_V+0) =*(syn_V+1);
        *(dmap +0) =*(dmap +1);
      }
      //flip
#if NTT_SUBPEL_VSP_M24796
      for(x=0; x< WidthUV; x++)
#else
      for(x=0; x< Width2*SubPelOption/2; x++)
#endif
      {
        imgpel temp;
#if NTT_SUBPEL_VSP_M24796
        int pos = WidthLuma_minus1-x;
#else
        int pos = Width2*SubPelOption-1-x;
#endif

        temp = *(syn_Y+x);
        *(syn_Y+x) = *(syn_Y+ pos);
        *(syn_Y+ pos) = temp;

        temp = *(syn_Y_Rownxt+x);
        *(syn_Y_Rownxt+x) = *(syn_Y_Rownxt+ pos);
        *(syn_Y_Rownxt+ pos) = temp;
      }
#if NTT_SUBPEL_VSP_M24796
      for (x=0; x< WidthUV/2; x++)
#else
      for (x=0; x< Width2*SubPelOption/4; x++)
#endif
      {
        imgpel temp = *(syn_U+x);
#if NTT_SUBPEL_VSP_M24796
        int pos = WidthUV_minus1-x;
#else
        int pos = Width2 * SubPelOption/2-1-x;
#endif

        *(syn_U+x) = *(syn_U+ pos);
        *(syn_U+ pos) = temp;

        temp = *(syn_V+x);
        *(syn_V+x) = *(syn_V+ pos);
        *(syn_V+ pos) = temp;
        
        temp = *(dmap+x);
        *(dmap+x) = *(dmap+ pos);
        *(dmap+ pos) = temp;
      }
    }//end y loop
    if(pDepth)
      free(pDepth);
  }//end RIGHT_VIEW


#if NTT_SUBPEL_VSP_M24796
  if(SynDirection==2)
#endif
  {
    int y;
    for(y = 0; y < Height/2; ++y)
    {
      imgpel* syn_hlf_Y=SynY[HLFLVIEW][2*y];
      imgpel* syn_hlf_Y_Rownxt=SynY[HLFLVIEW][2*y+1];


      imgpel* syn_hlf_U=SynU[HLFLVIEW][y];
      imgpel* syn_hlf_V=SynV[HLFLVIEW][y];
      imgpel* syn_hlf_D=Dmap[HLFLVIEW][y];

      imgpel* syn_merge_Y=SynY[ViewId][2*y];
      imgpel* syn_merge_Y_Rownxt=SynY[ViewId][2*y+1];

      imgpel* syn_merge_U=SynU[ViewId][y];
      imgpel* syn_merge_V=SynV[ViewId][y];
      imgpel* syn_merge_D=Dmap[ViewId][y];

#if NTT_SUBPEL_VSP_M24796
      memcpy(syn_hlf_Y,       syn_merge_Y,        Width*sizeof(imgpel));
      memcpy(syn_hlf_Y_Rownxt,syn_merge_Y_Rownxt, Width*sizeof(imgpel));

      memcpy(syn_hlf_U, syn_merge_U, WidthUV*sizeof(imgpel));
      memcpy(syn_hlf_V, syn_merge_V, WidthUV*sizeof(imgpel));
      memcpy(syn_hlf_D, syn_merge_D, WidthUV*sizeof(imgpel));
#else
      memcpy(syn_hlf_Y,syn_merge_Y,Width2*SubPelOption*sizeof(imgpel));
      memcpy(syn_hlf_Y_Rownxt,syn_merge_Y_Rownxt,Width2*SubPelOption*sizeof(imgpel));

      memcpy(syn_hlf_U,syn_merge_U,Width2 * SubPelOption/2*sizeof(imgpel));
      memcpy(syn_hlf_V,syn_merge_V,Width2 * SubPelOption/2*sizeof(imgpel));
      memcpy(syn_hlf_D,syn_merge_D,Width2 * SubPelOption/2*sizeof(imgpel));
#endif
    }
  }
}

#endif // NTT_VSP_HARMONIZATION_M25195

#endif
#endif


#if (ZJU_VSP && !BOUNDARY_DETECT_ATM02)
void CViewInterpolation1D::ViewGen_1LWH_splatting(int ViewId)
{
#if NTT_SUBPEL_VSP_M24796
  int count_num         = 16;      //how many pixels using splatting when an edge is detected
  int edge_detect_width = 4;       //edge detection step
#else
  int count_num         = 16 * UpsampleRefs;      //how many pixels using splatting when an edge is detected
  int edge_detect_width = 4 * UpsampleRefs;       //edge detection step
#endif
  int depth_edge_th     = (int)(4.0/fabs(DisparityLookupTable[ViewId][0].db_disparity-DisparityLookupTable[ViewId][1].db_disparity)); //edge detection threshold, corresponding to 4-pixel disparity difference
  int i;
  int count = 0;
  int count_e = 0;
#if NTT_SUBPEL_VSP_M24796
  int width_warped = Width; //width of the warped image (in the target view), related to UpsampleRefs and SubPelOption
#else
  int width_warped = Width2 * SubPelOption; //width of the warped image (in the target view), related to UpsampleRefs and SubPelOption
  int Width2_minus1 = Width2 - 1;           //width of the original image (in the original view) - 1
#endif
  int width_warped_minus1 = width_warped-1;
  int width_warped_half   = width_warped>>1;//half width of the warped image
  int x2_s, x2_floor, x2_ceil;
  int endpoint = 0;
  int x_flip = 0;
  int warping_valid_flag = 0;
#if NTT_SUBPEL_VSP_M24796
  int mem_id = SynDirection==2 ? ViewId : FINLVIEW;
#endif


#if VSP_OPTIMIZATION
  if(SynDirection==2)
  {
    for (i = 0; i < 3; i++)
    {
#if NTT_SUBPEL_VSP_M24796
      memset(SynY[i][0], 0,         Width*Height*sizeof(imgpel));
      memset(SynU[i][0], 0,         Width*Height*sizeof(imgpel));
      memset(SynV[i][0], 0,         Width*Height*sizeof(imgpel));
      memset(Dmap[i][0], 0,         Width*Height*sizeof(byte));
#else
      memset(SynY[i][0], 0,         Width2*Height*SubPelOption*sizeof(imgpel));
      memset(SynU[i][0], 0,         Width2*Height*SubPelOption*sizeof(imgpel));
      memset(SynV[i][0], 0,         Width2*Height*SubPelOption*sizeof(imgpel));
      memset(Dmap[i][0], 0,         Width2*Height*SubPelOption*sizeof(byte));
#endif
    }
  }
  else if(SynDirection==1)
  {
#if NTT_SUBPEL_VSP_M24796
    memset(SynU[FINLVIEW][0], 0,      Width*Height*sizeof(imgpel));
    memset(SynV[FINLVIEW][0], 0,      Width*Height*sizeof(imgpel));
#else
    memset(SynY[1][0], 0,      Width2*Height*SubPelOption*sizeof(imgpel));
    memset(SynU[1][0], 0,      Width2*Height*SubPelOption*sizeof(imgpel));
    memset(SynV[1][0], 0,      Width2*Height*SubPelOption*sizeof(imgpel));
    memset(Dmap[1][0], 0,      Width2*Height*SubPelOption*sizeof(byte));
#endif
  }
  else if(SynDirection==0)
  {
#if NTT_SUBPEL_VSP_M24796
    memset(SynU[FINLVIEW][0], 0,      Width*Height*sizeof(imgpel));
    memset(SynV[FINLVIEW][0], 0,      Width*Height*sizeof(imgpel));
#else
    memset(SynY[0][0], 0,         Width2*Height*SubPelOption*sizeof(imgpel));
    memset(SynU[0][0], 0,         Width2*Height*SubPelOption*sizeof(imgpel));
    memset(SynV[0][0], 0,         Width2*Height*SubPelOption*sizeof(imgpel));
    memset(Dmap[0][0], 0,         Width2*Height*SubPelOption*sizeof(byte));
#endif
  }
#endif
  if(ViewId == LEFTVIEW)
    WarpToRight = 1;
  else
    WarpToRight = 0;

  //start to warp each pixel
  if( WarpToRight == 0) // right view-- from right 2 left
  {
    int x, y, xLast, idx2=0, x2;
    imgpel* ref_Y=NULL;
    imgpel* ref_U=NULL;
    imgpel* ref_V=NULL;
    imgpel* ref_d=NULL;
    imgpel* dmap=NULL;
    imgpel* syn_Y=NULL;
    imgpel* syn_U=NULL;
    imgpel* syn_V=NULL;
    //imgpel* pDepth = (imgpel*)calloc(Width2*SubPelOption, sizeof(imgpel)); //same 
    imgpel* pDepth = NULL; //rev
    double  disp;
#if NTT_SUBPEL_VSP_M24796
    int  disp_integer;
    int  disp_fractional;
    int  p0, p1, p2, p3, p4, p5;
    int  x_far, x_near;
    imgpel half_pel[3];
#endif
    imgpel  crData;

    for (y = 0; y < Height; ++y)
    {
      ref_Y=RefView[ViewId][0][y];
      ref_U=RefView[ViewId][1][y];
      ref_V=RefView[ViewId][2][y];
      ref_d=RefDepth[ViewId][y];
#if NTT_SUBPEL_VSP_M24796
      syn_Y=SynY[mem_id][y];
      syn_U=SynU[mem_id][y];
      syn_V=SynV[mem_id][y];
      dmap =Dmap[mem_id][y];
      memset(syn_Y, 0, Width*sizeof(imgpel));
#else
      dmap=Dmap[ViewId][y];
      syn_Y=SynY[ViewId][y];
      syn_U=SynU[ViewId][y];
      syn_V=SynV[ViewId][y];
#endif
      pDepth = dmap;  //rev
      memset(pDepth, 0, width_warped*sizeof(imgpel)); //maybe set to "-1", as pixels may have 0 depth value; could be a bug
      xLast = 0;
      count = 0;
      count_e = edge_detect_width;

#if NTT_SUBPEL_VSP_M24796
      for (x = 0; x < Width;  ++x)
#else
      for (x = 0; x < Width2;  ++x)
#endif
      {
        //get disparity
        crData = *(ref_d+x);  //depth of current pixel being warped
#if NTT_SUBPEL_VSP_M24796
#if VSP_OPTIMIZATION
        disp_integer    = DisparityLookupTable[ViewId][crData].integer;
        disp_fractional = DisparityLookupTable[ViewId][crData].fractional;
#else
        {
          double z;
#if POZNAN_NONLINEAR_DEPTH
          int d = nonlinear_depth_lut_backward[crData];
#else
          int d = crData;
#endif
          double db_disparity, db_disparity_subpel;

          z = 1.0/( (d/255.0) * (1/Znear[ViewId] - 1/Zfar[ViewId]) + (1/Zfar[ViewId]) );
          
          db_disparity        = (FocalLength*LTranslation[ViewId]/z)-duPrincipal[ViewId];
          db_disparity_subpel = db_disparity*SubPelOption;

          disp_integer    = (int)db_disparity;
          disp_fractional = (int)(fabs(db_disparity_subpel)+0.5)-abs(disp_integer)*SubPelOption;
          if(disp_fractional==SubPelOption) {
            disp_fractional = 0;
            disp_integer += (db_disparity<0?-1:1);
          }
          if(SubPelOption==2) disp_fractional *= 2;
        }
#endif
#else // NTT_SUBPEL_VSP_M24796
#if VSP_OPTIMIZATION
        if(UpsampleRefs==2)
          disp=DisparityLookupTable[ViewId][crData].db_disparity_mul2;
        else if(UpsampleRefs==4)
          disp=DisparityLookupTable[ViewId][crData].db_disparity_mul4;
        else  
          disp=DisparityLookupTable[ViewId][crData].db_disparity;
#endif
#endif // NTT_SUBPEL_VSP_M24796

        /* ------------------- boundary detection, start ---------------*/
        if(count_e > 0)
          count_e--;
        else
        {
          count_e = edge_detect_width; //reset edge detection count; horizontal boundary detection performed every N pixels
          endpoint = x+edge_detect_width;
#if NTT_SUBPEL_VSP_M24796
          if(endpoint>= Width)
            endpoint = width_warped_minus1;
#else
          if(endpoint>= Width2)
            endpoint = Width2_minus1;
#endif
          //if( (ref_d[x]-ref_d[endpoint]) >= depth_edge_th ||  (ref_d[endpoint] - ref_d[x]) >= depth_edge_th ) 
          if ( abs(ref_d[x]-ref_d[endpoint]) >= depth_edge_th)
            count = count_num;      // reset splatting count, enabling splatting for next K pixels
        }
        /* ------------------- boundary detection, end ------------------*/

        /* --------- splatting/warping, start --------------*/
        warping_valid_flag = 0;
        if(count==0) //splatting if count>0;
        {
#if NTT_SUBPEL_VSP_M24796
          x2 = x - disp_integer;
#else
          x2 = (int)((x - disp) * SubPelOption + 0.5);
#endif
          x2_s = x2;
          if(x2 < width_warped) //since x2 >=0 always valid in this warping direction
          {
            if(pDepth[x2] <= crData) //to handle the cases: depth value = 0;
            {
              idx2 = x2;
#if NTT_SUBPEL_VSP_M24796
              if(disp_fractional)
              {
                if(disp_fractional<0)
                  INTERPOLATE_RIGHT_SIDE(x, width_warped_minus1)
                else
                  INTERPOLATE_LEFT_SIDE(x, width_warped_minus1)

                half_pel[0]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y),0,255);
#if !(NOKIA_CHROMA_BILINEAR)
                half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_U),0,255);
                half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_V),0,255);
#endif

                if(disp_fractional==1)
                {
                  *(syn_Y+idx2) = ((*(ref_Y+x_near)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR1(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR1(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = ((*(ref_U+x_near)+half_pel[1]+1)>>1);
                  *(syn_V+idx2) = ((*(ref_V+x_near)+half_pel[2]+1)>>1);
#endif
                }
                else if(disp_fractional==2)
                {
                  *(syn_Y+idx2) = half_pel[0];
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR2(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR2(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = half_pel[1];
                  *(syn_V+idx2) = half_pel[2];
#endif
                }
                else
                {
                  *(syn_Y+idx2) = ((*(ref_Y+x_far)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR3(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR3(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = ((*(ref_U+x_far)+half_pel[1]+1)>>1);
                  *(syn_V+idx2) = ((*(ref_V+x_far)+half_pel[2]+1)>>1);
#endif
                }
              }
              else
#endif
              {
                *(syn_Y+idx2)=*(ref_Y+x);
                *(syn_U+idx2)=*(ref_U+x);
                *(syn_V+idx2)=*(ref_V+x);
              }
              *(dmap+idx2) =*(ref_d+x);
              warping_valid_flag = 1;
            }
          }

        }
        else
        {
#if NTT_SUBPEL_VSP_M24796
          x2_floor = x - disp_integer;
#else
          x2_floor = (int)((x - disp) * SubPelOption);
#endif
          x2_ceil  = x2_floor + 1;

          x2 = x2_floor;
          x2_s = x2_ceil;

          if(x2_ceil < width_warped) //since x2 >=0 always valid in this warping direction
          {
            if(pDepth[x2_ceil] < crData) //note: adding "=" in splatting may cause slight difference
            {
              idx2 = x2_ceil;
#if NTT_SUBPEL_VSP_M24796
              if(disp_fractional)
              {
                if(disp_fractional>0)
                  INTERPOLATE_RIGHT_SIDE(x, width_warped_minus1)
                else
                  INTERPOLATE_LEFT_SIDE(x, width_warped_minus1)

                half_pel[0]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y),0,255);
#if !(NOKIA_CHROMA_BILINEAR)
                half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_U),0,255);
                half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_V),0,255);
#endif

                if(disp_fractional==1)
                {
                  *(syn_Y+idx2) = ((*(ref_Y+x_far)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR3(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR3(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = ((*(ref_U+x_far)+half_pel[1]+1)>>1);
                  *(syn_V+idx2) = ((*(ref_V+x_far)+half_pel[2]+1)>>1);
#endif
                }
                else if(disp_fractional==2)
                {
                  *(syn_Y+idx2) = half_pel[0];
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR2(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR2(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = half_pel[1];
                  *(syn_V+idx2) = half_pel[2];
#endif
                }
                else
                {
                  *(syn_Y+idx2) = ((*(ref_Y+x_near)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR1(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR1(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = ((*(ref_U+x_near)+half_pel[1]+1)>>1);
                  *(syn_V+idx2) = ((*(ref_V+x_near)+half_pel[2]+1)>>1);
#endif
                }
              }
              else
              {
                int x_neighbor = CLIP(x+1, 0, width_warped_minus1);
                *(syn_Y+idx2)=*(ref_Y+x_neighbor);
                *(syn_U+idx2)=*(ref_U+x_neighbor);
                *(syn_V+idx2)=*(ref_V+x_neighbor);
              }
#else
              *(syn_Y+idx2)=*(ref_Y+x);
              *(syn_U+idx2)=*(ref_U+x);
              *(syn_V+idx2)=*(ref_V+x);
#endif
              *(dmap+idx2) =*(ref_d+x);
              warping_valid_flag = 1;
            }
          }

          if(x2_floor < width_warped) //since x2 >=0 always valid in this warping direction
          {
            if(pDepth[x2_floor] < crData)
            {
              idx2 = x2_floor;
#if NTT_SUBPEL_VSP_M24796
              if(disp_fractional)
              {
                if(disp_fractional<0)
                  INTERPOLATE_RIGHT_SIDE(x, width_warped_minus1)
                else
                  INTERPOLATE_LEFT_SIDE(x, width_warped_minus1)

                half_pel[0]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y),0,255);
#if !(NOKIA_CHROMA_BILINEAR)
                half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_U),0,255);
                half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_V),0,255);
#endif

                if(disp_fractional==1)
                {
                  *(syn_Y+idx2) = ((*(ref_Y+x_near)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR1(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR1(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = ((*(ref_U+x_near)+half_pel[1]+1)>>1);
                  *(syn_V+idx2) = ((*(ref_V+x_near)+half_pel[2]+1)>>1);
#endif
                }
                else if(disp_fractional==2)
                {
                  *(syn_Y+idx2) = half_pel[0];
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR2(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR2(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = half_pel[1];
                  *(syn_V+idx2) = half_pel[2];
#endif
                }
                else
                {
                  *(syn_Y+idx2) = ((*(ref_Y+x_far)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR3(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR3(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = ((*(ref_U+x_far)+half_pel[1]+1)>>1);
                  *(syn_V+idx2) = ((*(ref_V+x_far)+half_pel[2]+1)>>1);
#endif
                }
              }
              else
#endif
              {
                *(syn_Y+idx2)=*(ref_Y+x);
                *(syn_U+idx2)=*(ref_U+x);
                *(syn_V+idx2)=*(ref_V+x);
              }
              *(dmap+idx2) =*(ref_d+x);
              warping_valid_flag = 1;
            }
          }
          count--;
        }
        /* --------- splatting/warping, end --------------*/

        if(warping_valid_flag) //filling the holes between two valid pixel projections
        {

          if(xLast >= x2)
          {
            int cnt, use, fill;
            if(xLast > x2) 
              memset((void*)&pDepth[idx2+1], 0, (xLast-x2));
            if(x2!=0 && pDepth[x2-1] == 0)
            {
              for(cnt = 0; cnt < x2; cnt++)
              {
                if(pDepth[x2-1-cnt]!= 0 ) 
                  break;
              }
              if(cnt == x2)
                use = x2;
              else if( pDepth[x2-cnt-1]>pDepth[x2])
                use = x2;
              else
                use = x2-1-cnt;
              for(fill = (x2-cnt); fill < x2; fill++)
              {
                *(syn_Y+fill) =*(syn_Y+use);
                *(syn_U+fill) =*(syn_U+use);
                *(syn_V+fill) =*(syn_V+use);
                *(dmap +fill) =*(dmap +use);
              }
            }
          }//this code for the case (if(xLast > x2)) may never be used actually - Yin
          else
          { //xLast < x2
            int fill;
            if( !(x2!=0 && xLast== (x2-1)) )
            {
              if(xLast == 0)
              {
                for(fill = xLast; fill < x2; fill++)
                {
                  *(syn_Y+fill)=*(syn_Y+x2);
                  *(syn_U+fill)=*(syn_U+x2);
                  *(syn_V+fill)=*(syn_V+x2);
                  *(dmap+fill) =*(dmap +x2);
                }
              }else
              {
                if(pDepth[xLast] > pDepth[x2])
                {
                  for(fill = (xLast+1); fill < x2; fill++)
                  {
                    *(syn_Y+fill)=*(syn_Y+x2);
                    *(syn_U+fill)=*(syn_U+x2);
                    *(syn_V+fill)=*(syn_V+x2);
                    *(dmap+fill) =*(dmap+x2);
                  }
                }else
                {
                  for(fill = (xLast+1); fill < x2; fill++)
                  {
                    *(syn_Y+fill)=*(syn_Y+xLast);
                    *(syn_U+fill)=*(syn_U+xLast);
                    *(syn_V+fill)=*(syn_V+xLast);
                    *(dmap+fill) =*(dmap+xLast);
                  }
                }
              }
            }
          }//end x2>=imgWidth
          xLast = x2_s; //rev
        }
      }//end x loop
      if(xLast != width_warped)
      {
        int fill;
        for(fill = xLast+1; fill < width_warped; fill++)
        {
          *(syn_Y+fill)=*(syn_Y+xLast);
          *(syn_U+fill)=*(syn_U+xLast);
          *(syn_V+fill)=*(syn_V+xLast);
          *(dmap+fill) =*(dmap+xLast);
        }
      }
      if(pDepth[0] == 0)
      {
        *(syn_Y+0) =*(syn_Y+1);
        *(syn_U+0) =*(syn_U+1);
        *(syn_V+0) =*(syn_V+1);
        *(dmap +0) =*(dmap +1);
      }
    }//end y loop
  }
  else  //if( WarpToRight == 1) 
  {
    int x, y, xLast, idx2=0, x2;
    imgpel* ref_Y=NULL;
    imgpel* ref_U=NULL;
    imgpel* ref_V=NULL;
    imgpel* ref_d=NULL;
    imgpel* dmap=NULL;
    imgpel* syn_Y=NULL;
    imgpel* syn_U=NULL;
    imgpel* syn_V=NULL;
    imgpel* pDepth = NULL; //rev
    double  disp;
#if NTT_SUBPEL_VSP_M24796
    int  disp_integer;
    int  disp_fractional;
    int  p0, p1, p2, p3, p4, p5;
    int  x_far, x_near;
    imgpel half_pel[3];
#endif
    imgpel  crData;

    for (y = 0; y < Height; ++y)
    {
      ref_Y=RefView[ViewId][0][y];
      ref_U=RefView[ViewId][1][y];
      ref_V=RefView[ViewId][2][y];
      ref_d=RefDepth[ViewId][y];
#if NTT_SUBPEL_VSP_M24796
      syn_Y=SynY[mem_id][y];
      syn_U=SynU[mem_id][y];
      syn_V=SynV[mem_id][y];
      dmap =Dmap[mem_id][y];
      memset(syn_Y, 0, Width2*sizeof(imgpel));
#else
      pDepth = dmap; //rev
      syn_Y=SynY[ViewId][y];
      syn_U=SynU[ViewId][y];
      syn_V=SynV[ViewId][y];
#endif
      dmap=Dmap[ViewId][y];
      memset(pDepth, 0, width_warped*sizeof(imgpel));
      xLast = 0;
      count = 0;
      count_e = edge_detect_width;

#if NTT_SUBPEL_VSP_M24796
      for(x = 0; x < Width;  ++x)
#else
      for(x = 0; x < Width2;  ++x)
#endif
      {
        //get disparity
#if NTT_SUBPEL_VSP_M24796
        x_flip = width_warped_minus1-x; //rev
#else
        x_flip = Width2_minus1-x; //rev
#endif
        crData = *(ref_d+ x_flip);

#if NTT_SUBPEL_VSP_M24796
#if VSP_OPTIMIZATION
        disp_integer    = DisparityLookupTable[ViewId][crData].integer;
        disp_fractional = DisparityLookupTable[ViewId][crData].fractional;
#else
        {
          double z;
#if POZNAN_NONLINEAR_DEPTH
          int d = nonlinear_depth_lut_backward[crData];
#else
          int d = crData;
#endif
          double db_disparity, db_disparity_subpel;

          z = 1.0/( (d/255.0) * (1/Znear[ViewId] - 1/Zfar[ViewId]) + (1/Zfar[ViewId]) );
          
          db_disparity        = (FocalLength*LTranslation[ViewId]/z)-duPrincipal[ViewId];
          db_disparity_subpel = db_disparity*SubPelOption;

          disp_integer    = (int)db_disparity;
          disp_fractional = (int)(fabs(db_disparity_subpel)+0.5)-abs(disp_integer)*SubPelOption;
          if(disp_fractional==SubPelOption) {
            disp_fractional = 0;
            disp_integer += (db_disparity<0?-1:1);
          }
          if(SubPelOption==2) disp_fractional *= 2;
        }
#endif // (VSP_OPTIMIZATION)
#else // NTT_SUBPEL_VSP_M24796
        if(UpsampleRefs==2)
          disp= DisparityLookupTable[ViewId][crData].db_disparity_mul2;
        else if(UpsampleRefs==4)
          disp= DisparityLookupTable[ViewId][crData].db_disparity_mul4;
        else  
          disp= DisparityLookupTable[ViewId][crData].db_disparity;
#endif // NTT_SUBPEL_VSP_M24796

        /* --------------- boundary detection, start ---------------*/
        if(count_e > 0)
          count_e--;
        else
        {
          count_e = edge_detect_width;
          endpoint = x_flip-edge_detect_width;
          if(endpoint<0)
            endpoint = 0;
          //if( (ref_d[x_flip]-ref_d[endpoint])>=depth_edge_th || (ref_d[endpoint] - ref_d[x_flip])>= depth_edge_th) 
          if ( abs(ref_d[x_flip]-ref_d[endpoint]) >= depth_edge_th )
            count = count_num; // reset count
        }
        /* ---------------- boundary detection, end -----------------*/

        /* --------- splatting/warping, start --------------*/
        warping_valid_flag = 0;
        if(count==0) //splatting if count>0; count is reset to count_num for large depth discontinuity
        {
#if NTT_SUBPEL_VSP_M24796
          x2 = x + disp_integer;
#else
          x2 = (int)((x + disp) * SubPelOption + 0.5);
#endif
          x2_s = x2;
          if(x2 < width_warped) //since x2 >=0 always valid in this warping direction
          {
            if(pDepth[x2] <= crData)
            {
              idx2 = x2;
#if NTT_SUBPEL_VSP_M24796
              if(disp_fractional)
              {
                if(disp_fractional<0)
                  INTERPOLATE_LEFT_SIDE(x_flip, width_warped_minus1)
                else
                  INTERPOLATE_RIGHT_SIDE(x_flip, width_warped_minus1)

                half_pel[0]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y),0,255);
#if !(NOKIA_CHROMA_BILINEAR)
                half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_U),0,255);
                half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_V),0,255);
#endif

                if(disp_fractional==1)
                {
                  *(syn_Y+idx2) = ((*(ref_Y+x_near)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR1(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR1(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = ((*(ref_U+x_near)+half_pel[1]+1)>>1);
                  *(syn_V+idx2) = ((*(ref_V+x_near)+half_pel[2]+1)>>1);
#endif
                }
                else if(disp_fractional==2)
                {
                  *(syn_Y+idx2) = half_pel[0];
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR2(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR2(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = half_pel[1];
                  *(syn_V+idx2) = half_pel[2];
#endif
                }
                else
                {
                  *(syn_Y+idx2) = ((*(ref_Y+x_far)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR3(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR3(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = ((*(ref_U+x_far)+half_pel[1]+1)>>1);
                  *(syn_V+idx2) = ((*(ref_V+x_far)+half_pel[2]+1)>>1);
#endif
                }
              }
              else
#endif
              {
                *(syn_Y+idx2)=*(ref_Y+x_flip); //flipping meanwhile
                *(syn_U+idx2)=*(ref_U+x_flip);
                *(syn_V+idx2)=*(ref_V+x_flip);
              }
              *(dmap+idx2) =*(ref_d+x_flip);
              warping_valid_flag = 1;
            }
          }

        }
        else
        {
#if NTT_SUBPEL_VSP_M24796
          x2_floor = x + disp_integer;
#else
          x2_floor = (int)((x + disp) * SubPelOption);
#endif
          x2_ceil  = x2_floor + 1;

          x2 = x2_floor;
          x2_s = x2_ceil;

          if(x2_ceil < width_warped)
          {
            if(pDepth[x2_ceil] < crData)
            {
              idx2 = x2_ceil;
#if NTT_SUBPEL_VSP_M24796
              if(disp_fractional)
              {
                if(disp_fractional>0)
                  INTERPOLATE_LEFT_SIDE(x_flip, width_warped_minus1)
                else
                  INTERPOLATE_RIGHT_SIDE(x_flip, width_warped_minus1)

                half_pel[0]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y),0,255);
#if !(NOKIA_CHROMA_BILINEAR)
                half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_U),0,255);
                half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_V),0,255);
#endif

                if(disp_fractional==1)
                {
                  *(syn_Y+idx2) = ((*(ref_Y+x_far)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR3(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR3(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = ((*(ref_U+x_far)+half_pel[1]+1)>>1);
                  *(syn_V+idx2) = ((*(ref_V+x_far)+half_pel[2]+1)>>1);
#endif
                }
                else if(disp_fractional==2)
                {
                  *(syn_Y+idx2) = half_pel[0];
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR2(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR2(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = half_pel[1];
                  *(syn_V+idx2) = half_pel[2];
#endif
                }
                else
                {
                  *(syn_Y+idx2) = ((*(ref_Y+x_near)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR1(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR1(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = ((*(ref_U+x_near)+half_pel[1]+1)>>1);
                  *(syn_V+idx2) = ((*(ref_V+x_near)+half_pel[2]+1)>>1);
#endif
                }
              }
              else
              {
                int x_neighbor = CLIP(x_flip-1, 0, width_warped_minus1);
                *(syn_Y+idx2)=*(ref_Y+x_neighbor);
                *(syn_U+idx2)=*(ref_U+x_neighbor);
                *(syn_V+idx2)=*(ref_V+x_neighbor);
              }
#else
              *(syn_Y+idx2)=*(ref_Y+x_flip);
              *(syn_U+idx2)=*(ref_U+x_flip);
              *(syn_V+idx2)=*(ref_V+x_flip);
#endif
              *(dmap+idx2) =*(ref_d+x_flip);
              warping_valid_flag = 1;
            }
          }

          if(x2_floor < width_warped)
          {
            if(pDepth[x2_floor] < crData)
            {
              idx2 = x2_floor;
#if NTT_SUBPEL_VSP_M24796
              if(disp_fractional)
              {
                if(disp_fractional<0)
                  INTERPOLATE_LEFT_SIDE(x_flip, width_warped_minus1)
                else
                  INTERPOLATE_RIGHT_SIDE(x_flip, width_warped_minus1)

                half_pel[0]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y),0,255);
#if !(NOKIA_CHROMA_BILINEAR)
                half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_U),0,255);
                half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_V),0,255);
#endif

                if(disp_fractional==1)
                {
                  *(syn_Y+idx2) = ((*(ref_Y+x_near)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR1(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR1(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = ((*(ref_U+x_near)+half_pel[1]+1)>>1);
                  *(syn_V+idx2) = ((*(ref_V+x_near)+half_pel[2]+1)>>1);
#endif
                }
                else if(disp_fractional==2)
                {
                  *(syn_Y+idx2) = half_pel[0];
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR2(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR2(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = half_pel[1];
                  *(syn_V+idx2) = half_pel[2];
#endif
                }
                else
                {
                  *(syn_Y+idx2) = ((*(ref_Y+x_far)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
                  *(syn_U+idx2) = BILINEAR3(ref_U, x_near, x_far);
                  *(syn_V+idx2) = BILINEAR3(ref_V, x_near, x_far);
#else
                  *(syn_U+idx2) = ((*(ref_U+x_far)+half_pel[1]+1)>>1);
                  *(syn_V+idx2) = ((*(ref_V+x_far)+half_pel[2]+1)>>1);
#endif
                }
              }
              else
#endif
              {
                *(syn_Y+idx2)=*(ref_Y+x_flip);
                *(syn_U+idx2)=*(ref_U+x_flip);
                *(syn_V+idx2)=*(ref_V+x_flip);
              }
              *(dmap+idx2) =*(ref_d+x_flip);
              warping_valid_flag = 1;
            }
          }
          count--;
        }
        /* --------- splatting/warping, start --------------*/

        if(warping_valid_flag)
        {
          if(xLast >= x2)
          {
            int cnt, use, fill;
            if(xLast > x2) 
              memset((void*)&pDepth[idx2+1], 0, (xLast-x2));
            if(x2!=0 && pDepth[x2-1] == 0)
            {
              for(cnt = 0; cnt < x2; cnt++)
              {
                if(pDepth[x2-1-cnt]!= 0) 
                  break;
              }
              if(cnt == x2)
                use = x2;
              else if( pDepth[x2-cnt-1]>pDepth[x2])
                use = x2;
              else
                use = x2-1-cnt;
              for(fill = (x2-cnt); fill < x2; fill++)
              {
                *(syn_Y+fill) =*(syn_Y+use);
                *(syn_U+fill) =*(syn_U+use);
                *(syn_V+fill) =*(syn_V+use);
                *(dmap +fill) =*(dmap +use);
              }
            }
          } 
          else
          { //xLast < x2
            int fill;
            if( !(x2!=0 && xLast== (x2-1)) )
            {
              if(xLast == 0)
              {
                for(fill = xLast; fill < x2; fill++)
                {
                  *(syn_Y+fill)=*(syn_Y+x2);
                  *(syn_U+fill)=*(syn_U+x2);
                  *(syn_V+fill)=*(syn_V+x2);
                  *(dmap+fill) =*(dmap +x2);
                }
              }else
              {
                if(pDepth[xLast] > pDepth[x2])
                {
                  for(fill = (xLast+1); fill < x2; fill++)
                  {
                    *(syn_Y+fill)=*(syn_Y+x2);
                    *(syn_U+fill)=*(syn_U+x2);
                    *(syn_V+fill)=*(syn_V+x2);
                    *(dmap+fill) =*(dmap+x2);
                  }
                }else
                {
                  for(fill = (xLast+1); fill < x2; fill++)
                  {
                    *(syn_Y+fill)=*(syn_Y+xLast);
                    *(syn_U+fill)=*(syn_U+xLast);
                    *(syn_V+fill)=*(syn_V+xLast);
                    *(dmap+fill) =*(dmap+xLast);
                  }
                }
              }
            }
          }//end x2>=imgWidth
          xLast = x2_s; //rev
        }

      }//end x loop
      if(xLast != width_warped)
      {
        int fill;
        for(fill = xLast+1; fill < width_warped; fill++)
        {
          *(syn_Y+fill)=*(syn_Y+xLast);
          *(syn_U+fill)=*(syn_U+xLast);
          *(syn_V+fill)=*(syn_V+xLast);
          *(dmap+fill) =*(dmap+xLast);
        }
      }
      if(pDepth[0] == 0)
      {
        *(syn_Y+0) =*(syn_Y+1);
        *(syn_U+0) =*(syn_U+1);
        *(syn_V+0) =*(syn_V+1);
        *(dmap +0) =*(dmap +1);
      }
      //flip
      for(x=0; x< width_warped_half; x++)
      {
        imgpel temp = *(syn_Y+x);
        int pos = width_warped_minus1-x;
        *(syn_Y+x) = *(syn_Y+ pos);
        *(syn_Y+ pos) = temp;

        temp = *(syn_U+x);
        *(syn_U+x) = *(syn_U+ pos);
        *(syn_U+ pos) = temp;

        temp = *(syn_V+x);
        *(syn_V+x) = *(syn_V+ pos);
        *(syn_V+ pos) = temp;

        temp = *(dmap+x);
        *(dmap+x) = *(dmap+ pos);
        *(dmap+ pos) = temp;
      }
    }//end y loop
  }//end RIGHT_VIEW



#if NTT_SUBPEL_VSP_M24796
  if(SynDirection==2)
#endif
  {
    int y;
    for(y = 0; y < Height; ++y)
    {
      imgpel* syn_hlf_Y=SynY[HLFLVIEW][y];
      imgpel* syn_hlf_U=SynU[HLFLVIEW][y];
      imgpel* syn_hlf_V=SynV[HLFLVIEW][y];
      imgpel* syn_hlf_D=Dmap[HLFLVIEW][y];
      imgpel* syn_merge_Y=SynY[ViewId][y];
      imgpel* syn_merge_U=SynU[ViewId][y];
      imgpel* syn_merge_V=SynV[ViewId][y];
      imgpel* syn_merge_D=Dmap[ViewId][y];

      memcpy(syn_hlf_Y,syn_merge_Y,width_warped*sizeof(imgpel));
      memcpy(syn_hlf_U,syn_merge_U,width_warped*sizeof(imgpel));
      memcpy(syn_hlf_V,syn_merge_V,width_warped*sizeof(imgpel));
      memcpy(syn_hlf_D,syn_merge_D,width_warped*sizeof(imgpel));
    }
  }
}
#endif

#if QCOM_VSP
void CViewInterpolation1D::ViewGen_1LWH(int ViewId)
{
  int i;
#if NTT_SUBPEL_VSP_M24796
  int width_warped_minus1 = Width - 1;
  int mem_id = SynDirection==2 ? ViewId : FINLVIEW;
#endif

#if VSP_OPTIMIZATION
  if(SynDirection==2)
  {
    for (i = 0; i < 3; i++)
    {
#if NTT_SUBPEL_VSP_M24796
      memset(SynY[i][0], 0,         Width*Height*sizeof(imgpel));
      memset(SynU[i][0], 0,         Width*Height*sizeof(imgpel));
      memset(SynV[i][0], 0,         Width*Height*sizeof(imgpel));
      memset(Dmap[i][0], 0,         Width*Height*sizeof(byte));
#else
      memset(SynY[i][0], 0,         Width2*Height*SubPelOption*sizeof(imgpel));
      memset(SynU[i][0], 0,         Width2*Height*SubPelOption*sizeof(imgpel));
      memset(SynV[i][0], 0,         Width2*Height*SubPelOption*sizeof(imgpel));
      memset(Dmap[i][0], 0,         Width2*Height*SubPelOption*sizeof(byte));
#endif
    }
  }
  else if(SynDirection==1)
  {
#if NTT_SUBPEL_VSP_M24796
    memset(SynU[FINLVIEW][0], 0,      Width*Height*sizeof(imgpel));
    memset(SynV[FINLVIEW][0], 0,      Width*Height*sizeof(imgpel));
#else
    memset(SynY[1][0], 0,      Width2*Height*SubPelOption*sizeof(imgpel));
    memset(SynU[1][0], 0,      Width2*Height*SubPelOption*sizeof(imgpel));
    memset(SynV[1][0], 0,      Width2*Height*SubPelOption*sizeof(imgpel));
    memset(Dmap[1][0], 0,      Width2*Height*SubPelOption*sizeof(byte));
#endif
  }
  else if(SynDirection==0)
  {
#if NTT_SUBPEL_VSP_M24796
    memset(SynU[FINLVIEW][0], 0,      Width*Height*sizeof(imgpel));
    memset(SynV[FINLVIEW][0], 0,      Width*Height*sizeof(imgpel));
#else
    memset(SynY[0][0], 0,      Width2*Height*SubPelOption*sizeof(imgpel));
    memset(SynU[0][0], 0,      Width2*Height*SubPelOption*sizeof(imgpel));
    memset(SynV[0][0], 0,      Width2*Height*SubPelOption*sizeof(imgpel));
    memset(Dmap[0][0], 0,      Width2*Height*SubPelOption*sizeof(byte));
#endif
  }
#endif
  if(ViewId == LEFTVIEW)
    WarpToRight = 1;
  else
    WarpToRight = 0;

  //start to warp each pixel
  if( WarpToRight == 0) // right view-- from right 2 left
  {
    int x, y, xLast, idx2, x2;
    imgpel* ref_Y=NULL;
    imgpel* ref_U=NULL;
    imgpel* ref_V=NULL;
    imgpel* ref_d=NULL;
    imgpel* dmap=NULL;
    //imgpel* mask=NULL;
    imgpel* syn_Y=NULL;
    imgpel* syn_U=NULL;
    imgpel* syn_V=NULL;
#if NTT_SUBPEL_VSP_M24796
    imgpel* pDepth = (imgpel*)calloc(Width, sizeof(imgpel));
    int  disp_integer;
    int  disp_fractional;
    int  p0, p1, p2, p3, p4, p5;
    int  x_far, x_near;
    imgpel half_pel[3];
#else
    imgpel* pDepth = (imgpel*)calloc(Width2*SubPelOption, sizeof(imgpel)); //same 
    //imgpel* pDepth = (imgpel*)malloc(Width2*SubPelOption*sizeof(imgpel)); //same 
    double  disp; // shoulde be revised to int?
#endif
    imgpel  crData;
    for (y = 0; y < Height; ++y)
    {
      ref_Y=RefView[ViewId][0][y];
      ref_U=RefView[ViewId][1][y];
      ref_V=RefView[ViewId][2][y];
      ref_d=RefDepth[ViewId][y];
      //mask=Mask[ViewId][y];
#if NTT_SUBPEL_VSP_M24796
      syn_Y=SynY[mem_id][y];
      syn_U=SynU[mem_id][y];
      syn_V=SynV[mem_id][y];
      dmap =Dmap[mem_id][y];
      memset(syn_Y, 0, Width*sizeof(imgpel));
      memset(pDepth, 0, Width*sizeof(imgpel));
#else
      dmap=Dmap[ViewId][y];
      syn_Y=SynY[ViewId][y];
      syn_U=SynU[ViewId][y];
      syn_V=SynV[ViewId][y];
      memset(pDepth, 0, Width2*SubPelOption*sizeof(imgpel));
#endif

      xLast = 0;
#if NTT_SUBPEL_VSP_M24796
      for (x = 0; x < Width;  ++x)
#else
      for (x = 0; x < Width2;  ++x)
#endif
      {
        //get disparity
#if NTT_SUBPEL_VSP_M24796
#if VSP_OPTIMIZATION
        disp_integer    = DisparityLookupTable[ViewId][*(ref_d+x)].integer;
        disp_fractional = DisparityLookupTable[ViewId][*(ref_d+x)].fractional;
#else
        {
          double z;
#if POZNAN_NONLINEAR_DEPTH
          int d = nonlinear_depth_lut_backward[*(ref_d+x)];
#else
          int d = *(ref_d+x);
#endif
          double db_disparity, db_disparity_subpel;

          z = 1.0/( (d/255.0) * (1/Znear[ViewId] - 1/Zfar[ViewId]) + (1/Zfar[ViewId]) );
          
          db_disparity        = (FocalLength*LTranslation[ViewId]/z)-duPrincipal[ViewId];
          db_disparity_subpel = db_disparity*SubPelOption;

          disp_integer    = (int)db_disparity;
          disp_fractional = (int)(fabs(db_disparity_subpel)+0.5)-abs(disp_integer)*SubPelOption;
          if(disp_fractional==SubPelOption) {
            disp_fractional = 0;
            disp_integer += (db_disparity<0?-1:1);
          }
          if(SubPelOption==2) disp_fractional *= 2;
        }
#endif
        x2 = x - disp_integer;
#else // NTT_SUBPEL_VSP_M24796
#if VSP_OPTIMIZATION
        if(UpsampleRefs==2)
          disp=DisparityLookupTable[ViewId][*(ref_d+x)].db_disparity_mul2;
        else if(UpsampleRefs==4)
          disp=DisparityLookupTable[ViewId][*(ref_d+x)].db_disparity_mul4;
        else  
          disp=DisparityLookupTable[ViewId][*(ref_d+x)].db_disparity;
#endif
        x2 = (int)((x - disp) * SubPelOption + 0.5);
#endif // NTT_SUBPEL_VSP_M24796

        crData = *(ref_d+x);

#if NTT_SUBPEL_VSP_M24796
        if(x2 < Width && x2 >=0)
#else
        if(x2 < Width2*SubPelOption && x2 >=0)
#endif
        {
//          if( x2 <0)
//            printf("x2 error %2d", x2);
          if(pDepth[x2] < crData)
          {
#if NTT_SUBPEL_VSP_M24796
            idx2 = x2;
#else
            idx2 = (int)((x - disp) * SubPelOption + 0.5);
#endif
#if NTT_SUBPEL_VSP_M24796
            if(disp_fractional)
            {
              if(disp_fractional<0)
                INTERPOLATE_RIGHT_SIDE(x, width_warped_minus1)
              else
                INTERPOLATE_LEFT_SIDE(x, width_warped_minus1)

              half_pel[0]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y),0,255);
#if !(NOKIA_CHROMA_BILINEAR)
              half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_U),0,255);
              half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_V),0,255);
#endif

              if(disp_fractional==1)
              {
                *(syn_Y+idx2) = ((*(ref_Y+x_near)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+idx2) = BILINEAR1(ref_U, x_near, x_far);
                *(syn_V+idx2) = BILINEAR1(ref_V, x_near, x_far);
#else
                *(syn_U+idx2) = ((*(ref_U+x_near)+half_pel[1]+1)>>1);
                *(syn_V+idx2) = ((*(ref_V+x_near)+half_pel[2]+1)>>1);
#endif
              }
              else if(disp_fractional==2)
              {
                *(syn_Y+idx2) = half_pel[0];
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+idx2) = BILINEAR2(ref_U, x_near, x_far);
                *(syn_V+idx2) = BILINEAR2(ref_V, x_near, x_far);
#else
                *(syn_U+idx2) = half_pel[1];
                *(syn_V+idx2) = half_pel[2];
#endif
              }
              else
              {
                *(syn_Y+idx2) = ((*(ref_Y+x_far)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+idx2) = BILINEAR3(ref_U, x_near, x_far);
                *(syn_V+idx2) = BILINEAR3(ref_V, x_near, x_far);
#else
                *(syn_U+idx2) = ((*(ref_U+x_far)+half_pel[1]+1)>>1);
                *(syn_V+idx2) = ((*(ref_V+x_far)+half_pel[2]+1)>>1);
#endif
              }
            }
            else
#endif
            {
              *(syn_Y+idx2)=*(ref_Y+x);
              *(syn_U+idx2)=*(ref_U+x);
              *(syn_V+idx2)=*(ref_V+x);
            }
            *(dmap+idx2) =*(ref_d+x);
            pDepth[x2] = crData;

            if(xLast >= x2)
            {
              int cnt, use, fill;
              if(xLast > x2) 
                memset((void*)&pDepth[idx2+1], 0, (xLast-x2));
              if(x2!=0 && pDepth[x2-1] == 0)
              {
                for(cnt = 0; cnt < x2; cnt++)
                {
                  if(pDepth[x2-1-cnt]!= 0 ) 
                    break;
                }
                if(cnt == x2)
                  use = x2;
                else if( pDepth[x2-cnt-1]>pDepth[x2])
                  use = x2;
                else
                  use = x2-1-cnt;
                for(fill = (x2-cnt); fill < x2; fill++)
                {
                  *(syn_Y+fill) =*(syn_Y+use);
                  *(syn_U+fill) =*(syn_U+use);
                  *(syn_V+fill) =*(syn_V+use);
                  *(dmap +fill) =*(dmap +use);
                }
              }
            } 
            else
            { //xLast < x2
              int fill;
              if( !(x2!=0 && xLast== (x2-1)) )
              {
                if(xLast == 0)
                {
                  for(fill = xLast; fill < x2; fill++)
                  {
                    *(syn_Y+fill)=*(syn_Y+x2);
                    *(syn_U+fill)=*(syn_U+x2);
                    *(syn_V+fill)=*(syn_V+x2);
                    *(dmap+fill) =*(dmap +x2);
                  }
                }
                else
                {
                  if(pDepth[xLast] > pDepth[x2])
                  {
                    for(fill = (xLast+1); fill < x2; fill++)
                    {
                      *(syn_Y+fill)=*(syn_Y+x2);
                      *(syn_U+fill)=*(syn_U+x2);
                      *(syn_V+fill)=*(syn_V+x2);
                      *(dmap+fill) =*(dmap+x2);
                    }
                  }
                  else
                  {
                    for(fill = (xLast+1); fill < x2; fill++)
                    {
                      *(syn_Y+fill)=*(syn_Y+xLast);
                      *(syn_U+fill)=*(syn_U+xLast);
                      *(syn_V+fill)=*(syn_V+xLast);
                      *(dmap+fill) =*(dmap+xLast);
                    }
                  }
                }
              }
            }//end x2>=imgWidth
            xLast = x2;

          }//end pDepth<crData

        }
      }//end x loop

#if NTT_SUBPEL_VSP_M24796
      if(xLast != Width)
#else
      if(xLast != Width2*SubPelOption)
#endif
      {
        int fill;
#if NTT_SUBPEL_VSP_M24796
        for(fill = xLast+1; fill < Width; fill++)
#else
        for(fill = xLast+1; fill < Width2*SubPelOption; fill++)
#endif
        {
          *(syn_Y+fill)=*(syn_Y+xLast);
          *(syn_U+fill)=*(syn_U+xLast);
          *(syn_V+fill)=*(syn_V+xLast);
          *(dmap+fill) =*(dmap+xLast);
        }
      }
      if(pDepth[0] == 0)
      {
        *(syn_Y+0) =*(syn_Y+1);
        *(syn_U+0) =*(syn_U+1);
        *(syn_V+0) =*(syn_V+1);
        *(dmap +0) =*(dmap +1);
      }
    }//end y loop
    if(pDepth)
      free(pDepth);

  }else
  {
    int x, y, xLast, idx2, x2;
    imgpel* ref_Y=NULL;
    imgpel* ref_U=NULL;
    imgpel* ref_V=NULL;
    imgpel* ref_d=NULL;
    imgpel* dmap=NULL;
    imgpel* syn_Y=NULL;
    imgpel* syn_U=NULL;
    imgpel* syn_V=NULL;
#if NTT_SUBPEL_VSP_M24796
    imgpel* pDepth = (imgpel*)calloc(Width, sizeof(imgpel));
    int  disp_integer;
    int  disp_fractional;
    int  p0, p1, p2, p3, p4, p5;
    int  x_far, x_near;
    int  x_flip;
    imgpel half_pel[3];
#else
    imgpel* pDepth = (imgpel*)calloc(Width2*SubPelOption, sizeof(imgpel)); //same 
    double  disp; // shoulde be revised to int?
#endif
    imgpel  crData;

    for (y = 0; y < Height; ++y)
    {
      ref_Y=RefView[ViewId][0][y];
      ref_U=RefView[ViewId][1][y];
      ref_V=RefView[ViewId][2][y];
      ref_d=RefDepth[ViewId][y];
#if NTT_SUBPEL_VSP_M24796
      syn_Y=SynY[mem_id][y];
      syn_U=SynU[mem_id][y];
      syn_V=SynV[mem_id][y];
      dmap =Dmap[mem_id][y];
      memset(syn_Y, 0, Width*sizeof(imgpel));
      memset(pDepth, 0, Width*sizeof(imgpel));
#else
      dmap=Dmap[ViewId][y];
      syn_Y=SynY[ViewId][y];
      syn_U=SynU[ViewId][y];
      syn_V=SynV[ViewId][y];
      memset(pDepth, 0, Width2*SubPelOption*sizeof(imgpel));
#endif

      xLast = 0;
#if NTT_SUBPEL_VSP_M24796
      for(x = 0; x < Width;  ++x)
#else
      for(x = 0; x < Width2;  ++x)
#endif
      {
        //get disparity
#if NTT_SUBPEL_VSP_M24796
        x_flip = Width-1-x;
        crData = *(ref_d+x_flip);
#if VSP_OPTIMIZATION
        disp_integer    = DisparityLookupTable[ViewId][crData].integer;
        disp_fractional = DisparityLookupTable[ViewId][crData].fractional;
#else
        {
          double z;
#if POZNAN_NONLINEAR_DEPTH
          int d = nonlinear_depth_lut_backward[crData];
#else
          int d = crData;
#endif
          double db_disparity, db_disparity_subpel;

          z = 1.0/( (d/255.0) * (1/Znear[ViewId] - 1/Zfar[ViewId]) + (1/Zfar[ViewId]) );
          
          db_disparity        = (FocalLength*LTranslation[ViewId]/z)-duPrincipal[ViewId];
          db_disparity_subpel = db_disparity*SubPelOption;

          disp_integer    = (int)db_disparity;
          disp_fractional = (int)(fabs(db_disparity_subpel)+0.5)-abs(disp_integer)*SubPelOption;
          if(disp_fractional==SubPelOption) {
            disp_fractional = 0;
            disp_integer += (db_disparity<0?-1:1);
          }
          if(SubPelOption==2) disp_fractional *= 2;
        }
#endif
        x2 = x + disp_integer;
#else // NTT_SUBPEL_VSP_M24796
#if VSP_OPTIMIZATION
        if(UpsampleRefs==2)
          disp= 0 - DisparityLookupTable[ViewId][*(ref_d+(Width2-1-x))].db_disparity_mul2;
        else if(UpsampleRefs==4)
          disp= 0 - DisparityLookupTable[ViewId][*(ref_d+(Width2-1-x))].db_disparity_mul4;
        else  
          disp= 0 - DisparityLookupTable[ViewId][*(ref_d+(Width2-1-x))].db_disparity;
#endif
        x2 = (int)((x - disp) * SubPelOption + 0.5);
        crData = *(ref_d+(Width2-1-x));
#endif // NTT_SUBPEL_VSP_M24796

#if NTT_SUBPEL_VSP_M24796
        if(x2 < Width && x2 >= 0)
#else
        if(x2 < Width2*SubPelOption && x2 >= 0)
#endif
        {
//          if( x2 <0)
//            printf("x2 error %2d", x2);
          if(pDepth[x2] < crData)
          {
            idx2 = x2;
#if NTT_SUBPEL_VSP_M24796
            if(disp_fractional)
            {
              if(disp_fractional<0)
                INTERPOLATE_LEFT_SIDE(x_flip, width_warped_minus1)
              else
                INTERPOLATE_RIGHT_SIDE(x_flip, width_warped_minus1)

              half_pel[0]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y),0,255);
#if !(NOKIA_CHROMA_BILINEAR)
              half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_U),0,255);
              half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_V),0,255);
#endif

              if(disp_fractional==1)
              {
                *(syn_Y+idx2) = ((*(ref_Y+x_near)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+idx2) = BILINEAR1(ref_U, x_near, x_far);
                *(syn_V+idx2) = BILINEAR1(ref_V, x_near, x_far);
#else
                *(syn_U+idx2) = ((*(ref_U+x_near)+half_pel[1]+1)>>1);
                *(syn_V+idx2) = ((*(ref_V+x_near)+half_pel[2]+1)>>1);
#endif
              }
              else if(disp_fractional==2)
              {
                *(syn_Y+idx2) = half_pel[0];
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+idx2) = BILINEAR2(ref_U, x_near, x_far);
                *(syn_V+idx2) = BILINEAR2(ref_V, x_near, x_far);
#else
                *(syn_U+idx2) = half_pel[1];
                *(syn_V+idx2) = half_pel[2];
#endif
              }
              else
              {
                *(syn_Y+idx2) = ((*(ref_Y+x_far)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+idx2) = BILINEAR3(ref_U, x_near, x_far);
                *(syn_V+idx2) = BILINEAR3(ref_V, x_near, x_far);
#else
                *(syn_U+idx2) = ((*(ref_U+x_far)+half_pel[1]+1)>>1);
                *(syn_V+idx2) = ((*(ref_V+x_far)+half_pel[2]+1)>>1);
#endif
              }
            }
            else
            {
              *(syn_Y+idx2)=*(ref_Y+(Width-1-x));
              *(syn_U+idx2)=*(ref_U+(Width-1-x));
              *(syn_V+idx2)=*(ref_V+(Width-1-x));
            }
            *(dmap+idx2) =*(ref_d+(Width-1-x));
            pDepth[x2] = crData;
#else
            *(syn_Y+idx2)=*(ref_Y+(Width2-1-x));
            *(syn_U+idx2)=*(ref_U+(Width2-1-x));
            *(syn_V+idx2)=*(ref_V+(Width2-1-x));
#endif
            *(dmap+idx2) =*(ref_d+(Width2-1-x));
            pDepth[x2] = crData;

            if(xLast >= x2)
            {
              int cnt, use, fill;
              if(xLast > x2) 
                memset((void*)&pDepth[idx2+1], 0, (xLast-x2));
              if(x2!=0 && pDepth[x2-1] == 0)
              {
                for(cnt = 0; cnt < x2; cnt++)
                {
                  if(pDepth[x2-1-cnt]!= 0) 
                    break;
                }
                if(cnt == x2)
                  use = x2;
                else if( pDepth[x2-cnt-1]>pDepth[x2])
                  use = x2;
                else
                  use = x2-1-cnt;
                for(fill = (x2-cnt); fill < x2; fill++)
                {
                  *(syn_Y+fill) =*(syn_Y+use);
                  *(syn_U+fill) =*(syn_U+use);
                  *(syn_V+fill) =*(syn_V+use);
                  *(dmap +fill) =*(dmap +use);
                }
              }
            } 
            else
            { //xLast < x2
              int fill;
              if( !(x2!=0 && xLast== (x2-1)) )
              {
                if(xLast == 0)
                {
                  for(fill = xLast; fill < x2; fill++)
                  {
                    *(syn_Y+fill)=*(syn_Y+x2);
                    *(syn_U+fill)=*(syn_U+x2);
                    *(syn_V+fill)=*(syn_V+x2);
                    *(dmap+fill) =*(dmap +x2);
                  }
                }else
                {
                  if(pDepth[xLast] > pDepth[x2])
                  {
                    for(fill = (xLast+1); fill < x2; fill++)
                    {
                      *(syn_Y+fill)=*(syn_Y+x2);
                      *(syn_U+fill)=*(syn_U+x2);
                      *(syn_V+fill)=*(syn_V+x2);
                      *(dmap+fill) =*(dmap+x2);
                    }
                  }else
                  {
                    for(fill = (xLast+1); fill < x2; fill++)
                    {
                      *(syn_Y+fill)=*(syn_Y+xLast);
                      *(syn_U+fill)=*(syn_U+xLast);
                      *(syn_V+fill)=*(syn_V+xLast);
                      *(dmap+fill) =*(dmap+xLast);
                    }
                  }
                }
              }
            }//end x2>=imgWidth
            xLast = x2;

          }//end pDepth<crData

        }
      }//end x loop

#if NTT_SUBPEL_VSP_M24796
      if(xLast != Width)
#else
      if(xLast != Width2*SubPelOption)
#endif
      {
        int fill;
#if NTT_SUBPEL_VSP_M24796
        for(fill = xLast+1; fill < Width; fill++)
#else
        for(fill = xLast+1; fill < Width2*SubPelOption; fill++)
#endif
        {
          *(syn_Y+fill)=*(syn_Y+xLast);
          *(syn_U+fill)=*(syn_U+xLast);
          *(syn_V+fill)=*(syn_V+xLast);
          *(dmap+fill) =*(dmap+xLast);
        }
      }
      if(pDepth[0] == 0)
      {
        *(syn_Y+0) =*(syn_Y+1);
        *(syn_U+0) =*(syn_U+1);
        *(syn_V+0) =*(syn_V+1);
        *(dmap +0) =*(dmap +1);
      }
      //flip
#if NTT_SUBPEL_VSP_M24796
      for(x=0; x< Width/2; x++)
      {
        imgpel temp = *(syn_Y+x);
        *(syn_Y+x) = *(syn_Y+ (Width-1-x));
        *(syn_Y+ (Width-1-x)) = temp;

        temp = *(syn_U+x);
        *(syn_U+x) = *(syn_U+ (Width-1-x));
        *(syn_U+ (Width-1-x)) = temp;

        temp = *(syn_V+x);
        *(syn_V+x) = *(syn_V+ (Width-1-x));
        *(syn_V+ (Width-1-x)) = temp;
        
        temp = *(dmap+x);
        *(dmap+x) = *(dmap+ (Width-1-x));
        *(dmap+ (Width-1-x)) = temp;
      }
#else
      for(x=0; x< Width2*SubPelOption/2; x++)
      {
        imgpel temp = *(syn_Y+x);
        *(syn_Y+x) = *(syn_Y+ (Width2*SubPelOption-1-x));
        *(syn_Y+ (Width2*SubPelOption-1-x)) = temp;

        temp = *(syn_U+x);
        *(syn_U+x) = *(syn_U+ (Width2*SubPelOption-1-x));
        *(syn_U+ (Width2*SubPelOption-1-x)) = temp;

        temp = *(syn_V+x);
        *(syn_V+x) = *(syn_V+ (Width2*SubPelOption-1-x));
        *(syn_V+ (Width2*SubPelOption-1-x)) = temp;
        
        temp = *(dmap+x);
        *(dmap+x) = *(dmap+ (Width2*SubPelOption-1-x));
        *(dmap+ (Width2*SubPelOption-1-x)) = temp;
      }
#endif
    }//end y loop
    if(pDepth)
      free(pDepth);
  }//end RIGHT_VIEW


#if NTT_SUBPEL_VSP_M24796
  if(SynDirection==2)
#endif
  {
    int y;
    for(y = 0; y < Height; ++y)
    {
      imgpel* syn_hlf_Y=SynY[HLFLVIEW][y];
      imgpel* syn_hlf_U=SynU[HLFLVIEW][y];
      imgpel* syn_hlf_V=SynV[HLFLVIEW][y];
      imgpel* syn_hlf_D=Dmap[HLFLVIEW][y];
      imgpel* syn_merge_Y=SynY[ViewId][y];
      imgpel* syn_merge_U=SynU[ViewId][y];
      imgpel* syn_merge_V=SynV[ViewId][y];
      imgpel* syn_merge_D=Dmap[ViewId][y];

#if NTT_SUBPEL_VSP_M24796
      memcpy(syn_hlf_Y,syn_merge_Y,Width*sizeof(imgpel));
      memcpy(syn_hlf_U,syn_merge_U,Width*sizeof(imgpel));
      memcpy(syn_hlf_V,syn_merge_V,Width*sizeof(imgpel));
      memcpy(syn_hlf_D,syn_merge_D,Width*sizeof(imgpel));
#else
      memcpy(syn_hlf_Y,syn_merge_Y,Width2*SubPelOption*sizeof(imgpel));
      memcpy(syn_hlf_U,syn_merge_U,Width2*SubPelOption*sizeof(imgpel));
      memcpy(syn_hlf_V,syn_merge_V,Width2*SubPelOption*sizeof(imgpel));
      memcpy(syn_hlf_D,syn_merge_D,Width2*SubPelOption*sizeof(imgpel));
#endif
    }
  }
}
#else
/*
* \brief
*    Map a pixel to the virtual view using the z buffer
*
* \input
* \param x, y
*    The image cooredinate of the pixel to be mapped
* \param d
*    The depth level of the pixel to be mapped
* \param z
*    The depth value of the pixel to be mapped
* \param RefY
*    The reference view buffer, Y. Note that UV components are stored in RefU[ViewId] and RefV[ViewId]
* \param ViewId
*    0 means left view; 1 means right view
* \param dk
*    The disparity of the pixel to be mapped
* \param flooring
*    Indicate if floor() or ceil() to be used in the mapping process
*
* \output
* \class member SynY[ViewId], SynU[ViewId], SynV[ViewId]
*    The warped picture is stored. 4:4:4
*
* \return
*    None
*
* \Modification history:
*    \Zhejiang Univ, May 2009
*        Bug fix on pixel mapping
*        Must work together with new version ForwardWarpSingleView() function as it need a correct warping direction.
*/
/*void CViewInterpolation1D::PixelMapping(int x, int y, unsigned char d, float z, 
                    imgpel** RefY, imgpel** RefU, imgpel** RefV,
                    int ViewId, double dk, int flooring,int first_map)*/
void CViewInterpolation1D::PixelMapping(int x, int y, unsigned char d, float z, 
                    imgpel** RefY, imgpel** RefU, imgpel** RefV,
                    int ViewId, double dk, int flooring)
{
  int   x2=0, y2=0;
  bool  nearToCamera=false;
  bool  bSplatting=false;
  int   left_pixel, right_pixel, left2_pixel, right2_pixel;
#if NTT_SUBPEL_VSP_M24796
  int   line_size = Width;
  double  db_disparity_subpel;
  int  disp_integer;
  int  disp_fractional;
  int mem_id = SynDirection==2 ? ViewId : FINLVIEW;
  int  p0, p1, p2, p3, p4, p5;
  int  x_far, x_near;
  imgpel half_pel[3];
#else
  int   line_size = Width2*SubPelOption;
#endif
  int   enable_warp_flag=2;
  int   depth_diff_th = 10;
  int   average_depth = 0;

  imgpel* mask=NULL;
  imgpel* dmap=NULL;
  imgpel* syn_Y=NULL;
  imgpel* syn_U=NULL;
  imgpel* syn_V=NULL;
  imgpel* ref_Y=NULL;
  imgpel* ref_U=NULL;
  imgpel* ref_V=NULL;

  // ind = (x,y) is the pixel position in ref view

  // Do splatting?  
  if (SplattingOption == 0)
    bSplatting = false;
  else if (SplattingOption == 1)
    bSplatting = true;
  else if (SplattingOption == 2)
  {
    // check if the ref pixel is boundary, revised by Yin (SplattingDepthThreshold: a sequence adaptive parameter)
    if (d < SplattingDepthThreshold || IsBoundary(BoundaryMask[ViewId], x, y) || x < 30 || x > Width2-30 )
      bSplatting = true;
    else
      bSplatting = false;
  }

  // Calc the position in syn view
#if NTT_SUBPEL_VSP_M24796
  db_disparity_subpel = dk*SubPelOption;
  disp_integer    = (int)dk;
  disp_fractional = (int)(fabs(db_disparity_subpel)+0.5)-abs(disp_integer)*SubPelOption;
  if(disp_fractional==SubPelOption)
  {
    disp_fractional = 0;
    disp_integer += (dk<0?-1:1);
  }
  if(SubPelOption==2) disp_fractional *= 2;
  if (bSplatting)
  {
    if (flooring)
      x2 = x - disp_integer;
    else
      x2 = x - disp_integer + 1;
  }
  else
    x2 = x - disp_integer;
#else
  if (bSplatting)
  {
    if (flooring)
      x2 = (int)((x - dk) * SubPelOption);
    else
      x2 = (int)((x - dk) * SubPelOption+1);
  }
  else
    x2 = (int)((x - dk) * SubPelOption + 0.5);
#endif

  y2 = y;

  if ( x2 < line_size && x2 >= 0 && y2<Height && y2>=0    ) // (x2,y2) must fall within the picture
  {

    mask=Mask[ViewId][y2];
#if NTT_SUBPEL_VSP_M24796
    syn_Y=SynY[mem_id][y2];
    syn_U=SynU[mem_id][y2];
    syn_V=SynV[mem_id][y2];
    dmap =Dmap[mem_id][y2];
#else
    dmap=Dmap[ViewId][y2];
    syn_Y=SynY[ViewId][y2];
    syn_U=SynU[ViewId][y2];
    syn_V=SynV[ViewId][y2];
#endif
    ref_Y=RefY[y];
    ref_U=RefU[y];
    ref_V=RefV[y];

    nearToCamera = false;
    if (Mask[ViewId][y2][x2]==NOTHOLEPIXEL)
    {

      if (Dmap[ViewId][y2][x2] < d)  //if depth value of current pixel is larger, it covers the previous result
        nearToCamera = true;
    }

    //pixel mapping
    //for HOLEPIXEL, revised to clean noise background pixels
    if( *(mask+x2)== HOLEPIXEL )// (x2,y2) is not filled with any value yet, should notice it is not a background pixel
    {
      //constrain
      left_pixel = CLIP(x2-1, 0, line_size-1);
      left2_pixel= CLIP(x2-2, 0, line_size-1);
      right_pixel= CLIP(x2+1, 0, line_size-1);
      right2_pixel=CLIP(x2+2, 0, line_size-1);

      //filter wrong background pixels caused by per-pixel processing
      //do not warp the pixel, because it is a suspicious pixel in the gap of foreground object
      if( *(mask+left_pixel)== NOTHOLEPIXEL && *(mask+right_pixel)== NOTHOLEPIXEL && WarpEnhancementOption == 1)
      {
        average_depth = (Dmap[ViewId][y2][left_pixel]+Dmap[ViewId][y2][right_pixel])>>1;
        depth_diff_th = (int)((average_depth-Minz)*0.05+0.5);
        if( abs( Dmap[ViewId][y2][left_pixel]-Dmap[ViewId][y2][right_pixel]) < depth_diff_th && abs(d-average_depth)> depth_diff_th )        
        {
          enable_warp_flag = 0;
          *(syn_Y+x2)=(*(syn_Y+left_pixel)+*(syn_Y+right_pixel))>>1;
          *(syn_U+x2)=(*(syn_U+left_pixel)+*(syn_U+right_pixel))>>1;
          *(syn_V+x2)=(*(syn_V+left_pixel)+*(syn_V+right_pixel))>>1;
          *(mask+x2)=NOTHOLEPIXEL;
          *(dmap+x2)=(*(dmap+left_pixel)+*(dmap+right_pixel))>>1;
          //CountHOLEFILLPIXEL++;
        }
        else  
          enable_warp_flag = 1;
      }
      else if( *(mask+left_pixel) == NOTHOLEPIXEL && *(mask+right2_pixel)== NOTHOLEPIXEL && WarpEnhancementOption == 1)
      {
        //average_depth = (Dmap[ViewId][y2][left_pixel]+Dmap[ViewId][y2][right2_pixel])/2;
        average_depth=(*(dmap+left_pixel)+*(dmap+right2_pixel))>>1;
        depth_diff_th = (int)((average_depth-Minz)*0.08+0.5);
        if( abs( *(dmap+left_pixel)-*(dmap+right2_pixel)) < depth_diff_th && abs(d-average_depth)> (depth_diff_th>>1))  
        {
          enable_warp_flag = 0;
          *(syn_Y+x2)=(*(syn_Y+left_pixel)+*(syn_Y+right2_pixel))>>1;
          *(syn_U+x2)=(*(syn_U+left_pixel)+*(syn_U+right2_pixel))>>1;
          *(syn_V+x2)=(*(syn_V+left_pixel)+*(syn_V+right2_pixel))>>1;
          *(mask+x2)=NOTHOLEPIXEL;
          *(dmap+x2)=(*(dmap+left_pixel)+*(dmap+right2_pixel))>>1;
        }
        else  
          enable_warp_flag = 1;
      }
      else if( *(mask+left2_pixel)== NOTHOLEPIXEL && *(mask+right_pixel) == NOTHOLEPIXEL && WarpEnhancementOption == 1)
      {
        //average_depth = (Dmap[ViewId][y2][left2_pixel]+Dmap[ViewId][y2][right_pixel])>>1;
        average_depth=(*(dmap+left2_pixel)+*(dmap+right_pixel))>>1;
        depth_diff_th = (int)((average_depth-Minz)*0.08+0.5);
        if( abs( *(dmap+left2_pixel)-*(dmap+right_pixel)) < depth_diff_th && abs(d-average_depth)> (depth_diff_th>>1))  
        {
          enable_warp_flag = 0;
          *(syn_Y+x2)=(*(syn_Y+left2_pixel)+*(syn_Y+right_pixel))>>1;
          *(syn_U+x2)=(*(syn_U+left2_pixel)+*(syn_U+right_pixel))>>1;
          *(syn_V+x2)=(*(syn_V+left2_pixel)+*(syn_V+right_pixel))>>1;
          *(mask+x2)=NOTHOLEPIXEL;
          *(dmap+x2)=(*(dmap+left2_pixel)+*(dmap+right_pixel))>>1;
        }
        else  
          enable_warp_flag = 1;
      }
      else 
        enable_warp_flag = 1;

      //enable to warp current pixel if the pixel is not belong to the area around strong depth edge
      //aiming to clean boundary noise causing by slow depth change or depth edge be a little inside of texture edge
      if( enable_warp_flag == 1)
      {
        //if the pixel is at left side of a RISE depth edge or at right side of a FALL depth edge, do not warp the pixel
        if( SplattingOption != 2 && CleanNoiseOption != 1)  // bug fixed July 16th: when SplattingOption == 0 or 1 && CleanNoiseOption == 0, no boundary detection was made
        {
#if NTT_SUBPEL_VSP_M24796
          if(disp_fractional)
          {
            if(disp_fractional<0)
              INTERPOLATE_RIGHT_SIDE(x, width_warped_minus1)
            else
              INTERPOLATE_LEFT_SIDE(x, width_warped_minus1)

            half_pel[0]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y),0,255);
#if !(NOKIA_CHROMA_BILINEAR)
            half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_U),0,255);
            half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_V),0,255);
#endif

            if(disp_fractional==1)
            {
              *(syn_Y+x2) = ((*(ref_Y+x_near)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
              *(syn_U+idx2) = BILINEAR1(ref_U, x_near, x_far);
              *(syn_V+idx2) = BILINEAR1(ref_V, x_near, x_far);
#else
              *(syn_U+x2) = ((*(ref_U+x_near)+half_pel[1]+1)>>1);
              *(syn_V+x2) = ((*(ref_V+x_near)+half_pel[2]+1)>>1);
#endif
            }
            else if(disp_fractional==2)
            {
              *(syn_Y+x2) = half_pel[0];
#if NOKIA_CHROMA_BILINEAR
              *(syn_U+idx2) = BILINEAR2(ref_U, x_near, x_far);
              *(syn_V+idx2) = BILINEAR2(ref_V, x_near, x_far);
#else
              *(syn_U+x2) = half_pel[1];
              *(syn_V+x2) = half_pel[2];
#endif
            }
            else
            {
              *(syn_Y+x2) = ((*(ref_Y+x_far)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
              *(syn_U+idx2) = BILINEAR3(ref_U, x_near, x_far);
              *(syn_V+idx2) = BILINEAR3(ref_V, x_near, x_far);
#else
              *(syn_U+x2) = ((*(ref_U+x_far)+half_pel[1]+1)>>1);
              *(syn_V+x2) = ((*(ref_V+x_far)+half_pel[2]+1)>>1);
#endif
            }
          }
          else
#endif
          {
            *(syn_Y+x2)=*(ref_Y+x);
            *(syn_U+x2)=*(ref_U+x);
            *(syn_V+x2)=*(ref_V+x);
          }
          *(mask+x2)=NOTHOLEPIXEL;
          *(dmap+x2)=d;
        }
        else  //Boundary aware splatting
        {
          if( BoundaryMask[ViewId][y][x] == SUSPECT && CleanNoiseOption == 1)
            ;//do nothing
          else
          {
#if NTT_SUBPEL_VSP_M24796
            if(disp_fractional)
            {
              if(disp_fractional<0)
                INTERPOLATE_RIGHT_SIDE(x, width_warped_minus1)
              else
                INTERPOLATE_LEFT_SIDE(x, width_warped_minus1)

              half_pel[0]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y),0,255);
#if !(NOKIA_CHROMA_BILINEAR)
              half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_U),0,255);
              half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_V),0,255);
#endif

              if(disp_fractional==1)
              {
                *(syn_Y+x2) = ((*(ref_Y+x_near)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+x2) = BILINEAR1(ref_U, x_near, x_far);
                *(syn_V+x2) = BILINEAR1(ref_V, x_near, x_far);
#else
                *(syn_U+x2) = ((*(ref_U+x_near)+half_pel[1]+1)>>1);
                *(syn_V+x2) = ((*(ref_V+x_near)+half_pel[2]+1)>>1);
#endif
              }
              else if(disp_fractional==2)
              {
                *(syn_Y+x2) = half_pel[0];
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+x2) = BILINEAR2(ref_U, x_near, x_far);
                *(syn_V+x2) = BILINEAR2(ref_V, x_near, x_far);
#else
                *(syn_U+x2) = half_pel[1];
                *(syn_V+x2) = half_pel[2];
#endif
              }
              else
              {
                *(syn_Y+x2) = ((*(ref_Y+x_far)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
                *(syn_U+x2) = BILINEAR3(ref_U, x_near, x_far);
                *(syn_V+x2) = BILINEAR3(ref_V, x_near, x_far);
#else
                *(syn_U+x2) = ((*(ref_U+x_far)+half_pel[1]+1)>>1);
                *(syn_V+x2) = ((*(ref_V+x_far)+half_pel[2]+1)>>1);
#endif
              }
            }
            else
#endif
            {
              *(syn_Y+x2)=*(ref_Y+x);
              *(syn_U+x2)=*(ref_U+x);
              *(syn_V+x2)=*(ref_V+x);
            }
            *(mask+x2)=NOTHOLEPIXEL;
            *(dmap+x2)=d;
          }
        }
      }
    }//end of if( Mask[ViewId][ind2] == HOLEPIXEL )
    else if( nearToCamera) // (x2,y2) was filled, but the new pixel is near to the camera  Mask[ViewId][y2][x2]==NOTHOLEPIXEL && Zmap[ViewId][y2][x2] > z )
    {
      if( SplattingOption != 2 && CleanNoiseOption != 1)  // bug fixed July 16th: when SplattingOption == 0 or 1 && CleanNoiseOption == 0 , no boundary detection was made
      {
#if NTT_SUBPEL_VSP_M24796
        if(disp_fractional)
        {
          if(disp_fractional<0)
            INTERPOLATE_RIGHT_SIDE(x, width_warped_minus1)
          else
            INTERPOLATE_LEFT_SIDE(x, width_warped_minus1)

          half_pel[0]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y),0,255);
#if !(NOKIA_CHROMA_BILINEAR)
          half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_U),0,255);
          half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_V),0,255);
#endif

          if(disp_fractional==1)
          {
            *(syn_Y+x2) = ((*(ref_Y+x_near)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
            *(syn_U+x2) = BILINEAR1(ref_U, x_near, x_far);
            *(syn_V+x2) = BILINEAR1(ref_V, x_near, x_far);
#else
            *(syn_U+x2) = ((*(ref_U+x_near)+half_pel[1]+1)>>1);
            *(syn_V+x2) = ((*(ref_V+x_near)+half_pel[2]+1)>>1);
#endif
          }
          else if(disp_fractional==2)
          {
            *(syn_Y+x2) = half_pel[0];
#if NOKIA_CHROMA_BILINEAR
            *(syn_U+x2) = BILINEAR2(ref_U, x_near, x_far);
            *(syn_V+x2) = BILINEAR2(ref_V, x_near, x_far);
#else
            *(syn_U+x2) = half_pel[1];
            *(syn_V+x2) = half_pel[2];
#endif
          }
          else
          {
            *(syn_Y+x2) = ((*(ref_Y+x_far)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
            *(syn_U+x2) = BILINEAR3(ref_U, x_near, x_far);
            *(syn_V+x2) = BILINEAR3(ref_V, x_near, x_far);
#else
            *(syn_U+x2) = ((*(ref_U+x_far)+half_pel[1]+1)>>1);
            *(syn_V+x2) = ((*(ref_V+x_far)+half_pel[2]+1)>>1);
#endif
          }
        }
        else
#endif
        {
          *(syn_Y+x2)=*(ref_Y+x);
          *(syn_U+x2)=*(ref_U+x);
          *(syn_V+x2)=*(ref_V+x);
        }
        *(mask+x2)=NOTHOLEPIXEL;
        *(dmap+x2)=d;
      }
      else
      {
        if( BoundaryMask[ViewId][y][x] == SUSPECT && CleanNoiseOption == 1) //
          ;//do nothing 
        else
        {
#if NTT_SUBPEL_VSP_M24796
          if(disp_fractional)
          {
            if(disp_fractional<0)
              INTERPOLATE_RIGHT_SIDE(x, width_warped_minus1)
            else
              INTERPOLATE_LEFT_SIDE(x, width_warped_minus1)

            half_pel[0]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y),0,255);
#if !(NOKIA_CHROMA_BILINEAR)
            half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_U),0,255);
            half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_V),0,255);
#endif

            if(disp_fractional==1)
            {
              *(syn_Y+x2) = ((*(ref_Y+x_near)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
              *(syn_U+x2) = BILINEAR1(ref_U, x_near, x_far);
              *(syn_V+x2) = BILINEAR1(ref_V, x_near, x_far);
#else
              *(syn_U+x2) = ((*(ref_U+x_near)+half_pel[1]+1)>>1);
              *(syn_V+x2) = ((*(ref_V+x_near)+half_pel[2]+1)>>1);
#endif
            }
            else if(disp_fractional==2)
            {
              *(syn_Y+x2) = half_pel[0];
#if NOKIA_CHROMA_BILINEAR
              *(syn_U+x2) = BILINEAR2(ref_U, x_near, x_far);
              *(syn_V+x2) = BILINEAR2(ref_V, x_near, x_far);
#else
              *(syn_U+x2) = half_pel[1];
              *(syn_V+x2) = half_pel[2];
#endif
            }
            else
            {
              *(syn_Y+x2) = ((*(ref_Y+x_far)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
              *(syn_U+x2) = BILINEAR3(ref_U, x_near, x_far);
              *(syn_V+x2) = BILINEAR3(ref_V, x_near, x_far);
#else
              *(syn_U+x2) = ((*(ref_U+x_far)+half_pel[1]+1)>>1);
              *(syn_V+x2) = ((*(ref_V+x_far)+half_pel[2]+1)>>1);
#endif
            }
          }
          else
#endif
          {
            *(syn_Y+x2)=*(ref_Y+x);
            *(syn_U+x2)=*(ref_U+x);
            *(syn_V+x2)=*(ref_V+x);
          }
          *(mask+x2)=NOTHOLEPIXEL;
          *(dmap+x2)=d;
        }
      }
    }
  }//end of if ( x2<line_size && x2>=0 ...)
}

void CViewInterpolation1D::PixelMapping(int x,byte d, imgpel* mask,imgpel* bounday,
                    imgpel* ref_Y,imgpel* ref_U,imgpel* ref_V,imgpel* dmap,
                    imgpel* syn_Y,imgpel*syn_U,imgpel* syn_V, 
                    int view_id,double dk,int flooring, int first_map)
{
  int   x2=0, y2=0;
  bool  nearToCamera=false;
  bool  bSplatting=false;
  int   left_pixel, right_pixel, left2_pixel, right2_pixel;
#if NTT_SUBPEL_VSP_M24796
  int   line_size = Width;
  double  db_disparity_subpel;
  int  disp_integer;
  int  disp_fractional;
  int  p0, p1, p2, p3, p4, p5;
  int  x_far, x_near;
  imgpel half_pel[3];
#else
  int   line_size = Width2*SubPelOption;
#endif
  int   enable_warp_flag=2;
  int   depth_diff_th = 10;
  int   average_depth = 0;
  static int first_pos=0;

  // ind = (x,y) is the pixel position in ref view

  // check if the ref pixel is boundary, revised by Yin (SplattingDepthThreshold: a sequence adaptive parameter)
  if (d < SplattingDepthThreshold || (*(bounday+x)!= NONE) || x < 30 || x > Width2-30 )
    bSplatting = true;
  else
    bSplatting = false;

  // Calc the position in syn view
#if NTT_SUBPEL_VSP_M24796
  db_disparity_subpel = dk*SubPelOption;
  disp_integer    = (int)dk;
  disp_fractional = (int)(fabs(db_disparity_subpel)+0.5)-abs(disp_integer)*SubPelOption;
  if(disp_fractional==SubPelOption)
  {
    disp_fractional = 0;
    disp_integer += (dk<0?-1:1);
  }
  if(SubPelOption==2) disp_fractional *= 2;
  if (bSplatting)
  {
    if (flooring)
      x2 = x - disp_integer;
    else
      x2 = x - disp_integer + 1;
  }
  else
    x2 = x - disp_integer;
#else
  if (bSplatting)
  {
    if (flooring)
      x2 = (int)((x - dk) * SubPelOption);
    else
      x2 = (int)((x - dk) * SubPelOption+1);
  }
  else
    x2 = (int)((x - dk) * SubPelOption + 0.5);
#endif

  if(first_map)
  {
    first_pos=x2;
  }
  else if(first_pos==x2)
  {
    return ;
  }

  if ( x2 < line_size && x2 >= 0 ) // (x2,y2) must fall within the picture
  {


    nearToCamera = false;

    if ((*(mask+x2)==NOTHOLEPIXEL)&&(*(dmap+x2)< d))
    {
      nearToCamera = true;
    }

    //pixel mapping
    //for HOLEPIXEL, revised to clean noise background pixels
    if( *(mask+x2)== HOLEPIXEL )// (x2,y2) is not filled with any value yet, should notice it is not a background pixel
    {
      //constrain
      left_pixel = CLIP(x2-1, 0, line_size-1);
      left2_pixel= CLIP(x2-2, 0, line_size-1);
      right_pixel= CLIP(x2+1, 0, line_size-1);
      right2_pixel=CLIP(x2+2, 0, line_size-1);

      //filter wrong background pixels caused by per-pixel processing
      //do not warp the pixel, because it is a suspicious pixel in the gap of foreground object
      if( *(mask+left_pixel)== NOTHOLEPIXEL && *(mask+right_pixel)== NOTHOLEPIXEL)
      {
        average_depth = (*(dmap+left_pixel)+*(dmap+right_pixel))>>1;
        depth_diff_th = (int)((average_depth-Minz)*0.05+0.5);
        if( abs( *(dmap+left_pixel)-*(dmap+right_pixel)) < depth_diff_th && abs(d-average_depth)> depth_diff_th )        
        {
          enable_warp_flag = 0;
          *(syn_Y+x2)=(*(syn_Y+left_pixel)+*(syn_Y+right_pixel))>>1;
          *(syn_U+x2)=(*(syn_U+left_pixel)+*(syn_U+right_pixel))>>1;
          *(syn_V+x2)=(*(syn_V+left_pixel)+*(syn_V+right_pixel))>>1;
          *(mask+x2)=NOTHOLEPIXEL;
          *(dmap+x2)=(*(dmap+left_pixel)+*(dmap+right_pixel))>>1;
        }
        else  
          enable_warp_flag = 1;
      }
      else if( *(mask+left_pixel) == NOTHOLEPIXEL && *(mask+right2_pixel)== NOTHOLEPIXEL)
      {
        //average_depth = (Dmap[ViewId][y2][left_pixel]+Dmap[ViewId][y2][right2_pixel])/2;
        average_depth=(*(dmap+left_pixel)+*(dmap+right2_pixel))>>1;
        depth_diff_th = (int)((average_depth-Minz)*0.08+0.5);
        if( abs( *(dmap+left_pixel)-*(dmap+right2_pixel)) < depth_diff_th && abs(d-average_depth)> (depth_diff_th>>1))  
        {
          enable_warp_flag = 0;
          *(syn_Y+x2)=(*(syn_Y+left_pixel)+*(syn_Y+right2_pixel))>>1;
          *(syn_U+x2)=(*(syn_U+left_pixel)+*(syn_U+right2_pixel))>>1;
          *(syn_V+x2)=(*(syn_V+left_pixel)+*(syn_V+right2_pixel))>>1;
          *(mask+x2)=NOTHOLEPIXEL;
          *(dmap+x2)=(*(dmap+left_pixel)+*(dmap+right2_pixel))>>1;
        }
        else  
          enable_warp_flag = 1;
      }
      else if( *(mask+left2_pixel)== NOTHOLEPIXEL && *(mask+right_pixel) == NOTHOLEPIXEL)
      {
        //average_depth = (Dmap[ViewId][y2][left2_pixel]+Dmap[ViewId][y2][right_pixel])>>1;
        average_depth=(*(dmap+left2_pixel)+*(dmap+right_pixel))>>1;
        depth_diff_th = (int)((average_depth-Minz)*0.08+0.5);
        if( abs( *(dmap+left2_pixel)-*(dmap+right_pixel)) < depth_diff_th && abs(d-average_depth)> (depth_diff_th>>1))  
        {
          enable_warp_flag = 0;
          *(syn_Y+x2)=(*(syn_Y+left2_pixel)+*(syn_Y+right_pixel))>>1;
          *(syn_U+x2)=(*(syn_U+left2_pixel)+*(syn_U+right_pixel))>>1;
          *(syn_V+x2)=(*(syn_V+left2_pixel)+*(syn_V+right_pixel))>>1;
          *(mask+x2)=NOTHOLEPIXEL;
          *(dmap+x2)=(*(dmap+left2_pixel)+*(dmap+right_pixel))>>1;
        }
        else  
          enable_warp_flag = 1;
      }
      else 
        enable_warp_flag = 1;

      //enable to warp current pixel if the pixel is not belong to the area around strong depth edge
      //aiming to clean boundary noise causing by slow depth change or depth edge be a little inside of texture edge
      if( enable_warp_flag == 1)
      {
        if( *(bounday+x) == SUSPECT)
          return ;
        else
        {
#if NTT_SUBPEL_VSP_M24796
          if(disp_fractional)
          {
            if(disp_fractional<0)
              INTERPOLATE_RIGHT_SIDE(x, width_warped_minus1)
            else
              INTERPOLATE_LEFT_SIDE(x, width_warped_minus1)

            half_pel[0]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y),0,255);
#if !(NOKIA_CHROMA_BILINEAR)
            half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_U),0,255);
            half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_V),0,255);
#endif

            if(disp_fractional==1)
            {
              *(syn_Y+x2) = ((*(ref_Y+x_near)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
              *(syn_U+x2) = BILINEAR1(ref_U, x_near, x_far);
              *(syn_V+x2) = BILINEAR1(ref_V, x_near, x_far);
#else
              *(syn_U+x2) = ((*(ref_U+x_near)+half_pel[1]+1)>>1);
              *(syn_V+x2) = ((*(ref_V+x_near)+half_pel[2]+1)>>1);
#endif
            }
            else if(disp_fractional==2)
            {
              *(syn_Y+x2) = half_pel[0];
#if NOKIA_CHROMA_BILINEAR
              *(syn_U+x2) = BILINEAR2(ref_U, x_near, x_far);
              *(syn_V+x2) = BILINEAR2(ref_V, x_near, x_far);
#else
              *(syn_U+x2) = half_pel[1];
              *(syn_V+x2) = half_pel[2];
#endif
            }
            else
            {
              *(syn_Y+x2) = ((*(ref_Y+x_far)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
              *(syn_U+x2) = BILINEAR3(ref_U, x_near, x_far);
              *(syn_V+x2) = BILINEAR3(ref_V, x_near, x_far);
#else
              *(syn_U+x2) = ((*(ref_U+x_far)+half_pel[1]+1)>>1);
              *(syn_V+x2) = ((*(ref_V+x_far)+half_pel[2]+1)>>1);
#endif
            }
          }
          else
#endif
          {
            *(syn_Y+x2)=*(ref_Y+x);
            *(syn_U+x2)=*(ref_U+x);
            *(syn_V+x2)=*(ref_V+x);
          }
          *(mask+x2)=NOTHOLEPIXEL;
          *(dmap+x2)=d;
        }
      }
    }//end of if( Mask[ViewId][ind2] == HOLEPIXEL )
    else if( nearToCamera) // (x2,y2) was filled, but the new pixel is near to the camera  Mask[ViewId][y2][x2]==NOTHOLEPIXEL && Zmap[ViewId][y2][x2] > z )
    {
  
      if( *(bounday+x) == SUSPECT) //
        return ;
      else
      {
#if NTT_SUBPEL_VSP_M24796
        if(disp_fractional)
        {
          if(disp_fractional<0)
            INTERPOLATE_RIGHT_SIDE(x, width_warped_minus1)
          else
            INTERPOLATE_LEFT_SIDE(x, width_warped_minus1)

          half_pel[0]=(imgpel)CLIP(AVC_HALF_PEL(ref_Y),0,255);
#if !(NOKIA_CHROMA_BILINEAR)
          half_pel[1]=(imgpel)CLIP(AVC_HALF_PEL(ref_U),0,255);
          half_pel[2]=(imgpel)CLIP(AVC_HALF_PEL(ref_V),0,255);
#endif

          if(disp_fractional==1)
          {
            *(syn_Y+x2) = ((*(ref_Y+x_near)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
            *(syn_U+x2) = BILINEAR1(ref_U, x_near, x_far);
            *(syn_V+x2) = BILINEAR1(ref_V, x_near, x_far);
#else
            *(syn_U+x2) = ((*(ref_U+x_near)+half_pel[1]+1)>>1);
            *(syn_V+x2) = ((*(ref_V+x_near)+half_pel[2]+1)>>1);
#endif
          }
          else if(disp_fractional==2)
          {
            *(syn_Y+x2) = half_pel[0];
#if NOKIA_CHROMA_BILINEAR
            *(syn_U+x2) = BILINEAR2(ref_U, x_near, x_far);
            *(syn_V+x2) = BILINEAR2(ref_V, x_near, x_far);
#else
            *(syn_U+x2) = half_pel[1];
            *(syn_V+x2) = half_pel[2];
#endif
          }
          else
          {
            *(syn_Y+x2) = ((*(ref_Y+x_far)+half_pel[0]+1)>>1);
#if NOKIA_CHROMA_BILINEAR
            *(syn_U+x2) = BILINEAR3(ref_U, x_near, x_far);
            *(syn_V+x2) = BILINEAR3(ref_V, x_near, x_far);
#else
            *(syn_U+x2) = ((*(ref_U+x_far)+half_pel[1]+1)>>1);
            *(syn_V+x2) = ((*(ref_V+x_far)+half_pel[2]+1)>>1);
#endif
          }
        }
        else
#endif
        {
          *(syn_Y+x2)=*(ref_Y+x);
          *(syn_U+x2)=*(ref_U+x);
          *(syn_V+x2)=*(ref_V+x);
        }
        *(mask+x2)=NOTHOLEPIXEL;
        *(dmap+x2)=d;
      }
    }
  }//end of if ( x2<line_size && x2>=0)
}

/*
* \brief
*    Warp one view
*
* \input
* \param Ref
*    The reference view buffer
* \param RefDepth
*    The reference depth view buffer
* \param ViewId
*    0 means left view; 1 means right view
*
* \output
* \class member SynY[ViewId], SynU[ViewId], SynV[ViewId]
*    The warped picture is stored. 4:4:4
*
* \return
*    None
*
* \Modification history:
*    \Zhejiang Univ, May 2009
*      Warping direction consideration
*      Change the warping order: if Warp to right side, pixel mapping from left to right; otherwise, the opposite.
*/
void CViewInterpolation1D::ForwardWarpSingleView(int ViewId)
{
  int x, y;
  double dk;
  double z=0.0;
  imgpel* ref_Y=NULL;
  imgpel* ref_U=NULL;
  imgpel* ref_V=NULL;
  imgpel* ref_d=NULL;
  imgpel* dmap=NULL;
  imgpel* mask=NULL;
  imgpel* boundary=NULL;
  imgpel* syn_Y=NULL;
  imgpel* syn_U=NULL;
  imgpel* syn_V=NULL;
#if NTT_SUBPEL_VSP_M24796
  int mem_id = SynDirection==2 ? ViewId : FINLVIEW;
#endif

  //CountHOLEFILLPIXEL = 0;
  //determine warp direction

  if(ViewId == LEFTVIEW)
    WarpToRight = 1;
  else
    WarpToRight = 0;

  //start to warp each pixel
  if( WarpToRight == 0) // right view-- from right 2 left
  {
    for (y = 0; y < Height; ++y)
    {
      ref_Y=RefView[ViewId][0][y];
      ref_U=RefView[ViewId][1][y];
      ref_V=RefView[ViewId][2][y];
      ref_d=RefDepth[ViewId][y];
      mask=Mask[ViewId][y];
#if NTT_SUBPEL_VSP_M24796
      syn_Y=SynY[mem_id][y];
      syn_U=SynU[mem_id][y];
      syn_V=SynV[mem_id][y];
      dmap =Dmap[mem_id][y];
      memset(syn_Y, 0, Width*sizeof(imgpel));
#else
      dmap=Dmap[ViewId][y];
      syn_Y=SynY[ViewId][y];
      syn_U=SynU[ViewId][y];
      syn_V=SynV[ViewId][y];
#endif
      boundary=BoundaryMask[ViewId][y];
      mask=Mask[ViewId][y];

#if NTT_SUBPEL_VSP_M24796
      for (x = 0; x < Width;  ++x)
#else
      for (x = 0; x < Width2;  ++x)
#endif
      {

#if NTT_SUBPEL_VSP_M24796
        // not optimized since LUT is not utilized here with the current implementation
        {
          double z;
#if POZNAN_NONLINEAR_DEPTH
          int d = nonlinear_depth_lut_backward[RefDepth[ViewId][y][x]];
#else
          int d = RefDepth[ViewId][y][x];
#endif
          z = 1.0/( (d/255.0) * (1/Znear[ViewId] - 1/Zfar[ViewId]) + (1/Zfar[ViewId]) );
          dk = (FocalLength*LTranslation[ViewId]/z)-duPrincipal[ViewId];
        }
#if VSP_OPTIMIZATION
        PixelMapping(x,*(ref_d+x),mask,boundary,ref_Y,ref_U,ref_V,dmap,syn_Y,syn_U,syn_V,ViewId,dk,1,1);
        PixelMapping(x,*(ref_d+x),mask,boundary,ref_Y,ref_U,ref_V,dmap,syn_Y,syn_U,syn_V,ViewId,dk,0,0);
#else
        PixelMapping(x, y, RefDepth[ViewId][y][x], (float)z, RefY, RefU, RefV, ViewId, dk, 1); //09-02-25
        if (SplattingOption)
          PixelMapping(x, y, RefDepth[ViewId][y][x], (float)z, RefY, RefU, RefV, ViewId, dk, 0);
#endif // (VSP_OPTIMIZATION)

#else // NTT_SUBPEL_VSP_M24796

#if VSP_OPTIMIZATION
        if(UpsampleRefs==2)
          dk=DisparityLookupTable[ViewId][*(ref_d+x)].db_disparity_mul2;
        else if(UpsampleRefs==4)
          dk=DisparityLookupTable[ViewId][*(ref_d+x)].db_disparity_mul4;
        else
          dk=DisparityLookupTable[ViewId][*(ref_d+x)].db_disparity;
        PixelMapping(x,*(ref_d+x),mask,boundary,ref_Y,ref_U,ref_V,dmap,syn_Y,syn_U,syn_V,ViewId,dk,1,1);
        PixelMapping(x,*(ref_d+x),mask,boundary,ref_Y,ref_U,ref_V,dmap,syn_Y,syn_U,syn_V,ViewId,dk,0,0);
#else
        //Calc real depth z from depth image
        depthLevel = RefDepth[ViewId][y][x];
#if POZNAN_NONLINEAR_DEPTH
        int in = nonlinear_depth_lut_backward[depthLevel];
#else 
        int in = depthLevel;
#endif
        z = 1.0 / ( (in/255.0) * (1/Znear[ViewId] - 1/Zfar[ViewId]) + (1/Zfar[ViewId]) );

        //Cacl dk; round to integer pixel postionns; 
        dk = (FocalLength * LTranslation[ViewId] / z) - duPrincipal[ViewId];

        if (UpsampleRefs == 2)
          dk = dk + dk;
        else if (UpsampleRefs == 4)
          dk = dk * 4;

        PixelMapping(x, y, RefDepth[ViewId][y][x], (float)z, RefY, RefU, RefV, ViewId, dk, 1); //09-02-25
        if (SplattingOption)
          PixelMapping(x, y, RefDepth[ViewId][y][x], (float)z, RefY, RefU, RefV, ViewId, dk, 0);
#endif
#endif // NTT_SUBPEL_VSP_M24796
      }
    }
  }
  else  //if(WarpToRight == 1) // left view-- from left 2 right
  {
    for (y = 0; y < Height; ++y)
    {
      ref_Y=RefView[ViewId][0][y];
      ref_U=RefView[ViewId][1][y];
      ref_V=RefView[ViewId][2][y];
      ref_d=RefDepth[ViewId][y];
      mask=Mask[ViewId][y];
#if NTT_SUBPEL_VSP_M24796
      syn_Y=SynY[mem_id][y];
      syn_U=SynU[mem_id][y];
      syn_V=SynV[mem_id][y];
      dmap =Dmap[mem_id][y];
      memset(syn_Y, 0, Width*sizeof(imgpel));
#else
      dmap=Dmap[ViewId][y];
      syn_Y=SynY[ViewId][y];
      syn_U=SynU[ViewId][y];
      syn_V=SynV[ViewId][y];
#endif
      boundary=BoundaryMask[ViewId][y];
      mask=Mask[ViewId][y];

#if NTT_SUBPEL_VSP_M24796
      for (x = Width-1; x>=0; --x)
#else
      for (x = Width2-1; x>=0; --x)
#endif
      {
#if NTT_SUBPEL_VSP_M24796
        // not optimized since LUT is not utilized here with the current implementation
        {
          double z;
#if POZNAN_NONLINEAR_DEPTH
          int d = nonlinear_depth_lut_backward[RefDepth[ViewId][y][x]];
#else
          int d = RefDepth[ViewId][y][x];
#endif
          z = 1.0/( (d/255.0) * (1/Znear[ViewId] - 1/Zfar[ViewId]) + (1/Zfar[ViewId]) );
          dk = (FocalLength*LTranslation[ViewId]/z)-duPrincipal[ViewId];
        }
#if VSP_OPTIMIZATION
        PixelMapping(x,*(ref_d+x),mask,boundary,ref_Y,ref_U,ref_V,dmap,syn_Y,syn_U,syn_V,ViewId,dk,0,1);
        PixelMapping(x,*(ref_d+x),mask,boundary,ref_Y,ref_U,ref_V,dmap,syn_Y,syn_U,syn_V,ViewId,dk,1,0);
#else
        PixelMapping(x, y, RefDepth[ViewId][y][x], (float)z, RefY, RefU, RefV, ViewId, dk, 0);  //Note: here must be flooring == 0 first
        if (SplattingOption)
          PixelMapping(x, y, RefDepth[ViewId][y][x], (float)z, RefY, RefU, RefV, ViewId, dk, 1);
#endif // (VSP_OPTIMIZATION)

#else // NTT_SUBPEL_VSP_M24796

#if VSP_OPTIMIZATION
        if(UpsampleRefs==2)
          dk=DisparityLookupTable[ViewId][*(ref_d+x)].db_disparity_mul2;
        else if(UpsampleRefs==4)
          dk=DisparityLookupTable[ViewId][*(ref_d+x)].db_disparity_mul4;
        else  
          dk=DisparityLookupTable[ViewId][*(ref_d+x)].db_disparity;

        PixelMapping(x,*(ref_d+x),mask,boundary,ref_Y,ref_U,ref_V,dmap,syn_Y,syn_U,syn_V,ViewId,dk,0,1);
        PixelMapping(x,*(ref_d+x),mask,boundary,ref_Y,ref_U,ref_V,dmap,syn_Y,syn_U,syn_V,ViewId,dk,1,0);
#else
        // Calc real depth z from depth image
        depthLevel = RefDepth[ViewId][y][x];
#if POZNAN_NONLINEAR_DEPTH
        int in = nonlinear_depth_lut_backward[depthLevel];
#else
        int in = depthLevel;
#endif
        z = 1.0 / ( (in/255.0) * (1/Znear[ViewId] - 1/Zfar[ViewId]) + (1/Zfar[ViewId]) );

        // Cacl dk; round to integer pixel postionns; 
        dk = (FocalLength * LTranslation[ViewId] / z) - duPrincipal[ViewId];

        if (UpsampleRefs == 2)
          dk = dk + dk;
        else if (UpsampleRefs == 4)
          dk = dk * 4;
        PixelMapping(x, y, RefDepth[ViewId][y][x], (float)z, RefY, RefU, RefV, ViewId, dk, 0);  //Note: here must be flooring == 0 first
        if (SplattingOption)
          PixelMapping(x, y, RefDepth[ViewId][y][x], (float)z, RefY, RefU, RefV, ViewId, dk, 1);
#endif
#endif // NTT_SUBPEL_VSP_M24796
        
      }
    }
  }

}


/*
* \brief
*    Perform forward warping
*
* \input
*    The images are arranged in a one dimension array
* \param RefLeft
*    The left ref view image
* \param RefRight
*    The right ref view image
* \param RefDepthLeft
*    The left ref depth view image
* \param RefDepthRight
*    The right ref depth view image
*
* \output
* \class member: SynY[i], SynU[i], SynV[i]
*    Warped view
*    i = 0 is from left view
*    i = 1 is from right view
*  \class member: Mask[i]
*    Hole positions
*    i = 0 is from left view
*    i = 1 is from right view
*  \class member: Zmap[i]
*    The depth value of warped pixels
*    i = 0 is from left view
*    i = 1 is from right view
*  \class member: Dmap[i]
*    The depth level of warped pixels
*    i = 0 is from left view
*    i = 1 is from right view
*    
* \return
*    None
*/
void CViewInterpolation1D::ForwardWarp() // unsigned char* RefLeft, unsigned char* RefRight, unsigned char* RefDepthLeft, unsigned char* RefDepthRight
{
  int i;

#if VSP_OPTIMIZATION
  if(SynDirection==2)
  {
    for (i = 0; i < 3; i++)
    {
#if NTT_SUBPEL_VSP_M24796
      memset(SynY[i][0], 0, Width*Height*sizeof(imgpel));
      memset(SynU[i][0], 0, Width*Height*sizeof(imgpel));
      memset(SynV[i][0], 0, Width*Height*sizeof(imgpel));
      memset(Mask[i][0], HOLEPIXEL, Width*Height*sizeof(byte));
      memset(Dmap[i][0], 0, Width*Height*sizeof(byte));
#else
      memset(SynY[i][0], 0,   Width2*Height*SubPelOption*sizeof(imgpel));
      memset(SynU[i][0], 0, Width2*Height*SubPelOption*sizeof(imgpel));
      memset(SynV[i][0], 0, Width2*Height*SubPelOption*sizeof(imgpel));
      memset(Mask[i][0], HOLEPIXEL, Width2*Height*SubPelOption*sizeof(byte));
      memset(Dmap[i][0], 0,   Width2*Height*SubPelOption*sizeof(byte));
#endif
    }
  }
  else if(SynDirection==1)
  {
#if NTT_SUBPEL_VSP_M24796
    memset(SynU[1][0], 0, Width*Height*sizeof(imgpel));
    memset(SynV[1][0], 0, Width*Height*sizeof(imgpel));
    memset(Mask[1][0], HOLEPIXEL, Width*Height*sizeof(byte));
#else
    memset(SynY[1][0], 0,   Width2*Height*SubPelOption*sizeof(imgpel));
    memset(SynU[1][0], 0, Width2*Height*SubPelOption*sizeof(imgpel));
    memset(SynV[1][0], 0, Width2*Height*SubPelOption*sizeof(imgpel));
    memset(Mask[1][0], HOLEPIXEL, Width2*Height*SubPelOption*sizeof(byte));
    memset(Dmap[1][0], 0,   Width2*Height*SubPelOption*sizeof(byte));
#endif
  }
  else if(SynDirection==0)
  {
#if NTT_SUBPEL_VSP_M24796
    memset(SynU[0][0], 0, Width*Height*sizeof(imgpel));
    memset(SynV[0][0], 0, Width*Height*sizeof(imgpel));
    memset(Mask[0][0], HOLEPIXEL, Width*Height*sizeof(byte));
#else
    memset(SynY[0][0], 0,   Width2*Height*SubPelOption*sizeof(imgpel));
    memset(SynU[0][0], 0, Width2*Height*SubPelOption*sizeof(imgpel));
    memset(SynV[0][0], 0, Width2*Height*SubPelOption*sizeof(imgpel));
    memset(Mask[0][0], HOLEPIXEL, Width2*Height*SubPelOption*sizeof(byte));
    memset(Dmap[0][0], 0,   Width2*Height*SubPelOption*sizeof(byte));
#endif
  }
#else
  for (i = 0; i < 3; i++)
  {
#if NTT_SUBPEL_VSP_M24796
    memset(SynY[i][0], 0, Width*Height*sizeof(imgpel));
    memset(SynU[i][0], 0, Width*Height*sizeof(imgpel));
    memset(SynV[i][0], 0, Width*Height*sizeof(imgpel));
    memset(Mask[i][0], HOLEPIXEL, Width*Height*sizeof(byte));
    memset(Dmap[i][0], 0, Width*Height*sizeof(byte));
#else
    memset(SynY[i][0], 0,   Width2*Height*SubPelOption*sizeof(imgpel));
    memset(SynU[i][0], 0, Width2*Height*SubPelOption*sizeof(imgpel));
    memset(SynV[i][0], 0, Width2*Height*SubPelOption*sizeof(imgpel));
    memset(Mask[i][0], HOLEPIXEL, Width2*Height*SubPelOption*sizeof(byte));
    memset(Dmap[i][0], 0,   Width2*Height*SubPelOption*sizeof(byte));
#endif
  }
#endif

  //find out the max and min z value in current frame
#if VSP_OPTIMIZATION
  if(SynDirection!=1)
    FindDepthMaxMin(RefDepth[LEFTVIEW], LEFTVIEW); //left view must have a similar depth structure as that of the right view
  else
    FindDepthMaxMin(RefDepth[RGHTVIEW], RGHTVIEW);
#else
  FindDepthMaxMin(RefDepth[LEFTVIEW], LEFTVIEW);
#endif


  //depth map edge detection before forward warping if SplattingOption==2 or CleanNoiseOption==1
  if(SplattingOption == 2 || CleanNoiseOption == 1)
  {
    double es = 0.15; //EdgeStrength
    int diff_z= Maxz-Minz;
    int th=(int)(es*diff_z /UpsampleRefs+1); //depth edge threshold = 15% * z_value_range / UpsampleRefs

#if VSP_OPTIMIZATION
    if(SynDirection==2)
    {
      //!<synthesize from left and right
      for(i=0; i<2; i++)
      {
#if NTT_SUBPEL_VSP_M24796
        memset( BoundaryMask[i][0],  NONE, Width*Height*sizeof(byte));
        DetectBoundary( BoundaryMask[i],  RefDepth[i], Width, Height, th);
#else
        memset( BoundaryMask[i][0],  NONE, Width2*Height*sizeof(byte));
        DetectBoundary( BoundaryMask[i],  RefDepth[i], Width2, Height, th);
#endif
      }
    }
    else if(SynDirection==1)
    {
      //!<synthesize from right
#if NTT_SUBPEL_VSP_M24796
      memset(BoundaryMask[1][0],NONE,Width*Height*sizeof(byte));
      DetectBoundary(BoundaryMask[1],  RefDepth[1], Width, Height, th);
#else
      memset(BoundaryMask[1][0],NONE,Width2*Height*sizeof(byte));
      DetectBoundary(BoundaryMask[1],  RefDepth[1], Width2, Height, th);
#endif

    }
    else if(SynDirection==0)
    {
      //!<synthesize from left
#if NTT_SUBPEL_VSP_M24796
      memset(BoundaryMask[0][0],NONE,Width*Height*sizeof(byte));
      DetectBoundary(BoundaryMask[0],  RefDepth[0], Width, Height, th);
#else
      memset(BoundaryMask[0][0],NONE,Width2*Height*sizeof(byte));
      DetectBoundary(BoundaryMask[0],  RefDepth[0], Width2, Height, th);
#endif
    }
#else
    for(i=0; i<2; i++)
    {
#if NTT_SUBPEL_VSP_M24796
      memset( BoundaryMask[i][0],  NONE, Width*Height*sizeof(byte));
      DetectBoundary( BoundaryMask[i],  RefDepth[i], Width, Height, th);
#else
      memset( BoundaryMask[i][0],  NONE, Width2*Height*sizeof(byte));
      DetectBoundary( BoundaryMask[i],  RefDepth[i], Width2, Height, th);
#endif
    }
#endif
  }

  //forward warping left and right view
  // From Left View
#if VSP_OPTIMIZATION
  if((SynDirection==0)||(SynDirection==2))
    ForwardWarpSingleView(LEFTVIEW); // RefLeft,  RefDepthLeft,
#else
  ForwardWarpSingleView(LEFTVIEW); // RefLeft,  RefDepthLeft,
#endif

  // From Right View
#if VSP_OPTIMIZATION
  if((SynDirection==1)||(SynDirection==2))
    ForwardWarpSingleView(RGHTVIEW); // RefRight, RefDepthRight,
#else
  ForwardWarpSingleView(RGHTVIEW); // RefRight, RefDepthRight,
#endif

}

/*
* \brief
*    Count the number of hole pixels around the specified position
* 
* \input
* \param MaskMap
*    The hole mask tabl
* \param x, y
*    Specify the position to count number of holes
* \param width
*    The width of the picture
*
* \return
*    Number of hole pixels
*/
int CViewInterpolation1D::CountHolePixels(byte** MaskMap, int x, int y, int width)
{
  int blksizey = 3;
#if NTT_SUBPEL_VSP_M24796
  int blksizex = blksizey;
#else
  int blksizex = blksizey*SubPelOption*UpsampleRefs;
#endif
  int startx, endx;
  int starty, endy;
  int i, j;
  int counter = 0;

  startx = x - blksizex;
  endx   = x + blksizex;
  starty = y - blksizey;
  endy   = y + blksizey;

  if (startx < 0) startx = 0;
  if (endx   > width) endx = width;
  if (starty < 0) starty = 0;
  if (endy   > Height)  endy = Height;

  for (j = starty; j < endy; j++)
  {
    for (i = startx; i < endx; i++)
    {
      counter += MaskMap[j][i];
    }
  }

  return counter;
}

/*
* \brief
*    Merge the two synthesized view into one
*
* \input
* \class member SynY[i], SynU[i], SynV[i]
*    The synthesized view, in 4:4:4
*    i = 0 from left view
*    i = 1 from right view
* \class member Mask[i]
*    The hole map
*    i = 0 from left view
*    i = 1 from right view
* \class member Zmap[i]
*    The depth map (real depth values stored) warped from
*    i = 0 from left view
*    i = 1 from right view
* \class member Dmap[i]
*    The depth map (depth levels stored) warped from
*    i = 0 from left view
*    i = 1 from right view
*
* \output
* \class member SynY[MERGVIEW], SynU[MERGVIEW], SynV[MERGVIEW]
*    The unique synthesized picture, in 4:4:4
* \class member Mask[MERGVIEW]
*    The unique hole mask
* \class member Zmap[MERGVIEW]
*    The unique synthesized depth map (real depth values stored)
* \class member Dmap[MERGVIEW]
*    The unique synthesized depth map (depth levels stored)
*
* \return
*    None
*/
void CViewInterpolation1D::Merge()
{
  int x, y;
  double Y, U, V, D;
  int HoleCountLeft, HoleCountRight;
  bool isLeftNearToCamera = true;  // just to avoid compile warning
#if NTT_SUBPEL_VSP_M24796
  int WidthSyn = Width;
#else
  int WidthSyn = Width2*SubPelOption;
#endif

  for (y = 0; y < Height; ++y)
  {
    imgpel* syn_left_Y=SynY[LEFTVIEW][y];
    imgpel* syn_left_U=SynU[LEFTVIEW][y];
    imgpel* syn_left_V=SynV[LEFTVIEW][y];
    imgpel* syn_right_Y=SynY[RGHTVIEW][y];
    imgpel* syn_right_U=SynU[RGHTVIEW][y];
    imgpel* syn_right_V=SynV[RGHTVIEW][y];
    imgpel* syn_merge_Y=SynY[MERGVIEW][y];
    imgpel* syn_merge_U=SynU[MERGVIEW][y];
    imgpel* syn_merge_V=SynV[MERGVIEW][y];
    imgpel* mask_left=Mask[LEFTVIEW][y];
    imgpel* mask_right=Mask[RGHTVIEW][y];
    imgpel* mask_merge=Mask[MERGVIEW][y];
    imgpel* dmap_left=Dmap[LEFTVIEW][y];
    imgpel* dmap_right=Dmap[RGHTVIEW][y];
    imgpel* dmap_merge=Dmap[MERGVIEW][y];

    for (x = 0; x < WidthSyn;  ++x)
    {
      // The current pixel has two versions
      if ( *(mask_left+x)== NOTHOLEPIXEL &&  *(mask_right+x)== NOTHOLEPIXEL)
      {
        if(MergingOption == 0)
        { // Z-buffer only
          if (*(dmap_left+x)>= *(dmap_right+x))
            isLeftNearToCamera = true;
          else
            isLeftNearToCamera = false;
          if (isLeftNearToCamera)
          {    // Left view is near to the camera
            *(syn_merge_Y+x)=*(syn_left_Y+x);
            *(syn_merge_U+x)=*(syn_left_U+x);
            *(syn_merge_V+x)=*(syn_left_V+x);
            *(dmap_merge+x)=*(dmap_left+x);
            *(mask_merge+x)=NOTHOLEPIXEL;
          }
          // Right view is near to the camera
          else
          {
            *(syn_merge_Y+x)=*(syn_right_Y+x);
            *(syn_merge_U+x)=*(syn_right_U+x);
            *(syn_merge_V+x)=*(syn_right_V+x);
            *(dmap_merge+x)=*(dmap_right+x);
            *(mask_merge+x)=NOTHOLEPIXEL;
          }
        }
        else if(MergingOption==1)
        {// Camera distance as weighting factor

#if VSP_OPTIMIZATION
          Y=(WeightLeftTable[SynY[LEFTVIEW][y][x]]+WeightRightTable[SynY[RGHTVIEW][y][x]]+32768)>>16;
          U=(WeightLeftTable[SynU[LEFTVIEW][y][x]]+WeightRightTable[SynU[RGHTVIEW][y][x]]+32768)>>16;
          V=(WeightLeftTable[SynV[LEFTVIEW][y][x]]+WeightRightTable[SynV[RGHTVIEW][y][x]]+32768)>>16;
          D=(WeightLeftTable[Dmap[LEFTVIEW][y][x]]+WeightRightTable[Dmap[RGHTVIEW][y][x]]+32768)>>16;
#else
          Y = WeightLeft*SynY[LEFTVIEW][y][x]+WeightRight*SynY[RGHTVIEW][y][x];
          U = WeightLeft*SynU[LEFTVIEW][y][x]+WeightRight*SynU[RGHTVIEW][y][x];
          V = WeightLeft*SynV[LEFTVIEW][y][x]+WeightRight*SynV[RGHTVIEW][y][x];
          D = WeightLeft*Dmap[LEFTVIEW][y][x]+WeightRight*Dmap[RGHTVIEW][y][x];
#endif

          SynY[MERGVIEW][y][x] = (unsigned char)Y;
          SynU[MERGVIEW][y][x] = (unsigned char)U;
          SynV[MERGVIEW][y][x] = (unsigned char)V;
          Dmap[MERGVIEW][y][x] = (unsigned char)D;
          Mask[MERGVIEW][y][x] = NOTHOLEPIXEL; 
        }

        else if(MergingOption == 2)
        {// Hole counting + Z-buffer

          // Note that because the DE s/w currently output diff depth range for diff views, the performance may be a little worse than expected

          // When the two pixel depthes are close enough, we may do average
          if(abs(Dmap[LEFTVIEW][y][x] - Dmap[RGHTVIEW][y][x]) <=  DepthThreshold) //camera weighting         Dmap[RGHTVIEW][y][x]
          {
            //hole counting
            HoleCountLeft  = CountHolePixels(Mask[LEFTVIEW], x, y, WidthSyn);
            HoleCountRight = CountHolePixels(Mask[RGHTVIEW], x, y, WidthSyn);
            bool One_view = (abs(HoleCountLeft - HoleCountRight) >= HoleCountThreshold*SubPelOption*UpsampleRefs);
            //One_view = false;

            //bool One_view;
            //if (IsBoundary(BoundaryMask[LEFTVIEW], x, y) || IsBoundary(BoundaryMask[RGHTVIEW], x, y))
            // If it is obvious that one synthesized result contains much less holes, it is assumed to be more reliable
            if(One_view)
            {
              if(HoleCountLeft <= HoleCountRight)
              { //pick left, sine less holes from left
                SynY[MERGVIEW][y][x] = SynY[LEFTVIEW][y][x];
                SynU[MERGVIEW][y][x] = SynU[LEFTVIEW][y][x];
                SynV[MERGVIEW][y][x] = SynV[LEFTVIEW][y][x];
                Dmap[MERGVIEW][y][x] = Dmap[LEFTVIEW][y][x];
                Mask[MERGVIEW][y][x] = NOTHOLEPIXEL;
              }
              else
              {//pick right, since less holes from right
                SynY[MERGVIEW][y][x] = SynY[RGHTVIEW][y][x];
                SynU[MERGVIEW][y][x] = SynU[RGHTVIEW][y][x];
                SynV[MERGVIEW][y][x] = SynV[RGHTVIEW][y][x];
                Dmap[MERGVIEW][y][x] = Dmap[RGHTVIEW][y][x];
                Mask[MERGVIEW][y][x] = NOTHOLEPIXEL;
              }
            }

            // Otherwise, do average
            else //camera distance weighting
            {
#if VSP_OPTIMIZATION
              Y=(WeightLeftTable[SynY[LEFTVIEW][y][x]]+WeightRightTable[SynY[RGHTVIEW][y][x]])>>16;
              U=(WeightLeftTable[SynU[LEFTVIEW][y][x]]+WeightRightTable[SynU[RGHTVIEW][y][x]])>>16;
              V=(WeightLeftTable[SynV[LEFTVIEW][y][x]]+WeightRightTable[SynV[RGHTVIEW][y][x]])>>16;
              D=(WeightLeftTable[Dmap[LEFTVIEW][y][x]]+WeightRightTable[Dmap[RGHTVIEW][y][x]])>>16;
#else
              Y = WeightLeft*(double)SynY[LEFTVIEW][y][x]+WeightRight*(double)SynY[RGHTVIEW][y][x];
              U = WeightLeft*(double)SynU[LEFTVIEW][y][x]+WeightRight*(double)SynU[RGHTVIEW][y][x];
              V = WeightLeft*(double)SynV[LEFTVIEW][y][x]+WeightRight*(double)SynV[RGHTVIEW][y][x];
              D = WeightLeft*(double)Dmap[LEFTVIEW][y][x]+WeightRight*(double)Dmap[RGHTVIEW][y][x];
#endif
              SynY[MERGVIEW][y][x] = (unsigned char)Y;
              SynU[MERGVIEW][y][x] = (unsigned char)U;
              SynV[MERGVIEW][y][x] = (unsigned char)V;
              Dmap[MERGVIEW][y][x]= (unsigned char)D;
              Mask[MERGVIEW][y][x] = NOTHOLEPIXEL;
            }
          }
          // When the two pixel depthes are far away enough, we always select one winner
          else 
          {
            // find out which is near to the camera
            if (Dmap[LEFTVIEW][y][x] >= Dmap[RGHTVIEW][y][x])
              isLeftNearToCamera = true;
            else
              isLeftNearToCamera = false;
            if (isLeftNearToCamera)
            {    // Left view is near to the camera
              SynY[MERGVIEW][y][x] = SynY[LEFTVIEW][y][x];
              SynU[MERGVIEW][y][x] = SynU[LEFTVIEW][y][x];
              SynV[MERGVIEW][y][x] = SynV[LEFTVIEW][y][x];
              Dmap[MERGVIEW][y][x] = Dmap[LEFTVIEW][y][x];
              Mask[MERGVIEW][y][x] = NOTHOLEPIXEL;
            }
            else 
            {// Right view is near to the camera
              SynY[MERGVIEW][y][x] = SynY[RGHTVIEW][y][x];
              SynU[MERGVIEW][y][x] = SynU[RGHTVIEW][y][x];
              SynV[MERGVIEW][y][x] = SynV[RGHTVIEW][y][x];
              Dmap[MERGVIEW][y][x] = Dmap[RGHTVIEW][y][x];
              Mask[MERGVIEW][y][x] = NOTHOLEPIXEL;
            }
          }
        }
      }
      // The current pixel only has one version from the left view
      else if (Mask[LEFTVIEW][y][x] == NOTHOLEPIXEL)
      {
        *(syn_merge_Y+x)=*(syn_left_Y+x);
        *(syn_merge_U+x)=*(syn_left_U+x);
        *(syn_merge_V+x)=*(syn_left_V+x);
        *(dmap_merge+x)=*(dmap_left+x);
        *(mask_merge+x)=NOTHOLEPIXEL;

      }
      // The current pixel only has one version from the right view
      else if (Mask[RGHTVIEW][y][x] == NOTHOLEPIXEL)
      {
        *(syn_merge_Y+x)=*(syn_right_Y+x);
        *(syn_merge_U+x)=*(syn_right_U+x);
        *(syn_merge_V+x)=*(syn_right_V+x);
        *(dmap_merge+x)=*(dmap_right+x);
      } 
    }
  }

}

/*
* \brief 
*    Check if the background is on the right of the hole
*
* \input
* \param xleft, y
*    The position of the pixel that is on the left of the hole
* \param xright, y
*    The position of the pixel that is on the right of the hole
* \param blksz
*    Size of the block that is used to check depth value
* \class member Zmap[MERGVIEW]
*    The depth image (real depth values stroed) warped to virtual view
* \class member Dmap[MERGVIEW]
*    The depth image (depth levels stored) warped to virtual view
* 
* \return
*    1: background is on the right of the hole
*    0: background is on the left of the hole
*/
int CViewInterpolation1D::IsBackgroundOnRight(int xleft, int xright, int y, int blksize)
{
  int i, j;
  int blksizeyMinus = 0;
  int blksizeyPlus  = 0;
  int startx, starty;
  int endx, endy;
#if VSP_OPTIMIZATION
  int dleft, dright;
#else
  double dleft, dright;
#endif
  int dn;

  // Calc the left side
  startx = xleft - blksize + 1;
  starty = y - blksizeyMinus;
  endx = xleft;
  endy = y + blksizeyPlus;
  if (startx < 0)
    startx = 0;
  if (starty < 0)
    starty = 0;
  if (endy >= Height)
    endy = Height - 1;

  dleft = 0;
  dn=0;
  for (j = starty; j <= endy; ++j)
  {
    for (i = startx; i <= endx; ++i)
    {
#if VSP_OPTIMIZATION
      if(SynDirection==2)
        dleft += Dmap[MERGVIEW][j][i];
      else if(SynDirection==1)
        dleft += Dmap[RGHTVIEW][j][i];
      else
        dleft += Dmap[LEFTVIEW][j][i];
#else

      dleft += (double)Dmap[MERGVIEW][j][i];
      ++dn;
#endif
    }
  }
#if(!VSP_OPTIMIZATION)
  if (dn != 0)
    dleft = dleft / dn;
#endif

  // Calc the right side
  startx = xright;
  starty = y - blksizeyMinus;
  endx = xright + blksize - 1;
  endy = y + blksizeyPlus;

#if NTT_SUBPEL_VSP_M24796
  if (endx >= Width)
    endx = Width - 1;
#else
  if (endx >= Width2*SubPelOption)
    endx = Width2*SubPelOption - 1;
#endif
  if (starty < 0)
    starty = 0;
  if (endy >= Height)
    endy = Height - 1;

  dright = 0;
  dn = 0;
  for (j = starty; j <= endy; ++j)
  {
    for (i = startx; i <= endx; ++i)
    {
#if VSP_OPTIMIZATION
      if(SynDirection==2)
        dright += Dmap[MERGVIEW][j][i];
      else if(SynDirection==1)
        dright += Dmap[RGHTVIEW][j][i];
      else if(SynDirection==0)
        dright+=Dmap[LEFTVIEW][j][i];
#else
      dright += (double)Dmap[MERGVIEW][j][i];
      dn++;
#endif
    }
  }

#if(!VSP_OPTIMIZATION)
  if (dn != 0)
    dright = dright / dn;
#endif

  // when using Dmap as conditions
  return (dright <= dleft);
}
/*
* \brief
*    Perform hole filling
*
* \input
* \param Yi
*    The synthesized picture, current have holes. One component.
* \class member Mask[MERGVIEW]
*    Show the hole positions
* \ class member Zmap[MERGVIEW]
*    the warped depth map.
*
* \output
* \param Yo
*    The synthesized picture itself is modified due to hole filling
*    
* \return
*    none
*/
void CViewInterpolation1D::FillHoles(imgpel** Yo, imgpel** Yi)
{
  int pixelValue=-1;
  int x=0, y=0, indX=0, ind2X = 0;
  int xnhole=0;
  int found=0;
  int fillDir=0; // 0: fail to fill; 1: fill from left; 2: fill from right

#if NTT_SUBPEL_VSP_M24796
  memset(&(Yo[0][0]), 0, Width*Height);
#else
  memset(&(Yo[0][0]), 0, Width2*Height*SubPelOption);
#endif

  for (y = 0; y < Height; y++)
  {
    indX = 0;
#if NTT_SUBPEL_VSP_M24796
    for (x = 0; x < Width;  x++)
#else
    for (x = 0; x < Width2*SubPelOption;  x++)
#endif
    {
      if (Mask[MERGVIEW][y][x] == HOLEPIXEL) // This is a starting pixel of a hole
      {
        if (pixelValue < 0) // This is the first pixel of a hole
        {
          found = 0;
          xnhole = x + 1;
          ind2X = indX + 1;
#if NTT_SUBPEL_VSP_M24796
          while (!found && xnhole < Width)
#else
          while (!found && xnhole < Width2*SubPelOption)
#endif
          {
            if (Mask[MERGVIEW][y][ind2X] == NOTHOLEPIXEL)
            {
              found = 1;
            }
            else
            { 
              xnhole++;
              ind2X++; 
            }
          }

          // fill from left or from right?
          if (x == 0) // the hole is at leftmost
          {
            if (found)
              fillDir = 2;  // fill from right
            else // the whole line is a hole
              fillDir = 0;  // maybe fill hole from above
          }
          else if (found == 0) // the hole is at rightmost
          {
            fillDir = 1;  // fill from left
          }
          else if (found) // the hole is in the middle
          {
            // fill the hole using the pixel with larger depth, farther away from the camera
            if (IsBackgroundOnRight(x-1, xnhole, y, 1))
              fillDir = 2;
            else
              fillDir = 1;
          }

          // Set the non-hole pixel
          pixelValue = 0;
          if (fillDir == 1) // from the left
            pixelValue = Yi[y][indX-1];
          else if (fillDir == 2) // from the right
            pixelValue = Yi[y][ind2X];

          Yo[y][indX] = pixelValue;
        }
        else
        {
          Yo[y][indX] = pixelValue;
          if (indX >= ind2X)
            pixelValue = -1;
        }
      }
      else // This is not a hole
      {
        Yo[y][x] = Yi[y][x];
        pixelValue = -1;
      }

      indX++; // Move to the next pixel
    }
  }
}

void CViewInterpolation1D::FillHoles()
{
  int x=0, y=0;
  int found_hole=0;
  int hole_left_pos=0,hole_right_pos=0;//!<the start/end position of the hole on the left/right 

  int height=Height;
#if NTT_SUBPEL_VSP_M24796
  int width=Width;
#else
  int width=Width2*SubPelOption;
#endif
  int width_minus1=width-1;

  int fill_dir=0;// 0: fail to fill; 1: fill from left; 2: fill from right

  imgpel pixel_Y=0;
  imgpel pixel_U=0;
  imgpel pixel_V=0;
  imgpel pixel_D=0;

  imgpel** input_Y=(SynDirection==2)?SynY[MERGVIEW]:((SynDirection==1)?SynY[RGHTVIEW]:SynY[LEFTVIEW]);
  imgpel** input_U=(SynDirection==2)?SynU[MERGVIEW]:((SynDirection==1)?SynU[RGHTVIEW]:SynU[LEFTVIEW]);
  imgpel** input_V=(SynDirection==2)?SynV[MERGVIEW]:((SynDirection==1)?SynV[RGHTVIEW]:SynV[LEFTVIEW]);
  imgpel** input_d=(SynDirection==2)?Dmap[MERGVIEW]:((SynDirection==1)?Dmap[RGHTVIEW]:Dmap[LEFTVIEW]);
  imgpel** input_mask=(SynDirection==2)?Mask[MERGVIEW]:((SynDirection==1)?Mask[RGHTVIEW]:Mask[LEFTVIEW]);


  for(y=0;y<Height;++y)
  {
    imgpel* syn_hlf_Y=SynY[HLFLVIEW][y];
    imgpel* syn_hlf_U=SynU[HLFLVIEW][y];
    imgpel* syn_hlf_V=SynV[HLFLVIEW][y];
    imgpel* syn_hlf_D=Dmap[HLFLVIEW][y];
    imgpel* syn_merge_Y=input_Y[y];
    imgpel* syn_merge_U=input_U[y];
    imgpel* syn_merge_V=input_V[y];
    imgpel* syn_merge_D=input_d[y];
    imgpel* mask=input_mask[y];

    found_hole=0;
    memcpy(syn_hlf_Y,syn_merge_Y,width*sizeof(imgpel));
    memcpy(syn_hlf_U,syn_merge_U,width*sizeof(imgpel));
    memcpy(syn_hlf_V,syn_merge_V,width*sizeof(imgpel));
    memcpy(syn_hlf_D,syn_merge_D,width*sizeof(imgpel));

    for(x=0;x<width;++x)
    {
      if(*(mask+x) == HOLEPIXEL)
      {
        if(!found_hole)
        {
          found_hole=1;
          hole_left_pos=x;
          hole_right_pos=x+1;
          while((hole_right_pos<width)&&(*(mask+hole_right_pos)== HOLEPIXEL))
            ++hole_right_pos;
          if((hole_left_pos>0)&&(hole_right_pos<width))
          {
            if (IsBackgroundOnRight(hole_left_pos-1, hole_right_pos, y, 1))
              fill_dir = 2;
            else
              fill_dir = 1;
          }
          else if(hole_left_pos==0)
            fill_dir=2;
          else if(hole_right_pos==width)
            fill_dir=1;

          pixel_Y=0;
          pixel_U=0;
          pixel_V=0;
          pixel_D=0;
          if (fill_dir == 1) // from the left
          {
            pixel_Y=*(syn_merge_Y+hole_left_pos-1);
            pixel_U=*(syn_merge_U+hole_left_pos-1);
            pixel_V=*(syn_merge_V+hole_left_pos-1);
            pixel_D=*(syn_merge_D+hole_left_pos-1);
          }
          else if (fill_dir == 2) // from the right
          {
            pixel_Y=*(syn_merge_Y+hole_right_pos);
            pixel_U=*(syn_merge_U+hole_right_pos);
            pixel_V=*(syn_merge_V+hole_right_pos);
            pixel_D=*(syn_merge_D+hole_right_pos);
          }
          *(syn_hlf_Y+x)=pixel_Y;
          *(syn_hlf_U+x)=pixel_U;
          *(syn_hlf_V+x)=pixel_V;
          *(syn_hlf_D+x)=pixel_D;
        }
        else
        {
          assert(found_hole);
          *(syn_hlf_Y+x)=pixel_Y;
          *(syn_hlf_U+x)=pixel_U;
          *(syn_hlf_V+x)=pixel_V;
          *(syn_hlf_D+x)=pixel_D;

        }
      }
      else
      {
        found_hole=0;
      }
    }
  }
}

/*
* \brief
*    Scale down the synthesized view
* \param Yo
*    
* \param Yi
*    
*    
*/
/*void CViewInterpolation1D::ScaleDownSyn(imgpel** Yo, imgpel** Yi)
{
#if 0
  // Using AVC downsampling filter
  int i, j;
  int k, x, idx;
  long int s, t;
#endif

  if (SubPelOption == 1)
  {
    memcpy(Yo, Yi, Width2*Height);
  }
  else if (SubPelOption == 2)
  {
    CPictureResample resmple;
    resmple.DownsampleView(Yo, Yi, Width2*SubPelOption, Height, 2);
  }  
  else if (SubPelOption == 4)
  {
    CPictureResample resmple;
    resmple.DownsampleView(Yo, Yi, Width2*SubPelOption, Height, 4);
#if 0
    // Using AVC downsampling filter
    int WidthMinus1 = Width2*SubPelOption-1;
    unsigned char *in, *out;
    int pel[7];

    for (j = 0; j < Height; j++)
    {
      out = &Yo[j*Width2*SubPelOption/4];
      in  = &Yi[j*Width2*SubPelOption];
      for(i = 0; i < Width2*SubPelOption; i+=4)
      {
        pel[0] = CLIP(i-3, 0, WidthMinus1);
        pel[1] = CLIP(i-2, 0, WidthMinus1);
        pel[2] = CLIP(i-1, 0, WidthMinus1);
        pel[3] = CLIP(i  , 0, WidthMinus1);
        pel[4] = CLIP(i+1, 0, WidthMinus1);
        pel[5] = CLIP(i+2, 0, WidthMinus1);
        pel[6] = CLIP(i+3, 0, WidthMinus1);

        out[(i >> 2)] = CLIP ( (16*in[pel[3]] + 9*(in[pel[2]] + in[pel[4]]) - (in[pel[0]] + in[pel[6]])) >> 5, 0, 255);
      }
    }
#endif
  }
  else
    printf("Error: The scale factor is not supported: %d\n", SubPelOption);
}  */

/*
* \brief
*    Perform boundary detection based on the depth image
*    The org function was replaced in May 2009.
*    -Zhejiang Univ. May 2009
*
* \input
* \param depth
*    Depth map
*
* \output
* \param edge
*    Edge map: 0-NONE, 1-RISE, 2-FALL (left to right), 3-AROUND, 4-SUSPECT
*    NONE: not boundary
*    RISE: the top position of a rising edge from left to right
*    FALL: the top position of a falling edge from left to right
*    AROUND: boundary growth area around both sides of RISE or FALL edge
*    SUSPECT: 2-pixel-wide area at left side of RISE edge and right side of FALL edge.
*    
* \return
*    none
*/
void CViewInterpolation1D::DetectBoundary( byte** edge, imgpel** depth, int width, int height, int threshold)
{
  int i,j;
  int diff=0;
  int edge_growth = BoundaryGrowth;
  byte**mask=NULL;
  get_mem2D(&mask,height,width);

  for(j=0; j<height; ++j)
  {
    edge[j][0]=NONE; //0th of each row
    for(i=1; i<width; ++i) //x-(x-1)
    {
      diff=depth[j][i]-depth[j][i-1];
      if( diff > threshold ) 
        edge[j][i] = RISE;
      else if( diff < -threshold)
        edge[j][i-1] = FALL;
      else
        edge[j][i] = NONE;
    }
  }

  for(j=0; j<height; ++j)
  {
    //to make edge at top point and 1-pixel-wide
    for( i=1; i<width-1; ++i)
    {
      if( edge[j][i]==RISE && edge[j][i+1]==NONE && depth[j][i+1]-depth[j][i]>5 )      
        mask[j][i+1] = 50; // later be changed to RISE
      else if( edge[j][i]==FALL && edge[j][i-1]==NONE && depth[j][i-1]-depth[j][i]>5 )  
        mask[j][i-1] = 60; // later be changed to FALL
      else if( edge[j][i]==RISE && edge[j][i+1]==RISE )                              
        mask[j][i] = 40;
      else if( edge[j][i]==FALL && edge[j][i-1]==FALL )                               
        mask[j][i] = 40;   // later be changed to NONE
    }

    for(i=0;i<width;++i)
    {
      if( mask[j][i] == 50 )      
      { 
        edge[j][i] = RISE;  
        edge[j][i-1] = NONE;
      }
      else if( mask[j][i] == 60 ) 
      {  
        edge[j][i] = FALL;  
        edge[j][i+1] = NONE;
      }
      else if( mask[j][i] == 40)
      { 
        edge[j][i] = NONE;
      }
    }
    //to mark 2-pixel-wide area at left side of RISE edge and at right side of FALL edge
    for(i=2;i<width-2;++i)
    {
      if(edge[j][i+1] == RISE || edge[j][i+2] == RISE)
        edge[j][i] = SUSPECT;
      else if(edge[j][i-1] == FALL || edge[j][i-2] == FALL)
        edge[j][i] = SUSPECT;
    }
    //to mark edge_growth-pixel-wide area around strong depth edge (both sides)
    for(i=edge_growth;i<width-edge_growth;++i)
    {
      //if current pixel is FALL, mask the right-40 pixel as boundary pixel
      if(edge[j][i] == FALL || edge[j][i] == RISE)
      {
        for(int p = i - edge_growth; p <= i + edge_growth; p++)
        {
          if( edge[j][p] == NONE)
            edge[j][p] = AROUND;
        }
      }
    }
  }

  free_mem2D(mask);
}

/*
* \brief
*    Check a small block around a specified pixel if there is any boundary pixel
*
* \input
* \param BoundaryArray
*    A map indicating which pixels are regarded as boundaries. (the mask has the size of Width2*Height)
* \param x, y
*    The pixel position
*    
* \return
*    true: if there is any boundary pixel around
*    false: if there is no any boundary pixel around
*/
bool CViewInterpolation1D::IsBoundary(byte** BoundaryArray, int x, int y)
{
  if( BoundaryArray[y][x] != NONE ) //which means == RISE || FALL || AROUND
    return true;
  else
    return false;
}
#endif
/*
* \brief
*    Scale down the synthesized view
* \param Yo
*    
* \param Yi
*    
*    
*/

#if (ZJU_VSP && BOUNDARY_DETECT_ATM02)
//This code is simplified version of DetectBoundary() in ATM0.2, only for supporting boundary-aware splatting,
//This code was proposed in m23629
void CViewInterpolation1D::DetectBoundary( byte** edge, imgpel** depth, int width, int height, int threshold)
{
  int i,j;
  int diff=0;
  int edge_growth = BoundaryGrowth;


  for(j=0; j<height; ++j)
  {
    for(i=edge_growth; i<width-edge_growth; ++i) //x-(x-1)
    {
      diff=abs(depth[j][i]-depth[j][i-1]);
      if( diff > threshold ) 
      {
        for(int p = i - edge_growth; p <= i + edge_growth; p++)
          edge[j][p] = AROUND;
      }
    }
  }

}
#endif

#if !NTT_SUBPEL_VSP_M24796
void CViewInterpolation1D::ScaleDownSyn(imgpel** Yo, imgpel** Yi)
{
  int h=0;
  if (SubPelOption == 1)
  {
    for(h=0;h<Height;++h)
    {
      memcpy(Yo[h],Yi[h],Width2*sizeof(imgpel));
    }
  }
  else if (SubPelOption == 2)
  {
    CPictureResample resmple;
    resmple.DownsampleView(Yo, Yi, Width2*SubPelOption, Height, 2);
  }  
  else if (SubPelOption == 4)
  {
    CPictureResample resmple;
    resmple.DownsampleView(Yo, Yi, Width2*SubPelOption, Height, 4);
  }
  else
    printf("Error: The scale factor is not supported: %d\n", SubPelOption);
}
#endif

/*
* \brief
*    Perform view interpolation using two refence views
*
* \input
*    The images are arranged in a one dimension array
* \param RefLeft
*    The left ref view image. YUV 444
* \param RefRight
*    The right ref view image YUV 444
* \param RefDepthLeft
*    The left ref depth view image
* \param RefDepthRight
*    The right ref depth view image
*
*
* \output
* \param Syn
*    The synthesized view. YUV 420
* \param SynMask
*    The mask showing the hole positions
*    
* \return
*    0: success
*    non-zero: fail
*/
#if QC_VSP_Asymmetric_Resolution
int CViewInterpolation1D::DoOneFrame(ViewSynIOFrmData* pVSIOFrmData, Boolean bMixedRes)
#else
int CViewInterpolation1D::DoOneFrame(ViewSynIOFrmData* pVSIOFrmData)
#endif
{
  imgpel** pRefLeft[3]={NULL};//!<Y,U,V
  imgpel** pRefRight[3]={NULL};//!<Y,U,V
  imgpel** pRefDepthLeft=NULL;
  imgpel** pRefDepthRight=NULL;
  CPictureResample Resampling;
  int h=0;

  //int OutputWidth=0;

  Width2 = Width*UpsampleRefs;

  //Zhejiang, Temporal Improvement Option
  if(TemporalImprovementOption == 1)
  {
#if QC_VSP_Asymmetric_Resolution
    if(bMixedRes)
    {
      TemporalImprovementMethod420(pVSIOFrmData->ColorLeft[0],  RefViewLast[0], pVSIOFrmData->DepthLeft,  RefDepthLast[0], FrameNumber, Width, Height);
      TemporalImprovementMethod420(pVSIOFrmData->ColorRight[0], RefViewLast[1], pVSIOFrmData->DepthRight, RefDepthLast[1], FrameNumber, Width, Height);
    }
    else
    {
#endif
      TemporalImprovementMethod(pVSIOFrmData->ColorLeft[0],  RefViewLast[0], pVSIOFrmData->DepthLeft,  RefDepthLast[0], FrameNumber, Width, Height);
      TemporalImprovementMethod(pVSIOFrmData->ColorRight[0], RefViewLast[1], pVSIOFrmData->DepthRight, RefDepthLast[1], FrameNumber, Width, Height);
#if QC_VSP_Asymmetric_Resolution
    }
#endif
    ++FrameNumber;
  }



  pRefLeft[0]=RefView[LEFTVIEW][0];   //!<Y
  pRefLeft[1]=RefView[LEFTVIEW][1];   //!<U
  pRefLeft[2]=RefView[LEFTVIEW][2];   //!<V
  pRefRight[0]=RefView[RGHTVIEW][0];  //!<Y
  pRefRight[1]=RefView[RGHTVIEW][1];  //!<U
  pRefRight[2]=RefView[RGHTVIEW][2];  //!<V


  pRefDepthLeft  = RefDepth[LEFTVIEW] ;
  pRefDepthRight = RefDepth[RGHTVIEW] ;

#if NTT_SUBPEL_VSP_M24796

#if QC_VSP_Asymmetric_Resolution
  if(bMixedRes)
  {
    if(SynDirection==0)
    {
      RefView[LEFTVIEW][0]  = pVSIOFrmData->ColorLeft[0];
      RefDepth[LEFTVIEW]    = pVSIOFrmData->DepthLeft;
      RefView[LEFTVIEW][1] = pVSIOFrmData->ColorLeft[1];
      RefView[LEFTVIEW][2] = pVSIOFrmData->ColorLeft[2];

      if( pVSIOFrmData->ColorSynthesized[0] )
      {
        SynY[FINLVIEW]      = pVSIOFrmData->ColorSynthesized[0];
        SynU[FINLVIEW]      = pVSIOFrmData->ColorSynthesized[1];
        SynV[FINLVIEW]      = pVSIOFrmData->ColorSynthesized[2];
      }
      if( pVSIOFrmData->DepthSynthesized )
        Dmap[FINLVIEW]      = pVSIOFrmData->DepthSynthesized;
    }
    else if(SynDirection==1)
    {
      RefView[RGHTVIEW][0]  = pVSIOFrmData->ColorRight[0];
      RefDepth[RGHTVIEW]    = pVSIOFrmData->DepthRight;
      RefView[RGHTVIEW][1] = pVSIOFrmData->ColorRight[1];
      RefView[RGHTVIEW][2] = pVSIOFrmData->ColorRight[2];

      if( pVSIOFrmData->ColorSynthesized[0] )
      {
        SynY[FINLVIEW]      = pVSIOFrmData->ColorSynthesized[0];
        SynU[FINLVIEW]      = pVSIOFrmData->ColorSynthesized[1];
        SynV[FINLVIEW]      = pVSIOFrmData->ColorSynthesized[2];
      }
      if( pVSIOFrmData->DepthSynthesized )
        Dmap[FINLVIEW]      = pVSIOFrmData->DepthSynthesized;
    }
    else
    {
      RefView[LEFTVIEW][0]  = pVSIOFrmData->ColorLeft[0];
      RefView[LEFTVIEW][1]  = pVSIOFrmData->ColorLeft[1];
      RefView[LEFTVIEW][2]  = pVSIOFrmData->ColorLeft[2];
      RefDepth[LEFTVIEW]    = pVSIOFrmData->DepthLeft;
      RefView[RGHTVIEW][0]  = pVSIOFrmData->ColorRight[0];
      RefView[RGHTVIEW][1]  = pVSIOFrmData->ColorRight[1];
      RefView[RGHTVIEW][2]  = pVSIOFrmData->ColorRight[2];
      RefDepth[RGHTVIEW]    = pVSIOFrmData->DepthRight;
    }
  }
  else
#endif
  {
    if(SynDirection==0)
    {
      RefView[LEFTVIEW][0]  = pVSIOFrmData->ColorLeft[0];
      RefDepth[LEFTVIEW]    = pVSIOFrmData->DepthLeft;

      // Upsample the UV component
      Resampling.PictureResample420to444(pRefLeft[1], pVSIOFrmData->ColorLeft[1], Width/2, Height/2);
      Resampling.PictureResample420to444(pRefLeft[2], pVSIOFrmData->ColorLeft[2], Width/2, Height/2);

      // Set output buffer
      if( pVSIOFrmData->ColorSynthesized[0] )
        SynY[FINLVIEW]      = pVSIOFrmData->ColorSynthesized[0];
      if( pVSIOFrmData->DepthSynthesized )
        Dmap[FINLVIEW]      = pVSIOFrmData->DepthSynthesized;
    }
    else if(SynDirection==1)
    {
      RefView[RGHTVIEW][0]  = pVSIOFrmData->ColorRight[0];
      RefDepth[RGHTVIEW]    = pVSIOFrmData->DepthRight;

      // Upsample the UV component
      Resampling.PictureResample420to444(pRefRight[1], pVSIOFrmData->ColorRight[1], Width/2, Height/2);
      Resampling.PictureResample420to444(pRefRight[2], pVSIOFrmData->ColorRight[2], Width/2, Height/2);

      // Set output buffer
      if( pVSIOFrmData->ColorSynthesized[0] )
        SynY[FINLVIEW]      = pVSIOFrmData->ColorSynthesized[0];
      if( pVSIOFrmData->DepthSynthesized )
        Dmap[FINLVIEW]      = pVSIOFrmData->DepthSynthesized;
    }
    else
    {
      for(h=0;h<Height;++h)
      {
        memcpy(pRefLeft[0][h],pVSIOFrmData->ColorLeft[0][h],Width*sizeof(imgpel));
        memcpy(pRefRight[0][h],pVSIOFrmData->ColorRight[0][h],Width*sizeof(imgpel));
        memcpy(pRefDepthLeft[h],pVSIOFrmData->DepthLeft[h],Width*sizeof(imgpel));
        memcpy(pRefDepthRight[h],pVSIOFrmData->DepthRight[h],Width*sizeof(imgpel));
      }
      // Upsample the UV component
      Resampling.PictureResample420to444(pRefLeft[1], pVSIOFrmData->ColorLeft[1] , Width/2, Height/2);
      Resampling.PictureResample420to444(pRefLeft[2], pVSIOFrmData->ColorLeft[2], Width/2, Height/2);
      Resampling.PictureResample420to444(pRefRight[1], pVSIOFrmData->ColorRight[1], Width/2, Height/2);
      Resampling.PictureResample420to444(pRefRight[2], pVSIOFrmData->ColorRight[2], Width/2, Height/2);
    }
  }
#else // NTT_SUBPEL_VSP_M24796
  if(UpsampleRefs==1)
  {

#if QC_VSP_Asymmetric_Resolution
    if(bMixedRes){
      for(h=0;h<Height;++h)
      {
        memcpy(pRefLeft[0][h], pVSIOFrmData->ColorLeft[0][h], Width*sizeof(imgpel));
        memcpy(pRefRight[0][h],pVSIOFrmData->ColorRight[0][h],Width*sizeof(imgpel));
      }

      for(h=0;h<Height/2;++h)
      {
        memcpy(pRefDepthLeft[h], pVSIOFrmData->DepthLeft[h], Width/2*sizeof(imgpel));
        memcpy(pRefDepthRight[h],pVSIOFrmData->DepthRight[h],Width/2*sizeof(imgpel));
      }
      for (h=0; h<Height/2; ++h)
      {
        memcpy(pRefLeft[1][h], pVSIOFrmData->ColorLeft[1][h], Width/2*sizeof(imgpel));
        memcpy(pRefLeft[2][h], pVSIOFrmData->ColorLeft[2][h], Width/2*sizeof(imgpel));

        memcpy(pRefRight[1][h], pVSIOFrmData->ColorRight[1][h], Width/2*sizeof(imgpel));
        memcpy(pRefRight[2][h], pVSIOFrmData->ColorRight[2][h], Width/2*sizeof(imgpel));
      }
    }else{
#endif
      for(h=0;h<Height;++h)
      {
        memcpy(pRefLeft[0][h],pVSIOFrmData->ColorLeft[0][h],Width*sizeof(imgpel));
        memcpy(pRefRight[0][h],pVSIOFrmData->ColorRight[0][h],Width*sizeof(imgpel));
        memcpy(pRefDepthLeft[h],pVSIOFrmData->DepthLeft[h],Width*sizeof(imgpel));
        memcpy(pRefDepthRight[h],pVSIOFrmData->DepthRight[h],Width*sizeof(imgpel));
      }
      // Upsample the UV component
      Resampling.PictureResample420to444(pRefLeft[1], pVSIOFrmData->ColorLeft[1] , Width/2, Height/2);
      Resampling.PictureResample420to444(pRefLeft[2], pVSIOFrmData->ColorLeft[2], Width/2, Height/2);
      Resampling.PictureResample420to444(pRefRight[1], pVSIOFrmData->ColorRight[1], Width/2, Height/2);
      Resampling.PictureResample420to444(pRefRight[2], pVSIOFrmData->ColorRight[2], Width/2, Height/2);
#if QC_VSP_Asymmetric_Resolution
    }
#endif
  }
  // Upsample ref views, if necessary
  else//!<Upsample==2 or 4
  {
#if VSP_OPTIMIZATION
    //imgpel**UV=NULL;
    //get_mem2D(&(UV),Height,Width);
#if QC_VSP_Asymmetric_Resolution
    if(bMixedRes){
      Resampling.UpsampleRefView420(pRefLeft[0],pVSIOFrmData->ColorLeft[0],
                   pRefLeft[1],pVSIOFrmData->ColorLeft[1],
                   pRefLeft[2],pVSIOFrmData->ColorLeft[2],
                   pRefDepthLeft,pVSIOFrmData->DepthLeft,
                   pRefRight[0],pVSIOFrmData->ColorRight[0],
                   pRefRight[1],pVSIOFrmData->ColorRight[1],
                   pRefRight[2],pVSIOFrmData->ColorRight[2],
                   pRefDepthRight,pVSIOFrmData->DepthRight,
                   Width,Height,UpsampleRefs,SynDirection);
    }else{
#endif
      imgpel** UV[4]={NULL,NULL,NULL,NULL};
      if((SynDirection==0)||(SynDirection==2))
      {
        get_mem2D(&(UV[0]),Height,Width);
        get_mem2D(&(UV[1]),Height,Width);
      }
      if((SynDirection==1)||(SynDirection==2))
      {
        get_mem2D(&(UV[2]),Height,Width);
        get_mem2D(&(UV[3]),Height,Width);
      }
      Resampling.PictureResample420to444ForRefView(UV[0],pVSIOFrmData->ColorLeft[1],
        UV[1],pVSIOFrmData->ColorLeft[2],
        UV[2],pVSIOFrmData->ColorRight[1],
        UV[3],pVSIOFrmData->ColorRight[2],
        Width/2, Height/2,SynDirection);

      Resampling.UpsampleRefView(pRefLeft[0],pVSIOFrmData->ColorLeft[0],
        pRefLeft[1],UV[0],
        pRefLeft[2],UV[1],
        pRefDepthLeft,pVSIOFrmData->DepthLeft,
        pRefRight[0],pVSIOFrmData->ColorRight[0],
        pRefRight[1],UV[2],
        pRefRight[2],UV[3],
        pRefDepthRight,pVSIOFrmData->DepthRight,
        Width,Height,UpsampleRefs,SynDirection);
      if(UV[0])
        free_mem2D(UV[0]);
      if(UV[1])
        free_mem2D(UV[1]);
      if(UV[2])
        free_mem2D(UV[2]);
      if(UV[3])
        free_mem2D(UV[3]);
#if QC_VSP_Asymmetric_Resolution
    }
#endif

#else

#if QC_VSP_Asymmetric_Resolution
    if(bMixedRes){
      Resampling.UpsampleRefView420(pRefLeft[0],pVSIOFrmData->ColorLeft[0],
                   pRefLeft[1],pVSIOFrmData->ColorLeft[1],
                   pRefLeft[2],pVSIOFrmData->ColorLeft[2],
                   pRefDepthLeft,pVSIOFrmData->DepthLeft,
                   pRefRight[0],pVSIOFrmData->ColorRight[0],
                   pRefRight[1],pVSIOFrmData->ColorRight[1],
                   pRefRight[2],pVSIOFrmData->ColorRight[2],
                   pRefDepthRight,pVSIOFrmData->DepthRight,
                   Width,Height,UpsampleRefs,SynDirection);
    }else{
#endif
      imgpel**UV=NULL;
      get_mem2D(&(UV),Height,Width);
      Resampling.UpsampleView(pRefLeft[0],pVSIOFrmData->ColorLeft[0],Width,Height,UpsampleRefs);
      Resampling.PictureResample420to444(UV, pVSIOFrmData->ColorLeft[1] , Width/2, Height/2);
      Resampling.UpsampleView(pRefLeft[1],UV,Width,Height,UpsampleRefs);
      Resampling.PictureResample420to444(UV, pVSIOFrmData->ColorLeft[2] , Width/2, Height/2);
      Resampling.UpsampleView(pRefLeft[2],UV,Width,Height,UpsampleRefs);

      Resampling.UpsampleView(pRefRight[0],pVSIOFrmData->ColorRight[0],Width,Height,UpsampleRefs);
      Resampling.PictureResample420to444(UV, pVSIOFrmData->ColorRight[1] , Width/2, Height/2);
      Resampling.UpsampleView(pRefRight[1],UV,Width,Height,UpsampleRefs);
      Resampling.PictureResample420to444(UV, pVSIOFrmData->ColorRight[2] , Width/2, Height/2);
      Resampling.UpsampleView(pRefRight[2],UV,Width,Height,UpsampleRefs);

      Resampling.UpsampleView(pRefDepthLeft, pVSIOFrmData->DepthLeft,Width,Height,UpsampleRefs);
      Resampling.UpsampleView(pRefDepthRight,pVSIOFrmData->DepthRight,Width,Height,UpsampleRefs);
      free_mem2D(UV);
#if QC_VSP_Asymmetric_Resolution
    }
#endif
#endif
  }
#endif // NTT_SUBPEL_VSP_M24796

#if QCOM_VSP
  if(SynDirection != 1)
  {
#if ZJU_VSP
#if QC_VSP_Asymmetric_Resolution
    if(bMixedRes)
      ViewGen_1LWH_splatting420(LEFTVIEW);
    else
#endif
      ViewGen_1LWH_splatting(LEFTVIEW);

#else
#if QC_VSP_Asymmetric_Resolution
    if(bMixedRes)
      ViewGen_1LWH420(LEFTVIEW);
    else
#endif
      ViewGen_1LWH(LEFTVIEW);

#endif

  }
  else
  {
#if ZJU_VSP
#if QC_VSP_Asymmetric_Resolution
    if(bMixedRes)
      ViewGen_1LWH_splatting420(RGHTVIEW); //2 is forbidden
    else
#endif
      ViewGen_1LWH_splatting(RGHTVIEW); //2 is forbidden

#else
#if QC_VSP_Asymmetric_Resolution
    if(bMixedRes)
      ViewGen_1LWH420(RGHTVIEW); //2 is forbidden
    else
#endif
      ViewGen_1LWH(RGHTVIEW); //2 is forbidden

#endif
  }

#else //#if QCOM_VSP
  ForwardWarp(); // pRefLeft, pRefRight, pRefDepthLeft, pRefDepthRight  
  // Merge the two synthesized view into one
#if VSP_OPTIMIZATION
  if(SynDirection==2)
    Merge();
#else
  Merge();
#endif



  // Fill holes
#if VSP_OPTIMIZATION
  FillHoles();
#else
  FillHoles(SynY[HLFLVIEW], SynY[MERGVIEW]);
  FillHoles(SynU[HLFLVIEW], SynU[MERGVIEW]);
  FillHoles(SynV[HLFLVIEW], SynV[MERGVIEW]);
  FillHoles(Dmap[HLFLVIEW], Dmap[MERGVIEW]);
#endif
#endif
  // Prepare the output


#if NTT_SUBPEL_VSP_M24796
  if(SynDirection!=2)
  {
    SynY[FINLVIEW] = Dmap[FINLVIEW] = RefView[LEFTVIEW][0] = RefDepth[LEFTVIEW] = RefView[RGHTVIEW][0] = RefDepth[RGHTVIEW] = NULL;
#if QC_VSP_Asymmetric_Resolution
    if(bMixedRes)
    {
      SynU[FINLVIEW] = SynV[FINLVIEW] = RefView[LEFTVIEW][1] = RefView[LEFTVIEW][2] =  RefView[RGHTVIEW][1] =  RefView[RGHTVIEW][2] = NULL;
    }
    else
#endif
    {
      if(pVSIOFrmData->ColorSynthesized[1]&&pVSIOFrmData->ColorSynthesized[2])
      {
        // Downsample the UV
        Resampling.PictureResample444to420(pVSIOFrmData->ColorSynthesized[1], SynU[FINLVIEW], Width/2, Height/2);
        Resampling.PictureResample444to420(pVSIOFrmData->ColorSynthesized[2], SynV[FINLVIEW], Width/2, Height/2);
      }
    }
  }
  else
  { // not sure
    if(pVSIOFrmData->ColorSynthesized[0])
    {
      for(h=0;h<Height;++h)
        memcpy(pVSIOFrmData->ColorSynthesized[0][h], SynY[HLFLVIEW][h], Width*sizeof(imgpel));
    }
    if(pVSIOFrmData->DepthSynthesized)
    {
#if QC_VSP_Asymmetric_Resolution
      if(bMixedRes)
      {
        for(h=0;h<Height/2;++h)
          memcpy(pVSIOFrmData->DepthSynthesized[h],Dmap[HLFLVIEW][h],Width*sizeof(imgpel)/2);
      }
      else
#endif
      {
        for(h=0;h<Height;++h)
          memcpy(pVSIOFrmData->DepthSynthesized[h],Dmap[HLFLVIEW][h],Width*sizeof(imgpel));
      }
    }
    if(pVSIOFrmData->ColorSynthesized[1]&&pVSIOFrmData->ColorSynthesized[2])
    {
      // Downsample the UV
#if QC_VSP_Asymmetric_Resolution
      if(bMixedRes)
      {
        for(h=0;h<Height/2;++h)
        {
          memcpy(pVSIOFrmData->ColorSynthesized[1][h], SynU[FINLVIEW][h], Width*sizeof(imgpel)/2);
          memcpy(pVSIOFrmData->ColorSynthesized[2][h], SynV[FINLVIEW][h], Width*sizeof(imgpel)/2);
        }
      }
      else
#endif
      {
        Resampling.PictureResample444to420(pVSIOFrmData->ColorSynthesized[1], SynU[HLFLVIEW], Width/2, Height/2);
        Resampling.PictureResample444to420(pVSIOFrmData->ColorSynthesized[2], SynV[HLFLVIEW], Width/2, Height/2);
      }
    }
  }
#else // NTT_SUBPEL_VSP_M24796

#if VSP_OPTIMIZATION
#if QC_VSP_Asymmetric_Resolution
  if(bMixedRes)
  Resampling.DownsampleRefView420(SynY[FINLVIEW],SynY[HLFLVIEW],
                 SynU[FINLVIEW],SynU[HLFLVIEW],
                 SynV[FINLVIEW],SynV[HLFLVIEW],
                 Dmap[FINLVIEW],Dmap[HLFLVIEW],
                 Width2*SubPelOption, Height,UpsampleRefs*SubPelOption);
  else
#endif
  Resampling.DownsampleRefView(SynY[FINLVIEW],SynY[HLFLVIEW],
                 SynU[FINLVIEW],SynU[HLFLVIEW],
                 SynV[FINLVIEW],SynV[HLFLVIEW],
                 Dmap[FINLVIEW],Dmap[HLFLVIEW],
                 Width2*SubPelOption, Height,UpsampleRefs*SubPelOption);
#else
  ScaleDownSyn(SynY[FINLVIEW],SynY[HLFLVIEW]);
  ScaleDownSyn(SynU[FINLVIEW],SynU[HLFLVIEW]);
  ScaleDownSyn(SynV[FINLVIEW],SynV[HLFLVIEW]);
  ScaleDownSyn(Dmap[FINLVIEW],Dmap[HLFLVIEW]);
  if(UpsampleRefs!=1)
  {
    Resampling.DownsampleView(SynY[FINLVIEW], SynY[FINLVIEW], Width2, Height, UpsampleRefs);
    Resampling.DownsampleView(SynU[FINLVIEW], SynU[FINLVIEW], Width2, Height, UpsampleRefs);
    Resampling.DownsampleView(SynV[FINLVIEW], SynV[FINLVIEW], Width2, Height, UpsampleRefs);
    Resampling.DownsampleView(Dmap[FINLVIEW], Dmap[FINLVIEW], Width2, Height, UpsampleRefs);
  }
#endif

  if(pVSIOFrmData->ColorSynthesized[0])
  {
    for(h=0;h<Height;++h)
      memcpy(pVSIOFrmData->ColorSynthesized[0][h], SynY[FINLVIEW][h], Width*sizeof(imgpel));
  }
  if(pVSIOFrmData->DepthSynthesized)
  {
#if QC_VSP_Asymmetric_Resolution
    if (bMixedRes)
    {
      for(h=0;h<Height/2;++h)
        memcpy(pVSIOFrmData->DepthSynthesized[h],Dmap[FINLVIEW][h],Width*sizeof(imgpel));
    }else{
#endif
      for(h=0;h<Height;++h)
        memcpy(pVSIOFrmData->DepthSynthesized[h],Dmap[FINLVIEW][h],Width*sizeof(imgpel));
#if QC_VSP_Asymmetric_Resolution
    }
#endif
  }

#if QC_VSP_Asymmetric_Resolution
  //no need to downsample, just copy
  if(bMixedRes && pVSIOFrmData->ColorSynthesized[1] && pVSIOFrmData->ColorSynthesized[2])
  {
    for(h=0;h<Height/2;++h)
    {
      memcpy(pVSIOFrmData->ColorSynthesized[1][h], SynU[FINLVIEW][h], Width*sizeof(imgpel)/2);
      memcpy(pVSIOFrmData->ColorSynthesized[2][h], SynV[FINLVIEW][h], Width*sizeof(imgpel)/2);
    }
  }else
#endif
  if(pVSIOFrmData->ColorSynthesized[1]&&pVSIOFrmData->ColorSynthesized[2])
  {
    // Downsample the UV
    Resampling.PictureResample444to420(pVSIOFrmData->ColorSynthesized[1], SynU[FINLVIEW], Width/2, Height/2);
    Resampling.PictureResample444to420(pVSIOFrmData->ColorSynthesized[2], SynV[FINLVIEW], Width/2, Height/2);
  }

#endif // NTT_SUBPEL_VSP_M24796

  //printf("<<<<<<<<<<<<\n");

#if 0 // debug
  {
    FILE *fout;
    fout = fopen("debug.yuv", "ab");
    for(h=0; h<Height; h++)
      fwrite(pVSIOFrmData->ColorSynthesized[0][h], Width, 1, fout);
    for(h=0; h<Height/2; h++)
      fwrite(pVSIOFrmData->ColorSynthesized[1][h], Width/2, 1, fout);
    for(h=0; h<Height/2; h++)
      fwrite(pVSIOFrmData->ColorSynthesized[2][h], Width/2, 1, fout);
    fclose(fout);
  }
#endif

  return 0;
}

#if QC_VSP_Asymmetric_Resolution
void CViewInterpolation1D::FindDepthMaxMin420( byte** map, int ViewID)
{
  int i, j;
  //double z;
  Maxdk = Mindk = 0;
  Maxz = 0;
  Minz = 255;

  for(j=0; j<Height/2; j=j+8)
  {
#if NTT_SUBPEL_VSP_M24796
    for(i=0; i<Width/2; i=i+8)
#else
    for(i=0; i<Width2/2; i=i+8)
#endif
    {
      if( map[j][i] > Maxz)
        Maxz = map[j][i];
      else if( map[j][i] < Minz)   
        Minz = map[j][i];
    }
  }

#if VSP_OPTIMIZATION
#if NTT_SUBPEL_VSP_M24796
  Maxdk=DisparityLookupTable[ViewID][Maxz].integer+1;//ceiling
  Mindk=DisparityLookupTable[ViewID][Minz].integer;//flooring
#else
  Maxdk=(int)(DisparityLookupTable[ViewID][Maxz].db_disparity+1);//ceiling
  Mindk=(int)(DisparityLookupTable[ViewID][Minz].db_disparity+0);//flooring
#endif
#else
#if POZNAN_NONLINEAR_DEPTH
        int maxi = nonlinear_depth_lut_backward[Maxz];        
#else
        int maxi = Maxz;
#endif
  z = (double)1.0 / ( (maxi/255.0) * (1.0/Znear[ViewID] - 1.0/Zfar[ViewID]) + (1.0/Zfar[ViewID]) );
  Maxdk = (int) ( FocalLength * LTranslation[ViewID] / z - fabs(duPrincipal[ViewID]) + 1); //ceilin
#if POZNAN_NONLINEAR_DEPTH
        int mini = nonlinear_depth_lut_backward[Minz];            
#else   
        int mini = Minz;
#endif
  z = (double)1.0 / ( (mini/255.0) * (1.0/Znear[ViewID] - 1.0/Zfar[ViewID]) + (1.0/Zfar[ViewID]) );
  Mindk = (int) ( FocalLength * LTranslation[ViewID] / z - fabs(duPrincipal[ViewID]) + 0); //flooring
#endif

  //UpsampleRefs, SubPelOption
#if NTT_SUBPEL_VSP_M24796
  Maxdk = Maxdk;
  Mindk = Mindk;
#else
  Maxdk = Maxdk * UpsampleRefs;
  Mindk = Mindk * UpsampleRefs;
#endif

  SplattingDepthThreshold = (int) ( (Maxz-Minz) *0.6 + Minz );
}
#endif
//Zhejiang
//FindDepthMaxMin -- search less point to get the approximate Zmax and Zmin
void CViewInterpolation1D::FindDepthMaxMin( byte** map, int ViewID)
{
  int i, j;
  //double z;
  Maxdk = Mindk = 0;
  Maxz = 0;
  Minz = 255;

  for(j=0; j<Height; j=j+16)
  {
    for(i=0; i<Width2; i=i+16)
    {
      if( map[j][i] > Maxz)
        Maxz = map[j][i];
      else if( map[j][i] < Minz)   
        Minz = map[j][i];
    }
  }

#if VSP_OPTIMIZATION
#if NTT_SUBPEL_VSP_M24796
  Maxdk=DisparityLookupTable[ViewID][Maxz].integer+1;//ceiling
  Mindk=DisparityLookupTable[ViewID][Minz].integer;//flooring
#else
  Maxdk=(int)(DisparityLookupTable[ViewID][Maxz].db_disparity+1);//ceiling
  Mindk=(int)(DisparityLookupTable[ViewID][Minz].db_disparity+0);//flooring
#endif
#else
#if POZNAN_NONLINEAR_DEPTH
        int maxi = nonlinear_depth_lut_backward[Maxz];        
#else
        int maxi = Maxz;
#endif
  z = (double)1.0 / ( (maxi/255.0) * (1.0/Znear[ViewID] - 1.0/Zfar[ViewID]) + (1.0/Zfar[ViewID]) );
  Maxdk = (int) ( FocalLength * LTranslation[ViewID] / z - fabs(duPrincipal[ViewID]) + 1); //ceilin
#if POZNAN_NONLINEAR_DEPTH
        int mini = nonlinear_depth_lut_backward[Minz];            
#else   
        int mini = Minz;
#endif
  z = (double)1.0 / ( (mini/255.0) * (1.0/Znear[ViewID] - 1.0/Zfar[ViewID]) + (1.0/Zfar[ViewID]) );
  Mindk = (int) ( FocalLength * LTranslation[ViewID] / z - fabs(duPrincipal[ViewID]) + 0); //flooring
#endif

  //UpsampleRefs, SubPelOption
#if NTT_SUBPEL_VSP_M24796
  Maxdk = Maxdk;
  Mindk = Mindk;
#else
  Maxdk = Maxdk * UpsampleRefs;
  Mindk = Mindk * UpsampleRefs;
#endif

  SplattingDepthThreshold = (int) ( (Maxz-Minz) *0.6 + Minz );
}

//2009.05.04, improvement, Zhejiang University(a little different from m16041: Temporal Improvement Method in View Synthesis)
//-if a block is moving, then its neigbouring 4*4 blocks are also dertermined to be moving, 
//--since the object boundary on depth map is not restrict to depth boundary.
void CViewInterpolation1D::TemporalImprovementMethod( imgpel**ImageCur, imgpel**ImageLast,
                            imgpel**DepthCur, imgpel**DepthLast,
                            int frame, int iWidth, int iHeight )
{
  int SAD_TH = 100;     //threshold of sad in 4*4 block motion detection
  double weight = 0.75;  //weight value for depth map weighting equation


  //internal variables
  int diff = 0;
  int s = 0;
  byte *flag=NULL;
  int t = 0;
  int h=0;

  flag = (byte*)malloc(iWidth/4);

  if( 0 == frame ) // first frame need not the weighting, but need to prepare the data
  {
    for(h=0;h<iHeight;++h)
    {
      memcpy(ImageLast[h], ImageCur[h], iWidth*sizeof(imgpel));
      memcpy(DepthLast[h], DepthCur[h], iWidth*sizeof(imgpel));
    }
  }
  else
  {
    for ( int y = 0; y < iHeight/4; y++)//!<height
    {
      //motion detection by SSE -- do a line
      for ( int x = 0; x < iWidth/4;  x++)//!<width
      {
        s = 0;
        for( int j = 0; j < 4; j++)   //!<height
        {
          for(int i = 0; i < 4; i++) //!<weight
          {
            diff=(ImageCur[(y<<2)+j][(i<<2)+i]-ImageLast[(y<<2)+j][(i<<2)+i]);
            s +=abs(diff);   //SAD
          }
        }
        if(s<SAD_TH) 
          flag[x]=0;
        else         
          flag[x]=1;
      }
      //temporal weighting according to motion detection result -- do a line
      for ( int x = 0; x < iWidth/4;  x++)
      {

#if TEMPOBLOCK3
        //3 block
        if(x == 0)          t = flag[x] + flag[x+1];
        else if( x == iWidth/4 -1)   t = flag[x-1] + flag[x];
        else            t = flag[x-1] + flag[x] + flag[x+1];
#else
        //5 block
        if(x == 0)                t = flag[x]   + flag[x+1] + flag[x+2];
        else if( x == 1)          t = flag[x-1] + flag[x]   + flag[x+1] + flag[x+2];
        else if( x == iWidth/4-2) t = flag[x-2] + flag[x-1] + flag[x]   + flag[x+1];
        else if( x == iWidth/4-1) t = flag[x-2] + flag[x-1] + flag[x];
        else                      t = flag[x-2] + flag[x-1] + flag[x]   + flag[x+1] + flag[x+2];
#endif

        if( t == 0 )// if not moving
        {
          for( int j = 0; j < 4; j++)
          {
            for( int i = 0; i < 4; i++)
            {
              DepthCur[(y<<2)+j][(x<<2)+i]=
              (imgpel)( DepthLast[(y<<2)+j][(x<<2)+i] * weight 
              + DepthCur[(y<<2)+j][(x<<2)+i] * ( 1 - weight ) );
            }
          }
        }
        //else, do nothing
      }
    }

    for(h=0;h<iHeight;++h)
    {
      memcpy(ImageLast[h], ImageCur[h], iWidth*sizeof(imgpel));
      memcpy(DepthLast[h], DepthCur[h], iWidth*sizeof(imgpel));
    }
  }
}

#if QC_VSP_Asymmetric_Resolution
void CViewInterpolation1D::TemporalImprovementMethod420( imgpel**ImageCur, imgpel**ImageLast,
                            imgpel**DepthCur, imgpel**DepthLast,
                            int frame, int iWidth, int iHeight )
{
  int SAD_TH = 100;     //threshold of sad in 4*4 block motion detection
  double weight = 0.75;  //weight value for depth map weighting equation


  //internal variables
  int diff = 0;
  int s = 0;
  byte *flag=NULL;
  int t = 0;
  int h=0;

  flag = (byte*)malloc(iWidth/4);

  if( 0 == frame ) // first frame need not the weighting, but need to prepare the data
  {
    for(h=0;h<iHeight;++h)
    {
      memcpy(ImageLast[h], ImageCur[h], iWidth*sizeof(imgpel));
      
    }
    for(h=0;h<iHeight/2;++h)
    {
      memcpy(DepthLast[h], DepthCur[h], iWidth*sizeof(imgpel)/2);
    }
  }
  else
  {
    for ( int y = 0; y < iHeight/4; y++)//!<height
    {
      //motion detection by SSE -- do a line
      for ( int x = 0; x < iWidth/4;  x++)//!<width
      {
        s = 0;
        for( int j = 0; j < 4; j++)   //!<height
        {
          for(int i = 0; i < 4; i++) //!<weight
          {
            diff=(ImageCur[(y<<2)+j][(i<<2)+i]-ImageLast[(y<<2)+j][(i<<2)+i]);
            s +=abs(diff);   //SAD
          }
        }
        if(s<SAD_TH) 
          flag[x]=0;
        else         
          flag[x]=1;
      }
      //temporal weighting according to motion detection result -- do a line
      for ( int x = 0; x < iWidth/4;  x++)
      {

#if TEMPOBLOCK3
        //3 block
        if(x == 0)          t = flag[x] + flag[x+1];
        else if( x == iWidth/4 -1)   t = flag[x-1] + flag[x];
        else            t = flag[x-1] + flag[x] + flag[x+1];
#else
        //5 block
        if(x == 0)                t = flag[x]   + flag[x+1] + flag[x+2];
        else if( x == 1)          t = flag[x-1] + flag[x]   + flag[x+1] + flag[x+2];
        else if( x == iWidth/4-2) t = flag[x-2] + flag[x-1] + flag[x]   + flag[x+1];
        else if( x == iWidth/4-1) t = flag[x-2] + flag[x-1] + flag[x];
        else                      t = flag[x-2] + flag[x-1] + flag[x]   + flag[x+1] + flag[x+2];
#endif

        if( t == 0 )// if not moving
        {
          for( int j = 0; j < 2; j++)
          {
            for( int i = 0; i < 2; i++)
            {
              DepthCur[(y<<1)+j][(x<<1)+i]=
              (imgpel)( DepthLast[(y<<1)+j][(x<<1)+i] * weight 
              + DepthCur[(y<<1)+j][(x<<1)+i] * ( 1 - weight ) );
            }
          }
        }
        //else, do nothing
      }
    }

    for(h=0;h<iHeight;++h)
    {
      memcpy(ImageLast[h], ImageCur[h], iWidth*sizeof(imgpel));
      
    }
    for(h=0;h<iHeight/2;++h)
    {
      memcpy(DepthLast[h], DepthCur[h], iWidth*sizeof(imgpel)/2);
    }
  }
}

#endif

