Tcl/Tk Insight: 位图 (bitmap)

@ 2009-07-03 16:05:51
标签:
    «目录»

    位图(bitmap)是使用像素阵列来表示的图像。Tk中的位图(http://tmml.sourceforge.net/doc/tk/bitmap.html|bitmap)的每一个像素可以显示两种颜色中的一种或者透明。Bitmap 用来填充图形或者用做小图标很不错。下面是Tk中自带的bitmap图例。

    定义一个 bitmap 需要四种元素:

    1. 背景色
    2. 前景色
    3. source data
    4. mask data

    source data和mask data中用可以看作是一个bit array。对于 mask data 中 bit=0 对应的地方不显示任何色彩,也就是透明的。 在source data中,bit=1的地方显示前景色,bit=0的地方显示背景色。这样的点阵就构成了图画。

    Tk 中可以通过 "image create bitmap ?name? ?options?"来创建位图。

    创建一个bitmap的例子(来自:http://wiki.tcl.tk/9862)。

    set data {
         #define data_width 16
         #define data_height 16
         static unsigned char data_bits[] = {
           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0,
           0x03, 0xe0, 0x07, 0xe0, 0x07, 0xe0, 0x07, 0xe0, 0x07, 0xc0, 0x03,
           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
       }
    
       set mask {
         #define mask_width 16
         #define mask_height 16
         static unsigned char mask_bits[] = {
           0xe0, 0x07, 0xf8, 0x1f, 0xfc, 0x3f, 0x3e, 0x7c, 0x0e, 0x70, 0xcf,
           0xf3, 0xe7, 0xe7, 0xe7, 0xe7, 0xe7, 0xe7, 0xe7, 0xe7, 0xcf, 0xf3,
           0x0e, 0x70, 0x3e, 0x7c, 0xfc, 0x3f, 0xf8, 0x1f, 0xe0, 0x07 };
       }
    
       set image [image create bitmap \
         -data $data \
         -maskdata $mask \
         -background blue \
         -foreground red \
       ]
    
       label .l -bg yellow -fg magenta -image $image
    
       catch {.l configure -compound left -text "This bitmap image has 2 colors"}
    
       pack .l
    
    标签:

      分享到:
      comments powered by Disqus

      28/31ms