|
| 1 | +/* |
| 2 | + * JavaScript Load Image Meta 1.0.1 |
| 3 | + * https://github.com/blueimp/JavaScript-Load-Image |
| 4 | + * |
| 5 | + * Copyright 2013, Sebastian Tschan |
| 6 | + * https://blueimp.net |
| 7 | + * |
| 8 | + * Image meta data handling implementation |
| 9 | + * based on the help and contribution of |
| 10 | + * Achim Stöhr. |
| 11 | + * |
| 12 | + * Licensed under the MIT license: |
| 13 | + * http://www.opensource.org/licenses/MIT |
| 14 | + */ |
| 15 | + |
| 16 | +/*jslint continue:true */ |
| 17 | +/*global define, window, DataView, Blob, Uint8Array, console */ |
| 18 | + |
| 19 | +(function (factory) { |
| 20 | + 'use strict'; |
| 21 | + if (typeof define === 'function' && define.amd) { |
| 22 | + // Register as an anonymous AMD module: |
| 23 | + define(['load-image'], factory); |
| 24 | + } else { |
| 25 | + // Browser globals: |
| 26 | + factory(window.loadImage); |
| 27 | + } |
| 28 | +}(function (loadImage) { |
| 29 | + 'use strict'; |
| 30 | + |
| 31 | + var hasblobSlice = window.Blob && (Blob.prototype.slice || |
| 32 | + Blob.prototype.webkitSlice || Blob.prototype.mozSlice); |
| 33 | + |
| 34 | + loadImage.blobSlice = hasblobSlice && function () { |
| 35 | + var slice = this.slice || this.webkitSlice || this.mozSlice; |
| 36 | + return slice.apply(this, arguments); |
| 37 | + }; |
| 38 | + |
| 39 | + loadImage.metaDataParsers = { |
| 40 | + jpeg: { |
| 41 | + 0xffe1: [] // APP1 marker |
| 42 | + } |
| 43 | + }; |
| 44 | + |
| 45 | + // Parses image meta data and calls the callback with an object argument |
| 46 | + // with the following properties: |
| 47 | + // * imageHead: The complete image head as ArrayBuffer (Uint8Array for IE10) |
| 48 | + // The options arguments accepts an object and supports the following properties: |
| 49 | + // * maxMetaDataSize: Defines the maximum number of bytes to parse. |
| 50 | + // * disableImageHead: Disables creating the imageHead property. |
| 51 | + loadImage.parseMetaData = function (file, callback, options) { |
| 52 | + options = options || {}; |
| 53 | + var that = this, |
| 54 | + // 256 KiB should contain all EXIF/ICC/IPTC segments: |
| 55 | + maxMetaDataSize = options.maxMetaDataSize || 262144, |
| 56 | + data = {}, |
| 57 | + noMetaData = !(window.DataView && file && file.size >= 12 && |
| 58 | + file.type === 'image/jpeg' && loadImage.blobSlice); |
| 59 | + if (noMetaData || !loadImage.readFile( |
| 60 | + loadImage.blobSlice.call(file, 0, maxMetaDataSize), |
| 61 | + function (e) { |
| 62 | + // Note on endianness: |
| 63 | + // Since the marker and length bytes in JPEG files are always |
| 64 | + // stored in big endian order, we can leave the endian parameter |
| 65 | + // of the DataView methods undefined, defaulting to big endian. |
| 66 | + var buffer = e.target.result, |
| 67 | + dataView = new DataView(buffer), |
| 68 | + offset = 2, |
| 69 | + maxOffset = dataView.byteLength - 4, |
| 70 | + headLength = offset, |
| 71 | + markerBytes, |
| 72 | + markerLength, |
| 73 | + parsers, |
| 74 | + i; |
| 75 | + // Check for the JPEG marker (0xffd8): |
| 76 | + if (dataView.getUint16(0) === 0xffd8) { |
| 77 | + while (offset < maxOffset) { |
| 78 | + markerBytes = dataView.getUint16(offset); |
| 79 | + // Search for APPn (0xffeN) and COM (0xfffe) markers, |
| 80 | + // which contain application-specific meta-data like |
| 81 | + // Exif, ICC and IPTC data and text comments: |
| 82 | + if ((markerBytes >= 0xffe0 && markerBytes <= 0xffef) || |
| 83 | + markerBytes === 0xfffe) { |
| 84 | + // The marker bytes (2) are always followed by |
| 85 | + // the length bytes (2), indicating the length of the |
| 86 | + // marker segment, which includes the length bytes, |
| 87 | + // but not the marker bytes, so we add 2: |
| 88 | + markerLength = dataView.getUint16(offset + 2) + 2; |
| 89 | + if (offset + markerLength > dataView.byteLength) { |
| 90 | + console.log('Invalid meta data: Invalid segment size.'); |
| 91 | + break; |
| 92 | + } |
| 93 | + parsers = loadImage.metaDataParsers.jpeg[markerBytes]; |
| 94 | + if (parsers) { |
| 95 | + for (i = 0; i < parsers.length; i += 1) { |
| 96 | + parsers[i].call( |
| 97 | + that, |
| 98 | + dataView, |
| 99 | + offset, |
| 100 | + markerLength, |
| 101 | + data, |
| 102 | + options |
| 103 | + ); |
| 104 | + } |
| 105 | + } |
| 106 | + offset += markerLength; |
| 107 | + headLength = offset; |
| 108 | + } else { |
| 109 | + // Not an APPn or COM marker, probably safe to |
| 110 | + // assume that this is the end of the meta data |
| 111 | + break; |
| 112 | + } |
| 113 | + } |
| 114 | + // Meta length must be longer than JPEG marker (2) |
| 115 | + // plus APPn marker (2), followed by length bytes (2): |
| 116 | + if (!options.disableImageHead && headLength > 6) { |
| 117 | + if (buffer.slice) { |
| 118 | + data.imageHead = buffer.slice(0, headLength); |
| 119 | + } else { |
| 120 | + // Workaround for IE10, which does not yet |
| 121 | + // support ArrayBuffer.slice: |
| 122 | + data.imageHead = new Uint8Array(buffer) |
| 123 | + .subarray(0, headLength); |
| 124 | + } |
| 125 | + } |
| 126 | + } else { |
| 127 | + console.log('Invalid JPEG file: Missing JPEG marker.'); |
| 128 | + } |
| 129 | + callback(data); |
| 130 | + }, |
| 131 | + 'readAsArrayBuffer' |
| 132 | + )) { |
| 133 | + callback(data); |
| 134 | + } |
| 135 | + }; |
| 136 | + |
| 137 | +})); |
0 commit comments