;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; 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>)
;; #? * (cvconvertmap <srcimage> <flundistmap> <undistmap> <interpolate>)

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

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

#? (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); #}
    ())

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

#? (cvConvertMap <srcimage> <flundistmap> <undistmap> <interpolate>)
;;.VP
;;  /*F//////////////////////////////////////////////////////////////////////////////////////
;;  //    Name: cvConvertMap
;;  //    Purpose: The function converts floating-point pixel coordinate map to
;;  //             faster fixed-point map, used within cvUnDistort (cvRemap)
;;  //    Context:
;;  //    Parameters:  srcImage  - sample image which header parameters (step and datatype)
;;  //                             are used to prepare map.
;;  //                 flUndistMap - source map: (width x height x 32fC2) or
;;  //                                           (width*2 x height x 32fC1).
;;  //                               each pair are pixel coordinates (x,y) in a source image.
;;  //                 undistMap - resultant map: (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;
;;  //                 interpolate - interpolation flag (turned on by default)
;;  //F*/
;;  OPENCVAPI  void  cvConvertMap( const CvArr* srcImage, const CvArr* flUndistMap,
;;                                 CvArr* undistMap, int iterpolate CV_DEFAULT(1) );

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

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

(dhc-make ()
  dummy_undistortion
  cvundistortonce
  cvundistortinit
  cvundistort
  cvremap
  cvconvertmap
)
