test_bitmap.js
4.88 KB
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
'use strict';
const assert = require('chai').assert;
const Jimp = require('jimp');
const Tools = require('./lib/tools');
const { BitmapImage, GifUtil } = require('../src/index');
const SAMPLE_PNG_PATH = Tools.getFixturePath('lenna.png');
const SAMPLE_JPG_PATH = Tools.getFixturePath('pelagrina.jpg');
describe("BitmapImage construction behavior", () => {
it("constructs an empty uncolored image", (done) => {
const i = new BitmapImage(10, 5);
Tools.checkBitmap(i.bitmap, 10, 5, 0);
done();
});
it("constructs an empty colored image", (done) => {
const color = 0x01020304;
const i = new BitmapImage(10, 5, color);
Tools.checkBitmap(i.bitmap, 10, 5, color);
done();
});
it("constructs a buffer-sourced image", (done) => {
const buf = new Buffer(10 * 5);
const i = new BitmapImage(10, 5, buf);
Tools.checkBitmap(i.bitmap, 10, 5, buf);
done();
});
it("sourced bitmaps are shared", (done) => {
const b = { width: 5, height: 6, data: new Buffer(5 * 6) };
const j = new BitmapImage(b);
assert.strictEqual(b, j.bitmap);
done();
});
it("sourced images are copied", (done) => {
const color = 0x01020304;
const j1 = new BitmapImage(10, 5, color);
const j2 = new BitmapImage(j1);
assert.notStrictEqual(j1.bitmap, j2.bitmap);
assert.deepStrictEqual(j1.bitmap, j2.bitmap);
done();
});
});
describe("GifFrame bad construction behavior", () => {
it("won't accept garbage", (done) => {
assert.throws(() => {
new BitmapImage();
}, /requires parameters/);
assert.throws(() => {
new BitmapImage(null);
}, /unrecognized/);
assert.throws(() => {
new BitmapImage("string");
}, /unrecognized/);
assert.throws(() => {
new BitmapImage(() => {});
}, /unrecognized/);
assert.throws(() => {
new BitmapImage({});
}, /unrecognized/);
assert.throws(() => {
new BitmapImage(new Buffer(25));
}, /unrecognized/);
done();
});
it("width requires height", (done) => {
assert.throws(() => {
new BitmapImage(5);
}, /unrecognized/);
assert.throws(() => {
new BitmapImage(5, new Buffer(5));
}, /unrecognized/);
assert.throws(() => {
new BitmapImage(5, {});
}, /unrecognized/);
done();
});
});
// TBD: test BitmapImage transformation methods
describe("Jimp compatibility", () => {
it("works when sourced from Jimp", (done) => {
new Jimp(SAMPLE_PNG_PATH, (err, j1) => {
if (err) return done(err);
assert.strictEqual(err, null);
const initialColor = j1.getPixelColor(5, 5);
const i = new BitmapImage(j1.bitmap);
assert.strictEqual(i.getRGBA(5, 5), initialColor);
const newColor = initialColor + 0x01010101;
i.fillRGBA(newColor);
assert.strictEqual(i.getRGBA(5, 5), newColor);
const j2 = GifUtil.shareAsJimp(Jimp, i);
assert.strictEqual(j2.getPixelColor(5, 5), newColor);
done();
});
});
it("works when sourcing Jimp via sharing", (done) => {
const initialColor = 0x12344321;
const i1 = new BitmapImage(10, 5, initialColor);
const j = GifUtil.shareAsJimp(Jimp, i1);
assert.strictEqual(j.getPixelColor(3, 3), initialColor);
const newColor = initialColor + 0x01010101;
j.setPixelColor(newColor, 3, 3);
assert.strictEqual(j.getPixelColor(3, 3), newColor);
const i2 = new BitmapImage(j.bitmap);
assert.strictEqual(i2.getRGBA(3,3), newColor);
done();
});
it("works when sourcing Jimp via copying", (done) => {
const initialColor = 0x12344321;
const i1 = new BitmapImage(10, 5, initialColor);
const j = GifUtil.copyAsJimp(Jimp, i1);
assert.strictEqual(j.getPixelColor(3, 3), initialColor);
const newColor = initialColor + 0x01010101;
j.setPixelColor(newColor, 3, 3);
assert.strictEqual(j.getPixelColor(3, 3), newColor);
const i2 = new BitmapImage(j.bitmap);
assert.strictEqual(i2.getRGBA(3,3), newColor);
done();
});
it("composing with a sprite having transparency", (done) => {
const i1 = new BitmapImage(Tools.getBitmap('singleFrameMonoOpaque'));
const j1 = GifUtil.shareAsJimp(Jimp, i1);
const i2 = new BitmapImage(Tools.getBitmap('sampleSprite'));
const j2 = GifUtil.shareAsJimp(Jimp, i2);
j1.composite(j2, 1, 1);
const result = new BitmapImage(j1.bitmap);
const expected = new BitmapImage(Tools.getBitmap('singleFrameMonoOpaqueSpriteAt1x1'));
assert.deepStrictEqual(result.bitmap, expected.bitmap);
done();
});
});