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

Content added Content deleted
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 1: Line 1:
{{task|Raster graphics operations}}
[[Category:Input Output]]
[[Category:Input Output]]
[[Category:Less Than 20 Examples]]
{{task|Raster graphics operations}}


This task is the ''opposite'' of the [[PPM conversion through a pipe]]. In this task, using a delegate tool (like '''cjpeg''', one of the netpbm package, or '''convert''' of the ImageMagick package) we read an image file and load it into the data storage type [[Basic bitmap storage|defined here]]. We can also use the code from [[Read ppm file]], so that we can use PPM format like a (natural) bridge between the foreign image format and our simple data storage.
This task is the ''opposite'' of the [[PPM conversion through a pipe]]. In this task, using a delegate tool (like '''cjpeg''', one of the netpbm package, or '''convert''' of the ImageMagick package) we read an image file and load it into the data storage type [[Basic bitmap storage|defined here]]. We can also use the code from [[Read ppm file]], so that we can use PPM format like a (natural) bridge between the foreign image format and our simple data storage.

=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
{{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:-")
<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 68: Line 68:
#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>
</syntaxhighlight>

=={{header|C}}==
=={{header|C}}==
{{works with|POSIX|.1-2001}}
{{works with|POSIX|.1-2001}}
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">image read_image(const char *name);</syntaxhighlight>


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


#define MAXCMDBUF 100
#define MAXCMDBUF 100
Line 103: Line 102:
return NULL;
return NULL;
}</syntaxhighlight>
}</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
<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 136: Line 134:
}
}
}</syntaxhighlight>
}</syntaxhighlight>

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


<syntaxhighlight 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)</syntaxhighlight>
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
<syntaxhighlight lang="scala">// Version 1.2.40


import java.awt.Color
import java.awt.Color
Line 283: Line 279:
}
}
}</syntaxhighlight>
}</syntaxhighlight>

=={{header|Lua}}==
=={{header|Lua}}==
Uses Bitmap class [[Bitmap#Lua|here]], with an RGB tuple pixel representation, and the rudimentary PPM support [[Bitmap/Flood_fill#Lua|here]], and the Lenna image [[:File:Lenna100.jpg|here]].
Uses Bitmap class [[Bitmap#Lua|here]], with an RGB tuple pixel representation, and the rudimentary PPM support [[Bitmap/Flood_fill#Lua|here]], and the Lenna image [[:File:Lenna100.jpg|here]].


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)
<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 302: Line 297:
end</syntaxhighlight>
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)
<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"</syntaxhighlight>
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>
<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
<syntaxhighlight lang="nim">import bitmap
import osproc
import osproc
import ppm_read
import ppm_read
Line 327: Line 320:
echo image.w, " ", image.h
echo image.w, " ", image.h
p.close()</syntaxhighlight>
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 =
<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 338: Line 330:
(img)
(img)
;;</syntaxhighlight>
;;</syntaxhighlight>

=={{header|Perl}}==
=={{header|Perl}}==
<syntaxhighlight lang=perl># 20211226 Perl programming solution
<syntaxhighlight lang="perl"># 20211226 Perl programming solution


use strict;
use strict;
Line 370: Line 361:
magick identify output.ppm
magick identify output.ppm
output.ppm PPM 512x512 512x512+0+0 8-bit sRGB 786464B 0.000u 0:00.014</pre>
output.ppm PPM 512x512 512x512+0+0 8-bit sRGB 786464B 0.000u 0:00.014</pre>

=={{header|Phix}}==
=={{header|Phix}}==
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)-->
<!--<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 409: Line 399:
<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>-->
<!--</syntaxhighlight>-->

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

=={{header|Python}}==
=={{header|Python}}==
<syntaxhighlight 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 430: Line 418:
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>
<syntaxhighlight lang="racket">


(define (read-ppm port)
(define (read-ppm port)
Line 464: Line 451:


(image->bmp "input.jpg")</syntaxhighlight>
(image->bmp "input.jpg")</syntaxhighlight>

=={{header|Raku}}==
=={{header|Raku}}==
(formerly Perl 6)
(formerly Perl 6)
Line 472: Line 458:
Uses imagemagick convert to pipe the image in.
Uses imagemagick convert to pipe the image in.


<syntaxhighlight lang=raku line>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 507: Line 493:


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].

=={{header|Ruby}}==
=={{header|Ruby}}==
Uses [[Raster graphics operations/Ruby]].
Uses [[Raster graphics operations/Ruby]].


<syntaxhighlight lang=ruby># frozen_string_literal: true
<syntaxhighlight lang="ruby"># frozen_string_literal: true


require_relative 'raster_graphics'
require_relative 'raster_graphics'
Line 556: Line 541:
bitmap.save('foto.ppm')
bitmap.save('foto.ppm')
</syntaxhighlight>
</syntaxhighlight>

=={{header|Tcl}}==
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
{{works with|Tcl|8.6}}
{{libheader|Tk}}
{{libheader|Tk}}
<syntaxhighlight lang=tcl>package require Tk
<syntaxhighlight lang="tcl">package require Tk


proc magickalReadImage {bufferImage fileName} {
proc magickalReadImage {bufferImage fileName} {
Line 570: Line 554:
}
}
}</syntaxhighlight>
}</syntaxhighlight>

=={{header|Wren}}==
=={{header|Wren}}==
{{libheader|DOME}}
{{libheader|DOME}}
Line 578: Line 561:
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
<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 660: Line 643:


var Game = Bitmap.new("output_piped.jpg", "output_piped.ppm", "output_piped_gs.jpg", 350, 350)</syntaxhighlight>
var Game = Bitmap.new("output_piped.jpg", "output_piped.ppm", "output_piped_gs.jpg", 350, 350)</syntaxhighlight>

=={{header|zkl}}==
=={{header|zkl}}==
{{trans|C}}
{{trans|C}}
Line 666: Line 648:


Using the convert utility by ImageMagick:
Using the convert utility by ImageMagick:
<syntaxhighlight 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();</syntaxhighlight>
img:=PPM.readPPM(p); p.close();</syntaxhighlight>

{{omit from|PARI/GP}}
{{omit from|PARI/GP}}
{{omit from|TI-83 BASIC}} {{omit from|TI-89 BASIC}} <!-- Does not have an external OS/command processor. -->
{{omit from|TI-83 BASIC}}
{{omit from|TI-89 BASIC}} <!-- Does not have an external OS/command processor. -->
[[Category:Less Than 20 Examples]]