mh-two-thousand-and-two
2024-04-12 7fc6dbf547b8899d949b67cdec36b96a7d1701c7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { Jimp, mkJGD, getTestDir } from '@jimp/test-utils';
import configure from '@jimp/custom';
import types from '@jimp/types';
 
import color from '../src';
 
const jimp = configure({ types: [types], plugins: [color] }, Jimp);
 
describe('Convolution', function() {
  this.timeout(15000);
 
  const imgs = [
    jimp.read(
      mkJGD(
        '22222222',
        '22222222',
        '22888822',
        '22888822',
        '22888822',
        '22888822',
        '22222222',
        '22222222'
      )
    ),
    jimp.read(
      mkJGD(
        '88222222',
        '88222222',
        '22222222',
        '22222222',
        '22222222',
        '22222222',
        '22222222',
        '22222222'
      )
    )
  ];
 
  let imgMid;
  let imgTopLeft; // stores the Jimp instances of the JGD images above.
 
  before(done => {
    Promise.all(imgs)
      .then(imgs => {
        imgMid = imgs[0];
        imgTopLeft = imgs[1];
        done();
      })
      .catch(done);
  });
 
  const sharpM = [[-1, -1, 0], [-1, 1, 1], [0, 1, 1]];
 
  it('3x3 sharp matrix on EDGE_EXTEND', done => {
    imgMid
      .clone()
      .convolution(sharpM)
      .getJGDSync()
      .should.be.sameJGD(
        mkJGD(
          '22222222',
          '28EEE822',
          '2EFFF802',
          '2EF88002',
          '2EF88002',
          '28800002',
          '22000002',
          '22222222'
        ),
        'Mid light block'
      );
    imgTopLeft
      .clone()
      .convolution(sharpM)
      .getJGDSync()
      .should.be.sameJGD(
        mkJGD(
          '80022222',
          '00022222',
          '00022222',
          '22222222',
          '22222222',
          '22222222',
          '22222222',
          '22222222'
        ),
        'Top left light block'
      );
    done();
  });
 
  it('3x3 sharp matrix on EDGE_WRAP', done => {
    imgMid
      .clone()
      .convolution(sharpM, jimp.EDGE_WRAP)
      .getJGDSync()
      .should.be.sameJGD(
        mkJGD(
          '66666666',
          '28EEE822',
          '2EFFF802',
          '2EF88002',
          '2EF88002',
          '28800002',
          '22000002',
          '22222222'
        ),
        'Mid light block'
      );
    imgTopLeft
      .clone()
      .convolution(sharpM, jimp.EDGE_WRAP)
      .getJGDSync()
      .should.be.sameJGD(
        mkJGD(
          'FC06666F',
          '80022228',
          '00022222',
          '22222222',
          '22222222',
          '22222222',
          '22222222',
          'E8222228'
        ),
        'Top left light block'
      );
    done();
  });
 
  it('3x3 sharp matrix on EDGE_CROP', done => {
    imgMid
      .clone()
      .convolution(sharpM, jimp.EDGE_CROP)
      .getJGDSync()
      .should.be.sameJGD(
        mkJGD(
          '86666662',
          '68EEE820',
          '6EFFF800',
          '6EF88000',
          '6EF88000',
          '68800000',
          '62000000',
          '20000000'
        ),
        'Mid light block'
      );
    imgTopLeft
      .clone()
      .convolution(sharpM, jimp.EDGE_CROP)
      .getJGDSync()
      .should.be.sameJGD(
        mkJGD(
          'FC066662',
          'C0022220',
          '00022220',
          '62222220',
          '62222220',
          '62222220',
          '62222220',
          '20000000'
        ),
        'Top left light block'
      );
    done();
  });
 
  it('new pixel value is greater than 255', async () => {
    const expectedImg = await jimp.read(
      getTestDir(__dirname) + '/images/qr-convoluted.png'
    );
    const image = await jimp.read(getTestDir(__dirname) + '/images/qr.jpg');
 
    image
      .convolution([
        [0, 0, 0, 0, 0],
        [0, 1, 1, 1, 0],
        [0, 1, 0, 1, 0],
        [0, 1, 1, 1, 0],
        [0, 0, 0, 0, 0]
      ])
      .bitmap.data.should.be.deepEqual(expectedImg.bitmap.data);
  });
});