Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DataView.prototype.getString=function(bi,L){
- var s="";
- while(bi!=L){
- s+=String.fromCharCode(this.getUint8(bi));
- bi++;
- }
- return s;
- };
- DataView.prototype.setString=function(bi,s){
- var L=s.length,i=0;
- while(i!=L){
- this.setUint8(bi+i,s.charCodeAt(i));
- i++;
- }
- };
- /*
- self.i2=document.createElement("img");
- document.body.appendChild(i2);
- self.v2=document.createElement("video");v2.controls=!0; document.body.appendChild(v2);
- finp=document.querySelector("input[type=file]");
- fi=finp.files[0];
- fi.arrayBuffer().then(function(a){ self.file_dv1=new DataView(a); });
- file_as_png(file_dv1,{"type":fi.type,"name":fi.name},function(r){ self.ri2=r; });
- URL.revokeObjectURL(i2.src);
- i2.src=URL.createObjectURL(ri2);
- self.rez7=open_png_filed(i2);
- URL.revokeObjectURL(v2.src);
- self.b2=new Blob([rez7[0]],{"type":rez7[1].type||"video/mp4"});
- v2.src=URL.createObjectURL(b2);
- */
- self.file_as_png=function(file_dv,meta,cb){
- var cnvs=document.createElement("canvas"),
- bL=file_dv.byteLength,ch=Math.ceil(bL/(2400)),df=(ch*2400)-bL,
- ee=new ImageData(800,ch);
- cnvs.width=800;
- cnvs.height=ch+1; //extra line at the end for metadata
- contxt=cnvs.getContext("2d");
- //to image
- var i=0,i2=2, eda=new DataView(ee.data.buffer);
- eda.setUint16(0,df);
- while(i<bL){
- if((i2&3)==3){ eda.setUint8(i2,255); i2++; }
- eda.setUint8(i2,file_dv.getUint8(i));
- i++; i2++;
- }
- //meta
- if(meta){
- var mds=JSON.stringify(meta /*{"type":"video/mp4","name":"a_file.mp4"}*/)+";\n",mdL=mds.length;
- if((i2&3)==3){ eda.setUint8(i2,255); i2++; }
- eda.setUint16(i2,mdL); i2+=2;
- //metadata size limit 2390 bytes
- i=0;
- while(i<=mdL){
- if((i2&3)==3){ eda.setUint8(i2,255); i2++; }
- eda.setUint8(i2,mds.charCodeAt(i)||255);
- i++; i2++;
- }
- }
- if((i2&3)==3){ eda.setUint8(i2,255); i2++; }
- contxt.putImageData(ee,0,0);
- cnvs.toBlob(function(a){ self.blb1=a; if(typeof cb=="function")cb(a); });
- };
- //from image
- self.open_png_filed=function(i1){ //input image element
- var cnvs=document.createElement("canvas");
- cnvs.height=i1.naturalHeight;
- cnvs.width=i1.naturalWidth;
- ctx=cnvs.getContext("2d");
- ctx.drawImage(i1,0,0);
- var dat=ctx.getImageData(0,0,cnvs.width,cnvs.height);
- var eda=new DataView(dat.data.buffer), df=eda.getUint16(0),bL=(cnvs.width*3*(cnvs.height-1))-df;
- var file_dv=new DataView(new ArrayBuffer(bL));
- var i=0, i2=2;
- while(i<bL){
- if((i2&3)==3) i2++;
- file_dv.setUint8(i,eda.getUint8(i2));
- i++; i2++;
- }
- //meta
- var mda={};
- if((i2&3)==3) i2++;
- var mdL=eda.getUint16(i2); i2+=2;
- //console.log(i); self.eda1=eda; //debuging line
- var mds="";
- if(mdL<2390){
- i=mdL;
- while(i--){
- if((i2&3)==3) i2++;
- mds+=String.fromCharCode(eda.getUint8(i2));
- i2++;
- }
- console.log(mds);
- try{ mda=JSON.parse(mds.substr(0,mds.lastIndexOf("}")+1)); }catch(e){}
- } //metadata size limit 2390 bytes
- return [file_dv,mda];
- };
Add Comment
Please, Sign In to add comment