; Created for Lylekj: http://gimpchat.com/viewtopic.php?f=9&t=7258&p=92490#p92490

(define (script-fu-lylejk_preblur_resize image drawable new-width new-height autodet)
  (let* (          
           (owidth (car (gimp-image-width image)))
           (oheight (car (gimp-image-height image)))
           (dominant -1)
           (width new-width) (height new-height)
           (blur-rad 0)
        )

    (gimp-image-undo-group-start image)

; check if auto detect for size is selected
; if it is, then determine which is bigger width or height, then set the width/height flag appropriately
    (if (= autodet TRUE)
      (begin
        (if (>= oheight owidth)
            (set! dominant 1)
        )
        (if (< oheight owidth)
            (set! dominant 0)
        )
      )
    )
    
    (if (= dominant 0) ; width is the important size, change the height accordingly
        (set! height (/ oheight (/ owidth new-width)))
    )
    (if (= dominant 1) ; height is the important size, change the width accordingly
        (set! width (/ owidth (/ oheight new-height)))
    )
    
    ; calulate the blur radius
    (set! blur-rad (/ owidth new-width)) 
    
    ; blur
    (plug-in-gauss RUN-NONINTERACTIVE image drawable blur-rad blur-rad 1)
    
    ; scale
    (gimp-image-scale-full image width height INTERPOLATION-NONE)
    
    ;Finish the undo group for the process
    (gimp-image-undo-group-end image)
    (gimp-displays-flush)
  )
)

(script-fu-register "script-fu-lylejk_preblur_resize"
                    _"Preblur Resize..."
                    "Preblur before a resize"
                    "Lylejk"
                    "http://gimpchat.com/viewtopic.php?f=9&t=7258&p=92490"
                    "17/05/2013"
                    "*"
                    SF-IMAGE "Input Image" 0
                    SF-DRAWABLE "Input Drawable" 0
                    SF-ADJUSTMENT _"Width" '(800 1 5000 1 10 0 1)
                    SF-ADJUSTMENT _"Height" '(800 1 5000 1 10 0 1)
                    SF-TOGGLE _"Auto Detect Bigger" TRUE
)

(script-fu-menu-register "script-fu-lylejk_preblur_resize" "<Image>/contributed")
