original commit: bfd6a146aa118c748841490b8959706b89a992e3
This commit is contained in:
Robby Findler 2004-07-24 01:44:24 +00:00
parent f8c86b2e82
commit 9b4698a09a

View File

@ -3,12 +3,18 @@
(lib "class.ss") (lib "class.ss")
(lib "string.ss")) (lib "string.ss"))
(provide argb-vector->bitmap (provide cache-image-snip%
snip-class
overlay-bitmap overlay-bitmap
build-bitmap build-bitmap
flatten-bitmap flatten-bitmap
cache-image-snip%
snip-class) argb->cache-image-snip
argb->bitmap
make-argb
argb-vector)
#| #|
@ -37,7 +43,8 @@
an alpha of 0 means the pixel value is 255 an alpha of 0 means the pixel value is 255
|# |#
;; type argb-vector = (vectorof rational[between 0 & 255]) ;; type argb = (make-argb (vectorof rational[between 0 & 255]) int)
(define-struct argb (vector width))
(define cache-image-snip% (define cache-image-snip%
(class snip% (class snip%
@ -45,18 +52,20 @@
;; draw-proc : (union #f ((is-a?/c dc<%>) int[dx] int[dy] -> void)) ;; draw-proc : (union #f ((is-a?/c dc<%>) int[dx] int[dy] -> void))
;; used for direct drawing ;; used for direct drawing
(init-field dc-proc) (init-field dc-proc)
(define/public (get-dc-proc) dc-proc)
;; bitmap-proc : ((vectorof rational[0 <= x <= 255]) int[dx] int[dy] -> void) ;; bitmap-proc : ((vectorof rational[0 <= x <= 255]) int[dx] int[dy] -> void)
;; used for drawing into a bitmap ;; used for drawing into a bitmap
(init-field argb-proc) (init-field argb-proc)
(define/public (get-argb-proc) argb-proc)
(init-field (width #f) (init-field (width #f)
(height #f)) (height #f))
(define/public (get-size) (define/public (get-size)
(values width height)) (values width height))
;; argb-vector : (union #f argb-vector) ;; argb : (union #f argb)
(init-field [argb-vector #f]) (init-field [argb #f])
;; bitmap : (union #f (is-a?/c bitmap%)) ;; bitmap : (union #f (is-a?/c bitmap%))
;; the way that this image is be drawn, on its own ;; the way that this image is be drawn, on its own
@ -68,28 +77,22 @@
(argb-proc argb-proc) (argb-proc argb-proc)
(width width) (width width)
(height height) (height height)
(argb-vector argb-vector))) (argb argb)))
;; this can use the draw proc, rather than the argb-vector.
;; get-bitmap : -> bitmap ;; get-bitmap : -> bitmap
;; returns a bitmap showing what the image would look like, ;; returns a bitmap showing what the image would look like,
;; if it were drawn ;; if it were drawn
(define/public (get-bitmap) (define/public (get-bitmap)
(unless bitmap (unless bitmap
(set! bitmap (flatten-bitmap (set! bitmap (flatten-bitmap (argb->bitmap (get-argb)))))
(argb-vector->bitmap
(get-argb-vector)
(ceiling (inexact->exact width))
(ceiling (inexact->exact height))))))
bitmap) bitmap)
;; get-argb-vector : -> argb-vector ;; get-argb : -> argb
(define/public (get-argb-vector) (define/public (get-argb)
(unless argb-vector (unless argb
(set! argb-vector (make-vector (* 4 width height) 255)) (set! argb (make-argb (make-vector (* 4 width height) 255) width))
(argb-proc argb-vector 0 0)) (argb-proc argb 0 0))
argb-vector) argb)
(define/override (get-extent dc x y w h descent space lspace rspace) (define/override (get-extent dc x y w h descent space lspace rspace)
(set-box/f! w width) (set-box/f! w width)
@ -101,7 +104,7 @@
(define/override (draw dc x y left top right bottom dx dy draw-caret) (define/override (draw dc x y left top right bottom dx dy draw-caret)
(cond (cond
[argb-vector [argb
(let ([bitmap (get-bitmap)]) (let ([bitmap (get-bitmap)])
(send dc draw-bitmap bitmap x y 'solid (send dc draw-bitmap bitmap x y 'solid
(send the-color-database find-color "black") (send the-color-database find-color "black")
@ -111,11 +114,9 @@
[else (void)])) [else (void)]))
(define/override (write f) (define/override (write f)
(printf "calling write\n")
(let ([str (format "~s" (let ([str (format "~s"
(list width (list (argb-vector (get-argb))
height width))])
(get-argb-vector)))])
(send f put str))) (send f put str)))
(super-new) (super-new)
@ -130,24 +131,17 @@
(define cache-image-snip-class% (define cache-image-snip-class%
(class snip-class% (class snip-class%
(define/override (read f) (define/override (read f)
(printf "called read\n")
(let ([data (read-from-string (send f get-string) (let ([data (read-from-string (send f get-string)
void void
(lambda (x) #f))]) (lambda (x) #f))])
(if data (if data
(new cache-image-snip% (argb->cache-image-snip (make-argb (car data) (cadr data)))
(width (car data))
(height (cadr data))
(argb-vector (caddr data))
(argb-proc void)
(dc-proc void))
(make-null-cache-image-snip)))) (make-null-cache-image-snip))))
(super-new))) (super-new)))
(define snip-class (new cache-image-snip-class%)) (define snip-class (new cache-image-snip-class%))
(send snip-class set-version 1) (send snip-class set-version 1)
(send snip-class set-classname (format "~s" `(lib "cache-image-snip.ss" "mrlib"))) (send snip-class set-classname (format "~s" `(lib "cache-image-snip.ss" "mrlib")))
(printf "installing snipclass\n")
(send (get-the-snip-class-list) add snip-class) (send (get-the-snip-class-list) add snip-class)
(define (make-null-cache-image-snip) (define (make-null-cache-image-snip)
@ -167,8 +161,8 @@
(height size) (height size)
(draw-proc draw) (draw-proc draw)
(argb-proc (argb-proc
(lambda (argb-vector dx dy) (lambda (argb dx dy)
(overlay-bitmap argb-vector dx dy bm bm))))) (overlay-bitmap argb size size dx dy bm bm)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
@ -215,10 +209,32 @@
;; argb vector utilties ;; argb vector utilties
;; ;;
;; argb-vector->bitmap : argb-vector int int -> bitmap ;; argb->cache-snip : argb -> cache-image-snip
(define (argb->cache-image-snip argb)
(let* ([width (argb-width argb)]
[argb-vector (argb-vector argb)]
[height (quotient (vector-length argb-vector) (* 4 width))]
[bitmap (argb->bitmap argb)]
[mask (send bitmap get-loaded-mask)])
(new cache-image-snip%
(width width)
(height height)
(argb argb)
(argb-proc
(lambda (argb dx dy)
(overlay-bitmap argb dx dy bitmap mask)))
(dc-proc (lambda (dc dx dy)
(send dc draw-bitmap bitmap dx dy 'solid
(send the-color-database find-color "black")
mask))))))
;; argb-vector->bitmap : argb -> bitmap
;; flattens the argb vector into a bitmap ;; flattens the argb vector into a bitmap
(define (argb-vector->bitmap argb-vector w h) (define (argb->bitmap argb)
(let* ([bm (make-object bitmap% w h)] (let* ([argb-vector (argb-vector argb)]
[w (argb-width argb)]
[h (quotient (vector-length argb-vector) (* w 4))]
[bm (make-object bitmap% w h)]
[mask-bm (make-object bitmap% w h)] [mask-bm (make-object bitmap% w h)]
[bdc (new bitmap-dc% (bitmap bm))] [bdc (new bitmap-dc% (bitmap bm))]
[str (make-string (vector-length argb-vector) #\377)] [str (make-string (vector-length argb-vector) #\377)]
@ -241,11 +257,13 @@
(send bm set-loaded-mask mask-bm) (send bm set-loaded-mask mask-bm)
bm)) bm))
;; overlay-bitmap : argb-vector int int bitmap bitmap -> void ;; overlay-bitmap : argb int int bitmap bitmap -> void
;; assumes that the mask bitmap only has greyscale in it ;; assumes that the mask bitmap only has greyscale in it
;; (ie, that looking at the red component of the mask is enough) ;; (ie, that looking at the red component of the mask is enough)
(define (overlay-bitmap argb-vector dx dy color mask) (define (overlay-bitmap argb dx dy color mask)
(let* ([w (send color get-width)] (let* ([argb-vector (argb-vector argb)]
[argb-w (argb-width argb)]
[w (send color get-width)]
[h (send color get-height)] [h (send color get-height)]
[color-str (make-string (* w h 4) #\000)] [color-str (make-string (* w h 4) #\000)]
[mask-str (make-string (* w h 4) #\000)] [mask-str (make-string (* w h 4) #\000)]
@ -263,7 +281,7 @@
[str-i str-i]) [str-i str-i])
(if (= x w) (if (= x w)
(yloop (add1 y) str-i) (yloop (add1 y) str-i)
(let* ([argb-i (* 4 (+ (+ dx x) (* (+ dy y) w)))] (let* ([argb-i (* 4 (+ (+ dx x) (* (+ dy y) argb-w)))]
[m1 (vector-ref argb-vector argb-i)] [m1 (vector-ref argb-vector argb-i)]
[m2 (char->integer (string-ref mask-str (+ str-i 1)))] ;; get red coordinate [m2 (char->integer (string-ref mask-str (+ str-i 1)))] ;; get red coordinate
[m3 (build-m3 m1 m2)] [m3 (build-m3 m1 m2)]