RSS/Atom feed Twitter
Site is read-only, email is disabled

Edit Alpha channel as Mask script

This discussion is connected to the gimp-user-list.gnome.org mailing list which is provided by the GIMP developers and not related to gimpusers.com.

This is a read-only list on gimpusers.com so this discussion thread is read-only, too.

2 of 2 messages available
Toggle history

Please log in to manage your subscriptions.

Edit Alpha channel as Mask script pggimeno@wanadoo.es 22 Mar 18:50
  Edit Alpha channel as Mask script Guillermo S. Romero / Familia Romero 22 Mar 19:26
pggimeno@wanadoo.es
2003-03-22 18:50:44 UTC (about 21 years ago)

Edit Alpha channel as Mask script

Hello,

I've just completed a Script-Fu script that some people may find quite useful (I've missed this functionality myself several times). The purpose is to convert a layer's Alpha channel into a mask for editing it and later using Apply Layer Mask to put it back. The idea is similar to what QuickMask does with selections.

Its working basis is using Threshold Alpha with a parameter of -1 (which can't be done interactively) to completely remove the alpha channel of the image after creating a mask out of it. Took a while to figure out how to do that.

Maybe there's an easier way to do the same, but after searching in the manual, the web and the groups I didn't find anything so I decided to try it by myself and found that solution.

Please let me know about any problem experienced during the usage of this script (not listed below), or any situation under which it does not work properly.

License:

The script is donated to the public domain.

Installation instructions:

Download this file:

and put it in /.gimp-1.2/scripts. If The Gimp was loaded when installing it, select Xtns -> Script-Fu -> Refresh to make it available without reloading the application.

That's all. The new function will appear in the image's menu, Script-Fu -> Utils -> Edit Alpha as Mask.

Requisites to use it:

- The layer must have an Alpha channel. - The layer must not already have a mask.

Usage:

Select the layer for which alpha is to be edited, execute the script and edit the mask in the usual way (with Alt + click in the mask thumbnail if needed, or whatever). When done, right-click on the Layers dialog and select the Apply Layer Mask option.

Caveats:

- The selection currently active will be lost when invoking the script.
- Some bugs in The Gimp make it behave in a somewhat unexpected manner, but they're mostly cosmetic concerns and the script works safely. Details follow.

Bugs/problems found in Gimp during the development of this script:

1. After using the script, the thumbnails in the Layers/Channels/Paths dialog are sometimes not refreshed. An Undo followed by a Redo on the image can serve as a workaround for this problem.

2. When using Apply Layer Mask or Delete Layer Mask after the mask has been edited, the enabled state of the menus is not refreshed. For this reason, if the mask (which is a monochrome image with no alpha) was selected when performing Apply Layer Mask, the script can't be invoked again because its menu entry is grayed. A workaround is to switch to another layer then return to this one. The undo/redo trick also works here.

3. The threshold-alpha plug-in does not suport INDEXEDA images (these are paletted images with Alpha channel). INDEXEDA support is disabled in the script until this problem is solved (if ever).

4. (Not directly related to the script but should be noted anyway.) At least Gimp 1.2.4pre-20030213 for Windows does not save the masks when saving to a .xcf file. Remember to apply the mask before saving to avoid losing the transparency info.

HTH

Guillermo S. Romero / Familia Romero
2003-03-22 19:26:41 UTC (about 21 years ago)

Edit Alpha channel as Mask script

pggimeno@wanadoo.es (2003-03-22 at 1850.44 +0100):

I've just completed a Script-Fu script that some people may find quite useful (I've missed this functionality myself several times). The purpose is to convert a layer's Alpha channel into a mask for editing it and later using Apply Layer Mask to put it back. The idea is similar to what QuickMask does with selections.

Maybe you can poke at the attached script, a bit old, for reference see http://groups.yahoo.com/group/gimpi/message/1479 and other posts about tribes 2 video game. This one does not lose the selection, IIRC.

GSR
;;; The GIMP -- an image manipulation program ;;; Copyright (C) 1995 Spencer Kimball and Peter Mattis ;;;
;;; script-fu-alpha-to-mask
;;; Moves the transparency of a layer to the layer mask ;;; Copyright (c) 2001 Guillermo S. Romero ;;; famrom infernal-iceberg.com
;;;
;;; ### License ###
;;;
;;; This program is free software; you can redistribute it and/or modify ;;; it under the terms of the GNU General Public License as published by ;;; the Free Software Foundation; either version 2 of the License, or ;;; (at your option) any later version. ;;;
;;; This program is distributed in the hope that it will be useful, ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;;; GNU General Public License for more details. ;;;
;;; You can get a copy of the GNU General Public License from ;;; http://www.gnu.org/, the site of the Free Software Foundation, Inc. ;;;
;;; ### Versions ###
;;;
;;; 1.00.00 Initial release (bug in L,C&P thumbnails?) ;;; 1.01.00 Support for existent mask added (merges transp and mask) ;;;
;;; ### Inline Documentation ###
;;;
;;; # Intro #
;;; Tribes 2, a 3D game, uses RGBA PNGs as skins. The RGB are used for ;;; the colour, and the A for the quantity of environment mapping. ;;; Or at least that is what I have read. ;;;
;;; # Requirements #
;;; This plugin was developed with Gimp 1.2.1 CVS ;;; Check all your plugins and scripts in the DB Browser before reporting bugs ;;; It merges the current layer mask, if any, so you keep all info ;;;
;;; # Parameters #
;;; None the user can play with
;;;
;;; # Debugging and to-do #
;;; Look for ";; @" comments

;; Helper functions from neon-logo.scm (define (set-pt a index x y)
(prog1
(aset a (* index 2) x)
(aset a (+ (* index 2) 1) y)))

(define (AlphaSpline) (let* ((a (cons-array 6 'byte)))
(set-pt a 0 0 255)
(set-pt a 1 127 255)
(set-pt a 2 255 255)
a))

;; The function
(define (script-fu-alpha-to-mask inImg inDrw)

;; Start (gimp-undo-push-group-start inImg)

;; Merge mask if any (set! OrigMask (car (gimp-layer-mask inDrw))) (if (not (= OrigMask -1))
(gimp-image-remove-layer-mask inImg inDrw APPLY))

;; Create a new mask (set! Mask (car (gimp-layer-create-mask inDrw ALPHA-MASK))) (gimp-image-add-layer-mask inImg inDrw Mask)

;; Unerase (gimp-curves-spline inDrw 4 6 (AlphaSpline))

;; End (gimp-undo-push-group-end inImg)
;; @ The image thumbnail does not update nice ;; @ Undo then redo and it refreshes to the right state ;; @ Or change to another image and back, to force the refresh too ;; @ A refresh call is missing in the C code, maybe fixed for Gimp 1.4 (gimp-displays-flush))

;; Register! (script-fu-register "script-fu-alpha-to-mask" _"/Script-Fu/Utils/Alpha to layer mask" "Moves the transparency of a layer to the layer mask" "Guillermo S. Romero"
"2001 Guillermo S. Romero"
"2001-04-29"
"RGBA GRAYA"
SF-IMAGE "Image where to work" 0 SF-DRAWABLE "Drawable to modify" 0)