wxpyimgview_gui.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #!/usr/bin/env python3
  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 os
  37. import signal
  38. import struct
  39. import sys
  40. import time
  41. import grass.script as grass
  42. from grass.script.setup import set_gui_path
  43. set_gui_path()
  44. from gui_core.wrap import BitmapFromImage
  45. import numpy
  46. import wx
  47. class Frame(wx.Frame):
  48. title = "Image Viewer"
  49. def __init__(self, app, size):
  50. self.app = app
  51. wx.Frame.__init__(self, None, title=Frame.title, size=size)
  52. self.Create()
  53. def Create(self):
  54. self.Bind(wx.EVT_ERASE_BACKGROUND, self.erase)
  55. self.Bind(wx.EVT_PAINT, self.redraw)
  56. self.Bind(wx.EVT_TIMER, self.tick, id=1)
  57. self.timer = wx.Timer(self, 1)
  58. self.timer.Start(100, True)
  59. # Python doesn't receive signals while wx is waiting for an event
  60. self.Bind(wx.EVT_TIMER, self.dummy, id=2)
  61. self.ticker = wx.Timer(self, 2)
  62. self.ticker.Start(100, False)
  63. def erase(self, ev):
  64. ev.GetDC()
  65. def draw(self):
  66. app = self.app
  67. size = self.GetSize()
  68. x0 = (size.GetWidth() - app.i_width) / 2
  69. y0 = (size.GetHeight() - app.i_height) / 2
  70. dc = wx.PaintDC(self)
  71. data = app.imgbuf.reshape((app.i_height, app.i_width, 4))
  72. data = data[::, ::, 2::-1]
  73. fn = getattr(data, "tobytes", getattr(data, "tostring"))
  74. image = wx.Image(app.i_width, app.i_height, fn())
  75. dc.DrawBitmap(BitmapFromImage(image), x0, y0, False)
  76. def redraw(self, ev):
  77. if self.app.fraction > 0.001:
  78. t0 = time.time()
  79. self.draw()
  80. t1 = time.time()
  81. last = t1 - t0
  82. delay = last / self.app.fraction
  83. self.timer.Start(int(delay * 1000), True)
  84. else:
  85. self.draw()
  86. def tick(self, ev):
  87. self.Refresh()
  88. def dummy(self, ev):
  89. pass
  90. class Application(wx.App):
  91. def __init__(self):
  92. self.image = sys.argv[1]
  93. self.fraction = int(sys.argv[2]) / 100.0
  94. self.HEADER_SIZE = 64
  95. wx.App.__init__(self)
  96. def read_bmp_header(self, header):
  97. magic, bmfh, bmih = struct.unpack("2s12s40s10x", header)
  98. if grass.decode(magic) != 'BM':
  99. raise SyntaxError("Invalid magic number")
  100. size, res1, res2, hsize = struct.unpack("<IHHI", bmfh)
  101. if hsize != self.HEADER_SIZE:
  102. raise SyntaxError("Invalid file header size")
  103. hsize, width, height, planes, bpp, compression, imsize, xppm, yppm, cused, cimp = \
  104. struct.unpack("<IiiHHIIiiII", bmih)
  105. if hsize != 40:
  106. raise SyntaxError("Invalid info header size")
  107. self.i_width = width
  108. self.i_height = -height
  109. if planes != 1:
  110. raise SyntaxError("Planar data not supported")
  111. if bpp != 32:
  112. raise SyntaxError("Only 32-BPP images supported")
  113. if compression != 0:
  114. raise SyntaxError("Compression not supported")
  115. if imsize != self.i_width * self.i_height * 4:
  116. raise SyntaxError("Invalid image data size")
  117. if size != self.HEADER_SIZE + self.i_width * self.i_height * 4:
  118. raise SyntaxError("Invalid image size")
  119. def map_file(self):
  120. f = open(self.image, 'rb')
  121. header = f.read(self.HEADER_SIZE)
  122. self.read_bmp_header(header)
  123. self.imgbuf = numpy.memmap(f, mode='r', offset=self.HEADER_SIZE)
  124. def signal_handler(self, sig, frame):
  125. wx.CallAfter(self.mainwin.Refresh)
  126. def set_handler(self):
  127. if 'SIGUSR1' in dir(signal):
  128. signal.signal(signal.SIGUSR1, self.signal_handler)
  129. def OnInit(self):
  130. self.map_file()
  131. size = wx.Size(self.i_width, self.i_height)
  132. self.mainwin = Frame(self, size)
  133. self.mainwin.Show()
  134. self.SetTopWindow(self.mainwin)
  135. self.set_handler()
  136. return True
  137. if __name__ == "__main__":
  138. app = Application()
  139. app.MainLoop()