Bitmap/Write a PPM file: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(→‎{{header|Fortran}}: rewrote rgbimage module)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 33:
end Put_PPM;</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|Aime}}==
Line 53 ⟶ 52:
} while ((i += 16) < w);
} while ((j += 1) < h);</lang>
 
 
=={{header|AutoHotkey}}==
Line 90 ⟶ 88:
}
</lang>
 
 
=={{header|AWK}}==
Line 507 ⟶ 504:
PutPixel(g, 2, 3, [0, 0, 0]);
WriteImage("example.ppm", g);</lang>
 
=={{header|Go}}==
Code below writes 8-bit P6 format only. See Bitmap task for additional file needed to build working raster package.
Line 1,168 ⟶ 1,166:
fwrite(fid,[r,g,b]','uint8');
fclose(fid);</lang>
 
 
=={{header|Modula-3}}==
Line 1,288 ⟶ 1,285:
xmax => 150, ymax => 150);
$image->write(file => 'bitmap.ppm') or die $image->errstr;</lang>
 
=={{header|Perl 6}}==
{{works with|Rakudo|2016-01}}
 
<lang perl6>class Pixel { has uint8 ($.R, $.G, $.B) }
class Bitmap {
has UInt ($.width, $.height);
has Pixel @!data;
 
method fill(Pixel $p) {
@!data = $p.clone xx ($!width*$!height)
}
method pixel(
$i where ^$!width,
$j where ^$!height
--> Pixel
) is rw { @!data[$i*$!height + $j] }
 
method data { @!data }
}
 
role PPM {
method P6 returns Blob {
"P6\n{self.width} {self.height}\n255\n".encode('ascii')
~ Blob.new: flat map { .R, .G, .B }, self.data
}
}
 
my Bitmap $b = Bitmap.new(width => 125, height => 125) but PPM;
for flat ^$b.height X ^$b.width -> $i, $j {
$b.pixel($i, $j) = Pixel.new: :R($i*2), :G($j*2), :B(255-$i*2);
}
 
$*OUT.write: $b.P6;</lang>
Converted to a png. (ppm files not locally supported)
 
[[File:Ppm-perl6.png‎]]
 
=={{header|Phix}}==
Line 1,414 ⟶ 1,374:
$b->setPixel(0, 15, array(255,0,0));
$b->writeP6('p6.ppm');</lang>
 
=={{header|PicoLisp}}==
<lang PicoLisp>(de ppmWrite (Ppm File)
(out File
(prinl "P6")
(prinl (length (car Ppm)) " " (length Ppm))
(prinl 255)
(for Y Ppm (for X Y (apply wr X))) ) )</lang>
 
=={{header|PL/I}}==
Line 1,463 ⟶ 1,431:
end put_integer;
end test;</lang>
 
=={{header|PicoLisp}}==
<lang PicoLisp>(de ppmWrite (Ppm File)
(out File
(prinl "P6")
(prinl (length (car Ppm)) " " (length Ppm))
(prinl 255)
(for Y Ppm (for X Y (apply wr X))) ) )</lang>
 
=={{header|PureBasic}}==
Line 1,632 ⟶ 1,592:
 
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2016-01}}
 
<lang perl6>class Pixel { has uint8 ($.R, $.G, $.B) }
class Bitmap {
has UInt ($.width, $.height);
has Pixel @!data;
 
method fill(Pixel $p) {
@!data = $p.clone xx ($!width*$!height)
}
method pixel(
$i where ^$!width,
$j where ^$!height
--> Pixel
) is rw { @!data[$i*$!height + $j] }
 
method data { @!data }
}
 
role PPM {
method P6 returns Blob {
"P6\n{self.width} {self.height}\n255\n".encode('ascii')
~ Blob.new: flat map { .R, .G, .B }, self.data
}
}
 
my Bitmap $b = Bitmap.new(width => 125, height => 125) but PPM;
for flat ^$b.height X ^$b.width -> $i, $j {
$b.pixel($i, $j) = Pixel.new: :R($i*2), :G($j*2), :B(255-$i*2);
}
 
$*OUT.write: $b.P6;</lang>
Converted to a png. (ppm files not locally supported)
 
[[File:Ppm-perl6.png‎]]
 
=={{header|REXX}}==
Line 1,930 ⟶ 1,928:
# do stuff to b, and save it:
b.write '$HOME/tmp/bitmap.ppm'</lang>
 
=={{header|Visual Basic .NET}}==
 
<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)
Dim bytes(bufferSize - 1) As Byte
Buffer.BlockCopy(Encoding.ASCII.GetBytes(header.ToString), 0, bytes, 0, header.Length)
Dim index As Integer = header.Length
For y As Integer = 0 To rasterBitmap.Height - 1
For x As Integer = 0 To rasterBitmap.Width - 1
Dim color As Rgb = rasterBitmap.GetPixel(x, y)
bytes(index) = color.R
bytes(index + 1) = color.G
bytes(index + 2) = color.B
index += 3
Next
Next
My.Computer.FileSystem.WriteAllBytes(filepath, bytes, False)
End Sub</lang>
 
=={{header|Vedit macro language}}==
Line 1,973 ⟶ 1,951:
Return
</pre>
 
=={{header|Visual Basic .NET}}==
 
<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)
Dim bytes(bufferSize - 1) As Byte
Buffer.BlockCopy(Encoding.ASCII.GetBytes(header.ToString), 0, bytes, 0, header.Length)
Dim index As Integer = header.Length
For y As Integer = 0 To rasterBitmap.Height - 1
For x As Integer = 0 To rasterBitmap.Width - 1
Dim color As Rgb = rasterBitmap.GetPixel(x, y)
bytes(index) = color.R
bytes(index + 1) = color.G
bytes(index + 2) = color.B
index += 3
Next
Next
My.Computer.FileSystem.WriteAllBytes(filepath, bytes, False)
End Sub</lang>
 
=={{header|XPL0}}==
10,333

edits