;;; edge-sharpen.scm v0.1 ;;; Copyright (c) 2004 John Hall (jhall@alum.wpi.edu) ;;; ;;; A Script-Fu script for The GIMP to automate an edge sharpening ;;; technique. ;;; ;;; ;;; 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.1: - First public release ;; Performs an edge sharpen. A copy of the image is enlarged to 120% ;; of its original size, sharpened with "smart" sharpening, then ;; reduced back to its original size. Finally, the sharpened layer is ;; copied back to the original image. This technique helps minimize ;; halo artifacts that can appear when using Unsharp Mask. ;; ;; I take no credit for inventing this technique. It is described by ;; Paul Caldwell here: ;; http://www.outbackphoto.com/workflow/wf_28/essay.html (define (script-fu-edge-sharpen image drawable) ;; Returns the least integer n that is greater than or equal to x. (define (ceiling x) (+ x (- 1 (fmod x 1)))) ;; Scales the specified drawable. This procedure provides backwards ;; compatibility for GIMP 2.0, which does not have the ;; gimp-drawable-transform-scale procedure (define (drawable-scale drawable width height) (if (not (symbol-bound? 'gimp-drawable-transform-scale)) (gimp-scale drawable TRUE 0 0 width height) (gimp-drawable-transform-scale drawable 0 0 width height TRANSFORM-FORWARD INTERPOLATION-CUBIC TRUE 3 FALSE))) ;; Returns the specified mask's layer, or -1 if none exists. This ;; procedure provides backwards compatibility for GIMP 2.0, which ;; does not have the gimp-layer-from-mask procedure (define (layer-from-mask mask) (define (layer-from-mask-iter num-layers layer-ids index) (if (>= index num-layers) -1 (if (= mask (car (gimp-layer-get-mask (aref layer-ids index)))) (list (aref layer-ids index)) (layer-from-mask-iter num-layers layer-ids (+ 1 index))))) (if (symbol-bound? 'gimp-layer-from-mask) (gimp-layer-from-mask mask) (let* ((layers (gimp-image-get-layers (car (gimp-drawable-get-image mask)))) (num-layers (car layers)) (layer-ids (cadr layers))) (layer-from-mask-iter num-layers layer-ids 0)))) (let* ((new-image (car (gimp-image-duplicate image))) (new-drawable (car (gimp-image-flatten new-image))) (original-width (car (gimp-image-width new-image))) (original-height (car (gimp-image-height new-image))) (new-width (ceiling (* original-width 1.2))) (new-height (ceiling (* original-height 1.2)))) (gimp-image-undo-group-start image) ;; Upsize image by 20%, apply "smart" sharpening, then downsize (gimp-image-resize new-image new-width new-height 0 0) (drawable-scale new-drawable new-width new-height) (script-fu-smart-sharpen2 new-image new-drawable) (gimp-image-scale new-image original-width original-height) (let* ((layer (car (gimp-image-get-active-drawable new-image))) (layer (if (gimp-drawable-is-layer-mask layer) (car (layer-from-mask layer)) layer)) (mask (car (gimp-layer-get-mask layer))) (sharpen-layer (car (gimp-layer-new image original-width original-height RGBA-IMAGE "Sharpening" 80.0 VALUE-MODE))) (sharpen-mask (car (gimp-layer-create-mask sharpen-layer WHITE-MASK)))) ;; Copy sharpened layer (gimp-image-add-layer image sharpen-layer -1) (gimp-edit-copy layer) (gimp-floating-sel-anchor (car (gimp-edit-paste sharpen-layer TRUE))) ;; Copy sharpened layer mask (gimp-layer-add-mask sharpen-layer sharpen-mask) (gimp-edit-copy mask) (gimp-floating-sel-anchor (car (gimp-edit-paste sharpen-mask TRUE)))) (gimp-image-delete new-image) (gimp-displays-flush) (gimp-image-undo-group-end image) (list image))) (script-fu-register "script-fu-edge-sharpen" "/Script-Fu/Enhance/Edge Sharpen" "Performs a smart sharpen on an image." "John Hall" "John Hall" "2004" "" SF-IMAGE "The image" 0 SF-DRAWABLE "The layer (not used)" 0)