September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -17,7 +17,7 @@ const proc: writePPM (in string: fileName, in PRIMITIVE_WINDOW: aWindow) is func
|
|||
for y range 0 to pred(height(aWindow)) do
|
||||
for x range 0 to pred(width(aWindow)) do
|
||||
pixColor := getPixelColor(aWindow, x, y);
|
||||
write(ppmFile, str(chr(pixColor.red_part)) <& chr(pixColor.green_part) <& chr(pixColor.blue_part));
|
||||
write(ppmFile, str(chr(pixColor.redLight)) <& chr(pixColor.greenLight) <& chr(pixColor.blueLight));
|
||||
end for;
|
||||
end for;
|
||||
close(ppmFile);
|
||||
|
|
|
|||
|
|
@ -1,24 +1,24 @@
|
|||
subset Int < Number {|n| n.is_int }
|
||||
subset Uint < Int {|n| n >= 0 }
|
||||
subset Uint8 < Int {|n| n ~~ ^256 }
|
||||
subset UInt < Int {|n| n >= 0 }
|
||||
subset UInt8 < Int {|n| n ~~ ^256 }
|
||||
|
||||
struct Pixel {
|
||||
R < Uint8,
|
||||
G < Uint8,
|
||||
B < Uint8
|
||||
R < UInt8,
|
||||
G < UInt8,
|
||||
B < UInt8
|
||||
}
|
||||
|
||||
class Bitmap(width < Uint, height < Uint) {
|
||||
class Bitmap(width < UInt, height < UInt) {
|
||||
has data = []
|
||||
|
||||
method fill(Pixel p) {
|
||||
data = (width*height -> of { Pixel(p.R, p.G, p.B) })
|
||||
}
|
||||
|
||||
method setpixel(i < Uint, j < Uint, Pixel p) {
|
||||
method setpixel(i < UInt, j < UInt, Pixel p) {
|
||||
|
||||
subset WidthLimit < Uint { |n| n ~~ ^width }
|
||||
subset HeightLimit < Uint { |n| n ~~ ^height }
|
||||
subset WidthLimit < UInt { |n| n ~~ ^width }
|
||||
subset HeightLimit < UInt { |n| n ~~ ^height }
|
||||
|
||||
func (w < WidthLimit, h < HeightLimit) {
|
||||
data[w*height + h] = p
|
||||
|
|
@ -26,8 +26,11 @@ class Bitmap(width < Uint, height < Uint) {
|
|||
}
|
||||
|
||||
method p6 {
|
||||
"P6\n#{width} #{height}\n255\n" +
|
||||
data.map {|p| [p.R, p.G, p.B].pack('C3') }.join
|
||||
<<-EOT + data.map {|p| [p.R, p.G, p.B].pack('C3') }.join
|
||||
P6
|
||||
#{width} #{height}
|
||||
255
|
||||
EOT
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -37,7 +40,4 @@ for i,j in (^b.height ~X ^b.width) {
|
|||
b.setpixel(i, j, Pixel(2*i, 2*j, 255 - 2*i))
|
||||
}
|
||||
|
||||
var file = File("palette.ppm")
|
||||
var fh = file.open('>:raw')
|
||||
fh.print(b.p6)
|
||||
fh.close
|
||||
%f"palette.ppm".write(b.p6, :raw)
|
||||
|
|
|
|||
17
Task/Bitmap-Write-a-PPM-file/Zkl/bitmap-write-a-ppm-file.zkl
Normal file
17
Task/Bitmap-Write-a-PPM-file/Zkl/bitmap-write-a-ppm-file.zkl
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
// 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue