CDE
This commit is contained in:
parent
518da4a923
commit
764da6cbbb
6144 changed files with 83610 additions and 11 deletions
|
|
@ -0,0 +1,71 @@
|
|||
(in-package #:rgb-pixel-buffer)
|
||||
|
||||
(defparameter *whitespaces-chars* '(#\SPACE #\RETURN #\TAB #\NEWLINE #\LINEFEED))
|
||||
|
||||
(defun read-header-chars (stream &optional (delimiter-list *whitespaces-chars*))
|
||||
(do ((c (read-char stream nil :eof)
|
||||
(read-char stream nil :eof))
|
||||
(vals nil (if (or (null c) (char= c #\#)) vals (cons c vals)))) ;;don't collect comment chars
|
||||
((or (eql c :eof) (member c delimiter-list)) (map 'string #'identity (nreverse vals))) ;;return strings
|
||||
(when (char= c #\#) ;;skip comments
|
||||
(read-line stream))))
|
||||
|
||||
(defun read-ppm-file-header (file)
|
||||
(with-open-file (s file :direction :input)
|
||||
(do ((failure-count 0 (1+ failure-count))
|
||||
(tokens nil (let ((t1 (read-header-chars s)))
|
||||
(if (> (length t1) 0)
|
||||
(cons t1 tokens)
|
||||
tokens))))
|
||||
((>= (length tokens) 4) (values (nreverse tokens)
|
||||
(file-position s)))
|
||||
(when (>= failure-count 10)
|
||||
(error (format nil "File ~a does not seem to be a proper ppm file - maybe too many comment lines" file)))
|
||||
(when (= (length tokens) 1)
|
||||
(when (not (or (string= (first tokens) "P6") (string= (first tokens) "P3")))
|
||||
(error (format nil "File ~a is not a ppm file - wrong magic-number. Read ~a instead of P6 or P3 " file (first tokens))))))))
|
||||
|
||||
(defun read-ppm-image (file)
|
||||
(flet ((image-data-reader (stream start-position width height image-build-function read-function)
|
||||
(file-position stream start-position)
|
||||
(dotimes (row height)
|
||||
(dotimes (col width)
|
||||
(funcall image-build-function row col (funcall read-function stream))))))
|
||||
(multiple-value-bind (header file-pos) (read-ppm-file-header file)
|
||||
(let* ((image-type (first header))
|
||||
(width (parse-integer (second header) :junk-allowed t))
|
||||
(height (parse-integer (third header) :junk-allowed t))
|
||||
(max-value (parse-integer (fourth header) :junk-allowed t))
|
||||
(image (make-rgb-pixel-buffer width height)))
|
||||
(when (> max-value 255)
|
||||
(error "unsupported depth - convert to 1byte depth with pamdepth"))
|
||||
(cond ((string= "P6" image-type)
|
||||
(with-open-file (stream file :direction :input :element-type '(unsigned-byte 8))
|
||||
(image-data-reader stream
|
||||
file-pos
|
||||
width
|
||||
height
|
||||
#'(lambda (w h val)
|
||||
(setf (rgb-pixel image w h) val))
|
||||
#'(lambda (stream)
|
||||
(make-rgb-pixel (read-byte stream)
|
||||
(read-byte stream)
|
||||
(read-byte stream))))
|
||||
image))
|
||||
((string= "P3" image-type)
|
||||
(with-open-file (stream file :direction :input)
|
||||
(image-data-reader stream
|
||||
file-pos
|
||||
width
|
||||
height
|
||||
#'(lambda (w h val)
|
||||
(setf (rgb-pixel image w h) val))
|
||||
#'(lambda (stream)
|
||||
(make-rgb-pixel (read stream)
|
||||
(read stream)
|
||||
(read stream))))
|
||||
image))
|
||||
(t 'unsupported))
|
||||
image))))
|
||||
|
||||
(export 'read-ppm-image)
|
||||
|
|
@ -0,0 +1 @@
|
|||
(read-ppm-image "feep.ppm")
|
||||
55
Task/Bitmap-Read-a-PPM-file/E/bitmap-read-a-ppm-file-1.e
Normal file
55
Task/Bitmap-Read-a-PPM-file/E/bitmap-read-a-ppm-file-1.e
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
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' || c == '#') {
|
||||
if (c == '#') {
|
||||
while (c != '\n') { c := chr(inputStream.read()) }
|
||||
}
|
||||
# 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
|
||||
# See [[Basic bitmap storage]] for the definition and origin of sign()
|
||||
def data := <elib:tables.makeFlexList>.fromType(<type:java.lang.Byte>, size)
|
||||
if (maxval >= 256) {
|
||||
for _ in 1..size {
|
||||
data.push(sign((inputStream.read() * 256 + inputStream.read()) * 255 // maxval))
|
||||
}
|
||||
} else {
|
||||
for _ in 1..size {
|
||||
data.push(sign(inputStream.read() * 255 // maxval))
|
||||
}
|
||||
}
|
||||
|
||||
def image := makeImage(width, height)
|
||||
image.replace(data.snapshot())
|
||||
return image
|
||||
}
|
||||
6
Task/Bitmap-Read-a-PPM-file/E/bitmap-read-a-ppm-file-2.e
Normal file
6
Task/Bitmap-Read-a-PPM-file/E/bitmap-read-a-ppm-file-2.e
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
def readPPMTask(inputFile, outputFile) {
|
||||
makeGrayscale \
|
||||
.fromColor(readPPM(<import:java.io.makeFileInputStream>(inputFile))) \
|
||||
.toColor() \
|
||||
.writePPM(<import:java.io.makeFileOutputStream>(outputFile))
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
include get.e
|
||||
|
||||
function get2(integer fn)
|
||||
sequence temp
|
||||
temp = get(fn)
|
||||
return temp[2] - temp[1]*temp[1]
|
||||
end function
|
||||
|
||||
function read_ppm(sequence filename)
|
||||
sequence image, line
|
||||
integer dimx, dimy, maxcolor
|
||||
atom fn
|
||||
fn = open(filename, "rb")
|
||||
if fn < 0 then
|
||||
return -1 -- unable to open
|
||||
end if
|
||||
line = gets(fn)
|
||||
if not equal(line,"P6\n") then
|
||||
return -1 -- only ppm6 files are supported
|
||||
end if
|
||||
dimx = get2(fn)
|
||||
if dimx < 0 then
|
||||
return -1
|
||||
end if
|
||||
dimy = get2(fn)
|
||||
if dimy < 0 then
|
||||
return -1
|
||||
end if
|
||||
maxcolor = get2(fn)
|
||||
if maxcolor != 255 then
|
||||
return -1 -- maxcolors other then 255 are not supported
|
||||
end if
|
||||
image = repeat(repeat(0,dimy),dimx)
|
||||
for y = 1 to dimy do
|
||||
for x = 1 to dimx do
|
||||
image[x][y] = getc(fn)*#10000 + getc(fn)*#100 + getc(fn)
|
||||
end for
|
||||
end for
|
||||
close(fn)
|
||||
return image
|
||||
end function
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
sequence image
|
||||
image = read_ppm("image.ppm")
|
||||
image = to_gray(image)
|
||||
image = to_color(image)
|
||||
write_ppm("image_gray.ppm",image)
|
||||
Loading…
Add table
Add a link
Reference in a new issue