image.lua 4.1 KB

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