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

81 lines
3.4 KiB
Racket

#lang br
8 years ago
(require pitfall/binprint binparser racket/dict)
;; http://www.matthewflickinger.com/lab/whatsinagif/bits_and_bytes.asp
8 years ago
(define-rule gif-header (:seq [signature (:bytes 3 #:type string/ascii?)]
[version (:bytes 3 #:type string/ascii?)]
logical-screen-descriptor
#:type assoc?))
8 years ago
(define-rule logical-screen-descriptor (:seq [width (:bytes 2 #:type integer?)]
[height (:bytes 2 #:type integer?)]
8 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?)]
8 years ago
[bgcolor-idx (:bytes 1 #:type integer?)]
[aspect (:bytes 1 #:type integer?)]
8 years ago
#:type assoc?))
8 years ago
(define ip (open-input-file "sample.gif"))
8 years ago
8 years ago
(define gh (gif-header ip))
gh
8 years ago
8 years ago
(define (global-color-quantity gh)
(expt 2 (add1 (dict-ref* gh 'logical-screen-descriptor 'lsd-flags 'global-color-table))))
8 years ago
8 years ago
(define-rule color (:bytes 1 #:type hex?))
(define-rule red color)
(define-rule green color)
(define-rule blue color)
(define-rule global-color-table (:repeat (global-color-quantity gh) (:seq red green blue #:type assoc?)))
(define gct (global-color-table ip))
8 years ago
8 years ago
gct
8 years ago
8 years ago
(define-rule graphic-control-extension
(:seq [extension-introducer (:bytes 1 #:type hex?)]
[graphic-control-label (:bytes 1 #:type hex?)]
[byte-size (:bytes 1 #:type integer?)]
[gce-flags (:seq [transparent-color-flag (:bits 1 #:type boolean?)]
[user-input-flag (:bits 1 #:type boolean?)]
[disposal-method (:bits 3)]
[reserved (:bits 3)]
#:type assoc?)]
[delay-time (:bytes 2 #:type integer?)]
[transparent-color-idx (:bytes 1 #:type integer?)]
[block-terminator (:bytes 1 #:type hex?)]
#:type assoc?))
8 years ago
8 years ago
(graphic-control-extension ip)
(define-rule image-descriptor
(:seq [image-separator (:bytes 1 #:type hex?)]
[left (:bytes 2 #:type integer?)]
[top (:bytes 2 #:type integer?)]
[width (:bytes 2 #:type integer?)]
[height (:bytes 2 #:type integer?)]
[id-flags (:seq [local-color-table-size (:bits 3 #:type integer?)]
[reserved (:bits 2)]
[sort-flag (:bits 1)]
[interlace-flag (:bits 1)]
[local-color-table-flag (:bits 1)]
#:type assoc?)]
#:type assoc?))
(image-descriptor ip)
8 years ago
#;(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")))))