praiadeseselle/wire/modules/Inputfield/InputfieldImage/piexif.min.js

1 line
38 KiB
JavaScript
Raw Normal View History

2022-11-05 18:32:48 +01:00
(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 zer