;;; contrast-mask.scm v0.3 ;;; Copyright (c) 2004 John Hall (jhall@alum.wpi.edu) ;;; ;;; A Script-Fu script for The GIMP to automate creating a contrast ;;; mask to reduce excessive contrast. ;;; ;;; ;;; 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 should have received a copy of the GNU General Public License ;;; along with this program; if not, write to the Free Software ;;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ;;; ;;; ;;; Revision history: ;;; ;;; v0.3: - Modified add-flattened-layer to add the new layer to the ;;; image. (Not doing so generates an error in The GIMP 2.0pre3. ;;; See bug #132504) ;;; v0.2: - Modified for The GIMP 2.0pre2 ;;; v0.1: - First public release (define (ceiling x) (+ x (- 1 (fmod x 1)))) ;; Creates a new layer for the image. The new layer is a copy of the ;; image with all of the layers flattened. Other attributes such as ;; name, layer mask mode, and opacity should be set with explicit ;; procedure calls. (define (add-flattened-layer image) (let* ((flattened-image (car (gimp-image-duplicate image))) (flattened-drawable (car (gimp-image-flatten flattened-image))) (new-layer (car (gimp-layer-new image (car (gimp-image-width image)) (car (gimp-image-height image)) RGBA-IMAGE "New Layer" 100.0 NORMAL-MODE)))) ;; Copy flattened image to new layer (gimp-edit-copy flattened-drawable) (gimp-image-add-layer image new-layer -1) (gimp-floating-sel-anchor (car (gimp-edit-paste new-layer TRUE))) (gimp-image-delete flattened-image) (list new-layer))) ;; Applies a contrast mask to an image to reduce excessive contrast. ;; ;; This method is described by Eric R. Jeschke here: ;; http://www.gimpguru.org/Tutorials/ContrastMask/ ;; ;; However, he adapted it for The GIMP from a Photoshop tutorial on ;; the luminous-landscape.com web site: ;; http://www.luminous-landscape.com/tutorials/contrast_masking.shtml ;; ;; The blur value for the contrast mask is calculated based on the ;; number of pixels in the image. The more pixels you have, the ;; higher the blur value. (define (script-fu-contrast-mask image drawable) (let* ((width (car (gimp-image-width image))) (height (car (gimp-image-height image))) (blur (* 5 (ceiling (/ (* width height) 1000000)))) (new-layer (car (add-flattened-layer image)))) (gimp-image-undo-group-start image) ;; Add contrast mask to image (gimp-drawable-set-name new-layer "Contrast Mask") (gimp-layer-set-mode new-layer OVERLAY-MODE) (gimp-layer-set-opacity new-layer 30.0) (gimp-desaturate new-layer) (gimp-invert new-layer) (plug-in-gauss-iir2 TRUE image new-layer blur blur) (gimp-image-undo-group-end image) (gimp-displays-flush) (list image))) (script-fu-register "script-fu-contrast-mask" "/Script-Fu/Enhance/Contrast Mask" "Reduces excessive contrast in an image." "John Hall" "John Hall" "2004" "" SF-IMAGE "The image" 0 SF-DRAWABLE "The layer (not used)" 0)