home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Extensions / img / test / xtestview.py < prev   
Encoding:
Python Source  |  2000-06-23  |  4.6 KB  |  175 lines

  1. import sys, Xt, Xm, X, img, imgformat, imgconvert
  2.  
  3. imgconvert.settrace(1)
  4.  
  5. error = 'xtestview.error'
  6.  
  7. vselect = {}
  8.  
  9. # handle arguments
  10. i = 1
  11. while i < len(sys.argv):
  12.     if sys.argv[i] == '-visualid':
  13.         del sys.argv[i]
  14.         if i == len(sys.argv):
  15.             raise error, '-visualid needs an argument'
  16.         vselect['visualid'] = eval(sys.argv[i])
  17.         del sys.argv[i]
  18.         continue
  19.     if sys.argv[i] == '-depth':
  20.         del sys.argv[i]
  21.         if i == len(sys.argv):
  22.             raise error, '-depth needs an argument'
  23.         vselect['depth'] = eval(sys.argv[i])
  24.         del sys.argv[i]
  25.         continue
  26.     if sys.argv[i] == '-visual':
  27.         del sys.argv[i]
  28.         if i == len(sys.argv):
  29.             raise error, '-visual needs an argument'
  30.         if sys.argv[i] == 'TrueColor':
  31.             vselect['class'] = X.TrueColor
  32.         elif sys.argv[i] == 'PseudoColor':
  33.             vselect['class'] = X.PseudoColor
  34.         else:
  35.             raise error, 'visual type not supported'
  36.         del sys.argv[i]
  37.         continue
  38.     i = i + 1
  39.  
  40. # default is to use TrueColor visual
  41. if not vselect.has_key('class') and not vselect.has_key('visualid'):
  42.     vselect['class'] = X.TrueColor
  43.  
  44. # initialize window system
  45. Xt.ToolkitInitialize()
  46. dpy = Xt.OpenDisplay(None, None, 'XTestview', [], sys.argv)
  47.  
  48. # find deepest visual that satisfies the requirements.
  49. visuals = dpy.GetVisualInfo(vselect)
  50. if not visuals:
  51.     raise error, 'requested visual does not exist'
  52. visual = visuals[0]
  53. for v in visuals:
  54.     if v.depth > visual.depth and v.depth in (8, 24):
  55.         visual = v
  56.  
  57. if visual.c_class == X.TrueColor:
  58.     vtype = 'TrueColor'
  59. elif visual.c_class == X.PseudoColor:
  60.     vtype = 'PsuedoColor'
  61. else:
  62.     vtype = 'unknown'
  63. print 'Using visual of type %s and depth %d' % (vtype, visual.depth)
  64. # for now ignore the fact that the colors may not be in the right order
  65. if visual.depth == 8:
  66.     format = imgformat.xrgb8
  67.     print 'Using format xrgb8'
  68. elif visual.depth == 24:
  69.     format = imgformat.rgb
  70.     print 'Using format rgb'
  71. else:
  72.     raise error, 'only visuals of depth 8 and 24 supported'
  73.  
  74. # create a colormap
  75. colormap = visual.CreateColormap(X.AllocNone)
  76. if visual.c_class == X.PseudoColor:
  77.     # allocate colormap entries
  78.     (plane_masks, pixels) = colormap.AllocColorCells(1, 8, 1)
  79.     xcolors = []
  80.     for n in range(256):
  81.         xcolors.append(n + pixels[0],
  82.                    int((((n >> 5) & 7) / 7.) * 255.)<<8,
  83.                    int((((n     ) & 7) / 7.) * 255.)<<8,
  84.                    int((((n >> 3) & 3) / 3.) * 255.)<<8,
  85.                    X.DoRed|X.DoGreen|X.DoBlue)
  86.     colormap.StoreColors(xcolors)
  87.     del plane_masks, pixels, xcolors, n
  88.  
  89. # create the main widget
  90. main = Xt.CreateApplicationShell('shell', Xt.ApplicationShell,
  91.                  {'visual': visual,
  92.                   'depth': visual.depth,
  93.                   'colormap': colormap})
  94. widget = main.CreateManagedWidget('image', Xm.DrawingArea, {'width':10, 'height':10})
  95. main.RealizeWidget()
  96. gc = widget.CreateGC({})
  97.  
  98. # clean up a little
  99. del visuals, v, vselect, vtype        # don't need these...
  100.  
  101. class view:
  102.     def __init__(self):
  103.         self.image = None
  104.         widget.AddCallback('exposeCallback', self.expose, None)
  105.         widget.AddCallback('inputCallback', self.input, None)
  106.  
  107.     def view(self, title, fullname):
  108.         self.fullname = fullname
  109.         print 'Reading from',fullname,'...'
  110.         reader = img.reader(format, fullname)
  111.         self.data = reader.read()
  112.         self.width = reader.width
  113.         self.height = reader.height
  114.         main.SetValues({'width': self.width, 'height': self.height,
  115.                 'title': title})
  116.         widget.SetValues({'width': self.width, 'height': self.height})
  117.         self.image = visual.CreateImage(visual.depth, X.ZPixmap, 0,
  118.                         self.data,
  119.                         self.width, self.height,
  120.                         format.descr['align'], 0)
  121.         gc.FillRectangle(0, 0, widget.width, widget.height)
  122.         gc.PutImage(self.image, 0, 0, 0, 0, self.width, self.height)
  123.  
  124.     def expose(self, w, client_data, call_data):
  125.         if self.image is not None:
  126.             gc.PutImage(self.image, 0, 0, 0, 0, self.width, self.height)
  127.  
  128.     def input(self, w, client_data, call_data):
  129.         import Xlib
  130.         event = call_data.event
  131.         if event.type == X.KeyPress:
  132.             string = Xlib.LookupString(event)[0]
  133.             if not string:
  134.                 return
  135.             if string[0] in ('w', 'W'):
  136.                 self.write()
  137.             if string[0] in ('\033', 'q', 'Q'):
  138.                 self.image = None
  139.                 if args:
  140.                     a = args[0]
  141.                     del args[0]
  142.                     self.view(a, a)
  143.                 else:
  144.                     raise error
  145.  
  146.     def write(self):
  147.         import os
  148.         head, tail = os.path.split(self.fullname)
  149.         if not head:
  150.             head = '.'
  151.         newname = head + '/@copy-' + tail
  152.         print 'Writing to',newname,'...'
  153.         try:
  154.             wrr = img.writer(format, newname)
  155.         except img.unsupported_error, arg:
  156.             print '** Failed, no converter for', arg[0].name, \
  157.               'to something supported by this filetype'
  158.             return
  159.         wrr.width, wrr.height = self.width, self.height
  160.         wrr.write(self.data)
  161.  
  162. args = sys.argv[1:]
  163. if not args:
  164.     print 'Usage:',sys.argv[0],'file ...'
  165.     sys.exit(1)
  166. a = args[0]
  167. del args[0]
  168. v = view()
  169. v.view(a, a)
  170.  
  171. try:
  172.     Xt.MainLoop()
  173. except error:
  174.     pass
  175.