Skip to content

Instantly share code, notes, and snippets.

@sudonhim
Created August 9, 2019 21:31
Show Gist options
  • Save sudonhim/3a212f5995e608d3fbadbe0672ac7e89 to your computer and use it in GitHub Desktop.
Save sudonhim/3a212f5995e608d3fbadbe0672ac7e89 to your computer and use it in GitHub Desktop.
|= dim=@ud
^- @ux
:: Pixel bytes for 24bpp dim*dim image
:: Note: this calculation does not support padding, so dim%4 must be 0
::
=/ datasize=@ (mul 3 (mul dim dim))
:: The header is a list of [bytes, value] to be composed into a single blob
::
=/ header=@
%+ can 3
^- (list [@ @])
:~
:: BMP header part, 14 bytes
::
[2 'BM'] :: format ID
[4 (add 54 datasize)] :: file size - 54 byte header + pixel data
[4 0] :: unused
[4 54] :: header size
:: DIB header part, 4 bytes
::
[4 40] :: DIB header size
[4 dim] :: width
[4 dim] :: height
[2 3] :: number of channels
[2 24] :: bits per pixel
[4 0] :: compression (none; BI_RGB)
[4 datasize] :: pixel data size
[4 2.835] :: print resolution x (72 DPI)
[4 2.835] :: print resolution y (72 DPI)
[8 0] :: palette stuff (unused)
==
=/ image=@
%+ rap 3
=/ py=@ 0
|-
^- (list @)
=/ px=@ 0
|-
^- (list @)
:: Define the image as a BGR888 pixel in terms of x and y
::
:- (add 128 (div (mul px 256) (mul 2 dim)))
:- 255
:- 255
:: Recurse for all pixels
::
?: (lth +(px) dim)
$(px +(px))
?: (lth +(py) dim)
^$(py +(py))
~
%+ can 3
^- (list [@ @])
:~
[54 header]
[datasize image]
==
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment