;Author: Tin Tran
; trying to reproduce a 4 color + 1 (erase every other line color) (so 5 shades in total) Obama Poster effect.
(define (script-fu-erase-rows2 img drawable orientation which type)
  (let* (
        (width (car (gimp-drawable-width drawable)))
        (height (car (gimp-drawable-height drawable)))
        (position-x (car (gimp-drawable-offsets drawable)))
        (position-y (cadr (gimp-drawable-offsets drawable)))
        )

    (gimp-context-push)
    (gimp-context-set-feather FALSE)

    (gimp-image-undo-group-start img)
    (letrec ((loop (lambda (i max)
                     (if (< i max)
                         (begin
                           (if (= orientation 0)
                               (gimp-image-select-rectangle img CHANNEL-OP-REPLACE position-x (+ i position-y) width 1)
                               (gimp-image-select-rectangle img CHANNEL-OP-REPLACE (+ i position-x) position-y 1 height))
                           (if (= type 0)
                               (gimp-edit-clear drawable)
                               (gimp-edit-fill drawable BACKGROUND-FILL))
                           (loop (+ i 2) max))))))
      (loop (if (= which 0)
                0
                1)
            (if (= orientation 0)
                height
                width)
      )
    )
    (gimp-selection-none img)
    (gimp-image-undo-group-end img)
    (gimp-context-pop)
    (gimp-displays-flush)
  )
)
(define (FU2-cutout
		img
		drawable
		colors
		smoothness
		inMerge
	)
    ;(gimp-image-undo-group-start img)
	(define indexed (car (gimp-drawable-is-indexed drawable)))
	(if (= indexed TRUE)(gimp-image-convert-rgb img))
  (let* (
	 (width (car (gimp-drawable-width drawable)))
	 (height (car (gimp-drawable-height drawable)))
	 (old-selection (car (gimp-selection-save img)))
	 (image-type (car (gimp-image-base-type img)))
         (blur (* width  smoothness 0.001 ))
	 (layer-type (car (gimp-drawable-type drawable)))
	 (layer-temp1 (car (gimp-layer-new img width height layer-type "temp1"  100 NORMAL-MODE)))
	 (img2 (car (gimp-image-new width height image-type)))
	 (layer-temp2 (car (gimp-layer-new img2 width height layer-type "temp2"  100 NORMAL-MODE)))
        ) 

    (if (eqv? (car (gimp-selection-is-empty img)) TRUE)
        (gimp-drawable-fill old-selection WHITE-IMAGE-FILL)) ; so Empty and All are the same.
    (gimp-selection-none img)
    (gimp-drawable-fill layer-temp1 TRANS-IMAGE-FILL)
    (gimp-image-insert-layer img layer-temp1 0 -1)
    (gimp-edit-copy drawable)
    (gimp-floating-sel-anchor (car (gimp-edit-paste layer-temp1 0)))

    (plug-in-gauss 1 img layer-temp1 blur blur 0)
    (gimp-edit-copy layer-temp1)

    (gimp-image-insert-layer img2 layer-temp2 0 -1)
    (gimp-drawable-fill layer-temp2 TRANS-IMAGE-FILL)
    (gimp-floating-sel-anchor (car (gimp-edit-paste layer-temp2 0)))
    (gimp-image-convert-indexed img2 0 0 colors 0 0 "0")
    (gimp-edit-copy layer-temp2)
    (gimp-image-delete img2)

    (gimp-layer-add-alpha layer-temp1)
    (gimp-floating-sel-anchor (car (gimp-edit-paste layer-temp1 0)))

    (gimp-image-select-item img CHANNEL-OP-REPLACE old-selection)
    (gimp-selection-invert img)
    (if (eqv? (car (gimp-selection-is-empty img)) FALSE) ; both Empty and All are denied
        (begin
        (gimp-edit-clear layer-temp1)
        ))

    (gimp-item-set-name layer-temp1 "Cutout")
    (gimp-image-select-item img CHANNEL-OP-REPLACE old-selection)
    (gimp-image-remove-channel img old-selection)

	(if (= inMerge TRUE)(gimp-image-merge-visible-layers img EXPAND-AS-NECESSARY))
    ;(gimp-image-undo-group-end img)
    ;(gimp-displays-flush)
  )
)
(define (script-fu-obama-posterize image layer 
			color1
			color2
			color3
			color5
			color4scale
         )
	
		(let* 
		   (
		   (width (car (gimp-image-width image)))
		   (height (car (gimp-image-height image)))
		   (color4 '(255 255 255))  ;color4 is defined by using color3 and color5, we'll just keep un unused color like white here to change to shaded color3/5 later.
		   )
			;(gimp-image-undo-disable image); DN = NO UNDO
			(gimp-context-push)
			(gimp-image-undo-group-start image)                   ;undo-group in one step
			
			(gimp-selection-none image)
			(gimp-item-set-name layer "Obama poster 1")
			
			(gimp-desaturate-full layer DESATURATE-LUMINOSITY)
			(FU2-cutout image layer 5.0 8.0 TRUE)
			(set! layer (car (gimp-image-get-layer-by-name image "Obama poster 1")))
			(gimp-image-convert-indexed image NO-DITHER MAKE-PALETTE 5 FALSE FALSE "Custom pallette ignored")
			(gimp-image-set-colormap image 15 (list->vector (append color1 color2 color3 color4 color5)))
			(gimp-image-convert-rgb image)
			
			;set these so that color select will only work on one color later.
			(gimp-context-set-antialias FALSE)
			(gimp-context-set-feather FALSE)
			(gimp-context-set-sample-merged FALSE)
			(gimp-context-set-sample-threshold 0)
			(gimp-context-set-sample-transparent TRUE)
			
			;new layer and fill it with color3
			(define new-layer-color3 (car (gimp-layer-new image width height RGBA-IMAGE "color 3 fill" 100 NORMAL-MODE)))
			(gimp-image-insert-layer image new-layer-color3 0 0)
			(gimp-context-set-foreground color3)
			(gimp-edit-fill new-layer-color3 FOREGROUND-FILL)
			
			;select the color4 area and invert and cut away so we're left with color4 filled with color3
			(set! layer (car (gimp-image-get-layer-by-name image "Obama poster 1")))
			(gimp-image-select-color image CHANNEL-OP-REPLACE layer color4)
			(gimp-selection-invert image)
			(gimp-edit-cut new-layer-color3)
			
			;new layer and fill it with color3
			(gimp-selection-none image)
			(define new-layer-color5 (car (gimp-layer-new image width height RGBA-IMAGE "color 5 bars" 100 NORMAL-MODE)))
			(gimp-image-insert-layer image new-layer-color5 0 0)
			(gimp-context-set-foreground color5)
			(gimp-edit-fill new-layer-color5 FOREGROUND-FILL)
			(script-fu-erase-rows2 image new-layer-color5 0 0 0)
			
			;scale bars to scale before cutting away.
			(gimp-layer-scale new-layer-color5 (* width color4scale) (* height color4scale) TRUE)
			(gimp-layer-resize-to-image-size new-layer-color5)
			
			;select the color4 area and invert and cut away so we're left with color4 filled with color5's bars.
			(set! layer (car (gimp-image-get-layer-by-name image "Obama poster 1")))
			(gimp-image-select-color image CHANNEL-OP-REPLACE layer color4)
			(gimp-selection-invert image)
			(gimp-edit-cut new-layer-color5)
			
			(gimp-selection-none image)
			
		   ;(gimp-image-undo-enable image) ;DN = NO UNDO
			(gimp-image-undo-group-end image)                     ;undo group in one step
			(gimp-context-pop)
			(gimp-displays-flush)
	    )
	
	
    
) ;end of define
(script-fu-register
  "script-fu-obama-posterize"         ;function name
  "<Image>/Script-Fu/Artist/Obama Posterize ..."    ;menu register
  "Obama posterize an image"       ;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
  ;SF-GRADIENT   "Gradient 1"           ""
  ;SF-GRADIENT   "Gradient 2"           ""
  SF-COLOR        "Color 1 Darkest color(Dark Blue)"     '(9 0 1)
  SF-COLOR        "Color 2 2nd Darkest color(Dark Red)"     '(175 42 27)
  SF-COLOR        "Color 3 2nd Lighest color(Light Blue)"     '(101 101 101)
  SF-COLOR        "Color 5 Lightest color(Light Gold)"     '(247 228 160)
  SF-ADJUSTMENT   "Scale of color 4 bars (color 3 and 5)" '(2 0 10 1 1 0 1)
)
