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
(7 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 267 ⟶ 272:
=={{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===
Line 353 ⟶ 360:
else
let
val @(r, g, b) = rgb24_values pix[x, y]
in
fprintln! (outf, r, " ", g, " ", b);
Line 375 ⟶ 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 420 ⟶ 425:
(*------------------------------------------------------------------*)
</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}}==
Line 2,517 ⟶ 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,541 ⟶ 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,564 ⟶ 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
14

edits