RosettaCodeData/Task/Bitmap-Write-a-PPM-file/Zkl/bitmap-write-a-ppm-file.zkl

18 lines
826 B
Plaintext

// convert Windows BMP (bit map) image to PPM
// Read BMP file
bmp:=File.stdin.read().howza(0); // BMP to memory (byte bucket), treat as bytes
_assert_(bmp[0]==0x42,"Stdin not a BMP file");
width:=bmp.toLittleEndian(18,2,False); height:=bmp.toLittleEndian(22,2,False); // signed
println(width," x ",height);
bmp.del(0,14 + bmp.toLittleEndian(14,2)); // get rid of header
// Write BMP to PPM image (in memory)
ppm:=Data(width*height*3 + 100); // sized byte bucket plus some header slop
ppm.write("P6\n#rosettacode BMP to PPM test\n%d %d\n255\n".fmt(width,height));
foreach y in ([height - 1 .. 0,-1]){ // BGR 1 byte each, image is stored upside down
bmp[y*width*3,width*3].pump(ppm,T(Void.Read,2),fcn(b,g,r){ return(r,g,b) });
}
File("foo.ppm","wb").write(ppm); // File.stdout isn't binary, let GC close file