(function(){"use strict";var that={};that.version="1.0.4";that.remove=function(jpeg){var b64=false;if(jpeg.slice(0,2)=="ÿØ"){}else if(jpeg.slice(0,23)=="data:image/jpeg;base64,"||jpeg.slice(0,22)=="data:image/jpg;base64,"){jpeg=atob(jpeg.split(",")[1]);b64=true}else{throw new Error("Given data is not jpeg.")}var segments=splitIntoSegments(jpeg);var newSegments=segments.filter(function(seg){return!(seg.slice(0,2)=="ÿá"&&seg.slice(4,10)=="Exif\0\0")});var new_data=newSegments.join("");if(b64){new_data="data:image/jpeg;base64,"+btoa(new_data)}return new_data};that.insert=function(exif,jpeg){var b64=false;if(exif.slice(0,6)!="Exif\0\0"){throw new Error("Given data is not exif.")}if(jpeg.slice(0,2)=="ÿØ"){}else if(jpeg.slice(0,23)=="data:image/jpeg;base64,"||jpeg.slice(0,22)=="data:image/jpg;base64,"){jpeg=atob(jpeg.split(",")[1]);b64=true}else{throw new Error("Given data is not jpeg.")}var exifStr="ÿá"+pack(">H",[exif.length+2])+exif;var segments=splitIntoSegments(jpeg);var new_data=mergeSegments(segments,exifStr);if(b64){new_data="data:image/jpeg;base64,"+btoa(new_data)}return new_data};that.load=function(data){var input_data;if(typeof data=="string"){if(data.slice(0,2)=="ÿØ"){input_data=data}else if(data.slice(0,23)=="data:image/jpeg;base64,"||data.slice(0,22)=="data:image/jpg;base64,"){input_data=atob(data.split(",")[1])}else if(data.slice(0,4)=="Exif"){input_data=data.slice(6)}else{throw new Error("'load' gots invalid file data.")}}else{throw new Error("'load' gots invalid type argument.")}var exifDict={};var exif_dict={"0th":{},Exif:{},GPS:{},Interop:{},"1st":{},thumbnail:null};var exifReader=new ExifReader(input_data);if(exifReader.tiftag===null){return exif_dict}if(exifReader.tiftag.slice(0,2)=="II"){exifReader.endian_mark="<"}else{exifReader.endian_mark=">"}var pointer=unpack(exifReader.endian_mark+"L",exifReader.tiftag.slice(4,8))[0];exif_dict["0th"]=exifReader.get_ifd(pointer,"0th");var first_ifd_pointer=exif_dict["0th"]["first_ifd_pointer"];delete exif_dict["0th"]["first_ifd_pointer"];if(34665 in exif_dict["0th"]){pointer=exif_dict["0th"][34665];exif_dict["Exif"]=exifReader.get_ifd(pointer,"Exif")}if(34853 in exif_dict["0th"]){pointer=exif_dict["0th"][34853];exif_dict["GPS"]=exifReader.get_ifd(pointer,"GPS")}if(40965 in exif_dict["Exif"]){pointer=exif_dict["Exif"][40965];exif_dict["Interop"]=exifReader.get_ifd(pointer,"Interop")}if(first_ifd_pointer!="\0\0\0\0"){pointer=unpack(exifReader.endian_mark+"L",first_ifd_pointer)[0];exif_dict["1st"]=exifReader.get_ifd(pointer,"1st");if(513 in exif_dict["1st"]&&514 in exif_dict["1st"]){var end=exif_dict["1st"][513]+exif_dict["1st"][514];var thumb=exifReader.tiftag.slice(exif_dict["1st"][513],end);exif_dict["thumbnail"]=thumb}}return exif_dict};that.dump=function(exif_dict_original){var TIFF_HEADER_LENGTH=8;var exif_dict=copy(exif_dict_original);var header="Exif\0\0MM\0*\0\0\0\b";var exif_is=false;var gps_is=false;var interop_is=false;var first_is=false;var zeroth_ifd,exif_ifd,interop_ifd,gps_ifd,first_ifd;if("0th"in exif_dict){zeroth_ifd=exif_dict["0th"]}else{zeroth_ifd={}}if("Exif"in exif_dict&&Object.keys(exif_dict["Exif"]).length||"Interop"in exif_dict&&Object.keys(exif_dict["Interop"]).length){zeroth_ifd[34665]=1;exif_is=true;exif_ifd=exif_dict["Exif"];if("Interop"in exif_dict&&Object.keys(exif_dict["Interop"]).length){exif_ifd[40965]=1;interop_is=true;interop_ifd=exif_dict["Interop"]}else if(Object.keys(exif_ifd).indexOf(that.ExifIFD.InteroperabilityTag.toString())>-1){delete exif_ifd[40965]}}else if(Object.keys(zeroth_ifd).indexOf(that.ImageIFD.ExifTag.toString())>-1){delete zeroth_ifd[34665]}if("GPS"in exif_dict&&Object.keys(exif_dict["GPS"]).length){zeroth_ifd[that.ImageIFD.GPSTag]=1;gps_is=true;gps_ifd=exif_dict["GPS"]}else if(Object.keys(zeroth_ifd).indexOf(that.ImageIFD.GPSTag.toString())>-1){delete zeroth_ifd[that.ImageIFD.GPSTag]}if("1st"in exif_dict&&"thumbnail"in exif_dict&&exif_dict["thumbnail"]!=null){first_is=true;exif_dict["1st"][513]=1;exif_dict["1st"][514]=1;first_ifd=exif_dict["1st"]}var zeroth_set=_dict_to_bytes(zeroth_ifd,"0th",0);var zeroth_length=zeroth_set[0].length+exif_is*12+gps_is*12+4+zeroth_set[1].length;var exif_set,exif_bytes="",exif_length=0,gps_set,gps_bytes="",gps_length=0,interop_set,interop_bytes="",interop_length=0,first_set,first_bytes="",thumbnail;if(exif_is){exif_set=_dict_to_bytes(exif_ifd,"Exif",zeroth_length);exif_length=exif_set[0].length+interop_is*12+exif_set[1].length}if(gps_is){gps_set=_dict_to_bytes(gps_ifd,"GPS",zeroth_length+exif_length);gps_bytes=gps_set.join("");gps_length=gps_bytes.length}if(interop_is){var offset=zeroth_length+exif_length+gps_length;interop_set=_dict_to_bytes(interop_ifd,"Interop",offset);interop_bytes=interop_set.join("");interop_length=interop_bytes.length}if(first_is){var offset=zeroth_length+exif_length+gps_length+interop_length;first_set=_dict_to_bytes(first_ifd,"1st",offset);thumbnail=_get_thumbnail(exif_dict["thumbnail"]);if(thumbnail.length>64e3){throw new Error("Given thumbnail is too large. max 64kB")}}var exif_pointer="",gps_pointer="",interop_pointer="",first_ifd_pointer="\0\0\0\0";if(exif_is){var pointer_value=TIFF_HEADER_LENGTH+zeroth_length;var pointer_str=pack(">L",[pointer_value]);var key=34665;var key_str=pack(">H",[key]);var type_str=pack(">H",[TYPES["Long"]]);var length_str=pack(">L",[1]);exif_pointer=key_str+type_str+length_str+pointer_str}if(gps_is){var pointer_value=TIFF_HEADER_LENGTH+zeroth_length+exif_length;var pointer_str=pack(">L",[pointer_value]);var key=34853;var key_str=pack(">H",[key]);var type_str=pack(">H",[TYPES["Long"]]);var length_str=pack(">L",[1]);gps_pointer=key_str+type_str+length_str+pointer_str}if(interop_is){var pointer_value=TIFF_HEADER_LENGTH+zeroth_length+exif_length+gps_length;var pointer_str=pack(">L",[pointer_value]);var key=40965;var key_str=pack(">H",[key]);var type_str=pack(">H",[TYPES["Long"]]);var length_str=pack(">L",[1]);interop_pointer=key_str+type_str+length_str+pointer_str}if(first_is){var pointer_value=TIFF_HEADER_LENGTH+zeroth_length+exif_length+gps_length+interop_length;first_ifd_pointer=pack(">L",[pointer_value]);var thumbnail_pointer=pointer_value+first_set[0].length+24+4+first_set[1].length;var thumbnail_p_bytes="\0\0\0\0"+pack(">L",[thumbnail_pointer]);var thumbnail_length_bytes="\0\0\0\0"+pack(">L",[thumbnail.length]);first_bytes=first_set[0]+thumbnail_p_bytes+thumbnail_length_bytes+"\0\0\0\0"+first_set[1]+thumbnail}var zeroth_bytes=zeroth_set[0]+exif_pointer+gps_pointer+first_ifd_pointer+zeroth_set[1];if(exif_is){exif_bytes=exif_set[0]+interop_pointer+exif_set[1]}return header+zeroth_bytes+exif_bytes+gps_bytes+interop_bytes+first_bytes};function copy(obj){return JSON.parse(JSON.stringify(obj))}function _get_thumbnail(jpeg){var segments=splitIntoSegments(jpeg);while("ÿà"<=segments[1].slice(0,2)&&segments[1].slice(0,2)<="ÿï"){segments=[segments[0]].concat(segments.slice(2))}return segments.join("")}function _pack_byte(array){return pack(">"+nStr("B",array.length),array)}function _pack_short(array){return pack(">"+nStr("H",array.length),array)}function _pack_long(array){return pack(">"+nStr("L",array.length),array)}function _value_to_bytes(raw_value,value_type,offset){var four_bytes_over="";var value_str="";var length,new_value,num,den;if(value_type=="Byte"){length=raw_value.length;if(length<=4){value_str=_pack_byte(raw_value)+nStr("\0",4-length)}else{value_str=pack(">L",[offset]);four_bytes_over=_pack_byte(raw_value)}}else if(value_type=="Short"){length=raw_value.length;if(length<=2){value_str=_pack_short(raw_value)+nStr("\0\0",2-length)}else{value_str=pack(">L",[offset]);four_bytes_over=_pack_short(raw_value)}}else if(value_type=="Long"){length=raw_value.length;if(length<=1){value_str=_pack_long(raw_value)}else{value_str=pack(">L",[offset]);four_bytes_over=_pack_long(raw_value)}}else if(value_type=="Ascii"){new_value=raw_value+"\0";length=new_value.length;if(length>4){value_str=pack(">L",[offset]);four_bytes_over=new_value}else{value_str=new_value+nStr("\0",4-length)}}else if(value_type=="Rational"){if(typeof raw_value[0]=="number"){length=1;num=raw_value[0];den=raw_value[1];new_value=pack(">L",[num])+pack(">L",[den])}else{length=raw_value.length;new_value="";for(var n=0;nL",[num])+pack(">L",[den])}}value_str=pack(">L",[offset]);four_bytes_over=new_value}else if(value_type=="SRational"){if(typeof raw_value[0]=="number"){length=1;num=raw_value[0];den=raw_value[1];new_value=pack(">l",[num])+pack(">l",[den])}else{length=raw_value.length;new_value="";for(var n=0;nl",[num])+pack(">l",[den])}}value_str=pack(">L",[offset]);four_bytes_over=new_value}else if(value_type=="Undefined"){length=raw_value.length;if(length>4){value_str=pack(">L",[offset]);four_bytes_over=raw_value}else{value_str=raw_value+nStr("\0",4-length)}}var length_str=pack(">L",[length]);return[length_str,value_str,four_bytes_over]}function _dict_to_bytes(ifd_dict,ifd,ifd_offset){var TIFF_HEADER_LENGTH=8;var tag_count=Object.keys(ifd_dict).length;var entry_header=pack(">H",[tag_count]);var entries_length;if(["0th","1st"].indexOf(ifd)>-1){entries_length=2+tag_count*12+4}else{entries_length=2+tag_count*12}var entries="";var values="";var key;for(var key in ifd_dict){if(typeof key=="string"){key=parseInt(key)}if(ifd=="0th"&&[34665,34853].indexOf(key)>-1){continue}else if(ifd=="Exif"&&key==40965){continue}else if(ifd=="1st"&&[513,514].indexOf(key)>-1){continue}var raw_value=ifd_dict[key];var key_str=pack(">H",[key]);var value_type=TAGS[ifd][key]["type"];var type_str=pack(">H",[TYPES[value_type]]);if(typeof raw_value=="number"){raw_value=[raw_value]}var offset=TIFF_HEADER_LENGTH+entries_length+ifd_offset+values.length;var b=_value_to_bytes(raw_value,value_type,offset);var length_str=b[0];var value_str=b[1];var four_bytes_over=b[2];entries+=key_str+type_str+length_str+value_str;values+=four_bytes_over}return[entry_header+entries,values]}function ExifReader(data){var segments,app1;if(data.slice(0,2)=="ÿØ"){segments=splitIntoSegments(data);app1=getExifSeg(segments);if(app1){this.tiftag=app1.slice(10)}else{this.tiftag=null}}else if(["II","MM"].indexOf(data.slice(0,2))>-1){this.tiftag=data}else if(data.slice(0,4)=="Exif"){this.tiftag=data.slice(6)}else{throw new Error("Given file is neither JPEG nor TIFF.")}}ExifReader.prototype={get_ifd:function(pointer,ifd_name){var ifd_dict={};var tag_count=unpack(this.endian_mark+"H",this.tiftag.slice(pointer,pointer+2))[0];var offset=pointer+2;var t;if(["0th","1st"].indexOf(ifd_name)>-1){t="Image"}else{t=ifd_name}for(var x=0;x4){pointer=unpack(this.endian_mark+"L",value)[0];data=unpack(this.endian_mark+nStr("B",length),this.tiftag.slice(pointer,pointer+length))}else{data=unpack(this.endian_mark+nStr("B",length),value.slice(0,length))}}else if(t==2){if(length>4){pointer=unpack(this.endian_mark+"L",value)[0];data=this.tiftag.slice(pointer,pointer+length-1)}else{data=value.slice(0,length-1)}}else if(t==3){if(length>2){pointer=unpack(this.endian_mark+"L",value)[0];data=unpack(this.endian_mark+nStr("H",length),this.tiftag.slice(pointer,pointer+length*2))}else{data=unpack(this.endian_mark+nStr("H",length),value.slice(0,length*2))}}else if(t==4){if(length>1){pointer=unpack(this.endian_mark+"L",value)[0];data=unpack(this.endian_mark+nStr("L",length),this.tiftag.slice(pointer,pointer+length*4))}else{data=unpack(this.endian_mark+nStr("L",length),value)}}else if(t==5){pointer=unpack(this.endian_mark+"L",value)[0];if(length>1){data=[];for(var x=0;x4){pointer=unpack(this.endian_mark+"L",value)[0];data=this.tiftag.slice(pointer,pointer+length)}else{data=value.slice(0,length)}}else if(t==9){if(length>1){pointer=unpack(this.endian_mark+"L",value)[0];data=unpack(this.endian_mark+nStr("l",length),this.tiftag.slice(pointer,pointer+length*4))}else{data=unpack(this.endian_mark+nStr("l",length),value)}}else if(t==10){pointer=unpack(this.endian_mark+"L",value)[0];if(length>1){data=[];for(var x=0;x>2;enc2=(chr1&3)<<4|chr2>>4;enc3=(chr2&15)<<2|chr3>>6;enc4=chr3&63;if(isNaN(chr2)){enc3=enc4=64}else if(isNaN(chr3)){enc4=64}output=output+keyStr.charAt(enc1)+keyStr.charAt(enc2)+keyStr.charAt(enc3)+keyStr.charAt(enc4)}return output}}if(typeof window!=="undefined"&&typeof window.atob==="function"){var atob=window.atob}if(typeof atob==="undefined"){var atob=function(input){var output="";var chr1,chr2,chr3;var enc1,enc2,enc3,enc4;var i=0;var keyStr="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";input=input.replace(/[^A-Za-z0-9\+\/\=]/g,"");while(i>4;chr2=(enc2&15)<<4|enc3>>2;chr3=(enc3&3)<<6|enc4;output=output+String.fromCharCode(chr1);if(enc3!=64){output=output+String.fromCharCode(chr2)}if(enc4!=64){output=output+String.fromCharCode(chr3)}}return output}}function getImageSize(imageArray){var segments=slice2Segments(imageArray);var seg,width,height,SOF=[192,193,194,195,197,198,199,201,202,203,205,206,207];for(var x=0;x=0){height=seg[5]*256+seg[6];width=seg[7]*256+seg[8];break}}return[width,height]}function pack(mark,array){if(!(array instanceof Array)){throw new Error("'pack' error. Got invalid type argument.")}if(mark.length-1!=array.length){throw new Error("'pack' error. "+(mark.length-1)+" marks, "+array.length+" elements.")}var littleEndian;if(mark[0]=="<"){littleEndian=true}else if(mark[0]==">"){littleEndian=false}else{throw new Error("")}var packed="";var p=1;var val=null;var c=null;var valStr=null;while(c=mark[p]){if(c.toLowerCase()=="b"){val=array[p-1];if(c=="b"&&val<0){val+=256}if(val>255||val<0){throw new Error("'pack' error.")}else{valStr=String.fromCharCode(val)}}else if(c=="H"){val=array[p-1];if(val>65535||val<0){throw new Error("'pack' error.")}else{valStr=String.fromCharCode(Math.floor(val%65536/256))+String.fromCharCode(val%256);if(littleEndian){valStr=valStr.split("").reverse().join("")}}}else if(c.toLowerCase()=="l"){val=array[p-1];if(c=="l"&&val<0){val+=4294967296}if(val>4294967295||val<0){throw new Error("'pack' error.")}else{valStr=String.fromCharCode(Math.floor(val/16777216))+String.fromCharCode(Math.floor(val%16777216/65536))+String.fromCharCode(Math.floor(val%65536/256))+String.fromCharCode(val%256);if(littleEndian){valStr=valStr.split("").reverse().join("")}}}else{throw new Error("'pack' error.")}packed+=valStr;p+=1}return packed}function unpack(mark,str){if(typeof str!="string"){throw new Error("'unpack' error. Got invalid type argument.")}var l=0;for(var markPointer=1;markPointer"){littleEndian=false}else{throw new Error("'unpack' error.")}var unpacked=[];var strPointer=0;var p=1;var val=null;var c=null;var length=null;var sliced="";while(c=mark[p]){if(c.toLowerCase()=="b"){length=1;sliced=str.slice(strPointer,strPointer+length);val=sliced.charCodeAt(0);if(c=="b"&&val>=128){val-=256}}else if(c=="H"){length=2;sliced=str.slice(strPointer,strPointer+length);if(littleEndian){sliced=sliced.split("").reverse().join("")}val=sliced.charCodeAt(0)*256+sliced.charCodeAt(1)}else if(c.toLowerCase()=="l"){length=4;sliced=str.slice(strPointer,strPointer+length);if(littleEndian){sliced=sliced.split("").reverse().join("")}val=sliced.charCodeAt(0)*16777216+sliced.charCodeAt(1)*65536+sliced.charCodeAt(2)*256+sliced.charCodeAt(3);if(c=="l"&&val>=2147483648){val-=4294967296}}else{throw new Error("'unpack' error. "+c)}unpacked.push(val);strPointer+=length;p+=1}return unpacked}function nStr(ch,num){var str="";for(var i=0;iH",data.slice(head+2,head+4))[0];var endPoint=head+length+2;segments.push(data.slice(head,endPoint));head=endPoint}if(head>=data.length){throw new Error("Wrong JPEG data.")}}return segments}function getExifSeg(segments){var seg;for(var i=0;i