; Puzzler rel 0.02
; Created by Graechan
;
; 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
; Rel 0.02 - Added the Logo section
 (define (script-fu-puzzler-logo
                                      text
									  letter-spacing
									  line-spacing
                                      color 
                                      font-in 
                                      font-size
                                      suround-type 
                                      suround-pattern
                                      suround-color
									  suround-gradient
									  style
									  anim
							          bev-width
							          bkg-type
							          bkg-color)
									  
  (let* (
         (image (car (gimp-image-new 256 256 RGB)))         
         (border (/ font-size 4))
		 (font (if (> (string-length font-in) 0) font-in (car (gimp-context-get-font))))
         (size-layer (car (gimp-text-fontname image -1 0 0 text border TRUE font-size PIXELS font)))
         (final-width (car (gimp-drawable-width size-layer)))
         (final-height (car (gimp-drawable-height size-layer)))
         (text-layer 0)
         (width 0)
         (height 0)
         (suround-layer 0)
		 (x 0)
		 (y 0)         
         )
		 
    (gimp-context-push)
	(gimp-context-set-foreground '(0 0 0))
	(gimp-context-set-background '(255 255 255))

;;;;Add the text layer for a temporary larger Image size
    (set! text-layer (car (gimp-text-fontname image -1 0 0 text (round (/ 200 4)) TRUE 200 PIXELS font)))
;;;;adjust text 
	(gimp-text-layer-set-justification text-layer 2)
	(gimp-text-layer-set-letter-spacing text-layer letter-spacing)
	(gimp-text-layer-set-line-spacing text-layer line-spacing)
;;;;set the new width and height	
    (set! width (car (gimp-drawable-width text-layer)))
    (set! height (car (gimp-drawable-height text-layer)))    
    (gimp-image-remove-layer image size-layer)
    (gimp-image-resize-to-layers image)

;;;;set the text clolor    
    (gimp-context-set-foreground color)
	(gimp-selection-layer-alpha text-layer)
	(gimp-edit-fill text-layer FOREGROUND-FILL)
	(gimp-selection-none image)
    
;;;;create the Suround layer    
	(set! suround-layer (car (gimp-layer-new image width height RGBA-IMAGE "Background" 100 NORMAL-MODE)))
    (gimp-image-add-layer image suround-layer 1)
	(gimp-context-set-pattern suround-pattern)
	(gimp-context-set-background suround-color)
	(gimp-context-set-gradient suround-gradient)
	(if (= suround-type 1) (gimp-drawable-fill suround-layer PATTERN-FILL))		
    (if (= suround-type 2) (gimp-drawable-fill suround-layer BACKGROUND-FILL))	
    (if (= suround-type 3) 
	(begin
	(gimp-selection-none image)
	(gimp-drawable-fill suround-layer BACKGROUND-FILL)	
	(gimp-edit-blend suround-layer CUSTOM-MODE NORMAL-MODE GRADIENT-SHAPEBURST-SPHERICAL 100 0 REPEAT-NONE FALSE FALSE 3 0.2 TRUE 0 0 width height)))
	
	(set! text-layer (car (gimp-image-merge-down image text-layer EXPAND-AS-NECESSARY)))
	(gimp-drawable-set-name text-layer text)
    
	(set! x (round (/ width 150)))
	(set! y (round (/ height 150)))
	(if (< x 2) (set! x 2))
	(if (< y 2) (set! y 2))
	
	(script-fu-puzzler image text-layer
                               style
							   anim
							   x
							   y
							   bev-width
							   bkg-type
							   bkg-color
							   FALSE)							   
							   
	(set! text-layer (car (gimp-image-get-active-layer image)))	

;;;;Scale Image to it's original size;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    (gimp-image-scale-full image final-width final-height 2)
	(set! width (car (gimp-image-width image)))
	(set! height (car (gimp-image-height image)))
              
;;;;resize the text-layer		
    (gimp-image-set-active-layer image text-layer)
	(gimp-layer-resize-to-image-size text-layer)    

	(gimp-context-pop)

    (gimp-display-new image)
	
    )
  )
  
(script-fu-register "script-fu-puzzler-logo"
  "Puzzler"
  "Create a Logo in the form of a Puzzle"
  "Graechan"
  "Graechan - http://gimpchat.com"
  "May 2013"
  ""
  SF-TEXT       "Text"    "Puzzler\nLogo"
  SF-ADJUSTMENT "Letter Spacing" '(0 -100 100 1 5 0 0)
  SF-ADJUSTMENT "Line Spacing" '(0 -100 100 1 5 0 0)
  SF-COLOR      "Text color"         '(0 0 0)
  SF-FONT       "Font"               "JasmineUPC Bold"
  SF-ADJUSTMENT "Font size (pixels)" '(200 6 500 1 1 0 1)
  SF-OPTION "Suround Type" '("White" "Pattern" "Color" "Gradient")
  SF-PATTERN    "Suround Pattern"            "Pink Marble"
  SF-COLOR      "Suround color"         '(153 153 153)
  SF-GRADIENT   "Suround Gradient" "Abstract 3"
  SF-OPTION "Shape" '("Square" "Curved")
  SF-TOGGLE     "Animate the Puzzle"   FALSE
  SF-ADJUSTMENT "Bevel Width" '(10 1 100 1 10 0 0)
  SF-OPTION "Background" '("Transparent" "Color" "The Image")
  SF-COLOR      "Background color"         "Gray"
  )
(script-fu-menu-register "script-fu-puzzler-logo" "<Image>/Script-Fu/Logos")
  
(define (script-fu-puzzler image drawable
                               style
							   anim
							   x-in
							   y-in
							   bev-width
							   bkg-type
							   bkg-color
							   conserve
							   )
	
    (gimp-image-undo-group-start image)	

 (let* (
            (width (car (gimp-drawable-width drawable)))
			(height (car (gimp-drawable-height drawable)))
			(alpha (car (gimp-drawable-has-alpha drawable)))
		    (layer-name (car (gimp-drawable-get-name drawable)))
		    (selection 0)
			(white-layer 0)
			(blend-lines 0)
			(blend-amount 0)
			(x x-in)
			(y y-in)
			(tilex (/ width x))
			(tiley (/ height y))
			(tileoffx (/ tilex 2))
			(tileoffy (/ tiley 2))
			(cnt (* x y))
			(layer 0)
        )
	
	
	(gimp-context-push)
    (gimp-context-set-default-colors)

	(if (= alpha FALSE) (gimp-layer-add-alpha drawable))	
		
;;;;begin the script;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
	(gimp-selection-none image)
	(set! white-layer (car (gimp-layer-new image width height RGBA-IMAGE "white-layer" 100 NORMAL-MODE)))
    (gimp-image-add-layer image white-layer (+ (car (gimp-image-get-layer-position image drawable)) 1))
	(gimp-drawable-fill white-layer BACKGROUND-FILL)
	(plug-in-jigsaw 1 image white-layer x y style blend-lines blend-amount)
	(gimp-image-select-color image 2 white-layer "White")
	(set! selection (car (gimp-selection-save image)))	
	
	(while (> cnt 0)(gimp-drawable-fill white-layer BACKGROUND-FILL)
	(gimp-image-select-contiguous-color image 2 selection tileoffx tileoffy)
    (gimp-image-set-active-layer image drawable)
	(gimp-edit-copy-visible image)	
	(set! layer (car (gimp-layer-new image width height RGBA-IMAGE "layer" 100 NORMAL-MODE)))
	(gimp-image-add-layer image layer 0) 
	(set! layer (car (gimp-edit-paste layer FALSE)))
	(gimp-floating-sel-anchor layer)
	(gimp-layer-resize-to-image-size (car (gimp-image-get-active-layer image)))
	(puzzler-beveller image (car (gimp-image-get-active-layer image)) 18 0 0 bev-width 0 0 135 30 3 3 0 FALSE)
	(if (= cnt 1) (begin
	(gimp-drawable-set-name (car (gimp-image-get-active-layer image)) (string-append (car (gimp-drawable-get-name (car (gimp-image-get-active-layer image)))) "(5000ms)")))
	(gimp-drawable-set-name (car (gimp-image-get-active-layer image)) (string-append (car (gimp-drawable-get-name (car (gimp-image-get-active-layer image)))) "(300ms)")))
	(gimp-selection-load selection)
	(set! cnt (- cnt 1))
	(set! tileoffx (+ tileoffx tilex))
	(if (> tileoffx width) (begin
	(set! tileoffx (/ tilex 2))
	(set! tileoffy (+ tileoffy tiley))))
	(if (and (= anim FALSE) (< cnt (- (* x y) 1))) (set! layer (car (gimp-image-merge-down image (car (gimp-image-get-active-layer image)) EXPAND-AS-NECESSARY))))
	(gimp-displays-flush))
	
	(set! layer (car (gimp-image-get-active-layer image)))
	
	(if (= bkg-type 0) (begin
	(gimp-image-remove-layer image white-layer)
	(gimp-drawable-fill drawable TRANSPARENT-FILL)))
	(if (= bkg-type 1) (begin
	(set! drawable (car (gimp-image-merge-down image drawable EXPAND-AS-NECESSARY)))
	(gimp-context-set-background bkg-color)
	(gimp-drawable-fill drawable BACKGROUND-FILL)))
	(if (= bkg-type 2) (gimp-image-remove-layer image white-layer))
	(gimp-drawable-set-name drawable "Background")
	(gimp-selection-none image)
	
	(gimp-drawable-set-name (car (gimp-image-get-active-layer image)) (string-append (car (gimp-drawable-get-name (car (gimp-image-get-active-layer image)))) "(1000ms)"))
	(gimp-image-remove-channel image selection)
    
	(gimp-image-set-active-layer image layer)
	(if (and (= conserve FALSE) (= anim FALSE)) (begin
    (set! layer (car (gimp-image-merge-down image layer EXPAND-AS-NECESSARY)))
	(gimp-drawable-set-name layer (string-append layer-name "-" "Puzzler"))))
	(if (and (= conserve TRUE) (= anim FALSE)) (gimp-drawable-set-name layer (string-append layer-name "-" "Puzzler")))
	
;;;;end of script;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
	
	(gimp-displays-flush)
	(gimp-image-undo-group-end image)
	(gimp-context-pop)
	
 )
)

(script-fu-register "script-fu-puzzler"        		    
  "Puzzler"
  "can create a puzzle animation on an Image"
  "Graechan"
  "Graechan - http://gimpchat.com"
  "Feb 2013"
  "RGB*"
  SF-IMAGE      "image"      0
  SF-DRAWABLE   "drawable"   0
  SF-OPTION "Shape" '("Square" "Curved")
  SF-TOGGLE     "Animate the Puzzle"   FALSE
  SF-ADJUSTMENT "Hor Tiles" '(4 2 20 1 1 0 1)
  SF-ADJUSTMENT "Vert Tiles" '(4 2 20 1 1 0 1)
  SF-ADJUSTMENT "Bevel Width" '(10 1 100 1 10 0 0)
  SF-OPTION "Background" '("Transparent" "Color" "The Image")
  SF-COLOR      "Background color"         "Gray"
  SF-TOGGLE     "Keep the Layers"   FALSE
)

(script-fu-menu-register "script-fu-puzzler" "<Image>/Filters/Animation")

(define (puzzler-beveller image layer
                               layer-mode
							   bev-type
							   bev-dir
							   bev-width
							   shape
							   prenoise
							   light-angle
							   light-elevation
							   depth
							   postblur
							   gloss
							   keep-selection-in
							   )
	
	(gimp-image-undo-group-start image)

 (let* (
            (width (car (gimp-image-width image)))
			(height (car (gimp-image-height image)))
			(alpha (car (gimp-drawable-has-alpha layer)))
			(layer-name (car (gimp-drawable-get-name layer)))
			(image-channel 0)
			(bevel-layer 0)
			(name 0)
			(*newpoint* (cons-array 6 'double))
		    (*newpointx1* (cons-array 16 'double))
			(y1 0)
			(y2 0)
			(y3 0)
			(y4 0)
			(y5 0)
			(y6 0)
			(keep-selection keep-selection-in)
			(selection-bounds 0)
			(bounds-x1 0)
			(bounds-y1 0)
			(bounds-x2 0)
			(bounds-y2 0)
						
        )
	
	
	(gimp-context-push)    
	(gimp-context-set-foreground '(0 0 0))
	(gimp-context-set-background '(255 255 255))

	(if (= alpha FALSE) (gimp-layer-add-alpha layer))
	
;;;;check that a selection was made if not make one	
;	(if (= (car (gimp-selection-is-empty image)) TRUE) (set! keep-selection FALSE))
	(if (= (car (gimp-selection-is-empty image)) TRUE) (gimp-selection-layer-alpha layer))

;;;;find the selection co-ordinates	
	(set! selection-bounds (cdr (gimp-selection-bounds image)))
	(set! bounds-x1 (car selection-bounds));x coordinate of upper left corner of selection bounds
    (set! bounds-y1 (cadr selection-bounds));y coordinate of upper left corner of selection bounds
    (set! bounds-x2 (caddr selection-bounds));x coordinate of lower right corner of selection bounds
    (set! bounds-y2 (cadddr selection-bounds));y coordinate of lower right corner of selection bounds	
;;;;This means the width of the selection can be calculated as (x2 - x1), its height as (y2 - y1).
;	(if (and (= bev-type 1) (or (= (- bounds-x2 bounds-x1) width) (= (- bounds-y2 bounds-y1) height))) (gimp-image-resize image (+ width (* bev-width 2)) (+ height (* bev-width 2)) bev-width bev-width))
;	(set! width (car (gimp-image-width image)))
;	(set! height (car (gimp-image-height image)))
;	(gimp-layer-resize-to-image-size layer)
;;;;save the selection    
	(gimp-selection-save image)
	(set! image-channel (car (gimp-image-get-active-drawable image)))	
	(gimp-channel-set-opacity image-channel 100)	
	(gimp-drawable-set-name image-channel "image-channel")
	(gimp-image-set-active-layer image layer)
	
;;;;Add new layer with Bevel
    (if (= bev-type 0) (set! name "Inner bevel"))
    (if (= bev-type 1) (set! name "Outer bevel"))
    (gimp-selection-load image-channel)
    (gimp-image-set-active-layer image layer)    
    (set! bevel-layer (car (gimp-layer-new image width height RGBA-IMAGE name 100 layer-mode)))
    (gimp-image-add-layer image bevel-layer -1)
    (gimp-image-set-active-layer image bevel-layer)    
    (gimp-context-set-foreground '(0 0 0))
    (gimp-context-set-background '(255 255 255))
	(gimp-drawable-fill bevel-layer FOREGROUND-FILL)    
    (gimp-selection-grow image 3)
    (gimp-selection-feather image bev-width)
    (if (= bev-type 0) (gimp-selection-shrink image (/ bev-width 2)))
	(if (= bev-type 1) (gimp-selection-grow image (+ (/ bev-width 2) 1)))
    (gimp-edit-fill bevel-layer BACKGROUND-FILL)
;;;;apply shape curve
    (if (or (< shape 0) (> shape 0))
	(begin
					(aset *newpoint* 0 0)    ; set the arrays
					(aset *newpoint* 1 0)
					(aset *newpoint* 2 (round (+ (/ shape 10) 127)))
	                (aset *newpoint* 3 (+ shape 127))
					(aset *newpoint* 4 255)
					(aset *newpoint* 5 255)
	(gimp-curves-spline bevel-layer 0 6 *newpoint*)
    )
    )	
	

    (gimp-selection-all image)
	
;;;;invert colours of whole layer if down                
    (if (= bev-dir 1) (gimp-invert bevel-layer))
;;;;add prenoise
    (if (> prenoise 0) (plug-in-hsv-noise 1 image bevel-layer 4 0 0 prenoise))
                        						
;;;;emboss the selection    
    (plug-in-emboss RUN-NONINTERACTIVE image bevel-layer light-angle light-elevation depth 1);Emboss

;;;;gloss the bevel-layer    
     (if (> gloss 0)
				(begin
                    (set! y1 (* 1.0 gloss))
					(set! y2 (* 3.0 gloss)) 
                    (set! y3 (* 9.6 gloss))
                    (set! y4 (* 3.2 gloss))
                    (set! y5 (* 4.0 gloss))
                    (set! y6 (* 0.4 gloss))
					
					(aset *newpointx1* 0 0)    ; set the arrays
					(aset *newpointx1* 1 0)
					(aset *newpointx1* 2 63)
	                (aset *newpointx1* 3 (+ 63 y1))
					(aset *newpointx1* 4 95)
					(aset *newpointx1* 5 (+ 95 y2))
					(aset *newpointx1* 6 127)
					(aset *newpointx1* 7 (- 127 y3))
					(aset *newpointx1* 8 156)
					(aset *newpointx1* 9 (+ 156 y4))
					(aset *newpointx1* 10 191)
					(aset *newpointx1* 11 (- 191 y5))
					(aset *newpointx1* 12 223)
					(aset *newpointx1* 13 (+ 223 y6))
					(aset *newpointx1* 14 255)
					(aset *newpointx1* 15 255)
					;(0,0, 63,(63 + y1), 95,(95 + y2), 127,(127 - y3), 156,(156 + y4), 191,(191 - y5), 223,(223 + y6), 255,255)
	 
	(gimp-curves-spline bevel-layer 0 16 *newpointx1*)
	 )
	 )
	(gimp-selection-load image-channel)
    (if (= bev-type 0) (begin
	(gimp-selection-shrink image (- bev-width (round (/ bev-width 3))))
    (gimp-edit-clear bevel-layer)))
  	(if (= bev-type 1) (begin
	(gimp-selection-grow image (+ bev-width 1))
	(gimp-selection-invert image)
	(gimp-edit-clear bevel-layer)))
;postblur                
	(if (> postblur 0) (plug-in-gauss-rle2 RUN-NONINTERACTIVE image bevel-layer postblur postblur))
	
	(gimp-layer-set-lock-alpha bevel-layer FALSE)
	
;reload selection
    (gimp-selection-load image-channel)
                
;clear excess
    (if (= bev-type 0) 
	(begin
	(gimp-selection-shrink image 1)
	(gimp-selection-invert image) 
	(gimp-edit-clear bevel-layer)
	(gimp-edit-clear layer)
	(gimp-selection-invert image)))

	(if (= bev-type 1) 
	(begin
	(gimp-selection-shrink image 1)
	(gimp-edit-clear bevel-layer)))
    
 
;;;;Clean up the layers
    (if (= keep-selection FALSE) (gimp-selection-none image))
	(gimp-image-remove-channel image image-channel)
	(if (= alpha FALSE) (gimp-layer-flatten layer))
	(set! layer (car (gimp-image-merge-down image bevel-layer EXPAND-AS-NECESSARY)))
	(gimp-drawable-set-name layer layer-name)
	(gimp-image-set-active-layer image layer)

	(gimp-displays-flush)
	(gimp-image-undo-group-end image)
	(gimp-context-pop)
    
 )
) 


