home *** CD-ROM | disk | FTP | other *** search
/ PC World 2000 December / PCWorld_2000-12_cd.bin / Komunikace / Comanche / BWidget-1.2 / xpm2image.tcl < prev   
Text File  |  2000-11-02  |  4KB  |  116 lines

  1. # ------------------------------------------------------------------------------
  2. #  xpm2image.tcl
  3. #  Slightly modified xpm-to-image command
  4. #  $Id: xpm2image.tcl,v 1.1.1.1 1996/02/22 06:05:56 daniel Exp $
  5. # ------------------------------------------------------------------------------
  6. #
  7. #  Copyright 1996 by Roger E. Critchlow Jr., San Francisco, California
  8. #  All rights reserved, fair use permitted, caveat emptor.
  9. #  rec@elf.org
  10. # ------------------------------------------------------------------------------
  11.  
  12. proc xpm-to-image { file } {
  13.     set f [open $file]
  14.     set string [read $f]
  15.     close $f
  16.  
  17.     #
  18.     # parse the strings in the xpm data
  19.     #
  20.     set xpm {}
  21.     foreach line [split $string "\n"] {
  22.         if {[regexp {^"([^\"]*)"} $line all meat]} {
  23.             if {[string first XPMEXT $meat] == 0} {
  24.                 break
  25.             }
  26.             lappend xpm $meat
  27.         }
  28.     }
  29.     #
  30.     # extract the sizes in the xpm data
  31.     #
  32.     set sizes  [lindex $xpm 0]
  33.     set nsizes [llength $sizes]
  34.     if { $nsizes == 4 || $nsizes == 6 || $nsizes == 7 } {
  35.         set data(width)   [lindex $sizes 0]
  36.         set data(height)  [lindex $sizes 1]
  37.         set data(ncolors) [lindex $sizes 2]
  38.         set data(chars_per_pixel) [lindex $sizes 3]
  39.         set data(x_hotspot) 0
  40.         set data(y_hotspot) 0
  41.         if {[llength $sizes] >= 6} {
  42.             set data(x_hotspot) [lindex $sizes 4]
  43.             set data(y_hotspot) [lindex $sizes 5]
  44.         }
  45.     } else {
  46.         error "size line {$sizes} in $file did not compute"
  47.     }
  48.  
  49.     #
  50.     # extract the color definitions in the xpm data
  51.     #
  52.     foreach line [lrange $xpm 1 $data(ncolors)] {
  53.         set colors [split $line \t]
  54.         set cname  [lindex $colors 0]
  55.         lappend data(cnames) $cname
  56.         if { [string length $cname] != $data(chars_per_pixel) } {
  57.             error "color definition {$line} in file $file has a bad size color name"
  58.         }
  59.         foreach record [lrange $colors 1 end] {
  60.             set key [lindex $record 0]
  61.             set color [string tolower [join [lrange $record 1 end] { }]]
  62.             set data(color-$key-$cname) $color
  63.             if { ![string compare $color "none"] } {
  64.                 set data(transparent) $cname
  65.             }
  66.         }
  67.         foreach key {c g g4 m} {
  68.             if {[info exists data(color-$key-$cname)]} {
  69.                 set color $data(color-$key-$cname)
  70.                 set data(color-$cname) $color
  71.                 set data(cname-$color) $cname
  72.                 lappend data(colors) $color
  73.                 break
  74.             }
  75.         }
  76.         if { ![info exists data(color-$cname)] } {
  77.             error "color definition {$line} in $file failed to define a color"
  78.         }
  79.     }
  80.  
  81.     #
  82.     # extract the image data in the xpm data
  83.     #
  84.     set image [image create photo -width $data(width) -height $data(height)]
  85.     set y 0
  86.     foreach line [lrange $xpm [expr 1+$data(ncolors)] [expr 1+$data(ncolors)+$data(height)]] {
  87.         set x 0
  88.         set pixels {}
  89.         while { [string length $line] > 0 } {
  90.             set pixel [string range $line 0 [expr {$data(chars_per_pixel)-1}]]
  91.             set c $data(color-$pixel)
  92.             if { ![string compare $c none] } {
  93.                 if { [string length $pixels] } {
  94.                     $image put [list $pixels] -to [expr {$x-[llength $pixels]}] $y
  95.                     set pixels {}
  96.                 }
  97.             } else {
  98.                 lappend pixels $c
  99.             }
  100.             set line [string range $line $data(chars_per_pixel) end]
  101.             incr x
  102.         }
  103.         if { [llength $pixels] } {
  104.             $image put [list $pixels] -to [expr {$x-[llength $pixels]}] $y
  105.         }
  106.         incr y
  107.     }
  108.  
  109.     #
  110.     # return the image
  111.     #
  112.     return $image
  113. }
  114.  
  115.