nbis-wrapper-js 0.0.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/README.md +50 -0
  2. package/dist/src/an2ktool.d.ts +12 -0
  3. package/dist/src/an2ktool.js +40 -0
  4. package/dist/src/bozorth3.d.ts +16 -0
  5. package/dist/src/bozorth3.js +152 -2
  6. package/dist/src/chkan2k.d.ts +2 -0
  7. package/dist/src/chkan2k.js +30 -0
  8. package/dist/src/cjp2k.d.ts +10 -0
  9. package/dist/src/cjp2k.js +51 -0
  10. package/dist/src/cjpeg.d.ts +8 -0
  11. package/dist/src/cjpeg.js +61 -0
  12. package/dist/src/cjpegb.d.ts +8 -0
  13. package/dist/src/cjpegb.js +25 -0
  14. package/dist/src/cjpegl.d.ts +8 -0
  15. package/dist/src/cjpegl.js +28 -0
  16. package/dist/src/cwsq.d.ts +8 -0
  17. package/dist/src/cwsq.js +22 -0
  18. package/dist/src/datainfo.d.ts +2 -0
  19. package/dist/src/datainfo.js +12 -0
  20. package/dist/src/djp2k.d.ts +10 -0
  21. package/dist/src/djp2k.js +52 -0
  22. package/dist/src/djpeg.d.ts +8 -0
  23. package/dist/src/djpeg.js +64 -0
  24. package/dist/src/djpegb.d.ts +9 -0
  25. package/dist/src/djpegb.js +26 -0
  26. package/dist/src/djpegl.d.ts +9 -0
  27. package/dist/src/djpegl.js +26 -0
  28. package/dist/src/dwsq.d.ts +9 -0
  29. package/dist/src/dwsq.js +23 -0
  30. package/dist/src/image-convert.d.ts +17 -0
  31. package/dist/src/image-convert.js +118 -0
  32. package/dist/src/intr2not.d.ts +8 -0
  33. package/dist/src/intr2not.js +22 -0
  34. package/dist/src/jpegtran.d.ts +8 -0
  35. package/dist/src/jpegtran.js +82 -0
  36. package/dist/src/mindtct.d.ts +18 -0
  37. package/dist/src/mindtct.js +80 -25
  38. package/dist/src/nfiq.d.ts +14 -0
  39. package/dist/src/nfiq.js +71 -21
  40. package/dist/src/not2intr.d.ts +13 -0
  41. package/dist/src/not2intr.js +22 -0
  42. package/dist/src/rdjpgcom.d.ts +3 -0
  43. package/dist/src/rdjpgcom.js +17 -0
  44. package/dist/src/rdwsqcom.d.ts +2 -0
  45. package/dist/src/rdwsqcom.js +12 -0
  46. package/dist/src/rgb2ycc.d.ts +8 -0
  47. package/dist/src/rgb2ycc.js +25 -0
  48. package/dist/src/temp-io.d.ts +15 -0
  49. package/dist/src/temp-io.js +102 -0
  50. package/dist/src/wrjpgcom.d.ts +7 -0
  51. package/dist/src/wrjpgcom.js +26 -0
  52. package/dist/src/wrwsqcom.d.ts +8 -0
  53. package/dist/src/wrwsqcom.js +21 -0
  54. package/dist/src/ycc2rgb.d.ts +13 -0
  55. package/dist/src/ycc2rgb.js +25 -0
  56. package/package.json +5 -1
@@ -1,3 +1,4 @@
1
+ import type { StreamInput, StreamIoOptions } from './temp-io';
1
2
  export type CjpeglOptions = {
2
3
  rawIn?: {
3
4
  width: number;
@@ -18,7 +19,14 @@ export type CjpeglResult = {
18
19
  stdout: string;
19
20
  stderr: string;
20
21
  };
22
+ export type CjpeglStreamOptions = CjpeglOptions & StreamIoOptions;
23
+ export type CjpeglStreamResult = {
24
+ stdout: string;
25
+ stderr: string;
26
+ outputBuffer?: Buffer;
27
+ };
21
28
  export declare class Cjpegl {
22
29
  version(): string;
23
30
  compress(outputExtension: string, imagePath: string, options?: CjpeglOptions): CjpeglResult;
31
+ compressStream(outputExtension: string, input: StreamInput, options?: CjpeglStreamOptions): Promise<CjpeglStreamResult>;
24
32
  }
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.Cjpegl = void 0;
7
7
  const path_1 = __importDefault(require("path"));
8
8
  const nbis_runtime_1 = require("./nbis-runtime");
9
+ const temp_io_1 = require("./temp-io");
9
10
  function buildRawAttr(raw) {
10
11
  const attrs = [raw.width, raw.height, raw.depth];
11
12
  if (raw.ppi != null) {
@@ -50,5 +51,32 @@ class Cjpegl {
50
51
  stderr: result.stderr
51
52
  };
52
53
  }
54
+ async compressStream(outputExtension, input, options = {}) {
55
+ return (0, temp_io_1.withTempDir)(async (tempDir) => {
56
+ const inputPath = await (0, temp_io_1.writeTempInput)(tempDir, input, options);
57
+ const args = [outputExtension, inputPath];
58
+ if (options.rawIn) {
59
+ args.push('-raw_in', buildRawAttr(options.rawIn));
60
+ }
61
+ if (options.nonInterleaved) {
62
+ args.push('-nonintrlv');
63
+ }
64
+ if (options.yCbCr) {
65
+ args.push('-YCbCr', buildYCbCr(options.yCbCr));
66
+ }
67
+ if (options.commentFile) {
68
+ args.push(options.commentFile);
69
+ }
70
+ const result = (0, nbis_runtime_1.runNbis)('cjpegl', args);
71
+ (0, nbis_runtime_1.assertSuccess)(result, 'cjpegl');
72
+ const outputPath = replaceExtension(inputPath, outputExtension);
73
+ const outputBuffer = await (0, temp_io_1.deliverTempOutput)(outputPath, options);
74
+ return {
75
+ stdout: result.stdout,
76
+ stderr: result.stderr,
77
+ outputBuffer
78
+ };
79
+ });
80
+ }
53
81
  }
54
82
  exports.Cjpegl = Cjpegl;
@@ -1,3 +1,4 @@
1
+ import type { StreamInput, StreamIoOptions } from './temp-io';
1
2
  export type CwsqOptions = {
2
3
  rawIn?: {
3
4
  width: number;
@@ -12,7 +13,14 @@ export type CwsqResult = {
12
13
  stdout: string;
13
14
  stderr: string;
14
15
  };
16
+ export type CwsqStreamOptions = CwsqOptions & StreamIoOptions;
17
+ export type CwsqStreamResult = {
18
+ stdout: string;
19
+ stderr: string;
20
+ outputBuffer?: Buffer;
21
+ };
15
22
  export declare class Cwsq {
16
23
  version(): string;
17
24
  compress(bitrate: number, outputExtension: string, imagePath: string, options?: CwsqOptions): CwsqResult;
25
+ compressStream(bitrate: number, outputExtension: string, input: StreamInput, options?: CwsqStreamOptions): Promise<CwsqStreamResult>;
18
26
  }
package/dist/src/cwsq.js CHANGED
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.Cwsq = void 0;
7
7
  const path_1 = __importDefault(require("path"));
8
8
  const nbis_runtime_1 = require("./nbis-runtime");
9
+ const temp_io_1 = require("./temp-io");
9
10
  function buildRawAttr(raw) {
10
11
  const attrs = [raw.width, raw.height, raw.depth];
11
12
  if (raw.ppi != null) {
@@ -40,5 +41,26 @@ class Cwsq {
40
41
  stderr: result.stderr
41
42
  };
42
43
  }
44
+ async compressStream(bitrate, outputExtension, input, options = {}) {
45
+ return (0, temp_io_1.withTempDir)(async (tempDir) => {
46
+ const inputPath = await (0, temp_io_1.writeTempInput)(tempDir, input, options);
47
+ const args = [String(bitrate), outputExtension, inputPath];
48
+ if (options.rawIn) {
49
+ args.push('-raw_in', buildRawAttr(options.rawIn));
50
+ }
51
+ if (options.commentFile) {
52
+ args.push(options.commentFile);
53
+ }
54
+ const result = (0, nbis_runtime_1.runNbis)('cwsq', args);
55
+ (0, nbis_runtime_1.assertSuccess)(result, 'cwsq');
56
+ const outputPath = replaceExtension(inputPath, outputExtension);
57
+ const outputBuffer = await (0, temp_io_1.deliverTempOutput)(outputPath, options);
58
+ return {
59
+ stdout: result.stdout,
60
+ stderr: result.stderr,
61
+ outputBuffer
62
+ };
63
+ });
64
+ }
43
65
  }
44
66
  exports.Cwsq = Cwsq;
@@ -1,3 +1,4 @@
1
+ import type { StreamInput, StreamInputOptions } from './temp-io';
1
2
  export type DatainfoResult = {
2
3
  stdout: string;
3
4
  stderr: string;
@@ -5,4 +6,5 @@ export type DatainfoResult = {
5
6
  export declare class Datainfo {
6
7
  version(): string;
7
8
  report(dataFile: string): DatainfoResult;
9
+ reportStream(input: StreamInput, options?: StreamInputOptions): Promise<DatainfoResult>;
8
10
  }
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Datainfo = void 0;
4
4
  const nbis_runtime_1 = require("./nbis-runtime");
5
+ const temp_io_1 = require("./temp-io");
5
6
  class Datainfo {
6
7
  version() {
7
8
  const result = (0, nbis_runtime_1.runNbis)('datainfo', ['-version']);
@@ -16,5 +17,16 @@ class Datainfo {
16
17
  stderr: result.stderr
17
18
  };
18
19
  }
20
+ async reportStream(input, options = {}) {
21
+ return (0, temp_io_1.withTempDir)(async (tempDir) => {
22
+ const dataFile = await (0, temp_io_1.writeTempInput)(tempDir, input, options);
23
+ const result = (0, nbis_runtime_1.runNbis)('datainfo', [dataFile]);
24
+ (0, nbis_runtime_1.assertSuccess)(result, 'datainfo');
25
+ return {
26
+ stdout: result.stdout,
27
+ stderr: result.stderr
28
+ };
29
+ });
30
+ }
19
31
  }
20
32
  exports.Datainfo = Datainfo;
@@ -1,3 +1,4 @@
1
+ import type { StreamInput, StreamIoOptions } from './temp-io';
1
2
  export type Djp2kOptions = {
2
3
  inputPath?: string;
3
4
  outputPath?: string;
@@ -14,7 +15,16 @@ export type Djp2kResult = {
14
15
  stdout: string;
15
16
  stderr: string;
16
17
  };
18
+ export type Djp2kStreamOptions = Omit<Djp2kOptions, 'inputPath' | 'outputPath' | 'imageDir'> & StreamIoOptions & {
19
+ outputExtension?: string;
20
+ };
21
+ export type Djp2kStreamResult = {
22
+ stdout: string;
23
+ stderr: string;
24
+ outputBuffer?: Buffer;
25
+ };
17
26
  export declare class Djp2k {
18
27
  version(): string;
19
28
  decompress(options: Djp2kOptions): Djp2kResult;
29
+ decompressStream(input: StreamInput, options?: Djp2kStreamOptions): Promise<Djp2kStreamResult>;
20
30
  }
package/dist/src/djp2k.js CHANGED
@@ -1,7 +1,18 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.Djp2k = void 0;
7
+ const path_1 = __importDefault(require("path"));
4
8
  const nbis_runtime_1 = require("./nbis-runtime");
9
+ const temp_io_1 = require("./temp-io");
10
+ function normalizeExtension(extension) {
11
+ if (!extension) {
12
+ return '';
13
+ }
14
+ return extension.startsWith('.') ? extension : `.${extension}`;
15
+ }
5
16
  class Djp2k {
6
17
  version() {
7
18
  const result = (0, nbis_runtime_1.runNbis)('djp2k', ['-h']);
@@ -49,5 +60,46 @@ class Djp2k {
49
60
  stderr: result.stderr
50
61
  };
51
62
  }
63
+ async decompressStream(input, options = {}) {
64
+ return (0, temp_io_1.withTempDir)(async (tempDir) => {
65
+ const inputPath = await (0, temp_io_1.writeTempInput)(tempDir, input, options);
66
+ const outputExtension = options.outputExtension || options.outputFormat;
67
+ if (!outputExtension) {
68
+ throw new Error('djp2k stream output requires outputExtension or outputFormat.');
69
+ }
70
+ const outputPath = path_1.default.join(tempDir, `output${normalizeExtension(outputExtension)}`);
71
+ const args = [];
72
+ if (options.outputFormat) {
73
+ args.push('-OutFor', options.outputFormat);
74
+ }
75
+ if (options.reduceFactor != null) {
76
+ args.push('-r', String(options.reduceFactor));
77
+ }
78
+ if (options.layers != null) {
79
+ args.push('-l', String(options.layers));
80
+ }
81
+ if (options.indexFile) {
82
+ args.push('-x', options.indexFile);
83
+ }
84
+ if (options.decodeArea) {
85
+ args.push('-d', options.decodeArea);
86
+ }
87
+ if (options.tileNumber != null) {
88
+ args.push('-t', String(options.tileNumber));
89
+ }
90
+ if (options.extraArgs) {
91
+ args.push(...options.extraArgs);
92
+ }
93
+ args.push('-i', inputPath, '-o', outputPath);
94
+ const result = (0, nbis_runtime_1.runNbis)('djp2k', args);
95
+ (0, nbis_runtime_1.assertSuccess)(result, 'djp2k');
96
+ const outputBuffer = await (0, temp_io_1.deliverTempOutput)(outputPath, options);
97
+ return {
98
+ stdout: result.stdout,
99
+ stderr: result.stderr,
100
+ outputBuffer
101
+ };
102
+ });
103
+ }
52
104
  }
53
105
  exports.Djp2k = Djp2k;
@@ -1,3 +1,4 @@
1
+ import type { StreamInput, StreamIoOptions } from './temp-io';
1
2
  export type DjpegOptions = {
2
3
  colors?: number;
3
4
  fast?: boolean;
@@ -20,6 +21,13 @@ export type DjpegResult = {
20
21
  stderr: string;
21
22
  outputPath?: string;
22
23
  };
24
+ export type DjpegStreamOptions = Omit<DjpegOptions, 'outputPath'> & StreamIoOptions;
25
+ export type DjpegStreamResult = {
26
+ stdout: string;
27
+ stderr: string;
28
+ outputBuffer?: Buffer;
29
+ };
23
30
  export declare class Djpeg {
24
31
  decompress(inputPath: string, options?: DjpegOptions): DjpegResult;
32
+ decompressStream(input: StreamInput, options?: DjpegStreamOptions): Promise<DjpegStreamResult>;
25
33
  }
package/dist/src/djpeg.js CHANGED
@@ -1,7 +1,12 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.Djpeg = void 0;
7
+ const path_1 = __importDefault(require("path"));
4
8
  const nbis_runtime_1 = require("./nbis-runtime");
9
+ const temp_io_1 = require("./temp-io");
5
10
  class Djpeg {
6
11
  decompress(inputPath, options = {}) {
7
12
  const args = [];
@@ -59,5 +64,64 @@ class Djpeg {
59
64
  outputPath: options.outputPath
60
65
  };
61
66
  }
67
+ async decompressStream(input, options = {}) {
68
+ return (0, temp_io_1.withTempDir)(async (tempDir) => {
69
+ const inputPath = await (0, temp_io_1.writeTempInput)(tempDir, input, options);
70
+ const outputPath = path_1.default.join(tempDir, 'output');
71
+ const args = [];
72
+ if (options.colors != null) {
73
+ args.push('-colors', String(options.colors));
74
+ }
75
+ if (options.fast) {
76
+ args.push('-fast');
77
+ }
78
+ if (options.grayscale) {
79
+ args.push('-grayscale');
80
+ }
81
+ if (options.scale) {
82
+ args.push('-scale', options.scale);
83
+ }
84
+ if (options.format) {
85
+ args.push(`-${options.format}`);
86
+ }
87
+ if (options.dct) {
88
+ args.push('-dct', options.dct);
89
+ }
90
+ if (options.dither) {
91
+ args.push('-dither', options.dither);
92
+ }
93
+ if (options.mapFile) {
94
+ args.push('-map', options.mapFile);
95
+ }
96
+ if (options.noSmooth) {
97
+ args.push('-nosmooth');
98
+ }
99
+ if (options.onePass) {
100
+ args.push('-onepass');
101
+ }
102
+ if (options.maxMemory) {
103
+ args.push('-maxmemory', options.maxMemory);
104
+ }
105
+ args.push('-outfile', outputPath);
106
+ if (options.verbose) {
107
+ args.push('-verbose');
108
+ }
109
+ if (options.debug) {
110
+ args.push('-debug');
111
+ }
112
+ if (options.extraArgs) {
113
+ args.push(...options.extraArgs);
114
+ }
115
+ args.push(inputPath);
116
+ const result = (0, nbis_runtime_1.runNbis)('djpeg', args);
117
+ (0, nbis_runtime_1.assertSuccess)(result, 'djpeg');
118
+ const outputBuffer = await (0, temp_io_1.deliverTempOutput)(outputPath, options);
119
+ return {
120
+ stdout: result.stdout,
121
+ stderr: result.stderr,
122
+ outputBuffer
123
+ };
124
+ });
125
+ }
62
126
  }
63
127
  exports.Djpeg = Djpeg;
@@ -1,3 +1,4 @@
1
+ import type { StreamInput, StreamIoOptions } from './temp-io';
1
2
  export type DjpegbOptions = {
2
3
  rawOut?: boolean;
3
4
  nonInterleaved?: boolean;
@@ -8,7 +9,15 @@ export type DjpegbResult = {
8
9
  stdout: string;
9
10
  stderr: string;
10
11
  };
12
+ export type DjpegbStreamOptions = DjpegbOptions & StreamIoOptions;
13
+ export type DjpegbStreamResult = {
14
+ stdout: string;
15
+ stderr: string;
16
+ outputBuffer?: Buffer;
17
+ nistcomBuffer?: Buffer;
18
+ };
11
19
  export declare class Djpegb {
12
20
  version(): string;
13
21
  decompress(outputExtension: string, imagePath: string, options?: DjpegbOptions): DjpegbResult;
22
+ decompressStream(outputExtension: string, input: StreamInput, options?: DjpegbStreamOptions): Promise<DjpegbStreamResult>;
14
23
  }
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.Djpegb = void 0;
7
7
  const path_1 = __importDefault(require("path"));
8
8
  const nbis_runtime_1 = require("./nbis-runtime");
9
+ const temp_io_1 = require("./temp-io");
9
10
  function replaceExtension(inputPath, outputExtension) {
10
11
  const parsed = path_1.default.parse(inputPath);
11
12
  const ext = outputExtension.startsWith('.') ? outputExtension : `.${outputExtension}`;
@@ -35,5 +36,30 @@ class Djpegb {
35
36
  stderr: result.stderr
36
37
  };
37
38
  }
39
+ async decompressStream(outputExtension, input, options = {}) {
40
+ return (0, temp_io_1.withTempDir)(async (tempDir) => {
41
+ const inputPath = await (0, temp_io_1.writeTempInput)(tempDir, input, options);
42
+ const args = [outputExtension, inputPath];
43
+ if (options.rawOut) {
44
+ args.push('-raw_out');
45
+ }
46
+ if (options.nonInterleaved) {
47
+ args.push('-nonintrlv');
48
+ }
49
+ const result = (0, nbis_runtime_1.runNbis)('djpegb', args);
50
+ (0, nbis_runtime_1.assertSuccess)(result, 'djpegb');
51
+ const outputPath = replaceExtension(inputPath, outputExtension);
52
+ const parsed = path_1.default.parse(inputPath);
53
+ const nistcomPath = path_1.default.join(parsed.dir, `${parsed.name}.ncm`);
54
+ const outputBuffer = await (0, temp_io_1.deliverTempOutput)(outputPath, options);
55
+ const nistcomBuffer = await (0, temp_io_1.readFileIfExists)(nistcomPath);
56
+ return {
57
+ stdout: result.stdout,
58
+ stderr: result.stderr,
59
+ outputBuffer,
60
+ nistcomBuffer
61
+ };
62
+ });
63
+ }
38
64
  }
39
65
  exports.Djpegb = Djpegb;
@@ -1,3 +1,4 @@
1
+ import type { StreamInput, StreamIoOptions } from './temp-io';
1
2
  export type DjpeglOptions = {
2
3
  rawOut?: boolean;
3
4
  nonInterleaved?: boolean;
@@ -8,7 +9,15 @@ export type DjpeglResult = {
8
9
  stdout: string;
9
10
  stderr: string;
10
11
  };
12
+ export type DjpeglStreamOptions = DjpeglOptions & StreamIoOptions;
13
+ export type DjpeglStreamResult = {
14
+ stdout: string;
15
+ stderr: string;
16
+ outputBuffer?: Buffer;
17
+ nistcomBuffer?: Buffer;
18
+ };
11
19
  export declare class Djpegl {
12
20
  version(): string;
13
21
  decompress(outputExtension: string, imagePath: string, options?: DjpeglOptions): DjpeglResult;
22
+ decompressStream(outputExtension: string, input: StreamInput, options?: DjpeglStreamOptions): Promise<DjpeglStreamResult>;
14
23
  }
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.Djpegl = void 0;
7
7
  const path_1 = __importDefault(require("path"));
8
8
  const nbis_runtime_1 = require("./nbis-runtime");
9
+ const temp_io_1 = require("./temp-io");
9
10
  function replaceExtension(inputPath, outputExtension) {
10
11
  const parsed = path_1.default.parse(inputPath);
11
12
  const ext = outputExtension.startsWith('.') ? outputExtension : `.${outputExtension}`;
@@ -35,5 +36,30 @@ class Djpegl {
35
36
  stderr: result.stderr
36
37
  };
37
38
  }
39
+ async decompressStream(outputExtension, input, options = {}) {
40
+ return (0, temp_io_1.withTempDir)(async (tempDir) => {
41
+ const inputPath = await (0, temp_io_1.writeTempInput)(tempDir, input, options);
42
+ const args = [outputExtension, inputPath];
43
+ if (options.rawOut) {
44
+ args.push('-raw_out');
45
+ }
46
+ if (options.nonInterleaved) {
47
+ args.push('-nonintrlv');
48
+ }
49
+ const result = (0, nbis_runtime_1.runNbis)('djpegl', args);
50
+ (0, nbis_runtime_1.assertSuccess)(result, 'djpegl');
51
+ const outputPath = replaceExtension(inputPath, outputExtension);
52
+ const parsed = path_1.default.parse(inputPath);
53
+ const nistcomPath = path_1.default.join(parsed.dir, `${parsed.name}.ncm`);
54
+ const outputBuffer = await (0, temp_io_1.deliverTempOutput)(outputPath, options);
55
+ const nistcomBuffer = await (0, temp_io_1.readFileIfExists)(nistcomPath);
56
+ return {
57
+ stdout: result.stdout,
58
+ stderr: result.stderr,
59
+ outputBuffer,
60
+ nistcomBuffer
61
+ };
62
+ });
63
+ }
38
64
  }
39
65
  exports.Djpegl = Djpegl;
@@ -1,3 +1,4 @@
1
+ import type { StreamInput, StreamIoOptions } from './temp-io';
1
2
  export type DwsqOptions = {
2
3
  rawOut?: boolean;
3
4
  };
@@ -7,7 +8,15 @@ export type DwsqResult = {
7
8
  stdout: string;
8
9
  stderr: string;
9
10
  };
11
+ export type DwsqStreamOptions = DwsqOptions & StreamIoOptions;
12
+ export type DwsqStreamResult = {
13
+ stdout: string;
14
+ stderr: string;
15
+ outputBuffer?: Buffer;
16
+ nistcomBuffer?: Buffer;
17
+ };
10
18
  export declare class Dwsq {
11
19
  version(): string;
12
20
  decompress(outputExtension: string, imagePath: string, options?: DwsqOptions): DwsqResult;
21
+ decompressStream(outputExtension: string, input: StreamInput, options?: DwsqStreamOptions): Promise<DwsqStreamResult>;
13
22
  }
package/dist/src/dwsq.js CHANGED
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.Dwsq = void 0;
7
7
  const path_1 = __importDefault(require("path"));
8
8
  const nbis_runtime_1 = require("./nbis-runtime");
9
+ const temp_io_1 = require("./temp-io");
9
10
  function replaceExtension(inputPath, outputExtension) {
10
11
  const parsed = path_1.default.parse(inputPath);
11
12
  const ext = outputExtension.startsWith('.') ? outputExtension : `.${outputExtension}`;
@@ -32,5 +33,27 @@ class Dwsq {
32
33
  stderr: result.stderr
33
34
  };
34
35
  }
36
+ async decompressStream(outputExtension, input, options = {}) {
37
+ return (0, temp_io_1.withTempDir)(async (tempDir) => {
38
+ const inputPath = await (0, temp_io_1.writeTempInput)(tempDir, input, options);
39
+ const args = [outputExtension, inputPath];
40
+ if (options.rawOut) {
41
+ args.push('-raw_out');
42
+ }
43
+ const result = (0, nbis_runtime_1.runNbis)('dwsq', args);
44
+ (0, nbis_runtime_1.assertSuccess)(result, 'dwsq');
45
+ const outputPath = replaceExtension(inputPath, outputExtension);
46
+ const parsed = path_1.default.parse(inputPath);
47
+ const nistcomPath = path_1.default.join(parsed.dir, `${parsed.name}.ncm`);
48
+ const outputBuffer = await (0, temp_io_1.deliverTempOutput)(outputPath, options);
49
+ const nistcomBuffer = await (0, temp_io_1.readFileIfExists)(nistcomPath);
50
+ return {
51
+ stdout: result.stdout,
52
+ stderr: result.stderr,
53
+ outputBuffer,
54
+ nistcomBuffer
55
+ };
56
+ });
57
+ }
35
58
  }
36
59
  exports.Dwsq = Dwsq;
@@ -0,0 +1,17 @@
1
+ export type AutoConvertConfig = {
2
+ enabled?: boolean;
3
+ wsqBitrate?: number;
4
+ ppi?: number;
5
+ };
6
+ export type NormalizedAutoConvertConfig = {
7
+ enabled: boolean;
8
+ wsqBitrate: number;
9
+ ppi?: number;
10
+ };
11
+ export type PreparedImage = {
12
+ path: string;
13
+ cleanup?: () => void;
14
+ converted?: boolean;
15
+ };
16
+ export declare function prepareFingerprintImageSync(inputPath: string, options?: boolean | AutoConvertConfig, tempDir?: string): PreparedImage;
17
+ export declare function normalizeAutoConvertConfig(options?: boolean | AutoConvertConfig): NormalizedAutoConvertConfig;
@@ -0,0 +1,118 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.prepareFingerprintImageSync = prepareFingerprintImageSync;
7
+ exports.normalizeAutoConvertConfig = normalizeAutoConvertConfig;
8
+ const fs_1 = __importDefault(require("fs"));
9
+ const os_1 = __importDefault(require("os"));
10
+ const path_1 = __importDefault(require("path"));
11
+ const pngjs_1 = require("pngjs");
12
+ const jpeg_js_1 = __importDefault(require("jpeg-js"));
13
+ const nbis_runtime_1 = require("./nbis-runtime");
14
+ const SUPPORTED_ANSI_NIST_EXTENSIONS = new Set(['.an2', '.an2k', '.an2k7', '.eft']);
15
+ function normalizeAutoConvert(config) {
16
+ if (config == null) {
17
+ return { enabled: true, wsqBitrate: 0.75 };
18
+ }
19
+ if (typeof config === 'boolean') {
20
+ return { enabled: config, wsqBitrate: 0.75 };
21
+ }
22
+ return {
23
+ enabled: config.enabled ?? true,
24
+ wsqBitrate: config.wsqBitrate ?? 0.75,
25
+ ppi: config.ppi
26
+ };
27
+ }
28
+ function detectFormat(inputPath) {
29
+ const ext = path_1.default.extname(inputPath).toLowerCase();
30
+ if (ext === '.wsq') {
31
+ return 'wsq';
32
+ }
33
+ if (SUPPORTED_ANSI_NIST_EXTENSIONS.has(ext)) {
34
+ return 'ansi-nist';
35
+ }
36
+ const fd = fs_1.default.openSync(inputPath, 'r');
37
+ try {
38
+ const header = Buffer.alloc(8);
39
+ const bytes = fs_1.default.readSync(fd, header, 0, header.length, 0);
40
+ if (bytes >= 8 && header.slice(0, 8).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]))) {
41
+ return 'png';
42
+ }
43
+ if (bytes >= 3 && header[0] === 0xff && header[1] === 0xd8 && header[2] === 0xff) {
44
+ return 'jpeg';
45
+ }
46
+ }
47
+ finally {
48
+ fs_1.default.closeSync(fd);
49
+ }
50
+ return 'unknown';
51
+ }
52
+ function toGrayscale(rgba) {
53
+ const pixelCount = Math.floor(rgba.length / 4);
54
+ const gray = Buffer.alloc(pixelCount);
55
+ for (let i = 0; i < pixelCount; i += 1) {
56
+ const idx = i * 4;
57
+ const r = rgba[idx];
58
+ const g = rgba[idx + 1];
59
+ const b = rgba[idx + 2];
60
+ gray[i] = Math.round(0.299 * r + 0.587 * g + 0.114 * b);
61
+ }
62
+ return gray;
63
+ }
64
+ function decodeImage(inputPath, format) {
65
+ const data = fs_1.default.readFileSync(inputPath);
66
+ if (format === 'png') {
67
+ const decoded = pngjs_1.PNG.sync.read(data);
68
+ return { width: decoded.width, height: decoded.height, pixels: toGrayscale(decoded.data) };
69
+ }
70
+ const decoded = jpeg_js_1.default.decode(data, { useTArray: true });
71
+ if (!decoded || !decoded.data) {
72
+ throw new Error('jpeg decode failed.');
73
+ }
74
+ return { width: decoded.width, height: decoded.height, pixels: toGrayscale(Buffer.from(decoded.data)) };
75
+ }
76
+ function replaceExtension(inputPath, outputExtension) {
77
+ const parsed = path_1.default.parse(inputPath);
78
+ const ext = outputExtension.startsWith('.') ? outputExtension : `.${outputExtension}`;
79
+ return path_1.default.join(parsed.dir, `${parsed.name}${ext}`);
80
+ }
81
+ function convertToWsq(inputPath, tempDir, config) {
82
+ const format = detectFormat(inputPath);
83
+ if (format !== 'png' && format !== 'jpeg') {
84
+ return inputPath;
85
+ }
86
+ const decoded = decodeImage(inputPath, format);
87
+ const rawPath = path_1.default.join(tempDir, 'input.raw');
88
+ fs_1.default.writeFileSync(rawPath, decoded.pixels);
89
+ const rawAttrs = [decoded.width, decoded.height, 8];
90
+ if (config.ppi != null) {
91
+ rawAttrs.push(config.ppi);
92
+ }
93
+ const args = [String(config.wsqBitrate), 'wsq', rawPath, '-raw_in', rawAttrs.join(',')];
94
+ const result = (0, nbis_runtime_1.runNbis)('cwsq', args);
95
+ (0, nbis_runtime_1.assertSuccess)(result, 'cwsq (auto-convert)');
96
+ return replaceExtension(rawPath, 'wsq');
97
+ }
98
+ function prepareFingerprintImageSync(inputPath, options, tempDir) {
99
+ const config = normalizeAutoConvert(options);
100
+ if (!config.enabled) {
101
+ return { path: inputPath };
102
+ }
103
+ const format = detectFormat(inputPath);
104
+ if (format === 'wsq' || format === 'ansi-nist' || format === 'unknown') {
105
+ return { path: inputPath };
106
+ }
107
+ const ownedTempDir = tempDir ?? fs_1.default.mkdtempSync(path_1.default.join(os_1.default.tmpdir(), 'nbis-img-'));
108
+ const convertedPath = convertToWsq(inputPath, ownedTempDir, config);
109
+ const cleanup = tempDir
110
+ ? undefined
111
+ : () => {
112
+ fs_1.default.rmSync(ownedTempDir, { recursive: true, force: true });
113
+ };
114
+ return { path: convertedPath, cleanup, converted: convertedPath !== inputPath };
115
+ }
116
+ function normalizeAutoConvertConfig(options) {
117
+ return normalizeAutoConvert(options);
118
+ }
@@ -1,3 +1,4 @@
1
+ import type { StreamInput, StreamIoOptions } from './temp-io';
1
2
  export type Intr2notOptions = {
2
3
  rawIn?: {
3
4
  width: number;
@@ -16,7 +17,14 @@ export type Intr2notResult = {
16
17
  stdout: string;
17
18
  stderr: string;
18
19
  };
20
+ export type Intr2notStreamOptions = Intr2notOptions & StreamIoOptions;
21
+ export type Intr2notStreamResult = {
22
+ stdout: string;
23
+ stderr: string;
24
+ outputBuffer?: Buffer;
25
+ };
19
26
  export declare class Intr2not {
20
27
  version(): string;
21
28
  convert(outputExtension: string, imagePath: string, options?: Intr2notOptions): Intr2notResult;
29
+ convertStream(outputExtension: string, input: StreamInput, options?: Intr2notStreamOptions): Promise<Intr2notStreamResult>;
22
30
  }