Jump to content

Bitmap/Write a PPM file: Difference between revisions

m
Fixed lang tags.
m (→‎{{header|Python}}: Remove the old func.)
m (Fixed lang tags.)
Line 6:
 
=={{header|Ada}}==
<lang ada>with Ada.Characters.Latin_1;
with Ada.Characters.Latin_1;
with Ada.Streams.Stream_IO; use Ada.Streams.Stream_IO;
 
Line 32 ⟶ 31:
end loop;
Character'Write (Stream (File), LF);
end Put_PPM;</lang>
</lang>
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|AutoHotkey}}==
<lang AutoHotkey>imwrite_ppm(filename, width, height, colors)
imwrite_ppm(filename, width, height, colors)
{
ppmfile =
Line 48 ⟶ 45:
}
 
imwrite_ppm("blank.ppm", 256, 256, 255)</lang>
</lang>
 
=={{header|C}}==
Line 72 ⟶ 68:
=={{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.
<lang csharp>using System;
using System;
using System.IO;
class PPMWriter
Line 96 ⟶ 91:
writerB.Close();
}
}</lang>
}
</lang>
 
=={{header|Common Lisp}}==
Line 128 ⟶ 122:
(write-byte green stream)
(write-byte blue stream)))))))
filename)</lang>
</lang>
 
=={{header|D}}==
Line 138 ⟶ 131:
 
Two additional imports are needed:
<lang D>import tango.io.protocol.model.IWriterWriter;
<lang D>
import tango.io.protocol.Writermodel.IWriter;</lang>
import tango.io.protocol.model.IWriter;
</lang>
 
P6Image will implement IWritable interface
<lang D>class P6Image : IWritable {
class P6Image : IWritable {
//....
 
Line 178 ⟶ 168:
output (); // flush
}
}</lang>
}
</lang>
 
Saving a file is easy as a pie:
<lang D>auto p6 = new P6Image(new FileConduit("image.ppm"));
<lang D>
auto p6 = new P6Image(new FileConduit("image.ppm"));
auto write = new Writer(new FileConduit("out.ppm", FileConduit.WriteCreate));
 
write (p6);</lang>
</lang>
 
=={{header|E}}==
Line 194 ⟶ 181:
 
=={{header|Forth}}==
<lang forth>: write-ppm { bmp fid -- }
s" P6" fid write-line throw
bmp bdim swap
0 <# bl hold #s #> fid write-file throw
0 <# #s #> fid write-line throw
s" 255" fid write-line throw
bmp bdata bmp bdim * pixels
bounds do
i 3 fid write-file throw
pixel +loop ;
 
s" red.ppm" w/o create-file throw
test over write-ppm
close-file throw</lang>
 
=={{header|Fortran}}==
Line 299 ⟶ 286:
=={{header|J}}==
'''Solution:'''
<lang j>require 'files'
require 'files'
 
NB. ($x) is height, width, colors per pixel
Line 309 ⟶ 295:
'''Example:'''
Using routines from [[Basic_bitmap_storage#J|Basic Bitmap Storage]]:
<lang j> NB. create 10 by 10 block of magenta pixels in top right quadrant of a 300 wide by 600 high green image
<lang j>
NB. create 10 by 10 block of magenta pixels in top right quadrant of a 300 wide by 600 high green image
myimg=: ((145 + pixellist) ; 255 0 255) setPixels 0 255 0 makeRGB 600 200
myimg writeppm jpath '~temp/myimg.ppm'
360015</lang>
</lang>
 
=={{header|Modula-3}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.