bare-jpeg 1.1.0 → 1.1.2

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/binding.c CHANGED
@@ -73,9 +73,9 @@ bare_jpeg_decode(js_env_t *env, js_callback_info_t *info) {
73
73
 
74
74
  jpeg_start_decompress(&decoder);
75
75
 
76
- int width = decoder.output_width;
77
- int height = decoder.output_height;
78
- int channels = decoder.output_components;
76
+ JDIMENSION width = decoder.output_width;
77
+ JDIMENSION height = decoder.output_height;
78
+ JDIMENSION channels = decoder.output_components;
79
79
 
80
80
  js_value_t *result;
81
81
  err = js_create_object(env, &result);
@@ -94,7 +94,7 @@ bare_jpeg_decode(js_env_t *env, js_callback_info_t *info) {
94
94
  V(height);
95
95
  #undef V
96
96
 
97
- len = width * height * 4;
97
+ len = (size_t) width * height * 4;
98
98
 
99
99
  js_value_t *buffer;
100
100
 
@@ -107,15 +107,15 @@ bare_jpeg_decode(js_env_t *env, js_callback_info_t *info) {
107
107
 
108
108
  JSAMPARRAY scanlines = (*decoder.mem->alloc_sarray)((j_common_ptr) &decoder, JPOOL_IMAGE, width * channels, 1);
109
109
 
110
- int row = 0;
110
+ JDIMENSION row = 0;
111
111
 
112
112
  while (decoder.output_scanline < decoder.output_height) {
113
113
  jpeg_read_scanlines(&decoder, scanlines, 1);
114
114
 
115
115
  const uint8_t *src = scanlines[0];
116
- uint8_t *dst = data + row * width * 4;
116
+ uint8_t *dst = data + (size_t) row * width * 4;
117
117
 
118
- for (int x = 0; x < width; x++) {
118
+ for (JDIMENSION x = 0; x < width; x++) {
119
119
  dst[x * 4 + 0] = src[x * channels + 0];
120
120
  dst[x * 4 + 1] = src[x * channels + 1];
121
121
  dst[x * 4 + 2] = src[x * channels + 2];
@@ -159,6 +159,13 @@ bare_jpeg_encode(js_env_t *env, js_callback_info_t *info) {
159
159
  err = js_get_value_int64(env, argv[3], &quality);
160
160
  assert(err == 0);
161
161
 
162
+ if (width < 0 || width > JPEG_MAX_DIMENSION || height < 0 || height > JPEG_MAX_DIMENSION) {
163
+ err = js_throw_error(env, NULL, "Invalid JPEG dimensions");
164
+ assert(err == 0);
165
+
166
+ return NULL;
167
+ }
168
+
162
169
  bare_jpeg_error_t error;
163
170
 
164
171
  struct jpeg_compress_struct encoder;
@@ -194,12 +201,12 @@ bare_jpeg_encode(js_env_t *env, js_callback_info_t *info) {
194
201
 
195
202
  jpeg_start_compress(&encoder, true);
196
203
 
197
- uint8_t *dst = malloc(width * 3);
204
+ uint8_t *dst = malloc((size_t) width * 3);
198
205
 
199
206
  while (encoder.next_scanline < encoder.image_height) {
200
- const uint8_t *src = data + encoder.next_scanline * width * 4;
207
+ const uint8_t *src = data + (size_t) encoder.next_scanline * width * 4;
201
208
 
202
- for (int x = 0; x < width; x++) {
209
+ for (JDIMENSION x = 0; x < (JDIMENSION) width; x++) {
203
210
  dst[x * 3 + 0] = src[x * 4 + 0];
204
211
  dst[x * 3 + 1] = src[x * 4 + 1];
205
212
  dst[x * 3 + 2] = src[x * 4 + 2];
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.2",
4
4
  "description": "JPEG support for Bare",
5
5
  "exports": {
6
6
  "./package": "./package.json",
Binary file
Binary file
Binary file
Binary file