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/pitfall/image.rkt

20 lines
664 B
Racket

#lang racket/base
(require
racket/class
"jpeg.rkt"
"png.rkt")
(provide PDFImage-open)
7 years ago
6 years ago
(define (PDFImage-open src label)
7 years ago
(define data (cond
6 years ago
[(bytes? src) (open-input-bytes src)]
[(regexp-match #rx"^data:.+;base64,(.*)$" src) (void)] ;; base64 ; todo
6 years ago
[else (open-input-file src)]))
6 years ago
(define image-class
(cond
[(equal? (peek-bytes 2 0 data) (bytes #xff #xd8)) JPEG]
[(equal? (peek-bytes 4 0 data) (apply bytes (map char->integer '(#\u0089 #\P #\N #\G)))) PNG]
[else (raise-argument-error 'PDFImage-open "valid image format" src)]))
(make-object image-class data label))
7 years ago