forked from CartoDB/node-mapnik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblend.test.js
478 lines (432 loc) · 19 KB
/
blend.test.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
"use strict";
var mapnik = require('../');
var assert = require('assert');
var fs = require('fs');
var images = [
fs.readFileSync('test/blend-fixtures/1.png'),
fs.readFileSync('test/blend-fixtures/2.png')
];
var images_reversed = [
fs.readFileSync('test/blend-fixtures/2.png'),
fs.readFileSync('test/blend-fixtures/1.png')
];
var images_one = [
fs.readFileSync('test/blend-fixtures/1.png')
];
var images_bad = [
fs.readFileSync('test/blend-fixtures/not_a_real_image.txt'),
fs.readFileSync('test/blend-fixtures/not_a_real_image.txt')
];
var images_alpha = [
fs.readFileSync('test/blend-fixtures/1a.png'),
fs.readFileSync('test/blend-fixtures/2a.png')
];
var images_tiny = [
fs.readFileSync('test/blend-fixtures/1x1.png'),
fs.readFileSync('test/blend-fixtures/1x1.png')
];
describe('mapnik.blend', function() {
it('blend fails', function() {
assert.throws(function() { mapnik.blend(); });
assert.throws(function() { mapnik.blend(images); });
assert.throws(function() { mapnik.blend(images, null); });
assert.throws(function() { mapnik.blend(images, null, function(err, result) {}); });
assert.throws(function() { mapnik.blend(images, {}, null); });
assert.throws(function() { mapnik.blend(images, {format:'foo'}, function(err, result) {}); });
assert.throws(function() { mapnik.blend([1,2], function(err, result) {}); });
assert.throws(function() { mapnik.blend(images_bad, function(err, result) {}); if (err) throw err;});
assert.throws(function() { mapnik.blend(images, {matte:'foo'}, function(err, result) {}); });
assert.throws(function() { mapnik.blend(images, {matte:''}, function(err, result) {}); });
assert.throws(function() { mapnik.blend(images, {matte:'#0000000'}, function(err, result) {}); });
assert.throws(function() { mapnik.blend(images, {compression:20}, function(err, result) {}); });
assert.throws(function() { mapnik.blend(images, {compression:'foo'}, function(err, result) {}); });
assert.throws(function() { mapnik.blend(images, {width:-1}, function(err, result) {}); });
assert.throws(function() { mapnik.blend(images, {height:-1}, function(err, result) {}); });
});
it('blended png', function(done) {
var expected = new mapnik.Image.open('test/blend-fixtures/expected.png');
mapnik.blend(images, function(err, result) {
if (err) throw err;
var actual = new mapnik.Image.fromBytesSync(result);
//actual.save('test/blend-fixtures/actual.png')
assert.equal(0,expected.compare(actual));
done();
});
});
it('blended png - reverse', function(done) {
var expected = new mapnik.Image.open('test/blend-fixtures/expected-reverse.png');
mapnik.blend(images_reversed, function(err, result) {
if (err) throw err;
var actual = new mapnik.Image.fromBytesSync(result);
//actual.save('test/blend-fixtures/actual-reverse.png')
assert.equal(0,expected.compare(actual));
done();
});
});
it('blended png - objects', function(done) {
var input = [{
buffer: fs.readFileSync('test/blend-fixtures/1.png')
},{
buffer: fs.readFileSync('test/blend-fixtures/2.png')
}];
var expected = new mapnik.Image.open('test/blend-fixtures/expected.png');
mapnik.blend(input, function(err, result) {
if (err) throw err;
var actual = new mapnik.Image.fromBytesSync(result);
//actual.save('test/blend-fixtures/actual.png')
assert.equal(0,expected.compare(actual));
done();
});
});
it('blended png - objects - BAD fails', function() {
var input = [{
buffer: null
},{
buffer: fs.readFileSync('test/blend-fixtures/2.png')
}];
assert.throws(function() { mapnik.blend(input, function(err, result) {
if (err) throw err;
}); });
});
it('blended png - objects - x and y', function(done) {
var input = [{
buffer: fs.readFileSync('test/blend-fixtures/1.png'),
x: 10,
y: 10
},{
buffer: fs.readFileSync('test/blend-fixtures/2.png')
}];
var expected = new mapnik.Image.open('test/blend-fixtures/expected-object-x-y.png');
mapnik.blend(input, function(err, result) {
if (err) throw err;
var actual = new mapnik.Image.fromBytesSync(result);
//actual.save('test/blend-fixtures/actual.png')
assert.equal(0,expected.compare(actual));
done();
});
});
it('blended png - single objects', function(done) {
var input = [{
buffer: fs.readFileSync('test/blend-fixtures/1a.png'),
}];
assert.throws(function() { mapnik.blend([{}], function(err, result) {} ); });
var expected = new mapnik.Image.open('test/blend-fixtures/1a.png');
mapnik.blend(input, function(err, result) {
if (err) throw err;
var actual = new mapnik.Image.fromBytesSync(result);
//actual.save('test/blend-fixtures/actual.png')
assert.equal(0,expected.compare(actual));
done();
});
});
it('blended png - single objects failure 0', function(done) {
var input = [{
buffer: fs.readFileSync('test/blend-fixtures/1a.png'),
x: -256,
y: -256
}];
mapnik.blend(input, function(err, result) {
assert.throws(function() { if (err) throw err; });
done();
});
});
it('blended png - single objects failure 1', function(done) {
var input = [{
buffer: fs.readFileSync('test/blend-fixtures/1a.png'),
x:-260,
y:-260
}];
mapnik.blend(input, {width:0, height:0}, function(err, result) {
assert.throws(function() {if (err) throw err; });
done();
});
});
it('blended png - single objects failure 2', function(done) {
var input = [{
buffer: fs.readFileSync('test/blend-fixtures/corrupt-1.png'),
}];
mapnik.blend(input, {width:1, height:1}, function(err, result) {
assert.throws(function() {
if (err) throw err;
});
done();
});
});
it('should fail reencode no buffers no width and height', function() {
var input = [];
assert.throws(function () { mapnik.blend(input, {reencode:true}, function(e, r) {}); });
});
it('blended png empty array', function(done) {
var input = [];
//var expected = new mapnik.Image.open('test/blend-fixtures/expected-object-x-y.png');
assert.throws(function() { mapnik.blend(input, {width:1, height:1}, function(err, result) {}) });
assert.throws(function() { mapnik.blend(input, {width:0, height:0, reeconde:true}, function(err, result) {}) });
mapnik.blend(input, {width:5, height:5, reencode:true}, function(err, result) {
if (err) throw err;
assert.ok(result);
//var actual = new mapnik.Image.fromBytesSync(result);
//actual.save('test/blend-fixtures/actual.png')
//assert.equal(0,expected.compare(actual));
done();
});
});
it('blended png - objects - tinting', function(done) {
var input = [{
buffer: fs.readFileSync('test/blend-fixtures/1a.png'),
tint: {
h: [0.1, 0.9],
s: [0.1, 0.9],
l: [0.1, 0.9],
a: [0.1, 0.9]
}
},{
buffer: fs.readFileSync('test/blend-fixtures/2a.png')
}];
var expected = new mapnik.Image.open('test/blend-fixtures/expected-object-tint.png');
mapnik.blend(input, function(err, result) {
if (err) throw err;
var actual = new mapnik.Image.fromBytesSync(result);
//actual.save('test/blend-fixtures/expected-object-tint.png')
assert.equal(0,expected.compare(actual));
done();
});
});
it('blended png - objects - tinting - fails h', function() {
var input = [{
buffer: fs.readFileSync('test/blend-fixtures/1a.png'),
tint: {
h: [0.1],
s: [0.1, 0.9],
l: [0.1, 0.9],
a: [0.1, 0.9]
}
},{
buffer: fs.readFileSync('test/blend-fixtures/2a.png')
}];
assert.throws(function() { mapnik.blend(input, function(err, result) {
if (err) throw err;
}); });
});
it('blended png - objects - tinting - fails s', function() {
var input = [{
buffer: fs.readFileSync('test/blend-fixtures/1a.png'),
tint: {
h: [0.1, 0.9],
s: [0.1],
l: [0.1, 0.9],
a: [0.1, 0.9]
}
},{
buffer: fs.readFileSync('test/blend-fixtures/2a.png')
}];
assert.throws(function() { mapnik.blend(input, function(err, result) {
if (err) throw err;
}); });
});
it('blended png - objects - tinting - fails l', function() {
var input = [{
buffer: fs.readFileSync('test/blend-fixtures/1a.png'),
tint: {
h: [0.1, 0.9],
s: [0.1, 0.9],
l: [0.1],
a: [0.1, 0.9]
}
},{
buffer: fs.readFileSync('test/blend-fixtures/2a.png')
}];
assert.throws(function() { mapnik.blend(input, function(err, result) {
if (err) throw err;
}); });
});
it('blended png - objects - tinting - fails a', function() {
var input = [{
buffer: fs.readFileSync('test/blend-fixtures/1a.png'),
tint: {
h: [0.1, 0.9],
s: [0.1, 0.9],
l: [0.1, 0.9],
a: [0.1]
}
},{
buffer: fs.readFileSync('test/blend-fixtures/2a.png')
}];
assert.throws(function() { mapnik.blend(input, function(err, result) {
if (err) throw err;
}); });
});
it('blended png - one image', function(done) {
// should be the same
var expected = new mapnik.Image.open('test/blend-fixtures/1.png');
mapnik.blend(images_one, function(err, result) {
if (err) throw err;
var actual = new mapnik.Image.fromBytesSync(result);
//actual.save('test/blend-fixtures/actual.png')
assert.equal(0,expected.compare(actual));
done();
});
});
it('blended png with palette', function(done) {
var pal = new mapnik.Palette(fs.readFileSync('./test/support/palettes/palette256.act'), 'act');
var expected = new mapnik.Image.open('test/blend-fixtures/expected-palette-256.png');
mapnik.blend(images, {palette:pal}, function(err, result) {
if (err) throw err;
var actual = new mapnik.Image.fromBytesSync(result);
//fs.writeFileSync('test/blend-fixtures/expected-palette-256.png',result);
assert.equal(0,expected.compare(actual));
done();
});
});
it('blended png with quality - paletted - octree', function(done) {
var expected = new mapnik.Image.open('test/blend-fixtures/expected-oct-palette-256.png');
mapnik.blend(images, {quality:256, mode:"octree"}, function(err, result) {
if (err) throw err;
var actual = new mapnik.Image.fromBytesSync(result);
//fs.writeFileSync('test/blend-fixtures/actual-oct-palette-256.png',result);
assert.equal(0,expected.compare(actual));
done();
});
});
it('blended png with quality - paletted - hextree', function(done) {
var expected = new mapnik.Image.open('test/blend-fixtures/expected-hex-palette-256.png');
mapnik.blend(images_alpha, {quality:256, mode:"hextree"}, function(err, result) {
if (err) throw err;
var actual = new mapnik.Image.fromBytesSync(result);
//fs.writeFileSync('test/blend-fixtures/actual-hex-palette-256.png',result);
assert.equal(0,expected.compare(actual));
done();
});
});
it('blended png with quality - paletted - hextree ("h") mode', function(done) {
var expected = new mapnik.Image.open('test/blend-fixtures/expected-hex-palette-256.png');
mapnik.blend(images_alpha, {quality:256, mode:"h"}, function(err, result) {
if (err) throw err;
var actual = new mapnik.Image.fromBytesSync(result);
//fs.writeFileSync('test/blend-fixtures/actual-hex-palette-256.png',result);
assert.equal(0,expected.compare(actual));
done();
});
});
it('blended fails png with quality - paletted - hextree', function(done) {
var expected = new mapnik.Image.open('test/blend-fixtures/expected-hex-palette-256-tiny.png');
mapnik.blend(images_tiny, {quality:256, mode:"hextree"}, function(err, result) {
if (err) throw err;
var actual = new mapnik.Image.fromBytesSync(result);
assert.equal(0,expected.compare(actual));
done();
});
});
it('blended png with compression', function(done) {
var expected = new mapnik.Image.open('test/blend-fixtures/expected.png');
mapnik.blend(images, {compression:8}, function(err, result) {
if (err) throw err;
var actual = new mapnik.Image.fromBytesSync(result);
//actual.save('test/blend-fixtures/actual.png')
assert.equal(0,expected.compare(actual));
done();
});
});
it('blended with matte 8', function(done) {
var expected = new mapnik.Image.open('test/blend-fixtures/expected-matte-8.png');
mapnik.blend(images_alpha, {matte: '#12345678'}, function(err, result) {
if (err) throw err;
var actual = new mapnik.Image.fromBytesSync(result);
//actual.save('test/blend-fixtures/actual-matte-8.png')
assert.equal(0,expected.compare(actual));
done();
});
});
it('blended with matte 6', function(done) {
var expected = new mapnik.Image.open('test/blend-fixtures/expected-matte-6.png');
mapnik.blend(images, {matte: '123456'}, function(err, result) {
if (err) throw err;
var actual = new mapnik.Image.fromBytesSync(result);
//actual.save('test/blend-fixtures/actual-matte-6.png')
assert.equal(0,expected.compare(actual));
done();
});
});
it('blended pass format png', function(done) {
assert.throws(function() {
mapnik.blend(images, {format:"png", quality:999}, function(err, result) {});
});
var expected = new mapnik.Image.open('test/blend-fixtures/expected.png');
mapnik.blend(images, {format:"png"}, function(err, result) {
if (err) throw err;
var actual = new mapnik.Image.fromBytesSync(result);
//actual.save('test/blend-fixtures/actual.png')
assert.equal(0,expected.compare(actual));
done();
});
});
it('blended pass format jpeg', function(done) {
assert.throws(function() {
mapnik.blend(images, {format:"jpeg", quality:101}, function(err, result) {});
});
var expected = new mapnik.Image.open('test/blend-fixtures/expected.jpg');
mapnik.blend(images, {format:"jpeg", quality:85}, function(err, result) {
if (err) throw err;
var actual = new mapnik.Image.fromBytesSync(result);
//fs.writeFileSync('test/blend-fixtures/actual.jpg',result);
// This is so flexible because windows is a PITA
assert(6200 > expected.compare(actual));
done();
});
});
it('blended pass format webp', function(done) {
assert.throws(function() {
mapnik.blend(images, {format:"webp", quality:999}, function(err, result) {});
});
var expected = new mapnik.Image.open('test/blend-fixtures/expected.webp');
mapnik.blend(images, {format:"webp"}, function(err, result) {
if (err) throw err;
var actual = new mapnik.Image.fromBytesSync(result);
//fs.writeFileSync('test/blend-fixtures/actual.webp',result);
assert.equal(0,expected.compare(actual));
done();
});
});
it('blended pass format webp with compression', function(done) {
assert.throws(function() {
mapnik.blend(images, {format:"webp", compression:999}, function(err, result) {});
});
var expected = new mapnik.Image.open('test/blend-fixtures/expected-compression-5.webp');
mapnik.blend(images, {format:"webp", compression:5}, function(err, result) {
if (err) throw err;
var actual = new mapnik.Image.fromBytesSync(result);
//fs.writeFileSync('test/blend-fixtures/actual-compression-5.webp',result);
var diff = expected.compare(actual);
// webp 0.5.0 leads to image with 730 diff pixels
assert.ok(diff <= 730);
done();
});
});
it('hsl to rgb works properly', function() {
// Assert throws on bad parameters
assert.throws(function() { mapnik.hsl2rgb(); });
assert.throws(function() { mapnik.hsl2rgb(1); });
assert.throws(function() { mapnik.hsl2rgb(1,2); });
assert.throws(function() { mapnik.hsl2rgb(1,2,'3'); });
var c = new mapnik.Color('green');
var result = mapnik.hsl2rgb(1/3,1,0.25098039215686274);
assert(Math.abs(result[0] - c.r) < 1e-7);
assert(Math.abs(result[1] - c.g) < 1e-7);
assert(Math.abs(result[2] - c.b) < 1e-7);
var result2 = mapnik.hsl2rgb(1/3,0,0.25098039215686274);
assert(Math.abs(result2[0] - 64) < 1e-7);
assert(Math.abs(result2[1] - 64) < 1e-7);
assert(Math.abs(result2[2] - 64) < 1e-7);
});
it('rgb to hsl works properly', function() {
// Assert throws on bad parameters
assert.throws(function() { mapnik.rgb2hsl(); });
assert.throws(function() { mapnik.rgb2hsl(1); });
assert.throws(function() { mapnik.rgb2hsl(1,2); });
assert.throws(function() { mapnik.rgb2hsl(1,2,'3'); });
var c = new mapnik.Color('green');
var result = mapnik.rgb2hsl(c.r,c.g,c.b);
assert(Math.abs(result[0] - 1/3) < 1e-7);
assert(Math.abs(result[1] - 1) < 1e-7);
assert(Math.abs(result[2] - 0.25098039215686274) < 1e-7);
});
});