[cffi-devel] Help with defcallback

Joeish W joeish80829 at yahoo.com
Mon Nov 11 04:53:57 UTC 2013


I think the issue is with my callback function but not 100% when i run the c code and print
with these 2 lines(from c callback):

cout << "a = " << "(" << "" << "," << _a << ")" << endl ;
          cout << "b = " << "(" << "" << "," << _b << ")" << endl;

 i get 53 elements printed not icluding the other printf output
and  in the lisp code i converterd 

when i print with this line(from lisp defcallback):

(format t "~%x~a~%b~a" _a _b)

 in the defcallback  i get 114 elements

Hoping someone can look at my code and tell me if my coding is off ....the functions you wont recognize are opencv functions
with correctly lispified names  i/e cvSeqSort = seq-sort ...  If requested will post the wrappers ....
the -> is a macro for with-foreign-slots (and if not a pointer just a getf for a plist)

;;;;;;;;C CODE;;;;;;;;;;;;;;;;;;;
      #include <cv.h>
      #include <highgui.h>
      using namespace std;

      static int cmp_func( const void* _a, const void* _b, void* userdata )
      {
        //cout << "test";
      //cout <<_a << _b;
          CvPoint* a = (CvPoint*)_a;
          CvPoint* b = (CvPoint*)_b;
          cout << "a = " << "(" << "" << "," << _a << ")" << endl ;
          cout << "b = " << "(" << "" << "," << _b << ")" << endl;
          int y_diff = a->y - b->y;
          int x_diff = a->x - b->x;
          return y_diff ? y_diff : x_diff;
      }


      int main()
      {
          CvMemStorage* storage = cvCreateMemStorage(0);
          CvSeq* seq = cvCreateSeq( CV_32SC2, sizeof(CvSeq), sizeof(CvPoint), storage );
          int i;
    
          printf("\n=== Test sequence sorting ===");

          for( i= 0; i < 10; i++ )
          {
              CvPoint pt;
              pt.x = rand() % 1000;  // 1000 以内的随机数
              pt.y = rand() % 1000;
              cvSeqPush( seq, &pt );
          }
    
          printf("\nOriginal point set:\n");
          for( i = 0; i < seq->total; i++ )
          {
              CvPoint* pt = (CvPoint*)cvGetSeqElem( seq, i );
              printf( "(%d,%d)\n", pt->x, pt->y );
          }

          cvSeqSort( seq, cmp_func, 0 /* userdata is not used here */ );
    
          /* print out the sorted sequence */
          printf("\nAfter sorting:\n");
          for( i = 0; i < seq->total; i++ )
          {
              CvPoint* pt = (CvPoint*)cvGetSeqElem( seq, i );
              printf( "(%d,%d)\n", pt->x, pt->y );
          }
    
          cvClearSeq( seq );   // Sequence clearing should be done before storage clearing
          cvReleaseMemStorage( &storage );
      }

;;;;;;;;;;;;;;;;;;;;;;;;;LISP CODE;;;;;;;;;;;;;;;;;;;;
(defcallback cmp-func :void  ((_a :pointer) (_b :pointer) (user-data :pointer) (a :int) (b :int) (y-diff :int) (x-diff :int))
  (format t "~%x~a~%b~a" _a _b)
  (setf a (mem-aref _a '(:struct cv-point)))
  (setf b (mem-aref _b '(:struct cv-point)))
                    ;(format t "~%a~a~%b~a" a b)
  (setf y-diff (- (-> cv-point a y) (-> cv-point b y)))
  (setf x-diff (- (-> cv-point a x) (-> cv-point b x)))
  (if y-diff y-diff x-diff))



(defun seq-sort-example ()
  (let* ((storage (create-mem-storage 0))
     (seq (create-seq +32sc2+ (size-of cv-seq) (size-of cv-point) storage)))
    (format t "~%=== Test sequence sorting ===")
    (with-foreign-object (pt '(:struct cv-point))
      (dotimes (i 10)
    (setf (foreign-slot-value pt '(:struct cv-point) 'x) (random 1000)
          (foreign-slot-value pt '(:struct cv-point) 'y) (random 1000))
    (seq-push seq pt)))

    (with-foreign-object (pt '(:pointer (:struct cv-point)))
      (format t "~%Original point set:~%")
      (dotimes (i (-> cv-seq seq total))
    (setf pt (get-seq-elem seq i))
    (format t "(~a, ~a)~%" (-> cv-point pt x) (-> cv-point pt y)  )))
    (seq-sort seq (callback cmp-func) (null-pointer))
    (with-foreign-object (pt '(:pointer (:struct cv-point)))
      (format t "~%After sorting:~%")
      (dotimes (i (-> cv-seq seq total))
    (setf pt (get-seq-elem seq i))
    (format t "(~a, ~a)~%" (-> cv-point pt x) (-> cv-point pt y)  )))
    (clear-seq seq)
    (release-mem-storage storage)))
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.common-lisp.net/pipermail/cffi-devel/attachments/20131110/c36ba782/attachment.html>


More information about the cffi-devel mailing list