如何使用 ltk 显示图像?

发布于 2024-07-05 13:05:47 字数 94 浏览 7 评论 0原文

我已经编写了读取 Windows 位图的代码,现在想用 ltk 显示它。 我怎样才能构造一个合适的对象? ltk有这样的功能吗? 如果不是,我怎样才能直接连接到 tk?

I have written code to read a windows bitmap and would now like to display it with ltk. How can I construct an appropriate object? Is there such functionality in ltk? If not how can I do it directly interfacing to tk?

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

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

发布评论

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

评论(2

满身野味 2024-07-12 13:05:47

我已经有一段时间没有使用 LTK 来做任何事情了,但使用 LTK 显示图像的最简单方法如下:

(defpackage #:ltk-image-example
  (:use #:cl #:ltk))

(in-package #:ltk-image-example)

(defun image-example ()
  (with-ltk ()
    (let ((image (make-image)))
      (image-load image "testimage.gif")
      (let ((canvas (make-instance 'canvas)))
        (create-image canvas 0 0 :image image)
        (configure canvas :width 800)
        (configure canvas :height 640)
        (pack canvas)))))

不幸的是,默认情况下您可以对图像执行的操作相当有限,并且只能使用 gif 或 ppm 图像 -但 ppm 文件格式 非常简单,您可以轻松地从位图创建 ppm 图像。 然而,您说您想要操作显示的图像,并查看定义图像对象的代码:

(defclass photo-image(tkobject)
  ((data :accessor data :initform nil :initarg :data)
   )
  )

(defmethod widget-path ((photo photo-image))
  (name photo))

(defmethod initialize-instance :after ((p photo-image)
                                       &key width height format grayscale data)
  (check-type data (or null string))
  (setf (name p) (create-name))
  (format-wish "image create photo ~A~@[ -width ~a~]~@[ -height ~a~]~@[ -format \"~a\"~]~@[ -grayscale~*~]~@[ -data ~s~]"
               (name p) width height format grayscale data))

(defun make-image ()
  (let* ((name (create-name))
     (i (make-instance 'photo-image :name name)))
    ;(create i)
    i))

(defgeneric image-load (p filename))
(defmethod image-load((p photo-image) filename)
  ;(format t "loading file ~a~&" filename)
  (send-wish (format nil "~A read {~A} -shrink" (name p) filename))
  p)

看起来图像的实际数据是由 Tcl/Tk 解释器存储的,并且无法从 lisp 中访问。 如果您想访问它,您可能需要使用 format-wishsend-wish 编写自己的函数。

当然,您可以简单地在画布对象上单独渲染每个像素,但我认为这样做不会获得很好的性能,一旦您尝试在画布上显示数千个不同的内容,画布小部件就会变得有点慢它。 总结一下 - 如果您不关心实时执行任何操作,则可以在每次想要显示位图时将其保存为 .ppm 图像,然后只需使用上面的代码加载它 - 这将是最简单的。 否则,您可以尝试从 tk 本身访问数据(将其作为 ppm 图像加载一次后),最后,如果这些都不起作用,您可以切换到另一个工具包。 大多数像样的 lisp GUI 工具包都是针对 Linux 的,所以如果你使用的是 Windows,你可能会运气不好。

It has been a while since I used LTK for anything, but the simplest way to display an image with LTK is as follows:

(defpackage #:ltk-image-example
  (:use #:cl #:ltk))

(in-package #:ltk-image-example)

(defun image-example ()
  (with-ltk ()
    (let ((image (make-image)))
      (image-load image "testimage.gif")
      (let ((canvas (make-instance 'canvas)))
        (create-image canvas 0 0 :image image)
        (configure canvas :width 800)
        (configure canvas :height 640)
        (pack canvas)))))

Unfortunately what you can do with the image by default is fairly limited, and you can only use gif or ppm images - but the ppm file format is very simple, you could easily create a ppm image from your bitmap. However you say you want to manipulate the displayed image, and looking at the code that defines the image object:

(defclass photo-image(tkobject)
  ((data :accessor data :initform nil :initarg :data)
   )
  )

(defmethod widget-path ((photo photo-image))
  (name photo))

(defmethod initialize-instance :after ((p photo-image)
                                       &key width height format grayscale data)
  (check-type data (or null string))
  (setf (name p) (create-name))
  (format-wish "image create photo ~A~@[ -width ~a~]~@[ -height ~a~]~@[ -format \"~a\"~]~@[ -grayscale~*~]~@[ -data ~s~]"
               (name p) width height format grayscale data))

(defun make-image ()
  (let* ((name (create-name))
     (i (make-instance 'photo-image :name name)))
    ;(create i)
    i))

(defgeneric image-load (p filename))
(defmethod image-load((p photo-image) filename)
  ;(format t "loading file ~a~&" filename)
  (send-wish (format nil "~A read {~A} -shrink" (name p) filename))
  p)

It looks like the the actual data for the image is stored by the Tcl/Tk interpreter and not accessible from within lisp. If you wanted to access it you would probably need to write your own functions using format-wish and send-wish.

Of course you could simply render each pixel individually on a canvas object, but I don't think you would get very good performance doing that, the canvas widget gets a bit slow once you are trying to display more than a few thousand different things on it. So to summarize - if you don't care about doing anything in real time, you could save your bitmap as a .ppm image every time you wanted to display it and then simply load it using the code above - that would be the easiest. Otherwise you could try to access the data from tk itself (after loading it once as a ppm image), finally if none of that works you could switch to another toolkit. Most of the decent lisp GUI toolkits are for linux, so you may be out of luck if you are using windows.

ぃ双果 2024-07-12 13:05:47

Tk 本身不支持 Windows 位图文件。 然而,“Img”扩展确实可以在几乎所有平台上免费使用。 您不需要读入数据,可以直接从磁盘上的文件创建图像。 在普通的 tcl/tk 中,您的代码可能如下所示:

package require Img
set image [image create photo -file /path/to/image.bmp]
label .l -image $image
pack .l

可以在 http://wiki 找到更多信息.tcl.tk/6165

Tk does not natively support windows bitmap files. However, the "Img" extension does and is freely available on just about every platform. You do not need to read the data in, you can create the image straight from the file on disk. In plain tcl/tk your code might look something like this:

package require Img
set image [image create photo -file /path/to/image.bmp]
label .l -image $image
pack .l

a little more information can be found at http://wiki.tcl.tk/6165

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文