test_texture_peek.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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)
  71. def test_texture_peek_ubyte_i():
  72. maxval = 255
  73. data = array('B', (2, 1, 0, maxval))
  74. peeker = peeker_from_pixel(Texture.T_unsigned_byte, Texture.F_rgba8i, data)
  75. col = LColor()
  76. peeker.fetch_pixel(col, 0, 0)
  77. assert col == (0, 1, 2, maxval)
  78. def test_texture_peek_byte_i():
  79. minval = -128
  80. maxval = 127
  81. data = array('b', (0, -1, minval, maxval))
  82. peeker = peeker_from_pixel(Texture.T_byte, Texture.F_rgba8i, data)
  83. col = LColor()
  84. peeker.fetch_pixel(col, 0, 0)
  85. assert col == (minval, -1, 0, maxval)
  86. def test_texture_peek_ushort_i():
  87. maxval = 65535
  88. data = array('H', (2, 1, 0, maxval))
  89. peeker = peeker_from_pixel(Texture.T_unsigned_short, Texture.F_rgba16i, data)
  90. col = LColor()
  91. peeker.fetch_pixel(col, 0, 0)
  92. assert col == (0, 1, 2, maxval)
  93. def test_texture_peek_short_i():
  94. minval = -32768
  95. maxval = 32767
  96. data = array('h', (0, -1, minval, maxval))
  97. peeker = peeker_from_pixel(Texture.T_short, Texture.F_rgba16i, data)
  98. col = LColor()
  99. peeker.fetch_pixel(col, 0, 0)
  100. assert col == (minval, -1, 0, maxval)
  101. def test_texture_peek_uint_i():
  102. # Highest integer that fits inside float
  103. maxval = 2147483648
  104. data = array('I', (2, 1, 0, maxval))
  105. peeker = peeker_from_pixel(Texture.T_unsigned_int, Texture.F_rgba32i, data)
  106. col = LColor()
  107. peeker.fetch_pixel(col, 0, 0)
  108. assert col == (0, 1, 2, maxval)
  109. def test_texture_peek_int_i():
  110. minval = -2147483648
  111. maxval = 2147483647
  112. data = array('i', (0, -1, minval, maxval))
  113. peeker = peeker_from_pixel(Texture.T_int, Texture.F_rgba32i, data)
  114. col = LColor()
  115. peeker.fetch_pixel(col, 0, 0)
  116. assert col == (minval, -1, 0, maxval)
  117. def test_texture_peek_cube():
  118. maxval = 255
  119. data_list = []
  120. for z in range(6):
  121. for y in range(3):
  122. for x in range(3):
  123. data_list += [z, y, x, maxval]
  124. data = array('B', data_list)
  125. tex = Texture("")
  126. tex.setup_cube_map(3, Texture.T_unsigned_byte, Texture.F_rgba8i)
  127. tex.set_ram_image(data)
  128. peeker = tex.peek()
  129. assert peeker.has_pixel(0, 0)
  130. assert peeker.has_pixel(0, 0, 0)
  131. # If no z is specified, face 0 is used by default
  132. col = LColor()
  133. peeker.fetch_pixel(col, 1, 2)
  134. assert col == (1, 2, 0, maxval)
  135. # Now try each face
  136. for faceidx in range(6):
  137. col = LColor()
  138. peeker.fetch_pixel(col, 0, 0, faceidx)
  139. assert col == (0, 0, faceidx, maxval)
  140. # Try some vector lookups.
  141. def lookup(*vec):
  142. col = LColor()
  143. peeker.lookup(col, *vec)
  144. return col
  145. assert lookup(1, 0, 0) == (1, 1, 0, maxval)
  146. assert lookup(-1, 0, 0) == (1, 1, 1, maxval)
  147. assert lookup(0, 1, 0) == (1, 1, 2, maxval)
  148. assert lookup(0, -1, 0) == (1, 1, 3, maxval)
  149. assert lookup(0, 0, 1) == (1, 1, 4, maxval)
  150. assert lookup(0, 0, -1) == (1, 1, 5, maxval)
  151. # Magnitude shouldn't matter
  152. assert lookup(0, 2, 0) == (1, 1, 2, maxval)
  153. assert lookup(0, 0, -0.5) == (1, 1, 5, maxval)
  154. # Sample in corner (slight bias to disambiguate which face is selected)
  155. assert lookup(1.00001, 1, 1) == (0, 0, 0, maxval)
  156. assert lookup(1.00001, 1, 0) == (1, 0, 0, maxval)
  157. assert lookup(1, 1.00001, 0) == (2, 1, 2, maxval)