
(libload "opencv/cv-config")
(libload "opencv/aux_macro_func")
(libload "opencv/data_structures")

(eval `(de |dummy_cvsmob| () ,cv-cpheader-cmd ()))

;;*********************************************************************
(defclass cvsmob object
  ((-gptr-) p))

(defmethod cvsmob cvsmob ()
  #{ $p = NULL; #} 
  ())

(defmethod cvsmob -destructor ()
  #{ if($p)  free($p); #}
  ())

(defmethod cvsmob getp ()
  p)

;;********************************************************************
(defclass cvsize cvsmob)

#? (new CvSize <width> <height>)
(defmethod cvsize cvsize (width height)
  ((-int-) width height)
  #{ $p = malloc(sizeof(CvSize)) ;
     ((CvSize *)$p)->width = $width; 
     ((CvSize *)$p)->height = $height; 
  #}
  ())

;;*******************************************************************
(defclass cvpoint cvsmob)

#? (new CvPoint <x> <y>)
(defmethod cvpoint cvpoint (x y)
  ((-int-) x y)
  #{  $p = malloc(sizeof(CvPoint));
      ((CvPoint *)$p)->x = $x;
      ((CvPoint *)$p)->y = $y;
  #}
  ())

(defmethod cvpoint copy ( pt )
  ((-gptr- "CvPoint *") pt)
  #{ if (!$p) $p = malloc(sizeof(CvPoint));
     memcpy($p,$pt, sizeof(CvPoint)); #}
  ())

(defmethod cvpoint get-x ()
  (int #{ ((CvPoint *)$p)->x #} ))

(defmethod cvpoint set-x ( x)
  ((-int-) x)
  #{ ((CvPoint *)$p)->x = $x;  #} 
  ())

(defmethod cvpoint get-y ()
  (int #{ ((CvPoint *)$p)->y #} ))

(defmethod cvpoint set-y ( y)
  ((-int-) y)
  #{ ((CvPoint *)$p)->y = $y; #} 
  ())

;;*******************************************************************
(defclass cvscalar cvsmob)

#? (new CvScalar <d0> <d1> <d2> <d3>)
(defmethod cvscalar cvscalar (d0 d1 d2 d3)
  ((-double-) d0 d1 d2 d3)
  #{  $p = malloc(sizeof(CvScalar));
      ((CvScalar *)$p)->val[0] = $d0;
      ((CvScalar *)$p)->val[1] = $d1;
      ((CvScalar *)$p)->val[2] = $d2;
      ((CvScalar *)$p)->val[3] = $d3;
  #}
  ())

(defmethod cvscalar copy (s)
  ((-gptr- "CvScalar *") s)
  #{ if (!$p) $p = malloc(sizeof(CvScalar));
     memcpy($p,$s, sizeof(CvScalar)); 
   #}
  ())

;;******************************************************************
(defclass cvpoint2d32f cvsmob)

#? (new CvPoint2D32f <x> <y>)
(defmethod cvpoint2d32f cvpoint2d32f (x y)
  ((-float-) x y)
  #{ $p = malloc(sizeof(CvPoint2D32f));
     ((CvPoint2D32f *)$p)->x = $x;
     ((CvPoint2D32f *)$p)->y = $y; 
  #}
  ())

(defmethod cvpoint2d32f copy ( pt )
  ((-gptr- "CvPoint2D32f *") pt)
  #{ memcpy($p, $pt, sizeof(CvPoint2D32f)); #}
  ())

(defmethod cvpoint2d32f get-x ()
  (float #{ ((CvPoint2D32f *)$p)->x  #} ))

(defmethod cvpoint2d32f get-y ()
  (float #{ ((CvPoint2D32f *)$p)->y #} ))

;;******************************************************************
(defclass cvpoint3d32f cvsmob)

#? (new CvPoint3D32f <x> <y>)
(defmethod cvpoint3d32f cvpoint3d32f (x y z)
  ((-float-) x y z)
  #{ $p = malloc(sizeof(CvPoint3D32f));
     ((CvPoint3D32f *)$p)->x = $x;
     ((CvPoint3D32f *)$p)->y = $y;
     ((CvPoint3D32f *)$p)->z = $z;
  #}
  ())

(defmethod cvpoint3d32f copy ( pt )
  ((-gptr- "CvPoint3D32f *") pt)
  #{ memcpy($p, $pt, sizeof(CvPoint3D32f)); #}
  ())

(defmethod cvpoint3d32f get-x ()
  (float #{ ((CvPoint3D32f *)$p)->x  #} ))

(defmethod cvpoint3d32f get-y ()
  (float #{ ((CvPoint3D32f *)$p)->y #} ))

(defmethod cvpoint3d32f get-z ()
  (float #{ ((CvPoint3D32f *)$p)->z #} ))


;;*******************************************************************
(defclass cvrect cvsmob)

#? (new CvRect <x> <y> <width> <height>)
(defmethod cvrect cvrect (x y width height)
  ((-int-) x y width height)
  #{  $p = malloc(sizeof(CvRect)); 
      ((CvRect *)$p)->x = $x;
      ((CvRect *)$p)->y = $y;
      ((CvRect *)$p)->width = $width;
      ((CvRect *)$p)->height = $height;
  #}
  ())
  
(defmethod cvrect copy ( pt)
  ((-gptr- "CvRect *") pt)
  #{ if (!$p) $p = malloc(sizeof(CvRect));
     memcpy($p,$pt, sizeof(CvRect)); #}
  ())

(defmethod cvrect get-x ()
  (int #{ ((CvRect *)$p)->x #} ))

(defmethod cvrect get-y ()
  (int #{ ((CvRect *)$p)->y #} ))

(defmethod cvrect get-width ()
  (int #{ ((CvRect *)$p)->width #} ))

(defmethod cvrect get-height ()
  (int #{ ((CvRect *)$p)->height #} ))

;;*******************************************************************
(defclass cvconnectedcomp cvsmob)

#? (new CvConnectedComp)
(defmethod cvconnectedcomp cvconnectedcomp ()
  #{ $p = malloc(sizeof(CvConnectedComp)); #}
  ())

(defmethod cvconnectedcomp copy (pt)
  ((-gptr- "CvConnectedComp *") pt)
  #{ memcpy($p,$pt, sizeof(CvConnectedComp)); #}
  ())

(defmethod cvconnectedcomp get-area ()
  (double #{ ((CvConnectedComp *)$p)->area #} ))

(defmethod cvconnectedcomp get-value ()
  (gptr #{ &((CvConnectedComp *)$p)->value #} ) )

(defmethod cvconnectedcomp get-rect ()
  (gptr #{ &(((CvConnectedComp *)$p)->rect) #}) )


;;******************************************************************
(defclass cvtermcriteria cvsmob)

#? (new CvTermCriteria <type> <max-iter> <epsilon>)
(defmethod cvtermcriteria cvtermcriteria (s m e)
  ((-int-) s m)
  ((-float-) e)
  #{ $p = malloc(sizeof(CvTermCriteria)); 
     ((CvTermCriteria *)$p)->type = $s;
     ((CvTermCriteria *)$p)->max_iter = $m;
     ((CvTermCriteria *)$p)->epsilon = $e;
  #}
  ())

;;********************************************************************
(defclass cvbigob object 
  ((-gptr-) p))

(defmethod cvbigob cvbigob ()
  #{ $p = NULL; #} 
  ())

(defmethod cvbigob -destructor ()
  #{ if($p)  free($p); #}
  ())

;;********************************************************************
(defclass cvimage cvbigob)

#? (new CvImage <width> <height> <depth> <channels>)
(defmethod cvimage cvimage (width height depth channels)
  ((-int-) width height depth channels)
  #{ $p = cvCreateImage( cvSize($width,$height), $depth, $channels); #}
  ())

(defmethod cvimage -destructor ()
  (cvReleaseImage  #{ (IplImage **)&$p #}) )

(defmethod cvimage get-width ()
  (int #{ ((IplImage *)$p)->width #} ))

(defmethod cvimage get-height ()
  (int #{ ((IplImage *)$p)->height #} ))

(defmethod cvimage get-depth ()
  (int #{ ((IplImage *)$p)->depth #} ))

(defmethod cvimage get-nchannels ()
  (int #{ ((IplImage *)$p)->nChannels #} ))

(defmethod cvimage draw-onepolyline ()
  ())

;;**********************************************************************
(defclass cvmat cvbigob)

;;  CvMat cvMat( int rows, int cols, int type, void* data CV_DEFAULT(NULL))
(defmethod cvmat cvmat ( rows cols type)
  ((-int-) rows cols type)
  #{  $p = cvCreateMat($rows, $cols, $type);  #}
  ())

(defmethod cvmat get ( row col)
  ((-int-) row col)
  (double #{ cvmGet((cvMat *)$p, $row, $col) #}))

(defmethod cvmat set ( row col val)
  ((-int-) row col)
  ((-double-) val)
  #{ cvmSet( (cvMat *)$p, $row, $col, $val); #}
  ())

(defmethod cvmat -destructor ()
  #{ cvReleaseMat( (CvMat **) &$p ); #}
  ())

;;**********************************************************************
(defclass cvsmobarray  cvsmob
  ((-int-) elemsize)
  ((-int-) elemnum))

#? (new cvsmobsarray <size> <num>)
(defmethod cvsmobarray cvsmobarray ( size num)
  ((-int-) size num)
  #{ $p = malloc($size * $num); 
     $elemsize = $size;
     $elemnum  = $num; 
  #}
  ())

(defmethod cvsmobarray ptr-nth ( n)
  ((-int-) n)
  (gptr #{  ((char *)$p) + ($n * $elemsize) #} ))

(dhc-make-with-c++ () ()
   |dummy_cvsmob|
   (cvsmob cvsmob getp -destructor)
   (cvsize cvsize)
   (cvpoint cvpoint copy get-x get-y set-x set-y)
   (cvscalar cvscalar copy)
   (cvpoint2d32f cvpoint2d32f copy get-x get-y)
   (cvpoint3d32f cvpoint3d32f copy get-x get-y)
   (cvrect cvrect  copy  get-x get-y get-width get-height)
   (cvconnectedcomp cvconnectedcomp copy get-area get-value get-rect)
   (cvtermcriteria cvtermcriteria)
   (cvbigob cvbigob -destructor)
   (cvimage cvimage -destructor get-width get-height get-depth get-nchannels)
   (cvmat cvmat -destructor get set)
   (cvsmobarray cvsmobarray ptr-nth)
)
