|
| 1 | +import { Image } from '../../Image'; |
| 2 | +import { rotatePoint } from '../../point/operations'; |
| 3 | +import { Point } from '../../utils/geometry/points'; |
| 4 | +import { assert } from '../../utils/validators/assert'; |
| 5 | + |
| 6 | +test('straight rectangle top left', () => { |
| 7 | + const image = testUtils.createGreyImage([ |
| 8 | + [1, 2, 3], |
| 9 | + [4, 5, 6], |
| 10 | + [7, 8, 9], |
| 11 | + ]); |
| 12 | + const points = testUtils.createPoints([0, 0], [2, 0], [2, 2], [0, 2]); |
| 13 | + |
| 14 | + const result = image.cropRectangle(points, { interpolationType: 'nearest' }); |
| 15 | + expect(result).toMatchImage( |
| 16 | + testUtils.createGreyImage([ |
| 17 | + [1, 2], |
| 18 | + [4, 5], |
| 19 | + ]), |
| 20 | + ); |
| 21 | +}); |
| 22 | + |
| 23 | +test('straight rectangle bottom right', () => { |
| 24 | + const image = testUtils.createGreyImage([ |
| 25 | + [1, 2, 3], |
| 26 | + [4, 5, 6], |
| 27 | + [7, 8, 9], |
| 28 | + ]); |
| 29 | + const points = testUtils.createPoints([1, 3], [3, 3], [3, 1], [1, 1]); |
| 30 | + |
| 31 | + const result = image.cropRectangle(points); |
| 32 | + expect(result).toMatchImage( |
| 33 | + testUtils.createGreyImage([ |
| 34 | + [5, 6], |
| 35 | + [8, 9], |
| 36 | + ]), |
| 37 | + ); |
| 38 | +}); |
| 39 | + |
| 40 | +test('vertical rectangle with small angle', () => { |
| 41 | + const image = testUtils.createGreyImage([ |
| 42 | + [1, 2, 3], |
| 43 | + [4, 5, 6], |
| 44 | + [7, 8, 9], |
| 45 | + ]); |
| 46 | + const points = testUtils |
| 47 | + .createPoints([1.5, 0], [2.5, 0], [2.5, 3], [1.5, 3]) |
| 48 | + .map((p) => rotatePoint(p, { row: 1.5, column: 1.5 }, 0.1)); |
| 49 | + |
| 50 | + const expected = testUtils.createGreyImage([[3], [6], [8]]); |
| 51 | + expectCropRectangleToMatch({ |
| 52 | + image, |
| 53 | + expected, |
| 54 | + points, |
| 55 | + }); |
| 56 | +}); |
| 57 | + |
| 58 | +test('horizontal rectangle with small angle', () => { |
| 59 | + const image = testUtils.createGreyImage([ |
| 60 | + [1, 2, 3], |
| 61 | + [4, 5, 6], |
| 62 | + [7, 8, 9], |
| 63 | + ]); |
| 64 | + const points = testUtils |
| 65 | + .createPoints([0, 1.5], [3, 1.5], [3, 2.5], [0, 2.5]) |
| 66 | + .map((p) => rotatePoint(p, { row: 1.5, column: 1.5 }, 0.1)); |
| 67 | + |
| 68 | + const expected = testUtils.createGreyImage([[4, 5, 9]]); |
| 69 | + expectCropRectangleToMatch({ |
| 70 | + image, |
| 71 | + expected, |
| 72 | + points, |
| 73 | + }); |
| 74 | +}); |
| 75 | + |
| 76 | +test('diagonal rectangle oriented slightly > 45 degrees clockwise', () => { |
| 77 | + const image = testUtils.createGreyImage([ |
| 78 | + [1, 2, 3], |
| 79 | + [4, 5, 6], |
| 80 | + [7, 8, 9], |
| 81 | + ]); |
| 82 | + const points = testUtils |
| 83 | + .createPoints([1.5, 0], [2.5, 0], [2.5, 3], [1.5, 3]) |
| 84 | + .map((p) => rotatePoint(p, { row: 1.5, column: 1.5 }, Math.PI / 4 + 0.01)); |
| 85 | + |
| 86 | + // Resulting image is horizontal because the rectangle is closer to the horizontal axis than the vertical one |
| 87 | + const expected = testUtils.createGreyImage([[0, 8, 6]]); |
| 88 | + expectCropRectangleToMatch({ |
| 89 | + image, |
| 90 | + expected, |
| 91 | + points, |
| 92 | + }); |
| 93 | +}); |
| 94 | + |
| 95 | +test('diagonal rectangle oriented slightly below 45 degrees', () => { |
| 96 | + const image = testUtils.createGreyImage([ |
| 97 | + [1, 2, 3], |
| 98 | + [4, 5, 6], |
| 99 | + [7, 8, 9], |
| 100 | + ]); |
| 101 | + |
| 102 | + const points = testUtils |
| 103 | + .createPoints([1.5, 0], [2.5, 0], [2.5, 3], [1.5, 3]) |
| 104 | + .map((p) => rotatePoint(p, { row: 1.5, column: 1.5 }, Math.PI / 4 - 0.01)); |
| 105 | + |
| 106 | + // Resulting image is vertical because the rectangle is closer to the vertical axis than the horizontal one |
| 107 | + const expected = testUtils.createGreyImage([[0], [6], [8]]); |
| 108 | + expectCropRectangleToMatch({ |
| 109 | + image, |
| 110 | + expected, |
| 111 | + points, |
| 112 | + }); |
| 113 | +}); |
| 114 | + |
| 115 | +function expectCropRectangleToMatch(options: { |
| 116 | + image: Image; |
| 117 | + expected: Image; |
| 118 | + points: Point[]; |
| 119 | +}) { |
| 120 | + const { image, expected, points } = options; |
| 121 | + assert(options.points.length === 4, 'Expected to receive 4 points'); |
| 122 | + const variants: Point[][] = []; |
| 123 | + const pointsReversed = points.slice().reverse(); |
| 124 | + for (let i = 0; i < 4; i++) { |
| 125 | + const variant: Point[] = []; |
| 126 | + variants.push(variant); |
| 127 | + for (let j = 0; j < 4; j++) { |
| 128 | + variant.push(points[(i + j) % 4]); |
| 129 | + } |
| 130 | + } |
| 131 | + for (let i = 0; i < 4; i++) { |
| 132 | + const variant: Point[] = []; |
| 133 | + variants.push(variant); |
| 134 | + for (let j = 0; j < 4; j++) { |
| 135 | + variant.push(pointsReversed[(i + j) % 4]); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + for (const points of variants) { |
| 140 | + const result = image.cropRectangle(points, { |
| 141 | + interpolationType: 'nearest', |
| 142 | + }); |
| 143 | + expect(result).toMatchImage(expected); |
| 144 | + } |
| 145 | +} |
0 commit comments