png.k 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. \d png
  2. PNGHDR:0x89504e470d0a1a0a
  3. A7:(0 5 3 5 1 5 3 5
  4. 6 6 6 6 6 6 6 6
  5. 4 5 4 5 4 5 4 5
  6. 6 6 6 6 6 6 6 6
  7. 2 5 3 5 2 5 3 5
  8. 6 6 6 6 6 6 6 6
  9. 4 5 4 5 4 5 4 5
  10. 6 6 6 6 6 6 6 6)
  11. / shell out for bit-oriented algos
  12. decmp:{`x(("./png.py";"--decompress");x)}
  13. cmp:{`x(("./png.py";"--compress");x)}
  14. crc:{`c$256\.`x(("./png.py";"--crc");x)}
  15. / Basic chunk deciphering
  16. ispng:{PNGHDR~8#x}
  17. chunk:{(ln;nm):2 4#@[x;y+!8];ln:256/+/1(256*0>)\ln;(ln;nm)}
  18. chnks:{+1_*+(~"IEND"~*|*:){(ln;nm):chunk[x;*|y];((ln;nm);ln+12+*|y)}[x]\(();8)}
  19. data:{(ls;ns):chnks x;+(ns;+/(8+-1_+\8,12+ls;!'ls))}
  20. idat:{x@,/(*|)'("IDAT"~/:*:')#data@x}
  21. ihdr:{(w;h;f):256!0 4 8_x@16+!13;(256/+(w;h);f)}
  22. / filter routines
  23. paeth:{(x;y;z)@*<{x|-x}((x+y)-z)-/:(x;y;z)}
  24. unnone:{z}
  25. unsub:{,/+\(0N,x)#z}
  26. unup:{y+z}
  27. unavg:{x_{[x;a;b;c]a,256!c+-2!b+a@(#a)-x}[x]/[x#0;y;z]}
  28. unpaeth:{x_{[x;a;b;c;d];a,256!d+paeth.(a@(#a)-x;b;c)}[x]/[x#0;y;(-x)_(x#0),y;z]}
  29. unfilt:{`c${(t;r):0 1_z;256!(*(unnone;unsub;unup;unavg;unpaeth)t)[x;y;r]}[x]\[(-1+#*y)#0;256!y]}
  30. / sizes of interleaved images
  31. iszs0:{(-8!({x,y}/#''='x)*((!7)!7-(|/,/&')'(!7)=\:x)+y)}
  32. iszs:{[w;h](iszs0.'+((A7;+A7);(w;h)))@\:!7}
  33. / Manual "compression". (Just use tools above.)
  34. (CMF;FLG):120 1
  35. / No compression [[https://datatracker.ietf.org/doc/html/rfc1950]]
  36. nozip:{`c$CMF,FLG,,/,'/|1(1_|128,#/|0,#:)\,'/|1(,'/2/'''0N 8#/:/:1~:\+(16#2)\#:')\0N 65535#x}
  37. / [[https://datatracker.ietf.org/doc/html/rfc1950#section-9]]
  38. ad32:{b:+/a:(1+\256!x);`c$2/'0N 8#,/(16#2)\'65521!/:(b;*|a)}
  39. nocmp:{,/(nozip;ad32)@\:x}
  40. / Manual crc32 algo (Just use tools above.)
  41. / [[http://www.libpng.org/pub/png/spec/iso/index-object.html#D-CRCAppendix]]
  42. P:,/+(8#2)\256!0xedb88320
  43. crctbl:(8{$[*|x;~P=-1_0,x;-1_0,x]}/(32#2)\)'!256
  44. /crc:{`c$256\2/~{~crctbl[2/-8#~x=(32#2)\y]=-8_(8#0),x}/[32#1;256!x]}
  45. mkchnk:{(`c$(4#256)\#y),,/1 crc\x,y}
  46. \d .
  47. /
  48. SPEC: https://www.w3.org/TR/png/