Updated 2015-07-18 11:43:47 by pooryorick

Find installed photo image format handlers

It is hard to determine at runtime what image formats image create photo id -format ?? can handle. image create photo id -format "x" does not even return an error until a file is tried, even if the file is empty. This is used below. See also Img for what should be supported.
#!/usr/bin/env wish
# abc 2015 / abc @ irc.freenode.net
# see http://wiki.tcl.tk/1404 for what should be supported but isn't yet (8.5 only for now)

# Hack to get the installed image type handlers short name (not for image create) (man n photo)
# V0: tested in 8.5; Returns: list [0 imgtype0 imgtype1 ...] or [-1 errmsg]
proc GetPhotoFormats {{dummyfile "/tmp/dummy.img"}} { ;# arg must be a writable non existing file
  if {[file exists $dummyfile]} {return [list -1 "won't delete $dummyfile"]}
  catch {exec rm $dummyfile}
  if {[catch {exec touch $dummyfile} err]} {return [list -1 $err]}
  set img GetPhotoFormatsDummyImg ;# long unique photo object name
  set r 0; foreach t [lsort {bmp pcx ico gif png xbm png jpg ppm xpm tiff tga pixmap ps tga sgi sun}] {
    catch {image create photo $img -format $t -file $dummyfile;image delete $img} err
    if {[regexp "couldn't recognize data in image file " $err]} {
      lappend r $t
  catch {exec rm $dummyfile}
  return $r

puts [GetPhotoFormats]
exit 0

PYK 2015-07-18: Small modification to make the returned value correspond to the documentation.