Alpha and translucency detection in Gimp

The alpha RLE code in Gimp makes per-pixel alpha much faster than before,
and actually not very much slower than normal simple transparency.
In fact, there is almost no difference in performance between blitting
a colourkeyed image, and the same image but with an alpha channel (and
thus only 0 and 255 as alpha values), if both are RLE encoded.

This makes it useful for a number of variants, especially sprites and
text with “soft” (antialiased) edges. Here the translucent (neither
opaque nor completely transparent) pixels are only present in a thin
stripe around the edges, and thus not too costly to blit.

I’ve noticed that it can be hard to find stray translucent pixels in an
image — almost-transparent pixels that are hiding in the clear areas,
or almost-opaque pixels in the filled areas — especially after you have
being playing with it long enough. Even if these rogue pixels don’t make
a visible difference, they will slow things down. So I made a small
gimp script, and thought I should share it.

Save it somewhere (.gimp-1.1/scripts/showtrans.scm, for example), and
(re)start Gimp. Now try the right-click menu in
Script-Fu->Utils->Show Translucency. It creates a layer that colours
opaque pixels red, transparent ones black, and the rest green.
The intrepid pixel hunter can then eliminate nasty greenies by painting
them black or red, and then use select by colour and clear (or anti-erase)
in the original layer to get rid of them. Happy Gimping!

;;; Create a new layer partitioning pixels by alpha: 0, 1…254, 255
;;;
;;; This is useful to locate stray translucent pixels when editing images
;;; with an alpha channel

(define showtrans-opaque-color '(255 0 0))
(define showtrans-translucent-color '(0 255 0))
(define showtrans-transparent-color '(0 0 0))

(define (fill-selection image drawable color)
(if (= (car (gimp-selection-is-empty image)) 0)
(let ((old-fg (car (gimp-palette-get-foreground))))
(gimp-palette-set-foreground color)
(gimp-bucket-fill drawable 0 0 100 0 0 0 0)
(gimp-palette-set-foreground old-fg))))

(define (script-fu-show-trans image drawable)
(if (gimp-drawable-is-layer drawable)
(begin
(gimp-image-undo-disable image)
(let ((lay1 (car (gimp-layer-copy drawable 1)))
(lay2 (car (gimp-layer-copy drawable 1))))
(gimp-image-add-layer image lay1 -1)
(gimp-image-add-layer image lay2 -1)
(gimp-selection-none image)
(plug-in-threshold-alpha 1 image lay1 0)
(gimp-selection-layer-alpha lay1)
(fill-selection image lay1 showtrans-translucent-color)
(gimp-selection-invert image)
(fill-selection image lay1 showtrans-transparent-color)
(gimp-selection-none image)
(plug-in-threshold-alpha 1 image lay2 254)
(gimp-selection-layer-alpha lay2)
(gimp-image-remove-layer image lay2)
(fill-selection image lay1 showtrans-opaque-color)
(gimp-selection-none image)
(gimp-layer-set-name lay1 “Translucency Map”))
(gimp-image-undo-enable image))))

(script-fu-register
"script-fu-show-trans"
"/Script-Fu/Utils/Show Translucency"
“Show map of translucent/opaque pixels”
“Mattias Engdeg?rd <@Mattias_Engdegard>”
“Mattias Engdeg?rd”
"July 2000"
"RGBA"
SF-IMAGE “Input Image” 0
SF-DRAWABLE “Input Drawable” 0)

I’ve noticed that it can be hard to find stray translucent pixels in an
image — almost-transparent pixels that are hiding in the clear areas,
or almost-opaque pixels in the filled areas — especially after you have
being playing with it long enough. Even if these rogue pixels don’t make
a visible difference, they will slow things down. So I made a small
gimp script, and thought I should share it.

another trick that I use is to cut off 10% of the alpha at either end of the
scale

ie. make alpha < 25 fully opaque
and alpha > 230 fully transparent

visually you can’t tell the difference but you still get the speed boost

“jan” a ?crit dans le message news:
8oerlb$hp6$1 at ftp.lokigames.com

I’ve noticed that it can be hard to find stray translucent pixels in an
image — almost-transparent pixels that are hiding in the clear areas,
or almost-opaque pixels in the filled areas — especially after you
have

being playing with it long enough. Even if these rogue pixels don’t make
a visible difference, they will slow things down. So I made a small
gimp script, and thought I should share it.

another trick that I use is to cut off 10% of the alpha at either end of
the
scale

ie. make alpha < 25 fully opaque
and alpha > 230 fully transparent

visually you can’t tell the difference but you still get the speed boost

and even faster, you could only process alpha with shifting and masking
you will only handle 0 25% 50% 75% 100% of alpha…

eg.
r_dest= ( ( r_src1 >> 1 ) & 0x7f ) + ( ( r_src2 >> 1 ) & 0x7f );

Gautier

(replying to two followups)

another trick that I use is to cut off 10% of the alpha at either end of
the
scale

ie. make alpha < 25 fully opaque
and alpha > 230 fully transparent

visually you can’t tell the difference but you still get the speed boost

For AA purposes you may still perceive a difference, and it isn’t really
costly. What I was getting at was eliminating pixels that are translucent
by sheer editing mistake, while keeping soft alpha gradients where you
want them.

and even faster, you could only process alpha with shifting and masking
you will only handle 0 25% 50% 75% 100% of alpha…

SDL already special-cases 50% transparency (alpha=128) for colorkey
RLE blitting with per-surface alpha (have a look at the code).
It is not clear that it’s a win for per-pixel alpha, and makes things
too messy in general.

Quantizing alpha is mostly useful in 8bpp, when you can use a look-up
table.