Is my Lisp enterprise enough?
1
Name:
Anonymous
2012-03-11 11:56
(defun png-load-stream (bs)
(let* ((chunks (png-parse bs))
(g nil)
(chns 0)
(IHDR (gethash "IHDR" chunks))
(IDAT (gethash "IDAT" chunks))
(PLTE (gethash "PLTE" chunks)))
(unless IHDR (gfx-load-error "Missing IHDR"))
(unless IDAT (gfx-load-error "Missing IDAT"))
(deser (strm IHDR) ((width msb 4)
(height msb 4)
(chn-depth msb 1)
(type msb 1)
(enc msb 1)
(filter msb 1)
(interlace msb 1))
(unless (= enc 0) (gfx-load-error "Unsupported encoding (~a)" enc))
(setf chns (case type
(0 1) ; grayscale
(2 3) ; truecolor
(3 2) ; indexed
(4 4) ; grayscale with alpha
(6 4) ; truecolor with alpha
(otherwise (gfx-load-error "Invalid color type (~d)" type))))
(setf g (gfx width height :c chns))
(setf IDAT (zlib-unpack-bytes IDAT))
(png-defilter g IDAT)
(case interlace
(0 ) ; no interlace
(1 (gfx-load-error "Interlaced PNGs are not supported")) (png-deinterlace-adam7 g))
(otherwise (gfx-load-error "Invalid interlace type (~d)" type)))
(when PLTE
(let ((a (vec 256 u4))
(n (truncate (length PLTE) 3))
(s (strm PLTE)))
(dotimes (i n)
(deser s ((r msb 1)
(g msb 1)
(b msb 1))
(setf (aref a i) (rgb r g b))))
(setf (gfx-m g) a)))
g))))
2
Name:
Anonymous
2012-03-11 12:06
>>1
Cool, although error handling can likely be done better by using conditions, but only if those errors can actually be recovered from.
3
Name:
Anonymous
2012-03-11 13:32
looks nice, how fast is it?
4
Name:
Anonymous
2012-03-11 13:51
>>3
Doesn't your monkey ass know how to benchmark code?
5
Name:
Anonymous
2012-03-11 13:58
>>4
There's no way
>>3 would be able to do it, even if he knew how, simply because you only provided one function and didn't include the other functions or macros.
6
Name:
Anonymous
2012-03-11 13:59
Your parens are dead. Like Batman's.
Actually they're just unbalanced.
7
Name:
Anonymous
2012-03-11 14:10
Too bad Lisp is shit.
8
Name:
Anonymous
2012-03-11 14:12
>>7
translation: i am incapable of understanding the power of lisp
9
Name:
Anonymous
2012-03-11 14:50
>>6
That is because I write my code in Microsoft Notepad.
10
Name:
Anonymous
2012-03-11 14:55
>>9
I'm surprised you manage to keep everything indented properly - CL indentation rules are quite fancy, but nobody has problems with this because the editors do it for you, but having to do it by yourself sounds like a bit of effort.
11
Name:
Anonymous
2012-03-11 16:07
Here is how I do "pointer arithmetics":
(defmacro inc (p &rest n)
(setf n (or (car n) 1))
`(prog1 (aref (car ,p) (the fixnum (cdr ,p)))
(setf (cdr ,p) (the fixnum (+ (cdr ,p) ,n)))))
(let ((p (cons #(6 1 2 3) 0)))
(list (inc p) (inc p) p))