test_texture_pool.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. from panda3d import core
  2. import pytest
  3. import tempfile
  4. @pytest.fixture(scope='function')
  5. def pool():
  6. "This fixture ensures the pool is properly emptied"
  7. pool = core.TexturePool
  8. pool.release_all_textures()
  9. yield pool
  10. pool.release_all_textures()
  11. def write_image(filename, channels):
  12. img = core.PNMImage(1, 1, channels)
  13. img.set_xel_a(0, 0, (0.0, 0.25, 0.5, 0.75))
  14. assert img.write(filename)
  15. @pytest.fixture(scope='session')
  16. def image_rgb_path():
  17. "Generates an RGB image."
  18. file = tempfile.NamedTemporaryFile(suffix='-rgb.png')
  19. path = core.Filename.from_os_specific(file.name)
  20. path.make_true_case()
  21. write_image(path, 3)
  22. yield path
  23. file.close()
  24. @pytest.fixture(scope='session')
  25. def image_rgba_path():
  26. "Generates an RGBA image."
  27. file = tempfile.NamedTemporaryFile(suffix='-rgba.png')
  28. path = core.Filename.from_os_specific(file.name)
  29. path.make_true_case()
  30. write_image(path, 4)
  31. yield path
  32. file.close()
  33. @pytest.fixture(scope='session')
  34. def image_gray_path():
  35. "Generates a grayscale image."
  36. file = tempfile.NamedTemporaryFile(suffix='-gray.png')
  37. path = core.Filename.from_os_specific(file.name)
  38. path.make_true_case()
  39. write_image(path, 1)
  40. yield path
  41. file.close()
  42. def test_load_texture_rgba(pool, image_rgba_path):
  43. tex = pool.load_texture(image_rgba_path)
  44. assert pool.has_texture(image_rgba_path)
  45. assert tex.num_components == 4
  46. def test_load_texture_rgba4(pool, image_rgba_path):
  47. tex = pool.load_texture(image_rgba_path, 4)
  48. assert pool.has_texture(image_rgba_path)
  49. assert tex.num_components == 4
  50. def test_load_texture_rgba3(pool, image_rgba_path):
  51. tex = pool.load_texture(image_rgba_path, 3)
  52. assert pool.has_texture(image_rgba_path)
  53. assert tex.num_components == 3
  54. def test_load_texture_rgba2(pool, image_rgba_path):
  55. tex = pool.load_texture(image_rgba_path, 2)
  56. assert pool.has_texture(image_rgba_path)
  57. assert tex.num_components == 2
  58. def test_load_texture_rgba1(pool, image_rgba_path):
  59. tex = pool.load_texture(image_rgba_path, 1)
  60. assert pool.has_texture(image_rgba_path)
  61. assert tex.num_components == 1
  62. def test_load_texture_rgb(pool, image_rgb_path):
  63. tex = pool.load_texture(image_rgb_path)
  64. assert pool.has_texture(image_rgb_path)
  65. assert tex.num_components == 3
  66. def test_load_texture_rgb4(pool, image_rgb_path):
  67. # Will not increase this
  68. tex = pool.load_texture(image_rgb_path, 4)
  69. assert pool.has_texture(image_rgb_path)
  70. assert tex.num_components == 3
  71. def test_load_texture_rgb3(pool, image_rgb_path):
  72. tex = pool.load_texture(image_rgb_path, 3)
  73. assert pool.has_texture(image_rgb_path)
  74. assert tex.num_components == 3
  75. def test_load_texture_rgb2(pool, image_rgb_path):
  76. # Cannot reduce this, since it would add an alpha channel
  77. tex = pool.load_texture(image_rgb_path, 2)
  78. assert pool.has_texture(image_rgb_path)
  79. assert tex.num_components == 3
  80. def test_load_texture_rgb1(pool, image_rgb_path):
  81. tex = pool.load_texture(image_rgb_path, 1)
  82. assert pool.has_texture(image_rgb_path)
  83. assert tex.num_components == 1
  84. def test_load_texture_rgba_alpha(pool, image_rgba_path, image_gray_path):
  85. tex = pool.load_texture(image_rgba_path, image_gray_path)
  86. assert tex.num_components == 4
  87. def test_load_texture_rgba4_alpha(pool, image_rgba_path, image_gray_path):
  88. tex = pool.load_texture(image_rgba_path, image_gray_path, 4)
  89. assert tex.num_components == 4
  90. def test_load_texture_rgba3_alpha(pool, image_rgba_path, image_gray_path):
  91. tex = pool.load_texture(image_rgba_path, image_gray_path, 3)
  92. assert tex.num_components == 4
  93. def test_load_texture_rgba2_alpha(pool, image_rgba_path, image_gray_path):
  94. #FIXME: why is this not consistent with test_load_texture_rgb2_alpha?
  95. tex = pool.load_texture(image_rgba_path, image_gray_path, 2)
  96. assert tex.num_components == 2
  97. def test_load_texture_rgba1_alpha(pool, image_rgba_path, image_gray_path):
  98. tex = pool.load_texture(image_rgba_path, image_gray_path, 1)
  99. assert tex.num_components == 2
  100. def test_load_texture_rgb_alpha(pool, image_rgb_path, image_gray_path):
  101. tex = pool.load_texture(image_rgb_path, image_gray_path)
  102. assert tex.num_components == 4
  103. def test_load_texture_rgb4_alpha(pool, image_rgb_path, image_gray_path):
  104. tex = pool.load_texture(image_rgb_path, image_gray_path, 4)
  105. assert tex.num_components == 4
  106. def test_load_texture_rgb3_alpha(pool, image_rgb_path, image_gray_path):
  107. tex = pool.load_texture(image_rgb_path, image_gray_path, 3)
  108. assert tex.num_components == 4
  109. def test_load_texture_rgb2_alpha(pool, image_rgb_path, image_gray_path):
  110. #FIXME: why is this not consistent with test_load_texture_rgba2_alpha?
  111. tex = pool.load_texture(image_rgb_path, image_gray_path, 2)
  112. assert tex.num_components == 4
  113. def test_load_texture_rgb1_alpha(pool, image_rgb_path, image_gray_path):
  114. tex = pool.load_texture(image_rgb_path, image_gray_path, 1)
  115. assert tex.num_components == 2
  116. def test_reload_texture_fewer_channels(pool, image_rgba_path):
  117. tex = pool.load_texture(image_rgba_path)
  118. assert pool.has_texture(image_rgba_path)
  119. assert tex.num_components == 4
  120. tex = pool.load_texture(image_rgba_path, 3)
  121. assert tex.num_components == 3
  122. def test_reload_texture_more_channels(pool, image_rgba_path):
  123. tex = pool.load_texture(image_rgba_path, 3)
  124. assert pool.has_texture(image_rgba_path)
  125. assert tex.num_components == 3
  126. tex = pool.load_texture(image_rgba_path)
  127. assert tex.num_components == 4
  128. def test_reload_texture_with_alpha(pool, image_rgb_path, image_gray_path):
  129. tex = pool.load_texture(image_rgb_path)
  130. assert pool.has_texture(image_rgb_path)
  131. assert tex.num_components == 3
  132. tex = pool.load_texture(image_rgb_path, image_gray_path)
  133. assert tex.num_components == 4
  134. def test_reload_texture_without_alpha(pool, image_rgb_path, image_gray_path):
  135. tex = pool.load_texture(image_rgb_path, image_gray_path)
  136. assert tex.num_components == 4
  137. tex = pool.load_texture(image_rgb_path)
  138. assert tex.num_components == 3