[plt-scheme] writing quickly to bitmap-dc%

From: Alexander Friedman (alex at inga.mit.edu)
Date: Tue Aug 5 13:10:48 EDT 2003

Hi all,

In an attemp to optimize the execution time of my plotting library, i decided 
to circumvent the file system and keep everything in memory. The c library 
writes to a block of data in memeory (currently a homo-u8-vector ), that scheme 
then interprets as a bitmap, drawing each pixel into a bitmap-dc%. 
The result : a significant decrease in perfomance. 

The code:

(define (bits->bitmap-dc% bitmap)
        (let ((bmdc (instantiate bitmap-dc% () (bitmap (make-object bitmap% x-
size y-size #f)))))
          (send bmdc set-background (make-object color% 255 255 255))
          (send bmdc clear)
          (bm-helper x-size y-size 0 0 bitmap bmdc)
            (send bmdc get-bitmap)
            (send bmdc set-bitmap #f))))

      ; do the actuall proccessing
      (define (bm-helper x-max y-max x-c y-c bitmap dc)
          [(= x-max x-c) (bm-helper x-max y-max 0 (add1 y-c) bitmap dc)]
          [(= y-c y-max) #t]
           (let ((r (u8vector-ref bitmap (+ (* 3 y-c x-max) (* 3 x-c) 0)))
                 (g (u8vector-ref bitmap (+ (* 3 y-c x-max) (* 3 x-c) 1)))
                 (b (u8vector-ref bitmap (+ (* 3 y-c x-max) (* 3 x-c) 2))))
             (unless (= r g b 255)
               (send dc set-pixel x-c y-c 
                 (make-object color% r g b)))               
             (bm-helper x-max y-max (add1 x-c) y-c bitmap dc))]))

 From what i understand, doing set-pixel on X-windows based systems is expensive,
even with the optimization of only writing the pixels that arn't white (they 
are all white by default). 

There are other places on the chain of execution where i can intercept the C 
library. The c-lib uses drivers to create the image - the one i was using 
before was an interface to the gd library, the one i am using now writes 
directly to memory. The drivers only have to implement a small set of commands
to satisfy the interface: draw a line, draw multiple lines, and change state 
(color, pen width, etc). Perhaps i could take a bitmap-dc and have the driver 
call the equivelent functions on it directly. Even better would be to be able 
to access the bitmap portion of the bitmap-dc% and simply copy over the bitmap 
(in C) that is created by the c-library without going through scheme.

Any suggestions?


Posted on the users mailing list.