-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlibraw.test.ts
More file actions
404 lines (359 loc) · 12.6 KB
/
libraw.test.ts
File metadata and controls
404 lines (359 loc) · 12.6 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
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
/*
* libraw.js - node wrapper for LibRaw
* Copyright (C) 2020-2021 Justin Kambic
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*
* Direct further questions to justinkambic.github@gmail.com.
*/
import { LibRaw } from '../src/libraw';
import path from 'path';
import fs, { promises as fsPromises } from 'fs';
import * as t from 'io-ts';
import { isRight } from 'fp-ts/Either';
const RAW_SONY_FILE_PATH = path.join(
__dirname,
'test_images/RAW_SONY_ILCA-77M2.ARW'
);
const RAW_NIKON_FILE_PATH = path.join(
__dirname,
'test_images/RAW_NIKON_Z6.NEF'
);
const TEST_THUMBNAIL_JPG = path.join(
__dirname,
'test_images',
'test_thumb.jpg'
);
const SAMPLE_TIFF = path.join(__dirname, 'test_images', 'sample.tiff');
const TEST_TIFF_OUTPUT_PATH = path.join(__dirname, 'test_images', 'test.tiff');
const __2dNumArray = t.array(t.array(t.number));
const __dngColor = t.array(
t.type({
calibration: __2dNumArray,
colormatrix: __2dNumArray,
forwardmatrix: __2dNumArray,
illuminant: t.number,
parsedfields: t.number,
})
);
const __dngLevels = t.type({
LinearResponseLimit: t.number,
analogbalance: t.array(t.number),
asshotneutral: t.array(t.number),
baseline_exposure: t.number,
default_crop: t.array(t.number),
dng_black: t.number,
dng_cblack: t.array(t.number),
dng_fblack: t.number,
dng_fcblack: t.array(t.number),
dng_whitelevel: t.array(t.number),
parsedfields: t.number,
preview_colorspace: t.number,
});
const __color = t.type({
P1_color: t.array(t.unknown),
WB_Coeffs: __2dNumArray,
WBCT_Coeffs: __2dNumArray,
cblack: t.array(t.number),
ccm: __2dNumArray,
cmatrix: __2dNumArray,
curve: t.array(t.number),
dng_color: __dngColor,
dng_levels: __dngLevels,
white: __2dNumArray,
});
/**
* This is not intended to be a definitive representation of the full
* compliment of metadata fields LibRaw could return. It provides some
* of the expected fields so it is easier for the tests to use the
* interpolated types `io-ts` creates. This dependency should only be used
* for testing purposes.
*/
const metadataType = t.type({
color: __color,
idata: t.type({
make: t.string,
model: t.string,
xmplen: t.number,
xmpdata: t.unknown,
xtrans: __2dNumArray,
xtrans_abs: __2dNumArray,
}),
lens: t.type({
Lens: t.string,
}),
makernotes: t.type({
canon: t.type({
AFAreaHeights: t.array(t.number),
AFAreaWidths: t.array(t.number),
AFAreaXPositions: t.array(t.number),
AFAreaYPositions: t.array(t.number),
}),
common: t.type({
CameraTemperature: t.number,
}),
}),
other: t.type({
gpsdata: t.array(t.number),
timestamp: t.number,
}),
rawdata: t.type({
color: __color,
iparams: t.type({
xtrans: __2dNumArray,
xtrans_abs: __2dNumArray,
}),
}),
});
type Metadata = t.TypeOf<typeof metadataType>;
function decodeLibRawMetadata(metadata: unknown) {
const decoded = metadataType.decode(metadata);
// `isRight` will return truthy if the provided metadata object corresponds to
// the type as defined above, otherwise, it will error log the context of each
// parsing failure that `io-ts` encountered. Typically this is enough to debug
// issues, but there are additional fields you can log when debugging tests.
if (!isRight(decoded)) {
console.error(JSON.stringify(decoded.left.map(({ context }) => context)));
throw new Error('Decoded metadata does not conform to expected type');
}
return decoded.right;
}
function deleteLargeFields(m: Metadata) {
// these fields are very large - `io-ts` verifies they conform to the
// expected type/shape, and then we delete them to keep snapshots manageable
delete m.color.P1_color;
delete m.color.WB_Coeffs;
delete m.color.WBCT_Coeffs;
delete m.color.cblack;
delete m.color.ccm;
delete m.color.cmatrix;
delete m.color.curve;
delete m.color.dng_color;
delete m.color.dng_levels;
delete m.color.white;
delete m.idata.xtrans;
delete m.idata.xtrans_abs;
delete m.other.gpsdata;
delete m.makernotes.canon.AFAreaHeights;
delete m.makernotes.canon.AFAreaWidths;
delete m.makernotes.canon.AFAreaXPositions;
delete m.makernotes.canon.AFAreaYPositions;
delete m.rawdata.color.WB_Coeffs;
delete m.rawdata.color.WBCT_Coeffs;
delete m.rawdata.color.cblack;
delete m.rawdata.color.ccm;
delete m.rawdata.color.cmatrix;
delete m.rawdata.color.curve;
delete m.rawdata.color.dng_color;
delete m.rawdata.color.dng_levels;
delete m.rawdata.color.white;
delete m.rawdata.iparams.xmpdata;
delete m.rawdata.iparams.xtrans;
delete m.rawdata.iparams.xtrans_abs;
}
/*
* To avoid issues with mismatched clocks on CI server
* we're replacing the value for the snapshot with a hardcoded one
* and ensuring the date has the appropriate d/m/y.
*/
function normalizeTimestampAndTest(
metadata: Metadata,
expectedDateVals: { day: number; month: number; year: number }
) {
const { day, month, year } = expectedDateVals;
const metadataDate = new Date(metadata.other.timestamp * 1000);
expect(metadataDate.getDate()).toBe(day);
expect(metadataDate.getMonth()).toBe(month);
expect(metadataDate.getFullYear()).toBe(year);
metadata.other.timestamp = 1414528361;
expect(metadata.other).toMatchSnapshot();
}
describe('LibRaw', () => {
let lr: LibRaw;
beforeEach(() => {
lr = new LibRaw();
});
afterEach(async () => {
await lr.recycle();
});
describe('getMetadata', () => {
test('basic metadata fields supported', async () => {
await lr.openFile(RAW_SONY_FILE_PATH);
const metadata = decodeLibRawMetadata(await lr.getMetadata());
deleteLargeFields(metadata);
expect(metadata.idata).toMatchSnapshot();
expect(metadata.lens).toMatchSnapshot();
normalizeTimestampAndTest(metadata, { day: 28, month: 9, year: 2014 });
expect(metadata.makernotes.common).toMatchSnapshot();
});
test('track key names to identify and prevent typo injection', async () => {
await lr.openFile(RAW_NIKON_FILE_PATH);
const metadata = decodeLibRawMetadata(await lr.getMetadata());
deleteLargeFields(metadata);
normalizeTimestampAndTest(metadata, { day: 26, month: 6, year: 2019 });
expect(metadata.idata.xmplen).toEqual(12289);
expect(metadata).toMatchSnapshot();
});
test('reads img data from buffer', async () => {
const buffer = fs.readFileSync(RAW_SONY_FILE_PATH);
await lr.readBuffer(buffer);
await lr.unpack();
const metadata = decodeLibRawMetadata(await lr.getMetadata());
deleteLargeFields(metadata);
expect(metadata.idata).toMatchSnapshot();
expect(metadata.lens).toMatchSnapshot();
normalizeTimestampAndTest(metadata, { day: 28, month: 9, year: 2014 });
expect(metadata.other).toMatchSnapshot();
});
});
describe('getXmp', () => {
test('parses xmp', async () => {
const lr = new LibRaw();
await lr.openFile(path.join(__dirname, 'test_images/RAW_NIKON_Z6.NEF'));
const xmp = (await lr.getXmp()).toString('utf8');
expect(xmp).toMatchSnapshot();
});
});
describe('errorCount', () => {
test('returns 0 with no errors', async () => {
await lr.openFile(RAW_SONY_FILE_PATH);
await lr.unpack();
expect(await lr.errorCount()).toBe(0);
});
});
describe('openFile', () => {
test('openFile opens file', async () => {
expect(await lr.openFile(RAW_NIKON_FILE_PATH)).toEqual(0);
});
test('openFile returns error code for nonexistent file', async () => {
expect(await lr.openFile('some nonexistent path')).not.toEqual(0);
});
test('throws exception if filename is not string', async () => {
// ignore ban-ts-comment for testing purposes
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore testing C++ condition
await expect(lr.openFile(23)).rejects.toThrow(
'openFile received an invalid argument, filename must be a string.'
);
});
test('throws exception if bigfile_size is not number', async () => {
// ignore ban-ts-comment for testing purposes
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore testing C++ condition
await expect(lr.openFile('a string', 'not a number')).rejects.toThrow(
'openFile received an invalid argument, bigfile_size must be a number.'
);
});
});
describe('openBuffer', () => {
test('throws exception if buffer is not provided', async () => {
// ignore ban-ts-comment for testing purposes
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore testing C++ condition
await expect(lr.openBuffer()).rejects.toThrow(
'openBuffer received a null argument, buffer is required.'
);
});
test('openBuffer accepts data', async () => {
const buffer = fs.readFileSync(RAW_SONY_FILE_PATH);
expect(await lr.openBuffer(buffer)).toEqual(0);
});
});
describe('unpack', () => {
test('unpacks image without error', async () => {
expect(await lr.openFile(RAW_NIKON_FILE_PATH)).toEqual(0);
expect(await lr.unpack()).toBe(0);
});
test('returns error code for un-opened file', async () => {
expect(await lr.unpack()).not.toEqual(0);
});
});
describe('unpackThumb', () => {
test('unpacks thumbnail without error', async () => {
expect(await lr.openFile(RAW_NIKON_FILE_PATH)).toBe(0);
expect(await lr.unpackThumb()).toBe(0);
});
test('returns out-of-order error code when unpacking un-opened file', async () => {
expect(await lr.unpackThumb()).toBe(-4);
});
});
describe('getThumbnail', () => {
test('throws error if there is no thumbnail', async () => {
await expect(lr.getThumbnail()).rejects.toThrow(
'Thumbnail is not unpacked or is null.'
);
});
test('gets the thumbnail from a RAW file', async () => {
expect(await lr.openFile(RAW_SONY_FILE_PATH)).toBe(0);
expect(await lr.unpackThumb()).toBe(0);
const thumbnail = await lr.getThumbnail();
const testBuffer = fs.readFileSync(TEST_THUMBNAIL_JPG);
expect(thumbnail.equals(testBuffer)).toBe(true);
});
});
describe('extractTiff', () => {
test('throws error if there is an invalid path', async () => {
expect(await lr.openFile(RAW_SONY_FILE_PATH)).toBe(0);
// @ts-expect-error testing C++ error logic
await expect(lr.extract_tiff(0)).rejects.toThrow(
'extract_tiff received an invalid argument, tiffpath must be a string.'
);
});
test('extracts a tiff', async () => {
jest.setTimeout(20000);
expect(await lr.openFile(RAW_SONY_FILE_PATH)).toBe(0);
expect(await lr.extract_tiff(TEST_TIFF_OUTPUT_PATH)).toBe(0);
const [output, sample] = await Promise.all([
fsPromises.readFile(TEST_TIFF_OUTPUT_PATH, { encoding: 'binary' }),
fsPromises.readFile(SAMPLE_TIFF, { encoding: 'binary' }),
]);
expect(output).toEqual(sample);
});
});
describe('cameraCount', () => {
test('gives the number of supported cameras', async () => {
expect(await lr.cameraCount()).toBe(1117);
});
});
describe('cameraList', () => {
test('gives a list of supported cameras', async () => {
expect(JSON.stringify(await lr.cameraList(), null, 2)).toMatchSnapshot();
});
});
describe('recycle', () => {
test('runs without error', async () => {
expect(await lr.openFile(RAW_SONY_FILE_PATH)).toBe(0);
expect(await lr.recycle());
});
});
describe('strerror', () => {
test('handles error code 4', async () => {
expect(await lr.strerror(-4)).toEqual(
'Out of order call of libraw function'
);
});
});
describe('version', () => {
test('returns version string', async () => {
expect(await lr.version()).toEqual('0.20.2-Release');
});
});
describe('versionNumber', () => {
test('returns a version number >= version at time of add-on creation', async () => {
expect(await lr.versionNumber()).toBeGreaterThanOrEqual(4869);
});
});
});