exif.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. (function() {
  2. var debug = false;
  3. var root = this;
  4. var EXIF = function(obj) {
  5. if (obj instanceof EXIF) return obj;
  6. if (!(this instanceof EXIF)) return new EXIF(obj);
  7. this.EXIFwrapped = obj;
  8. };
  9. if (typeof exports !== 'undefined') {
  10. if (typeof module !== 'undefined' && module.exports) {
  11. exports = module.exports = EXIF;
  12. }
  13. exports.EXIF = EXIF;
  14. } else {
  15. root.EXIF = EXIF;
  16. }
  17. var ExifTags = EXIF.Tags = {
  18. // version tags
  19. 0x9000 : "ExifVersion", // EXIF version
  20. 0xA000 : "FlashpixVersion", // Flashpix format version
  21. // colorspace tags
  22. 0xA001 : "ColorSpace", // Color space information tag
  23. // image configuration
  24. 0xA002 : "PixelXDimension", // Valid width of meaningful image
  25. 0xA003 : "PixelYDimension", // Valid height of meaningful image
  26. 0x9101 : "ComponentsConfiguration", // Information about channels
  27. 0x9102 : "CompressedBitsPerPixel", // Compressed bits per pixel
  28. // user information
  29. 0x927C : "MakerNote", // Any desired information written by the manufacturer
  30. 0x9286 : "UserComment", // Comments by user
  31. // related file
  32. 0xA004 : "RelatedSoundFile", // Name of related sound file
  33. // date and time
  34. 0x9003 : "DateTimeOriginal", // Date and time when the original image was generated
  35. 0x9004 : "DateTimeDigitized", // Date and time when the image was stored digitally
  36. 0x9290 : "SubsecTime", // Fractions of seconds for DateTime
  37. 0x9291 : "SubsecTimeOriginal", // Fractions of seconds for DateTimeOriginal
  38. 0x9292 : "SubsecTimeDigitized", // Fractions of seconds for DateTimeDigitized
  39. // picture-taking conditions
  40. 0x829A : "ExposureTime", // Exposure time (in seconds)
  41. 0x829D : "FNumber", // F number
  42. 0x8822 : "ExposureProgram", // Exposure program
  43. 0x8824 : "SpectralSensitivity", // Spectral sensitivity
  44. 0x8827 : "ISOSpeedRatings", // ISO speed rating
  45. 0x8828 : "OECF", // Optoelectric conversion factor
  46. 0x9201 : "ShutterSpeedValue", // Shutter speed
  47. 0x9202 : "ApertureValue", // Lens aperture
  48. 0x9203 : "BrightnessValue", // Value of brightness
  49. 0x9204 : "ExposureBias", // Exposure bias
  50. 0x9205 : "MaxApertureValue", // Smallest F number of lens
  51. 0x9206 : "SubjectDistance", // Distance to subject in meters
  52. 0x9207 : "MeteringMode", // Metering mode
  53. 0x9208 : "LightSource", // Kind of light source
  54. 0x9209 : "Flash", // Flash status
  55. 0x9214 : "SubjectArea", // Location and area of main subject
  56. 0x920A : "FocalLength", // Focal length of the lens in mm
  57. 0xA20B : "FlashEnergy", // Strobe energy in BCPS
  58. 0xA20C : "SpatialFrequencyResponse", //
  59. 0xA20E : "FocalPlaneXResolution", // Number of pixels in width direction per FocalPlaneResolutionUnit
  60. 0xA20F : "FocalPlaneYResolution", // Number of pixels in height direction per FocalPlaneResolutionUnit
  61. 0xA210 : "FocalPlaneResolutionUnit", // Unit for measuring FocalPlaneXResolution and FocalPlaneYResolution
  62. 0xA214 : "SubjectLocation", // Location of subject in image
  63. 0xA215 : "ExposureIndex", // Exposure index selected on camera
  64. 0xA217 : "SensingMethod", // Image sensor type
  65. 0xA300 : "FileSource", // Image source (3 == DSC)
  66. 0xA301 : "SceneType", // Scene type (1 == directly photographed)
  67. 0xA302 : "CFAPattern", // Color filter array geometric pattern
  68. 0xA401 : "CustomRendered", // Special processing
  69. 0xA402 : "ExposureMode", // Exposure mode
  70. 0xA403 : "WhiteBalance", // 1 = auto white balance, 2 = manual
  71. 0xA404 : "DigitalZoomRation", // Digital zoom ratio
  72. 0xA405 : "FocalLengthIn35mmFilm", // Equivalent foacl length assuming 35mm film camera (in mm)
  73. 0xA406 : "SceneCaptureType", // Type of scene
  74. 0xA407 : "GainControl", // Degree of overall image gain adjustment
  75. 0xA408 : "Contrast", // Direction of contrast processing applied by camera
  76. 0xA409 : "Saturation", // Direction of saturation processing applied by camera
  77. 0xA40A : "Sharpness", // Direction of sharpness processing applied by camera
  78. 0xA40B : "DeviceSettingDescription", //
  79. 0xA40C : "SubjectDistanceRange", // Distance to subject
  80. // other tags
  81. 0xA005 : "InteroperabilityIFDPointer",
  82. 0xA420 : "ImageUniqueID" // Identifier assigned uniquely to each image
  83. };
  84. var TiffTags = EXIF.TiffTags = {
  85. 0x0100 : "ImageWidth",
  86. 0x0101 : "ImageHeight",
  87. 0x8769 : "ExifIFDPointer",
  88. 0x8825 : "GPSInfoIFDPointer",
  89. 0xA005 : "InteroperabilityIFDPointer",
  90. 0x0102 : "BitsPerSample",
  91. 0x0103 : "Compression",
  92. 0x0106 : "PhotometricInterpretation",
  93. 0x0112 : "Orientation",
  94. 0x0115 : "SamplesPerPixel",
  95. 0x011C : "PlanarConfiguration",
  96. 0x0212 : "YCbCrSubSampling",
  97. 0x0213 : "YCbCrPositioning",
  98. 0x011A : "XResolution",
  99. 0x011B : "YResolution",
  100. 0x0128 : "ResolutionUnit",
  101. 0x0111 : "StripOffsets",
  102. 0x0116 : "RowsPerStrip",
  103. 0x0117 : "StripByteCounts",
  104. 0x0201 : "JPEGInterchangeFormat",
  105. 0x0202 : "JPEGInterchangeFormatLength",
  106. 0x012D : "TransferFunction",
  107. 0x013E : "WhitePoint",
  108. 0x013F : "PrimaryChromaticities",
  109. 0x0211 : "YCbCrCoefficients",
  110. 0x0214 : "ReferenceBlackWhite",
  111. 0x0132 : "DateTime",
  112. 0x010E : "ImageDescription",
  113. 0x010F : "Make",
  114. 0x0110 : "Model",
  115. 0x0131 : "Software",
  116. 0x013B : "Artist",
  117. 0x8298 : "Copyright"
  118. };
  119. var GPSTags = EXIF.GPSTags = {
  120. 0x0000 : "GPSVersionID",
  121. 0x0001 : "GPSLatitudeRef",
  122. 0x0002 : "GPSLatitude",
  123. 0x0003 : "GPSLongitudeRef",
  124. 0x0004 : "GPSLongitude",
  125. 0x0005 : "GPSAltitudeRef",
  126. 0x0006 : "GPSAltitude",
  127. 0x0007 : "GPSTimeStamp",
  128. 0x0008 : "GPSSatellites",
  129. 0x0009 : "GPSStatus",
  130. 0x000A : "GPSMeasureMode",
  131. 0x000B : "GPSDOP",
  132. 0x000C : "GPSSpeedRef",
  133. 0x000D : "GPSSpeed",
  134. 0x000E : "GPSTrackRef",
  135. 0x000F : "GPSTrack",
  136. 0x0010 : "GPSImgDirectionRef",
  137. 0x0011 : "GPSImgDirection",
  138. 0x0012 : "GPSMapDatum",
  139. 0x0013 : "GPSDestLatitudeRef",
  140. 0x0014 : "GPSDestLatitude",
  141. 0x0015 : "GPSDestLongitudeRef",
  142. 0x0016 : "GPSDestLongitude",
  143. 0x0017 : "GPSDestBearingRef",
  144. 0x0018 : "GPSDestBearing",
  145. 0x0019 : "GPSDestDistanceRef",
  146. 0x001A : "GPSDestDistance",
  147. 0x001B : "GPSProcessingMethod",
  148. 0x001C : "GPSAreaInformation",
  149. 0x001D : "GPSDateStamp",
  150. 0x001E : "GPSDifferential"
  151. };
  152. var StringValues = EXIF.StringValues = {
  153. ExposureProgram : {
  154. 0 : "Not defined",
  155. 1 : "Manual",
  156. 2 : "Normal program",
  157. 3 : "Aperture priority",
  158. 4 : "Shutter priority",
  159. 5 : "Creative program",
  160. 6 : "Action program",
  161. 7 : "Portrait mode",
  162. 8 : "Landscape mode"
  163. },
  164. MeteringMode : {
  165. 0 : "Unknown",
  166. 1 : "Average",
  167. 2 : "CenterWeightedAverage",
  168. 3 : "Spot",
  169. 4 : "MultiSpot",
  170. 5 : "Pattern",
  171. 6 : "Partial",
  172. 255 : "Other"
  173. },
  174. LightSource : {
  175. 0 : "Unknown",
  176. 1 : "Daylight",
  177. 2 : "Fluorescent",
  178. 3 : "Tungsten (incandescent light)",
  179. 4 : "Flash",
  180. 9 : "Fine weather",
  181. 10 : "Cloudy weather",
  182. 11 : "Shade",
  183. 12 : "Daylight fluorescent (D 5700 - 7100K)",
  184. 13 : "Day white fluorescent (N 4600 - 5400K)",
  185. 14 : "Cool white fluorescent (W 3900 - 4500K)",
  186. 15 : "White fluorescent (WW 3200 - 3700K)",
  187. 17 : "Standard light A",
  188. 18 : "Standard light B",
  189. 19 : "Standard light C",
  190. 20 : "D55",
  191. 21 : "D65",
  192. 22 : "D75",
  193. 23 : "D50",
  194. 24 : "ISO studio tungsten",
  195. 255 : "Other"
  196. },
  197. Flash : {
  198. 0x0000 : "Flash did not fire",
  199. 0x0001 : "Flash fired",
  200. 0x0005 : "Strobe return light not detected",
  201. 0x0007 : "Strobe return light detected",
  202. 0x0009 : "Flash fired, compulsory flash mode",
  203. 0x000D : "Flash fired, compulsory flash mode, return light not detected",
  204. 0x000F : "Flash fired, compulsory flash mode, return light detected",
  205. 0x0010 : "Flash did not fire, compulsory flash mode",
  206. 0x0018 : "Flash did not fire, auto mode",
  207. 0x0019 : "Flash fired, auto mode",
  208. 0x001D : "Flash fired, auto mode, return light not detected",
  209. 0x001F : "Flash fired, auto mode, return light detected",
  210. 0x0020 : "No flash function",
  211. 0x0041 : "Flash fired, red-eye reduction mode",
  212. 0x0045 : "Flash fired, red-eye reduction mode, return light not detected",
  213. 0x0047 : "Flash fired, red-eye reduction mode, return light detected",
  214. 0x0049 : "Flash fired, compulsory flash mode, red-eye reduction mode",
  215. 0x004D : "Flash fired, compulsory flash mode, red-eye reduction mode, return light not detected",
  216. 0x004F : "Flash fired, compulsory flash mode, red-eye reduction mode, return light detected",
  217. 0x0059 : "Flash fired, auto mode, red-eye reduction mode",
  218. 0x005D : "Flash fired, auto mode, return light not detected, red-eye reduction mode",
  219. 0x005F : "Flash fired, auto mode, return light detected, red-eye reduction mode"
  220. },
  221. SensingMethod : {
  222. 1 : "Not defined",
  223. 2 : "One-chip color area sensor",
  224. 3 : "Two-chip color area sensor",
  225. 4 : "Three-chip color area sensor",
  226. 5 : "Color sequential area sensor",
  227. 7 : "Trilinear sensor",
  228. 8 : "Color sequential linear sensor"
  229. },
  230. SceneCaptureType : {
  231. 0 : "Standard",
  232. 1 : "Landscape",
  233. 2 : "Portrait",
  234. 3 : "Night scene"
  235. },
  236. SceneType : {
  237. 1 : "Directly photographed"
  238. },
  239. CustomRendered : {
  240. 0 : "Normal process",
  241. 1 : "Custom process"
  242. },
  243. WhiteBalance : {
  244. 0 : "Auto white balance",
  245. 1 : "Manual white balance"
  246. },
  247. GainControl : {
  248. 0 : "None",
  249. 1 : "Low gain up",
  250. 2 : "High gain up",
  251. 3 : "Low gain down",
  252. 4 : "High gain down"
  253. },
  254. Contrast : {
  255. 0 : "Normal",
  256. 1 : "Soft",
  257. 2 : "Hard"
  258. },
  259. Saturation : {
  260. 0 : "Normal",
  261. 1 : "Low saturation",
  262. 2 : "High saturation"
  263. },
  264. Sharpness : {
  265. 0 : "Normal",
  266. 1 : "Soft",
  267. 2 : "Hard"
  268. },
  269. SubjectDistanceRange : {
  270. 0 : "Unknown",
  271. 1 : "Macro",
  272. 2 : "Close view",
  273. 3 : "Distant view"
  274. },
  275. FileSource : {
  276. 3 : "DSC"
  277. },
  278. Components : {
  279. 0 : "",
  280. 1 : "Y",
  281. 2 : "Cb",
  282. 3 : "Cr",
  283. 4 : "R",
  284. 5 : "G",
  285. 6 : "B"
  286. }
  287. };
  288. function addEvent(element, event, handler) {
  289. if (element.addEventListener) {
  290. element.addEventListener(event, handler, false);
  291. } else if (element.attachEvent) {
  292. element.attachEvent("on" + event, handler);
  293. }
  294. }
  295. function imageHasData(img) {
  296. return !!(img.exifdata);
  297. }
  298. function base64ToArrayBuffer(base64, contentType) {
  299. contentType = contentType || base64.match(/^data\:([^\;]+)\;base64,/mi)[1] || ''; // e.g. 'data:image/jpeg;base64,...' => 'image/jpeg'
  300. base64 = base64.replace(/^data\:([^\;]+)\;base64,/gmi, '');
  301. var binary = atob(base64);
  302. var len = binary.length;
  303. var buffer = new ArrayBuffer(len);
  304. var view = new Uint8Array(buffer);
  305. for (var i = 0; i < len; i++) {
  306. view[i] = binary.charCodeAt(i);
  307. }
  308. return buffer;
  309. }
  310. function objectURLToBlob(url, callback) {
  311. var http = new XMLHttpRequest();
  312. http.open("GET", url, true);
  313. http.responseType = "blob";
  314. http.onload = function(e) {
  315. if (this.status == 200 || this.status === 0) {
  316. callback(this.response);
  317. }
  318. };
  319. http.send();
  320. }
  321. function getImageData(img, callback) {
  322. function handleBinaryFile(binFile) {
  323. var data = findEXIFinJPEG(binFile);
  324. var iptcdata = findIPTCinJPEG(binFile);
  325. img.exifdata = data || {};
  326. img.iptcdata = iptcdata || {};
  327. if (callback) {
  328. callback.call(img);
  329. }
  330. }
  331. if (img.src) {
  332. if (/^data\:/i.test(img.src)) { // Data URI
  333. var arrayBuffer = base64ToArrayBuffer(img.src);
  334. handleBinaryFile(arrayBuffer);
  335. } else if (/^blob\:/i.test(img.src)) { // Object URL
  336. var fileReader = new FileReader();
  337. fileReader.onload = function(e) {
  338. handleBinaryFile(e.target.result);
  339. };
  340. objectURLToBlob(img.src, function (blob) {
  341. fileReader.readAsArrayBuffer(blob);
  342. });
  343. } else {
  344. var http = new XMLHttpRequest();
  345. http.onload = function() {
  346. if (this.status == 200 || this.status === 0) {
  347. handleBinaryFile(http.response);
  348. } else {
  349. throw "Could not load image";
  350. }
  351. http = null;
  352. };
  353. http.open("GET", img.src, true);
  354. http.responseType = "arraybuffer";
  355. http.send(null);
  356. }
  357. } else if (window.FileReader && (img instanceof window.Blob || img instanceof window.File)) {
  358. var fileReader = new FileReader();
  359. fileReader.onload = function(e) {
  360. if (debug) console.log("Got file of length " + e.target.result.byteLength);
  361. handleBinaryFile(e.target.result);
  362. };
  363. fileReader.readAsArrayBuffer(img);
  364. }
  365. }
  366. function findEXIFinJPEG(file) {
  367. var dataView = new DataView(file);
  368. if (debug) console.log("Got file of length " + file.byteLength);
  369. if ((dataView.getUint8(0) != 0xFF) || (dataView.getUint8(1) != 0xD8)) {
  370. if (debug) console.log("Not a valid JPEG");
  371. return false; // not a valid jpeg
  372. }
  373. var offset = 2,
  374. length = file.byteLength,
  375. marker;
  376. while (offset < length) {
  377. if (dataView.getUint8(offset) != 0xFF) {
  378. if (debug) console.log("Not a valid marker at offset " + offset + ", found: " + dataView.getUint8(offset));
  379. return false; // not a valid marker, something is wrong
  380. }
  381. marker = dataView.getUint8(offset + 1);
  382. if (debug) console.log(marker);
  383. // we could implement handling for other markers here,
  384. // but we're only looking for 0xFFE1 for EXIF data
  385. if (marker == 225) {
  386. if (debug) console.log("Found 0xFFE1 marker");
  387. return readEXIFData(dataView, offset + 4, dataView.getUint16(offset + 2) - 2);
  388. // offset += 2 + file.getShortAt(offset+2, true);
  389. } else {
  390. offset += 2 + dataView.getUint16(offset+2);
  391. }
  392. }
  393. }
  394. function findIPTCinJPEG(file) {
  395. var dataView = new DataView(file);
  396. if (debug) console.log("Got file of length " + file.byteLength);
  397. if ((dataView.getUint8(0) != 0xFF) || (dataView.getUint8(1) != 0xD8)) {
  398. if (debug) console.log("Not a valid JPEG");
  399. return false; // not a valid jpeg
  400. }
  401. var offset = 2,
  402. length = file.byteLength;
  403. var isFieldSegmentStart = function(dataView, offset){
  404. return (
  405. dataView.getUint8(offset) === 0x38 &&
  406. dataView.getUint8(offset+1) === 0x42 &&
  407. dataView.getUint8(offset+2) === 0x49 &&
  408. dataView.getUint8(offset+3) === 0x4D &&
  409. dataView.getUint8(offset+4) === 0x04 &&
  410. dataView.getUint8(offset+5) === 0x04
  411. );
  412. };
  413. while (offset < length) {
  414. if ( isFieldSegmentStart(dataView, offset )){
  415. // Get the length of the name header (which is padded to an even number of bytes)
  416. var nameHeaderLength = dataView.getUint8(offset+7);
  417. if(nameHeaderLength % 2 !== 0) nameHeaderLength += 1;
  418. // Check for pre photoshop 6 format
  419. if(nameHeaderLength === 0) {
  420. // Always 4
  421. nameHeaderLength = 4;
  422. }
  423. var startOffset = offset + 8 + nameHeaderLength;
  424. var sectionLength = dataView.getUint16(offset + 6 + nameHeaderLength);
  425. return readIPTCData(file, startOffset, sectionLength);
  426. break;
  427. }
  428. // Not the marker, continue searching
  429. offset++;
  430. }
  431. }
  432. var IptcFieldMap = {
  433. 0x78 : 'caption',
  434. 0x6E : 'credit',
  435. 0x19 : 'keywords',
  436. 0x37 : 'dateCreated',
  437. 0x50 : 'byline',
  438. 0x55 : 'bylineTitle',
  439. 0x7A : 'captionWriter',
  440. 0x69 : 'headline',
  441. 0x74 : 'copyright',
  442. 0x0F : 'category'
  443. };
  444. function readIPTCData(file, startOffset, sectionLength){
  445. var dataView = new DataView(file);
  446. var data = {};
  447. var fieldValue, fieldName, dataSize, segmentType, segmentSize;
  448. var segmentStartPos = startOffset;
  449. while(segmentStartPos < startOffset+sectionLength) {
  450. if(dataView.getUint8(segmentStartPos) === 0x1C && dataView.getUint8(segmentStartPos+1) === 0x02){
  451. segmentType = dataView.getUint8(segmentStartPos+2);
  452. if(segmentType in IptcFieldMap) {
  453. dataSize = dataView.getInt16(segmentStartPos+3);
  454. segmentSize = dataSize + 5;
  455. fieldName = IptcFieldMap[segmentType];
  456. fieldValue = getStringFromDB(dataView, segmentStartPos+5, dataSize);
  457. // Check if we already stored a value with this name
  458. if(data.hasOwnProperty(fieldName)) {
  459. // Value already stored with this name, create multivalue field
  460. if(data[fieldName] instanceof Array) {
  461. data[fieldName].push(fieldValue);
  462. }
  463. else {
  464. data[fieldName] = [data[fieldName], fieldValue];
  465. }
  466. }
  467. else {
  468. data[fieldName] = fieldValue;
  469. }
  470. }
  471. }
  472. segmentStartPos++;
  473. }
  474. return data;
  475. }
  476. function readTags(file, tiffStart, dirStart, strings, bigEnd) {
  477. var entries = file.getUint16(dirStart, !bigEnd),
  478. tags = {},
  479. entryOffset, tag,
  480. i;
  481. for (i=0;i<entries;i++) {
  482. entryOffset = dirStart + i*12 + 2;
  483. tag = strings[file.getUint16(entryOffset, !bigEnd)];
  484. if (!tag && debug) console.log("Unknown tag: " + file.getUint16(entryOffset, !bigEnd));
  485. tags[tag] = readTagValue(file, entryOffset, tiffStart, dirStart, bigEnd);
  486. }
  487. return tags;
  488. }
  489. function readTagValue(file, entryOffset, tiffStart, dirStart, bigEnd) {
  490. var type = file.getUint16(entryOffset+2, !bigEnd),
  491. numValues = file.getUint32(entryOffset+4, !bigEnd),
  492. valueOffset = file.getUint32(entryOffset+8, !bigEnd) + tiffStart,
  493. offset,
  494. vals, val, n,
  495. numerator, denominator;
  496. switch (type) {
  497. case 1: // byte, 8-bit unsigned int
  498. case 7: // undefined, 8-bit byte, value depending on field
  499. if (numValues == 1) {
  500. return file.getUint8(entryOffset + 8, !bigEnd);
  501. } else {
  502. offset = numValues > 4 ? valueOffset : (entryOffset + 8);
  503. vals = [];
  504. for (n=0;n<numValues;n++) {
  505. vals[n] = file.getUint8(offset + n);
  506. }
  507. return vals;
  508. }
  509. case 2: // ascii, 8-bit byte
  510. offset = numValues > 4 ? valueOffset : (entryOffset + 8);
  511. return getStringFromDB(file, offset, numValues-1);
  512. case 3: // short, 16 bit int
  513. if (numValues == 1) {
  514. return file.getUint16(entryOffset + 8, !bigEnd);
  515. } else {
  516. offset = numValues > 2 ? valueOffset : (entryOffset + 8);
  517. vals = [];
  518. for (n=0;n<numValues;n++) {
  519. vals[n] = file.getUint16(offset + 2*n, !bigEnd);
  520. }
  521. return vals;
  522. }
  523. case 4: // long, 32 bit int
  524. if (numValues == 1) {
  525. return file.getUint32(entryOffset + 8, !bigEnd);
  526. } else {
  527. vals = [];
  528. for (n=0;n<numValues;n++) {
  529. vals[n] = file.getUint32(valueOffset + 4*n, !bigEnd);
  530. }
  531. return vals;
  532. }
  533. case 5: // rational = two long values, first is numerator, second is denominator
  534. if (numValues == 1) {
  535. numerator = file.getUint32(valueOffset, !bigEnd);
  536. denominator = file.getUint32(valueOffset+4, !bigEnd);
  537. val = new Number(numerator / denominator);
  538. val.numerator = numerator;
  539. val.denominator = denominator;
  540. return val;
  541. } else {
  542. vals = [];
  543. for (n=0;n<numValues;n++) {
  544. numerator = file.getUint32(valueOffset + 8*n, !bigEnd);
  545. denominator = file.getUint32(valueOffset+4 + 8*n, !bigEnd);
  546. vals[n] = new Number(numerator / denominator);
  547. vals[n].numerator = numerator;
  548. vals[n].denominator = denominator;
  549. }
  550. return vals;
  551. }
  552. case 9: // slong, 32 bit signed int
  553. if (numValues == 1) {
  554. return file.getInt32(entryOffset + 8, !bigEnd);
  555. } else {
  556. vals = [];
  557. for (n=0;n<numValues;n++) {
  558. vals[n] = file.getInt32(valueOffset + 4*n, !bigEnd);
  559. }
  560. return vals;
  561. }
  562. case 10: // signed rational, two slongs, first is numerator, second is denominator
  563. if (numValues == 1) {
  564. return file.getInt32(valueOffset, !bigEnd) / file.getInt32(valueOffset+4, !bigEnd);
  565. } else {
  566. vals = [];
  567. for (n=0;n<numValues;n++) {
  568. vals[n] = file.getInt32(valueOffset + 8*n, !bigEnd) / file.getInt32(valueOffset+4 + 8*n, !bigEnd);
  569. }
  570. return vals;
  571. }
  572. }
  573. }
  574. function getStringFromDB(buffer, start, length) {
  575. var outstr = "";
  576. for (n = start; n < start+length; n++) {
  577. outstr += String.fromCharCode(buffer.getUint8(n));
  578. }
  579. return outstr;
  580. }
  581. function readEXIFData(file, start) {
  582. if (getStringFromDB(file, start, 4) != "Exif") {
  583. if (debug) console.log("Not valid EXIF data! " + getStringFromDB(file, start, 4));
  584. return false;
  585. }
  586. var bigEnd,
  587. tags, tag,
  588. exifData, gpsData,
  589. tiffOffset = start + 6;
  590. // test for TIFF validity and endianness
  591. if (file.getUint16(tiffOffset) == 0x4949) {
  592. bigEnd = false;
  593. } else if (file.getUint16(tiffOffset) == 0x4D4D) {
  594. bigEnd = true;
  595. } else {
  596. if (debug) console.log("Not valid TIFF data! (no 0x4949 or 0x4D4D)");
  597. return false;
  598. }
  599. if (file.getUint16(tiffOffset+2, !bigEnd) != 0x002A) {
  600. if (debug) console.log("Not valid TIFF data! (no 0x002A)");
  601. return false;
  602. }
  603. var firstIFDOffset = file.getUint32(tiffOffset+4, !bigEnd);
  604. if (firstIFDOffset < 0x00000008) {
  605. if (debug) console.log("Not valid TIFF data! (First offset less than 8)", file.getUint32(tiffOffset+4, !bigEnd));
  606. return false;
  607. }
  608. tags = readTags(file, tiffOffset, tiffOffset + firstIFDOffset, TiffTags, bigEnd);
  609. if (tags.ExifIFDPointer) {
  610. exifData = readTags(file, tiffOffset, tiffOffset + tags.ExifIFDPointer, ExifTags, bigEnd);
  611. for (tag in exifData) {
  612. switch (tag) {
  613. case "LightSource" :
  614. case "Flash" :
  615. case "MeteringMode" :
  616. case "ExposureProgram" :
  617. case "SensingMethod" :
  618. case "SceneCaptureType" :
  619. case "SceneType" :
  620. case "CustomRendered" :
  621. case "WhiteBalance" :
  622. case "GainControl" :
  623. case "Contrast" :
  624. case "Saturation" :
  625. case "Sharpness" :
  626. case "SubjectDistanceRange" :
  627. case "FileSource" :
  628. exifData[tag] = StringValues[tag][exifData[tag]];
  629. break;
  630. case "ExifVersion" :
  631. case "FlashpixVersion" :
  632. exifData[tag] = String.fromCharCode(exifData[tag][0], exifData[tag][1], exifData[tag][2], exifData[tag][3]);
  633. break;
  634. case "ComponentsConfiguration" :
  635. exifData[tag] =
  636. StringValues.Components[exifData[tag][0]] +
  637. StringValues.Components[exifData[tag][1]] +
  638. StringValues.Components[exifData[tag][2]] +
  639. StringValues.Components[exifData[tag][3]];
  640. break;
  641. }
  642. tags[tag] = exifData[tag];
  643. }
  644. }
  645. if (tags.GPSInfoIFDPointer) {
  646. gpsData = readTags(file, tiffOffset, tiffOffset + tags.GPSInfoIFDPointer, GPSTags, bigEnd);
  647. for (tag in gpsData) {
  648. switch (tag) {
  649. case "GPSVersionID" :
  650. gpsData[tag] = gpsData[tag][0] +
  651. "." + gpsData[tag][1] +
  652. "." + gpsData[tag][2] +
  653. "." + gpsData[tag][3];
  654. break;
  655. }
  656. tags[tag] = gpsData[tag];
  657. }
  658. }
  659. return tags;
  660. }
  661. EXIF.getData = function(img, callback) {
  662. if ((img instanceof Image || img instanceof HTMLImageElement) && !img.complete) return false;
  663. if (!imageHasData(img)) {
  664. getImageData(img, callback);
  665. } else {
  666. if (callback) {
  667. callback.call(img);
  668. }
  669. }
  670. return true;
  671. }
  672. EXIF.getTag = function(img, tag) {
  673. if (!imageHasData(img)) return;
  674. return img.exifdata[tag];
  675. }
  676. EXIF.getAllTags = function(img) {
  677. if (!imageHasData(img)) return {};
  678. var a,
  679. data = img.exifdata,
  680. tags = {};
  681. for (a in data) {
  682. if (data.hasOwnProperty(a)) {
  683. tags[a] = data[a];
  684. }
  685. }
  686. return tags;
  687. }
  688. EXIF.pretty = function(img) {
  689. if (!imageHasData(img)) return "";
  690. var a,
  691. data = img.exifdata,
  692. strPretty = "";
  693. for (a in data) {
  694. if (data.hasOwnProperty(a)) {
  695. if (typeof data[a] == "object") {
  696. if (data[a] instanceof Number) {
  697. strPretty += a + " : " + data[a] + " [" + data[a].numerator + "/" + data[a].denominator + "]\r\n";
  698. } else {
  699. strPretty += a + " : [" + data[a].length + " values]\r\n";
  700. }
  701. } else {
  702. strPretty += a + " : " + data[a] + "\r\n";
  703. }
  704. }
  705. }
  706. return strPretty;
  707. }
  708. EXIF.readFromBinaryFile = function(file) {
  709. return findEXIFinJPEG(file);
  710. }
  711. if (typeof define === 'function' && define.amd) {
  712. define('exif-js', [], function() {
  713. return EXIF;
  714. });
  715. }
  716. }.call(this));