nbis-wrapper-js 0.2.0 → 0.3.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.
package/README.md CHANGED
@@ -64,7 +64,11 @@ const mindtct = new Mindtct({ autoConvert: false });
64
64
 
65
65
  // Or tune WSQ conversion:
66
66
  const tuned = new Mindtct({
67
- autoConvert: { wsqBitrate: 0.75, ppi: 500 }
67
+ autoConvert: {
68
+ wsqBitrate: 0.75,
69
+ ppi: 500,
70
+ upscale: { enabled: true, minSize: 256 }
71
+ }
68
72
  });
69
73
  ```
70
74
 
@@ -2,11 +2,20 @@ export type AutoConvertConfig = {
2
2
  enabled?: boolean;
3
3
  wsqBitrate?: number;
4
4
  ppi?: number;
5
+ upscale?: boolean | AutoConvertUpscaleConfig;
6
+ };
7
+ export type AutoConvertUpscaleConfig = {
8
+ enabled?: boolean;
9
+ minSize?: number;
5
10
  };
6
11
  export type NormalizedAutoConvertConfig = {
7
12
  enabled: boolean;
8
13
  wsqBitrate: number;
9
14
  ppi?: number;
15
+ upscale: {
16
+ enabled: boolean;
17
+ minSize: number;
18
+ };
10
19
  };
11
20
  export type PreparedImage = {
12
21
  path: string;
@@ -13,16 +13,26 @@ const jpeg_js_1 = __importDefault(require("jpeg-js"));
13
13
  const nbis_runtime_1 = require("./nbis-runtime");
14
14
  const SUPPORTED_ANSI_NIST_EXTENSIONS = new Set(['.an2', '.an2k', '.an2k7', '.eft']);
15
15
  function normalizeAutoConvert(config) {
16
+ const defaultUpscale = { enabled: false, minSize: 256 };
16
17
  if (config == null) {
17
- return { enabled: true, wsqBitrate: 0.75 };
18
+ return { enabled: true, wsqBitrate: 0.75, upscale: defaultUpscale };
18
19
  }
19
20
  if (typeof config === 'boolean') {
20
- return { enabled: config, wsqBitrate: 0.75 };
21
+ return { enabled: config, wsqBitrate: 0.75, upscale: defaultUpscale };
21
22
  }
23
+ const upscale = config.upscale == null
24
+ ? defaultUpscale
25
+ : typeof config.upscale === 'boolean'
26
+ ? { enabled: config.upscale, minSize: defaultUpscale.minSize }
27
+ : {
28
+ enabled: config.upscale.enabled ?? defaultUpscale.enabled,
29
+ minSize: config.upscale.minSize ?? defaultUpscale.minSize
30
+ };
22
31
  return {
23
32
  enabled: config.enabled ?? true,
24
33
  wsqBitrate: config.wsqBitrate ?? 0.75,
25
- ppi: config.ppi
34
+ ppi: config.ppi,
35
+ upscale
26
36
  };
27
37
  }
28
38
  function detectFormat(inputPath) {
@@ -61,6 +71,17 @@ function toGrayscale(rgba) {
61
71
  }
62
72
  return gray;
63
73
  }
74
+ function upscaleNearest(pixels, width, height, newWidth, newHeight) {
75
+ const scaled = Buffer.alloc(newWidth * newHeight);
76
+ for (let y = 0; y < newHeight; y += 1) {
77
+ const srcY = Math.min(height - 1, Math.floor((y * height) / newHeight));
78
+ for (let x = 0; x < newWidth; x += 1) {
79
+ const srcX = Math.min(width - 1, Math.floor((x * width) / newWidth));
80
+ scaled[y * newWidth + x] = pixels[srcY * width + srcX];
81
+ }
82
+ }
83
+ return scaled;
84
+ }
64
85
  function decodeImage(inputPath, format) {
65
86
  const data = fs_1.default.readFileSync(inputPath);
66
87
  if (format === 'png') {
@@ -84,9 +105,23 @@ function convertToWsq(inputPath, tempDir, config) {
84
105
  return inputPath;
85
106
  }
86
107
  const decoded = decodeImage(inputPath, format);
108
+ let width = decoded.width;
109
+ let height = decoded.height;
110
+ let pixels = decoded.pixels;
111
+ if (config.upscale.enabled) {
112
+ const minSize = config.upscale.minSize;
113
+ if (width < minSize || height < minSize) {
114
+ const scale = Math.max(minSize / width, minSize / height);
115
+ const newWidth = Math.ceil(width * scale);
116
+ const newHeight = Math.ceil(height * scale);
117
+ pixels = upscaleNearest(pixels, width, height, newWidth, newHeight);
118
+ width = newWidth;
119
+ height = newHeight;
120
+ }
121
+ }
87
122
  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];
123
+ fs_1.default.writeFileSync(rawPath, pixels);
124
+ const rawAttrs = [width, height, 8];
90
125
  if (config.ppi != null) {
91
126
  rawAttrs.push(config.ppi);
92
127
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nbis-wrapper-js",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Node.js wrappers for NBIS 5.0.0 binaries",
5
5
  "main": "dist/src/index.js",
6
6
  "types": "dist/src/index.d.ts",