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



#? *** aux_convert

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

#? * (|dummy_aux_convert|)
#? * (ipl8u-idx <img> <mat>)
#? * (ipl8s-idx <img> <mat>)
#? * (ipl16s-idx <img> <mat>)
#? * (ipl32s-idx <img> <mat>)
#? * (ipl32f-idx <img> <mat>)
#? * (ipl64f-idx <img> <mat>)
#? * (idx8u-ipl <mat> <img>)
#? * (idx8s-ipl <mat> <img>)
#? * (idx16s-ipl <mat> <img>)
#? * (idx32s-ipl <mat> <img>)
#? * (idx32f-ipl <mat> <img>)
#? * (idx64f-ipl <mat> <img>)
#? * (ipl8u-idx-share <img> <mat>)
#? * (ipl8s-idx-share <img> <mat>)
#? * (ipl16s-idx-share <img> <mat>)
#? * (ipl32s-idx-share <img> <mat>)
#? * (ipl32f-idx-share <img> <mat>)
#? * (ipl64f-idx-share <img> <mat>)
#? * (idx8u-ipl-share <mat> <img>)
#? * (idx8s-ipl-share <mat> <img>)
#? * (idx16s-ipl-share <mat> <img>)
#? * (idx32s-ipl-share <mat> <img>)
#? * (idx32f-ipl-share <mat> <img>)
#? * (idx64f-ipl-share <mat> <img>)
#? * (print-ipl-info <s>)

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



     
(de ipl8u_idx (img mat)
    ((-gptr- "IplImage *") img)
    ((-idx3- (-ubyte-)) mat)
     #{ {
     int d1,d2,d3,rowbytes;
     char * imgptr;
     uchar * matptr;

     d1 = $img->height;
     d2 = $img->width;
     d3 = $img->nChannels;
     rowbytes = d2*d3*sizeof(unsigned char);
     
     Midx_setdim3($mat,d1,d2,d3);
     Midx_update_mod_from_dim($mat);

     imgptr = $img->imageData;
     Midx_begin_bloop1( tmp, $mat, uchar)
            matptr = IDX_PTR( &tmp, uchar);
            memcpy((void *)matptr,(void *)imgptr,rowbytes);
            imgptr += $img->widthStep;
     Midx_end_bloop1( tmp, $mat, uchar)
     } #}
     ())
           
 
(de ipl8s_idx (img mat)
    ((-gptr- "IplImage *") img)
    ((-idx3- (-byte-)) mat)
     #{ {
     int d1,d2,d3,rowbytes;
     char * imgptr;
     signed char * matptr;

     d1 = $img->height;
     d2 = $img->width;
     d3 = $img->nChannels;
     rowbytes = d2*d3*sizeof(signed char);
     
     Midx_setdim3($mat,d1,d2,d3);
     Midx_update_mod_from_dim($mat);

     imgptr = $img->imageData;
     Midx_begin_bloop1( tmp, $mat, signed char)
            matptr = IDX_PTR( &tmp, signed char);
            memcpy((void *)matptr,(void *)imgptr,rowbytes);
            imgptr += $img->widthStep;
     Midx_end_bloop1( tmp, $mat, signed char)
     } #}
     ())
                    
(de ipl16s_idx (img mat)
    ((-gptr- "IplImage *") img)
    ((-idx3- (-short-)) mat)
     #{ {
     int d1,d2,d3,rowbytes;
     char * imgptr;
     short * matptr;

     d1 = $img->height;
     d2 = $img->width;
     d3 = $img->nChannels;
     rowbytes = d2*d3*sizeof(short);
     
     Midx_setdim3($mat,d1,d2,d3);
     Midx_update_mod_from_dim($mat);

     imgptr = $img->imageData;
     Midx_begin_bloop1( tmp, $mat, short)
            matptr = IDX_PTR( &tmp, short);
            memcpy((void *)matptr,(void *)imgptr,rowbytes);
            imgptr += $img->widthStep;
     Midx_end_bloop1( tmp, $mat, short)
     } #}
     ())
                    
     

(de ipl32s_idx (img mat)
    ((-gptr- "IplImage *") img)
    ((-idx3- (-int-)) mat)
     #{ {
     int d1,d2,d3,rowbytes;
     char * imgptr;
     int * matptr;

     d1 = $img->height;
     d2 = $img->width;
     d3 = $img->nChannels;
     rowbytes = d2*d3*sizeof(int);
     
     Midx_setdim3($mat,d1,d2,d3);
     Midx_update_mod_from_dim($mat);

     imgptr = $img->imageData;
     Midx_begin_bloop1( tmp, $mat, int)
            matptr = IDX_PTR( &tmp, int);
            memcpy((void *)matptr,(void *)imgptr,rowbytes);
            imgptr += $img->widthStep;
     Midx_end_bloop1( tmp, $mat, int)
     } #}
     ())



(de ipl32f_idx (img mat)
    ((-gptr- "IplImage *") img)
    ((-idx3- (-float-)) mat)
     #{ {
     int d1,d2,d3,rowbytes;
     char * imgptr;
     float * matptr;

     d1 = $img->height;
     d2 = $img->width;
     d3 = $img->nChannels;
     rowbytes = d2*d3*sizeof(float);
     
     Midx_setdim3($mat,d1,d2,d3);
     Midx_update_mod_from_dim($mat);

     imgptr = $img->imageData;
     Midx_begin_bloop1( tmp, $mat, float)
            matptr = IDX_PTR( &tmp,float);
            memcpy((void *)matptr,(void *)imgptr,rowbytes);
            imgptr += $img->widthStep;
     Midx_end_bloop1( tmp, $mat, float)
     } #}
     ())
           

(de ipl64f_idx (img mat)
    ((-gptr- "IplImage *") img)
    ((-idx3- (-double-)) mat)
     #{ {
     int d1,d2,d3,rowbytes;
     char * imgptr;
     double * matptr;

     d1 = $img->height;
     d2 = $img->width;
     d3 = $img->nChannels;
     rowbytes = d2*d3*sizeof(double);
     
     Midx_setdim3($mat,d1,d2,d3);
     Midx_update_mod_from_dim($mat);

     imgptr = $img->imageData;
     Midx_begin_bloop1( tmp, $mat, double)
            matptr = IDX_PTR( &tmp, double);
            memcpy((void *)matptr,(void *)imgptr,rowbytes);
            imgptr += $img->widthStep;
     Midx_end_bloop1( tmp, $mat, double)
     } #}
     ())
           
     
(de idx8u_ipl (mat img)
    ((-gptr- "IplImage *") img)
    ((-idx3- (-ubyte-)) mat)
     #{ {
     int width,height,channels,rowbytes;
     char * imgptr;
     uchar * matptr;
     CvSize sz;


     height = Midx_dim($mat,0);
     width  = Midx_dim($mat,1);
     channels = Midx_dim($mat,2);
     rowbytes = Midx_mod($mat,0)*sizeof(unsigned char);
     sz=cvSize(width,height);

     if($img->imageData)
           cvReleaseData($img);
     cvInitImageHeader($img,sz,IPL_DEPTH_8U,channels,IPL_ORIGIN_TL,4);
     cvCreateData($img);

     imgptr = $img->imageData;
     Midx_begin_bloop1( tmp, $mat, uchar)
            matptr = IDX_PTR( &tmp, uchar);
            memcpy((void *)imgptr,(void *)matptr,rowbytes);
            imgptr += $img->widthStep;
     Midx_end_bloop1( tmp, $mat, uchar)
     } #}
     ())
 

(de idx8s_ipl (mat img)
    ((-gptr- "IplImage *") img)
    ((-idx3- (-byte-)) mat)
     #{ {
     int width,height,channels,rowbytes;
     char * imgptr;
     signed char * matptr;
     CvSize sz;


     height = Midx_dim($mat,0);
     width  = Midx_dim($mat,1);
     channels = Midx_dim($mat,2);
     rowbytes = Midx_mod($mat,0)*sizeof(signed char);
     sz=cvSize(width,height);

     if($img->imageData)
           cvReleaseData($img);
     cvInitImageHeader($img,sz,IPL_DEPTH_8S,channels,IPL_ORIGIN_TL,4);
     cvCreateData($img);

     imgptr = $img->imageData;
     Midx_begin_bloop1( tmp, $mat, signed char)
            matptr = IDX_PTR( &tmp, signed char);
            memcpy((void *)imgptr,(void *)matptr,rowbytes);
            imgptr += $img->widthStep;
     Midx_end_bloop1( tmp, $mat, uchar)
     } #}
     ())
     

(de idx16s_ipl (mat img)
    ((-gptr- "IplImage *") img)
    ((-idx3- (-short-)) mat)
     #{ {
     int width,height,channels,rowbytes;
     char * imgptr;
     short * matptr;
     CvSize sz;


     height = Midx_dim($mat,0);
     width  = Midx_dim($mat,1);
     channels = Midx_dim($mat,2);
     rowbytes = Midx_mod($mat,0)*sizeof(short);
     sz=cvSize(width,height);

     if($img->imageData)
           cvReleaseData($img);
     cvInitImageHeader($img,sz,IPL_DEPTH_16S,channels,IPL_ORIGIN_TL,4);
     cvCreateData($img);

     imgptr = $img->imageData;
     Midx_begin_bloop1( tmp, $mat, short)
            matptr = IDX_PTR( &tmp, short);
            memcpy((void *)imgptr,(void *)matptr,rowbytes);
            imgptr += $img->widthStep;
     Midx_end_bloop1( tmp, $mat, uchar)
     } #}
     ())
     

(de idx32s_ipl (mat img)
    ((-gptr- "IplImage *") img)
    ((-idx3- (-int-)) mat)
     #{ {
     int width,height,channels,rowbytes;
     char * imgptr;
     int * matptr;
     CvSize sz;


     height = Midx_dim($mat,0);
     width  = Midx_dim($mat,1);
     channels = Midx_dim($mat,2);
     rowbytes = Midx_mod($mat,0)*sizeof(int);
     sz=cvSize(width,height);

     if($img->imageData)
           cvReleaseData($img);
     cvInitImageHeader($img,sz,IPL_DEPTH_32S,channels,IPL_ORIGIN_TL,4);
     cvCreateData($img);

     imgptr = $img->imageData;
     Midx_begin_bloop1( tmp, $mat, int)
            matptr = IDX_PTR( &tmp, int);
            memcpy((void *)imgptr,(void *)matptr,rowbytes);
            imgptr += $img->widthStep;
     Midx_end_bloop1( tmp, $mat, uchar)
     } #}
     ())
     

(de idx32f_ipl (mat img)
    ((-gptr- "IplImage *") img)
    ((-idx3- (-float-)) mat)
     #{ {
     int width,height,channels,rowbytes;
     char * imgptr;
     float * matptr;
     CvSize sz;


     height = Midx_dim($mat,0);
     width  = Midx_dim($mat,1);
     channels = Midx_dim($mat,2);
     rowbytes = Midx_mod($mat,0)*sizeof(float);
     sz=cvSize(width,height);

     if($img->imageData)
           cvReleaseData($img);
     cvInitImageHeader($img,sz,IPL_DEPTH_32F,channels,IPL_ORIGIN_TL,4);
     cvCreateData($img);

     imgptr = $img->imageData;
     Midx_begin_bloop1( tmp, $mat, float)
            matptr = IDX_PTR( &tmp, float);
            memcpy((void *)imgptr,(void *)matptr,rowbytes);
            imgptr += $img->widthStep;
     Midx_end_bloop1( tmp, $mat, uchar)
     } #}
     ())
               

(de idx64f_ipl (mat img)
    ((-gptr- "IplImage *") img)
    ((-idx3- (-double-)) mat)
     #{ {
     int width,height,channels,rowbytes;
     char * imgptr;
     double * matptr;
     CvSize sz;


     height = Midx_dim($mat,0);
     width  = Midx_dim($mat,1);
     channels = Midx_dim($mat,2);
     rowbytes = Midx_mod($mat,0)*sizeof(double);
     sz=cvSize(width,height);

     if($img->imageData)
           cvReleaseData($img);
     cvInitImageHeader($img,sz,IPL_DEPTH_64F,channels,IPL_ORIGIN_TL,4);
     cvCreateData($img);

     imgptr = $img->imageData;
     Midx_begin_bloop1( tmp, $mat, double)
            matptr = IDX_PTR( &tmp, double);
            memcpy((void *)imgptr,(void *)matptr,rowbytes);
            imgptr += $img->widthStep;
     Midx_end_bloop1( tmp, $mat, uchar)
     } #}
     ())

     
(de ipl8u_idx_share (img mat)
    ((-gptr- "IplImage *") img)
    ((-idx3- (-ubyte-)) mat)
     #{ {
     Midx_setdim3($mat,$img->height,$img->width,$img->nChannels);
     Midx_update_mod_from_dim($mat);
     ($mat)->srg->data = (uchar *)$img->imageData;
     } #}
     ())
           
 
(de ipl8s_idx_share (img mat)
    ((-gptr- "IplImage *") img)
    ((-idx3- (-byte-)) mat)
     #{ {
     Midx_setdim3($mat,$img->height,$img->width,$img->nChannels);
     Midx_update_mod_from_dim($mat);
     ($mat)->srg->data = (signed char *)$img->imageData;
     } #}
     ())
                    
(de ipl16s_idx_share (img mat)
    ((-gptr- "IplImage *") img)
    ((-idx3- (-short-)) mat)
     #{ {
     Midx_setdim3($mat,$img->height,$img->width,$img->nChannels);
     Midx_update_mod_from_dim($mat);
     ($mat)->srg->data  = $img->imageData;
     } #}
     ())
                    
     

(de ipl32s_idx_share (img mat)
    ((-gptr- "IplImage *") img)
    ((-idx3- (-int-)) mat)
     #{ {
     Midx_setdim3($mat,$img->height,$img->width,$img->nChannels);
     Midx_update_mod_from_dim($mat);
     ($mat)->srg->data = $img->imageData;
     } #}
     ())



(de ipl32f_idx_share (img mat)
    ((-gptr- "IplImage *") img)
    ((-idx3- (-float-)) mat)
     #{ {
     Midx_setdim3($mat,$img->height,$img->width,$img->nChannels);
     Midx_update_mod_from_dim($mat);
     ($mat)->srg->data = $img->imageData;
     } #}
     ())
           

(de ipl64f_idx_share (img mat)
    ((-gptr- "IplImage *") img)
    ((-idx3- (-double-)) mat)
     #{ {
     Midx_setdim3($mat,$img->height,$img->width,$img->nChannels);
     Midx_update_mod_from_dim($mat);
     ($mat)->srg->data = $img->imageData;
     } #}
     ())
           
     
(de idx8u_ipl_share (mat img)
    ((-gptr- "IplImage *") img)
    ((-idx3- (-ubyte-)) mat)
     #{ {
     int width,height,channels,rowbytes;
     CvSize sz;

     height = Midx_dim($mat,0);
     width  = Midx_dim($mat,1);
     channels = Midx_dim($mat,2);
     rowbytes = Midx_mod($mat,0)*sizeof(unsigned char);
     sz=cvSize(width,height);

     if($img->imageData)
           cvReleaseData($img);

     cvInitImageHeader($img,sz,IPL_DEPTH_8U,channels,IPL_ORIGIN_TL,4);
     $img->imageData = (char *) IDX_PTR($mat, uchar);
     } #}
     ())
 

(de idx8s_ipl_share (mat img)
    ((-gptr- "IplImage *") img)
    ((-idx3- (-byte-)) mat)
     #{ {
     int width,height,channels,rowbytes;
     CvSize sz;


     height = Midx_dim($mat,0);
     width  = Midx_dim($mat,1);
     channels = Midx_dim($mat,2);
     rowbytes = Midx_mod($mat,0)*sizeof(signed char);
     sz=cvSize(width,height);

     if($img->imageData)
           cvReleaseData($img);

     cvInitImageHeader($img,sz,IPL_DEPTH_8S,channels,IPL_ORIGIN_TL,4);
     $img->imageData = (char *)IDX_PTR($mat, signed char);
     } #}
     ())
     

(de idx16s_ipl_share (mat img)
    ((-gptr- "IplImage *") img)
    ((-idx3- (-short-)) mat)
     #{ {
     int width,height,channels,rowbytes;
     CvSize sz;

     height = Midx_dim($mat,0);
     width  = Midx_dim($mat,1);
     channels = Midx_dim($mat,2);
     rowbytes = Midx_mod($mat,0)*sizeof(short);
     sz=cvSize(width,height);

     if($img->imageData)
           cvReleaseData($img);

     cvInitImageHeader($img,sz,IPL_DEPTH_16S,channels,IPL_ORIGIN_TL,4);
     $img->imageData = (char *) IDX_PTR($mat, short);
     } #}
     ())
     

(de idx32s_ipl_share (mat img)
    ((-gptr- "IplImage *") img)
    ((-idx3- (-int-)) mat)
     #{ {
     int width,height,channels,rowbytes;
     CvSize sz;

     height = Midx_dim($mat,0);
     width  = Midx_dim($mat,1);
     channels = Midx_dim($mat,2);
     rowbytes = Midx_mod($mat,0)*sizeof(int);
     sz=cvSize(width,height);

     if($img->imageData)
           cvReleaseData($img);

     cvInitImageHeader($img,sz,IPL_DEPTH_32S,channels,IPL_ORIGIN_TL,4);
     $img->imageData = (char *)IDX_PTR($mat, int);
     } #}
     ())
     

(de idx32f_ipl_share (mat img)
    ((-gptr- "IplImage *") img)
    ((-idx3- (-float-)) mat)
     #{ {
     int width,height,channels,rowbytes;
     CvSize sz;

     height = Midx_dim($mat,0);
     width  = Midx_dim($mat,1);
     channels = Midx_dim($mat,2);
     rowbytes = Midx_mod($mat,0)*sizeof(float);
     sz=cvSize(width,height);

     if($img->imageData)
           cvReleaseData($img);

     cvInitImageHeader($img,sz,IPL_DEPTH_32F,channels,IPL_ORIGIN_TL,4);
     $img->imageData = (char *)IDX_PTR($mat, float);
     } #}
     ())
               

(de idx64f_ipl_share (mat img)
    ((-gptr- "IplImage *") img)
    ((-idx3- (-double-)) mat)
     #{ {
     int width,height,channels,rowbytes;
     CvSize sz;

     height = Midx_dim($mat,0);
     width  = Midx_dim($mat,1);
     channels = Midx_dim($mat,2);
     rowbytes = Midx_mod($mat,0)*sizeof(double);
     sz=cvSize(width,height);

     if($img->imageData)
           cvReleaseData($img);

     cvInitImageHeader($img,sz,IPL_DEPTH_64F,channels,IPL_ORIGIN_TL,4);
     $img->imageData = (char *)IDX_PTR($mat, double);
     } #}
     ())


(de print_ipl_info (s)
    ((-gptr- "IplImage *") s)
    #{ printf(" nSize: %d\n", $s->nSize);
     printf(" ID: %d\n", $s->ID);
     printf(" nChannels: %d\n", $s->nChannels);
     printf(" alphaChannel: %d\n", $s->alphaChannel);
     printf(" depth: %d\n", $s->depth);
     printf(" dataOrder: %d\n", $s->dataOrder);
     printf(" origin: %d\n", $s->origin);
     printf(" align: %d\n", $s->align);
     printf(" width: %d\n", $s->width);
     printf(" height: %d\n", $s->height);
     printf(" imageSize: %d\n", $s->imageSize);
     printf(" widthStep: %d\n", $s->widthStep); #}
     ())


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

(dhc-make ()  
  |dummy_aux_convert|
  ipl8u-idx
  ipl8s-idx
  ipl16s-idx
  ipl32s-idx
  ipl32f-idx
  ipl64f-idx
  idx8u-ipl
  idx8s-ipl
  idx16s-ipl
  idx32s-ipl
  idx32f-ipl
  idx64f-ipl
  ipl8u-idx-share
  ipl8s-idx-share
  ipl16s-idx-share
  ipl32s-idx-share
  ipl32f-idx-share
  ipl64f-idx-share
  idx8u-ipl-share
  idx8s-ipl-share
  idx16s-ipl-share
  idx32s-ipl-share
  idx32f-ipl-share
  idx64f-ipl-share
  print-ipl-info
)
