test_texture.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. from panda3d.core import Texture, PNMImage, LColor
  2. from array import array
  3. import math
  4. def image_from_stored_pixel(component_type, format, data):
  5. """ Creates a 1-pixel texture with the given settings and pixel data,
  6. then returns a PNMImage as result of calling texture.store(). """
  7. tex = Texture("")
  8. tex.setup_1d_texture(1, component_type, format)
  9. tex.set_ram_image(data)
  10. img = PNMImage()
  11. assert tex.store(img)
  12. return img
  13. def peek_tex_with_clear_color(component_type, format, clear_color):
  14. """ Creates a 1-pixel texture with the given settings and clear color,
  15. then peeks the value at this pixel and returns it. """
  16. tex = Texture("")
  17. tex.setup_1d_texture(1, component_type, format)
  18. tex.set_clear_color(clear_color)
  19. tex.make_ram_image()
  20. col = LColor()
  21. tex.peek().fetch_pixel(col, 0, 0)
  22. return col
  23. def test_texture_store_unsigned_byte():
  24. data = array('B', (2, 1, 0, 0xff))
  25. img = image_from_stored_pixel(Texture.T_unsigned_byte, Texture.F_rgba, data)
  26. assert img.maxval == 0xff
  27. pix = img.get_pixel(0, 0)
  28. assert tuple(pix) == (0, 1, 2, 0xff)
  29. def test_texture_store_unsigned_short():
  30. data = array('H', (2, 1, 0, 0xffff))
  31. img = image_from_stored_pixel(Texture.T_unsigned_short, Texture.F_rgba, data)
  32. assert img.maxval == 0xffff
  33. pix = img.get_pixel(0, 0)
  34. assert tuple(pix) == (0, 1, 2, 0xffff)
  35. def test_texture_store_unsigned_int():
  36. data = array('I', (0x30000, 2, 0, 0xffffffff))
  37. img = image_from_stored_pixel(Texture.T_unsigned_int, Texture.F_rgba, data)
  38. assert img.maxval == 0xffff
  39. pix = img.get_pixel(0, 0)
  40. assert tuple(pix) == (0, 0, 3, 0xffff)
  41. def test_texture_store_float():
  42. data = array('f', (0.5, 0.0, -2.0, 10000.0))
  43. img = image_from_stored_pixel(Texture.T_float, Texture.F_rgba, data)
  44. assert img.maxval == 0xffff
  45. col = img.get_xel_a(0, 0)
  46. assert col.almost_equal((0.0, 0.0, 0.5, 1.0), 1 / 255.0)
  47. def test_texture_store_half():
  48. # Python's array class doesn't support half floats, so we hardcode the
  49. # binary representation of these numbers:
  50. data = array('H', (
  51. # -[exp][mantissa]
  52. 0b0011110000000000, # 1.0
  53. 0b1100000000000000, # -2.0
  54. 0b0111101111111111, # 65504.0
  55. 0b0011010101010101, # 0.333251953125
  56. ))
  57. img = image_from_stored_pixel(Texture.T_half_float, Texture.F_rgba, data)
  58. assert img.maxval == 0xffff
  59. col = img.get_xel_a(0, 0)
  60. assert col.almost_equal((1.0, 0.0, 1.0, 0.333251953125), 1 / 255.0)
  61. def test_texture_store_srgb():
  62. # 188 = roughly middle gray
  63. data = array('B', [188, 188, 188])
  64. img = image_from_stored_pixel(Texture.T_unsigned_byte, Texture.F_srgb, data)
  65. # We allow some imprecision.
  66. assert img.maxval == 0xff
  67. col = img.get_xel(0, 0)
  68. assert col.almost_equal((0.5, 0.5, 0.5), 1 / 255.0)
  69. def test_texture_store_srgb_alpha():
  70. # 188 = middle gray
  71. data = array('B', [188, 188, 188, 188])
  72. img = image_from_stored_pixel(Texture.T_unsigned_byte, Texture.F_srgb_alpha, data)
  73. # We allow some imprecision.
  74. assert img.maxval == 0xff
  75. col = img.get_xel_a(0, 0)
  76. assert col.almost_equal((0.5, 0.5, 0.5, 188 / 255.0), 1 / 255.0)
  77. def test_texture_clear_unsigned_byte():
  78. col = peek_tex_with_clear_color(Texture.T_unsigned_byte, Texture.F_rgba, (0, 1 / 255.0, 254 / 255.0, 1.0))
  79. assert col == LColor(0, 1 / 255.0, 254 / 255.0, 1.0)
  80. def test_texture_clear_float():
  81. col = peek_tex_with_clear_color(Texture.T_float, Texture.F_rgba, (0, 0.25, -0.5, 2))
  82. assert col == LColor(0, 0.25, -0.5, 2)
  83. def test_texture_clear_half():
  84. col = peek_tex_with_clear_color(Texture.T_half_float, Texture.F_rgba, (0, 0.25, -0.5, 2))
  85. assert col == LColor(0, 0.25, -0.5, 2)
  86. # Test edge cases
  87. inf = float('inf')
  88. nan = float('nan')
  89. col = peek_tex_with_clear_color(Texture.T_half_float, Texture.F_rgba, (65504, 65536, inf, nan))
  90. assert col.x == 65504
  91. assert col.y == inf
  92. assert col.z == inf
  93. assert math.isnan(col.w)
  94. # Negative edge case
  95. col = peek_tex_with_clear_color(Texture.T_half_float, Texture.F_rgba, (-65504, -65536, -inf, -nan))
  96. assert col.x == -65504
  97. assert col.y == -inf
  98. assert col.z == -inf
  99. assert math.isnan(col.w)
  100. def test_texture_deepcopy():
  101. from copy import deepcopy
  102. empty_tex = Texture("empty-texture")
  103. empty_tex.setup_2d_texture(16, 16, Texture.T_unsigned_byte, Texture.F_rgba)
  104. assert not empty_tex.has_ram_image()
  105. empty_tex2 = deepcopy(empty_tex)
  106. assert empty_tex2.name == empty_tex.name
  107. assert not empty_tex2.has_ram_image()
  108. tex = Texture("texture")
  109. tex.setup_2d_texture(16, 16, Texture.T_unsigned_byte, Texture.F_rgba)
  110. img = tex.make_ram_image()
  111. assert tex.has_ram_image()
  112. assert img.get_ref_count() == 2
  113. tex2 = deepcopy(tex)
  114. assert tex2.name == tex.name
  115. assert tex2.has_ram_image()
  116. img2 = tex2.get_ram_image()
  117. assert img2.get_ref_count() == 2