User:Stefan Vladuck/Unsharp Mask Multilayer

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search

Unsharp Mask Multilayer is an experimental GIMP 2.4 script for image sharpening based on the unsharp mask filter. Its main purpose is to reduce the unsightly haloes around high-contrast edges. Note however that haloes in the original image can not be reduced (and will probably be enhanced, though possibly less than with standard unsharp mask). Noise amplification also tends to be lower than with standard unsharp mask.

Usage notes[edit]

  • The script is called via Filters > Enhance > Unsharp Mask Multilayer.
  • The parameters have the same meaning as for the standard unsharp mask, except that there are two separate sets for highlight and shadow sharpening, respectively.
  • Hints for parameter choice: For good results, the radius probably shouldn't be much greater than 1 (default: 0.3). You can choose a fairly large amount (default: 2) and adjust it later via layer opacity (see below). A too large amount however may create unsightly artifacts, so don't crank this to 11 if you don't need it. You can use the threshold (default: 0) to reduce noise amplification. However, this method tends to produce less noise amplification than standard unsharp mask, even with zero threshold.
  • The script will create four new layers. The original image remains unchanged.
    • The Highlight sharpen and Shadow sharpen layers control highlight and shadow sharpening, respectively. You can fine-tune the strength of the sharpening by altering the layer opacities.
    • The Highlight sharpen limit and Shadow sharpen limit layers reduce the bright resp. dark haloes around high-contrast edges. As a side effect, the amplification of fine-grained noise is also mitigated. However, the enhancement of very fine (pixel scale) detail is also reduced. You can fine-tune the strength of the halo reduction by altering the layer opacities.
  • Note: For some strange reason, the image doesn't display correctly after applying the script (image looks blurred, at least on my computer). Switching the active layer fixes the display.

To do[edit]

  • add proper description
  • add proper license

The script (v0.1)[edit]

Tested with GIMP 2.4.5. Save with extension ".scm" in $HOME/.gimp-2.4/scripts/ or the system-wide scripts folder.

(script-fu-register
	"script-fu-unsharp-mask-multilayer"
	"Unsharp Mask Multilayer..."
	"Advanced sharpening based on USM"
	"Stefan Vladuck"
	"copyright 2008, Stefan Vladuck"
	"May 21, 2008"
	"RGB* GRAY*"
	SF-IMAGE	"Image"		0
	SF-DRAWABLE	"Drawable"	0
	SF-ADJUSTMENT "Highlight USM Radius" '(.3 .1 120 .1 1 1 0)
	SF-ADJUSTMENT "Highlight USM Amount" '(2 0 10 .01 .1 2 0)
	SF-ADJUSTMENT "Highlight USM Threshold" '(0 0 255 1 10 0 0)
	SF-ADJUSTMENT "Shadow USM Radius" '(.3 .1 120 .1 1 1 0)
	SF-ADJUSTMENT "Shadow USM Amount" '(2 0 10 .01 .1 2 0)
	SF-ADJUSTMENT "Shadow USM Threshold" '(0 0 255 1 10 0 0)
)
(script-fu-menu-register "script-fu-unsharp-mask-multilayer" "<Image>/Filters/Enhance")
(define (script-fu-unsharp-mask-multilayer 
	image 
	drawable 
	light-radius 
	light-amount
	light-threshold
	dark-radius
	dark-amount
	dark-threshold)
  (let* (
    (layer-light 0)
    (layer-dark 0)
    (layer-light-limit 0)
    (layer-dark-limit 0)
    (layer-temp 0)
    )
    (gimp-image-undo-group-start image)

    (set! layer-dark 
      (car 
        (gimp-layer-new-from-drawable drawable image)))
    (gimp-image-add-layer image layer-dark -1)
    (gimp-drawable-set-name layer-dark "Shadow sharpen")
    (plug-in-unsharp-mask 1 image layer-dark dark-radius dark-amount dark-threshold)
    (gimp-layer-set-mode layer-dark DARKEN-ONLY-MODE)

    (set! layer-light 
      (car 
        (gimp-layer-new-from-drawable drawable image)))
    (gimp-image-add-layer image layer-light -1)
    (gimp-drawable-set-name layer-light "Highlight sharpen")
    (plug-in-unsharp-mask 1 image layer-light light-radius light-amount light-threshold)
    (gimp-layer-set-mode layer-light LIGHTEN-ONLY-MODE)

    (set! layer-dark-limit
      (car
        (gimp-layer-new-from-drawable drawable image)))
    (gimp-image-add-layer image layer-dark-limit -1)
    (gimp-drawable-set-name layer-dark-limit "Shadow sharpen limit")
    (set! layer-temp
      (car
        (gimp-layer-new-from-drawable drawable image)))
    (gimp-image-add-layer image layer-temp -1)
    (gimp-layer-set-mode layer-temp DARKEN-ONLY-MODE)
    (gimp-drawable-offset layer-temp FALSE OFFSET-TRANSPARENT 1 0)
    (gimp-image-merge-down image layer-temp 1)
    (set! layer-temp
      (car
        (gimp-layer-new-from-drawable drawable image)))
    (gimp-image-add-layer image layer-temp -1)
    (gimp-layer-set-mode layer-temp DARKEN-ONLY-MODE)
    (gimp-drawable-offset layer-temp FALSE OFFSET-TRANSPARENT -1 0)
    (gimp-image-merge-down image layer-temp 1)
    (set! layer-temp
      (car
        (gimp-layer-new-from-drawable 
          (car (gimp-image-get-active-drawable image))
          image)))
    (gimp-image-add-layer image layer-temp -1)
    (gimp-layer-set-mode layer-temp DARKEN-ONLY-MODE)
    (gimp-drawable-offset layer-temp FALSE OFFSET-TRANSPARENT 0 1)
    (gimp-image-merge-down image layer-temp 1)
    (set! layer-temp
      (car
        (gimp-layer-new-from-drawable 
          (car (gimp-image-get-active-drawable image))
          image)))
    (gimp-image-add-layer image layer-temp -1)
    (gimp-layer-set-mode layer-temp DARKEN-ONLY-MODE)
    (gimp-drawable-offset layer-temp FALSE OFFSET-TRANSPARENT 0 -1)
    (gimp-image-merge-down image layer-temp 1)
    (gimp-layer-set-mode
      (car (gimp-image-get-active-drawable image))
      LIGHTEN-ONLY-MODE)

    (set! layer-light-limit
      (car
        (gimp-layer-new-from-drawable drawable image)))
    (gimp-image-add-layer image layer-light-limit -1)
    (gimp-drawable-set-name layer-light-limit "Highlight sharpen limit")
    (set! layer-temp
      (car
        (gimp-layer-new-from-drawable drawable image)))
    (gimp-image-add-layer image layer-temp -1)
    (gimp-layer-set-mode layer-temp LIGHTEN-ONLY-MODE)
    (gimp-drawable-offset layer-temp FALSE OFFSET-TRANSPARENT 1 0)
    (gimp-image-merge-down image layer-temp 1)
    (set! layer-temp
      (car
        (gimp-layer-new-from-drawable drawable image)))
    (gimp-image-add-layer image layer-temp -1)
    (gimp-layer-set-mode layer-temp LIGHTEN-ONLY-MODE)
    (gimp-drawable-offset layer-temp FALSE OFFSET-TRANSPARENT -1 0)
    (gimp-image-merge-down image layer-temp 1)
    (set! layer-temp
      (car
        (gimp-layer-new-from-drawable 
          (car (gimp-image-get-active-drawable image))
          image)))
    (gimp-image-add-layer image layer-temp -1)
    (gimp-layer-set-mode layer-temp LIGHTEN-ONLY-MODE)
    (gimp-drawable-offset layer-temp FALSE OFFSET-TRANSPARENT 0 1)
    (gimp-image-merge-down image layer-temp 1)
    (set! layer-temp
      (car
        (gimp-layer-new-from-drawable 
          (car (gimp-image-get-active-drawable image))
          image)))
    (gimp-image-add-layer image layer-temp -1)
    (gimp-layer-set-mode layer-temp LIGHTEN-ONLY-MODE)
    (gimp-drawable-offset layer-temp FALSE OFFSET-TRANSPARENT 0 -1)
    (gimp-image-merge-down image layer-temp 1)
    (gimp-layer-set-mode
      (car (gimp-image-get-active-drawable image))
      DARKEN-ONLY-MODE)

    (gimp-image-undo-group-end image)
  )
)