test_texture_peek.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. from panda3d.core import Texture, LColor
  2. from array import array
  3. def peeker_from_pixel(component_type, format, data):
  4. """ Creates a 1-pixel texture with the given settings and pixel data,
  5. then returns a TexturePeeker as result of calling texture.peek(). """
  6. tex = Texture("")
  7. tex.setup_1d_texture(1, component_type, format)
  8. tex.set_ram_image(data)
  9. peeker = tex.peek()
  10. assert peeker.has_pixel(0, 0)
  11. return peeker
  12. def test_texture_peek_ubyte():
  13. maxval = 255
  14. data = array('B', (2, 1, 0, maxval))
  15. peeker = peeker_from_pixel(Texture.T_unsigned_byte, Texture.F_rgba, data)
  16. col = LColor()
  17. peeker.fetch_pixel(col, 0, 0)
  18. col *= maxval
  19. assert col == (0, 1, 2, maxval)
  20. def test_texture_peek_ushort():
  21. maxval = 65535
  22. data = array('H', (2, 1, 0, maxval))
  23. peeker = peeker_from_pixel(Texture.T_unsigned_short, Texture.F_rgba, data)
  24. col = LColor()
  25. peeker.fetch_pixel(col, 0, 0)
  26. col *= maxval
  27. assert col == (0, 1, 2, maxval)
  28. def test_texture_peek_uint():
  29. maxval = 4294967295
  30. data = array('I', (2, 1, 0, maxval))
  31. peeker = peeker_from_pixel(Texture.T_unsigned_int, Texture.F_rgba, data)
  32. col = LColor()
  33. peeker.fetch_pixel(col, 0, 0)
  34. col *= maxval
  35. assert col == (0, 1, 2, maxval)
  36. def test_texture_peek_float():
  37. data = array('f', (1.0, 0.0, -2.0, 10000.0))
  38. peeker = peeker_from_pixel(Texture.T_float, Texture.F_rgba, data)
  39. col = LColor()
  40. peeker.fetch_pixel(col, 0, 0)
  41. assert col == (-2.0, 0.0, 1.0, 10000.0)
  42. def test_texture_peek_half():
  43. # Python's array class doesn't support half floats, so we hardcode the
  44. # binary representation of these numbers:
  45. data = array('H', (
  46. 0b0011110000000000, # 1.0
  47. 0b1100000000000000, # -2.0
  48. 0b0111101111111111, # 65504.0
  49. 0b0011010101010101, # 0.333251953125
  50. ))
  51. peeker = peeker_from_pixel(Texture.T_half_float, Texture.F_rgba, data)
  52. col = LColor()
  53. peeker.fetch_pixel(col, 0, 0)
  54. assert col == (65504.0, -2.0, 1.0, 0.333251953125)
  55. def test_texture_peek_srgb():
  56. # 188 = roughly middle gray
  57. data = array('B', [188, 188, 188])
  58. peeker = peeker_from_pixel(Texture.T_unsigned_byte, Texture.F_srgb, data)
  59. col = LColor()
  60. peeker.fetch_pixel(col, 0, 0)
  61. # We allow some imprecision.
  62. assert col.almost_equal((0.5, 0.5, 0.5, 1.0), 1 / 255.0)
  63. def test_texture_peek_srgba():
  64. # 188 = middle gray
  65. data = array('B', [188, 188, 188, 188])
  66. peeker = peeker_from_pixel(Texture.T_unsigned_byte, Texture.F_srgb_alpha, data)
  67. col = LColor()
  68. peeker.fetch_pixel(col, 0, 0)
  69. # We allow some imprecision.
  70. assert col.almost_equal((0.5, 0.5, 0.5, 188 / 255.0), 1 / 255.0)