Bitmap/Write a PPM file: Difference between revisions

m
m (→‎{{header|zkl}}: reformat)
Line 1,607:
=={{header|zkl}}==
<lang zkl>// convert Windows BMP (bit map) image to PPM
 
bmp:=File.stdin.read(); // BMP to memory
// Read BMP file
if(bmp[0]!=0x42) throw(Exception.TypeError("Not a 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
 
ppm:=Data(width*height*3 + 100); // createWrite 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]){ // BMP: BGR 1 byte each, image is stored upside down
bmp[y*width*3,width*3].pump(0,ppm,T(Void.Read,2),fcn(b,g,r){ return(r,g,b) });
}
 
Line 1,622 ⟶ 1,626:
<pre>
$ zkl bbb < lena.bmp
512 x 512
$ ls -l foo.ppm
-rw-r--r-- 1 craigd craigd 786476 Aug 30 01:31 foo.ppm
Anonymous user