Bitmap/Read a PPM file: Difference between revisions

add UNIX Shell
(add UNIX Shell)
Line 1,432:
}
}</lang>
 
=={{header|UNIX Shell}}==
{{works with|ksh93}}
Ref: [[Bitmap#UNIX Shell]]
 
Add the following functions to the <tt>RGBColor_t</tt> type
<lang bash> function setrgb {
_.r=$1
_.g=$2
_.b=$3
}
function grayscale {
integer x=$(( round( 0.2126*_.r + 0.7152*_.g + 0.0722*_.b ) ))
_.r=$x
_.g=$x
_.b=$x
}</lang>
 
Add the following function to the <tt>Bitmap_t</tt> type
<lang bash> function grayscale {
RGBColor_t c
for ((y=0; y<_.height; y++)); do
for ((x=0; x<_.width; x++)); do
c.setrgb ${_.data[y][x]}
c.grayscale
_.data[y][x]=$(c.to_s)
done
done
}
 
function read {
exec 4<"$1"
typeset filetype
read -u4 filetype
if [[ $filetype != "P3" ]]; then
print -u2 "error: I can only read P3 type PPM files"
else
read -u4 _.width _.height
integer maxval
read -u4 maxval
integer x y r g b
typeset -a bytes
for ((y=0; y<_.height; y++)); do
read -u4 -A bytes
for ((x=0; x<_.width; x++)); do
r=${bytes[3*x+0]}
g=${bytes[3*x+1]}
b=${bytes[3*x+2]}
if (( r > maxval || g > maxval || b > maxval )); then
print -u2 "error: invalid color ($r $g $b), max=$maxval"
return 1
fi
_.data[y][x]="$r $g $b"
done
done
fi
exec 4<&-
}</lang>
 
Now we can:
<lang bash>Bitmap_t c
c.read "$HOME/tmp/bitmap.ppm"
c.to_s
 
if [[ $(c.to_s) == $(cat "$HOME/tmp/bitmap.ppm") ]]; then
echo looks OK
else
echo something is wrong
fi
 
c.grayscale
c.to_s
c.write "$HOME/tmp/bitmap_g.ppm"</lang>
 
=={{header|Vedit macro language}}==
Anonymous user