Bitmap/Write a PPM file: Difference between revisions

→‎Ada: Fixed tipo an better reference
m (syntax highlighting fixup automation)
(→‎Ada: Fixed tipo an better reference)
Tags: Mobile edit Mobile web edit
(11 intermediate revisions by 6 users not shown)
Line 1:
[[Category:Input Output]]
{{task|Raster graphics operations}}
[[Category:Input Output]]
 
Using the data storage type defined [[Basic_bitmap_storage|on this page]] for raster images, write the image to a PPM file (binary P6 preferedpreferred).
 
(Read [[wp:Netpbm_format|the definition of PPM file]] on Wikipedia.)
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">T Colour
Byte r, g, b
 
Line 77 ⟶ 76:
print(bitmap.writeppmp3())
 
File(‘tmp.ppm’, ‘w’WRITE).write_bytes(bitmap.writeppmp6())</syntaxhighlight>
 
{{out}}
Line 93 ⟶ 92:
=={{header|Action!}}==
{{libheader|Action! Bitmap tools}}
<syntaxhighlight lang=Action"action!">INCLUDE "H6:RGBIMAGE.ACT" ;from task Bitmap
 
PROC SaveHeader(RgbImage POINTER img
Line 202 ⟶ 201:
63 31 127 127 31 63 127 63 31
</pre>
 
=={{header|Ada}}==
<syntaxhighlight lang="ada">with Ada.Characters.Latin_1;
with Ada.Streams.Stream_IO; use Ada.Streams.Stream_IO;
 
with Bitmap_Store; use Bitmap_Store;
-- This package is defined in the Bitmap task.
 
procedure Put_PPM (File : File_Type; Picture : Image) is
Line 233 ⟶ 234:
 
=={{header|Aime}}==
<syntaxhighlight lang="aime">integer i, h, j, w;
file f;
 
Line 250 ⟶ 251:
} while ((i += 16) < w);
} while ((j += 1) < h);</syntaxhighlight>
 
=={{header|Applesoft BASIC}}==
<syntaxhighlight lang="gwbasic"> 100 W = 8
110 H = 8
120 BA = 24576
Line 270:
500 R = C - INT (C / 256) * 256:B = INT (C / 65536):G = INT (C / 256) - B * 256:A = BB + X * 3 + Y * W * 3: POKE A,R: POKE A + 1,G: POKE A + 2,B: RETURN
600 FOR Y = 0 TO H - 1: FOR X = 0 TO W - 1: GOSUB 500: NEXT X,Y: RETURN</syntaxhighlight>
=={{header|ATS}}==
For this code you will also need <code>bitmap_task.sats</code> and <code>bitmap_task.dats</code> from [[Bitmap#ATS]].
 
The static file provides templates for writing a PPM in either raw or plain format, regardless of what type you use to represent a pixel. The dynamic file, however, provides implementations ''only'' for the <code>rgb24</code> type defined in <code>bitmap_task.sats</code>.
 
===The ATS static file===
The following interface file should be named <code>bitmap_write_ppm_task.sats</code>.
<syntaxhighlight lang="ats">
#define ATS_PACKNAME "Rosetta_Code.bitmap_write_ppm_task"
 
staload "bitmap_task.sats"
 
(* Only pixmaps with positive width and height (pixmap1) are accepted
for writing a PPM. *)
 
fn {a : t@ype}
pixmap_write_ppm_raw_or_plain
(outf : FILEref,
pix : !pixmap1 a,
plain : bool)
: bool (* success *)
 
fn {a : t@ype}
pixmap_write_ppm_raw
(outf : FILEref,
pix : !pixmap1 a)
: bool (* success *)
 
overload pixmap_write_ppm with pixmap_write_ppm_raw_or_plain
overload pixmap_write_ppm with pixmap_write_ppm_raw
</syntaxhighlight>
 
===The ATS dynamic file===
The following file of implementations should be named <code>bitmap_write_ppm_task.dats</code>.
<syntaxhighlight lang="ats">
(*------------------------------------------------------------------*)
 
#define ATS_DYNLOADFLAG 0
#define ATS_PACKNAME "Rosetta_Code.bitmap_write_ppm_task"
 
#include "share/atspre_staload.hats"
 
staload "bitmap_task.sats"
 
(* You need to staload bitmap_task.dats, so the ATS compiler will have
access to its implementations of templates. But we staload it
anonymously, so the programmer will not have access. *)
staload _ = "bitmap_task.dats"
 
staload "bitmap_write_ppm_task.sats"
 
(*------------------------------------------------------------------*)
 
(* Realizing that MAXVAL, and how to represent depend on the
pixel type, we implement the template functions ONLY for pixels of
type rgb24. *)
 
(* We will implement raw PPM using "dump", and plain PPM using the
"get a pixel" square brackets. The latter method is simpler than
writing a different implementation of pixmap$pixels_dump<rgb24>,
and also helps us satisfy the stated requirements of the task.
("Dump" goes beyond what was asked for.) *)
 
implement
pixmap_write_ppm_raw_or_plain<rgb24> (outf, pix, plain) =
begin
fprintln! (outf, (if plain then "P3" else "P6") : string);
fprintln! (outf, width pix, " ", height pix);
fprintln! (outf, "255");
if ~plain then
dump<rgb24> (outf, pix)
else
let
val w = width pix and h = height pix
prval [w : int] EQINT () = eqint_make_guint w
prval [h : int] EQINT () = eqint_make_guint h
 
fun
loop {x, y : nat | x <= w; y <= h}
.<h - y, w - x>.
(pix : !pixmap (rgb24, w, h),
x : size_t x,
y : size_t y)
: void =
if y = h then
()
else if x = w then
loop (pix, i2sz 0, succ y)
else
let
val @(r, g, b) = rgb24_values pix[x, y]
in
fprintln! (outf, r, " ", g, " ", b);
loop (pix, succ x, y)
end
in
loop (pix, i2sz 0, i2sz 0);
true
end
end
 
implement
pixmap_write_ppm_raw<rgb24> (outf, pix) =
pixmap_write_ppm_raw_or_plain<rgb24> (outf, pix, false)
 
(*------------------------------------------------------------------*)
 
#ifdef BITMAP_WRITE_PPM_TASK_TEST #then
 
implement
main0 () =
let
val bgcolor = rgb24_make (217u, 217u, 214u)
and fgcolor1 = rgb24_make (210, 0, 0)
and fgcolor2 = rgb24_make (0, 150, 0)
and fgcolor3 = rgb24_make (0, 0, 220)
 
stadef w = 300
stadef h = 200
val w : size_t w = i2sz 300
and h : size_t h = i2sz 200
 
val @(pfgc | pix) = pixmap_make<rgb24> (w, h, bgcolor)
val () =
let
var x : Size_t
in
for* {x : nat | x <= w}
.<w - x>.
(x : size_t x) =>
(x := i2sz 0; x <> w; x := succ x)
begin
pix[x, i2sz 50] := fgcolor1;
pix[x, i2sz 100] := fgcolor2;
pix[x, i2sz 150] := fgcolor3
end
end
 
val outf_raw = fileref_open_exn ("image-raw.ppm", file_mode_w)
and outf_plain = fileref_open_exn ("image-plain.ppm", file_mode_w)
 
val success = pixmap_write_ppm<rgb24> (outf_raw, pix)
val () = assertloc success
val success = pixmap_write_ppm<rgb24> (outf_plain, pix, true)
val () = assertloc success
in
fileref_close outf_raw;
fileref_close outf_plain;
free (pfgc | pix)
end
 
#endif
 
(*------------------------------------------------------------------*)
</syntaxhighlight>
 
There is a test program that you can compile and run thus:
<pre>$ patscc -std=gnu2x -g -O2 -DATS_MEMALLOC_LIBC -DATS BITMAP_WRITE_PPM_TASK_TEST bitmap_{,write_ppm_}task.{s,d}ats
$ ./a.out
</pre>
If everything worked, you should end up with two image files, <code>image-raw.ppm</code> and <code>image-plain.ppm</code>. The former will have been made with the "dump" functionality that outputs the raw pixel data in one call to <code>fwrite(3)</code>. The latter will have been written more in the way the task assumes: reading pixels individually, left-to-right and top-to-bottom.
 
The images should appear thus:
 
[[File:Bitmap write ppm task ATS.png|alt=A gray background with red, green, and blue horizontal stripes, one pixel thick each, evenly placed, top to bottom.]]
 
=={{header|AutoHotkey}}==
{{works with|AutoHotkey_L|45}}
<syntaxhighlight lang=AutoHotkey"autohotkey">
cyan := color(0,255,255) ; r,g,b
cyanppm := Bitmap(10, 10, cyan) ; width, height, background-color
Line 307 ⟶ 473:
 
=={{header|AWK}}==
<syntaxhighlight lang=AWK"awk">#!/usr/bin/awk -f
BEGIN {
split("255,0,0,255,255,0",R,",");
Line 320 ⟶ 486:
close(outfile);
}</syntaxhighlight>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> Width% = 200
Height% = 200
Line 353 ⟶ 518:
SWAP ?^col%,?(^col%+2)
= col%</syntaxhighlight>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">header_ppm ← "P6
4 8
255
Line 371 ⟶ 535:
"small.ppm" •file.Bytes bytes_ppm</syntaxhighlight>
{{trans|C}}
<syntaxhighlight lang="bqn">header_ppm ← "P6
800 800
255
Line 377 ⟶ 541:
image_ppm ← @ + ⥊ > {256|𝕨‿𝕩‿(𝕨×𝕩)}⌜˜ ↕800
"first_bqn.ppm" •file.Bytes header_ppm ∾ image_ppm</syntaxhighlight>
 
=={{header|C}}==
 
This is one file program which writes one color in each step :
<syntaxhighlight lang="c">#include <stdlib.h>
#include <stdio.h>
 
Line 408 ⟶ 571:
This program writes whole array in one step :
 
<syntaxhighlight lang="c">#include <stdio.h>
 
int main()
Line 452 ⟶ 615:
Interface:
 
<syntaxhighlight lang="c">void output_ppm(FILE *fd, image img);</syntaxhighlight>
 
Implementation:
 
<syntaxhighlight lang="c">#include "imglib.h"
 
void output_ppm(FILE *fd, image img)
Line 466 ⟶ 629:
(void) fflush(fd);
}</syntaxhighlight>
 
=={{header|C sharp|C#}}==
This implementation uses a StreamWriter to write the header in text, then writes the pixel data in binary using a BinaryWriter.
<syntaxhighlight lang="csharp">using System;
using System.IO;
class PPMWriter
Line 494 ⟶ 656:
}
}</syntaxhighlight>
 
=={{header|C++}}==
{{trans|C}}
<syntaxhighlight lang="cpp">#include <fstream>
#include <cstdio>
 
int main() {
constexpr auto dimx = 800u, dimy = 800u;
 
std::ofstream ofs("first.ppm", ios_base::out | ios_base::binary);
using namespace std;
ofs << "P6\n" << dimx << ' ' << dimy << "\n255\n";
ofstream ofs("first.ppm", ios_base::out | ios_base::binary);
ofs << "P6" << endl << dimx << ' ' << dimy << endl << "255" << endl;
 
for (auto j = 0u; j < dimy; ++j)
for (auto i = 0u; i < dimx; ++i)
ofs << (char) (i % 256) static_cast<< (char) (j % 256) << (char) (>(i * j) % 256); // red, green, blue
<< static_cast<char>(j % 256)
 
<< static_cast<char>((i * j) % 256);
ofs.close();
 
return EXIT_SUCCESS;
}</syntaxhighlight>
 
=={{header|Common Lisp}}==
 
<syntaxhighlight lang="lisp">(defun write-rgb-buffer-to-ppm-file (filename buffer)
(with-open-file (stream filename
:element-type '(unsigned-byte 8)
Line 546 ⟶ 703:
(write-byte blue stream)))))))
filename)</syntaxhighlight>
 
=={{header|D}}==
The Image module contains a savePPM6 function to save binary PPM images.
 
=={{header|Delphi}}==
Helper class to enable bitmap export to ppm.
<syntaxhighlight lang=Delphi"delphi">
program btm2ppm;
 
Line 605 ⟶ 760:
end.
</syntaxhighlight>
 
=={{header|E}}==
 
The code for this task is incorporated into [[Basic bitmap storage#E]].
 
=={{header|Erlang}}==
Writes a bitmap to PPM file. Uses 24 bit color depth (color max value 255).
<syntaxhighlight lang="erlang">
-module(ppm).
 
Line 660 ⟶ 813:
 
</syntaxhighlight>
 
=={{header|Euphoria}}==
{{trans|C}}
<syntaxhighlight lang="euphoria">constant dimx = 800, dimy = 800
constant fn = open("first.ppm","wb") -- b - binary mode
sequence color
Line 680 ⟶ 832:
 
Procedure writing [[Bitmap#Euphoria|bitmap]] data storage:
<syntaxhighlight lang="euphoria">procedure write_ppm(sequence filename, sequence image)
integer fn,dimx,dimy
dimy = length(image[1])
Line 694 ⟶ 846:
close(fn)
end procedure</syntaxhighlight>
 
=={{header|FBSL}}==
This code converts a Windows BMP to a PPM. Uses FBSL volatiles for brevity.
Line 700 ⟶ 851:
'''24-bpp P.O.T.-size BMP solution:'''
[[File:FBSLWritePpm.PNG|right]]
<syntaxhighlight lang="qbasic">#ESCAPECHARS ON
 
DIM bmpin = ".\\LenaClr.bmp", ppmout = ".\\Lena.ppm", bmpblob = 54 ' Size of BMP file headers
Line 723 ⟶ 874:
 
FILEPUT(FILEOPEN(ppmout, BINARY_NEW), ppmdata): FILECLOSE(FILEOPEN)</syntaxhighlight>
 
=={{header|Forth}}==
<syntaxhighlight lang="forth">: write-ppm { bmp fid -- }
s" P6" fid write-line throw
bmp bdim swap
Line 739 ⟶ 889:
test over write-ppm
close-file throw</syntaxhighlight>
 
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
It loads <code>rgbimage_m</code> module, which is defined [[Basic bitmap storage#Fortran|here]].
<syntaxhighlight lang="fortran">program main
 
use rgbimage_m
Line 769 ⟶ 918:
 
end program</syntaxhighlight>
 
=={{header|GAP}}==
<syntaxhighlight lang="gap"># Dirty implementation
# Only P3 format, an image is a list of 3 matrices (r, g, b)
# Max color is always 255
Line 822 ⟶ 970:
PutPixel(g, 2, 3, [0, 0, 0]);
WriteImage("example.ppm", g);</syntaxhighlight>
 
=={{header|Go}}==
Code below writes 8-bit P6 format only. See Bitmap task for additional file needed to build working raster package.
<syntaxhighlight lang="go">package raster
 
import (
Line 880 ⟶ 1,027:
}</syntaxhighlight>
Demonstration program. Note that it imports package raster. To build package raster, put code above in one file, put code from Bitmap task in another, and compile and link them into a Go package.
<syntaxhighlight lang="go">package main
 
// Files required to build supporting package raster are found in:
Line 899 ⟶ 1,046:
}
}</syntaxhighlight>
 
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">{-# LANGUAGE ScopedTypeVariables #-}
 
module Bitmap.Netpbm(readNetpbm, writeNetpbm) where
Line 951 ⟶ 1,097:
where magicNumber = netpbmMagicNumber (nil :: c)
maxval = netpbmMaxval (nil :: c)</syntaxhighlight>
 
=={{header|J}}==
'''Solution:'''
<syntaxhighlight lang="j">require 'files'
 
NB. ($x) is height, width, colors per pixel
Line 963 ⟶ 1,108:
'''Example:'''
Using routines from [[Basic_bitmap_storage#J|Basic Bitmap Storage]]:
<syntaxhighlight lang="j"> NB. create 10 by 10 block of magenta pixels in top right quadrant of a 300 wide by 600 high green image
pixellist=: >,{;~i.10
myimg=: ((150 + pixellist) ; 255 0 255) setPixels 0 255 0 makeRGB 600 300
myimg writeppm jpath '~temp/myimg.ppm'
540015</syntaxhighlight>
 
=={{header|Java}}==
 
See [[Basic_bitmap_storage#Java|Basic Bitmap Storage]] for the <tt>BasicBitmapStorage</tt> class.
 
<syntaxhighlight lang="java">import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
Line 1,002 ⟶ 1,146:
}
}</syntaxhighlight>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
 
<syntaxhighlight lang="julia">using Images, FileIO
 
h, w = 50, 70
Line 1,017 ⟶ 1,160:
save("data/bitmapWrite.ppm", img)
save("data/bitmapWrite.png", img)</syntaxhighlight>
 
=={{header|Kotlin}}==
For convenience, we repeat the code for the class used in the [[Bitmap]] task here.
<syntaxhighlight lang="scala">// Version 1.2.40
 
import java.awt.Color
Line 1,072 ⟶ 1,214:
}
}</syntaxhighlight>
=={{header|LiveCode}}==
 
LiveCode has built in support for importing and exporting PBM, JPEG, GIF, BMP or PNG graphics formats
<syntaxhighlight lang="livecode">
export image "test" to file "~/Test.PPM" as paint -- paint format is one of PBM, PGM, or PPM
</syntaxhighlight>
=={{header|Lua}}==
===Original===
<syntaxhighlight lang="lua">
 
-- helper function, simulates PHP's array_fill function
Line 1,186 ⟶ 1,332:
===Alternate===
Uses the alternate Bitmap implementation [[Bitmap#Alternate|here]], extending it with..
<syntaxhighlight lang="lua">Bitmap.savePPM = function(self, filename)
local fp = io.open(filename, "wb")
if fp == nil then return false end
Line 1,200 ⟶ 1,346:
end</syntaxhighlight>
Example usage:
<syntaxhighlight lang="lua">local bitmap = Bitmap(11,5)
bitmap:clear({255,255,255})
for y = 1, 5 do
Line 1,210 ⟶ 1,356:
end
bitmap:savePPM("lua3x5.ppm")</syntaxhighlight>
 
 
=={{header|LiveCode}}==
LiveCode has built in support for importing and exporting PBM, JPEG, GIF, BMP or PNG graphics formats
<syntaxhighlight lang=LiveCode>
export image "test" to file "~/Test.PPM" as paint -- paint format is one of PBM, PGM, or PPM
</syntaxhighlight>
 
 
=={{header|M2000 Interpreter}}==
Added ToFile in group which return the function Bitmap. In this example we export using ToFile and get bytes (unsigned values) from buffer, and we export from outside, using getpixel and convert the RGB value to bytes (color returned as a negative number, so we have to invert before further process it)
===P3 type===
<syntaxhighlight lang=M2000"m2000 Interpreterinterpreter">
Module Checkit {
Function Bitmap (x as long, y as long) {
Line 1,356 ⟶ 1,493:
 
===P6 type===
<syntaxhighlight lang=M2000"m2000 Interpreterinterpreter">
Module PPMbinaryP6 {
If Version<9.4 then 1000
Line 1,536 ⟶ 1,673:
 
</syntaxhighlight>
 
=={{header|Mathematica}}/ {{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica"mathematica">Export["file.ppm",image,"PPM"]</syntaxhighlight>
 
=={{header|MATLAB}} / {{header|Octave}}==
<syntaxhighlight lang=MATLAB"matlab">R=[255,0,0;255,255,0];
G=[0,255,0;255,255,0];
B=[0,0,255;0,0,0];
Line 1,553 ⟶ 1,688:
fwrite(fid,[r,g,b]','uint8');
fclose(fid);</syntaxhighlight>
 
=={{header|Modula-3}}==
<code>Bitmap</code> is the module from [[Basic_bitmap_storage#Modula-3|Basic Bitmap Storage]].
<syntaxhighlight lang="modula3">INTERFACE PPM;
 
IMPORT Bitmap, Pathname;
Line 1,563 ⟶ 1,697:
 
END PPM.</syntaxhighlight>
<syntaxhighlight lang="modula3">MODULE PPM;
 
IMPORT Bitmap, Wr, FileWr, Pathname;
Line 1,595 ⟶ 1,729:
 
=={{Header|Nim}}==
<syntaxhighlight lang="nim">import bitmap
import streams
 
Line 1,634 ⟶ 1,768:
=={{Header|OCaml}}==
 
<syntaxhighlight lang="ocaml">let output_ppm ~oc ~img:(_, r_channel, g_channel, b_channel) =
let width = Bigarray.Array2.dim1 r_channel
and height = Bigarray.Array2.dim2 r_channel in
Line 1,648 ⟶ 1,782:
flush oc;
;;</syntaxhighlight>
 
=={{header|Oz}}==
As a function in the module <code>BitmapIO.oz</code>:
<syntaxhighlight lang="oz">functor
import
Bitmap
Line 1,690 ⟶ 1,823:
end
end</syntaxhighlight>
 
=={{header|Perl}}==
{{libheader|Imager}}
<syntaxhighlight lang="perl">use Imager;
 
$image = Imager->new(xsize => 200, ysize => 200);
Line 1,701 ⟶ 1,833:
xmax => 150, ymax => 150);
$image->write(file => 'bitmap.ppm') or die $image->errstr;</syntaxhighlight>
 
=={{header|Phix}}==
Copy of [[Bitmap/Write_a_PPM_file#Euphoria|Euphoria]]. The results may be verified with demo\rosetta\viewppm.exw
<syntaxhighlight lang=Phix"phix">-- demo\rosetta\Bitmap_write_ppm.exw
constant dimx = 512, dimy = 512
constant fn = open("first.ppm","wb") -- b - binary mode
Line 1,719 ⟶ 1,850:
close(fn)</syntaxhighlight>
The following more general purpose routine is used in several other examples (via include ppm.e):
<syntaxhighlight lang=Phix"phix">global procedure write_ppm(string filename, sequence image)
integer fn = open(filename,"wb"),
dimx = length(image),
Line 1,734 ⟶ 1,865:
close(fn)
end procedure</syntaxhighlight>
 
=={{header|PHP}}==
Writes a P6 binary file
<syntaxhighlight lang=PHP"php">class Bitmap {
public $data;
public $w;
Line 1,789 ⟶ 1,919:
$b->setPixel(0, 15, array(255,0,0));
$b->writeP6('p6.ppm');</syntaxhighlight>
 
=={{header|PicoLisp}}==
<syntaxhighlight lang=PicoLisp"picolisp">(de ppmWrite (Ppm File)
(out File
(prinl "P6")
Line 1,797 ⟶ 1,926:
(prinl 255)
(for Y Ppm (for X Y (apply wr X))) ) )</syntaxhighlight>
 
=={{header|PL/I}}==
<syntaxhighlight lang=PL"pl/Ii">/* BITMAP FILE: write out a file in PPM format, P6 (binary). 14/5/2010 */
test: procedure options (main);
declare image (0:19,0:19) bit (24);
Line 1,846 ⟶ 1,974:
end put_integer;
end test;</syntaxhighlight>
 
=={{header|Prolog}}==
This is an extremely straight forward way to write in Prolog, more complicated methods might use DCGs:
<syntaxhighlight lang="prolog">
:- module(bitmapIO, [
write_ppm_p6/2]).
Line 1,870 ⟶ 1,997:
usage:
 
<syntaxhighlight lang="prolog">
:- use_module(bitmap).
:- use_module(bitmapIO).
Line 1,880 ⟶ 2,007:
 
</syntaxhighlight>
 
=={{header|PureBasic}}==
<syntaxhighlight lang=PureBasic"purebasic">Procedure SaveImageAsPPM(Image, file$, Binary = 1)
; Author Roger Rösch (Nickname Macros)
IDFiIe = CreateFile(#PB_Any, file$)
Line 1,916 ⟶ 2,042:
EndIf
EndProcedure</syntaxhighlight>
 
=={{header|Python}}==
{{works with|Python|3.1}}
 
Extending the example given [[Basic_bitmap_storage#Alternative_version|here]]
<syntaxhighlight lang="python">
# String masquerading as ppm file (version P3)
import io
Line 1,985 ⟶ 2,110:
ppmfileout.close()
</syntaxhighlight>
 
=={{header|R}}==
{{libheader|pixmap}}
<syntaxhighlight lang="r">
# View the existing code in the library
library(pixmap)
Line 1,996 ⟶ 2,120:
write.pnm(theimage, filename)
</syntaxhighlight>
 
=={{header|Racket}}==
<syntaxhighlight lang="racket">
;P3
(define (bitmap->ppm bitmap output-port)
Line 2,041 ⟶ 2,164:
 
</syntaxhighlight>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2016-01}}
 
<syntaxhighlight lang="raku" line>class Pixel { has uint8 ($.R, $.G, $.B) }
class Bitmap {
has UInt ($.width, $.height);
Line 2,079 ⟶ 2,201:
 
[[File:Ppm-perl6.png‎]]
 
=={{header|REXX}}==
<syntaxhighlight lang="rexx">/*REXX program writes a PPM formatted image file, also known as a P6 (binary) file. */
green = 00ff00 /*define a pixel with the color green. */
parse arg oFN width height color . /*obtain optional arguments from the CL*/
Line 2,103 ⟶ 2,224:
/*stick a fork in it, we're all done. */</syntaxhighlight>
<br><br>
 
=={{header|Ruby}}==
Extending [[Basic_bitmap_storage#Ruby]]
<syntaxhighlight lang="ruby">class RGBColour
def values
[@red, @green, @blue]
Line 2,126 ⟶ 2,246:
alias_method :write, :save
end</syntaxhighlight>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">use std::path::Path;
use std::io::Write;
use std::fs::File;
Line 2,197 ⟶ 2,316:
}
}</syntaxhighlight>
 
=={{header|Scala}}==
Extends Pixmap class from task [[Read_ppm_file#Scala|Read PPM file]].
<syntaxhighlight lang="scala">object Pixmap {
def save(bm:RgbBitmap, filename:String)={
val out=new DataOutputStream(new FileOutputStream(filename))
Line 2,213 ⟶ 2,331:
}
}</syntaxhighlight>
 
=={{header|Scheme}}==
{{Works with|Scheme|R<math>^5</math>RS}}
<syntaxhighlight lang="scheme">(define (write-ppm image file)
(define (write-image image)
(define (write-row row)
Line 2,239 ⟶ 2,356:
(write-image image)))))</syntaxhighlight>
Example using definitions in [[Basic bitmap storage#Scheme]]:
<syntaxhighlight lang="scheme">(define image (make-image 800 600))
(image-fill! image *black*)
(image-set! image 400 300 *blue*)
(write-ppm image "out.ppm")</syntaxhighlight>
 
=={{header|Seed7}}==
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "draw.s7i";
include "color.s7i";
Line 2,270 ⟶ 2,386:
end if;
end func;</syntaxhighlight>
 
=={{header|Sidef}}==
{{trans|Raku}}
<syntaxhighlight lang="ruby">subset Int < Number {|n| n.is_int }
subset UInt < Int {|n| n >= 0 }
subset UInt8 < Int {|n| n ~~ ^256 }
Line 2,316 ⟶ 2,431:
 
%f"palette.ppm".write(b.p6, :raw)</syntaxhighlight>
 
=={{header|Stata}}==
 
P3 format only, with Mata.
 
<syntaxhighlight lang="stata">mata
void writeppm(name, r, g, b) {
n = rows(r)
Line 2,341 ⟶ 2,455:
writeppm("image.ppm", r, g, b)
end</syntaxhighlight>
 
=={{header|Tcl}}==
{{libheader|Tk}}
Referring to [[Basic bitmap storage#Tcl]]:
<syntaxhighlight lang="tcl">package require Tk
 
proc output_ppm {image filename} {
Line 2,364 ⟶ 2,477:
foreach colour $pixel {puts [expr {$colour & 0xff}]} ;# ==> 255 \n 0 \n 0 \n
close $fh</syntaxhighlight>
 
=={{header|UNIX Shell}}==
{{works with|ksh93}}
Line 2,370 ⟶ 2,482:
 
Add the following function to the <tt>Bitmap_t</tt> type
<syntaxhighlight lang="bash"> function write {
_.to_s > "$1"
}</syntaxhighlight>
Then you can:
<syntaxhighlight lang="bash">Bitmap_t b
# do stuff to b, and save it:
b.write '$HOME/tmp/bitmap.ppm'</syntaxhighlight>
 
=={{header|Vedit macro language}}==
 
Line 2,400 ⟶ 2,511:
Return
</pre>
 
=={{header|Visual Basic .NET}}==
 
<syntaxhighlight lang="vbnet">Public Shared Sub SaveRasterBitmapToPpmFile(ByVal rasterBitmap As RasterBitmap, ByVal filepath As String)
Dim header As String = String.Format("P6{0}{1}{2}{3}{0}255{0}", vbLf, rasterBitmap.Width, " "c, rasterBitmap.Height)
Dim bufferSize As Integer = header.Length + (rasterBitmap.Width * rasterBitmap.Height * 3)
Line 2,420 ⟶ 2,530:
My.Computer.FileSystem.WriteAllBytes(filepath, bytes, False)
End Sub</syntaxhighlight>
 
=={{header|Wren}}==
{{libheader|DOME}}
{{libheader|Wren-str}}
This takes a while to run as DOME needs to build up the file contents in string form before saving them to a PPM file. It is not currently possible to write files a line at a time.
<syntaxhighlight lang=ecmascript"wren">import "graphics" for Canvas, ImageData, Color
import "dome" for Window, Process
import "io" for FileSystem
import "./str" for Strs
 
class Bitmap {
Line 2,447 ⟶ 2,557:
init() {
// write bitmap to a PPM file
var ppm = ["P6\n%(_w) %(_h)\n255\n"]
for (y in 0..._h) {
for (x in 0..._w) {
var c = pget(x, y)
ppm = ppm + .add(String.fromByte(c.r))
ppm = ppm + .add(String.fromByte(c.g))
ppm = ppm + .add(String.fromByte(c.b))
}
}
FileSystem.save("output.ppm", Strs.concat(ppm))
Process.exit(0)
}
Line 2,472 ⟶ 2,582:
 
=={{header|XPL0}}==
<syntaxhighlight lang=XPL0"xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
def Width=180, Height=135, Color=$123456;
 
Line 2,510 ⟶ 2,620:
SetVid(3); \restore display to normal text mode
]</syntaxhighlight>
 
=={{header|Yabasic}}==
<syntaxhighlight lang=Yabasic"yabasic">clear screen
 
wid = 150 : hei = 200
Line 2,542 ⟶ 2,651:
poke #fn, asc("\n")
close #fn</syntaxhighlight>
 
=={{header|zkl}}==
<syntaxhighlight lang="zkl">// convert Windows BMP (bit map) image to PPM
 
// Read BMP file
Line 2,568 ⟶ 2,676:
-rw-r--r-- 1 craigd craigd 786476 Aug 30 01:31 foo.ppm
</pre>
 
{{omit from|PARI/GP}}
3

edits