load-image-meta.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 = typeof Blob !== 'undefined' && (Blob.prototype.slice ||
  30. Blob.prototype.webkitSlice || Blob.prototype.mozSlice)
  31. loadImage.blobSlice = hasblobSlice && function () {
  32. var slice = this.slice || this.webkitSlice || this.mozSlice
  33. return slice.apply(this, arguments)
  34. }
  35. loadImage.metaDataParsers = {
  36. jpeg: {
  37. 0xffe1: [] // APP1 marker
  38. }
  39. }
  40. // Parses image meta data and calls the callback with an object argument
  41. // with the following properties:
  42. // * imageHead: The complete image head as ArrayBuffer (Uint8Array for IE10)
  43. // The options arguments accepts an object and supports the following properties:
  44. // * maxMetaDataSize: Defines the maximum number of bytes to parse.
  45. // * disableImageHead: Disables creating the imageHead property.
  46. loadImage.parseMetaData = function (file, callback, options, data) {
  47. options = options || {}
  48. data = data || {}
  49. var that = this
  50. // 256 KiB should contain all EXIF/ICC/IPTC segments:
  51. var maxMetaDataSize = options.maxMetaDataSize || 262144
  52. var noMetaData = !(typeof DataView !== 'undefined' && file && file.size >= 12 &&
  53. file.type === 'image/jpeg' && loadImage.blobSlice)
  54. if (noMetaData || !loadImage.readFile(
  55. loadImage.blobSlice.call(file, 0, maxMetaDataSize),
  56. function (e) {
  57. if (e.target.error) {
  58. // FileReader error
  59. console.log(e.target.error)
  60. callback(data)
  61. return
  62. }
  63. // Note on endianness:
  64. // Since the marker and length bytes in JPEG files are always
  65. // stored in big endian order, we can leave the endian parameter
  66. // of the DataView methods undefined, defaulting to big endian.
  67. var buffer = e.target.result
  68. var dataView = new DataView(buffer)
  69. var offset = 2
  70. var maxOffset = dataView.byteLength - 4
  71. var headLength = offset
  72. var markerBytes
  73. var markerLength
  74. var parsers
  75. var i
  76. // Check for the JPEG marker (0xffd8):
  77. if (dataView.getUint16(0) === 0xffd8) {
  78. while (offset < maxOffset) {
  79. markerBytes = dataView.getUint16(offset)
  80. // Search for APPn (0xffeN) and COM (0xfffe) markers,
  81. // which contain application-specific meta-data like
  82. // Exif, ICC and IPTC data and text comments:
  83. if ((markerBytes >= 0xffe0 && markerBytes <= 0xffef) ||
  84. markerBytes === 0xfffe) {
  85. // The marker bytes (2) are always followed by
  86. // the length bytes (2), indicating the length of the
  87. // marker segment, which includes the length bytes,
  88. // but not the marker bytes, so we add 2:
  89. markerLength = dataView.getUint16(offset + 2) + 2
  90. if (offset + markerLength > dataView.byteLength) {
  91. console.log('Invalid meta data: Invalid segment size.')
  92. break
  93. }
  94. parsers = loadImage.metaDataParsers.jpeg[markerBytes]
  95. if (parsers) {
  96. for (i = 0; i < parsers.length; i += 1) {
  97. parsers[i].call(
  98. that,
  99. dataView,
  100. offset,
  101. markerLength,
  102. data,
  103. options
  104. )
  105. }
  106. }
  107. offset += markerLength
  108. headLength = offset
  109. } else {
  110. // Not an APPn or COM marker, probably safe to
  111. // assume that this is the end of the meta data
  112. break
  113. }
  114. }
  115. // Meta length must be longer than JPEG marker (2)
  116. // plus APPn marker (2), followed by length bytes (2):
  117. if (!options.disableImageHead && headLength > 6) {
  118. if (buffer.slice) {
  119. data.imageHead = buffer.slice(0, headLength)
  120. } else {
  121. // Workaround for IE10, which does not yet
  122. // support ArrayBuffer.slice:
  123. data.imageHead = new Uint8Array(buffer)
  124. .subarray(0, headLength)
  125. }
  126. }
  127. } else {
  128. console.log('Invalid JPEG file: Missing JPEG marker.')
  129. }
  130. callback(data)
  131. },
  132. 'readAsArrayBuffer'
  133. )) {
  134. callback(data)
  135. }
  136. }
  137. // Determines if meta data should be loaded automatically:
  138. loadImage.hasMetaOption = function (options) {
  139. return options && options.meta
  140. }
  141. var originalTransform = loadImage.transform
  142. loadImage.transform = function (img, options, callback, file, data) {
  143. if (loadImage.hasMetaOption(options)) {
  144. loadImage.parseMetaData(file, function (data) {
  145. originalTransform.call(loadImage, img, options, callback, file, data)
  146. }, options, data)
  147. } else {
  148. originalTransform.apply(loadImage, arguments)
  149. }
  150. }
  151. }))