From 0e9de4a1988fe5029c1bc5903f5925c69a1649ae Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Sun, 21 May 2017 11:57:29 -1000 Subject: [PATCH] the struggle --- pitfall/pitfall/png-reader.rkt | 6 +++--- pitfall/pitfall/png.rkt | 36 +++++++++++++++++++--------------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/pitfall/pitfall/png-reader.rkt b/pitfall/pitfall/png-reader.rkt index 3faf45c8..15001b4d 100644 --- a/pitfall/pitfall/png-reader.rkt +++ b/pitfall/pitfall/png-reader.rkt @@ -87,11 +87,11 @@ Grab key chunks from PNG. Doesn't require heavy lifting from libpng. (define pos 0) (define c 0) - (report* width height) + #;(report* width height) (parameterize ([current-input-port (open-input-bytes data)]) (for/fold ([_ #f]) ([row (in-naturals)] #:break (eof-object? (peek-byte))) - (report row) + #;(report row) (define b (read-byte)) (case b [(0) ; none @@ -165,7 +165,7 @@ Grab key chunks from PNG. Doesn't require heavy lifting from libpng. )] [else (error 'invalid-filter-algorithm (format "~a" b))]))) - (report (bytes-length pixels)) + #;(report (bytes-length pixels)) (fn pixels)) diff --git a/pitfall/pitfall/png.rkt b/pitfall/pitfall/png.rkt index c0fdd6d0..04022097 100644 --- a/pitfall/pitfall/png.rkt +++ b/pitfall/pitfall/png.rkt @@ -93,25 +93,28 @@ (define (pixel-proc pixels) (define colorByteSize (* (· this image colors) (/ (· this image bits) 8))) (define pixelCount (* (· this width) (· this height))) - (define imgData (make-bytes (* pixelCount colorByteSize))) - (define alphaChannel (make-bytes pixelCount)) + #;(define imgData (make-bytes (* pixelCount colorByteSize))) + #;(define alphaChannel (make-bytes pixelCount)) - (define i 0) - (define p 0) - (define a 0) (define len (bytes-length pixels)) - (for ([idx (in-naturals)] - #:when (< i len)) - (bytes-set! imgData p (bytes-ref pixels i)) - (increment! p) (increment! i) - (bytes-set! imgData p (bytes-ref pixels i)) - (increment! p) (increment! i) - (bytes-set! imgData p (bytes-ref pixels i)) - (increment! p) (increment! i) - (bytes-set! alphaChannel a (bytes-ref pixels i)) - (increment! a) (increment! i)) + #;(report* len (* pixelCount colorByteSize) pixelCount) + (define-values (imgBytes alphaBytes) + (for/fold ([img-bytes empty] + [alpha-bytes empty]) + ([b (in-bytes pixels)] + [i (in-naturals)]) + (if (= (modulo i 4) 3) + (values img-bytes (cons b alpha-bytes)) + (values (cons b img-bytes) alpha-bytes)))) + (define imgData (apply bytes (reverse imgBytes))) + (define alphaChannel (apply bytes (reverse alphaBytes))) + + #;(report* (bytes-length imgData) (bytes-length alphaChannel)) + + #;(error 'in-pixel-proc) + (define done 0) (set-field! imgData this (deflate imgData)) (increment! done) @@ -121,7 +124,8 @@ (increment! done) (when (= done 2) (· this finalize)) - + (report* done) + (void) ) (decodePixels (· this imgData) (· this pixelBitlength) (· this width) (· this height) pixel-proc))