Bitmap/Write a PPM file: Difference between revisions
Content added Content deleted
m (→{{header|BQN}}: Trivial simplifications: +¨ to +, and combine 256| of a list) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 10: | Line 10: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang=11l>T Colour |
||
Byte r, g, b |
Byte r, g, b |
||
Line 77: | Line 77: | ||
print(bitmap.writeppmp3()) |
print(bitmap.writeppmp3()) |
||
File(‘tmp.ppm’, ‘w’).write_bytes(bitmap.writeppmp6())</ |
File(‘tmp.ppm’, ‘w’).write_bytes(bitmap.writeppmp6())</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 93: | Line 93: | ||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
{{libheader|Action! Bitmap tools}} |
{{libheader|Action! Bitmap tools}} |
||
< |
<syntaxhighlight lang=Action!>INCLUDE "H6:RGBIMAGE.ACT" ;from task Bitmap |
||
PROC SaveHeader(RgbImage POINTER img |
PROC SaveHeader(RgbImage POINTER img |
||
Line 184: | Line 184: | ||
PrintF("Loading %S...%E%E",path3) |
PrintF("Loading %S...%E%E",path3) |
||
Load(path3) |
Load(path3) |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Write_a_PPM_file.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Write_a_PPM_file.png Screenshot from Atari 8-bit computer] |
||
Line 204: | Line 204: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang=ada>with Ada.Characters.Latin_1; |
||
with Ada.Streams.Stream_IO; use Ada.Streams.Stream_IO; |
with Ada.Streams.Stream_IO; use Ada.Streams.Stream_IO; |
||
Line 229: | Line 229: | ||
end loop; |
end loop; |
||
Character'Write (Stream (File), LF); |
Character'Write (Stream (File), LF); |
||
end Put_PPM;</ |
end Put_PPM;</syntaxhighlight> |
||
The solution writes the image into an opened file. The file format might fail to work on certain [[OS]]es, because output might mangle control characters like LF, CR, FF, HT, VT etc. The OS might also limit the line length of a text file. In general it is a bad idea to mix binary and text output in one file. This solution uses ''stream I/O'', which should be as portable as possible. |
The solution writes the image into an opened file. The file format might fail to work on certain [[OS]]es, because output might mangle control characters like LF, CR, FF, HT, VT etc. The OS might also limit the line length of a text file. In general it is a bad idea to mix binary and text output in one file. This solution uses ''stream I/O'', which should be as portable as possible. |
||
=={{header|Aime}}== |
=={{header|Aime}}== |
||
< |
<syntaxhighlight lang=aime>integer i, h, j, w; |
||
file f; |
file f; |
||
Line 249: | Line 249: | ||
16.times(f_bytes, f, drand(255), drand(255), drand(255)); |
16.times(f_bytes, f, drand(255), drand(255), drand(255)); |
||
} while ((i += 16) < w); |
} while ((i += 16) < w); |
||
} while ((j += 1) < h);</ |
} while ((j += 1) < h);</syntaxhighlight> |
||
=={{header|Applesoft BASIC}}== |
=={{header|Applesoft BASIC}}== |
||
< |
<syntaxhighlight lang=gwbasic> 100 W = 8 |
||
110 H = 8 |
110 H = 8 |
||
120 BA = 24576 |
120 BA = 24576 |
||
Line 269: | Line 269: | ||
250 END |
250 END |
||
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 |
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</ |
600 FOR Y = 0 TO H - 1: FOR X = 0 TO W - 1: GOSUB 500: NEXT X,Y: RETURN</syntaxhighlight> |
||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
{{works with|AutoHotkey_L|45}} |
{{works with|AutoHotkey_L|45}} |
||
< |
<syntaxhighlight lang=AutoHotkey> |
||
cyan := color(0,255,255) ; r,g,b |
cyan := color(0,255,255) ; r,g,b |
||
cyanppm := Bitmap(10, 10, cyan) ; width, height, background-color |
cyanppm := Bitmap(10, 10, cyan) ; width, height, background-color |
||
Line 304: | Line 304: | ||
return 0 |
return 0 |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
< |
<syntaxhighlight lang=AWK>#!/usr/bin/awk -f |
||
BEGIN { |
BEGIN { |
||
split("255,0,0,255,255,0",R,","); |
split("255,0,0,255,255,0",R,","); |
||
Line 319: | Line 319: | ||
} |
} |
||
close(outfile); |
close(outfile); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
{{works with|BBC BASIC for Windows}} |
{{works with|BBC BASIC for Windows}} |
||
< |
<syntaxhighlight lang=bbcbasic> Width% = 200 |
||
Height% = 200 |
Height% = 200 |
||
Line 352: | Line 352: | ||
col% = TINT(x%*2,y%*2) |
col% = TINT(x%*2,y%*2) |
||
SWAP ?^col%,?(^col%+2) |
SWAP ?^col%,?(^col%+2) |
||
= col%</ |
= col%</syntaxhighlight> |
||
=={{header|BQN}}== |
=={{header|BQN}}== |
||
< |
<syntaxhighlight lang=bqn>header_ppm ← "P6 |
||
4 8 |
4 8 |
||
255 |
255 |
||
Line 369: | Line 369: | ||
image_ppm ↩ @ + ⥊ image_ppm # deshape, convert to chars (uint8_t) |
image_ppm ↩ @ + ⥊ image_ppm # deshape, convert to chars (uint8_t) |
||
bytes_ppm ← header_ppm ∾ image_ppm |
bytes_ppm ← header_ppm ∾ image_ppm |
||
"small.ppm" •file.Bytes bytes_ppm</ |
"small.ppm" •file.Bytes bytes_ppm</syntaxhighlight> |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang=bqn>header_ppm ← "P6 |
||
800 800 |
800 800 |
||
255 |
255 |
||
" |
" |
||
image_ppm ← @ + ⥊ > {256|𝕨‿𝕩‿(𝕨×𝕩)}⌜˜ ↕800 |
image_ppm ← @ + ⥊ > {256|𝕨‿𝕩‿(𝕨×𝕩)}⌜˜ ↕800 |
||
"first_bqn.ppm" •file.Bytes header_ppm ∾ image_ppm</ |
"first_bqn.ppm" •file.Bytes header_ppm ∾ image_ppm</syntaxhighlight> |
||
=={{header|C}}== |
=={{header|C}}== |
||
This is one file program which writes one color in each step : |
This is one file program which writes one color in each step : |
||
< |
<syntaxhighlight lang=c>#include <stdlib.h> |
||
#include <stdio.h> |
#include <stdio.h> |
||
Line 403: | Line 403: | ||
(void) fclose(fp); |
(void) fclose(fp); |
||
return EXIT_SUCCESS; |
return EXIT_SUCCESS; |
||
}</ |
}</syntaxhighlight> |
||
This program writes whole array in one step : |
This program writes whole array in one step : |
||
< |
<syntaxhighlight lang=c>#include <stdio.h> |
||
int main() |
int main() |
||
Line 443: | Line 443: | ||
printf("OK - file %s saved\n", filename); |
printf("OK - file %s saved\n", filename); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
Line 452: | Line 452: | ||
Interface: |
Interface: |
||
< |
<syntaxhighlight lang=c>void output_ppm(FILE *fd, image img);</syntaxhighlight> |
||
Implementation: |
Implementation: |
||
< |
<syntaxhighlight lang=c>#include "imglib.h" |
||
void output_ppm(FILE *fd, image img) |
void output_ppm(FILE *fd, image img) |
||
Line 465: | Line 465: | ||
(void) fwrite(img->buf, sizeof(pixel), n, fd); |
(void) fwrite(img->buf, sizeof(pixel), n, fd); |
||
(void) fflush(fd); |
(void) fflush(fd); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C sharp|C#}}== |
=={{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. |
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; |
using System.IO; |
||
class PPMWriter |
class PPMWriter |
||
Line 493: | Line 493: | ||
writerB.Close(); |
writerB.Close(); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang=cpp>#include <fstream> |
||
#include <cstdio> |
#include <cstdio> |
||
Line 514: | Line 514: | ||
return EXIT_SUCCESS; |
return EXIT_SUCCESS; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang=lisp>(defun write-rgb-buffer-to-ppm-file (filename buffer) |
||
(with-open-file (stream filename |
(with-open-file (stream filename |
||
:element-type '(unsigned-byte 8) |
:element-type '(unsigned-byte 8) |
||
Line 545: | Line 545: | ||
(write-byte green stream) |
(write-byte green stream) |
||
(write-byte blue stream))))))) |
(write-byte blue stream))))))) |
||
filename)</ |
filename)</syntaxhighlight> |
||
=={{header|D}}== |
=={{header|D}}== |
||
Line 552: | Line 552: | ||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
Helper class to enable bitmap export to ppm. |
Helper class to enable bitmap export to ppm. |
||
< |
<syntaxhighlight lang=Delphi> |
||
program btm2ppm; |
program btm2ppm; |
||
Line 604: | Line 604: | ||
end; |
end; |
||
end. |
end. |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|E}}== |
=={{header|E}}== |
||
Line 612: | Line 612: | ||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
Writes a bitmap to PPM file. Uses 24 bit color depth (color max value 255). |
Writes a bitmap to PPM file. Uses 24 bit color depth (color max value 255). |
||
< |
<syntaxhighlight lang=erlang> |
||
-module(ppm). |
-module(ppm). |
||
Line 659: | Line 659: | ||
integer_to_list(Number). |
integer_to_list(Number). |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang=euphoria>constant dimx = 800, dimy = 800 |
||
constant fn = open("first.ppm","wb") -- b - binary mode |
constant fn = open("first.ppm","wb") -- b - binary mode |
||
sequence color |
sequence color |
||
Line 677: | Line 677: | ||
end for |
end for |
||
end for |
end for |
||
close(fn)</ |
close(fn)</syntaxhighlight> |
||
Procedure writing [[Bitmap#Euphoria|bitmap]] data storage: |
Procedure writing [[Bitmap#Euphoria|bitmap]] data storage: |
||
< |
<syntaxhighlight lang=euphoria>procedure write_ppm(sequence filename, sequence image) |
||
integer fn,dimx,dimy |
integer fn,dimx,dimy |
||
dimy = length(image[1]) |
dimy = length(image[1]) |
||
Line 693: | Line 693: | ||
end for |
end for |
||
close(fn) |
close(fn) |
||
end procedure</ |
end procedure</syntaxhighlight> |
||
=={{header|FBSL}}== |
=={{header|FBSL}}== |
||
Line 700: | Line 700: | ||
'''24-bpp P.O.T.-size BMP solution:''' |
'''24-bpp P.O.T.-size BMP solution:''' |
||
[[File:FBSLWritePpm.PNG|right]] |
[[File:FBSLWritePpm.PNG|right]] |
||
< |
<syntaxhighlight lang=qbasic>#ESCAPECHARS ON |
||
DIM bmpin = ".\\LenaClr.bmp", ppmout = ".\\Lena.ppm", bmpblob = 54 ' Size of BMP file headers |
DIM bmpin = ".\\LenaClr.bmp", ppmout = ".\\Lena.ppm", bmpblob = 54 ' Size of BMP file headers |
||
Line 722: | Line 722: | ||
WEND |
WEND |
||
FILEPUT(FILEOPEN(ppmout, BINARY_NEW), ppmdata): FILECLOSE(FILEOPEN)</ |
FILEPUT(FILEOPEN(ppmout, BINARY_NEW), ppmdata): FILECLOSE(FILEOPEN)</syntaxhighlight> |
||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
< |
<syntaxhighlight lang=forth>: write-ppm { bmp fid -- } |
||
s" P6" fid write-line throw |
s" P6" fid write-line throw |
||
bmp bdim swap |
bmp bdim swap |
||
Line 738: | Line 738: | ||
s" red.ppm" w/o create-file throw |
s" red.ppm" w/o create-file throw |
||
test over write-ppm |
test over write-ppm |
||
close-file throw</ |
close-file throw</syntaxhighlight> |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
{{works with|Fortran|90 and later}} |
{{works with|Fortran|90 and later}} |
||
It loads <code>rgbimage_m</code> module, which is defined [[Basic bitmap storage#Fortran|here]]. |
It loads <code>rgbimage_m</code> module, which is defined [[Basic bitmap storage#Fortran|here]]. |
||
< |
<syntaxhighlight lang=fortran>program main |
||
use rgbimage_m |
use rgbimage_m |
||
Line 768: | Line 768: | ||
call im%write('fig.ppm') |
call im%write('fig.ppm') |
||
end program</ |
end program</syntaxhighlight> |
||
=={{header|GAP}}== |
=={{header|GAP}}== |
||
< |
<syntaxhighlight lang=gap># Dirty implementation |
||
# Only P3 format, an image is a list of 3 matrices (r, g, b) |
# Only P3 format, an image is a list of 3 matrices (r, g, b) |
||
# Max color is always 255 |
# Max color is always 255 |
||
Line 821: | Line 821: | ||
PutPixel(g, 2, 2, [255, 255, 255]); |
PutPixel(g, 2, 2, [255, 255, 255]); |
||
PutPixel(g, 2, 3, [0, 0, 0]); |
PutPixel(g, 2, 3, [0, 0, 0]); |
||
WriteImage("example.ppm", g);</ |
WriteImage("example.ppm", g);</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
Code below writes 8-bit P6 format only. See Bitmap task for additional file needed to build working raster package. |
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 ( |
import ( |
||
Line 878: | Line 878: | ||
} |
} |
||
return f.Close() |
return f.Close() |
||
}</ |
}</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. |
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: |
// Files required to build supporting package raster are found in: |
||
Line 898: | Line 898: | ||
fmt.Println(err) |
fmt.Println(err) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang=haskell>{-# LANGUAGE ScopedTypeVariables #-} |
||
module Bitmap.Netpbm(readNetpbm, writeNetpbm) where |
module Bitmap.Netpbm(readNetpbm, writeNetpbm) where |
||
Line 950: | Line 950: | ||
stToIO (getPixels i) >>= hPutStr h . toNetpbm |
stToIO (getPixels i) >>= hPutStr h . toNetpbm |
||
where magicNumber = netpbmMagicNumber (nil :: c) |
where magicNumber = netpbmMagicNumber (nil :: c) |
||
maxval = netpbmMaxval (nil :: c)</ |
maxval = netpbmMaxval (nil :: c)</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
'''Solution:''' |
'''Solution:''' |
||
< |
<syntaxhighlight lang=j>require 'files' |
||
NB. ($x) is height, width, colors per pixel |
NB. ($x) is height, width, colors per pixel |
||
Line 960: | Line 960: | ||
header=. 'P6',LF,(":1 0{$x),LF,'255',LF |
header=. 'P6',LF,(":1 0{$x),LF,'255',LF |
||
(header,,x{a.) fwrite y |
(header,,x{a.) fwrite y |
||
)</ |
)</syntaxhighlight> |
||
'''Example:''' |
'''Example:''' |
||
Using routines from [[Basic_bitmap_storage#J|Basic Bitmap Storage]]: |
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 |
pixellist=: >,{;~i.10 |
||
myimg=: ((150 + pixellist) ; 255 0 255) setPixels 0 255 0 makeRGB 600 300 |
myimg=: ((150 + pixellist) ; 255 0 255) setPixels 0 255 0 makeRGB 600 300 |
||
myimg writeppm jpath '~temp/myimg.ppm' |
myimg writeppm jpath '~temp/myimg.ppm' |
||
540015</ |
540015</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
Line 973: | Line 973: | ||
See [[Basic_bitmap_storage#Java|Basic Bitmap Storage]] for the <tt>BasicBitmapStorage</tt> class. |
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.File; |
||
import java.io.FileOutputStream; |
import java.io.FileOutputStream; |
||
Line 1,001: | Line 1,001: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{works with|Julia|0.6}} |
{{works with|Julia|0.6}} |
||
< |
<syntaxhighlight lang=julia>using Images, FileIO |
||
h, w = 50, 70 |
h, w = 50, 70 |
||
Line 1,016: | Line 1,016: | ||
save("data/bitmapWrite.ppm", img) |
save("data/bitmapWrite.ppm", img) |
||
save("data/bitmapWrite.png", img)</ |
save("data/bitmapWrite.png", img)</syntaxhighlight> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
For convenience, we repeat the code for the class used in the [[Bitmap]] task here. |
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 |
import java.awt.Color |
||
Line 1,071: | Line 1,071: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
===Original=== |
===Original=== |
||
< |
<syntaxhighlight lang=lua> |
||
-- helper function, simulates PHP's array_fill function |
-- helper function, simulates PHP's array_fill function |
||
Line 1,183: | Line 1,183: | ||
example_colorful_stripes():writeP6('p6.ppm') |
example_colorful_stripes():writeP6('p6.ppm') |
||
</syntaxhighlight> |
|||
</lang> |
|||
===Alternate=== |
===Alternate=== |
||
Uses the alternate Bitmap implementation [[Bitmap#Alternate|here]], extending it with.. |
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") |
local fp = io.open(filename, "wb") |
||
if fp == nil then return false end |
if fp == nil then return false end |
||
Line 1,198: | Line 1,198: | ||
fp:close() |
fp:close() |
||
return true |
return true |
||
end</ |
end</syntaxhighlight> |
||
Example usage: |
Example usage: |
||
< |
<syntaxhighlight lang=lua>local bitmap = Bitmap(11,5) |
||
bitmap:clear({255,255,255}) |
bitmap:clear({255,255,255}) |
||
for y = 1, 5 do |
for y = 1, 5 do |
||
Line 1,209: | Line 1,209: | ||
end |
end |
||
end |
end |
||
bitmap:savePPM("lua3x5.ppm")</ |
bitmap:savePPM("lua3x5.ppm")</syntaxhighlight> |
||
=={{header|LiveCode}}== |
=={{header|LiveCode}}== |
||
LiveCode has built in support for importing and exporting PBM, JPEG, GIF, BMP or PNG graphics formats |
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 |
export image "test" to file "~/Test.PPM" as paint -- paint format is one of PBM, PGM, or PPM |
||
</syntaxhighlight> |
|||
</lang> |
|||
Line 1,222: | Line 1,222: | ||
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) |
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=== |
===P3 type=== |
||
< |
<syntaxhighlight lang=M2000 Interpreter> |
||
Module Checkit { |
Module Checkit { |
||
Function Bitmap (x as long, y as long) { |
Function Bitmap (x as long, y as long) { |
||
Line 1,335: | Line 1,335: | ||
Checkit |
Checkit |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,356: | Line 1,356: | ||
===P6 type=== |
===P6 type=== |
||
< |
<syntaxhighlight lang=M2000 Interpreter> |
||
Module PPMbinaryP6 { |
Module PPMbinaryP6 { |
||
If Version<9.4 then 1000 |
If Version<9.4 then 1000 |
||
Line 1,535: | Line 1,535: | ||
PPMbinaryP6 |
PPMbinaryP6 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Mathematica}}/ {{header|Wolfram Language}}== |
=={{header|Mathematica}}/ {{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang=Mathematica>Export["file.ppm",image,"PPM"]</syntaxhighlight> |
||
=={{header|MATLAB}} / {{header|Octave}}== |
=={{header|MATLAB}} / {{header|Octave}}== |
||
< |
<syntaxhighlight lang=MATLAB>R=[255,0,0;255,255,0]; |
||
G=[0,255,0;255,255,0]; |
G=[0,255,0;255,255,0]; |
||
B=[0,0,255;0,0,0]; |
B=[0,0,255;0,0,0]; |
||
Line 1,552: | Line 1,552: | ||
fprintf(fid,'P6\n%i %i\n255\n',size(R)); |
fprintf(fid,'P6\n%i %i\n255\n',size(R)); |
||
fwrite(fid,[r,g,b]','uint8'); |
fwrite(fid,[r,g,b]','uint8'); |
||
fclose(fid);</ |
fclose(fid);</syntaxhighlight> |
||
=={{header|Modula-3}}== |
=={{header|Modula-3}}== |
||
<code>Bitmap</code> is the module from [[Basic_bitmap_storage#Modula-3|Basic Bitmap Storage]]. |
<code>Bitmap</code> is the module from [[Basic_bitmap_storage#Modula-3|Basic Bitmap Storage]]. |
||
< |
<syntaxhighlight lang=modula3>INTERFACE PPM; |
||
IMPORT Bitmap, Pathname; |
IMPORT Bitmap, Pathname; |
||
Line 1,562: | Line 1,562: | ||
PROCEDURE Create(imgfile: Pathname.T; img: Bitmap.T); |
PROCEDURE Create(imgfile: Pathname.T; img: Bitmap.T); |
||
END PPM.</ |
END PPM.</syntaxhighlight> |
||
< |
<syntaxhighlight lang=modula3>MODULE PPM; |
||
IMPORT Bitmap, Wr, FileWr, Pathname; |
IMPORT Bitmap, Wr, FileWr, Pathname; |
||
Line 1,592: | Line 1,592: | ||
BEGIN |
BEGIN |
||
END PPM.</ |
END PPM.</syntaxhighlight> |
||
=={{Header|Nim}}== |
=={{Header|Nim}}== |
||
< |
<syntaxhighlight lang=nim>import bitmap |
||
import streams |
import streams |
||
Line 1,630: | Line 1,630: | ||
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")</syntaxhighlight> |
||
=={{Header|OCaml}}== |
=={{Header|OCaml}}== |
||
< |
<syntaxhighlight lang=ocaml>let output_ppm ~oc ~img:(_, r_channel, g_channel, b_channel) = |
||
let width = Bigarray.Array2.dim1 r_channel |
let width = Bigarray.Array2.dim1 r_channel |
||
and height = Bigarray.Array2.dim2 r_channel in |
and height = Bigarray.Array2.dim2 r_channel in |
||
Line 1,647: | Line 1,647: | ||
output_char oc '\n'; |
output_char oc '\n'; |
||
flush oc; |
flush oc; |
||
;;</ |
;;</syntaxhighlight> |
||
=={{header|Oz}}== |
=={{header|Oz}}== |
||
As a function in the module <code>BitmapIO.oz</code>: |
As a function in the module <code>BitmapIO.oz</code>: |
||
< |
<syntaxhighlight lang=oz>functor |
||
import |
import |
||
Bitmap |
Bitmap |
||
Line 1,689: | Line 1,689: | ||
end |
end |
||
end |
end |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{libheader|Imager}} |
{{libheader|Imager}} |
||
< |
<syntaxhighlight lang=perl>use Imager; |
||
$image = Imager->new(xsize => 200, ysize => 200); |
$image = Imager->new(xsize => 200, ysize => 200); |
||
Line 1,700: | Line 1,700: | ||
xmin => 50, ymin => 50, |
xmin => 50, ymin => 50, |
||
xmax => 150, ymax => 150); |
xmax => 150, ymax => 150); |
||
$image->write(file => 'bitmap.ppm') or die $image->errstr;</ |
$image->write(file => 'bitmap.ppm') or die $image->errstr;</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Copy of [[Bitmap/Write_a_PPM_file#Euphoria|Euphoria]]. The results may be verified with demo\rosetta\viewppm.exw |
Copy of [[Bitmap/Write_a_PPM_file#Euphoria|Euphoria]]. The results may be verified with demo\rosetta\viewppm.exw |
||
< |
<syntaxhighlight lang=Phix>-- demo\rosetta\Bitmap_write_ppm.exw |
||
constant dimx = 512, dimy = 512 |
constant dimx = 512, dimy = 512 |
||
constant fn = open("first.ppm","wb") -- b - binary mode |
constant fn = open("first.ppm","wb") -- b - binary mode |
||
Line 1,717: | Line 1,717: | ||
end for |
end for |
||
end for |
end for |
||
close(fn)</ |
close(fn)</syntaxhighlight> |
||
The following more general purpose routine is used in several other examples (via include ppm.e): |
The following more general purpose routine is used in several other examples (via include ppm.e): |
||
< |
<syntaxhighlight lang=Phix>global procedure write_ppm(string filename, sequence image) |
||
integer fn = open(filename,"wb"), |
integer fn = open(filename,"wb"), |
||
dimx = length(image), |
dimx = length(image), |
||
Line 1,733: | Line 1,733: | ||
end for |
end for |
||
close(fn) |
close(fn) |
||
end procedure</ |
end procedure</syntaxhighlight> |
||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
Writes a P6 binary file |
Writes a P6 binary file |
||
< |
<syntaxhighlight lang=PHP>class Bitmap { |
||
public $data; |
public $data; |
||
public $w; |
public $w; |
||
Line 1,788: | Line 1,788: | ||
$b->fill(2, 2, 18, 18, array(240,240,240)); |
$b->fill(2, 2, 18, 18, array(240,240,240)); |
||
$b->setPixel(0, 15, array(255,0,0)); |
$b->setPixel(0, 15, array(255,0,0)); |
||
$b->writeP6('p6.ppm');</ |
$b->writeP6('p6.ppm');</syntaxhighlight> |
||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang=PicoLisp>(de ppmWrite (Ppm File) |
||
(out File |
(out File |
||
(prinl "P6") |
(prinl "P6") |
||
(prinl (length (car Ppm)) " " (length Ppm)) |
(prinl (length (car Ppm)) " " (length Ppm)) |
||
(prinl 255) |
(prinl 255) |
||
(for Y Ppm (for X Y (apply wr X))) ) )</ |
(for Y Ppm (for X Y (apply wr X))) ) )</syntaxhighlight> |
||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
< |
<syntaxhighlight lang=PL/I>/* BITMAP FILE: write out a file in PPM format, P6 (binary). 14/5/2010 */ |
||
test: procedure options (main); |
test: procedure options (main); |
||
declare image (0:19,0:19) bit (24); |
declare image (0:19,0:19) bit (24); |
||
Line 1,845: | Line 1,845: | ||
write file (out) from (ch); |
write file (out) from (ch); |
||
end put_integer; |
end put_integer; |
||
end test;</ |
end test;</syntaxhighlight> |
||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
This is an extremely straight forward way to write in Prolog, more complicated methods might use DCGs: |
This is an extremely straight forward way to write in Prolog, more complicated methods might use DCGs: |
||
< |
<syntaxhighlight lang=prolog> |
||
:- module(bitmapIO, [ |
:- module(bitmapIO, [ |
||
write_ppm_p6/2]). |
write_ppm_p6/2]). |
||
Line 1,866: | Line 1,866: | ||
maplist(maplist(maplist(put_byte(Output))),Pixels), |
maplist(maplist(maplist(put_byte(Output))),Pixels), |
||
close(Output). |
close(Output). |
||
</syntaxhighlight> |
|||
</lang> |
|||
usage: |
usage: |
||
< |
<syntaxhighlight lang=prolog> |
||
:- use_module(bitmap). |
:- use_module(bitmap). |
||
:- use_module(bitmapIO). |
:- use_module(bitmapIO). |
||
Line 1,879: | Line 1,879: | ||
write_ppm_p6('AlmostAllBlack.ppm',AlmostAllBlack). |
write_ppm_p6('AlmostAllBlack.ppm',AlmostAllBlack). |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang=PureBasic>Procedure SaveImageAsPPM(Image, file$, Binary = 1) |
||
; Author Roger Rösch (Nickname Macros) |
; Author Roger Rösch (Nickname Macros) |
||
IDFiIe = CreateFile(#PB_Any, file$) |
IDFiIe = CreateFile(#PB_Any, file$) |
||
Line 1,915: | Line 1,915: | ||
CloseFile(IDFiIe) |
CloseFile(IDFiIe) |
||
EndIf |
EndIf |
||
EndProcedure</ |
EndProcedure</syntaxhighlight> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
Line 1,921: | Line 1,921: | ||
Extending the example given [[Basic_bitmap_storage#Alternative_version|here]] |
Extending the example given [[Basic_bitmap_storage#Alternative_version|here]] |
||
< |
<syntaxhighlight lang=python> |
||
# String masquerading as ppm file (version P3) |
# String masquerading as ppm file (version P3) |
||
import io |
import io |
||
Line 1,984: | Line 1,984: | ||
bitmap.writeppm(ppmfileout) |
bitmap.writeppm(ppmfileout) |
||
ppmfileout.close() |
ppmfileout.close() |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|R}}== |
=={{header|R}}== |
||
{{libheader|pixmap}} |
{{libheader|pixmap}} |
||
<syntaxhighlight lang=r> |
|||
<lang r> |
|||
# View the existing code in the library |
# View the existing code in the library |
||
library(pixmap) |
library(pixmap) |
||
Line 1,995: | Line 1,995: | ||
#Usage |
#Usage |
||
write.pnm(theimage, filename) |
write.pnm(theimage, filename) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang=racket> |
||
;P3 |
;P3 |
||
(define (bitmap->ppm bitmap output-port) |
(define (bitmap->ppm bitmap output-port) |
||
Line 2,040: | Line 2,040: | ||
;or any other output port |
;or any other output port |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Line 2,046: | Line 2,046: | ||
{{works with|Rakudo|2016-01}} |
{{works with|Rakudo|2016-01}} |
||
<lang |
<syntaxhighlight lang=raku line>class Pixel { has uint8 ($.R, $.G, $.B) } |
||
class Bitmap { |
class Bitmap { |
||
has UInt ($.width, $.height); |
has UInt ($.width, $.height); |
||
Line 2,075: | Line 2,075: | ||
} |
} |
||
$*OUT.write: $b.P6;</ |
$*OUT.write: $b.P6;</syntaxhighlight> |
||
Converted to a png. (ppm files not locally supported) |
Converted to a png. (ppm files not locally supported) |
||
Line 2,081: | Line 2,081: | ||
=={{header|REXX}}== |
=={{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. */ |
green = 00ff00 /*define a pixel with the color green. */ |
||
parse arg oFN width height color . /*obtain optional arguments from the CL*/ |
parse arg oFN width height color . /*obtain optional arguments from the CL*/ |
||
Line 2,101: | Line 2,101: | ||
call charout oFID, _ /*write the image's raster to the file.*/ |
call charout oFID, _ /*write the image's raster to the file.*/ |
||
call charout oFID /*close the output file just to be safe*/ |
call charout oFID /*close the output file just to be safe*/ |
||
/*stick a fork in it, we're all done. */</ |
/*stick a fork in it, we're all done. */</syntaxhighlight> |
||
<br><br> |
<br><br> |
||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
Extending [[Basic_bitmap_storage#Ruby]] |
Extending [[Basic_bitmap_storage#Ruby]] |
||
< |
<syntaxhighlight lang=ruby>class RGBColour |
||
def values |
def values |
||
[@red, @green, @blue] |
[@red, @green, @blue] |
||
Line 2,125: | Line 2,125: | ||
end |
end |
||
alias_method :write, :save |
alias_method :write, :save |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang=rust>use std::path::Path; |
||
use std::io::Write; |
use std::io::Write; |
||
use std::fs::File; |
use std::fs::File; |
||
Line 2,196: | Line 2,196: | ||
Ok(()) |
Ok(()) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
Extends Pixmap class from task [[Read_ppm_file#Scala|Read PPM file]]. |
Extends Pixmap class from task [[Read_ppm_file#Scala|Read PPM file]]. |
||
< |
<syntaxhighlight lang=scala>object Pixmap { |
||
def save(bm:RgbBitmap, filename:String)={ |
def save(bm:RgbBitmap, filename:String)={ |
||
val out=new DataOutputStream(new FileOutputStream(filename)) |
val out=new DataOutputStream(new FileOutputStream(filename)) |
||
Line 2,212: | Line 2,212: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
{{Works with|Scheme|R<math>^5</math>RS}} |
{{Works with|Scheme|R<math>^5</math>RS}} |
||
< |
<syntaxhighlight lang=scheme>(define (write-ppm image file) |
||
(define (write-image image) |
(define (write-image image) |
||
(define (write-row row) |
(define (write-row row) |
||
Line 2,237: | Line 2,237: | ||
(display 255) |
(display 255) |
||
(newline) |
(newline) |
||
(write-image image)))))</ |
(write-image image)))))</syntaxhighlight> |
||
Example using definitions in [[Basic bitmap storage#Scheme]]: |
Example using definitions in [[Basic bitmap storage#Scheme]]: |
||
< |
<syntaxhighlight lang=scheme>(define image (make-image 800 600)) |
||
(image-fill! image *black*) |
(image-fill! image *black*) |
||
(image-set! image 400 300 *blue*) |
(image-set! image 400 300 *blue*) |
||
(write-ppm image "out.ppm")</ |
(write-ppm image "out.ppm")</syntaxhighlight> |
||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
< |
<syntaxhighlight lang=seed7>$ include "seed7_05.s7i"; |
||
include "draw.s7i"; |
include "draw.s7i"; |
||
include "color.s7i"; |
include "color.s7i"; |
||
Line 2,269: | Line 2,269: | ||
close(ppmFile); |
close(ppmFile); |
||
end if; |
end if; |
||
end func;</ |
end func;</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang=ruby>subset Int < Number {|n| n.is_int } |
||
subset UInt < Int {|n| n >= 0 } |
subset UInt < Int {|n| n >= 0 } |
||
subset UInt8 < Int {|n| n ~~ ^256 } |
subset UInt8 < Int {|n| n ~~ ^256 } |
||
Line 2,315: | Line 2,315: | ||
} |
} |
||
%f"palette.ppm".write(b.p6, :raw)</ |
%f"palette.ppm".write(b.p6, :raw)</syntaxhighlight> |
||
=={{header|Stata}}== |
=={{header|Stata}}== |
||
Line 2,321: | Line 2,321: | ||
P3 format only, with Mata. |
P3 format only, with Mata. |
||
< |
<syntaxhighlight lang=stata>mata |
||
void writeppm(name, r, g, b) { |
void writeppm(name, r, g, b) { |
||
n = rows(r) |
n = rows(r) |
||
Line 2,340: | Line 2,340: | ||
b = J(6, 6, 255) |
b = J(6, 6, 255) |
||
writeppm("image.ppm", r, g, b) |
writeppm("image.ppm", r, g, b) |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
{{libheader|Tk}} |
{{libheader|Tk}} |
||
Referring to [[Basic bitmap storage#Tcl]]: |
Referring to [[Basic bitmap storage#Tcl]]: |
||
< |
<syntaxhighlight lang=tcl>package require Tk |
||
proc output_ppm {image filename} { |
proc output_ppm {image filename} { |
||
Line 2,363: | Line 2,363: | ||
binary scan [read $fh 3] c3 pixel |
binary scan [read $fh 3] c3 pixel |
||
foreach colour $pixel {puts [expr {$colour & 0xff}]} ;# ==> 255 \n 0 \n 0 \n |
foreach colour $pixel {puts [expr {$colour & 0xff}]} ;# ==> 255 \n 0 \n 0 \n |
||
close $fh</ |
close $fh</syntaxhighlight> |
||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
Line 2,370: | Line 2,370: | ||
Add the following function to the <tt>Bitmap_t</tt> type |
Add the following function to the <tt>Bitmap_t</tt> type |
||
< |
<syntaxhighlight lang=bash> function write { |
||
_.to_s > "$1" |
_.to_s > "$1" |
||
}</ |
}</syntaxhighlight> |
||
Then you can: |
Then you can: |
||
< |
<syntaxhighlight lang=bash>Bitmap_t b |
||
# do stuff to b, and save it: |
# do stuff to b, and save it: |
||
b.write '$HOME/tmp/bitmap.ppm'</ |
b.write '$HOME/tmp/bitmap.ppm'</syntaxhighlight> |
||
=={{header|Vedit macro language}}== |
=={{header|Vedit macro language}}== |
||
Line 2,403: | Line 2,403: | ||
=={{header|Visual Basic .NET}}== |
=={{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 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) |
Dim bufferSize As Integer = header.Length + (rasterBitmap.Width * rasterBitmap.Height * 3) |
||
Line 2,419: | Line 2,419: | ||
Next |
Next |
||
My.Computer.FileSystem.WriteAllBytes(filepath, bytes, False) |
My.Computer.FileSystem.WriteAllBytes(filepath, bytes, False) |
||
End Sub</ |
End Sub</syntaxhighlight> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|DOME}} |
{{libheader|DOME}} |
||
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. |
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>import "graphics" for Canvas, ImageData, Color |
||
import "dome" for Window, Process |
import "dome" for Window, Process |
||
import "io" for FileSystem |
import "io" for FileSystem |
||
Line 2,469: | Line 2,469: | ||
} |
} |
||
var Game = Bitmap.new("Bitmap - write to PPM file", 320, 320)</ |
var Game = Bitmap.new("Bitmap - write to PPM file", 320, 320)</syntaxhighlight> |
||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang=XPL0>include c:\cxpl\codes; \intrinsic 'code' declarations |
||
def Width=180, Height=135, Color=$123456; |
def Width=180, Height=135, Color=$123456; |
||
Line 2,509: | Line 2,509: | ||
WriteImage; |
WriteImage; |
||
SetVid(3); \restore display to normal text mode |
SetVid(3); \restore display to normal text mode |
||
]</ |
]</syntaxhighlight> |
||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
< |
<syntaxhighlight lang=Yabasic>clear screen |
||
wid = 150 : hei = 200 |
wid = 150 : hei = 200 |
||
Line 2,541: | Line 2,541: | ||
poke #fn, asc("\n") |
poke #fn, asc("\n") |
||
close #fn</ |
close #fn</syntaxhighlight> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang=zkl>// convert Windows BMP (bit map) image to PPM |
||
// Read BMP file |
// Read BMP file |
||
Line 2,560: | Line 2,560: | ||
} |
} |
||
File("foo.ppm","wb").write(ppm); // File.stdout isn't binary, let GC close file</ |
File("foo.ppm","wb").write(ppm); // File.stdout isn't binary, let GC close file</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |