; script:MapFold.scm
; author: Tin Tran
; date: April 15, 2016
; Map Fold effect
; creates a mapfold effect out of exist image/layer like shown here: http://gimpchat.com/viewtopic.php?f=23&t=4439
; 
(define (script-fu-map-fold-sub image layer 
            Y-Shift-P
			width
			height
         )
	
		(let* 
		   ((Y-Shift 0)
		   (X-Width 0)
		   
		   (sheared-item 0)
		   (transformed-item 0)
		   (copied-layer 0)
		   )
			;(gimp-image-undo-disable image); DN = NO UNDO
			(set! Y-Shift (/ (* height Y-Shift-P) 100))
			(set! X-Width (sqrt (- (* (/ width 4) (/ width 4)) (* Y-Shift Y-Shift))))
			(gimp-image-undo-group-start image)                   ;undo-group in one step
			
			;resize canvas to double the image size and center layer
			
			
			;select and shear
			;2nd section
			(gimp-rect-select image (+ (/ width 2) (ceiling (/ width 4))) (/ height 2) (ceiling (/ width 4)) height CHANNEL-OP-REPLACE FALSE 0)
			(set! sheared-item (car (gimp-item-transform-shear layer ORIENTATION-VERTICAL Y-Shift)))
			(gimp-item-transform-scale sheared-item (- width X-Width) (- (/ height 2) (/ Y-Shift 2))
			                                        width             (+ (* (/ height 2) 3) (/ Y-Shift 2))
			)
			
			;3rd section
			(gimp-rect-select image (+ (/ width 2) (* (ceiling (/ width 4)) 2)) (/ height 2) (ceiling (/ width 4)) height CHANNEL-OP-REPLACE FALSE 0)
			(set! sheared-item (car (gimp-item-transform-shear layer ORIENTATION-VERTICAL (- 0 Y-Shift))))
			(gimp-item-transform-scale sheared-item width (- (/ height 2) (/ Y-Shift 2))
			                                        (+ width X-Width) (+ (* (/ height 2) 3) (/ Y-Shift 2))
			)
			;1st section
			(gimp-rect-select image (/ width 2) (/ height 2) (ceiling (/ width 4)) height CHANNEL-OP-REPLACE FALSE 0)
			(set! sheared-item (car (gimp-item-transform-shear layer ORIENTATION-VERTICAL (- 0 Y-Shift))))
			(gimp-item-transform-scale sheared-item (- (- width X-Width) X-Width) (- (/ height 2) (/ Y-Shift 2))
			                                         (- width X-Width) (+ (* (/ height 2) 3) (/ Y-Shift 2))
			)
			;4th section
			(gimp-rect-select image (+ (/ width 2) (* (ceiling (/ width 4)) 3)) (/ height 2) (ceiling (/ width 4)) height CHANNEL-OP-REPLACE FALSE 0)
			(set! sheared-item (car (gimp-item-transform-shear layer ORIENTATION-VERTICAL Y-Shift)))
			(set! sheared-item (car (gimp-item-transform-scale sheared-item (+ width X-Width) (- (/ height 2) (/ Y-Shift 2))
			                                         (+ (+ width X-Width) X-Width) (+ (* (/ height 2) 3) (/ Y-Shift 2))
			)))
			
			;anchor the last sheared item to flaten image
			(gimp-floating-sel-anchor sheared-item)
			
			;select the even sections (section 2 and 4) to darken a bit
			(gimp-rect-select image (+ (/ width 2) (ceiling (/ width 4))) 0 (ceiling (/ width 4)) (* height 2) CHANNEL-OP-REPLACE FALSE 0)
			(gimp-rect-select image (+ (/ width 2) (* (ceiling (/ width 4)) 3)) 0 (ceiling (/ width 4)) (* height 2) CHANNEL-OP-ADD FALSE 0)
		
		    ;darken selected area a bit
			(gimp-brightness-contrast layer -50 0)
			
			;apply perspective
			(gimp-selection-all image)
			(set! transformed-item (car (gimp-item-transform-perspective layer (/ width 4) 0 (- (* width 2) (/ width 4)) 0
			                                        0 (* height 2) (* width 2) (* height 2))))
			(gimp-floating-sel-anchor transformed-item)									
												
		    ;copy the layer 
			(set! copied-layer (car (gimp-layer-new-from-drawable layer image)))
            (gimp-image-insert-layer image copied-layer 0 1)
			
			;set it to black
			(gimp-image-select-item image CHANNEL-OP-REPLACE copied-layer)
			(gimp-context-set-background '(0 0 0))
			(gimp-edit-fill copied-layer BACKGROUND-FILL)
			
			;apply perspective
			(gimp-selection-all image)
			(set! transformed-item (car (gimp-item-transform-perspective copied-layer 0 (/ height 2) (* width 2) (/ height 2)
			                                        0 (* height 2) (* width 2) (* height 2))))
			(gimp-floating-sel-anchor transformed-item)
			
			;blur this layer to make it look like shadow
			(gimp-selection-all image)
			(plug-in-gauss RUN-NONINTERACTIVE image copied-layer (/ width 20) (/ width 20) 1)
			
			;set opacity 50%
			(gimp-layer-set-opacity copied-layer 50)
			
			
		   ;(gimp-image-undo-enable image) ;DN = NO UNDO
			(gimp-image-undo-group-end image)                     ;undo group in one step
			(gimp-displays-flush)
	    )
	
	
    
) ;end of define
(define (script-fu-map-fold-animate image layer 			
         )
	
		(let* 
		   (
		   (copied-layer 0)
		   (width (car (gimp-image-width image)))
		   (height (car (gimp-image-height image)))
		   )
			;(gimp-image-undo-disable image); DN = NO UNDO
			
			(gimp-image-undo-group-start image)                   ;undo-group in one step
			(gimp-image-resize image (* width 2) (* height 2) (/ width 2) (/ height 2))
			(gimp-layer-resize-to-image-size layer)
			
			
			
			(set! copied-layer (car (gimp-layer-new-from-drawable layer image)))
            (gimp-image-insert-layer image copied-layer 0 0)
			(gimp-image-set-active-layer image copied-layer)
			(script-fu-map-fold-sub image copied-layer 5 width height)
			(gimp-image-merge-down image copied-layer EXPAND-AS-NECESSARY)
			
			(set! copied-layer (car (gimp-layer-new-from-drawable layer image)))
            (gimp-image-insert-layer image copied-layer 0 0)
			(gimp-image-set-active-layer image copied-layer)
			(script-fu-map-fold-sub image copied-layer 10 width height)
			(gimp-image-merge-down image copied-layer EXPAND-AS-NECESSARY)
			
			(set! copied-layer (car (gimp-layer-new-from-drawable layer image)))
            (gimp-image-insert-layer image copied-layer 0 0)
			(gimp-image-set-active-layer image copied-layer)
			(script-fu-map-fold-sub image copied-layer 15 width height)
			(gimp-image-merge-down image copied-layer EXPAND-AS-NECESSARY)
			
			(set! copied-layer (car (gimp-layer-new-from-drawable layer image)))
            (gimp-image-insert-layer image copied-layer 0 0)
			(gimp-image-set-active-layer image copied-layer)
			(script-fu-map-fold-sub image copied-layer 20 width height)
			(gimp-image-merge-down image copied-layer EXPAND-AS-NECESSARY)
			
			(set! copied-layer (car (gimp-layer-new-from-drawable layer image)))
            (gimp-image-insert-layer image copied-layer 0 0)
			(gimp-image-set-active-layer image copied-layer)
			(script-fu-map-fold-sub image copied-layer 25 width height)
			(gimp-image-merge-down image copied-layer EXPAND-AS-NECESSARY)
			
			(set! copied-layer (car (gimp-layer-new-from-drawable layer image)))
            (gimp-image-insert-layer image copied-layer 0 0)
			(gimp-image-set-active-layer image copied-layer)
			(script-fu-map-fold-sub image copied-layer 30 width height)
			(gimp-image-merge-down image copied-layer EXPAND-AS-NECESSARY)
			
			
		   ;(gimp-image-undo-enable image) ;DN = NO UNDO
			(gimp-image-undo-group-end image)                     ;undo group in one step
			(gimp-displays-flush)
	    )
	
	
    
) ;end of define
(script-fu-register
  "script-fu-map-fold-animate"         ;function name
  "<Image>/Script-Fu/Create New/Map Fold Effect Animate"    ;menu register
  "Creates Map Fold effect"       ;description
  "Tin Tran"                          ;author name
  "copyright info and description"         ;copyright info or description
  "2016"                          ;date
  "RGB*, GRAY*"                        ;mode
  SF-IMAGE      "Image" 0                   
  SF-DRAWABLE   "Layer" 0
)