;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; 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")



#? *** utilities

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

#? * (dummy_utilities)
#? * (cvinitlineiterator <array> <pt1> <pt2> <lineiterator> <connectivity>)
#? * (cvsampleline <array> <pt1> <pt2> <buffer> <connectivity>)
#? * (cvgetrectsubpix <src> <dst> <center>)
#? * (cvgetquadranglesubpix <src> <dstarr> <matrixarr> <filloutliers> <fillvalue>)
#? * (cvcvtpixtoplane <src> <dst0> <dst1> <dst2> <dst3>)
#? * (cvcvtplanetopix <src0> <src1> <src2> <src3> <dst>)
#? * (cvconvertscale <src> <dst> <scale> <shift>)
#? * (cvcvtscale <src> <dst> <scale> <shift>)
#? * (cvscale <src> <dst> <scale> <shift>)
#? * (cvconvertscaleabs <src> <dst> <scale> <shift>)
#? * (cvcvtscaleabs <src> <dst> <scale> <shift>)
#? * (cvabsdiff <srca> <srcb> <dst>)
#? * (cvabsdiffs <src> <dst> <value>)
#? * (cvmatchtemplate <array> <templ> <result> <method>)
#? * (cvbfastarctan <y> <x> <angle> <len>)
#? * (cvbcarttopolar <y> <x> <magnitude> <angle> <len>)
#? * (cvbsqrt <x> <sqrtx> <len>)
#? * (cvcheckarr <arr> <flags> <minval> <maxval>)
#? * (cvbinvsqrt <x> <invsqrtx> <len>)
#? * (cvbreciprocal <x> <invx> <len>)
#? * (cvbfastexp <x> <expx> <len>)
#? * (cvbfastlog <x> <logx> <len>)
#? * (cvrandinit <state> <lower> <upper> <seed>)
#? * (cvrandsetrange <state> <lower> <upper> <index>)
#? * (cvbrand <state> <dst> <len>)
#? * (cvrand <state> <arr>)
#? * (cvrandnext <state>)

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

;;  /****************************************************************************************\
;;  *                                       Utilities                                        *
;;  \****************************************************************************************/


#? (cvInitLineIterator <array> <pt1> <pt2> <lineiterator> <connectivity>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //
;;  //    Name:    cvInitLineIterator
;;  //    Purpose:
;;  //      Initializes iterator that gets all the pixels, lying on the raster line between
;;  //      two given points
;;  //    Context:
;;  //    Parameters:
;;  //        img  - image.
;;  //        pt1  - starting point
;;  //        pt2  - ending point. Both points must be inside the image
;;  //        lineIterator - pointer to initialized iterator state
;;  //    Returns:
;;  //        number of pixels between pt1 and pt2.
;;  //        It is equal to max( abs(pt1.x - pt2.x), abs(pt1.y - pt2.y))
;;  //F*/
;;  OPENCVAPI  int  cvInitLineIterator( const CvArr* array, CvPoint pt1, CvPoint pt2,
;;                                      CvLineIterator* lineIterator,
;;                                      int connectivity CV_DEFAULT(8));

(de cvInitLineIterator(array pt1 pt2 lineiterator connectivity)
    ((-gptr- "const CvArr *") array)
    ((-gptr- "CvPoint *") pt1 pt2)
    ((-gptr- "CvLineIterator *") lineiterator)
    ((-int-) connectivity)
    (let ((s 0))
      ((-int-) s)
      #{  $s = cvInitLineIterator($array, *$pt1, *$pt2, $lineiterator, $connectivity, 0); #}
      s))


#? (cvSampleLine <array> <pt1> <pt2> <buffer> <connectivity>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //
;;  //    Name:    cvSampleLine
;;  //    Purpose:
;;  //      Fetch all the pixel, lying on the raster line between two given points and
;;  //      writes them to the buffer
;;  //    Context:
;;  //    Parameters:
;;  //        img  - image.
;;  //        pt1  - starting point
;;  //        pt2  - ending point. Both points must be inside the image
;;  //        buffer - pointer to destination buffer.
;;  //    Returns:
;;  //        number of pixels stored.
;;  //        It is equal to max( abs(pt1.x - pt2.x), abs(pt1.y - pt2.y))
;;  //F*/
;;  OPENCVAPI  int  cvSampleLine( const CvArr* array, CvPoint pt1, CvPoint pt2, void* buffer,
;;                                int connectivity CV_DEFAULT(8));

(de cvSampleLine(array pt1 pt2 buffer connectivity)
    ((-gptr- "const CvArr *") array)
    ((-gptr- "CvPoint *") pt1 pt2)
    ((-gptr-) buffer)
    ((-int-) connectivity)
    (let ((s 0))
      ((-int-) s)
      #{ $s = cvSampleLine($array, *$pt1, *$pt2, $buffer, $connectivity); #}
      s))

#? (cvGetRectSubPix  <src> <dst> <center>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //
;;  //    Name:    cvGetRectSubPix
;;  //    Purpose:
;;  //      Retrieves rectangle from the image with sub-pixel accuracy
;;  //    Context:
;;  //    Parameters:
;;  //        src  - source image.
;;  //        dst  - destination image.
;;  //        center - center point of the extracted rectangle.
;;  //                 Size of extracted rectangle is equal to
;;  //                 desination image ROI size.
;;  //    Returns:
;;  //F*/
;;  OPENCVAPI  void  cvGetRectSubPix( const CvArr* src, CvArr* dst, CvPoint2D32f center );

(de cvGetRectSubPix( src dst center)
    ((-gptr- "const CvArr *") src)
    ((-gptr- "CvArr *") dst)
    ((-gptr- "CvPoint2D32f *") center)
    #{  cvGetRectSubPix($src, $dst, *$center); #}
    ())

#? (cvGetQuadrangleSubPix <src> <dstarr> <matrix>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //
;;  //    Name:    cvGetQuandrangleSubPix
;;  //    Purpose:
;;  //      Retrieves rectangle from the image with sub-pixel accuracy
;;  //    Context:
;;  //    Parameters:
;;  //        src  - source image.
;;  //        dst  - destination image.
;;  //        matrix - transformation matrix (2 rows x 3 columns).
;;  //                 ( a11  a12 | b1 )      dst([x,y]') = src(A[x y]' + b)
;;  //                 ( a21  a22 | b2 )      (bilinear interpolation is used)
;;  //    Returns:
;;  //F*/
;;  OPENCVAPI  void  cvGetQuadrangleSubPix( const CvArr* src, CvArr* dstarr,
;;                                          const CvArr* matrixarr)

(de cvGetQuadrangleSubPix(src dstarr matrixarr)
    ((-gptr- "const CvArr *") src matrixarr)
    ((-gptr- "CvArr *") dstarr)
    #{ cvGetQuadrangleSubPix($src, $dstarr, $matrixarr); #}
    ())
    
#? (cvCvtPixToPlane <src> <dst0> <dst1> <dst2> <dst3>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //
;;  //    Name:    cvCvtPixToPlane
;;  //    Purpose:
;;  //      Splits source image into several separate planes
;;  //    Context:
;;  //    Parameters:
;;  //        src  - source image. Must have 3 or 4 channels.
;;  //        dst0, dst1, dst2, dst3  - destination images. Must have single channel.
;;  //               if src has 3 channels, dst3 must be NULL.
;;  //               if one of the destination images is not NULL,
;;  //               the corresponding channel is extracted from source image.
;;  //               Else, all 3 or 4 images must be non NULL and all the source image
;;  //               channels are written to destination images.
;;  //    Returns:
;;  //F*/
;;  OPENCVAPI  void  cvCvtPixToPlane( const void *src, void *dst0, void *dst1,
;;                                    void *dst2, void *dst3 );

(de cvCvtPixToPlane(src dst0 dst1 dst2 dst3)
    ((-gptr- "const void *") src)
    ((-gptr-) dst0 dst1 dst2 dst3)
    #{  cvCvtPixToPlane($src, $dst0, $dst1, $dst2, $dst3);  #}
    ())

#? (cvCvtPlaneToPix <src0> <src1> <src2> <src3> <dst>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //
;;  //    Name:    cvCvtPlaneToPix
;;  //    Purpose:
;;  //      Composes destination image from separate single-channel images
;;  //    Context:
;;  //    Parameters:
;;  //        src0, src1, src2, src3  - source images. Must have single channel.
;;  //              if destination image has 3 channels, src3 must be NULL, else must be
;;  //              non NULL. Other images must always be non NULL.
;;  //        dst - destination image. Must have 3 or 4 channels.
;;  //    Returns:
;;  //F*/
;;  OPENCVAPI  void  cvCvtPlaneToPix( const void *src0, const void *src1,
;;                                    const void *src2, const void *src3,
;;                                    void *dst );

(de cvCvtPlaneToPix(src0 src1 src2 src3 dst)
    ((-gptr- "const void *") src0 src1 src2 src3)
    ((-gptr-) dst)
    #{ cvCvtPlaneToPix($src0, $src1, $src2, $src3, $dst); #}
    ())

#? (cvConvertScale  <src> <dst> <scale> <shift>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //
;;  //    Name:    cvConvertScale
;;  //    Purpose:
;;  //      Converts image from one depth to another with linear transform
;;  //    Context:
;;  //    Parameters:
;;  //        src - source image.
;;  //        dst - destination image.
;;  //        scale - multiplier
;;  //        shift - delta. That is, dst(x,y) = src(x,y)*scale + shift.
;;  //    Returns:
;;  //    Notes:
;;  //        only float->uchar and uchar->float are supported by now.
;;  //F*/
;;  OPENCVAPI  void  cvConvertScale( const CvArr *src, CvArr *dst,
;;                                   double scale CV_DEFAULT(1),
;;                                   double shift CV_DEFAULT(0) );

(de cvConvertScale (src dst scale shift)
    ((-gptr- "const CvArr *") src)
    ((-gptr- "CvArr *") dst)
    ((-double-) scale shift)
    #{  cvConvertScale($src, $dst, $scale, $shift); #}
    ())

;;  #define cvCvtScale cvConvertScale

(de cvCvtScale (src dst scale shift)
    ((-gptr- "const CvArr *") src)
    ((-gptr- "CvArr *") dst)
    ((-double-) scale shift)
    #{  cvConvertScale($src, $dst, $scale, $shift); #}
    ())


;;  #define cvScale  cvConvertScale

(de cvScale (src dst scale shift)
    ((-gptr- "const CvArr *") src)
    ((-gptr- "CvArr *") dst)
    ((-double-) scale shift)
    #{  cvConvertScale($src, $dst, $scale, $shift); #}
    ())


#? (cvConvertScaleAbs  <src> <dst> <scale> <shift>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //
;;  //    Name:    cvConvertScaleAbs
;;  //    Purpose:
;;  //      Converts image from one depth to another
;;  //    Context:
;;  //    Parameters:
;;  //        src - source image.
;;  //        dst - destination image.
;;  //        scale - multiplier
;;  //        shift - delta. That is, dst(x,y) = abs(src(x,y)*scale + shift).
;;  //    Returns:
;;  //    Notes:
;;  //F*/
;;  OPENCVAPI  void  cvConvertScaleAbs( const void *src, void *dst,
;;                                      double scale CV_DEFAULT(1),
;;                                      double shift CV_DEFAULT(0) );

(de cvConvertScaleAbs (src dst scale shift)
    ((-gptr- "const void *") src)
    ((-gptr- "void *") dst)
    ((-double-) scale shift)
    #{  cvConvertScaleAbs($src, $dst, $scale, $shift); #}
    ())

    
;;  #define cvCvtScaleAbs  cvConvertScaleAbs

(de cvCvtScaleAbs (src dst scale shift)
    ((-gptr- "const void *") src)
    ((-gptr- "void *") dst)
    ((-double-) scale shift)
    #{  cvConvertScaleAbs($src, $dst, $scale, $shift); #}
    ())

#? (cvAbsDiff  <srca> <srcb> <dst>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //
;;  //    Name:    cvAbsDiff
;;  //    Purpose:
;;  //      Finds per-pixel absolute difference between two images
;;  //    Context:
;;  //    Parameters:
;;  //        srcA - first source image.
;;  //        srcB - second source image
;;  //        dst  - destination image, May be equal to srcA or srcB
;;  //    Returns:
;;  //F*/
;;  OPENCVAPI  void  cvAbsDiff( const CvArr* srcA, const CvArr* srcB, CvArr* dst );

(de cvAbsDiff (srca srcb dst)
    ((-gptr- "const CvArr *") srca srcb)
    ((-gptr- "CvArr *") dst)
    #{  cvAbsDiff($srca, $srcb, $dst); #}
    ())

#? (cvAbsDiffS  <src> <dst> <value>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //
;;  //    Name:    cvAbsDiffS
;;  //    Purpose:
;;  //      Finds per-pixel absolute difference between image and scalar value
;;  //    Context:
;;  //    Parameters:
;;  //        src - source image.
;;  //        dst - destination image, May be equal to srcA or srcB
;;  //        value - scalar value to compare with
;;  //    Returns:
;;  //F*/
;;  OPENCVAPI  void  cvAbsDiffS( const CvArr* src, CvArr* dst, CvScalar value );

(de cvAbsDiffS (src dst value)
    ((-gptr- "const CvArr *") src)
    ((-gptr- "CvArr *") dst)
    ((-gptr- "CvScalar *") value)
    #{  cvAbsDiffS($src, $dst, *$value); #}
    ())

#? (cvMatchTemplate <array> <templ> <result> <method>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //
;;  //    Name: cvMatchTemplate
;;  //    Purpose:
;;  //      measures similarity between template and overlapped windows in the source image
;;  //      and fills the resultant image with the measurements.
;;  //    Context:
;;  //    Parameters:
;;  //      img     - source image
;;  //      templ   - template to find
;;  //      result  - resultant image. its ROI must have size:
;;  //                     (img_width - templ_width + 1, img_height - templ_height + 1)
;;  //      method  - comparison method:
;;  //---------------------------------------------------------------------------------------
;;  //             CV_TM_SQDIFF:  res0(i,j)=sum(y=0,TH-1) sum(x=0,TW-1)[I(i+x,j+y)-T(x,y)]^2
;;  //                    (where  TW - template width, TH - template height
;;  //                          res0(i,j) - pixel value of result at location(i,j)
;;  //                                     (zero-th method)
;;  //                          Iij(x,y) - pixel value of source image at location(i+x,j+y)
;;  //                                     Iij alone means window of source image
;;  //                                     with top-left corner(i,j) and template size.
;;  //                          T(x,y) - pixel value of template at location(x,y)
;;  //                                   T alone means template.
;;  //---------------------------------------------------------------------------------------
;;  //             CV_TM_SQDIFF_NORMED:  res1(i,j) = res0(i,j)/
;;  //                                             (l2_norm(Iij)*l2_norm(templ);
;;  //                      where  l2_norm(A) = sqrt(
;;  //                                     sum(y=0,A_height-1) sum(x=0,A_width-1) A(x,y)^2);
;;  //---------------------------------------------------------------------------------------
;;  //             CV_TM_CCORR:  res2(i,j)=sum(y=0,TH-1) sum(x=0,TW-1)[Iij(x,y)*T(x,y)]
;;  //---------------------------------------------------------------------------------------
;;  //             CV_TM_CCORR_NORMED:  res3(i,j) = res2(i,j)/[l2_norm(Iij)*l2_norm(templ)];
;;  //---------------------------------------------------------------------------------------
;;  //             CV_TM_CCOEFF:  res4(i,j)=sum(y=0,TH-1) sum(x=0,TW-1) [I'ij(x,y)*T'(x,y)]
;;  //                   where A'(x,y) = A(x,y)-1/(A_width*A_height)*
;;  //                                   sum(l=0,A_height-1) sum(k=0,A_width-1)A(k,l)
;;  //---------------------------------------------------------------------------------------
;;  //             CV_TM_CCOEFF_NORMED:
;;  //                   res5(i,j)=res4(i,j)/[l2_norm(I'ij)*l2_norm(T')]
;;  //---------------------------------------------------------------------------------------
;;  //    Returns:
;;  //F*/
;;  /* method for comparing two images */
(de cvMatchTemplate(array templ result method)
    ((-gptr- "const CvArr *") array templ)
    ((-gptr- "CvArr *") result)
    ((-int-) method)
    #{ cvMatchTemplate($array, $templ, $result, $method);  #}
    ())


#? (cvbFastArctan <y> <x> <angle> <len>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //
;;  //    Name:    cvbFastArctan
;;  //    Purpose:
;;  //      Calculates arctangent for arrays of abscissas and ordinates
;;  //    Context:
;;  //    Parameters:
;;  //        y - array of abscissas
;;  //        x - array of ordinates
;;  //        angle - array of results: array[i] = arctan(y[i]/x[i])
;;  //        len - number of elements in arrays
;;  //    Returns:
;;  //    Notes:
;;  //      The function takes into account signs of both argument, so it is similar
;;  //      to atan2, but it returns angle in degrees(from 0 to 359.999 degrees)
;;  //      Maximal error is ~0.1 degreee.
;;  //F*/
;;  OPENCVAPI  void  cvbFastArctan( const float* y, const float* x, float* angle, int len );

(de cvbFastArctan(y x angle len)
    ((-gptr- "const float *") y x)
    ((-gptr- "float *") angle)
    ((-int-) len)
    #{ cvbFastArctan($y, $x, $angle, $len); #}
    ())

#? (cvbCartToPolar <y> <x> <magnitude> <angle> <len>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //
;;  //    Name:    cvbCartToPolar
;;  //    Purpose:
;;  //      Converts input arrays of abscissas and ordinates to polar form
;;  //    Context:
;;  //    Parameters:
;;  //        y - array of abscissas
;;  //        x - array of ordinates
;;  //        magnitude - array of magnitudes: mag[i] = sqrt(y[i]*y[i] + x[i]*x[i])
;;  //        angle - array of angles: array[i] = arctan(y[i]/x[i])
;;  //        len - number of elements in arrays
;;  //    Returns:
;;  //    Notes:
;;  //      The function calculates angle(similar to cvbFastArctan) and magnitude for
;;  //      every 2D vector(x[i],y[i]). Both output arguments are optional. If some
;;  //      output parameter is absent, corresponding part is not calculated
;;  //F*/
;;  OPENCVAPI  void  cvbCartToPolar( const float* y, const float* x,
;;                                  float* magnitude, float* angle, int len );

(de cvbCartToPolar(y x magnitude angle len)
    ((-gptr- "const float *") y x )
    ((-gptr- "float *") magnitude angle)
    ((-int-) len)
    #{ cvbCartToPolar($y, $x, $magnitude, $angle, $len); #}
    ())

#? (cvbSqrt <x> <sqrtx> <len>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //
;;  //    Name:    cvbSqrt
;;  //    Purpose:
;;  //      Calculates square root for array of floats
;;  //    Context:
;;  //    Parameters:
;;  //        x - array of arguments
;;  //        sqrt_x - array of results
;;  //        len - number of elements in arrays
;;  //    Returns:
;;  //    Notes:
;;  //      Elements of input array must be non-negative, else the result is not defined.
;;  //      Maximal relative error is ~3e-7
;;  //F*/
;;  OPENCVAPI  void  cvbSqrt( const float* x, float* sqrt_x, int len );

(de cvbSqrt(x sqrtx len)
    ((-gptr- "const float *") x)
    ((-gptr- "float *") sqrtx)
    ((-int-) len)
    #{ cvbSqrt($x, $sqrtx, $len); #}
    ())

#? (cvCheckArr <arr> <flags> <minval> <maxval>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //
;;  //    Name:    cvCheckArr
;;  //    Purpose:
;;  //      Checks array for bad elements (NaNs, Infinities or just too big
;;  //                                     positive or negative values)
;;  //    Context:
;;  //    Parameters:
;;  //      arr - input array
;;  //      flags - operation flags, that may be zero or combination of the following values:
;;  //               CV_CHECK_RANGE - the function checks that the array elements are
;;  //                                within [minVal,maxVal) range. By default, only NaNs
;;  //                                and Infinities are checked.
;;  //               CV_CHECK_QUIET - do not raise error if some elements is out of
;;  //                                range. It is not a default mode.
;;  //    Returns:
;;  //      1 if array is ok, 0 otherwise. If CV_CHECK_QUIET is not set, function
;;  //      raises the CV_StsOutOfRange error in the latter case.
;;  //F*/
(de cvCheckArr(arr flags minval maxval)
    ((-gptr- "const CvArr *") arr)
    ((-int-) flags)
    ((-double-) minval maxval)
    (let ((s 0))
      ((-int-) s)
      #{  $s = cvCheckArr($arr, $flags, $minval, $maxval); #}
      s))

#? (cvbInvSqrt <x> <invsqrtx> <len>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //
;;  //    Name:    cvbInvSqrt
;;  //    Purpose:
;;  //      Calculates inverse square root for array of floats
;;  //    Context:
;;  //    Parameters:
;;  //        x - array of arguments
;;  //        sqrt_x - array of results
;;  //        len - number of elements in arrays
;;  //    Returns:
;;  //    Notes:
;;  //      Elements of input array must be positive, else the result is not defined.
;;  //      Maximal relative error is ~2e-7
;;  //F*/
;;  OPENCVAPI  void  cvbInvSqrt( const float* x, float* inv_sqrt_x, int len );

(de cvbInvSqrt(x invsqrtx len)
    ((-gptr- "const float *") x)
    ((-gptr- "float *") invsqrtx)
    ((-int-) len)
    #{ cvbInvSqrt($x, $invsqrtx, $len); #}
    ())

#? (cvbReciprocal <x> <invx> <len>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //
;;  //    Name:    cvbReciprocal
;;  //    Purpose:
;;  //      Calculates inverse value(1/x) for array of floats
;;  //    Context:
;;  //    Parameters:
;;  //        x - array of arguments
;;  //        inv_x - array of results
;;  //        len - number of elements in arrays
;;  //    Returns:
;;  //    Notes:
;;  //      For zero elements result is 0.
;;  //      Maximal relative error is <2e-7
;;  //F*/
;;  OPENCVAPI  void  cvbReciprocal( const float* x, float* inv_x, int len );

(de cvbReciprocal(x invx len)
    ((-gptr- "const float *") x)
    ((-gptr- "float *") invx)
    ((-int-) len)
    #{ cvbReciprocal($x, $invx, $len); #}
    ())

#? (cvbFastExp  <x> <expx> <len>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //
;;  //    Name:    cvbFastExp
;;  //    Purpose:
;;  //      Calculates fast exponent approximation for array of floats
;;  //    Context:
;;  //    Parameters:
;;  //        x - array of arguments
;;  //        exp_x - array of results
;;  //        len - number of elements in arrays
;;  //    Returns:
;;  //    Notes:
;;  //      Overflow is not handled yet. Underflow is handled.
;;  //      Maximal relative error is ~7e-6
;;  //F*/
;;  OPENCVAPI  void  cvbFastExp( const float* x, double* exp_x, int len );

(de cvbFastExp (x expx len)
    ((-gptr- "const float *") x)
    ((-gptr- "double *") expx)
    ((-int-) len)
    #{ cvbFastExp($x, $expx, $len); #}
    ())

#? (cvbFastLog  <x> <logx> <len>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //
;;  //    Name:    cvbFastLog
;;  //    Purpose:
;;  //      Calculates fast logarithm approximation for array of doubles
;;  //    Context:
;;  //    Parameters:
;;  //        x - array of arguments
;;  //        log_x - array of logarithms of absolute values of arguments
;;  //        len - number of elements in arrays
;;  //    Returns:
;;  //    Notes:
;;  //      Negative values are negated before logarithm is taken.
;;  //      Logarithm of 0 gives large negative number(~700)
;;  //      Maximal relative error is ~3e-7
;;  //F*/
;;  OPENCVAPI  void  cvbFastLog( const double* x, float* log_x, int len );

(de cvbFastLog( x logx len)
    ((-gptr- "const double *") x)
    ((-gptr- "float *") logx)
    ((-int-) len)
    #{ cvbFastLog( $x, $logx, $len); #}
    ())

#? (cvRandInit <state> <lower> <upper> <seed>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //
;;  //    Name:    cvRandInit
;;  //    Purpose:
;;  //      Initializes random number generator(RNG)
;;  //    Context:
;;  //    Parameters:
;;  //      state - pointer to initialized RNG state
;;  //      lower - lower bound of random values
;;  //      upper - upper bound of random values.
;;  //              Generated random numbers belong to range [lower,upper)
;;  //      seed  - initializing 32-bit integer for RNG
;;  //    Returns:
;;  //F*/
;;  OPENCVAPI  void  cvRandInit( CvRandState* state, double lower, double upper, int seed );

(de cvRandInit(state lower upper seed)
    ((-gptr- "CvRandState *") state)
    ((-double-) lower upper)
    ((-int-) seed)
    #{ cvRandInit($state, $lower, $upper, $seed, CV_RAND_UNI); #}
    ())

#? (cvRandSetRange <state> <lower> <upper> <index>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //    Name:    cvRandSetRange
;;  //    Purpose: sets range of generated random numbers without reinitializing RNG
;;  //    Context:
;;  //    Parameters:
;;  //      state - pointer to state structure
;;  //      lower - lower bound
;;  //      upper - upper bound
;;  //      dim  - optional parameter.
;;  //             Index of the dimension to set the range for (0th, 1st etc.)
;;  //             -1 means to set the same range for all dimensions.
;;  //    Returns:
;;  //      CV_OK or error code if:
;;  //         state pointer is zero or
;;  //         lower bound greater than upper bound.
;;  //    Notes:
;;  //F*/
;;  OPENCVAPI  void  cvRandSetRange( CvRandState* state, double lower, double upper,
;;                                   int index CV_DEFAULT(-1));

(de cvRandSetRange(state lower upper index)
    ((-gptr- "CvRandState *") state)
    ((-double-) lower upper)
    ((-int-) index)
    #{ cvRandSetRange($state, $lower, $upper, $index); #}
    ())

#? (cvbRand  <state> <dst> <len>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //
;;  //    Name:    cvbRand
;;  //    Purpose:
;;  //      Fills array of floats with random numbers and updates RNG state
;;  //    Context:
;;  //    Parameters:
;;  //      state - RNG state
;;  //      dst   - destination floating-point array
;;  //      len   - number of elements in the array.
;;  //    Returns:
;;  //F*/
;;  OPENCVAPI  void  cvbRand( CvRandState* state, float* dst, int len );

(de cvbRand (state dst len)
    ((-gptr- "CvRandState *") state)
    ((-gptr- "float *") dst)
    ((-int-) len)
    #{  cvbRand($state, $dst, $len); #}
    ())

#? (cvRand  <state> <arr>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //
;;  //    Name:    cvRand
;;  //    Purpose:
;;  //      Fills an array with random numbers and updates RNG state
;;  //    Context:
;;  //    Parameters:
;;  //      state - RNG state
;;  //      arr   - the destination array
;;  //    Returns:
;;  //F*/
;;  OPENCVAPI  void  cvRand( CvRandState* state, CvArr* arr );

(de cvRand( state arr)
    ((-gptr- "CvRandState *") state)
    ((-gptr- "CvArr *") arr)
    #{  cvRand($state, $arr); #}
    ())

#? (cvRandNext  <state>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //
;;  //    Name:    cvRandNext
;;  //    Purpose:
;;  //      Updates RNG state and returns 32-bit random number
;;  //    Context:
;;  //    Parameters:
;;  //      state - RNG state
;;  //    Returns:
;;  //      random number
;;  //F*/
;;  OPENCVAPI  unsigned  cvRandNext( CvRandState* state );

;; Jie's comments: will the return value be larger than the largest-integer?

(de cvRandNext( state)
    ((-gptr- "CvRandState *") state)
    (let ((s 0))
      ((-int-) s)
      #{ $s = cvRandNext($state);  #}
      s))
;; ********************************************************

(dhc-make ()
  dummy_utilities
  cvinitlineiterator
  cvsampleline
  cvgetrectsubpix
  cvgetquadranglesubpix
  cvcvtpixtoplane
  cvcvtplanetopix
  cvconvertscale
  cvcvtscale
  cvscale
  cvconvertscaleabs
  cvcvtscaleabs
  cvabsdiff
  cvabsdiffs
  cvmatchtemplate
  cvbfastarctan
  cvbcarttopolar
  cvbsqrt
  cvcheckarr
  cvbinvsqrt
  cvbreciprocal
  cvbfastexp
  cvbfastlog
  cvrandinit
  cvrandsetrange
  cvbrand
  cvrand
  cvrandnext
)
