Using the data storage type defined on this page for raster images, write the image to a PPM file (binary P6 prefered).

(Read the definition of PPM file on Wikipedia.)




#ESCAPECHARS ON
 
DIM bmpin = ".\\LenaClr.bmp", ppmout = ".\\Lena.ppm", bmpblob = 54 ' Size of BMP file headers
FILEGET(FILEOPEN(bmpin, BINARY), FILELEN(bmpin)): FILECLOSE(FILEOPEN) ' Fill buffer
 
DIM ppmheader AS STRING * 256, breadth, height
LET(breadth, height) = 128 ' Image width and height
SPRINTF(ppmheader, "P6\n%d %d\n255\n", breadth, height) ' Create PPM file header
 
DIM ppmdata AS STRING * (STRLEN(ppmheader) + FILELEN - bmpblob)
DIM head = @ppmdata + STRLEN, tail = @FILEGET + FILELEN - breadth * 3 - 2 ' Start of last scanline
ppmdata = ppmheader ' Copy PPM file header
 
WHILE tail >= @FILEGET + bmpblob ' Flip upside down
	FOR DIM w = 0 TO (breadth - 1) * 3 STEP 3
		POKE(head + 0 + w, CHR(PEEK(tail + 2 + w, 1))) ' Swap R
		POKE(head + 1 + w, CHR(PEEK(tail + 1 + w, 1))) ' Keep G
		POKE(head + 2 + w, CHR(PEEK(tail + 0 + w, 1))) ' Swap B
	NEXT
	INCR(head, breadth * 3): DECR(tail, breadth * 3) ' Next scanline
WEND
 
FILEPUT(FILEOPEN(ppmout, BINARY_NEW), ppmdata): FILECLOSE(FILEOPEN)