Bitmap/Read an image through a pipe: Difference between revisions
Content added Content deleted
(added Perl programming solution) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 7: | Line 7: | ||
{{works with | AutoHotkey_L}} |
{{works with | AutoHotkey_L}} |
||
Uses [http://www.autohotkey.com/forum/viewtopic.php?t=16823 StdoutTovar.ahk] |
Uses [http://www.autohotkey.com/forum/viewtopic.php?t=16823 StdoutTovar.ahk] |
||
< |
<syntaxhighlight lang=AutoHotkey>ppm := Run("cmd.exe /c convert lena50.jpg ppm:-") |
||
; pipe in from imagemagick |
; pipe in from imagemagick |
||
img := ppm_read("", ppm) ; |
img := ppm_read("", ppm) ; |
||
Line 67: | Line 67: | ||
#include bitmap_storage.ahk ; from http://rosettacode.org/wiki/Basic_bitmap_storage/AutoHotkey |
#include bitmap_storage.ahk ; from http://rosettacode.org/wiki/Basic_bitmap_storage/AutoHotkey |
||
#include run.ahk ; http://www.autohotkey.com/forum/viewtopic.php?t=16823 |
#include run.ahk ; http://www.autohotkey.com/forum/viewtopic.php?t=16823 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|C}}== |
=={{header|C}}== |
||
Line 73: | Line 73: | ||
Here I've used '''convert''' by ImageMagick. It is up to the program to ''understand'' the source file type; in this way, we can read theoretically any image format ImageMagick can handle. The <tt>get_ppm</tt> function defined in [[Read ppm file]] is used. |
Here I've used '''convert''' by ImageMagick. It is up to the program to ''understand'' the source file type; in this way, we can read theoretically any image format ImageMagick can handle. The <tt>get_ppm</tt> function defined in [[Read ppm file]] is used. |
||
< |
<syntaxhighlight lang=c>image read_image(const char *name);</syntaxhighlight> |
||
< |
<syntaxhighlight lang=c>#include "imglib.h" |
||
#define MAXCMDBUF 100 |
#define MAXCMDBUF 100 |
||
Line 102: | Line 102: | ||
} |
} |
||
return NULL; |
return NULL; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
This example uses convert to convert the test image for the flood fill task. It reads through the pipe as required for this task, then writes as a .ppm file convenient for the flood fill task. |
This example uses convert to convert the test image for the flood fill task. It reads through the pipe as required for this task, then writes as a .ppm file convenient for the flood fill task. |
||
< |
<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 135: | Line 135: | ||
log.Fatal(err) |
log.Fatal(err) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{works with|Julia|0.6}} |
{{works with|Julia|0.6}} |
||
< |
<syntaxhighlight lang=julia>using Images, FileIO |
||
img = load("data/bitmapOutputTest.jpg") |
img = load("data/bitmapOutputTest.jpg") |
||
save("data/bitmapOutputTest.ppm", img)</ |
save("data/bitmapOutputTest.ppm", img)</syntaxhighlight> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{works with|Ubuntu 16.04}} |
{{works with|Ubuntu 16.04}} |
||
The code for this is similar to that for the [[Bitmap/Read a PPM file]] task except that the .jpg file is converted via a pipe to .ppm format using the ImageMagick 'convert' tool and stored in a BasicBitmapStorage object. It is then converted to grayscale and saved back to disk as a .jpg file. |
The code for this is similar to that for the [[Bitmap/Read a PPM file]] task except that the .jpg file is converted via a pipe to .ppm format using the ImageMagick 'convert' tool and stored in a BasicBitmapStorage object. It is then converted to grayscale and saved back to disk as a .jpg file. |
||
< |
<syntaxhighlight lang=scala>// Version 1.2.40 |
||
import java.awt.Color |
import java.awt.Color |
||
Line 282: | Line 282: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
Line 288: | Line 288: | ||
First, the <code>loadPPM()</code> method is altered to allow passing an existing file handle: |
First, the <code>loadPPM()</code> method is altered to allow passing an existing file handle: |
||
< |
<syntaxhighlight lang=lua>function Bitmap:loadPPM(filename, fp) |
||
if not fp then fp = io.open(filename, "rb") end |
if not fp then fp = io.open(filename, "rb") end |
||
if not fp then return end |
if not fp then return end |
||
Line 300: | Line 300: | ||
end |
end |
||
fp:close() |
fp:close() |
||
end</ |
end</syntaxhighlight> |
||
Then, for the actual "read-from-pipe" task, a Lua environment that supports <code>io.popen()</code> is required: |
Then, for the actual "read-from-pipe" task, a Lua environment that supports <code>io.popen()</code> is required: |
||
< |
<syntaxhighlight lang=lua>local bitmap = Bitmap(0,0) |
||
fp = io.popen("magick Lenna100.jpg ppm:-", "rb") |
fp = io.popen("magick Lenna100.jpg ppm:-", "rb") |
||
bitmap:loadPPM(nil, fp) |
bitmap:loadPPM(nil, fp) |
||
bitmap:savePPM("Lenna100.ppm") -- just as "proof"</ |
bitmap:savePPM("Lenna100.ppm") -- just as "proof"</syntaxhighlight> |
||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
Based off the Julia program. |
Based off the Julia program. |
||
< |
<syntaxhighlight lang=Mathematica>Export["data/bitmapOutputTest.ppm",Import["data/bitmapOutputTest.jpg"]];</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
Using "jpegtopnm" from Netpbm suite. Input is a JPEG file and result (the PPM file) is sent to stdout. The procedure "readPPM" reads directly from the stream and build the image container. |
Using "jpegtopnm" from Netpbm suite. Input is a JPEG file and result (the PPM file) is sent to stdout. The procedure "readPPM" reads directly from the stream and build the image container. |
||
< |
<syntaxhighlight lang=Nim>import bitmap |
||
import osproc |
import osproc |
||
import ppm_read |
import ppm_read |
||
Line 326: | Line 326: | ||
let image = stream.readPPM() |
let image = stream.readPPM() |
||
echo image.w, " ", image.h |
echo image.w, " ", image.h |
||
p.close()</ |
p.close()</syntaxhighlight> |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
The <code>read_ppm</code> function of the page [[read ppm file]] and used by the code below would need to be changed to take as parameter an input channel instead of the filename. |
The <code>read_ppm</code> function of the page [[read ppm file]] and used by the code below would need to be changed to take as parameter an input channel instead of the filename. |
||
< |
<syntaxhighlight lang=ocaml>let read_image ~filename = |
||
if not(Sys.file_exists filename) |
if not(Sys.file_exists filename) |
||
then failwith(Printf.sprintf "the file %s does not exist" filename); |
then failwith(Printf.sprintf "the file %s does not exist" filename); |
||
Line 337: | Line 337: | ||
let img = read_ppm ~ic in |
let img = read_ppm ~ic in |
||
(img) |
(img) |
||
;;</ |
;;</syntaxhighlight> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang=perl># 20211226 Perl programming solution |
||
use strict; |
use strict; |
||
Line 364: | Line 364: | ||
Imager::i_writeppm_wiol $im, $IO2 ; |
Imager::i_writeppm_wiol $im, $IO2 ; |
||
close $fh2; |
close $fh2; |
||
undef($im);</ |
undef($im);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>file output.ppm |
<pre>file output.ppm |
||
Line 374: | Line 374: | ||
Uses the demo\rosetta\viewppm.exw utility to accomplish this task.<br> |
Uses the demo\rosetta\viewppm.exw utility to accomplish this task.<br> |
||
The returned data is raw binary, so you can either write it direct or chuck it through read_ppm/write_ppm. |
The returned data is raw binary, so you can either write it direct or chuck it through read_ppm/write_ppm. |
||
<!--< |
<!--<syntaxhighlight lang=Phix>(notonline)--> |
||
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Bitmap_Read_an_image_through_a_pipe.exw</span> |
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Bitmap_Read_an_image_through_a_pipe.exw</span> |
||
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- file i/o, system_exec(), pipes[!!]</span> |
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- file i/o, system_exec(), pipes[!!]</span> |
||
Line 408: | Line 408: | ||
<span style="color: #0000FF;">?</span><span style="color: #008000;">"done"</span> |
<span style="color: #0000FF;">?</span><span style="color: #008000;">"done"</span> |
||
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span> |
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang=PicoLisp>(setq *Ppm (ppmRead '("convert" "img.jpg" "ppm:-")))</syntaxhighlight> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang=Python> |
||
""" |
""" |
||
Adapted from https://stackoverflow.com/questions/26937143/ppm-to-jpeg-jpg-conversion-for-python-3-4-1 |
Adapted from https://stackoverflow.com/questions/26937143/ppm-to-jpeg-jpg-conversion-for-python-3-4-1 |
||
Line 427: | Line 427: | ||
im = Image.open("boxes_1.jpg") |
im = Image.open("boxes_1.jpg") |
||
im.save("boxes_1v2.ppm") |
im.save("boxes_1v2.ppm") |
||
</syntaxhighlight> |
|||
</lang> |
|||
Does not need to pipe through a conversion utility |
Does not need to pipe through a conversion utility |
||
because the Pillow module does the conversion. |
because the Pillow module does the conversion. |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang=racket> |
||
(define (read-ppm port) |
(define (read-ppm port) |
||
Line 463: | Line 463: | ||
bmp) |
bmp) |
||
(image->bmp "input.jpg")</ |
(image->bmp "input.jpg")</syntaxhighlight> |
||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Line 472: | Line 472: | ||
Uses imagemagick convert to pipe the image in. |
Uses imagemagick convert to pipe the image in. |
||
<lang |
<syntaxhighlight lang=raku line>class Pixel { has UInt ($.R, $.G, $.B) } |
||
class Bitmap { |
class Bitmap { |
||
has UInt ($.width, $.height); |
has UInt ($.width, $.height); |
||
Line 504: | Line 504: | ||
{ Pixel.new(R => .[0], G => .[1], B => .[2]) }; |
{ Pixel.new(R => .[0], G => .[1], B => .[2]) }; |
||
'./camelia.ppm'.IO.open(:bin, :w).write: $b.P6;</ |
'./camelia.ppm'.IO.open(:bin, :w).write: $b.P6;</syntaxhighlight> |
||
See [https://github.com/thundergnat/rc/blob/master/img/camelia.png camelia image here]. |
See [https://github.com/thundergnat/rc/blob/master/img/camelia.png camelia image here]. |
||
Line 511: | Line 511: | ||
Uses [[Raster graphics operations/Ruby]]. |
Uses [[Raster graphics operations/Ruby]]. |
||
< |
<syntaxhighlight lang=ruby># frozen_string_literal: true |
||
require_relative 'raster_graphics' |
require_relative 'raster_graphics' |
||
Line 555: | Line 555: | ||
bitmap = Pixmap.open_from_jpeg('foto.jpg') |
bitmap = Pixmap.open_from_jpeg('foto.jpg') |
||
bitmap.save('foto.ppm') |
bitmap.save('foto.ppm') |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
{{works with|Tcl|8.6}} |
{{works with|Tcl|8.6}} |
||
{{libheader|Tk}} |
{{libheader|Tk}} |
||
< |
<syntaxhighlight lang=tcl>package require Tk |
||
proc magickalReadImage {bufferImage fileName} { |
proc magickalReadImage {bufferImage fileName} { |
||
Line 569: | Line 569: | ||
close $f |
close $f |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
Line 578: | Line 578: | ||
We can now use this plug-in in the following script which calls ''ImageMagick'' to convert the ''output_piped.jpg'' file to a ''ppm'' file so that we can load the latter, convert it to a gray scale image, display it and save it to a .jpg file. |
We can now use this plug-in in the following script which calls ''ImageMagick'' to convert the ''output_piped.jpg'' file to a ''ppm'' file so that we can load the latter, convert it to a gray scale image, display it and save it to a .jpg file. |
||
< |
<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 659: | Line 659: | ||
} |
} |
||
var Game = Bitmap.new("output_piped.jpg", "output_piped.ppm", "output_piped_gs.jpg", 350, 350)</ |
var Game = Bitmap.new("output_piped.jpg", "output_piped.ppm", "output_piped_gs.jpg", 350, 350)</syntaxhighlight> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
Line 666: | Line 666: | ||
Using the convert utility by ImageMagick: |
Using the convert utility by ImageMagick: |
||
< |
<syntaxhighlight lang=zkl>p:=System.popen(0'|convert "fractalTree.jpg" ppm:-|,"r"); |
||
img:=PPM.readPPM(p); p.close();</ |
img:=PPM.readPPM(p); p.close();</syntaxhighlight> |
||
{{omit from|PARI/GP}} |
{{omit from|PARI/GP}} |