Using `draw-bitmap` and `draw-bitmap-section` with transparent color images

Hi All,

I need help to draw a "sprite" (small bitmap) on top of a bitmap.
The sprite in question is consists of a transparent background and has a solid yellow circle in the center.

My test program looks like this (scroll to see the end of the program):

#lang racket
(require racket/gui)
(define w 200)
(define h 200)

(define bm    (make-object bitmap% w h))
(define dc    (new bitmap-dc% [bitmap bm]))
(define black (send the-color-database find-color "black"))
(define sprite (make-object bitmap% "sprites.png"))

(send dc set-background black)
(send dc clear)
bm
sprite

(send dc set-smoothing 'unsmoothed)
(send dc draw-bitmap-section
      sprite          ; source 
      0 0             ; dest
      0 0 16 16       ; source
      'solid          ; ignored for color bitmaps
      black           ; ignored for color bitmaps
      sprite          ; use the α channel as mask
      )
bm

The output is shown below. It shows that the sprite is drawn on top of the black background.
I was expecting a "solid" yellow, but the black background is seen below the sprite.

Why?

The image "sprites.png" is here:

sprites

I think I found a fix:

(define sprite (make-object bitmap% "sprites.png" 'png/alpha))

I think for some reason the implementation decides to just load your particular image with kind 'png so to force it to load it with alpha I set it explicitly. I am not sure how it decides what kind to use when it is implicitly choosen and whether it can be influenced by the image and what format it has etc.

But I could imagine that images with particular formats may have different kinds assigned implicitly.
For example I saw that your image uses indexed colors with an alpha channel which is relatively rare in circumstances I am used too, maybe that influences it? Well all of that is speculation for more answers we would have to dig into the implementation...

2 Likes

That's it!

I assumed that the png was loaded with its alpha channel automatically.

Thanks for find the error - it was beginning to bug me :slight_smile:

2 Likes