<commit_before>;;;; -*- mode: lisp; indent-tabs: nil -*-
;;;; core.lisp
;;;; OpenCV bindings for SBCL
;;;; Core functionality
(in-package :cl-opencv)


;;; Basic Structures

;; We do some heinous kludging here for now. CFFI cannot pass structs
;; by value, which is almost always how OpenCV uses CvSize and
;; CvPoint. We get around this by treating the these structs as a
;; 64-bit integer, which CFFI can handle (at least on some
;; platforms). The correct answer is to write some glue C code to
;; translate structs passed by value to pointers.

(defmacro make-structure-serializers (struct slot1 slot2)
  "Create a serialization and deserialization functionf for the
structure STRUCT with integer slots SLOT1 and SLOT2. These functions
will pack and unpack the structure into an INT64."
  (let ((pack-fn (intern (concatenate 'string (string struct) 
				      (string '->int64))))
	(slot1-fn (intern (concatenate 'string (string struct) "-" 
				       (string slot1))))
	(slot2-fn (intern (concatenate 'string (string struct) "-" 
				       (string slot2))))
	(unpack-fn (intern (concatenate 'string (string 'int64->) 
					(string struct))))
	(make-fn (intern (concatenate 'string (string 'make-) 
				      (string struct)))))
    `(progn
       (defun ,pack-fn (s)
	 (+ (,slot1-fn s) (ash (,slot2-fn s) 32)))
       (defun ,unpack-fn (n)
	 (,make-fn ,slot1 (logand n #x00000000ffffffff)
		   ,slot2 (ash n -32))))))

;; CvPoint
(defstruct cv-point (x 0) (y 0))
(make-structure-serializers :cv-point :x :y)

;; CvSize
(defstruct cv-size (width 0) (height 0))
(make-structure-serializers :cv-size :width :height)

;; CvRect
;; More kludging Lisp structs to 64-bit integers which are really C structs.
(defstruct cv-rect (x 0) (y 0) (width 0) (height 0))

(defun cv-rect->int64s (r)
  "Convert a cv-rect struct R into two 64-bit integers."
  (let ((i1 (+ (cv-rect-x r) (ash (cv-rect-y r) 32)))
	(i2 (+ (cv-rect-width r) (ash (cv-rect-height r) 32))))
    (list i1 i2)))

;; CvScalar 
;; In OpenCV a scalar is a struct whose single member is an array of
;; four double values. We just use a list with values coerced to
;; double. We provide a helper function to create a Lisp version of a
;; scalar. The helper ensures that there are only four values in the
;; scalar.
(defun make-cv-scalar (&rest args)
  (mapcar #'(lambda (v) (coerce v 'double-float))
	  (cond ((> (length args) 4) (subseq args 0 4))
		((< (length args) 4) 
		 (do ((new-args (reverse args)))
		     ((= (length new-args) 4) (reverse new-args))
		   (push 0 new-args)))
		(t args))))

;; CvMat
(defctype cv-matrix :pointer)

;; IplImage
(defanonenum
  (+ipl-depth-1u+ 1)
  (+ipl-depth-8u+ 8)
  (+ipl-depth-16u+ 16)
  (+ipl-depth-32f+ 32)
  (+ipl-depth-64f+ 64)
  (+ipl-depth-8s+ #x80000008)
  (+ipl-depth-16s+ #x80000010)
  (+ipl-depth-32s+ #x80000020))

(defctype ipl-image :pointer)

;; CvArr
(defctype cv-array :pointer)




<commit_msg>Fixed bug with creating images with signed types.<commit_after>;;;; -*- mode: lisp; indent-tabs: nil -*-
;;;; core.lisp
;;;; OpenCV bindings for SBCL
;;;; Core functionality
(in-package :cl-opencv)


;;; Basic Structures

;; We do some heinous kludging here for now. CFFI cannot pass structs
;; by value, which is almost always how OpenCV uses CvSize and
;; CvPoint. We get around this by treating the these structs as a
;; 64-bit integer, which CFFI can handle (at least on some
;; platforms). The correct answer is to write some glue C code to
;; translate structs passed by value to pointers.

(defmacro make-structure-serializers (struct slot1 slot2)
  "Create a serialization and deserialization functionf for the
structure STRUCT with integer slots SLOT1 and SLOT2. These functions
will pack and unpack the structure into an INT64."
  (let ((pack-fn (intern (concatenate 'string (string struct) 
				      (string '->int64))))
	(slot1-fn (intern (concatenate 'string (string struct) "-" 
				       (string slot1))))
	(slot2-fn (intern (concatenate 'string (string struct) "-" 
				       (string slot2))))
	(unpack-fn (intern (concatenate 'string (string 'int64->) 
					(string struct))))
	(make-fn (intern (concatenate 'string (string 'make-) 
				      (string struct)))))
    `(progn
       (defun ,pack-fn (s)
	 (+ (,slot1-fn s) (ash (,slot2-fn s) 32)))
       (defun ,unpack-fn (n)
	 (,make-fn ,slot1 (logand n #x00000000ffffffff)
		   ,slot2 (ash n -32))))))

;; CvPoint
(defstruct cv-point (x 0) (y 0))
(make-structure-serializers :cv-point :x :y)

;; CvSize
(defstruct cv-size (width 0) (height 0))
(make-structure-serializers :cv-size :width :height)

;; CvRect
;; More kludging Lisp structs to 64-bit integers which are really C structs.
(defstruct cv-rect (x 0) (y 0) (width 0) (height 0))

(defun cv-rect->int64s (r)
  "Convert a cv-rect struct R into two 64-bit integers."
  (let ((i1 (+ (cv-rect-x r) (ash (cv-rect-y r) 32)))
	(i2 (+ (cv-rect-width r) (ash (cv-rect-height r) 32))))
    (list i1 i2)))

;; CvScalar 
;; In OpenCV a scalar is a struct whose single member is an array of
;; four double values. We just use a list with values coerced to
;; double. We provide a helper function to create a Lisp version of a
;; scalar. The helper ensures that there are only four values in the
;; scalar.
(defun make-cv-scalar (&rest args)
  (mapcar #'(lambda (v) (coerce v 'double-float))
	  (cond ((> (length args) 4) (subseq args 0 4))
		((< (length args) 4) 
		 (do ((new-args (reverse args)))
		     ((= (length new-args) 4) (reverse new-args))
		   (push 0 new-args)))
		(t args))))

;; CvMat
(defctype cv-matrix :pointer)

;; IplImage
(defanonenum
  (+ipl-depth-1u+ 1)
  (+ipl-depth-8u+ 8)
  (+ipl-depth-16u+ 16)
  (+ipl-depth-32f+ 32)
  (+ipl-depth-64f+ 64)
  (+ipl-depth-sign+ -2147483648)  ; IPL_DEPTH_SIGN is #x80000000
  (+ipl-depth-8s+ -2147483640)    ; IPL_DEPTH_SIGN | 8
  (+ipl-depth-16s+ -2147483632)   ; IPL_DEPTH_SIGN | 16
  (+ipl-depth-32s+ -2147483616))  ; IPL_DEPTH_SIGN | 32

(defctype ipl-image :pointer)

;; CvArr
(defctype cv-array :pointer)





{'fraction_non_alphanumeric': 0.1475928702315509, 'fraction_numerical': 0.03981342662002332, 'mean_word_length': 3.5279034690799396, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 5, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6973191', 'n_tokens_mistral': 2391, 'n_tokens_neox': 2110, 'n_words': 754}