tools.js
7.23 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
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
'use strict';
const assert = require('chai').assert;
const Path = require('path');
const Jimp = require('jimp');
const Bitmaps = require('./bitmaps');
const { BitmapImage } = require('../../src/index');
const { GifFrame } = require('../../src/gifframe');
exports.checkBitmap = function (bitmap, width, height, rgbaOrBuf) {
assert.strictEqual(bitmap.width, width, "width");
assert.strictEqual(bitmap.height, height, "height");
if (Buffer.isBuffer(rgbaOrBuf)) {
assert.strictEqual(bitmap.data.length, rgbaOrBuf.length, "length");
for (let i = 0; i < rgbaOrBuf.length; ++i ) {
assert.strictEqual(rgbaOrBuf[i], bitmap.data[i], "at buffer index "+ i);
}
}
else if (typeof rgbaOrBuf === 'number') {
const rgba = rgbaOrBuf
const buf = bitmap.data;
for (let bi = 0; bi < buf.length; bi += 4) {
const found = buf.readUInt32BE(bi);
if (found !== rgba) {
assert.fail(found, rgba, `buffer fill color ${found} != ${rgba}`);
}
}
}
else {
throw new Error("last checkBitmap() param must be a color or a Buffer");
}
}
exports.checkFrameDefaults = function (actualInfo, options, frameIndex = 0) {
options = Object.assign({}, options); // don't munge caller
options.xOffset = options.xOffset || 0;
options.yOffset = options.yOffset || 0;
options.delayCentisecs = options.delayCentisecs || 8;
options.interlaced = (options.interlaced === true);
options.disposalMethod =
(options.disposalMethod || GifFrame.DisposeToBackgroundColor);
exports.verifyFrameInfo(actualInfo, options, frameIndex);
};
exports.compareToFrameDump = function (actualFrames, expectedDump) {
assert(Array.isArray(actualFrames));
assert.strictEqual(actualFrames.length, expectedDump.length);
for (let i = 0; i < actualFrames.length; ++i) {
const actualFrame = actualFrames[i];
const dump = expectedDump[i];
exports.verifyFrameInfo(actualFrame, {
xOffset: dump[0],
yOffset: dump[1],
bitmap: {
width: dump[2],
height: dump[3]
},
delayCentisecs: dump[4],
interlaced: dump[5],
disposalMethod: dump[6]
}, i);
}
};
exports.dumpFramesAsCode = function (frames) {
let first = true;
process.stdout.write("[\n");
frames.forEach(f => {
if (!first) {
process.stdout.write(",\n");
}
process.stdout.write(` [${f.xOffset}, ${f.yOffset}, `+
`${f.bitmap.width}, ${f.bitmap.height}, `+
`${f.delayCentisecs}, ${f.interlaced}, ${f.disposalMethod}]`);
first = false;
});
process.stdout.write("\n]\n");
};
exports.getBitmap = function (bitmapName, transparentRGB) {
const stringPic = Bitmaps.PREMADE[bitmapName];
if (stringPic === undefined) {
throw new Error(`Bitmap '${bitmapName}' not found`);
}
if (Array.isArray(stringPic[0])) {
throw new Error(`'${bitmapName}' is a bitmap series`);
}
return _stringsToBitmap(stringPic, transparentRGB);
};
exports.getFixturePath = function (filename) {
return Path.join(__dirname, "../fixtures", filename);
};
exports.getGifPath = function (filenameMinusExtension) {
return exports.getFixturePath(filenameMinusExtension + '.gif');
};
exports.getImagePath = function (filename) {
return exports.getFixturePath(filename);
};
exports.getSeries = function (seriesName, transparentRGB) {
const series = Bitmaps.PREMADE[seriesName];
if (series === undefined) {
throw new Error(`Bitmap series '${seriesName}' not found`);
}
if (!Array.isArray(series[0])) {
throw new Error(`'${seriesName}' is not a bitmap series`);
}
return series.map(stringPic =>
(_stringsToBitmap(stringPic, transparentRGB)));
};
exports.loadBitmapImage = function (imagePath) {
return new Promise((resolve, reject) => {
new Jimp(imagePath, (err, jimp) => {
if (err) return reject(err);
resolve(new BitmapImage(jimp.bitmap));
});
});
};
exports.saveBitmapImage = function (bitmapImage, path) {
let jimp = new Jimp(1, 1, 0);
jimp.bitmap = bitmapImage.bitmap;
return new Promise((resolve, reject) => {
jimp.write(path, (err) => {
if (err) return reject(err);
resolve();
});
});
};
exports.verifyFrameInfo = function (actual, expected, frameIndex=0, note='') {
expected = Object.assign({}, expected); // don't munge caller
if (expected.xOffset !== undefined) {
assert.strictEqual(actual.xOffset, expected.xOffset,
`frame ${frameIndex} same x offset${note}`);
}
if (expected.yOffset !== undefined) {
assert.strictEqual(actual.yOffset, expected.yOffset,
`frame ${frameIndex} same y offset${note}`);
}
if (expected.bitmap !== undefined) {
assert.strictEqual(actual.bitmap.width, expected.bitmap.width,
`frame ${frameIndex} same width${note}`);
assert.strictEqual(actual.bitmap.height, expected.bitmap.height,
`frame ${frameIndex} same height${note}`);
}
if (expected.delayCentisecs !== undefined) {
assert.strictEqual(actual.delayCentisecs, expected.delayCentisecs,
`frame ${frameIndex} same delay${note}`);
}
if (expected.disposalMethod !== undefined) {
assert.strictEqual(actual.disposalMethod, expected.disposalMethod,
`frame ${frameIndex} same disposal method${note}`);
}
assert.strictEqual(actual.interlaced, (expected.interlaced === true),
`frame ${frameIndex} same interlacing${note}`);
};
function _stringsToBitmap(stringPic, transparentRGB) {
const trans = transparentRGB; // shortens code, leaves parameter clear
const width = stringPic[0].length;
const height = stringPic.length;
const data = new Buffer(width * height * 4);
let offset = 0;
for (let y = 0; y < height; ++y) {
const row = stringPic[y];
if (row.length !== width) {
throw new Error("Inconsistent pixel string length");
}
for (let x = 0; x < width; ++x) {
if (Bitmaps.COLORS[row[x]] !== undefined) {
const color = Bitmaps.COLORS[row[x]];
const alpha = color & 0xff;
if (alpha !== 0 || trans === undefined) {
data[offset] = (color >> 24) & 0xff;
data[offset + 1] = (color >> 16) & 0xff;
data[offset + 2] = (color >> 8) & 0xff;
data[offset + 3] = color & 0xff;
}
else {
// not concerned about speed
data[offset] = (trans >> 16) & 0xff;
data[offset + 1] = (trans >> 8) & 0xff;
data[offset + 2] = trans & 0xff;
data[offset + 3] = 0;
}
offset += 4;
}
else {
const validChars = Object.keys(Bitmaps.COLORS).join('');
throw new Error(`Invalid pixel char '${row[x]}'. `+
`Valid chars are "${validChars}".`);
}
}
}
return { width, height, data };
}