Advertisement
Guest User

Untitled

a guest
Feb 16th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. test('blackenLow test', function(){
  2. let url = 'https://people.cs.umass.edu/~joydeepb/robot.jpg';
  3. let img = lib220.loadImageFromURL(url);
  4. let blacken_img = blackenLow(img)
  5. for(let x=0; x<img.width; ++x){
  6. for(let y=0; y<img.height; ++y){
  7. let pixel = img.getPixel(x, y)
  8. let blacken_pixel = blacken_img.getPixel(x, y)
  9. for(let channel=0; channel<3; ++channel){
  10. if(pixel[channel] < 0.298){
  11. assert(blacken_pixel[channel] === 0)
  12. }
  13. if(pixel[channel] > 0.302){
  14. assert(blacken_pixel[channel] === pixel[channel])
  15. }
  16. }
  17. }
  18. }
  19. });
  20.  
  21. test('saturate test', function(){
  22. let url = 'https://people.cs.umass.edu/~joydeepb/robot.jpg';
  23. let img = lib220.loadImageFromURL(url);
  24. let saturated_img = saturateHigh(img)
  25. for(let x=0; x<img.width; ++x){
  26. for(let y=0; y<img.height; ++y){
  27. let pixel = img.getPixel(x, y)
  28. let saturated_pixel = saturated_img.getPixel(x, y)
  29. for(let channel=0; channel<3; ++channel){
  30. if(pixel[channel] > 0.702){
  31. assert(saturated_pixel[channel] === 1)
  32. }
  33. if(pixel[channel] < 0.698){
  34. assert(saturated_pixel[channel] === pixel[channel])
  35. }
  36. }
  37. }
  38. }
  39. });
  40.  
  41. test('gray test', function(){
  42. let url = 'https://people.cs.umass.edu/~joydeepb/robot.jpg';
  43. let img = lib220.loadImageFromURL(url);
  44. let gray_img = toGrayscale(img)
  45.  
  46. for(let x=0; x<img.width; ++x){
  47. for(let y=0; y<img.height; ++y){
  48. let pixel = img.getPixel(x, y)
  49. let gray_pixel = gray_img.getPixel(x,y)
  50.  
  51. for(let channel=0; channel<3; ++channel){
  52. if(pixel[channel] > 0.702 || pixel[channel] < 0.298){
  53. assert(pixelEq(pixel, gray_pixel))
  54. }
  55. }
  56. }
  57. }
  58. });
  59.  
  60. test('imageMapXY function definition is correct', function() {
  61. let identityFunction = function(image, x, y) {
  62. return image.getPixel(x, y);
  63. };
  64. let inputImage = lib220.createImage(10, 10, [0, 0, 0]);
  65. let outputImage = imageMapXY(inputImage, identityFunction);
  66. // Output should be an image, so getPixel must work without errors.
  67. let p = outputImage.getPixel(0, 0);
  68. assert(p[0] === 0);
  69. assert(p[1] === 0);
  70. assert(p[2] === 0);
  71. assert(inputImage !== outputImage);
  72. });
  73.  
  74. function pixelEq (p1, p2) {
  75. const epsilon = 0.002;
  76. for (let i = 0; i < 3; ++i) {
  77. if (Math.abs(p1[i] - p2[i]) > epsilon) {
  78. return false;
  79. }
  80. }
  81. return true;
  82. };
  83.  
  84. test('identity function with imageMapXY', function() {
  85. let identityFunction = function(image, x, y ) {
  86. return image.getPixel(x, y);
  87. };
  88. let inputImage = lib220.createImage(10, 10, [0.2, 0.2, 0.2]);
  89. inputImage.setPixel(0, 0, [0.5, 0.5, 0.5]);
  90. inputImage.setPixel(5, 5, [0.1, 0.2, 0.3]);
  91. inputImage.setPixel(2, 8, [0.9, 0.7, 0.8]);
  92. let outputImage = imageMapXY(inputImage, identityFunction);
  93. assert(pixelEq(outputImage.getPixel(0, 0), [0.5, 0.5, 0.5]));
  94. assert(pixelEq(outputImage.getPixel(5, 5), [0.1, 0.2, 0.3]));
  95. assert(pixelEq(outputImage.getPixel(2, 8), [0.9, 0.7, 0.8]));
  96. assert(pixelEq(outputImage.getPixel(9, 9), [0.2, 0.2, 0.2]));
  97. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement