-
Notifications
You must be signed in to change notification settings - Fork 6
/
same_img_different_format-spec.js
147 lines (135 loc) · 4.43 KB
/
same_img_different_format-spec.js
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
#!/usr/bin/env babel-node
import fs from 'fs';
import PNG, { sync as PNGSync } from '../lib';
import test from 'tape';
import bufferEqual from 'buffer-equal';
const debugBufferMismatch = ({ rgbData, grayData }) => {
if (!rgbData) {
console.log('No rgbData');
return;
}
if (!grayData) {
console.log('No grayData');
return;
}
const errs = [];
for (let i = 0; i < rgbData.length; i += 1) {
if (rgbData[i] !== grayData[i]) {
errs[errs.length] = i;
if (errs.length > 20) {
break;
}
}
}
console.log(errs.map((i) => `[${i}] ${rgbData[i]} (rgb) != ${grayData[i]} (grayscale)`));
};
test('Check same image saved in different modes generate the same buffers', function(t) {
t.timeoutAfter(1000 * 60 * 5);
const grayscaleGradient = __dirname + '/imgs/img8_gradient.png';
const rgbGradient = __dirname + '/imgs/img8_gradient_rgb.png';
const { data: grayData } = PNGSync.read(fs.readFileSync(grayscaleGradient));
const { data: rgbData } = PNGSync.read(fs.readFileSync(rgbGradient));
let isBufferEqual = bufferEqual(grayData, rgbData);
t.ok(isBufferEqual, 'compares with grayscale file ok');
if (!isBufferEqual) {
debugBufferMismatch({
rgbData,
grayData,
});
}
t.end();
});
test('Check same image saved in different modes generate the same buffers in async with stream', function(t) {
t.timeoutAfter(1000 * 60 * 5);
const grayscaleGradient = __dirname + '/imgs/img8_gradient.png';
const rgbGradient = __dirname + '/imgs/img8_gradient_rgb.png';
const grayPNG = new PNG();
const rgbPNG = new PNG();
fs.createReadStream(grayscaleGradient)
.pipe(grayPNG)
.on('error', function(error) {
t.fail(`Failed to convert grayscale file: ${error.message}`);
t.end();
})
.on('parsed', (grayData) => {
fs.createReadStream(rgbGradient)
.pipe(rgbPNG)
.on('error', function(error) {
t.fail(`Failed to convert rgb file: ${error.message}`);
t.end();
})
.on('parsed', (rgbData) => {
let isBufferEqual = bufferEqual(rgbData, grayData);
t.ok(isBufferEqual, 'compares with grayscale file ok');
if (!isBufferEqual) {
debugBufferMismatch({
rgbData,
grayData,
});
}
t.end();
});
});
});
test('Check same image saved in different modes generate the same buffers in async with initGrayscaleData', function(t) {
t.timeoutAfter(1000 * 60 * 5);
const options = {
initGrayscaleData: true,
};
const grayscaleGradient = __dirname + '/imgs/img8_gradient.png';
const rgbGradient = __dirname + '/imgs/img8_gradient_rgb.png';
const grayPNG = new PNG(options);
const rgbPNG = new PNG(options);
fs.createReadStream(grayscaleGradient)
.pipe(grayPNG)
.on('error', function(error) {
t.fail(`Failed to convert grayscale file: ${error.message}`);
t.end();
})
.on('parsed', (grayData) => {
fs.createReadStream(rgbGradient)
.pipe(rgbPNG)
.on('error', function(error) {
t.fail(`Failed to convert rgb file: ${error.message}`);
t.end();
})
.on('parsed', (rgbData) => {
let isBufferEqual = bufferEqual(rgbData, grayData);
t.ok(isBufferEqual, 'compares with grayscale file ok');
if (!isBufferEqual) {
debugBufferMismatch({
rgbData,
grayData,
});
}
t.end();
});
});
});
test('Check same image saved in different modes generate the same buffers in async - alternative syntax', function(t) {
t.timeoutAfter(1000 * 60 * 5);
const options = {
skipRescale: true,
initGrayscaleData: true,
initPropData: { shrinkMax: true, shrinkMin: true },
};
const grayscaleGradient = __dirname + '/imgs/img8_gradient.png';
const rgbGradient = __dirname + '/imgs/img8_gradient_rgb.png';
let buf = fs.readFileSync(grayscaleGradient);
const gray = new PNG(options);
gray.parse(buf, (error, grayData) => {
buf = fs.readFileSync(rgbGradient);
const rgb = new PNG(options);
rgb.parse(buf, (error2, rgbData) => {
let isBufferEqual = bufferEqual(rgbData.data, grayData.data);
t.ok(isBufferEqual, 'compares with grayscale file ok');
if (!isBufferEqual) {
debugBufferMismatch({
rgbData: rgbData.grayscaleData(),
grayData: grayData.grayscaleData(),
});
}
t.end();
});
});
});