Advertisement
Guest User

Untitled

a guest
May 24th, 2015
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. """Bitmapper
  2.  
  3. Usage:
  4. bitmapper.py <input> <output> [<width>]
  5.  
  6. Options:
  7. -h --help Show this screen.
  8. <input> The input binary file.
  9. <output> The output bitmap.
  10. <width> The width of the bitmap in pixels. Optional.
  11. """
  12.  
  13. import docopt
  14. import struct
  15.  
  16. DEFAULT_WIDTH = 1000
  17.  
  18. HEADER = ("42 4D 36 E0 2E 00 00 00 00 00 36 00 00 00 28 00"
  19. "00 00 E8 03 00 00 00 04 00 00 01 00 20 00 00 00"
  20. "00 00 00 E0 2E 00 00 00 00 00 00 00 00 00 00 00"
  21. "00 00 00 00 00 00").decode("hex-bytes")
  22.  
  23.  
  24. def make_bitmap(data, width=1000):
  25. size = len(data)
  26. height = len(data) / 4 / width
  27. header = (HEADER[:2] +
  28. struct.pack("<I", size) +
  29. HEADER[6:0x12] +
  30. struct.pack("<I", width) +
  31. struct.pack("<I", height) +
  32. HEADER[0x1A:0x22] +
  33. struct.pack("<I", size - 54) +
  34. HEADER[0x26:])
  35.  
  36. return header + data
  37.  
  38.  
  39. if __name__ == '__main__':
  40. arguments = docopt.docopt(__doc__)
  41.  
  42. input_path = arguments["<input>"]
  43. output_path = arguments["<output>"]
  44.  
  45. try:
  46. width = int(arguments["<width>"])
  47. except (ValueError, TypeError):
  48. width = DEFAULT_WIDTH
  49.  
  50. with open(input_path, "rb") as f:
  51. data = f.read()
  52.  
  53. bitmap = make_bitmap(data, width)
  54.  
  55. with open(output_path, "wb") as f:
  56. f.write(bitmap)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement