Bitmap/Read an image through a pipe: Difference between revisions

Content added Content deleted
(added Perl programming solution)
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]
<lang AutoHotkey>ppm := Run("cmd.exe /c convert lena50.jpg ppm:-")
<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.


<lang c>image read_image(const char *name);</lang>
<syntaxhighlight lang=c>image read_image(const char *name);</syntaxhighlight>


<lang c>#include "imglib.h"
<syntaxhighlight lang=c>#include "imglib.h"


#define MAXCMDBUF 100
#define MAXCMDBUF 100
Line 102: Line 102:
}
}
return NULL;
return NULL;
}</lang>
}</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.
<lang go>package main
<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)
}
}
}</lang>
}</syntaxhighlight>


=={{header|Julia}}==
=={{header|Julia}}==
{{works with|Julia|0.6}}
{{works with|Julia|0.6}}


<lang julia>using Images, FileIO
<syntaxhighlight lang=julia>using Images, FileIO


img = load("data/bitmapOutputTest.jpg")
img = load("data/bitmapOutputTest.jpg")
save("data/bitmapOutputTest.ppm", img)</lang>
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.
<lang scala>// Version 1.2.40
<syntaxhighlight lang=scala>// Version 1.2.40


import java.awt.Color
import java.awt.Color
Line 282: Line 282:
}
}
}
}
}</lang>
}</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:
<lang lua>function Bitmap:loadPPM(filename, fp)
<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</lang>
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:
<lang lua>local bitmap = Bitmap(0,0)
<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"</lang>
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.
<lang Mathematica>Export["data/bitmapOutputTest.ppm",Import["data/bitmapOutputTest.jpg"]];</lang>
<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.


<lang Nim>import bitmap
<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()</lang>
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.
<lang ocaml>let read_image ~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)
;;</lang>
;;</syntaxhighlight>


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl># 20211226 Perl programming solution
<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);</lang>
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.
<!--<lang Phix>(notonline)-->
<!--<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>
<!--</lang>-->
<!--</syntaxhighlight>-->


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<lang PicoLisp>(setq *Ppm (ppmRead '("convert" "img.jpg" "ppm:-")))</lang>
<syntaxhighlight lang=PicoLisp>(setq *Ppm (ppmRead '("convert" "img.jpg" "ppm:-")))</syntaxhighlight>


=={{header|Python}}==
=={{header|Python}}==
<lang 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}}==
<lang racket>
<syntaxhighlight lang=racket>


(define (read-ppm port)
(define (read-ppm port)
Line 463: Line 463:
bmp)
bmp)


(image->bmp "input.jpg")</lang>
(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 perl6>class Pixel { has UInt ($.R, $.G, $.B) }
<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;</lang>
'./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]].


<lang ruby># frozen_string_literal: true
<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}}
<lang tcl>package require Tk
<syntaxhighlight lang=tcl>package require Tk


proc magickalReadImage {bufferImage fileName} {
proc magickalReadImage {bufferImage fileName} {
Line 569: Line 569:
close $f
close $f
}
}
}</lang>
}</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.


<lang ecmascript>import "graphics" for Canvas, ImageData, Color
<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)</lang>
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:
<lang zkl>p:=System.popen(0'|convert "fractalTree.jpg" ppm:-|,"r");
<syntaxhighlight lang=zkl>p:=System.popen(0'|convert "fractalTree.jpg" ppm:-|,"r");
img:=PPM.readPPM(p); p.close();</lang>
img:=PPM.readPPM(p); p.close();</syntaxhighlight>


{{omit from|PARI/GP}}
{{omit from|PARI/GP}}