example.k 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. \l png.k
  2. / [[https://github.com/mthom/scryer-prolog/tree/master/logo]]
  3. png:1:fn:"scryer.png"
  4. $[~png.ispng[png];`err"Not a png file: ", fn;]
  5. (w;h):*png.ihdr@png
  6. idat:png.decmp@png.idat@png
  7. data:png.unfilt[8](h,0N)#idat
  8. / mark each pixel as either bg color or not
  9. / extend each row to have length a multiple of 8
  10. bw:((0x5757d3d3dede~/:-2_'0N 8#)'data),\:0
  11. / collect this data as bytes
  12. / add a byte to the front to indicate no filtering
  13. bwdata:,/(`c$0),/:(`c$2/+0N 8#)'bw
  14. / build up a b/w png
  15. png0:png.PNGHDR
  16. / same dimensions, but now bit-depth 1, color-type 0
  17. png0,:png.mkchnk["IHDR";`c$,/((4#256)\'w,h),1 0 0 0 0]
  18. / "compress" at level 0
  19. png0,:png.mkchnk["IDAT";png.nocmp@bwdata]
  20. png0,:png.mkchnk["IEND";!0]
  21. "/tmp/bw.png" 1:png0
  22. / bottom image
  23. / [[https://www.w3.org/Graphics/PNG/Inline-img.html]]
  24. png:1:fn:"test.png"
  25. $[~png.ispng[png];`err"Not a png file: ",fn;]
  26. (w;h):*png.ihdr@png
  27. idat:png.decmp@png.idat@png
  28. / Pull out the interlaced images
  29. c:png.iszs[w;h]
  30. PIXELWIDTH:3
  31. ii:(-1_+\0,(*|c)+PIXELWIDTH**/c)_idat
  32. / Unfilter each separately
  33. rr:png.unfilt[PIXELWIDTH]'((*|c),\:0N)#'ii
  34. / interlace back into a single image with no filtering
  35. dd:,/(`c$0),/:,/'(0N,w)#((0N,PIXELWIDTH)#,/,/'rr)@<<,/w#'h#png.A7
  36. png0:png.PNGHDR
  37. png0,:png.mkchnk["IHDR";`c$,/((4#256)\'w,h),8 2 0 0 0]
  38. png0,:png.mkchnk["IDAT";png.cmp@dd]
  39. png0,:png.mkchnk["IEND";!0]
  40. "/tmp/testing.png" 1:png0