test_texture.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from panda3d.core import Texture, PNMImage
  2. from array import array
  3. def image_from_stored_pixel(component_type, format, data):
  4. """ Creates a 1-pixel texture with the given settings and pixel data,
  5. then returns a PNMImage as result of calling texture.store(). """
  6. tex = Texture("")
  7. tex.setup_1d_texture(1, component_type, format)
  8. tex.set_ram_image(data)
  9. img = PNMImage()
  10. assert tex.store(img)
  11. return img
  12. def test_texture_store_unsigned_byte():
  13. data = array('B', (2, 1, 0, 0xff))
  14. img = image_from_stored_pixel(Texture.T_unsigned_byte, Texture.F_rgba, data)
  15. assert img.maxval == 0xff
  16. pix = img.get_pixel(0, 0)
  17. assert tuple(pix) == (0, 1, 2, 0xff)
  18. def test_texture_store_unsigned_short():
  19. data = array('H', (2, 1, 0, 0xffff))
  20. img = image_from_stored_pixel(Texture.T_unsigned_short, Texture.F_rgba, data)
  21. assert img.maxval == 0xffff
  22. pix = img.get_pixel(0, 0)
  23. assert tuple(pix) == (0, 1, 2, 0xffff)
  24. def test_texture_store_unsigned_int():
  25. data = array('I', (0x30000, 2, 0, 0xffffffff))
  26. img = image_from_stored_pixel(Texture.T_unsigned_int, Texture.F_rgba, data)
  27. assert img.maxval == 0xffff
  28. pix = img.get_pixel(0, 0)
  29. assert tuple(pix) == (0, 0, 3, 0xffff)
  30. def test_texture_store_float():
  31. data = array('f', (0.5, 0.0, -2.0, 10000.0))
  32. img = image_from_stored_pixel(Texture.T_float, Texture.F_rgba, data)
  33. assert img.maxval == 0xffff
  34. col = img.get_xel_a(0, 0)
  35. assert col.almost_equal((0.0, 0.0, 0.5, 1.0), 1 / 255.0)
  36. def test_texture_store_half():
  37. # Python's array class doesn't support half floats, so we hardcode the
  38. # binary representation of these numbers:
  39. data = array('H', (
  40. # -[exp][mantissa]
  41. 0b0011110000000000, # 1.0
  42. 0b1100000000000000, # -2.0
  43. 0b0111101111111111, # 65504.0
  44. 0b0011010101010101, # 0.333251953125
  45. ))
  46. img = image_from_stored_pixel(Texture.T_half_float, Texture.F_rgba, data)
  47. assert img.maxval == 0xffff
  48. col = img.get_xel_a(0, 0)
  49. assert col.almost_equal((1.0, 0.0, 1.0, 0.333251953125), 1 / 255.0)
  50. def test_texture_store_srgb():
  51. # 188 = roughly middle gray
  52. data = array('B', [188, 188, 188])
  53. img = image_from_stored_pixel(Texture.T_unsigned_byte, Texture.F_srgb, data)
  54. # We allow some imprecision.
  55. assert img.maxval == 0xff
  56. col = img.get_xel(0, 0)
  57. assert col.almost_equal((0.5, 0.5, 0.5), 1 / 255.0)
  58. def test_texture_store_srgb_alpha():
  59. # 188 = middle gray
  60. data = array('B', [188, 188, 188, 188])
  61. img = image_from_stored_pixel(Texture.T_unsigned_byte, Texture.F_srgb_alpha, data)
  62. # We allow some imprecision.
  63. assert img.maxval == 0xff
  64. col = img.get_xel_a(0, 0)
  65. assert col.almost_equal((0.5, 0.5, 0.5, 188 / 255.0), 1 / 255.0)