0

I am currently trying to load a .bmp image file with Deno and then convert the data of the image into a black/white Uint8Array.

I load the image using:

const imageFile = await Deno.readFile('./path/to/my/image.bmp');

This returns me a Uint8Array, but this is not only the image itself, it contains also other data, like the image header or other metadata.

I will try to explain what I would like to do with an example. I have the following bitmap image file. Each field represents one pixel:

enter image description here 4x4 bitmap

This bitmap would then output the following Uint8Array to me:

// hex
[ 0x5A, 0x5A ]

// binary
[ 0b01011010, 0b01011010 ]

// visual
[
  0101
  1010,
  0101
  1010
]

Is it possible to achieve this using Deno? Any suggestion is appreciated! Thanks!

~Mqx

Mqx
  • 182
  • 7
  • Have you looked at the PNG spec? It has everything you need: https://www.w3.org/TR/2003/REC-PNG-20031110/ – jsejcksn Mar 03 '23 at 20:44
  • [^](https://stackoverflow.com/questions/75626699/deno-convert-bmp-image-file-data-into-a-uint8array?noredirect=1#comment133432552_75626699) @Mqx My mistake — I must have misread your question because I linked you to PNG information, but you asked about BMP: Here are some analogous resources related to that format: [Wikipedia](https://en.wikipedia.org/wiki/BMP_file_format), [Microsoft Learn](https://learn.microsoft.com/en-us/search/?terms=bmp), [BMP file format - University of Alberta](https://www.ece.ualberta.ca/~elliott/ee552/studentAppNotes/2003_w/misc/bmp_file_format/bmp_file_format.htm) – jsejcksn Mar 03 '23 at 23:49
  • @jsejcksn I have updated the post. The problem ist that the `file-header`/`bitmap-header` do not match at all. – Mqx Mar 06 '23 at 08:32
  • @jsejcksn my mistake. I used an online converter which did not seem to work properly. – Mqx Mar 06 '23 at 09:11
  • Here are two more decoding resources: https://github.com/LinusU/decode-bmp, https://github.com/josephrocca/wasm-image-decoder, and I've [answered previously](https://stackoverflow.com/a/73211022/438273) about RGBA transforms in a worker. – jsejcksn Mar 07 '23 at 00:36
  • @jsejcksn sadly the [second lib](https://github.com/josephrocca/wasm-image-decoder/issues/6) throws an error when trying to decode an image. – Mqx Mar 07 '23 at 08:13

0 Answers0