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

added autohotkey implementation
(added autohotkey implementation)
Line 4:
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}}==
{{works with | AutoHotkey_L}}
Uses [http://www.autohotkey.com/forum/viewtopic.php?t=16823 StdoutTovar.ahk]
<lang AutoHotkey>ppm := Run("cmd.exe /c convert lena50.jpg -compress none ppm:-")
; pipe in from imagemagick
img := ppm_read("", ppm) ;
x := img[4,4] ; get pixel(4,4)
msgbox % "img: 4,4,R,G,B, RGB: " x.R ", " x.G ", " x.B ", " x.rgb()
img.write("lena50.ppm")
return
ppm_read(filename, ppmo=0) ; only ppm3 files supported (ascii)
{
if !ppmo ; if image not already in memory, read from filename
fileread, ppmo, % filename, utf-8
loop, parse, ppmo, `n, `r
{
if (substr(A_LoopField, 1, 1) == "#")
continue
ppm_nocomment .= A_LoopField "`n"
}
index := 1
pos := 1
while pos := regexmatch(ppm_nocomment, "\d+", pixel, pos)
{
pos := regexmatch(ppm_nocomment, "\s", x, pos)
bitmap%A_Index% := pixel
if (index == 4)
Break
index ++
}
type := bitmap1
width := bitmap2
height := bitmap3
maxcolor := bitmap4
bitmap := Bitmap(width, height, color(0,0,0))
index := 1
width := 1
height := 1
while pos := regexmatch(ppm_nocomment, "\d+", pixel, pos)
{
pix%index% := pixel
index++
if (index > 3)
{
index := 1
pixel := Color(pix1, pix2, pix3)
bitmap[height, width] := pixel
if (width == bitmap.width)
{
width := 1
height += 1
}
else
width++
}
pos := regexmatch(ppm_nocomment, "\s", x, pos)
}
 
return bitmap
}
#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
</lang>
 
=={{header|C}}==
Anonymous user