2008-09-16 60 views
4

我已經編寫了讀取Windows位圖的代碼,現在想用ltk顯示它。我怎樣才能構建一個合適的對象? ltk中是否有這樣的功能?如果不是,我怎麼能直接連接到tk呢?如何用ltk顯示圖像?

回答

3

發現它已經有一段時間,因爲我用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圖像你的位圖。但是你說你要操縱所顯示的圖像,並期待在定義圖像對象的代碼:

(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 [email protected][ -width ~a~][email protected][ -height ~a~][email protected][ -format \"~a\"~][email protected][ -grayscale~*~][email protected][ -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,你可能會失敗。

2

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

+0

在這種情況下,我的問題是:如何獲得數據?我想申請一些圖像過濾器。 – Sarien 2008-09-17 08:13:08