save and restore procs
# ============================== # # serializing a bitmap # # ============================== # --------------------- # bitmap:save proc # # return a serialized bitmap # --------------------- # parm1: image name # --------------------- # return: serialized bitmap # --------------------- # use: # bitmap:save name # --------------------- proc bitmap:save {name} \ { # save name set res [list $name] # save options set options {} foreach key {-back -fore -data -maskdata -file -maskfile} \ { set value [$name cget $key] if {$value != ""} { lappend options [list $key $value] } } lappend res $options # return string return $res } # --------------------- # bitmap:restore # # create a bitmap from a serialized one # --------------------- # parm1: serialized bitmap # parm2: optional name option # -noname: don't restore the name # non empty: set the name with parm2 # --------------------- # use: # bitmap:restore bitmap_string ?-noname|name? # --------------------- proc bitmap:restore {image args} \ { # init cmd set cmd "image create bitmap" # set name switch -glob -- $args \ { -non* { } "" { append cmd " [lindex $image 0]" } default { append cmd " $args" } } # set options foreach option [lindex $image 1] { append cmd " $option" } # create bitmap set img [eval $cmd] }
Little demo
# ============================== # # demo # # ============================== # create a bitmap from a string set data "#define v_width 8\n#define v_height 4" append data { static unsigned char v_bits[] = { 0x18, 0x3c, 0x7e, 0xff }; } image create bitmap _img1_ -data $data # create a bitmap from a file set fp [open bitmap.data w] puts $fp $data close $fp image create bitmap _img2_ -file bitmap.data # save bitmaps set saved1 [bitmap:save _img1_] set saved2 [bitmap:save _img2_] # delete bitmaps image delete _img1_ _img2_ # restore bitmaps bitmap:restore $saved1 bitmap:restore $saved2 # show them pack [canvas .c] .c create image 10 10 -image _img1_ .c create image 30 10 -image _img2_