bitmap.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # Copyright 2017 Google Inc.
  2. #
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """
  6. Visualize bitmaps in gdb.
  7. (gdb) source <path to this file>
  8. (gdb) sk_bitmap <symbol>
  9. This should pop up a window with the bitmap displayed.
  10. Right clicking should bring up a menu, allowing the
  11. bitmap to be saved to a file.
  12. """
  13. import gdb
  14. from enum import Enum
  15. try:
  16. from PIL import Image
  17. except ImportError:
  18. import Image
  19. class ColorType(Enum):
  20. unknown = 0
  21. alpha_8 = 1
  22. rgb_565 = 2
  23. argb_4444 = 3
  24. rgba_8888 = 4
  25. rgbx_8888 = 5
  26. bgra_8888 = 6
  27. rgba_1010102 = 7
  28. rgb_101010x = 8
  29. gray_8 = 9
  30. rgba_F16 = 10
  31. class AlphaType(Enum):
  32. unknown = 0
  33. opaque = 1
  34. premul = 2
  35. unpremul = 3
  36. class sk_bitmap(gdb.Command):
  37. """Displays the content of an SkBitmap image."""
  38. def __init__(self):
  39. super(sk_bitmap, self).__init__('sk_bitmap',
  40. gdb.COMMAND_SUPPORT,
  41. gdb.COMPLETE_FILENAME)
  42. def invoke(self, arg, from_tty):
  43. frame = gdb.selected_frame()
  44. val = frame.read_var(arg)
  45. if str(val.type.strip_typedefs()) == 'SkBitmap':
  46. pixmap = val['fPixmap']
  47. pixels = pixmap['fPixels']
  48. row_bytes = pixmap['fRowBytes']
  49. info = pixmap['fInfo']
  50. dimensions = info['fDimensions']
  51. width = dimensions['fWidth']
  52. height = dimensions['fHeight']
  53. color_type = info['fColorType']
  54. alpha_type = info['fAlphaType']
  55. process = gdb.selected_inferior()
  56. memory_data = process.read_memory(pixels, row_bytes * height)
  57. size = (width, height)
  58. image = None
  59. # See Unpack.c for the values understood after the "raw" parameter.
  60. if color_type == ColorType.bgra_8888.value:
  61. if alpha_type == AlphaType.unpremul.value:
  62. image = Image.frombytes("RGBA", size, memory_data,
  63. "raw", "BGRA", row_bytes, 1)
  64. elif alpha_type == AlphaType.premul.value:
  65. # RGBA instead of RGBa, because Image.show() doesn't work with RGBa.
  66. image = Image.frombytes("RGBA", size, memory_data,
  67. "raw", "BGRa", row_bytes, 1)
  68. if image:
  69. # Fails on premultiplied alpha, it cannot convert to RGB.
  70. image.show()
  71. else:
  72. print ("Need to add support for %s %s." % (
  73. str(ColorType(int(color_type))),
  74. str(AlphaType(int(alpha_type)))
  75. ))
  76. sk_bitmap()