#!/usr/bin/env retro

This is a quick tool to generate an image.js from the ngaImage for use with the JavaScript and Typescript implementations of the VM.

First up, I need to load the image to a buffer, so I allocate space for this now.

~~~#65535 #4 * 'IMAGE-SIZE const 'Image d:create   IMAGE-SIZE allot ~~~

Next is reading in the file. This is slightly complicated by the need to pack the individual bytes into the memory cells.

So, a variable to track the open file ID.

~~~'FID var ~~~

Then read in a byte.

~~~:read-byte (n-)  @FID file:read #255 and ; ~~~

Reading in four bytes, I can shift and merge them back into a single cell.

~~~:read-cell (-n)   read-byte    read-byte    read-byte  read-byte   #-8 shift +  #-8 shift +  #-8 shift + ; ~~~

The next step is a word to return the size of the file in cells.

~~~:size (-n) @FID file:size #4 / ; ~~~

And then, using the above, read in the data from the file to the image buffer.

~~~:load-image (s-)   file:R file:open !FID   &Image size [ read-cell over store n:inc ] times drop   @FID file:close ; ~~~

Read in the file.

~~~'ngaImage load-image ~~~

The final part is to export the image as a C array. To keep line length to a reasonible length, I have a counter and add a newline after 18 values.

~~~'Count var :EOL? &Count v:inc @Count #18 eq? [ nl #0 !Count ] if ; ~~~

The rest is easy. Display the relevant header bits, then the cells, then the needed footer.

~~~'var_ngaImage_=_[ s:put nl   &Image dup #3 + fetch [ fetch-next n:put $, c:put EOL? ] times nl drop   ']; s:put nl ~~~