wxpyimgview_gui.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: wxpyimgview
  5. # AUTHOR(S): Glynn Clements <glynn@gclements.plus.com>
  6. # COPYRIGHT: (C) 2010 Glynn Clements
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # /
  19. #%module
  20. #% description: Views BMP images from the PNG driver.
  21. #% keyword: display
  22. #% keyword: raster
  23. #%end
  24. #%option G_OPT_F_INPUT
  25. #% key: image
  26. #% description: Name of input image file
  27. #%end
  28. #%option
  29. #% key: percent
  30. #% type: integer
  31. #% required: no
  32. #% multiple: no
  33. #% description: Percentage of CPU time to use
  34. #% answer: 10
  35. #%end
  36. import sys
  37. import struct
  38. import numpy
  39. import time
  40. import signal
  41. import wxversion
  42. wxversion.select('2.8')
  43. import wx
  44. class Frame(wx.Frame):
  45. title = "Image Viewer"
  46. def __init__(self, app, size):
  47. self.app = app
  48. wx.Frame.__init__(self, None, title=Frame.title, size=size)
  49. self.Create()
  50. def Create(self):
  51. self.Bind(wx.EVT_ERASE_BACKGROUND, self.erase)
  52. self.Bind(wx.EVT_PAINT, self.redraw)
  53. self.Bind(wx.EVT_TIMER, self.tick, id=1)
  54. self.timer = wx.Timer(self, 1)
  55. self.timer.Start(100, True)
  56. # Python doesn't receive signals while wx is waiting for an event
  57. self.Bind(wx.EVT_TIMER, self.dummy, id=2)
  58. self.ticker = wx.Timer(self, 2)
  59. self.ticker.Start(100, False)
  60. def erase(self, ev):
  61. ev.GetDC()
  62. def draw(self):
  63. app = self.app
  64. size = self.GetSize()
  65. x0 = (size.GetWidth() - app.i_width) / 2
  66. y0 = (size.GetHeight() - app.i_height) / 2
  67. dc = wx.PaintDC(self)
  68. data = app.imgbuf.reshape((app.i_height, app.i_width, 4))
  69. data = data[::, ::, 2::-1]
  70. fn = getattr(data, "tobytes", getattr(data, "tostring"))
  71. image = wx.ImageFromData(app.i_width, app.i_height, fn())
  72. dc.DrawBitmap(wx.BitmapFromImage(image), x0, y0, False)
  73. def redraw(self, ev):
  74. if self.app.fraction > 0.001:
  75. t0 = time.time()
  76. self.draw()
  77. t1 = time.time()
  78. last = t1 - t0
  79. delay = last / self.app.fraction
  80. self.timer.Start(int(delay * 1000), True)
  81. else:
  82. self.draw()
  83. def tick(self, ev):
  84. self.Refresh()
  85. def dummy(self, ev):
  86. pass
  87. class Application(wx.App):
  88. def __init__(self):
  89. self.image = sys.argv[1]
  90. self.fraction = int(sys.argv[2]) / 100.0
  91. self.HEADER_SIZE = 64
  92. wx.App.__init__(self)
  93. def read_bmp_header(self, header):
  94. magic, bmfh, bmih = struct.unpack("2s12s40s10x", header)
  95. if magic != 'BM':
  96. raise SyntaxError("Invalid magic number")
  97. size, res1, res2, hsize = struct.unpack("<IHHI", bmfh)
  98. if hsize != self.HEADER_SIZE:
  99. raise SyntaxError("Invalid file header size")
  100. hsize, width, height, planes, bpp, compression, imsize, xppm, yppm, cused, cimp = \
  101. struct.unpack("<IiiHHIIiiII", bmih)
  102. if hsize != 40:
  103. raise SyntaxError("Invalid info header size")
  104. self.i_width = width
  105. self.i_height = -height
  106. if planes != 1:
  107. raise SyntaxError("Planar data not supported")
  108. if bpp != 32:
  109. raise SyntaxError("Only 32-BPP images supported")
  110. if compression != 0:
  111. raise SyntaxError("Compression not supported")
  112. if imsize != self.i_width * self.i_height * 4:
  113. raise SyntaxError("Invalid image data size")
  114. if size != self.HEADER_SIZE + self.i_width * self.i_height * 4:
  115. raise SyntaxError("Invalid image size")
  116. def map_file(self):
  117. f = open(self.image, 'r')
  118. header = f.read(self.HEADER_SIZE)
  119. self.read_bmp_header(header)
  120. self.imgbuf = numpy.memmap(f, mode='r', offset=self.HEADER_SIZE)
  121. def signal_handler(self, sig, frame):
  122. wx.CallAfter(self.mainwin.Refresh)
  123. def set_handler(self):
  124. if 'SIGUSR1' in dir(signal):
  125. signal.signal(signal.SIGUSR1, self.signal_handler)
  126. def OnInit(self):
  127. self.map_file()
  128. size = wx.Size(self.i_width, self.i_height)
  129. self.mainwin = Frame(self, size)
  130. self.mainwin.Show()
  131. self.SetTopWindow(self.mainwin)
  132. self.set_handler()
  133. return True
  134. if __name__ == "__main__":
  135. app = Application()
  136. app.MainLoop()