|
29 | 29 | })(function (loadImage) { |
30 | 30 | 'use strict' |
31 | 31 |
|
32 | | - loadImage.blobSlice = |
33 | | - loadImage.global.Blob && |
| 32 | + var global = loadImage.global |
| 33 | + var originalTransform = loadImage.transform |
| 34 | + |
| 35 | + var blobSlice = |
| 36 | + global.Blob && |
34 | 37 | (Blob.prototype.slice || |
35 | 38 | Blob.prototype.webkitSlice || |
36 | 39 | Blob.prototype.mozSlice) |
37 | 40 |
|
38 | | - loadImage.bufferSlice = |
39 | | - (loadImage.global.ArrayBuffer && |
40 | | - loadImage.global.ArrayBuffer.prototype.slice) || |
| 41 | + var bufferSlice = |
| 42 | + (global.ArrayBuffer && ArrayBuffer.prototype.slice) || |
41 | 43 | function (begin, end) { |
42 | 44 | // Polyfill for IE10, which does not support ArrayBuffer.slice |
43 | 45 | // eslint-disable-next-line no-param-reassign |
|
48 | 50 | return arr2.buffer |
49 | 51 | } |
50 | 52 |
|
51 | | - loadImage.metaDataParsers = { |
| 53 | + var metaDataParsers = { |
52 | 54 | jpeg: { |
53 | 55 | 0xffe1: [], // APP1 marker |
54 | 56 | 0xffed: [] // APP13 marker |
|
82 | 84 | function executor(resolve, reject) { |
83 | 85 | if ( |
84 | 86 | !( |
85 | | - loadImage.global.DataView && |
86 | | - loadImage.blobSlice && |
| 87 | + global.DataView && |
| 88 | + blobSlice && |
87 | 89 | file && |
88 | 90 | file.size >= 12 && |
89 | 91 | file.type === 'image/jpeg' |
|
96 | 98 | var maxMetaDataSize = options.maxMetaDataSize || 262144 |
97 | 99 | if ( |
98 | 100 | !loadImage.readFile( |
99 | | - loadImage.blobSlice.call(file, 0, maxMetaDataSize), |
| 101 | + blobSlice.call(file, 0, maxMetaDataSize), |
100 | 102 | function (buffer) { |
101 | 103 | // Note on endianness: |
102 | 104 | // Since the marker and length bytes in JPEG files are always |
|
135 | 137 | console.log('Invalid JPEG metadata: Invalid segment size.') |
136 | 138 | break |
137 | 139 | } |
138 | | - parsers = loadImage.metaDataParsers.jpeg[markerBytes] |
| 140 | + parsers = metaDataParsers.jpeg[markerBytes] |
139 | 141 | if (parsers && !options.disableMetaDataParsers) { |
140 | 142 | for (i = 0; i < parsers.length; i += 1) { |
141 | 143 | parsers[i].call( |
|
159 | 161 | // Meta length must be longer than JPEG marker (2) |
160 | 162 | // plus APPn marker (2), followed by length bytes (2): |
161 | 163 | if (!options.disableImageHead && headLength > 6) { |
162 | | - data.imageHead = loadImage.bufferSlice.call(buffer, 0, headLength) |
| 164 | + data.imageHead = bufferSlice.call(buffer, 0, headLength) |
163 | 165 | } |
164 | 166 | resolve(data) |
165 | 167 | }, |
|
172 | 174 | } |
173 | 175 | } |
174 | 176 | options = options || {} // eslint-disable-line no-param-reassign |
175 | | - if (loadImage.global.Promise && typeof callback !== 'function') { |
| 177 | + if (global.Promise && typeof callback !== 'function') { |
176 | 178 | options = callback || {} // eslint-disable-line no-param-reassign |
177 | 179 | data = options // eslint-disable-line no-param-reassign |
178 | 180 | return new Promise(executor) |
|
184 | 186 | /** |
185 | 187 | * Replaces the head of a JPEG Blob |
186 | 188 | * |
187 | | - * @param {Blob} blob Resolution function |
| 189 | + * @param {Blob} blob Blob object |
188 | 190 | * @param {ArrayBuffer} oldHead Old JPEG head |
189 | 191 | * @param {ArrayBuffer} newHead New JPEG head |
190 | 192 | * @returns {Blob} Combined Blob |
191 | 193 | */ |
192 | 194 | function replaceJPEGHead(blob, oldHead, newHead) { |
193 | | - return new Blob( |
194 | | - [newHead, loadImage.blobSlice.call(blob, oldHead.byteLength)], |
195 | | - { type: 'image/jpeg' } |
196 | | - ) |
| 195 | + return new Blob([newHead, blobSlice.call(blob, oldHead.byteLength)], { |
| 196 | + type: 'image/jpeg' |
| 197 | + }) |
197 | 198 | } |
198 | 199 |
|
199 | | - // Replaces the image head of a JPEG blob with the given one. |
200 | | - // Returns a Promise or calls the callback with the new Blob: |
201 | | - loadImage.replaceHead = function (blob, head, callback) { |
| 200 | + /** |
| 201 | + * Replaces the image head of a JPEG blob with the given one. |
| 202 | + * Returns a Promise or calls the callback with the new Blob. |
| 203 | + * |
| 204 | + * @param {Blob} blob Blob object |
| 205 | + * @param {ArrayBuffer} head New JPEG head |
| 206 | + * @param {Function} [callback] Callback function |
| 207 | + * @returns {Promise|undefined} Combined Blob |
| 208 | + */ |
| 209 | + function replaceHead(blob, head, callback) { |
202 | 210 | var options = { maxMetaDataSize: 256, disableMetaDataParsers: true } |
203 | | - if (!callback && loadImage.global.Promise) { |
| 211 | + if (!callback && global.Promise) { |
204 | 212 | return parseMetaData(blob, options).then(function (data) { |
205 | 213 | return replaceJPEGHead(blob, data.imageHead, head) |
206 | 214 | }) |
|
214 | 222 | ) |
215 | 223 | } |
216 | 224 |
|
217 | | - var originalTransform = loadImage.transform |
218 | 225 | loadImage.transform = function (img, options, callback, file, data) { |
219 | 226 | if (loadImage.requiresMetaData(options)) { |
220 | 227 | data = data || {} // eslint-disable-line no-param-reassign |
|
223 | 230 | function (result) { |
224 | 231 | if (result !== data) { |
225 | 232 | // eslint-disable-next-line no-console |
226 | | - if (loadImage.global.console) console.log(result) |
| 233 | + if (global.console) console.log(result) |
227 | 234 | result = data // eslint-disable-line no-param-reassign |
228 | 235 | } |
229 | 236 | originalTransform.call( |
|
243 | 250 | } |
244 | 251 | } |
245 | 252 |
|
| 253 | + loadImage.blobSlice = blobSlice |
| 254 | + loadImage.bufferSlice = bufferSlice |
| 255 | + loadImage.replaceHead = replaceHead |
246 | 256 | loadImage.parseMetaData = parseMetaData |
| 257 | + loadImage.metaDataParsers = metaDataParsers |
247 | 258 | }) |
0 commit comments