Bitmap/Read a PPM file: Difference between revisions

add E example
m (→‎{{header|AutoHotkey}}: Minor indentation and casing edit)
(add E example)
Line 319:
auto p6 = new P6Image(new FileConduit("image.ppm"));
</lang>
 
=={{header|E}}==
 
<lang e>def chr := <import:java.lang.makeCharacter>.asChar
 
def readPPM(inputStream) {
# Proper native-to-E stream IO facilities have not been designed and
# implemented yet, so we are borrowing Java's. Poorly. This *will* be
# improved eventually.
# Reads one header token, skipping comments and whitespace, and exactly
# one trailing whitespace character
def readToken() {
var token := ""
var c := chr(inputStream.read())
while (c == ' ' || c == '\t' || c == '\r' || c == '\n') {
# skip over initial whitespace
c := chr(inputStream.read())
}
while (!(c == ' ' || c == '\t' || c == '\r' || c == '\n')) {
if (c == '#') {
while (c != '\n') { c := chr(inputStream.read()) }
} else {
token += E.toString(c)
c := chr(inputStream.read())
}
}
return token
}
 
# Header
require(readToken() == "P6")
def width := __makeInt(readToken())
def height := __makeInt(readToken())
def maxval := __makeInt(readToken())
 
def size := width * height * 3
 
# Body
# Ack, this is messy. Need that proper stream facility...
var data := inputStream.readAvailable(size)
while (inputStream.available() > 0 && data.size() < size) {
data += inputStream.readAvailable(size - data.size())
}
 
def image := makeImage(width, height)
image.replace(data)
return image
}</lang>
 
[[Category:E examples needing attention]]Note: As of this writing the [[grayscale image]] task has not been implemented, so the task code (below) won't actually run yet. But readPPM above has been tested separately.
 
<lang e>def readPPMTask(inputFile, outputFile) {
makeGrayscale \
.fromColor(readPPM(<import:java.io.makeFileInputStream>(inputFile))) \
.toColor() \
.writePPM(<import:java.io.makeFileOutputStream>(outputFile))
}</lang>
 
=={{header|Forth}}==