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



#? *** morphology

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

;; #? * (dummy_morphology)
;; #? * (cvcreatestructuringelementex <cols> <rows> <anchorx> <anchory> <shape> <values>)
;; #? * (cvreleasestructuringelement <ele>)
;; #? * (cverode <src> <dst> <ele> <ite>)
;; #? * (cvdilate <src> <dst> <ele> <ite>)
;; #? * (cvmorphologyex <src> <dst> <temp> <ele> <op> <ite>)

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

;;  /****************************************************************************************\
;;  *                                    Morphology                                          *
;;  \****************************************************************************************/
#? (cvCreateStructuringElementEx  <cols> <rows> <anchorx> <anchory> <shape> <values>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //
;;  //    Name:    cvCreateStructuringElementEx
;;  //    Purpose:
;;  //      Allocates and fills IplConvKernel structure
;;  //      which can be used as a structuring element in following morphological operations
;;  //    Context:
;;  //    Parameters:
;;  //        cols   - number of columns in the kernel
;;  //        rows   - number of rows in the kernel
;;  //        anchorX - x-coordinate of anchor point(0..cols-1)
;;  //        anchorY - y-coordinate of anchor point(0..rows-1)
;;  //        shape   - shape of the structuring element
;;  //              CV_SHAPE_RECT - rectangular element
;;  //              CV_SHAPE_CROSS - cross-shaped element
;;  //              CV_SHAPE_ELLIPSE - elliptic element
;;  //              CV_SHAPE_CUSTOM - arbitrary element.
;;  //              <values> array determines mask
;;  //        values  - mask array. non-zero pixels determine shape of the element
;;  //    Returns:
;;  //        structuring element
;;  //F*/
;;  OPENCVAPI  IplConvKernel*  cvCreateStructuringElementEx( int  cols,    int rows,
;;                                                           int  anchorX, int anchorY,
;;                                                           CvElementShape shape,
;;                                                           int* values CV_DEFAULT(0) );

(de cvCreateStructuringElementEx (cols rows anchorx anchory shape values)
    ((-int-) cols rows anchorx anchory)
    ((-int-) shape)
    ((-gptr- "int *") values)
    (let ((p (gptr ())))
      #{  $p = (IplConvKernel *) cvCreateStructuringElementEx($cols, $rows,$anchorx,$anchory,$shape,$values);  #}
      p))

#? (cvReleaseStructuringElement  <ele>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //
;;  //    Name:    cvReleaseStructuringElement
;;  //    Purpose:
;;  //      Releases structuring element and clears pointer
;;  //    Context:
;;  //    Parameters:
;;  //        element - double pointer to structuring element
;;  //    Returns:
;;  //F*/
;;  OPENCVAPI  void  cvReleaseStructuringElement( IplConvKernel** element );

(de cvReleaseStructuringElement (ele)
    ((-gptr- "IplConvKernel **") ele)
    #{  cvReleaseStructuringElement($ele); #} 
    ())

#? (cvErode  <src> <dst> <ele> <ite>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //
;;  //    Name:    cvErode
;;  //    Purpose:
;;  //      Applies minimum filter to the source image. Structuring element specifies
;;  //      a shape of a pixel neigborhood, over which the minimum is calculated
;;  //    Context:
;;  //    Parameters:
;;  //        src    - source image
;;  //        dst    - destination image, may be the same as source one.
;;  //        element - structuring element.
;;  //                  If the pointer is 0, 3x3 rectangular element is used.
;;  //        iterations - how many times the erosion needs to be applied
;;  //    Returns:
;;  //F*/
;;  OPENCVAPI  void  cvErode( const CvArr* src, CvArr* dst,
;;                            IplConvKernel* element CV_DEFAULT(0),
;;                            int iterations CV_DEFAULT(1) );

(de cvErode (src dst ele ite)
    ((-gptr- "const CvArr *") src)
    ((-gptr- "CvArr *") dst)
    ((-gptr- "IplConvKernel *") ele)
    ((-int-) ite)
    #{  cvErode($src, $dst, $ele, $ite); #}
    ())

#? (cvDilate  <src> <dst> <ele> <ite>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //
;;  //    Name:    cvDilate
;;  //    Purpose:
;;  //      Applies maximum filter to the source image. Structuring element specifies
;;  //      a shape of a pixel neigborhood, over which the maximum is calculated
;;  //    Context:
;;  //    Parameters:
;;  //        src    - source image
;;  //        dst    - destination image, may be the same as source one.
;;  //        element - structuring element.
;;  //                  If the pointer is 0, 3x3 rectangular element is used.
;;  //        iterations - how many times the dilation needs to be applied
;;  //    Returns:
;;  //F*/
;;  OPENCVAPI  void  cvDilate( const CvArr* src, CvArr* dst,
;;                             IplConvKernel* element CV_DEFAULT(0),
;;                             int iterations CV_DEFAULT(1) );

(de cvDilate (src dst ele ite)
    ((-gptr- "const CvArr *") src)
    ((-gptr- "CvArr *") dst)
    ((-gptr- "IplConvKernel *") ele)
    ((-int-) ite)
    #{  cvDilate($src, $dst, $ele, $ite); #}
    ())


#? (cvMorphologyEx  <src> <dst> <temp> <ele> <op> <ite>)
;;.VP
;;  /*F///////////////////////////////////////////////////////////////////////////////////////
;;  //
;;  //    Name:    cvMorphologyEx
;;  //    Purpose:
;;  //      Applies one of the extended morphological operations that are based on
;;  //      erosion and dilation.
;;  //    Context:
;;  //    Parameters:
;;  //        src    - source image
;;  //        dst    - destination image, may be the same as source one.
;;  //        temp   - temporary image. The parameter must be non-zero
;;  //                 if operation is CV_MOP_TOPHAT or CV_MOP_BLACKHAT and src == dst, or
;;  //                 if operation is CV_MOP_GRADIENT
;;  //        element - structuring element.
;;  //                  If the pointer is 0, 3x3 rectangular element is used.
;;  //        operation - one of the following:
;;  //               (let's nB = "<element>, applied <iterations> times")
;;  //                CV_MOP_OPEN:   dst = dilate(erode(src,nB),nB);
;;  //                CV_MOP_CLOSE:  dst = erode(dilate(src,nB),nB);
;;  //                CV_MOP_GRADIENT: dst = dilate(src,nB)-erode(src,nB)
;;  //                CV_MOP_TOPHAT:   dst = src - erode(src,nB)
;;  //                CV_MOP_BLACKHAT: dst = dilate(src,nB) - src
;;  //        iterations - how many times the erosion/dilation needs to be applied
;;  //    Returns:
;;  //F*/

(de cvMorphologyEx (src dst temp ele op ite)
    ((-gptr- "const CvArr *") src)
    ((-gptr- "CvArr *") dst)
    ((-gptr- "IplConvKernel *") ele)
    ((-gptr- "CvArr *") temp)
    ((-int-) op ite)
    #{ cvMorphologyEx($src, $dst, $temp, $ele, $op, $ite);  #}
    ())

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

(dhc-make ()
  dummy_morphology
  cvcreatestructuringelementex
  cvreleasestructuringelement
  cverode
  cvdilate
  cvmorphologyex
)
