IWBH_01

file encode as image pixels

Sep 5th, 2021 (edited)
1,353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. DataView.prototype.getString=function(bi,L){
  2.  var s="";
  3.  while(bi!=L){
  4.   s+=String.fromCharCode(this.getUint8(bi));
  5.   bi++;
  6.  }
  7.  return s;
  8. };
  9.  
  10. DataView.prototype.setString=function(bi,s){
  11.  var L=s.length,i=0;
  12.  while(i!=L){
  13.   this.setUint8(bi+i,s.charCodeAt(i));
  14.   i++;
  15.  }
  16. };
  17.  
  18.  
  19. /*
  20.  
  21. self.i2=document.createElement("img");
  22. document.body.appendChild(i2);
  23. self.v2=document.createElement("video");v2.controls=!0; document.body.appendChild(v2);
  24.  
  25.  
  26. finp=document.querySelector("input[type=file]");
  27. fi=finp.files[0];
  28. fi.arrayBuffer().then(function(a){ self.file_dv1=new DataView(a); });
  29.  
  30.  
  31.  
  32. file_as_png(file_dv1,{"type":fi.type,"name":fi.name},function(r){ self.ri2=r; });
  33.  
  34. URL.revokeObjectURL(i2.src);
  35. i2.src=URL.createObjectURL(ri2);
  36.  
  37.  
  38. self.rez7=open_png_filed(i2);
  39. URL.revokeObjectURL(v2.src);
  40.  
  41. self.b2=new Blob([rez7[0]],{"type":rez7[1].type||"video/mp4"});
  42. v2.src=URL.createObjectURL(b2);
  43.  
  44.  
  45. */
  46.  
  47.  
  48. self.file_as_png=function(file_dv,meta,cb){
  49. var cnvs=document.createElement("canvas"),
  50. bL=file_dv.byteLength,ch=Math.ceil(bL/(2400)),df=(ch*2400)-bL,
  51. ee=new ImageData(800,ch);
  52. cnvs.width=800;
  53. cnvs.height=ch+1; //extra line at the end for metadata
  54. contxt=cnvs.getContext("2d");
  55.  
  56. //to image
  57. var i=0,i2=2, eda=new DataView(ee.data.buffer);
  58.  
  59. eda.setUint16(0,df);
  60.  
  61. while(i<bL){
  62.  if((i2&3)==3){ eda.setUint8(i2,255); i2++; }
  63.  eda.setUint8(i2,file_dv.getUint8(i));
  64.  i++; i2++;
  65. }
  66.  
  67. //meta
  68. if(meta){
  69. var mds=JSON.stringify(meta /*{"type":"video/mp4","name":"a_file.mp4"}*/)+";\n",mdL=mds.length;
  70. if((i2&3)==3){ eda.setUint8(i2,255); i2++; }
  71. eda.setUint16(i2,mdL); i2+=2;
  72.  //metadata size limit 2390 bytes
  73.  i=0;
  74. while(i<=mdL){
  75.  if((i2&3)==3){ eda.setUint8(i2,255); i2++; }
  76.  eda.setUint8(i2,mds.charCodeAt(i)||255);
  77.  i++; i2++;
  78. }
  79. }
  80.  
  81. if((i2&3)==3){ eda.setUint8(i2,255); i2++; }
  82.  
  83.  
  84. contxt.putImageData(ee,0,0);
  85.  
  86. cnvs.toBlob(function(a){ self.blb1=a; if(typeof cb=="function")cb(a); });
  87. };
  88.  
  89.  
  90.  
  91.  
  92.  
  93. //from image
  94.  
  95. self.open_png_filed=function(i1){ //input image element
  96. var cnvs=document.createElement("canvas");
  97. cnvs.height=i1.naturalHeight;
  98. cnvs.width=i1.naturalWidth;
  99.  
  100.  
  101. ctx=cnvs.getContext("2d");
  102. ctx.drawImage(i1,0,0);
  103. var dat=ctx.getImageData(0,0,cnvs.width,cnvs.height);
  104.  
  105. var eda=new DataView(dat.data.buffer), df=eda.getUint16(0),bL=(cnvs.width*3*(cnvs.height-1))-df;
  106.  
  107. var file_dv=new DataView(new ArrayBuffer(bL));
  108.  
  109.  
  110.  
  111.  
  112. var i=0, i2=2;
  113.  
  114. while(i<bL){
  115.  if((i2&3)==3) i2++;
  116.  file_dv.setUint8(i,eda.getUint8(i2));
  117.  i++; i2++;
  118. }
  119.  
  120. //meta
  121. var mda={};
  122. if((i2&3)==3) i2++;
  123. var mdL=eda.getUint16(i2); i2+=2;
  124. //console.log(i); self.eda1=eda; //debuging line
  125. var mds="";
  126.  
  127. if(mdL<2390){
  128.  i=mdL;
  129. while(i--){
  130.  if((i2&3)==3) i2++;
  131.  mds+=String.fromCharCode(eda.getUint8(i2));
  132.  i2++;
  133. }
  134.  console.log(mds);
  135.  try{ mda=JSON.parse(mds.substr(0,mds.lastIndexOf("}")+1)); }catch(e){}
  136. } //metadata size limit 2390 bytes
  137.  
  138. return [file_dv,mda];
  139.  
  140. };
  141.  
Add Comment
Please, Sign In to add comment