Bitmap/Write a PPM file: Difference between revisions

Added an example; modified the way to write into file.
(Changes to work with version 1.2.x; added an overload function.)
(Added an example; modified the way to write into file.)
Line 1,317:
proc writePPM(img: Image, file: var File) =
## Write an image in a PPM file.
file.writeLine("P6 ", img.w, " ", img.h, " 255")
 
file.writeLine("P6 ", $img.w, " ", $img.h, " 255")
for x,y in img.indices:
 
file.writeBytes(img[x,y], 0, 3)
for x, y in img.indices:
file.writeByteswrite(chr(img[x, y], 0, 3.r))
file.write(chr(img[x, y].g))
file.write(chr(img[x, y].b))
 
 
proc writePPM(img: Image; filename: string) =
Line 1,327 ⟶ 1,330:
 
var file = open(filename, fmWrite)
fileimg.writePPM(imgfile)
file.close()
 
 
when isMainModule:
var image = initImage(100, 50)
image.fill(px(255, 0, 0))
for row in 10..20:
for col in 0..<image.w:
image[col, row] = px(0, 255, 0)
for row in 30..40:
for col in 0..<image.w:
image[col, row] = px(0, 0, 255)
image.writePPM("output.ppm")
</lang>
 
Anonymous user