Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify FLIF pass 1 #77

Merged
merged 3 commits into from
May 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 36 additions & 74 deletions base.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,6 @@
; number of times to loop a FLIF (0 = forever)
(define image-num-loops 0)
(define animation-thread (make-parameter #f))
(define decoder-thread (make-parameter #f))
; number from the decoder thread that shows how much the image is loaded
(define flif-load-progress (box 0))
; GIF cumulative animation
(define cumulative? (make-parameter #f))
(define exact-search? (make-parameter #f))
Expand Down Expand Up @@ -158,6 +155,8 @@

; get index of an item in the list
; numbering starts from 0
;
; may replace with index-of from Racket v6.7
(define/contract (get-index item lst)
(any/c list? . -> . (or/c integer? false?))
(define len (length lst))
Expand Down Expand Up @@ -212,44 +211,32 @@
(bytes-set! pixels (+ 2 offset) green)
(bytes-set! pixels (+ 3 offset) blue))))

(define (flif->list dec-ptr)
(define/contract (flif->list image #:animation? [animation? (want-animation?)])
((or/c path-string? bytes?) . -> . (listof (is-a?/c bitmap%)))
; create the decoder pointer
(define dec-ptr (flif-create-decoder))
; decode either the file from the path or by its bytes
(if (path-string? image)
(flif-decoder-decode-file! dec-ptr image)
(flif-decoder-decode-memory! dec-ptr image))
; only look at the first frame if we want a static image
(define num (if (want-animation?) (flif-decoder-num-images dec-ptr) 1))
(define dimensions (flif-dimensions (image-path)))
(for/list ([i (in-range num)])
(define image (flif-decoder-get-image dec-ptr i))
(define width (first dimensions))
(define height (second dimensions))
; make sure to decode with the proper depth
(define reader (if (= (flif-image-get-depth image) 8)
flif-image-read-rgba8
flif-image-read-rgba16))
(define pixels (reader image width height))
(rgba->argb! pixels)
(define bitmap (make-object bitmap% width height #f #t))
(send bitmap set-argb-pixels 0 0 width height pixels)
bitmap))

(define (progressive-callback quality bytes-read decode-over? user-data context)
(flif-decoder-generate-preview context)
(define lst (flif->list (decoder)))
(set! image-bmp-master (first lst))
(cond [(and (want-animation?) (> (length lst) 1))
(set! image-lst-timings
(let ([image (flif-decoder-get-image (decoder) 0)])
(make-list (length lst) (flif-image-get-frame-delay image))))
(set! image-num-loops (flif-decoder-num-loops (decoder)))
(load-image (map bitmap lst) 'default)]
[else (load-image (first lst) 'default)])
; set the new frame label
;(define-values (base name must-be-dir?) (split-path (image-path)))
#;(send (send (ivy-canvas) get-parent)
set-label
(format "(~a%) ~a" (exact->inexact (/ quality 100)) (path->string name)))
; set the load progress
;(set-box! flif-load-progress quality)
; the fewer the calls, the faster the total decoding
(+ quality 5000))
(define num (if animation? (flif-decoder-num-images dec-ptr) 1))
(define lst
(for/list ([i (in-range num)])
(define img-ptr (flif-decoder-get-image dec-ptr i))
(define width (flif-image-get-width img-ptr))
(define height (flif-image-get-height img-ptr))
; make sure to decode with the proper depth
(define reader (if (= (flif-image-get-depth img-ptr) 8)
flif-image-read-rgba8
flif-image-read-rgba16))
(define pixels (reader img-ptr width height))
(rgba->argb! pixels)
(define bitmap (make-object bitmap% width height #f #t))
(send bitmap set-argb-pixels 0 0 width height pixels)
bitmap))
(flif-destroy-decoder! dec-ptr)
lst)

; objects that will be used extensively in transparency-grid
(define dgray-color (make-object color% 128 128 128))
Expand Down Expand Up @@ -684,24 +671,20 @@
; load animated flif
[(and (want-animation?) (flif? img) (flif-animated? img))
(cumulative? #f)
(decoder (flif-create-decoder))
; progressive decoding
;(flif-decoder-set-callback! (decoder) progressive-callback #f)
;(flif-decoder-set-first-callback-quality! (decoder) 10000)
(define dec-ptr (flif-create-decoder))
; regular decoding
(flif-decoder-decode-file! (decoder) img)
(let ([image (flif-decoder-get-image (decoder) 0)]
[num-frames (flif-decoder-num-images (decoder))])
(define timing-delay (flif-image-get-frame-delay image))
(define lst (flif->list (decoder)))
(flif-decoder-decode-file! dec-ptr img)
(let ([img-ptr (flif-decoder-get-image dec-ptr 0)]
[num-frames (flif-decoder-num-images dec-ptr)])
(define timing-delay (flif-image-get-frame-delay img-ptr))
(define lst (flif->list img))
(set! image-bmp-master (first lst))
(set! image-lst-master lst)
(set! image-lst (map (λ (flaf-frame) (scale-image flaf-frame scale)) lst))
(set! image-lst-timings (make-list num-frames (/ timing-delay 1000)))
(set! image-pict #f)
(set! image-num-loops (flif-decoder-num-loops (decoder)))
(flif-destroy-decoder! (decoder))
(decoder #f))
(set! image-num-loops (flif-decoder-num-loops dec-ptr))
(flif-destroy-decoder! dec-ptr))
; set the new frame label
(send ivy-frame set-label (string-truncate (path->string name) +label-max+))
; set the gui information
Expand Down Expand Up @@ -733,18 +716,8 @@
(and (set! image-bmp-master (load-svg-from-file img)) #t)]
[(flif? img)
(cumulative? #f)
(decoder (flif-create-decoder))
; set the load progress to 0
;(set-box! flif-load-progress 0)
; progressive decoding
;(flif-decoder-set-callback! (decoder) progressive-callback #f)
;(flif-decoder-set-first-callback-quality! (decoder) 5000)
; regular decoding
(flif-decoder-decode-file! (decoder) img)
(define lst (flif->list (decoder)))
(define lst (flif->list img))
(set! image-bmp-master (first lst))
(flif-destroy-decoder! (decoder))
(decoder #f)
(load-image (first lst) scale)]
[else (send image-bmp-master load-file img 'unknown/alpha)]))
(cond [load-success
Expand Down Expand Up @@ -972,17 +945,6 @@
; kill the animation thread, if applicable
(unless (or (false? (animation-thread)) (thread-dead? (animation-thread)))
(kill-thread (animation-thread)))
#;(unless (or (false? (decoder-thread)) (thread-dead? (decoder-thread)))
(kill-thread (decoder-thread))
(displayln "load-image-in-collection; aborting decoder...")
(flif-abort-decoder! (decoder))
(flif-destroy-decoder! (decoder))
(displayln "done")
(decoder #f))
(when (decoder)
(flif-abort-decoder! (decoder))
(flif-destroy-decoder! (decoder))
(decoder #f))
(send (ivy-tag-tfield) set-field-background color-white)
(define prev-index (get-index (image-path) (pfs)))
(case direction
Expand Down
2 changes: 1 addition & 1 deletion embed.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
riff
txexpr
xml
(only-in "files.rkt" ivy-version decoder))
(only-in "files.rkt" ivy-version))
(provide add-embed-tags!
dc:subject->list
del-embed-tags!
Expand Down
3 changes: 0 additions & 3 deletions files.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,3 @@
(make-directory* thumbnails-path)
(unless (eq? (system-type) 'windows)
(file-or-directory-permissions thumbnails-path #o700)))

; flif decoder parameter
(define decoder (make-parameter #f))
17 changes: 0 additions & 17 deletions frame.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@
(for ([pair (in-hash-pairs xmp-threads)])
(printf "Waiting for thread ~a to finish...~n" (car pair))
(sync (cdr pair))))
; clean up the decoder pointer
(when (decoder)
(flif-abort-decoder! (decoder))
(flif-destroy-decoder! (decoder))
(decoder #f))
(disconnect sqlc))))

(define closer-frame%
Expand Down Expand Up @@ -299,18 +294,6 @@
(λ (i e)
(unless (or (false? (animation-thread)) (thread-dead? (animation-thread)))
(kill-thread (animation-thread)))
#;(unless (or (false? (decoder-thread)) (thread-dead? (decoder-thread)))
(kill-thread (decoder-thread))
(displayln "New collection; aborting decoder...")
(flif-abort-decoder! (decoder))
(display "Destroying decoder... ")
(flif-destroy-decoder! (decoder))
(displayln "done")
(decoder #f))
(when (decoder)
(flif-abort-decoder! (decoder))
(flif-destroy-decoder! (decoder))
(decoder #f))
(image-dir (find-system-path 'home-dir))
(pfs (list +root-path+))
(image-path +root-path+)
Expand Down
7 changes: 1 addition & 6 deletions thumbnails.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,7 @@
[(#".svg" #".SVG")
(load-svg-from-file path)]
[(#".flif" #".FLIF" #".flaf" #".FLAF")
(define dec (flif-create-decoder))
(flif-decoder-decode-file! dec path)
(parameterize ([want-animation? #f])
(define bmp (first (flif->list dec)))
(flif-destroy-decoder! dec)
bmp)]
(first (flif->list path #:animation? #f))]
[else (read-bitmap path)]))
(define thumb-path (path->md5 path))
; use a temporary file in case there's concurrent
Expand Down