[racket] 2htdp/image: colors and alphas

From: Robby Findler (robby at eecs.northwestern.edu)
Date: Mon Dec 27 08:58:51 EST 2010

[ sorry -- hit send too soon. ]

I like your idea for alphas in colors. I'll add that, hopefully today.

In the meantime, here's map-image with the current colors.

#lang racket/base
(require racket/draw
         (only-in mrlib/image-core render-image))

(define (map-image img f)
  (define w (image-width img))
  (define h (image-height img))
  (define bm (make-bitmap w h))
  (define bdc (make-object bitmap-dc% bm))
  (render-image img bdc 0 0)
  (send bdc set-bitmap #f)
  (define bytes (make-bytes (* w h 4)))
  (send bm get-argb-pixels 0 0 w h bytes)
  (for ([i (in-range 0 (* w h 4) 4)])
    (define nc
      (f (make-color (bytes-ref bytes (+ i 1))
                     (bytes-ref bytes (+ i 2))
                     (bytes-ref bytes (+ i 3)))))
    (bytes-set! bytes i 255)
    (bytes-set! bytes (+ i 1) (color-red nc))
    (bytes-set! bytes (+ i 2) (color-green nc))
    (bytes-set! bytes (+ i 3) (color-blue nc)))
  (send bm set-argb-pixels 0 0 w h bytes)
  (make-object image-snip% bm))

(overlay (rectangle 100 10 "solid" "purple")
         (rectangle 10 100 "solid" "forestgreen"))
(map-image (overlay (rectangle 100 10 "solid" "purple")
                    (rectangle 10 100 "solid" "forestgreen"))
           (λ (c)
             (define avg (floor (/ (+ (color-red c)
                                      (color-blue c)
                                      (color-green c))
             (make-color avg avg avg)))

On Mon, Dec 27, 2010 at 7:47 AM, Robby Findler
<robby at eecs.northwestern.edu> wrote:
> On Mon, Dec 27, 2010 at 7:13 AM, Stephen Bloch <sbloch at adelphi.edu> wrote:
>> I'm trying to port the picturing-programs teachpack to still work with 5.1.  (BTW, approximately how long do I have to do that before 5.1 is released?)
> We have not started the release process yet, but the release is
> sometime in Jan. Generally releases are every 3 months. Ryan should
> know the precise dates.
>> Right now it crashes because the API for make-bitmap has changed -- for the better, I think, but I still need to fix things.
> Which make-bitmap would that be? Are you talking about the internal,
> private constructor?
>> I've got a "map-image" function that creates a [bitmap] image the same width and height as an existing image by mapping a color->color function on its pixels, and a "build-image" function that creates a [bitmap] image of a specified width and height by mapping a specified function on the coordinates of its pixels.  I also wrote (but didn't really document) a "map-masked-image" and "build-masked-image" which do the same, but the mapped function is also allowed to return #f, in which case the specified pixel is made transparent (i.e. alpha=0).  I can tweak those to work with the new make-bitmap, but the process got me wondering...
>> It would be nice if I could allow students to generate alphas other than 1 and 0.  It seems to me the cleanest way to do this is with an alpha-color struct (which existed in htdp/image, but seems to be gone from 2htdp/image).  I can do that myself, putting in glue code between it and 2htdp/image to convert back and forth among various color-like-things (color, alpha-color, string, symbol, color%, and #f for transparency), but 2htdp/image is already doing a lot of that, and it feels really inefficient to re-do it.
>> What would you think of ALL "color" structs actually having an alpha component, with the constructor allowing alpha to default to 1?
> I think you should be operating at the racket/draw bitmap level, not
> the 2htdp/image bitmap level. That is, use the render-image function
> to get a bitmap, and then make a loop that calls their function and
> fills in a bytes and then turn that bytes back into a bitmap. This
> will save creating a bunch of intermediate data and is a simpler
> function overall.
> Robby

Posted on the users mailing list.