bare-jpeg 1.0.2 → 1.1.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
@@ -24,6 +24,30 @@ const encoded = jpeg.encode(decoded)
24
24
  // <Buffer>
25
25
  ```
26
26
 
27
+ Header:
28
+
29
+ ```js
30
+ const header = jpeg.readHeader(image)
31
+ // {
32
+ // width: 200,
33
+ // height: 400,
34
+ // colorSpace: 3,
35
+ // ...
36
+ // markers: [{ marker: 224, data: <Buffer> }, ...] // APP0-APP15 and COM
37
+ // }
38
+ ```
39
+
40
+ Utility to replace markers:
41
+
42
+ ```js
43
+ const newImage = jpeg.replaceMarkers(image, [
44
+ { marker: 0xfe, data: Buffer.from('This is a comment') }
45
+ ])
46
+ // <Buffer>
47
+ ```
48
+
49
+ `replaceMarkers()` returns a new image, replacing the existing `APP0`-`APP15` and `COM` markers without re-encoding the pixel data.
50
+
27
51
  ## License
28
52
 
29
53
  Apache-2.0
package/binding.c CHANGED
@@ -4,6 +4,7 @@
4
4
  #include <js.h>
5
5
  #include <setjmp.h>
6
6
  #include <stdlib.h>
7
+ #include <string.h>
7
8
 
8
9
  typedef struct {
9
10
  struct jpeg_error_mgr handle;
@@ -219,6 +220,208 @@ bare_jpeg_encode(js_env_t *env, js_callback_info_t *info) {
219
220
  return result;
220
221
  }
221
222
 
223
+ static js_value_t *
224
+ bare_jpeg_read_header(js_env_t *env, js_callback_info_t *info) {
225
+ int err;
226
+
227
+ size_t argc = 1;
228
+ js_value_t *argv[1];
229
+
230
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
231
+ assert(err == 0);
232
+
233
+ assert(argc == 1);
234
+
235
+ uint8_t *jpeg;
236
+ size_t len;
237
+ err = js_get_typedarray_info(env, argv[0], NULL, (void **) &jpeg, &len, NULL, NULL);
238
+ assert(err == 0);
239
+
240
+ bare_jpeg_error_t error;
241
+ struct jpeg_decompress_struct cinfo;
242
+
243
+ cinfo.err = jpeg_std_error(&error.handle);
244
+
245
+ jpeg_create_decompress(&cinfo);
246
+
247
+ error.handle.error_exit = bare_jpeg__on_error_exit;
248
+ error.handle.emit_message = bare_jpeg__on_emit_message;
249
+
250
+ if (setjmp(error.jump)) {
251
+ err:
252
+ err = js_throw_error(env, NULL, error.message);
253
+ assert(err == 0);
254
+
255
+ jpeg_destroy_decompress(&cinfo);
256
+
257
+ return NULL;
258
+ }
259
+
260
+ jpeg_mem_src(&cinfo, jpeg, len);
261
+
262
+ for (int marker = 0; marker < 16; marker++) {
263
+ jpeg_save_markers(&cinfo, JPEG_APP0 + marker, 0xffff);
264
+ }
265
+
266
+ jpeg_save_markers(&cinfo, JPEG_COM, 0xffff);
267
+
268
+ if (jpeg_read_header(&cinfo, true) != JPEG_HEADER_OK) goto err;
269
+
270
+ js_value_t *result;
271
+ err = js_create_object(env, &result);
272
+ assert(err == 0);
273
+
274
+ #define V_SET_INT(target, name, value) \
275
+ { \
276
+ js_value_t *val; \
277
+ err = js_create_int64(env, value, &val); \
278
+ assert(err == 0); \
279
+ err = js_set_named_property(env, target, name, val); \
280
+ assert(err == 0); \
281
+ }
282
+
283
+ #define V_SET_BOOL(target, name, value) \
284
+ { \
285
+ js_value_t *val; \
286
+ err = js_get_boolean(env, value, &val); \
287
+ assert(err == 0); \
288
+ err = js_set_named_property(env, target, name, val); \
289
+ assert(err == 0); \
290
+ }
291
+
292
+ V_SET_INT(result, "width", cinfo.image_width);
293
+ V_SET_INT(result, "height", cinfo.image_height);
294
+ V_SET_INT(result, "components", cinfo.num_components);
295
+ V_SET_INT(result, "precision", cinfo.data_precision);
296
+ V_SET_INT(result, "colorSpace", cinfo.jpeg_color_space);
297
+ V_SET_INT(result, "outputColorSpace", cinfo.out_color_space);
298
+ V_SET_BOOL(result, "progressive", cinfo.progressive_mode);
299
+ V_SET_BOOL(result, "arithmetic", cinfo.arith_code);
300
+ V_SET_INT(result, "restartInterval", cinfo.restart_interval);
301
+ V_SET_INT(result, "maxHSampFactor", cinfo.max_h_samp_factor);
302
+ V_SET_INT(result, "maxVSampFactor", cinfo.max_v_samp_factor);
303
+ V_SET_BOOL(result, "ccir601Sampling", cinfo.CCIR601_sampling);
304
+
305
+ js_value_t *component_info;
306
+ err = js_create_array_with_length(env, cinfo.num_components, &component_info);
307
+ assert(err == 0);
308
+
309
+ for (int c = 0; c < cinfo.num_components; c++) {
310
+ jpeg_component_info *ci = &cinfo.comp_info[c];
311
+
312
+ js_value_t *entry;
313
+ err = js_create_object(env, &entry);
314
+ assert(err == 0);
315
+
316
+ V_SET_INT(entry, "id", ci->component_id);
317
+ V_SET_INT(entry, "hSampFactor", ci->h_samp_factor);
318
+ V_SET_INT(entry, "vSampFactor", ci->v_samp_factor);
319
+ V_SET_INT(entry, "quantTblNo", ci->quant_tbl_no);
320
+
321
+ err = js_set_element(env, component_info, c, entry);
322
+ assert(err == 0);
323
+ }
324
+
325
+ err = js_set_named_property(env, result, "componentInfo", component_info);
326
+ assert(err == 0);
327
+
328
+ int quant_tables = 0;
329
+ for (int i = 0; i < NUM_QUANT_TBLS; i++) {
330
+ if (cinfo.quant_tbl_ptrs[i] != NULL) quant_tables++;
331
+ }
332
+
333
+ int dc_huff = 0, ac_huff = 0;
334
+ for (int i = 0; i < NUM_HUFF_TBLS; i++) {
335
+ if (cinfo.dc_huff_tbl_ptrs[i] != NULL) dc_huff++;
336
+ if (cinfo.ac_huff_tbl_ptrs[i] != NULL) ac_huff++;
337
+ }
338
+
339
+ V_SET_INT(result, "quantTables", quant_tables);
340
+ V_SET_INT(result, "dcHuffmanTables", dc_huff);
341
+ V_SET_INT(result, "acHuffmanTables", ac_huff);
342
+
343
+ if (cinfo.saw_JFIF_marker) {
344
+ js_value_t *jfif;
345
+ err = js_create_object(env, &jfif);
346
+ assert(err == 0);
347
+
348
+ V_SET_INT(jfif, "majorVersion", cinfo.JFIF_major_version);
349
+ V_SET_INT(jfif, "minorVersion", cinfo.JFIF_minor_version);
350
+ V_SET_INT(jfif, "densityUnit", cinfo.density_unit);
351
+ V_SET_INT(jfif, "xDensity", cinfo.X_density);
352
+ V_SET_INT(jfif, "yDensity", cinfo.Y_density);
353
+
354
+ err = js_set_named_property(env, result, "jfif", jfif);
355
+ assert(err == 0);
356
+ } else {
357
+ js_value_t *null_val;
358
+ err = js_get_null(env, &null_val);
359
+ assert(err == 0);
360
+ err = js_set_named_property(env, result, "jfif", null_val);
361
+ assert(err == 0);
362
+ }
363
+
364
+ if (cinfo.saw_Adobe_marker) {
365
+ js_value_t *adobe;
366
+ err = js_create_object(env, &adobe);
367
+ assert(err == 0);
368
+
369
+ V_SET_INT(adobe, "transform", cinfo.Adobe_transform);
370
+
371
+ err = js_set_named_property(env, result, "adobe", adobe);
372
+ assert(err == 0);
373
+ } else {
374
+ js_value_t *null_val;
375
+ err = js_get_null(env, &null_val);
376
+ assert(err == 0);
377
+ err = js_set_named_property(env, result, "adobe", null_val);
378
+ assert(err == 0);
379
+ }
380
+
381
+ uint32_t count = 0;
382
+ for (jpeg_saved_marker_ptr m = cinfo.marker_list; m != NULL; m = m->next) {
383
+ count++;
384
+ }
385
+
386
+ js_value_t *markers;
387
+ err = js_create_array_with_length(env, count, &markers);
388
+ assert(err == 0);
389
+
390
+ uint32_t i = 0;
391
+ for (jpeg_saved_marker_ptr m = cinfo.marker_list; m != NULL; m = m->next) {
392
+ js_value_t *entry;
393
+ err = js_create_object(env, &entry);
394
+ assert(err == 0);
395
+
396
+ V_SET_INT(entry, "marker", m->marker);
397
+
398
+ uint8_t *data = malloc(m->data_length == 0 ? 1 : m->data_length);
399
+ assert(data != NULL);
400
+
401
+ memcpy(data, m->data, m->data_length);
402
+
403
+ js_value_t *buffer;
404
+ err = js_create_external_arraybuffer(env, data, m->data_length, bare_jpeg__on_finalize, NULL, &buffer);
405
+ assert(err == 0);
406
+
407
+ err = js_set_named_property(env, entry, "data", buffer);
408
+ assert(err == 0);
409
+
410
+ err = js_set_element(env, markers, i++, entry);
411
+ assert(err == 0);
412
+ }
413
+
414
+ err = js_set_named_property(env, result, "markers", markers);
415
+ assert(err == 0);
416
+
417
+ #undef V_SET_BOOL
418
+ #undef V_SET_INT
419
+
420
+ jpeg_destroy_decompress(&cinfo);
421
+
422
+ return result;
423
+ }
424
+
222
425
  static js_value_t *
223
426
  bare_jpeg_exports(js_env_t *env, js_value_t *exports) {
224
427
  int err;
@@ -234,6 +437,7 @@ bare_jpeg_exports(js_env_t *env, js_value_t *exports) {
234
437
 
235
438
  V("decode", bare_jpeg_decode)
236
439
  V("encode", bare_jpeg_encode)
440
+ V("readHeader", bare_jpeg_read_header)
237
441
  #undef V
238
442
 
239
443
  return exports;
package/index.js CHANGED
@@ -1,5 +1,13 @@
1
1
  const binding = require('./binding')
2
2
 
3
+ const MARKER_PREFIX = 0xff
4
+ const SOI = 0xd8
5
+ const EOI = 0xd9
6
+ const SOS = 0xda
7
+ const APP0 = 0xe0
8
+ const APP15 = 0xef
9
+ const COM = 0xfe
10
+
3
11
  exports.decode = function decode(image) {
4
12
  const { width, height, data } = binding.decode(image)
5
13
 
@@ -13,16 +21,82 @@ exports.decode = function decode(image) {
13
21
  exports.encode = function encode(image, opts = {}) {
14
22
  const { quality = 90 } = opts
15
23
 
16
- const buffer = binding.encode(
17
- image.data,
18
- image.width,
19
- image.height,
20
- clamp(quality, 0, 100)
21
- )
24
+ const buffer = binding.encode(image.data, image.width, image.height, clamp(quality, 0, 100))
22
25
 
23
26
  return Buffer.from(buffer)
24
27
  }
25
28
 
29
+ exports.readHeader = function readHeader(image) {
30
+ const header = binding.readHeader(image)
31
+
32
+ return {
33
+ ...header,
34
+ markers: header.markers.map(({ marker, data }) => ({
35
+ marker,
36
+ data: Buffer.from(data)
37
+ }))
38
+ }
39
+ }
40
+
41
+ exports.replaceMarkers = function replaceMarkers(image, markers = []) {
42
+ if (image.length < 2 || image[0] !== MARKER_PREFIX || image[1] !== SOI) {
43
+ throw new Error('Invalid JPEG')
44
+ }
45
+
46
+ const output = [image.subarray(0, 2)]
47
+ let offset = 2
48
+
49
+ while (offset < image.length) {
50
+ const start = offset
51
+
52
+ while (offset < image.length && image[offset] === MARKER_PREFIX) {
53
+ offset++
54
+ }
55
+
56
+ if (offset >= image.length) {
57
+ throw new Error('Invalid JPEG')
58
+ }
59
+
60
+ const marker = image[offset]
61
+
62
+ if (marker === SOS || marker === EOI) {
63
+ for (const marker of markers) {
64
+ const payload = Buffer.from(marker.data)
65
+ const segment = Buffer.allocUnsafe(payload.length + 4)
66
+
67
+ segment[0] = MARKER_PREFIX
68
+ segment[1] = marker.marker
69
+ segment.writeUInt16BE(payload.length + 2, 2)
70
+ payload.copy(segment, 4)
71
+
72
+ output.push(segment)
73
+ }
74
+
75
+ output.push(image.subarray(start))
76
+ return Buffer.concat(output)
77
+ }
78
+
79
+ if (offset + 2 >= image.length) {
80
+ throw new Error('Invalid JPEG')
81
+ }
82
+
83
+ const length = image.readUInt16BE(offset + 1)
84
+ const end = offset + 1 + length
85
+
86
+ if (length < 2 || end > image.length) {
87
+ throw new Error('Invalid JPEG')
88
+ }
89
+
90
+ if ((marker < APP0 || marker > APP15) && marker !== COM) {
91
+ output.push(image.subarray(start, end))
92
+ }
93
+
94
+ offset = end
95
+ }
96
+
97
+ throw new Error('Invalid JPEG')
98
+ }
99
+
26
100
  function clamp(value, min, max) {
27
101
  return Math.max(min, Math.min(max, value))
28
102
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-jpeg",
3
- "version": "1.0.2",
3
+ "version": "1.1.0",
4
4
  "description": "JPEG support for Bare",
5
5
  "exports": {
6
6
  "./package": "./package.json",
@@ -15,7 +15,9 @@
15
15
  ],
16
16
  "addon": true,
17
17
  "scripts": {
18
- "test": "prettier . --check && bare test.js"
18
+ "format": "prettier --write . && lunte --fix",
19
+ "lint": "prettier --check . && lunte",
20
+ "test": "brittle-bare --coverage test.js"
19
21
  },
20
22
  "repository": {
21
23
  "type": "git",
@@ -30,8 +32,9 @@
30
32
  "devDependencies": {
31
33
  "brittle": "^3.4.0",
32
34
  "cmake-bare": "^1.1.2",
33
- "cmake-ports": "^1.4.2",
35
+ "cmake-ports": "^1.5.9",
36
+ "lunte": "^1.8.0",
34
37
  "prettier": "^3.3.3",
35
- "prettier-config-standard": "^7.0.0"
38
+ "prettier-config-holepunch": "^2.0.0"
36
39
  }
37
40
  }
Binary file
Binary file
Binary file
Binary file