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

33 lines
949 B
Racket

#lang racket/base
(require
"helper.rkt"
racket/class
racket/contract
"jpeg.rkt"
"png.rkt")
(provide PDFImage-open)
8 years ago
#;(define PDFImage
8 years ago
(class object%
(super-new)
(as-methods
)))
8 years ago
(define/contract (PDFImage-open src label)
8 years ago
(any/c any/c . -> . (or/c (is-a?/c PNG) (is-a?/c JPEG)))
8 years ago
(define data (cond
6 years ago
[(bytes? src) src]
8 years ago
;;else if src instanceof ArrayBuffer
;;data = new Buffer(new Uint8Array(src))
[(regexp-match #rx"^data:.+;base64,(.*)$" src)
(void)] ;; base64 ; todo
6 years ago
[else (open-input-file src)]))
8 years ago
(cond
6 years ago
[(equal? (peek-bytes 2 0 data) (bytes #xff #xd8))
6 years ago
(make-object JPEG data label)]
6 years ago
[(equal? (peek-bytes 4 0 data) (apply bytes (cons #x89 (map char->integer '(#\P #\N #\G)))))
(make-object PNG data label)]
8 years ago
[else (raise-argument-error 'PDFImage-open "valid image format" src)]))