TinyChan

New reply in topic: Random discussions thread

You are not recognized as the original poster of this topic.

:

You are required to fill in a captcha for your first 5 posts. Sorry, but this is required to stop people from posting while drunk. Please be responsible and don't drink and post!
If you receive this often, consider not clearing your cookies.

Please familiarise yourself with the rules and markup syntax before posting.


Replying to Anonymous Z-7…

@656,037
I also remember the thread /λ/14081 on Lainchan, https://jsfiddle.net/hasbdw8g/:
<!DOCTYPE html><head><meta charset="utf-8"><title>Raw data to image conversion and vice versa</title><style>body{font-family:Sans-Serif;}::-webkit-file-upload-button{background-color:#fff;color:#000;border:1px solid #ccc;border-radius:4px;}</style></head><body><fieldset><legend>Type:</legend><input type="radio" name="type" value="encode" checked>Encode<br><input type="radio" name="type" value="decode">Decode to file<br><input type="radio" name="type" value="decode-img">Decode to image<br><input type="radio" name="type" value="decode-txt">Decode to text<br><input type="radio" name="type" value="hex">Hex dump<br></fieldset><br><p>You can also paste files.</p><label for="file">Your file:</label><input type="file" id="file"><br><div id="result" style="padding:4px;"><p style="color:#888;">Result appears here.</p></div><script>function removeChildren(e){for(;e.lastChild;)e.removeChild(e.lastChild)}function saveAs(e,t){var a=window.URL.createObjectURL(e),n=document.createElement("a");n.style="display: none",n.href=a,n.download=t,document.body.appendChild(n),n.click(),document.body.removeChild(n),setTimeout(function(){window.URL.revokeObjectURL(a)},1e3)}var fileInput=document.getElementById("file"),getType=()=>document.querySelector('input[name="type"]:checked').value,result=document.getElementById("result");function textToResult(e){var t=document.createElement("pre");t.appendChild(document.createTextNode(e)),result.appendChild(t)}function readBytes(e){return new Promise(t=>{var a=new FileReader;a.onload=(()=>{t(new Uint8Array(a.result))}),a.readAsArrayBuffer(e)})}function readAsDataURL(e){return new Promise(t=>{var a=new FileReader;a.onload=(()=>{t(a.result)}),a.readAsDataURL(e)})}const LAST_PIXEL=[[255,0,0],[255,255,0],[255,255,255]];function encode(e){var t=document.createElement("canvas"),a=t.getContext("2d"),n=Math.ceil(e.length/3)+1,r=Math.ceil(Math.sqrt(n)),o=Math.ceil(n/r);t.width=r,t.height=o;for(var d,l=a.getImageData(0,0,t.width,t.height),i=0;i<e.length;i+=3)d=i/3*4,l.data[d]=e[i],l.data[d+1]=e[i+1],l.data[d+2]=e[i+2],l.data[d+3]=255;d=i/3*4;var c=e.length-i+3;c=LAST_PIXEL[(c+2)%3],l.data[d]=c[0],l.data[d+1]=c[1],l.data[d+2]=c[2],l.data[d+3]=255,a.putImageData(l,0,0);var s=new Image;return s.src=t.toDataURL(),s}function decode(e){var t=document.createElement("canvas"),a=t.getContext("2d");t.width=e.naturalWidth,t.height=e.naturalHeight,a.drawImage(e,0,0);var n,r,o=a.getImageData(0,0,t.width,t.height).data;e:for(var d=o.length-4;d>=0;d-=4)for(var l=0;l<LAST_PIXEL.length;l++)if(o[d]===LAST_PIXEL[l][0]&&o[d+1]===LAST_PIXEL[l][1]&&o[d+2]===LAST_PIXEL[l][2]&&o[d+3]>0){r=d,n=l,console.log(`lpi:${n}\nli:${r}`);break e}for(var i=new Uint8Array(3*(r/4-1)+n+1),c=0;c<r-4;c+=4){i[d=c/4*3]=o[c],i[d+1]=o[c+1],i[d+2]=o[c+2]}for(d=0;d<=n;d++)i[3*(r/4-1)+d]=o[r-4+d];return i}function nDigits(e,t=10){return Math.max(1,1+Math.floor(Math.log(e)/Math.log(t)))}function toHex(e){return e.toString(16).toUpperCase()}function hexDump(e,t=16){for(var a="",n=nDigits(Math.floor((e.length-1)/t)*t,16),r=0;r<e.length;r++)r%t==0&&(a+=toHex(r).padStart(n,0)+" | "),a+=toHex(e[r]).padStart(2,0)+((r+1)%t==0?"\n":" ");return a}async function operate(e){switch(getType()){case"encode":removeChildren(result),result.appendChild(encode(await readBytes(e)));break;case"decode":(t=new Image).onload=(()=>{saveAs(new Blob([decode(t)],{type:"octet/stream"}),"decoded.bin")}),t.src=await readAsDataURL(e);break;case"decode-img":removeChildren(result),(t=new Image).onload=(()=>{var e=new Image;e.src=URL.createObjectURL(new Blob([decode(t)],{type:"image"})),result.appendChild(e)}),t.src=await readAsDataURL(e);break;case"decode-txt":var t;removeChildren(result),(t=new Image).onload=(()=>{textToResult((new TextDecoder).decode(decode(t)))}),t.src=await readAsDataURL(e);break;case"hex":removeChildren(result),textToResult(hexDump(await readBytes(e)))}}fileInput.onclick=function(){this.value=null},fileInput.addEventListener("change",async e=>{1===fileInput.files.length?operate(fileInput.files[0]):console.error("Expected one file only.")}),document.onpaste=(e=>{var t=(e.clipboardData||e.originalEvent.clipboardData).items;console.log(JSON.stringify(t));for(var a=0;a<t.length;a++){var n=t[a];"file"===n.kind&&operate(n.getAsFile())}});</script></body>