|
58 | 58 | var noMetaData = !(typeof DataView !== 'undefined' && file && file.size >= 12 && |
59 | 59 | file.type === 'image/jpeg' && loadImage.blobSlice) |
60 | 60 | if (noMetaData || !loadImage.readFile( |
61 | | - loadImage.blobSlice.call(file, 0, maxMetaDataSize), |
62 | | - function (e) { |
63 | | - if (e.target.error) { |
64 | | - // FileReader error |
65 | | - console.log(e.target.error) |
66 | | - callback(data) |
67 | | - return |
68 | | - } |
69 | | - // Note on endianness: |
70 | | - // Since the marker and length bytes in JPEG files are always |
71 | | - // stored in big endian order, we can leave the endian parameter |
72 | | - // of the DataView methods undefined, defaulting to big endian. |
73 | | - var buffer = e.target.result |
74 | | - var dataView = new DataView(buffer) |
75 | | - var offset = 2 |
76 | | - var maxOffset = dataView.byteLength - 4 |
77 | | - var headLength = offset |
78 | | - var markerBytes |
79 | | - var markerLength |
80 | | - var parsers |
81 | | - var i |
82 | | - // Check for the JPEG marker (0xffd8): |
83 | | - if (dataView.getUint16(0) === 0xffd8) { |
84 | | - while (offset < maxOffset) { |
85 | | - markerBytes = dataView.getUint16(offset) |
86 | | - // Search for APPn (0xffeN) and COM (0xfffe) markers, |
87 | | - // which contain application-specific meta-data like |
88 | | - // Exif, ICC and IPTC data and text comments: |
89 | | - if ((markerBytes >= 0xffe0 && markerBytes <= 0xffef) || |
90 | | - markerBytes === 0xfffe) { |
91 | | - // The marker bytes (2) are always followed by |
92 | | - // the length bytes (2), indicating the length of the |
93 | | - // marker segment, which includes the length bytes, |
94 | | - // but not the marker bytes, so we add 2: |
95 | | - markerLength = dataView.getUint16(offset + 2) + 2 |
96 | | - if (offset + markerLength > dataView.byteLength) { |
97 | | - console.log('Invalid meta data: Invalid segment size.') |
98 | | - break |
99 | | - } |
100 | | - parsers = loadImage.metaDataParsers.jpeg[markerBytes] |
101 | | - if (parsers) { |
102 | | - for (i = 0; i < parsers.length; i += 1) { |
103 | | - parsers[i].call( |
104 | | - that, |
105 | | - dataView, |
106 | | - offset, |
107 | | - markerLength, |
108 | | - data, |
109 | | - options |
110 | | - ) |
111 | | - } |
112 | | - } |
113 | | - offset += markerLength |
114 | | - headLength = offset |
115 | | - } else { |
116 | | - // Not an APPn or COM marker, probably safe to |
117 | | - // assume that this is the end of the meta data |
| 61 | + loadImage.blobSlice.call(file, 0, maxMetaDataSize), |
| 62 | + function (e) { |
| 63 | + if (e.target.error) { |
| 64 | + // FileReader error |
| 65 | + console.log(e.target.error) |
| 66 | + callback(data) |
| 67 | + return |
| 68 | + } |
| 69 | + // Note on endianness: |
| 70 | + // Since the marker and length bytes in JPEG files are always |
| 71 | + // stored in big endian order, we can leave the endian parameter |
| 72 | + // of the DataView methods undefined, defaulting to big endian. |
| 73 | + var buffer = e.target.result |
| 74 | + var dataView = new DataView(buffer) |
| 75 | + var offset = 2 |
| 76 | + var maxOffset = dataView.byteLength - 4 |
| 77 | + var headLength = offset |
| 78 | + var markerBytes |
| 79 | + var markerLength |
| 80 | + var parsers |
| 81 | + var i |
| 82 | + // Check for the JPEG marker (0xffd8): |
| 83 | + if (dataView.getUint16(0) === 0xffd8) { |
| 84 | + while (offset < maxOffset) { |
| 85 | + markerBytes = dataView.getUint16(offset) |
| 86 | + // Search for APPn (0xffeN) and COM (0xfffe) markers, |
| 87 | + // which contain application-specific meta-data like |
| 88 | + // Exif, ICC and IPTC data and text comments: |
| 89 | + if ((markerBytes >= 0xffe0 && markerBytes <= 0xffef) || |
| 90 | + markerBytes === 0xfffe) { |
| 91 | + // The marker bytes (2) are always followed by |
| 92 | + // the length bytes (2), indicating the length of the |
| 93 | + // marker segment, which includes the length bytes, |
| 94 | + // but not the marker bytes, so we add 2: |
| 95 | + markerLength = dataView.getUint16(offset + 2) + 2 |
| 96 | + if (offset + markerLength > dataView.byteLength) { |
| 97 | + console.log('Invalid meta data: Invalid segment size.') |
118 | 98 | break |
119 | 99 | } |
120 | | - } |
121 | | - // Meta length must be longer than JPEG marker (2) |
122 | | - // plus APPn marker (2), followed by length bytes (2): |
123 | | - if (!options.disableImageHead && headLength > 6) { |
124 | | - if (buffer.slice) { |
125 | | - data.imageHead = buffer.slice(0, headLength) |
126 | | - } else { |
127 | | - // Workaround for IE10, which does not yet |
128 | | - // support ArrayBuffer.slice: |
129 | | - data.imageHead = new Uint8Array(buffer) |
130 | | - .subarray(0, headLength) |
| 100 | + parsers = loadImage.metaDataParsers.jpeg[markerBytes] |
| 101 | + if (parsers) { |
| 102 | + for (i = 0; i < parsers.length; i += 1) { |
| 103 | + parsers[i].call( |
| 104 | + that, |
| 105 | + dataView, |
| 106 | + offset, |
| 107 | + markerLength, |
| 108 | + data, |
| 109 | + options |
| 110 | + ) |
| 111 | + } |
131 | 112 | } |
| 113 | + offset += markerLength |
| 114 | + headLength = offset |
| 115 | + } else { |
| 116 | + // Not an APPn or COM marker, probably safe to |
| 117 | + // assume that this is the end of the meta data |
| 118 | + break |
132 | 119 | } |
133 | | - } else { |
134 | | - console.log('Invalid JPEG file: Missing JPEG marker.') |
135 | 120 | } |
136 | | - callback(data) |
137 | | - }, |
138 | | - 'readAsArrayBuffer' |
139 | | - )) { |
| 121 | + // Meta length must be longer than JPEG marker (2) |
| 122 | + // plus APPn marker (2), followed by length bytes (2): |
| 123 | + if (!options.disableImageHead && headLength > 6) { |
| 124 | + if (buffer.slice) { |
| 125 | + data.imageHead = buffer.slice(0, headLength) |
| 126 | + } else { |
| 127 | + // Workaround for IE10, which does not yet |
| 128 | + // support ArrayBuffer.slice: |
| 129 | + data.imageHead = new Uint8Array(buffer) |
| 130 | + .subarray(0, headLength) |
| 131 | + } |
| 132 | + } |
| 133 | + } else { |
| 134 | + console.log('Invalid JPEG file: Missing JPEG marker.') |
| 135 | + } |
| 136 | + callback(data) |
| 137 | + }, |
| 138 | + 'readAsArrayBuffer' |
| 139 | + )) { |
140 | 140 | callback(data) |
141 | 141 | } |
142 | 142 | } |
|
0 commit comments