load-image-meta.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * JavaScript Load Image Meta
  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. * https://opensource.org/licenses/MIT
  14. */
  15. /* global define, Blob */
  16. ;(function (factory) {
  17. 'use strict'
  18. if (typeof define === 'function' && define.amd) {
  19. // Register as an anonymous AMD module:
  20. define(['./load-image'], factory)
  21. } else if (typeof module === 'object' && module.exports) {
  22. factory(require('./load-image'))
  23. } else {
  24. // Browser globals:
  25. factory(window.loadImage)
  26. }
  27. })(function (loadImage) {
  28. 'use strict'
  29. var hasblobSlice =
  30. typeof Blob !== 'undefined' &&
  31. (Blob.prototype.slice ||
  32. Blob.prototype.webkitSlice ||
  33. Blob.prototype.mozSlice)
  34. loadImage.blobSlice =
  35. hasblobSlice &&
  36. function () {
  37. var slice = this.slice || this.webkitSlice || this.mozSlice
  38. return slice.apply(this, arguments)
  39. }
  40. loadImage.metaDataParsers = {
  41. jpeg: {
  42. 0xffe1: [] // APP1 marker
  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, data) {
  52. options = options || {}
  53. data = data || {}
  54. var that = this
  55. // 256 KiB should contain all EXIF/ICC/IPTC segments:
  56. var maxMetaDataSize = options.maxMetaDataSize || 262144
  57. var noMetaData = !(
  58. typeof DataView !== 'undefined' &&
  59. file &&
  60. file.size >= 12 &&
  61. file.type === 'image/jpeg' &&
  62. loadImage.blobSlice
  63. )
  64. if (
  65. noMetaData ||
  66. !loadImage.readFile(
  67. loadImage.blobSlice.call(file, 0, maxMetaDataSize),
  68. function (e) {
  69. if (e.target.error) {
  70. // FileReader error
  71. console.log(e.target.error)
  72. callback(data)
  73. return
  74. }
  75. // Note on endianness:
  76. // Since the marker and length bytes in JPEG files are always
  77. // stored in big endian order, we can leave the endian parameter
  78. // of the DataView methods undefined, defaulting to big endian.
  79. var buffer = e.target.result
  80. var dataView = new DataView(buffer)
  81. var offset = 2
  82. var maxOffset = dataView.byteLength - 4
  83. var headLength = offset
  84. var markerBytes
  85. var markerLength
  86. var parsers
  87. var i
  88. // Check for the JPEG marker (0xffd8):
  89. if (dataView.getUint16(0) === 0xffd8) {
  90. while (offset < maxOffset) {
  91. markerBytes = dataView.getUint16(offset)
  92. // Search for APPn (0xffeN) and COM (0xfffe) markers,
  93. // which contain application-specific meta-data like
  94. // Exif, ICC and IPTC data and text comments:
  95. if (
  96. (markerBytes >= 0xffe0 && markerBytes <= 0xffef) ||
  97. markerBytes === 0xfffe
  98. ) {
  99. // The marker bytes (2) are always followed by
  100. // the length bytes (2), indicating the length of the
  101. // marker segment, which includes the length bytes,
  102. // but not the marker bytes, so we add 2:
  103. markerLength = dataView.getUint16(offset + 2) + 2
  104. if (offset + markerLength > dataView.byteLength) {
  105. console.log('Invalid meta data: Invalid segment size.')
  106. break
  107. }
  108. parsers = loadImage.metaDataParsers.jpeg[markerBytes]
  109. if (parsers) {
  110. for (i = 0; i < parsers.length; i += 1) {
  111. parsers[i].call(
  112. that,
  113. dataView,
  114. offset,
  115. markerLength,
  116. data,
  117. options
  118. )
  119. }
  120. }
  121. offset += markerLength
  122. headLength = offset
  123. } else {
  124. // Not an APPn or COM marker, probably safe to
  125. // assume that this is the end of the meta data
  126. break
  127. }
  128. }
  129. // Meta length must be longer than JPEG marker (2)
  130. // plus APPn marker (2), followed by length bytes (2):
  131. if (!options.disableImageHead && headLength > 6) {
  132. if (buffer.slice) {
  133. data.imageHead = buffer.slice(0, headLength)
  134. } else {
  135. // Workaround for IE10, which does not yet
  136. // support ArrayBuffer.slice:
  137. data.imageHead = new Uint8Array(buffer).subarray(0, headLength)
  138. }
  139. }
  140. } else {
  141. console.log('Invalid JPEG file: Missing JPEG marker.')
  142. }
  143. callback(data)
  144. },
  145. 'readAsArrayBuffer'
  146. )
  147. ) {
  148. callback(data)
  149. }
  150. }
  151. // Determines if meta data should be loaded automatically:
  152. loadImage.hasMetaOption = function (options) {
  153. return options && options.meta
  154. }
  155. var originalTransform = loadImage.transform
  156. loadImage.transform = function (img, options, callback, file, data) {
  157. if (loadImage.hasMetaOption(options)) {
  158. loadImage.parseMetaData(
  159. file,
  160. function (data) {
  161. originalTransform.call(loadImage, img, options, callback, file, data)
  162. },
  163. options,
  164. data
  165. )
  166. } else {
  167. originalTransform.apply(loadImage, arguments)
  168. }
  169. }
  170. })