; Rescaler rel 0.01
; Created by Graechan
; You will need to install GMIC to run this Scipt
; GMIC can be downloaded from http://sourceforge.net/projects/gmic/files/ 
; Comments directed to http://gimpchat.com or http://gimpscripts.com
;
; License: GPLv3
;    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 3 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.
;
;    To view a copy of the GNU General Public License
;    visit: http://www.gnu.org/licenses/gpl.html
;
;
; ------------
;| Change Log |
; ------------ 
; Rel 0.01 - Initial Release
;
(define (script-fu-rescaler image drawable scale-parameter new-size)

    (gimp-image-undo-group-start image)
	
  (let* (
         (width (car (gimp-drawable-width drawable)))
         (height (car (gimp-drawable-height drawable)))
		 (aspect 0)
        )

	(cond 
	((= scale-parameter 0)	
		
    (while (< (car (gimp-drawable-width drawable)) new-size)
	;; Render Smooth bilateral using G'MIC.
				(plug-in-gmic 1 image drawable 1
					(string-append
						"-v - " ; To have a silent output. Remove it to display errors from the G'MIC interpreter on stderr.
						"-gcd_xbr2x"))
	(set! drawable (car (gimp-image-get-active-layer image)))					
    ) ;endwhile                             
	(set! drawable (car (gimp-image-get-active-layer image)))				
    ;;;;Scale Image to it's final size
    (set! aspect (/ new-size (car (gimp-drawable-width drawable)))) 
    (gimp-layer-scale-full drawable new-size (* (car (gimp-drawable-height drawable)) aspect) FALSE 2)
	)
    ((= scale-parameter 1)
	
	(while (< (car (gimp-drawable-height drawable)) new-size)
	;; Render Smooth bilateral using G'MIC.
				(plug-in-gmic 1 image drawable 1
					(string-append
						"-v - " ; To have a silent output. Remove it to display errors from the G'MIC interpreter on stderr.
						"-gcd_xbr2x"))
	(set! drawable (car (gimp-image-get-active-layer image)))					
    ) ;endwhile                             
	(set! drawable (car (gimp-image-get-active-layer image)))				
    ;;;;Scale Image to it's final size
    (set! aspect (/ new-size (car (gimp-drawable-height drawable)))) 
    (gimp-layer-scale-full drawable (* (car (gimp-drawable-width drawable)) aspect) new-size FALSE 2)
	)
    )
	
	(gimp-image-resize-to-layers image)

    (gimp-displays-flush)
	(gimp-image-undo-group-end image)	

 )
) 



(script-fu-register "script-fu-rescaler"        		    
  "Rescaler"
  "Instructions"
  "Graechan"
  "Graechan - http://gimpchat.com"
  "2012"
  "RGB*"
  SF-IMAGE      "image"      0
  SF-DRAWABLE   "drawable"   0
  SF-OPTION "Scaling Parameter to Use" '("Width" "Height")
  SF-ADJUSTMENT "Parameter Size (pixels)" '(750 1 1000 1 10 0 1)
)

(script-fu-menu-register "script-fu-rescaler" "<Image>/Temp/")