Dreamstime

Friday, 31 August 2018

GIMP Scripts For Dealing With Blue And Green Fringe

My Canon Powershot SX530 HS suffers from bad Purple, Blue and Green chromatic aberration when taking pictures in high contrast area. I use Darla's Purple Fringe script for GIMP to deal with Purple chromatic aberration - some limitations but that's the best solution that I can find.

I could not find any GIMP scripts for dealing with Blue and Green chromatic aberration. So I did the next best thing: I rolled my own.

The code for the scripts are below. Once installed, it will appear under the "Script-Fu / Enhance /" menu. And one more thing: the same limitations apply as with Darla's Purple Fringe script.


==== BEGIN (BlueFringe.scm) ====

;
; Blue Fringe, V1.0
;
; AUTHOR: Chuah TC (http://chuahtc.blogspot.com) (C) 2018
;
; This plugin was tested with GIMP 2.8.16
;
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License Version 3 as 
; published by the Free Software Foundation.
; 
; 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 at  http://www.gnu.org/licenses for
; more details.
;
; DESCRIPTION:
; To reduce the the effects of blue fringing or chromatic aberration 
; in high contrast areas of an affected photograph.
; The script is located in menu "<Image> / Script-Fu / Enhance / Blue Fringe"
;
; =============================================================================
;
;
; SCRIPT SUMMARY:
; use mask to affect edges only, adjust saturation of blue, magenta, cyan, green
; 
; Version 1.0 (2018) - Initial version - adapted from 'Darla-PurpleFringe.scm'.
; =============================================================================

(define (script-fu-BlueFringe InImage InLayer InEdge InBlur InDeBlue InDeMag InDeCyan InDeGreen InFlatten)
        (gimp-image-undo-group-start InImage)

        (let*   (
                (FringeLayer (car (gimp-layer-copy InLayer TRUE)))
                (MaskImage (car (gimp-image-duplicate InImage)))
                (MaskLayer (cadr (gimp-image-get-layers MaskImage)))
                (HSVImage  (car (plug-in-decompose TRUE InImage InLayer "Value" TRUE)))
                (HSVLayer  (cadr (gimp-image-get-layers HSVImage)))
                )

                ; desats
                (gimp-image-add-layer InImage FringeLayer -1)
                (gimp-hue-saturation FringeLayer 5 0 0 InDeBlue)
                (gimp-hue-saturation FringeLayer 6 0 0 InDeMag)
                (gimp-hue-saturation FringeLayer 4 0 0 InDeCyan)
                (gimp-hue-saturation FringeLayer 3 0 0 InDeGreen)

                ; Find edges, Warpmode = Smear (1), Edgemode = Sobel (0)
                (plug-in-edge TRUE MaskImage (aref MaskLayer 0) InEdge 1 0)
                (gimp-levels (aref MaskLayer 0) 0 75 255 0.8 0 255)
                (gimp-convert-grayscale MaskImage)
                (plug-in-gauss TRUE MaskImage (aref MaskLayer 0) InBlur InBlur 0)

                ; next change FringeLayer to new layer
                (let*   (
                        (FringeMask (car (gimp-layer-create-mask FringeLayer ADD-WHITE-MASK)))
                        )
                        (gimp-layer-add-mask FringeLayer FringeMask)
                        (gimp-selection-all MaskImage)
                        (gimp-edit-copy (aref MaskLayer 0))
                        (gimp-floating-sel-anchor (car (gimp-edit-paste FringeMask FALSE)))
                        (gimp-image-delete MaskImage)
                )

                ; flatten image if needed
                (cond
                        ((= InFlatten TRUE) (gimp-image-merge-down InImage FringeLayer CLIP-TO-IMAGE))
                        ((= InFlatten FALSE) (gimp-drawable-set-name FringeLayer "Blue Fringe Adjustment"))
                )
        )

        (gimp-image-undo-group-end InImage)
        (gimp-displays-flush)
)

(script-fu-register 
        "script-fu-BlueFringe"
        "<Image>/Script-F_u/Enhance/_Blue Fringe"
        "Blue Fringe \n\
To reduce the effects of Blue Fringing or \
Chromatic Aberration in high contrast areas of \
an affected photograph. \n\
==="
        "Chuah TC (http://chuahtc.blogspot.com)"
        "Chuah TC"
        "2018"
        "RGB*"
        SF-IMAGE                "The Image"             0
        SF-DRAWABLE             "The Layer"             0
        SF-ADJUSTMENT   _"Edges: Detect Amount" '(7.0 1.0 10.0 1.0 0 1 0)
        SF-ADJUSTMENT   _"Edges: Blur Pixels"   '(5.0 1.0 10.0 1.0 0 1 0)
        SF-ADJUSTMENT   _"Desat level: Blue"    '(-80 -100 100 1 0 0 0)
        SF-ADJUSTMENT   _"Desat level: Magenta" '(-60 -100 100 1 0 0 0)
        SF-ADJUSTMENT   _"Desat level: Cyan"    '(-40 -100 100 1 0 0 0)
        SF-ADJUSTMENT   _"Desat level: Green"   '(0 -100 100 1 0 0 0)
        SF-TOGGLE               _"Flatten Image"                FALSE
)

==== END ====


==== BEGIN (GreenFringe.scm) ====

;
; Green Fringe, V1.0
;
; AUTHOR: Chuah TC (http://chuahtc.blogspot.com) (C) 2018
;
; This plugin was tested with GIMP 2.8.16
;
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License Version 3 as 
; published by the Free Software Foundation.
; 
; 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 at  http://www.gnu.org/licenses for
; more details.
;
; DESCRIPTION:
; To reduce the the effects of green fringing or chromatic aberration 
; in high contrast areas of an affected photograph.
; The script is located in menu "<Image> / Script-Fu / Enhance / Green Fringe"
;
; =============================================================================
;
;
; SCRIPT SUMMARY:
; use mask to affect edges only, adjust saturation of green, cyan, yellow, red
; 
; Version 1.0 (2018) - Initial version - adapted from 'Darla-PurpleFringe.scm'.
; =============================================================================

(define (script-fu-GreenFringe InImage InLayer InEdge InBlur InDeGreen InDeCyan InDeYellow InDeRed InFlatten)
        (gimp-image-undo-group-start InImage)

        (let*   (
                (FringeLayer (car (gimp-layer-copy InLayer TRUE)))
                (MaskImage (car (gimp-image-duplicate InImage)))
                (MaskLayer (cadr (gimp-image-get-layers MaskImage)))
                (HSVImage  (car (plug-in-decompose TRUE InImage InLayer "Value" TRUE)))
                (HSVLayer  (cadr (gimp-image-get-layers HSVImage)))
                )

                ; desats
                (gimp-image-add-layer InImage FringeLayer -1)
                (gimp-hue-saturation FringeLayer 3 0 0 InDeGreen)
                (gimp-hue-saturation FringeLayer 4 0 0 InDeCyan)
                (gimp-hue-saturation FringeLayer 2 0 0 InDeYellow)
                (gimp-hue-saturation FringeLayer 1 0 0 InDeRed)

                ; Find edges, Warpmode = Smear (1), Edgemode = Sobel (0)
                (plug-in-edge TRUE MaskImage (aref MaskLayer 0) InEdge 1 0)
                (gimp-levels (aref MaskLayer 0) 0 75 255 0.8 0 255)
                (gimp-convert-grayscale MaskImage)
                (plug-in-gauss TRUE MaskImage (aref MaskLayer 0) InBlur InBlur 0)

                ; next change FringeLayer to new layer
                (let*   (
                        (FringeMask (car (gimp-layer-create-mask FringeLayer ADD-WHITE-MASK)))
                        )
                        (gimp-layer-add-mask FringeLayer FringeMask)
                        (gimp-selection-all MaskImage)
                        (gimp-edit-copy (aref MaskLayer 0))
                        (gimp-floating-sel-anchor (car (gimp-edit-paste FringeMask FALSE)))
                        (gimp-image-delete MaskImage)
                )

                ; flatten image if needed
                (cond
                        ((= InFlatten TRUE) (gimp-image-merge-down InImage FringeLayer CLIP-TO-IMAGE))
                        ((= InFlatten FALSE) (gimp-drawable-set-name FringeLayer "Green Fringe Adjustment"))
                )
        )

        (gimp-image-undo-group-end InImage)
        (gimp-displays-flush)
)

(script-fu-register 
        "script-fu-GreenFringe"
        "<Image>/Script-F_u/Enhance/_Green Fringe"
        "Green Fringe \n\
To reduce the effects of Green Fringing or \
Chromatic Aberration in high contrast areas of \
an affected photograph. \n\
==="
        "Chuah TC (http://chuahtc.blogspot.com)"
        "Chuah TC"
        "2018"
        "RGB*"
        SF-IMAGE                "The Image"             0
        SF-DRAWABLE             "The Layer"             0
        SF-ADJUSTMENT   _"Edges: Detect Amount" '(7.0 1.0 10.0 1.0 0 1 0)
        SF-ADJUSTMENT   _"Edges: Blur Pixels"   '(5.0 1.0 10.0 1.0 0 1 0)
        SF-ADJUSTMENT   _"Desat level: Green"   '(-80 -100 100 1 0 0 0)
        SF-ADJUSTMENT   _"Desat level: Cyan"    '(-60 -100 100 1 0 0 0)
        SF-ADJUSTMENT   _"Desat level: Yellow"  '(-40 -100 100 1 0 0 0)
        SF-ADJUSTMENT   _"Desat level: Red"     '(0 -100 100 1 0 0 0)
        SF-TOGGLE               _"Flatten Image"                FALSE
)

==== END ====


0 comments:

Post a Comment