bare-jpeg 1.1.0 → 1.1.1

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/index.js CHANGED
@@ -45,6 +45,8 @@ exports.replaceMarkers = function replaceMarkers(image, markers = []) {
45
45
 
46
46
  const output = [image.subarray(0, 2)]
47
47
  let offset = 2
48
+ let next = 0
49
+ let insertAt = 1
48
50
 
49
51
  while (offset < image.length) {
50
52
  const start = offset
@@ -60,18 +62,13 @@ exports.replaceMarkers = function replaceMarkers(image, markers = []) {
60
62
  const marker = image[offset]
61
63
 
62
64
  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)
65
+ const leftover = []
66
+ while (next < markers.length) {
67
+ leftover.push(buildSegment(markers[next++]))
68
+ }
69
+ if (leftover.length > 0) {
70
+ output.splice(insertAt, 0, ...leftover)
73
71
  }
74
-
75
72
  output.push(image.subarray(start))
76
73
  return Buffer.concat(output)
77
74
  }
@@ -87,7 +84,12 @@ exports.replaceMarkers = function replaceMarkers(image, markers = []) {
87
84
  throw new Error('Invalid JPEG')
88
85
  }
89
86
 
90
- if ((marker < APP0 || marker > APP15) && marker !== COM) {
87
+ if ((marker >= APP0 && marker <= APP15) || marker === COM) {
88
+ if (next < markers.length) {
89
+ output.push(buildSegment(markers[next++]))
90
+ insertAt = output.length
91
+ }
92
+ } else {
91
93
  output.push(image.subarray(start, end))
92
94
  }
93
95
 
@@ -97,6 +99,18 @@ exports.replaceMarkers = function replaceMarkers(image, markers = []) {
97
99
  throw new Error('Invalid JPEG')
98
100
  }
99
101
 
102
+ function buildSegment(marker) {
103
+ const payload = Buffer.from(marker.data)
104
+ const segment = Buffer.allocUnsafe(payload.length + 4)
105
+
106
+ segment[0] = MARKER_PREFIX
107
+ segment[1] = marker.marker
108
+ segment.writeUInt16BE(payload.length + 2, 2)
109
+ payload.copy(segment, 4)
110
+
111
+ return segment
112
+ }
113
+
100
114
  function clamp(value, min, max) {
101
115
  return Math.max(min, Math.min(max, value))
102
116
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-jpeg",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "JPEG support for Bare",
5
5
  "exports": {
6
6
  "./package": "./package.json",
Binary file
Binary file
Binary file
Binary file