2
0

test_pnmimage.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. from panda3d.core import PNMImage, PNMImageHeader
  2. from random import randint
  3. def test_pixelspec_ctor():
  4. assert tuple(PNMImage.PixelSpec(1)) == (1, 1, 1, 0)
  5. assert tuple(PNMImage.PixelSpec(1, 2)) == (1, 1, 1, 2)
  6. assert tuple(PNMImage.PixelSpec(1, 2, 3)) == (1, 2, 3, 0)
  7. assert tuple(PNMImage.PixelSpec(1, 2, 3, 4)) == (1, 2, 3, 4)
  8. assert tuple(PNMImage.PixelSpec((1, 2, 3))) == (1, 2, 3, 0)
  9. assert tuple(PNMImage.PixelSpec((1, 2, 3), 4)) == (1, 2, 3, 4)
  10. # Copy constructor
  11. spec = PNMImage.PixelSpec(1, 2, 3, 4)
  12. assert tuple(PNMImage.PixelSpec(spec)) == (1, 2, 3, 4)
  13. def test_pixelspec_coerce():
  14. img = PNMImage(1, 1, 4)
  15. img.set_pixel(0, 0, (1, 2, 3, 4))
  16. assert img.get_pixel(0, 0) == (1, 2, 3, 4)
  17. def test_pnmimage_to_val():
  18. img = PNMImage(1, 1)
  19. assert img.to_val(-0.5) == 0
  20. assert img.to_val(0.0) == 0
  21. assert img.to_val(0.5) == 128
  22. assert img.to_val(1.0) == 255
  23. assert img.to_val(2.0) == 255
  24. def test_pnmimage_from_val():
  25. img = PNMImage(1, 1)
  26. assert img.from_val(0) == 0.0
  27. assert img.to_val(img.from_val(128)) == 128
  28. assert img.from_val(255) == 1.0
  29. def test_pnmimage_quantize():
  30. img = PNMImage(32, 32, 3)
  31. for x in range(32):
  32. for y in range(32):
  33. img.set_xel_val(x, y, randint(0, 100), randint(50, 100), randint(0, 1))
  34. hist = PNMImage.Histogram()
  35. img.make_histogram(hist)
  36. num_colors = hist.get_num_pixels()
  37. assert num_colors > 100
  38. img2 = PNMImage(img)
  39. img2.quantize(100)
  40. hist = PNMImage.Histogram()
  41. img2.make_histogram(hist)
  42. assert hist.get_num_pixels() <= 100
  43. # Make sure that this is reasonably close
  44. max_dist = 0
  45. for x in range(32):
  46. for y in range(32):
  47. diff = img.get_xel(x, y) - img2.get_xel(x, y)
  48. max_dist = max(max_dist, diff.length_squared())
  49. # Also make sure that they are not out of range of the original
  50. col = img2.get_xel_val(x, y)
  51. assert col.r <= 100
  52. assert col.g >= 50 and col.g <= 100
  53. assert col.b in (0, 1)
  54. assert max_dist < 0.1 ** 2
  55. def test_pnmimage_add_sub_image():
  56. dst = PNMImage(2, 2)
  57. dst.fill(0.5, 0, 0) #adding color to dst
  58. #dst_color will store rgb values at each pixel of dst
  59. dst_color = ((dst.get_xel(0, 0), dst.get_xel(0, 1)), (dst.get_xel(1, 0), dst.get_xel(1, 1)))
  60. src = PNMImage(1, 1)
  61. src.fill(0, 0.7, 0) #adding color to src
  62. #src_color will store rgb values at each pixel of src
  63. src_color = src.get_xel(0, 0)
  64. dst.add_sub_image(src, 1, 1, 0, 0, 1, 1)
  65. final_color = ((dst.get_xel(0, 0), dst.get_xel(0, 1)), (dst.get_xel(1, 0), dst.get_xel(1, 1)))
  66. assert final_color[0][0] == dst_color[0][0]
  67. assert final_color[0][1] == dst_color[0][1]
  68. assert final_color[1][0] == dst_color[1][0]
  69. assert final_color[1][1] == dst_color[1][1] + src_color
  70. def test_pnmimage_mult_sub_image():
  71. dst = PNMImage(2, 2)
  72. dst.fill(0.5, 0, 0) #adding color to dst
  73. #dst_color will store rgb values at each pixel of dst
  74. dst_color = ((dst.get_xel(0, 0), dst.get_xel(0, 1)), (dst.get_xel(1, 0), dst.get_xel(1, 1)))
  75. src = PNMImage(1, 1)
  76. src.fill(0, 0.7, 0) #adding color to src
  77. #src_color will store rgb values at each pixel of src
  78. src_color = src.get_xel(0, 0)
  79. dst.mult_sub_image(src, 1, 1, 0, 0, 1, 1)
  80. final_color = ((dst.get_xel(0, 0), dst.get_xel(0, 1)), (dst.get_xel(1, 0), dst.get_xel(1, 1)))
  81. assert final_color[0][0] == dst_color[0][0]
  82. assert final_color[0][1] == dst_color[0][1]
  83. assert final_color[1][0] == dst_color[1][0]
  84. assert final_color[1][1][0] == dst_color[1][1][0] * src_color[0] and final_color[1][1][1] == dst_color[1][1][1] * src_color[1] and final_color[1][1][2] == dst_color[1][1][2] * src_color[2]