Bitmap/Write a PPM file: Difference between revisions

Content added Content deleted
m (Changed "px" to "color" to match "bitmap.nim" interface.)
(Changed File to Stream which allows to use the module to write in a pipe.)
Line 1,312: Line 1,312:


== {{Header|Nim}} ==
== {{Header|Nim}} ==
<lang nim>
<lang nim>import bitmap
import bitmap
import streams


#---------------------------------------------------------------------------------------------------
proc writePPM*(img: Image, file: var File) =

## Write an image in a PPM file.
proc writePPM*(img: Image, stream: Stream) =
## Write an image to a PPM stream.


file.writeLine("P6 ", $img.w, " ", $img.h, " 255")
stream.writeLine("P6 ", $img.w, " ", $img.h, " 255")


for x, y in img.indices:
for x, y in img.indices:
file.write(chr(img[x, y].r))
stream.write(chr(img[x, y].r))
file.write(chr(img[x, y].g))
stream.write(chr(img[x, y].g))
file.write(chr(img[x, y].b))
stream.write(chr(img[x, y].b))


#---------------------------------------------------------------------------------------------------


proc writePPM*(img: Image; filename: string) =
proc writePPM*(img: Image; filename: string) =
## Write an image in a PPM file.
## Write an image in a PPM file.


var file = open(filename, fmWrite)
var file = openFileStream(filename, fmWrite)
img.writePPM(file)
img.writePPM(file)
file.close()
file.close()


#———————————————————————————————————————————————————————————————————————————————————————————————————


when isMainModule:
when isMainModule:
Line 1,343: Line 1,347:
for col in 0..<image.w:
for col in 0..<image.w:
image[col, row] = color(0, 0, 255)
image[col, row] = color(0, 0, 255)
image.writePPM("output.ppm")
image.writePPM("output.ppm")</lang>
</lang>


== {{Header|OCaml}} ==
== {{Header|OCaml}} ==