methodize image
parent
b62b95b66e
commit
ae1623ecc1
@ -1,32 +1,19 @@
|
||||
#lang racket/base
|
||||
(require
|
||||
"helper.rkt"
|
||||
racket/class
|
||||
racket/contract
|
||||
"jpeg.rkt"
|
||||
"png.rkt")
|
||||
(provide PDFImage-open)
|
||||
|
||||
#;(define PDFImage
|
||||
(class object%
|
||||
(super-new)
|
||||
|
||||
(as-methods
|
||||
)))
|
||||
|
||||
(define/contract (PDFImage-open src label)
|
||||
(any/c any/c . -> . (or/c (is-a?/c PNG) (is-a?/c JPEG)))
|
||||
(define (PDFImage-open src label)
|
||||
(define data (cond
|
||||
[(bytes? src) src]
|
||||
;;else if src instanceof ArrayBuffer
|
||||
;;data = new Buffer(new Uint8Array(src))
|
||||
[(regexp-match #rx"^data:.+;base64,(.*)$" src)
|
||||
(void)] ;; base64 ; todo
|
||||
[(bytes? src) (open-input-bytes src)]
|
||||
[(regexp-match #rx"^data:.+;base64,(.*)$" src) (void)] ;; base64 ; todo
|
||||
[else (open-input-file src)]))
|
||||
(cond
|
||||
[(equal? (peek-bytes 2 0 data) (bytes #xff #xd8))
|
||||
(make-object JPEG data label)]
|
||||
[(equal? (peek-bytes 4 0 data) (apply bytes (cons #x89 (map char->integer '(#\P #\N #\G)))))
|
||||
(make-object PNG data label)]
|
||||
[else (raise-argument-error 'PDFImage-open "valid image format" src)]))
|
||||
(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))
|
||||
|
||||
|
Loading…
Reference in New Issue