You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
typesetting/pitfall/binparser/gif-parse.rkt

46 lines
2.0 KiB
Racket

#lang br
7 years ago
(require pitfall/binprint binparser racket/dict)
;; http://www.matthewflickinger.com/lab/whatsinagif/bits_and_bytes.asp
7 years ago
(define-rule gif (:seq [signature (:bytes 3 #:type string/ascii?)]
[version (:bytes 3 #:type string/ascii?)]
logical-screen-descriptor
7 years ago
global-color-table
#:type assoc?))
7 years ago
(define-rule logical-screen-descriptor (:seq [width (:bytes 2 #:type integer?)]
[height (:bytes 2 #:type integer?)]
7 years ago
[lsd-flags (:seq [global-color-table-size (:bits 3 #:type integer?)]
[sort (:bits 1 #:type boolean?)]
[resolution (:bits 3 #:type integer?)]
[global-color-table (:bits 1 #:type integer?)]
#:type assoc?)]
7 years ago
[bgcolor-idx (:bytes 1 #:type integer?)]
[aspect (:bytes 1 #:type integer?)]
7 years ago
#:type assoc?))
7 years ago
(define-rule global-color-table (:repeat 4 (:bytes 3)))
7 years ago
#;(define-rule color (:bytes 3 #:type hex?))
(define g (gif (open-input-file "sample.gif")))
(define (global-color-quantity)
(define val (dict-ref (dict-ref (dict-ref g 'logical-screen-descriptor) 'lsd-flags) 'global-color-table))
(expt 2 (add1 val)))
g
#;(check-equal? (gif (gif (open-input-file "sample.gif"))) (read-bytes 13 (open-input-file "sample.gif")))
(require rackunit)
#;(check-equal? (parse-with-template "test.gif" gif)
(cons 'gif
(make-hasheq (list (cons 'logical-screen-descriptor '(162 162 (#f #t #f #f #f #t #f #t) 0 0))
'(signature . "GIF")
'(version . "87a")))))