image.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. -- love.image
  2. --------------------------------------------------------------------------------
  3. --------------------------------------------------------------------------------
  4. ------------------------------------OBJECTS-------------------------------------
  5. --------------------------------------------------------------------------------
  6. --------------------------------------------------------------------------------
  7. -- CompressedImageData (love.image.newCompressedImageData)
  8. love.test.image.CompressedImageData = function(test)
  9. -- create obj
  10. local idata = love.image.newCompressedData('resources/love.dxt1')
  11. test:assertObject(idata)
  12. -- check string + size
  13. test:assertNotEquals(nil, idata:getString(), 'check data string')
  14. test:assertEquals(2744, idata:getSize(), 'check data size')
  15. -- check img dimensions
  16. local iw, ih = idata:getDimensions()
  17. test:assertEquals(64, iw, 'check image dimension w')
  18. test:assertEquals(64, ih, 'check image dimension h')
  19. test:assertEquals(64, idata:getWidth(), 'check image direct w')
  20. test:assertEquals(64, idata:getHeight(), 'check image direct h')
  21. -- check format
  22. test:assertEquals('DXT1', idata:getFormat(), 'check image format')
  23. -- check mipmap count
  24. test:assertEquals(7, idata:getMipmapCount(), 'check mipmap count')
  25. end
  26. -- ImageData (love.image.newImageData)
  27. love.test.image.ImageData = function(test)
  28. -- create obj
  29. local idata = love.image.newImageData('resources/love.png')
  30. test:assertObject(idata)
  31. -- check string + size
  32. test:assertNotEquals(nil, idata:getString(), 'check data string')
  33. test:assertEquals(16384, idata:getSize(), 'check data size')
  34. -- check img dimensions
  35. local iw, ih = idata:getDimensions()
  36. test:assertEquals(64, iw, 'check image dimension w')
  37. test:assertEquals(64, ih, 'check image dimension h')
  38. test:assertEquals(64, idata:getWidth(), 'check image direct w')
  39. test:assertEquals(64, idata:getHeight(), 'check image direct h')
  40. -- check format
  41. test:assertEquals('rgba8', idata:getFormat(), 'check image format')
  42. -- manipulate image data so white heart is black
  43. local mapdata = function(x, y, r, g, b, a)
  44. if r == 1 and g == 1 and b == 1 then
  45. r = 0; g = 0; b = 0
  46. end
  47. return r, g, b, a
  48. end
  49. idata:mapPixel(mapdata, 0, 0, 64, 64)
  50. local r1, g1, b1 = idata:getPixel(25, 25)
  51. test:assertEquals(0, r1+g1+b1, 'check mapped black')
  52. -- map some other data into the idata
  53. local idata2 = love.image.newImageData('resources/loveinv.png')
  54. idata:paste(idata2, 0, 0, 0, 0)
  55. r1, g1, b1 = idata:getPixel(25, 25)
  56. test:assertEquals(3, r1+g1+b1, 'check back to white')
  57. -- set pixels directly
  58. idata:setPixel(25, 25, 1, 0, 0, 1)
  59. local r2, g2, b2 = idata:getPixel(25, 25)
  60. test:assertEquals(1, r2+g2+b2, 'check set to red')
  61. -- check encoding to an image
  62. idata:encode('png', 'test-encode.png')
  63. local read = love.filesystem.openFile('test-encode.png', 'r')
  64. test:assertNotNil(read)
  65. love.filesystem.remove('test-encode.png')
  66. end
  67. --------------------------------------------------------------------------------
  68. --------------------------------------------------------------------------------
  69. ------------------------------------METHODS-------------------------------------
  70. --------------------------------------------------------------------------------
  71. --------------------------------------------------------------------------------
  72. -- love.image.isCompressed
  73. -- @NOTE really we need to test each of the files listed here:
  74. -- https://love2d.org/wiki/CompressedImageFormat
  75. -- also need to be platform dependent (e.g. dxt not suppored on phones)
  76. love.test.image.isCompressed = function(test)
  77. test:assertTrue(love.image.isCompressed('resources/love.dxt1'),
  78. 'check dxt1 valid compressed image')
  79. end
  80. -- love.image.newCompressedData
  81. -- @NOTE this is just basic nil checking, objs have their own test method
  82. love.test.image.newCompressedData = function(test)
  83. test:assertObject(love.image.newCompressedData('resources/love.dxt1'))
  84. end
  85. -- love.image.newImageData
  86. -- @NOTE this is just basic nil checking, objs have their own test method
  87. love.test.image.newImageData = function(test)
  88. test:assertObject(love.image.newImageData('resources/love.png'))
  89. test:assertObject(love.image.newImageData(16, 16, 'rgba8', nil))
  90. end