;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; LUSH Lisp Universal Shell
;;;   Copyright (C) 2002 Leon Bottou, Yann Le Cun, AT&T Corp, NECI.
;;; Includes parts of TL3:
;;;   Copyright (C) 1987-1999 Leon Bottou and Neuristique.
;;; Includes selected parts of SN3.2:
;;;   Copyright (C) 1991-2001 AT&T Corp.
;;;
;;; Includes information derived from the GSL (Gnu Scientific Library)
;;;   header files which are copyrighted and distributed under the GPL
;;;   by their respective authors.
;;;
;;; This program is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 2 of the License, or
;;; (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program; if not, write to the Free Software
;;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA
;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(libload "opencv/cv-config")



#? *** undistortion

;; dummy function that adds OpenCV C header files in C file
(eval `(de dummy_undistortion () ,cv-cpheader-cmd ()))

;; #? * (dummy_undistortion)
;; #? * (cvundistortonce <srcimage> <dstimage> <intrmatrix> <distcoeffs> <interpolate>)
;; #? * (cvundistortinit <srcimage> <undistmap> <intrmatrix> <distcoeffs> <interpolate>)
;; #? * (cvundistort <srcimage> <dstimage> <undistmap> <interpolate>)
;; #? * (cvremap <srcimage> <dstimage> <undistmap> <interpolate>)

;; ********************************************************

;;  /****************************************************************************************\
;;  *                               Undistortion functions                                  *
;;  \****************************************************************************************/

#? (cvInitUndistortMap <intrinsicmat> <distcoeffs> <mapx> <mapy>)
;;.VP
;;  OPENCVAPI  void  cvInitUndistortMap( const CvMat* intrinsicMatrix,
;;                                       const CvMat* distortionCoeffs,
;;                                       CvArr*       mapX,
;;                                       CvArr*       mapY);

(de cvInitUndistortMap(intrinsicmat distortion mapx mapy)
    ((-gptr- "CvArr *") mapx mapy)
    ((-gptr- "const CvMat *") intrinsicmat distortion)
    #{ cvInitUndistortMap($intrinsicmat, $distortion, $mapx, $mapy); #}
    ()
)


#? (cvUndistort2 <srcimage> <dstimage> <intrinsicmat> <distortion>)
;;.VP
;;  OPENCVAPI  void  cvUndistort2( const CvArr* srcImage,
;;                                 CvArr*       dstImage,
;;                                 const CvMat* intrinsicMatrix,
;;                                 const cvMat* distortionCoeffs);

(de cvUndistort2(srcimage dstimage intrinsicmat distortion)
    ((-gptr- "const CvArr *") srcimage)
    ((-gptr- "CvArr *") dstimage)
    ((-gptr- "const CvMat *") intrinsicmat distortion)
    #{ cvUndistort2($srcimage, $dstimage, $intrinsicmat, $distortion); #}
    ()
)

#? (cvWarpPerspective <src> <dst> <mapmatrix> <flags> <fillval>)
;;.VP
;;  OPENCVAPI  void  cvWarpPerspective( const CvArr* src,
;;                                      CvArr* dst,
;;                                      const CvMat* map_matrix,
;;                                      int flags=CV_INTER_LINEAR
;;                                      +CV_WARP_FILL_OUTLIERS,
;;                                      CvScalar fillval=cvScalarAll(0));

(de cvWarpPerspective (src dst mapmatrix flags fillval)
    ((-gptr- "const CvArr *") src dst)
    ((-gptr- "const CvMat *") mapmatrix)
    ((-gptr- "CvScalar *") fillval)
    ((-int-) flags)
    #{ cvWarpPerspective($src, $dst, $mapmatrix, $flags, *$fillval); #}
    ()
)


#? (cvWarpPerspectiveQMatrix <src> <dst> <mapmatrix>)
;;.VP
;;  OPENCVAPI  void  cvWarpPerspectiveQMatrix( const CvPoint2D32f* src,
;;                                             const CvPoint2D32f* dst,
;;                                             CvMat* map_matrix);

(de cvWarpPerspectiveQMatrix (src dst mapmatrix)
    ((-gptr- "const CvPoint2D32f *") src dst)
    ((-gptr- "CvMat *") mapmatrix)
    #{ cvWarpPerspectiveQMatrix($src, $dst, $mapmatrix); #}
    ()
)


#? (cvRemap <src> <dst> <mapx> <mapy> <flags> <fillval>)
;;.VP
;;  OPENCVAPI  void  cvRemap( const CvArr* src, CvArr* dst,
;;                            const CvArr* mapx, const CvArr* mapy,
;;                            int flags=CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS,
;;                            CvScalar fillval=cvScalarAll(0) );

(de cvRemap (src dst mapx mapy flags fillval)
    ((-gptr- "const CvArr *") src mapx mapy)
    ((-gptr- "CvArr *") dst)
    ((-gptr- "CvScalar *") fillval)
    ((-int-) flags)
    #{ cvRemap($src, $dst, $mapx, $mapy, $flags, *$fillval); #}
    ()
)

#? (cvUnDistortOnce <srcimage> <dstimage> <intrmatrix> <distcoeffs> <interpolate>)
;;.VP
;;  /*F//////////////////////////////////////////////////////////////////////////////////////
;;  //    Name: cvUnDistortOnce
;;  //    Purpose: The function corrects radial and tangential image distortion using known
;;  //             matrix of the camera intrinsic parameters and distortion coefficients
;;  //    Context:
;;  //    Parameters:  srcImage    - source(distorted) image
;;  //                 dstImage    - output(undistorted) image
;;  //                 intrMatrix  - matrix of the camera intrinsic parameters
;;  //                 distCoeffs  - vector of the distortion coefficients(k1, k2, p1 and p2)
;;  //                 interpolate - interpolation flag (turned on by default)
;;  //F*/
;;  OPENCVAPI  void  cvUnDistortOnce( const CvArr* srcImage, CvArr* dstImage,
;;                                    const float* intrMatrix,
;;                                    const float* distCoeffs,
;;                                    int interpolate CV_DEFAULT(1) );

(de cvUnDistortOnce(srcimage dstimage intrmatrix distcoeffs interpolate)
    ((-gptr- "const CvArr *") srcimage)
    ((-gptr- "CvArr *") dstimage)
    ((-gptr- "const float *") intrmatrix distcoeffs)
    ((-int-) interpolate)
    #{ cvUnDistortOnce($srcimage, $dstimage, $intrmatrix, $distcoeffs, $interpolate); #}
    ())

#? (cvUnDistortInit <srcimage> <undistmap> <intrmatrix> <distcoeffs> <interpolate>)
;;.VP
;;  /*F//////////////////////////////////////////////////////////////////////////////////////
;;  //    Name: cvUnDistortInit
;;  //    Purpose: The function calculates arrays of distorted points indices and
;;  //             interpolation coefficients for cvUnDistort function using known
;;  //             matrix of the camera intrinsic parameters and distortion coefficients
;;  //    Context:
;;  //    Parameters:  srcImage    - source(distorted) image
;;  //                 intrMatrix  - matrix of the camera intrinsic parameters
;;  //                 distCoeffs  - vector of the distortion coefficients(k1, k2, p1 and p2)
;;  //                 undistMap   - distortion data array (CV_32SC1)
;;  //                 interpolate - interpolation flag (turned on by default)
;;  //F*/
;;  OPENCVAPI  void  cvUnDistortInit( const CvArr* srcImage, CvArr* undistMap,
;;                                    const float* intrMatrix,
;;                                    const float* distCoeffs,
;;                                    int interpolate CV_DEFAULT(1) );

(de cvUnDistortInit(srcimage undistmap intrmatrix distcoeffs interpolate)
    ((-gptr- "const CvArr *") srcimage)
    ((-gptr- "CvArr *") undistmap)
    ((-gptr- "const float *") intrmatrix distcoeffs)
    ((-int-) interpolate)
    #{ cvUnDistortInit($srcimage, $undistmap, $intrmatrix, $distcoeffs, $interpolate); #}
    ())

#? (cvUnDistort <srcimage> <dstimage> <undistmap> <interpolate>)
;;.VP
;;  /*F//////////////////////////////////////////////////////////////////////////////////////
;;  //    Name: cvUnDistort
;;  //    Purpose: The function corrects radial and tangential distortion in the frame
;;  //             using previousely calculated arrays of distorted points indices and
;;  //             undistortion coefficients. The function can be also used
;;  //             for arbitrary pre-calculated geometrical transformation.
;;  //             The function processes as following:
;;  //                 for (x,y) in dstImage:
;;  //                    dstImage(x,y) = srcImage[ undistMap(x,y) ].
;;  //    Context:
;;  //    Parameters:  srcImage  - source(distorted) image (width x height x 8uC1/8uC3)
;;  //                 dstImage  - output(undistorted) image (width x height x 8uC1/8uC3)
;;  //                 undistMap - distortion data array:
;;  //                                 (width x height x 32sC3) or
;;  //                                 (width*3 x height x 32sC1) if interpolation is enabled;
;;  //                                 (width x height x 32sC1) if interpolation is disabled;
;;  //                             This array can be calculated from camera
;;  //                             lens distortion parameters using cvUnDistortInit or
;;  //                             from arbitrary floating-point map using cvConvertMap
;;  //                 interpolate - interpolation flag (turned on by default)
;;  //F*/
;;  OPENCVAPI  void  cvUnDistort( const CvArr* srcImage, CvArr* dstImage,
;;                                const CvArr* undistMap, int interpolate CV_DEFAULT(1));
;;  #define cvRemap cvUnDistort


(de cvUnDistort(srcimage dstimage undistmap interpolate)
    ((-gptr- "const CvArr *") srcimage undistmap)
    ((-gptr- "CvArr *") dstimage)
    ((-int-) interpolate)
    #{ cvUnDistort($srcimage, $dstimage, $undistmap, $interpolate); #}
    ())

;; ********************************************************

(dhc-make ()
  dummy_undistortion
  ;; added or changed
  cvundistort2
  cvinitundistortmap
  cvwarpperspective
  cvwarpperspectiveqmatrix
  cvremap
  ;;
  cvundistortonce
  cvundistortinit
  cvundistort
)
