lambda-gtk 负指针

发布于 2024-08-11 17:26:21 字数 581 浏览 1 评论 0原文

我试图用 Lisp 在 (Gdk) pixbuf 上编写自己的 put-pixel 。当我最终意识到如何在 CL 中操作 C 指针时,新的障碍出现了 - (gdk:pixbuf-get-pixels pb) 返回负数。我的问题是:我可以以某种方式将其转换为有效的指针吗?我尝试使用 cffi:convert-from-foreign 和 cffi:translate-from-foreign (它们之间有什么区别?)失败了。

下面是我的实际(不工作)代码:

(defun put-pixel (pixbuf x y r g b)
  (let ((p (+ (gdk:pixbuf-get-pixels pixbuf) (* x (gdk:pixbuf-get-n-channels pixbuf)) (* y (gdk:pixbuf-get-rowstride pixbuf)))))
    (setf (cffi:mem-aref p :unsigned-char 0) r)
    (setf (cffi:mem-aref p :unsigned-char 1) g)
    (setf (cffi:mem-aref p :unsigned-char 2) b)))

I was trying to write my own put-pixel on (Gdk) pixbuf in Lisp. When I finally realized how I can operate on C pointers in CL, new obstacle came along - (gdk:pixbuf-get-pixels pb) returns me negative number. My question is: can I convert it somehow to a valid pointer? My attempts to use cffi:convert-from-foreign and cffi:translate-from-foreign (what's the difference between them anyway?) failed.

Below is my actual (not working) code:

(defun put-pixel (pixbuf x y r g b)
  (let ((p (+ (gdk:pixbuf-get-pixels pixbuf) (* x (gdk:pixbuf-get-n-channels pixbuf)) (* y (gdk:pixbuf-get-rowstride pixbuf)))))
    (setf (cffi:mem-aref p :unsigned-char 0) r)
    (setf (cffi:mem-aref p :unsigned-char 1) g)
    (setf (cffi:mem-aref p :unsigned-char 2) b)))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请别遗忘我 2024-08-18 17:26:21

CFFI:TRANSLATE-FROM-FOREIGN 是一个通用函数。您可以使用 CFFI:DEFINE-FOREIGN-TYPE 定义自己的外部类型,然后添加CFFI:TRANSLATE-FROM-FOREIGN 的方法指定如何从外部值到 Lisp 值的转换。

如果您需要显式转换,您应该调用 CFFI:CONVERT-FROM-FOREIGN一些价值。它将在幕后调用 CFFI:TRANSLATE-FROM-FOREIGN,并且如果可能的话,它可能会执行一些编译时优化。

同样的情况也适用于 CFFI:CONVERT-TO-FOREIGNCFFI:翻译至国外

CFFI:TRANSLATE-FROM-FOREIGN is a generic function. You can define your own foreign types using CFFI:DEFINE-FOREIGN-TYPE and then add a method to CFFI:TRANSLATE-FROM-FOREIGN to specify how the conversions from foreign to Lisp values should work.

CFFI:CONVERT-FROM-FOREIGN is what you should call if you need to explicitly convert some value. It will call CFFI:TRANSLATE-FROM-FOREIGN behind the scenes and it might perform some compile-time optimizations if possible.

Same thing applies to CFFI:CONVERT-TO-FOREIGN and CFFI:TRANSLATE-TO-FOREIGN.

清音悠歌 2024-08-18 17:26:21

我认为 lambda-gtk 错误地定义了 pixbuf-get-pixels 的绑定。

由于将无符号整数错误地解释为有符号整数,可能会出现指针值的负值。

更正此值的最简单方法是使用 mod

CL-USER> (mod -1 (expt 2 #+cffi-features:x86 32 #+cffi-features:x86-64 64))
4294967295

I think that lambda-gtk incorrectly defined binding for pixbuf-get-pixels.

The negative value for pointer value might appear because of incorrect interpretation of unsigned integer as a signed integer.

The simplest way to correct this value is to use mod:

CL-USER> (mod -1 (expt 2 #+cffi-features:x86 32 #+cffi-features:x86-64 64))
4294967295
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文