crop-exif.js 28 KB

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