Bitmap/Write a PPM file: Difference between revisions

→‎Ada: Fixed tipo an better reference
(→‎Ada: Fixed tipo an better reference)
Tags: Mobile edit Mobile web edit
(5 intermediate revisions by 4 users not shown)
Line 76:
print(bitmap.writeppmp3())
 
File(‘tmp.ppm’, ‘w’WRITE).write_bytes(bitmap.writeppmp6())</syntaxhighlight>
 
{{out}}
Line 89:
0 0 0 255 255 255 0 0 0 0 0 0
</pre>
 
=={{header|Action!}}==
{{libheader|Action! Bitmap tools}}
Line 203 ⟶ 204:
<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 228 ⟶ 232:
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.
 
=={{header|Aime}}==
<syntaxhighlight lang="aime">integer i, h, j, w;
Line 269 ⟶ 274:
 
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>.
 
''(An attentive reader might notice that this is not really an adequate design, because the ''<code>rgb24</code>'' type is indistinguishable from any other triple of uint8 values. Thus the template system will not do what we actually want it to do, once we introduce other pixel types. It is a subtle matter. Now that ''I'' have noticed this, I plan to address the issue and post new code.)''
 
===The ATS static file===
Line 357 ⟶ 360:
else
let
val @(r, g, b) = rgb24_values pix[x, y]
in
fprintln! (outf, r, " ", g, " ", b);
Line 379 ⟶ 382:
main0 () =
let
val bgcolor = rgb24_make (217u, 217u, 214u)
extern castfn i2u8 : int -<> uint8
and fgcolor1 = rgb24_make (210, 0, 0)
 
valand bgcolorfgcolor2 = @rgb24_make (i2u8 2170, i2u8 217150, i2u8 2140)
and fgcolor1fgcolor3 = @rgb24_make (i2u8 2100, i2u8 0, i2u8 0220)
and fgcolor2 = @(i2u8 0, i2u8 150, i2u8 0)
and fgcolor3 = @(i2u8 0, i2u8 0, i2u8 220)
 
stadef w = 300
Line 2,531 ⟶ 2,532:
=={{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="ecmascriptwren">import "graphics" for Canvas, ImageData, Color
import "dome" for Window, Process
import "io" for FileSystem
import "./str" for Strs
 
class Bitmap {
Line 2,555 ⟶ 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,578 ⟶ 2,580:
 
var Game = Bitmap.new("Bitmap - write to PPM file", 320, 320)</syntaxhighlight>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
4

edits