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/png.rkt

56 lines
1.8 KiB
Racket

7 years ago
#lang pitfall/racket
(require "png-reader.rkt")
(provide PNG)
(define-subclass object% (PNG data label)
(super-new)
(field [image (read-png data)]
[width (· image width)]
[height (· image height)]
[imgData (· image imgData)]
7 years ago
[obj #f])
(as-methods
embed))
(define/contract (embed this doc-in)
(object? . ->m . void?)
(unless (· this obj)
(set-field! obj this
7 years ago
(send doc-in ref
(mhash 'Type "XObject"
'Subtype "Image"
'BitsPerComponent (· this image bits)
'Width (· this width)
'Height (· this height)
'Filter "FlateDecode")))
(unless (· this image hasAlphaChannel)
7 years ago
(define params (send doc-in ref (mhash 'Predictor 15
'Colors (· this image colors)
'BitsPerComponent (· this image bits)
'Columns (· this width))))
(hash-set! (· this obj payload) 'DecodeParms params)
(send params end))
(cond
[(hash-ref (· this image) 'palette #f)
;; embed the color palette in the PDF as an object stream
7 years ago
(define palette-ref (· doc-in ref))
(send palette-ref end (· this image palette))
;; build the color space array for the image
(hash-set! (· this object payload) 'Colorspace
(list "Indexed" "DeviceRGB" (sub1 (bytes-length (· this image palette))) palette-ref))]
[else (hash-set! (· this obj payload) 'ColorSpace "DeviceRGB")])
7 years ago
;; todo: transparency & alpha channel shit
7 years ago
;; embed the actual image data
(send (· this obj) end (· this imgData))))
7 years ago