forked from mapnik/node-mapnik
-
Notifications
You must be signed in to change notification settings - Fork 2
/
image_view.test.js
278 lines (250 loc) · 9.04 KB
/
image_view.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
"use strict";
var test = require('tape');
var mapnik = require('../');
var fs = require('fs');
test('should throw with invalid usage', (assert) => {
// no 'new' keyword
assert.throws(function() { mapnik.ImageView(1, 1); });
assert.throws(function() { new mapnik.ImageView(); });
assert.end();
});
test('should be initialized properly', (assert) => {
var im = new mapnik.Image(256, 256);
var view = im.view(0, 0, 256, 256);
assert.equal(view.isSolidSync(), true);
var pixel = view.getPixel(0, 0, {get_color:true});
assert.equal(pixel.r, 0);
assert.equal(pixel.g, 0);
assert.equal(pixel.b, 0);
assert.equal(pixel.a, 0);
im = new mapnik.Image(256, 256);
im.fill(new mapnik.Color(2, 2, 2, 2));
view = im.view(0, 0, 256, 256);
assert.equal(view.isSolidSync(), true);
pixel = view.getPixel(0, 0, {get_color:true});
assert.equal(pixel.r, 2);
assert.equal(pixel.g, 2);
assert.equal(pixel.b, 2);
assert.equal(pixel.a, 2);
assert.equal(view.getPixel(99999999, 9999999), undefined);
assert.end();
});
test('isSolid for view should return blue with view being offset', (assert) => {
var im = new mapnik.Image(256, 256);
im.fill(new mapnik.Color('blue'));
im.setPixel(0,0,new mapnik.Color('green'));
var view = im.view(1, 1, 255, 255);
assert.equal(view.isSolid(), true);
assert.equal(view.isSolidSync(), true);
view.isSolid(function(err,solid,pixel) {
assert.equal(solid, true);
assert.equal(pixel, 4294901760);
assert.end();
});
});
test('isSolid async works if true', (assert) => {
var im = new mapnik.Image(256, 256);
var view = im.view(0, 0, 256, 256);
assert.equal(view.isSolidSync(), true);
view.isSolid(function(err,solid,pixel) {
assert.equal(solid, true);
assert.equal(pixel, 0);
assert.end();
});
});
test('isSolid async works if true and white', (assert) => {
var im = new mapnik.Image(256, 256);
var color = new mapnik.Color('white');
im.fill(color);
var view = im.view(0, 0, 256, 256);
assert.equal(view.isSolidSync(), true);
view.isSolid(function(err,solid,pixel) {
assert.equal(solid, true);
assert.equal(pixel, 4294967295);
assert.end();
});
});
test('isSolid async works if false', (assert) => {
var im = new mapnik.Image.open('./test/support/a.png');
var view = im.view(0, 0, im.width(), im.height());
assert.equal(view.isSolid(), false);
assert.throws(function() { view.isSolid(null); });
assert.equal(view.isSolidSync(), false);
view.isSolid(function(err,solid,pixel) {
assert.equal(solid, false);
assert.equal(pixel, undefined);
assert.end();
});
});
test('isSolid should fail with bad parameters', (assert) => {
var im = new mapnik.Image(0,0);
var view = im.view(0,0,0,0);
assert.throws(function() { view.isSolidSync(); });
view.isSolid(function(err,solid,pixel) {
assert.throws(function() { if (err) throw err; });
assert.end();
});
});
test('getPixel should fail with bad parameters', (assert) => {
var im = new mapnik.Image(4,4,{type: mapnik.imageType.rgba8});
im.fill(1);
var view = im.view(0,0,4,4);
assert.throws(function() { view.getPixel(); });
assert.throws(function() { view.getPixel(1); });
assert.throws(function() { view.getPixel(1,'2'); });
assert.throws(function() { view.getPixel('1',2); });
assert.throws(function() { view.getPixel(1,2, null); });
assert.throws(function() { view.getPixel(1,2, {get_color:1}); });
assert.end();
});
test('getPixel supports rgba8', (assert) => {
var im = new mapnik.Image(4,4,{type: mapnik.imageType.rgba8});
im.fill(1);
var view = im.view(0,0,4,4);
assert.equal(view.getPixel(0,0), 1);
assert.end();
});
test('getPixel supports gray8', (assert) => {
var im = new mapnik.Image(4,4,{type: mapnik.imageType.gray8});
im.fill(1);
var view = im.view(0,0,4,4);
assert.equal(view.getPixel(0,0), 1);
assert.end();
});
test('getPixel supports gray8s', (assert) => {
var im = new mapnik.Image(4,4,{type: mapnik.imageType.gray8s});
im.fill(1);
var view = im.view(0,0,4,4);
assert.equal(view.getPixel(0,0), 1);
assert.end();
});
test('getPixel supports gray16', (assert) => {
var im = new mapnik.Image(4,4,{type: mapnik.imageType.gray16});
im.fill(1);
var view = im.view(0,0,4,4);
assert.equal(view.getPixel(0,0), 1);
assert.end();
});
test('getPixel supports gray16s', (assert) => {
var im = new mapnik.Image(4,4,{type: mapnik.imageType.gray16s});
im.fill(1);
var view = im.view(0,0,4,4);
assert.equal(view.getPixel(0,0), 1);
assert.end();
});
test('getPixel supports gray32', (assert) => {
var im = new mapnik.Image(4,4,{type: mapnik.imageType.gray32});
im.fill(1);
var view = im.view(0,0,4,4);
assert.equal(view.getPixel(0,0), 1);
assert.end();
});
test('getPixel supports gray32s', (assert) => {
var im = new mapnik.Image(4,4,{type: mapnik.imageType.gray32s});
im.fill(1);
var view = im.view(0,0,4,4);
assert.equal(view.getPixel(0,0), 1);
assert.end();
});
test('getPixel supports gray32f', (assert) => {
var im = new mapnik.Image(4,4,{type: mapnik.imageType.gray32f});
im.fill(1);
var view = im.view(0,0,4,4);
assert.equal(view.getPixel(0,0), 1);
assert.end();
});
test('getPixel supports gray64', (assert) => {
var im = new mapnik.Image(4,4,{type: mapnik.imageType.gray64});
im.fill(1);
var view = im.view(0,0,4,4);
assert.equal(view.getPixel(0,0), 1);
assert.end();
});
test('getPixel supports gray64s', (assert) => {
var im = new mapnik.Image(4,4,{type: mapnik.imageType.gray64s});
im.fill(1);
var view = im.view(0,0,4,4);
assert.equal(view.getPixel(0,0), 1);
assert.end();
});
test('getPixel supports gray64f', (assert) => {
var im = new mapnik.Image(4,4,{type: mapnik.imageType.gray64f});
im.fill(1);
var view = im.view(0,0,4,4);
assert.equal(view.getPixel(0,0), 1);
assert.end();
});
test('should throw with invalid encoding', (assert) => {
var im = new mapnik.Image(256, 256);
var view = im.view(0,0,256,256);
assert.throws(function() { view.encodeSync('foo'); });
assert.throws(function() { view.encodeSync(1); });
assert.throws(function() { view.encodeSync('png', null); });
assert.throws(function() { view.encodeSync('png', {palette:null}); });
assert.throws(function() { view.encodeSync('png', {palette:{}}); });
assert.throws(function() { view.encode('png', {palette:{}}, function(err, result) {}); });
assert.throws(function() { view.encode('png', {palette:null}, function(err, result) {}); });
assert.throws(function() { view.encode('png', null, function(err, result) {}); });
assert.throws(function() { view.encode(1, {}, function(err, result) {}); });
assert.throws(function() { view.encode('png', {}, null); });
view.encode('foo', {}, function(err, result) {
assert.throws(function() { if (err) throw err; });
assert.end();
});
});
test('should encode with a pallete', (assert) => {
var im = new mapnik.Image(256, 256);
var view = im.view(0,0,256,256);
var pal = new mapnik.Palette(Buffer.from('\xff\x09\x93\xFF\x01\x02\x03\x04','ascii'));
assert.ok(view.encodeSync('png', {palette:pal}));
view.encode('png', {palette:pal}, function(err, result) {
if (err) throw err;
assert.ok(result);
assert.end();
});
});
test('should be able to save an ImageView', (assert) => {
var im = new mapnik.Image(256, 256);
var view = im.view(0,0,256,256);
var pal = new mapnik.Palette(Buffer.from('\xff\x09\x93\xFF\x01\x02\x03\x04','ascii'));
var expected = './test/tmp/mapnik-image-view-saved.png';
view.saveSync(expected);
assert.ok(fs.existsSync(expected));
assert.end();
});
test('should throw with invalid formats', (assert) => {
var im = new mapnik.Image(256, 256);
var view = im.view(0,0,256,256);
assert.throws(function() { view.saveSync('foo','foo'); });
assert.throws(function() { view.saveSync(); });
assert.throws(function() { view.saveSync('file.png', null); });
assert.throws(function() { view.saveSync('foo'); });
assert.throws(function() { view.saveSync('foo','foo'); });
assert.throws(function() { view.saveSync(); });
assert.throws(function() { view.saveSync('file.png', null); });
assert.throws(function() { view.saveSync('foo'); });
assert.throws(function() { view.saveSync(); });
assert.throws(function() { view.saveSync('foo','foo'); });
assert.throws(function() { view.saveSync('file.png', null); });
assert.throws(function() { view.saveSync('foo'); });
assert.throws(function() { view.saveSync(function(err) {}); });
assert.throws(function() { view.saveSync('file.png', null, function(err) {}); });
assert.throws(function() { view.saveSync('foo', function(err) {}); });
assert.end();
});
if (mapnik.supports.webp) {
test('should support webp encoding', (assert) => {
var im = new mapnik.Image(256,256);
im.fill(new mapnik.Color('green'));
im.encode('webp',function(err,buf1) { // jshint ignore:line
if (err) throw err;
var v = im.view(0,0,256,256);
v.encode('webp', function(err,buf2) { // jshint ignore:line
if (err) throw err;
// disabled because this is not stable across mapnik versions or webp versions
//assert.equal(buf1.length,buf2.length);
assert.end();
});
});
});
}