#
#  Xpaint が生成した XPM ファイルを TCL に変換するルーチン
#  行数をもっと少なくするバージョン
#
#  Usage:
#   wish -f maketviicon.tcl foo.xpm       foo       >  foo.tcl
#                           ↑xpmファイル ↑イメージ名 ↑出力ファイル名
text .textin
text .textout
pack .textin .textout
set image [lindex $argv 1]

set inputfile [open [lindex $argv 0]]
.textin insert end [read $inputfile 100000000]
close $inputfile
.textin delete "end -1 c" end

set width   [lindex [.textin get 3.1 "3.1 lineend -2 c" ] 0]
set height  [lindex [.textin get 3.1 "3.1 lineend -2 c" ] 1]
set num_col [lindex [.textin get 3.1 "3.1 lineend -2 c" ] 2]

for {set i 1} {$i <= $num_col} {incr i} {
   set col_sym($i) [.textin get [expr 3 + $i].1 [expr 3 + $i].2 ]
   set color($i)   [lindex [.textin get [expr 3 + $i].2 \
                               "[expr 3 + $i].2 lineend -2 c" ] 1 ]
}

for {set j 1} {$j <= $height} {incr j} {
    set iflag 0
    for {set i 1} {$i <= $width} {incr i} {
       set col_now [.textin get \
         [expr 3 + $num_col + $j].$i [expr 3 + $num_col + $j].[expr $i +1]]
       for {set k 2} {$k <= $num_col} {incr k} {
          if {[string compare $col_now $col_sym($k)] == 0 } {

              # もし前と同じy座標で隣り合ったx座標で
              # 同じ色を出そうとしていたら，
              # 前の行を消して一体化したものを出す
              set before_line [.textout get "end -2c linestart" \
                                         "end -2c lineend"]
              set before_col [lindex $before_line 2]
              set before_col [lindex $before_col 0]
              set before_col [string trim $before_col]
              set before_col [lindex $before_col 0]
                  #.textout insert 1.0 $before_col
              set num_list [llength $before_line]
                  #.textout insert 1.0 $num_list
              set before_y   [lindex $before_line [expr $num_list -1]]
              set now_y  [expr $j -1]
              if {($before_col==$color($k)) && ($before_y==$now_y) && ($iflag==$i)} {
                  .textout delete "end -2c linestart" "end -2c lineend +1c"

                  set ins_char [string trimright $before_line \
                                                  "-to1234567890 " ]
                  set ins_char [string trimright $ins_char "\{\} " ]
                  .textout insert end $ins_char
                  .textout insert end  " " 
                  .textout insert end  $color($k)
                  #.textout insert end " { { "
                  .textout insert end " } } "
                  .textout insert end  "-to "
                  .textout insert end  [lindex $before_line [expr $num_list -2]]
                  .textout insert end  " " 
                  .textout insert end  [expr $j -1]
                  .textout insert end  " " 
                  .textout insert end \n
                  set iflag [expr $i+1]
             # そうでない場合はただ出す
              } else {
                  .textout insert end "$image put"
                  .textout insert end " { { "
                  .textout insert end  $color($k)
                  .textout insert end " } } "
                  .textout insert end  "-to "
                  .textout insert end  [expr $i -1]
                  .textout insert end  " " 
                  .textout insert end  [expr $j -1]
                  .textout insert end  " " 
                  .textout insert end \n
                  set iflag [expr $i+1]
              }
          }
       }
    }
}

puts [.textout get 1.0 "end - 2 c"]

exit

           #       for {set m 0} {$m <= [expr $num_list -4]} {incr m} {
           #           .textout insert end [lindex $before_line $m]
           #           .textout insert end  " " 
           #       }
           #       .textout insert end  $color($k)
           #       .textout insert end  "}"
           #       .textout insert end  " " 
           #       .textout insert end  " -to "
           #       .textout insert end  [lindex $before_line 4]
           #       .textout insert end  " " 
           #       .textout insert end  [lindex $before_line \
           #                                     [expr $num_list -1]]
           #       .textout insert end  " " 
           #       .textout insert end \n
              # そうでない場合はただ出す
