bufInquire(pMilSrcImages[SrcIdx], M_HOST_ADDRESS, M_NULL);
      MosPrintf(MIL_TEXT("CS3D: Loading the image from grab %i in the CS3D API..."), SrcIdx);
      if(p3DApi->setSrcImgPtr(SrcIdx, pImageData) < 0)
         MosPrintf(MIL_TEXT("Image info not acceptable for calculation.\n"));
      else
         MosPrintf(MIL_TEXT("Done.\n"));
      p3DApi->setSrcImgLoaded(SrcIdx);
      }
   MosPrintf(MIL_TEXT("\n"));

   // Get the resulting image.
   MosPrintf(MIL_TEXT("CS3D: Calculating the depth map..."));
   p3DApi->getNextImgBlocking();

   void* pDisparityData = (void*)MbufInquire(MilDisparityImage, M_HOST_ADDRESS, M_NULL);
   int DisparityPitchByte = (int)MbufInquire(MilDisparityImage, M_PITCH_BYTE, M_NULL);
   p3DApi->getLastImage(&pDisparityData, DisparityPitchByte, IMG_OUT_DISP);
   
   if(MilRectifiedImage)
      {
      void* pRectifiedData = (void*)MbufInquire(MilRectifiedImage, M_HOST_ADDRESS, M_NULL);
      int RectifiedPitchByte = (int)MbufInquire(MilRectifiedImage, M_PITCH_BYTE, M_NULL);
      MIL_INT SizeBand = MbufInquire(MilRectifiedImage, M_SIZE_BAND, M_NULL);
      p3DApi->getLastImage(&pRectifiedData, (int)RectifiedPitchByte, SizeBand == 1? IMG_OUT_GRAY : IMG_OUT_BGRA);
      }
   MosPrintf(MIL_TEXT("Done\n\n"));

   // Get only the workable area of the disparity map.
   MIL_INT WorkSizeX = MbufInquire(MilCorrectedWorkDepthMap, M_SIZE_X, M_NULL);
   MIL_INT WorkSizeY = MbufInquire(MilCorrectedWorkDepthMap, M_SIZE_Y, M_NULL);
   MbufCopyColor2d(MilDisparityImage, MilCorrectedWorkDepthMap, 0, BORDER_SIZE_X, 0, 0, 0, 0, WorkSizeX, WorkSizeY);
   MIL_ID MilSourceRectifiedImage = MilRectifiedImage == 0? MilDisparityImage : MilRectifiedImage;
   MbufCopyColor2d(MilSourceRectifiedImage, MilCorrectedWorkColorMap, M_ALL_BANDS, BORDER_SIZE_X, 0, M_ALL_BANDS, 0, 0, WorkSizeX, WorkSizeY);

   // Show the disparity image.
   MosPrintf(MIL_TEXT("The depth map is displayed.\n\n")
             MIL_TEXT("Press <Enter> to continue.\n\n"));
   if(NbSrcImage == 2)
      MdispSelect(pMilDisplays[1], M_NULL);
   ShowImage(pMilDisplays[0], MilCorrectedWorkDepthMap, true);
   }


//*****************************************************************************
// FillHolesAndSmooth. Smooths the depth map and fills its hole. Invalid pixels
//                     whose neighborhood contains at least 10% of valid pixels
//                     are replaced by the average of the valid neighbors.
//*****************************************************************************
void FillHolesAndSmooth(MIL_ID MilDisplay, MIL_ID MilDepthMap, MIL_ID MilFilledHolesDepthMap, MIL_INT FilterSize)
   {
   MIL_ID MilSystem = MbufInquire(MilDepthMap, M_OWNER_SYSTEM, M_NULL);
   MIL_INT ImageSizeX = MbufInquire(MilDepthMap, M_SIZE_X, M_NULL);
   MIL_INT ImageSizeY = MbufInquire(MilDepthMap, M_SIZE_Y, M_NULL);

   // Allocate the work images.
   MIL_ID MilValidImage = MbufAlloc2d(MilSystem, ImageSizeX, ImageSizeY, 16+M_UNSIGNED, M_IMAGE+M_PROC, M_NULL);
   MIL_ID MilNormImage = MbufAlloc2d(MilSystem, ImageSizeX, ImageSizeY, 16+M_UNSIGNED, M_IMAGE+M_PROC, M_NULL);
   MIL_ID MilTempFilledHolesDepthMap32 = MbufAlloc2d(MilSystem, ImageSizeX, ImageSizeY, 32 + M_UNSIGNED, M_IMAGE + M_PROC, M_NULL);

   // Create the valid image.
   MimBinarize(MilDepthMap, MilValidImage, M_FIXED + M_GREATER, 0, M_NULL);

   MIL_ID MilUniformKernel = MbufAlloc2d(MilSystem, FilterSize, FilterSize, 8+M_UNSIGNED, M_KERNEL, M_NULL);
   MbufClear(MilUniformKernel, 1);
   MbufControl(MilUniformKernel, M_NORMALIZATION_FACTOR, FilterSize*FilterSize);
   MimConvolve(MilValidImage, MilNormImage, MilUniformKernel);
   MimConvolve(MilDepthMap, MilFilledHolesDepthMap, MilUniformKernel);
   
   // Normalize the values.
   MimShift(MilFilledHolesDepthMap, MilTempFilledHolesDepthMap32, 16);
   MimArith(MilTempFilledHolesDepthMap32, MilNormImage, MilFilledHolesDepthMap, M_DIV);

   // Invalidate the data of values that do not have enough valid values in their neighborhood.
   MimBinarize(MilNormImage, MilValidImage, M_GREATER, 0.1 * MIL_UINT16_MAX, M_NULL);
   MbufClearCond(MilFilledHolesDepthMap, MIL_UINT16_MAX, M_NULL, M_NULL, MilValidImage, M_EQUAL, 0);

   // Show the depth map without the holes.
   MosPrintf(MIL_TEXT("The depth map was smoothed and its holes were filled.\n\n")
             MIL_TEXT("Press <Enter> to continue.\n\n"));
   ShowImage(MilDisplay, MilFilledHolesDepthMap, true);

   MbufFree(MilUniformKernel);
   MbufFree(MilTempFilledHolesDepthMap32);
   MbufFree(MilNormImage);
   MbufFree(MilValidImage);
   }

//*****************************************************************************
// CorrectHorizontalCurve. Corrects the horizontal curve of the depth map, 
//                         assuming that it should be flat.
//*****************************************************************************
void CorrectHorizontalCurve(MIL_ID MilDepthMap, MIL_INT ChildOffsetY, MIL_INT ChildSizeY)
   {
   MIL_ID MilSystem = MbufInquire(MilDepthMap, M_OWNER_SYSTEM, M_NULL);
   MIL_INT ImageSizeX = MbufInquire(MilDepthMap, M_SIZE_X, M_NULL);
   MIL_INT ImageSizeY = MbufInquire(MilDepthMap, M_SIZE_Y, M_NULL);
   
   // Allocate the temporary images and get the child.
   MIL_ID MilCorrectionSourceChild = MbufChild2d(MilDepthMap, 0, ChildOffsetY, ImageSizeX, ChildSizeY, M_NULL);
   MIL_ID MilAverageColumn = MbufAlloc2d(MilSystem, ImageSizeX, 1, 16+M_UNSIGNED, M_IMAGE+M_PROC, M_NULL);
   MIL_ID MilAverageImage = MbufAlloc2d(MilSystem, ImageSizeX, ImageSizeY, 16+M_UNSIGNED, M_IMAGE+M_PROC, M_NULL);
   
   // Get the average of each column to get an estimation of the horizontal curve.
   MimResize(MilCorrectionSourceChild, MilAverageColumn, M_FILL_DESTINATION, M_FILL_DESTINATION, M_AVERAGE);
   MimResize(MilAverageColumn, MilAverageImage, M_FILL_DESTINATION, M_FILL_DESTINATION, M_NEAREST_NEIGHBOR);
   
   // Remove the curve.
   McalAssociate(MilDepthMap, MilAverageImage, M_DEFAULT);
   M3dmapArith(MilDepthMap, MilAverageImage, MilDepthMap,