Sun Feb 27, 2011 10:57 pm
Sun Feb 27, 2011 11:07 pm
(define (script_fu_MultiFill img inLayer inColour inThresh inRandom inPalette inIgnoreSize inFlagSize inFlagColour inHowCheck inMakeCopy inFlatten inPatternDirectory)
;helper function to compare two lists . .i.e. colours
(define (listeql? list1 list2)
(let* ((result TRUE)
(counter (length list1)))
(if (<> (length list1) (length list2))
(set! result FALSE)
(while (> counter 0)
(set! counter (- counter 1))
(if (<> (list-ref list1 counter) (list-ref list2 counter))
(set! result FALSE)
)
)
)
result
)
)
(let*
(
(width (car (gimp-image-width img)))
(height (car (gimp-image-height img)))
(bounds 0)
(countX 0)
(selectionValue 0)
(numcolours (car (gimp-palette-get-colors inPalette)))
(palettecount 0)
(nextcolour 0)
(layercopy 0)
(temp 0)
(channel 0)
(lastY 0)
(patterns (cadr (file-glob (string-append inPatternDirectory DIR-SEPARATOR "*.pat"))))
(numpatterns (length patterns)) ; NOTE: should generate error if zero
(patterncount 0) ; index into list of patterns
)
; it begins here
(gimp-context-push)
(gimp-image-undo-group-start img)
(gimp-image-undo-disable img)
;logging
;(gimp-message-set-handler ERROR-CONSOLE)
;(gimp-message-set-handler CONSOLE)
;(gimp-message-set-handler MESSAGE-BOX)
;or start GIMP wwith "gimp --console-messages" to spawn a console box
;then use this:
;(gimp-message "foobar")
;testing for functions defined
;(if (defined? 'plug-in-shift) (gimp-message "It Exists") (gimp-message "Doesnt Exist"))
(if (= inMakeCopy TRUE)
(begin
(set! layercopy (car (gimp-layer-copy inLayer (car (gimp-drawable-has-alpha inLayer)))))
(gimp-image-add-layer img layercopy -1)
(gimp-layer-set-mode layercopy MULTIPLY-MODE)
(gimp-image-set-active-layer img inLayer)
)
)
; Change to pure B & W
(gimp-by-color-select inLayer inColour inThresh CHANNEL-OP-REPLACE FALSE FALSE 0 FALSE)
(gimp-context-set-foreground (list 255 255 255))
(gimp-edit-fill inLayer FOREGROUND-FILL)
;Save it to a channel
(set! channel (car (gimp-selection-save img)))
(gimp-selection-invert img)
(gimp-context-set-foreground (list 0 0 0))
(gimp-edit-fill inLayer FOREGROUND-FILL)
;select white
(gimp-by-color-select inLayer (list 255 255 255) 0 CHANNEL-OP-REPLACE FALSE FALSE 0 FALSE)
(while (= (car (gimp-selection-is-empty img)) FALSE)
(set! bounds (gimp-selection-bounds img))
;get start the loop looking for this colour)
(if (<> lastY (caddr bounds)) ; there is a change in Y, get the new boundary
(begin
(set! countX (cadr bounds)) ; x1
(set! lastY (caddr bounds))
)
)
(set! lastY (caddr bounds))
(set! selectionValue (car (gimp-selection-value img countX (caddr bounds))))
;find a bg area
(while (< selectionValue 255) ; while not white
(set! countX (+ countX 1))
(set! selectionValue (car (gimp-selection-value img countX (caddr bounds))))
)
;select and fill
(gimp-fuzzy-select inLayer countX (caddr bounds) 0 CHANNEL-OP-REPLACE FALSE FALSE 0 FALSE)
(set! bounds (gimp-selection-bounds img))
(if (or (and (= inHowCheck 0)
(<= (- (cadddr bounds) (cadr bounds)) inIgnoreSize)
(<= (- (list-ref bounds 4) (caddr bounds)) inIgnoreSize) ) ; X and Y size
(and (= inHowCheck 1)
(<= (cadddr (gimp-histogram inLayer HISTOGRAM-VALUE 0 255)) inIgnoreSize) )) ; area
(begin
(gimp-context-set-foreground (list 0 0 0)) ; fill with black - will be ignored in flatten step
(gimp-edit-fill inLayer FOREGROUND-FILL) )
(if (or (and (= inHowCheck 0)
(> (- (cadddr bounds) (cadr bounds)) inFlagSize)
(> (- (list-ref bounds 4) (caddr bounds)) inFlagSize) ) ; X and Y size
(and (= inHowCheck 1)
(> (cadddr (gimp-histogram inLayer HISTOGRAM-VALUE 0 255)) inFlagSize) )) ; area
(cond
((= fill-type 0) ; Random color fill
(set! nextcolour inFlagColour) ; guarantees 'while' will loop at least once
(while (equal? nextcolour inFlagColour) ; random color must not be the flag color
(set! nextcolour (list (+ (rand 254) 1) ; not black or white either
(+ (rand 254) 1) ; hence color is ([1-254],[1-254],[1-254])
(+ (rand 254) 1) )))
(gimp-context-set-foreground nextcolour)
(gimp-edit-fill inLayer FOREGROUND-FILL)
)
((= fill-type 1) ; get color from palette
(set! nextcolour (car (gimp-palette-entry-get-color inPalette palettecount))) ; next palette colour, - not B, W or flagcoulor
(while (or (= (apply + nextcolour) 765)
(= (apply + nextcolour) 0)
(equal? nextcolour inFlagColour) )
(set! palettecount (modulo (+ palettecount 1) numcolours)) ; increment palette
(set! nextcolour (car (gimp-palette-entry-get-color inPalette palettecount))) )
(set! palettecount (modulo (+ palettecount 1) numcolours))
(gimp-context-set-foreground nextcolour)
(gimp-edit-fill inLayer FOREGROUND-FILL) )
((= fill-type 2) ; fill with a pattern from specified directory
(gimp-context-set-pattern (list-ref patterns patterncount))
(gimp-edit-fill inLayer PATTERN-FILL)
(set! patterncount (modulo (+ patterncount 1) numpatterns)) ))
(gimp-context-set-foreground inFlagColour) )))
(gimp-selection-none img)
(if (= inFlatten TRUE)
(begin
(gimp-by-color-select inLayer (list 0 0 0) 0 CHANNEL-OP-REPLACE FALSE TRUE 0.5 FALSE)
(while (= (car (gimp-selection-is-empty img)) FALSE)
(plug-in-dilate RUN-NONINTERACTIVE img inLayer 1 HISTOGRAM-VALUE 1.0 7 0 255)
(gimp-by-color-select inLayer (list 0 0 0) 0 CHANNEL-OP-REPLACE FALSE TRUE 0.5 FALSE)
)
)
)
(gimp-image-remove-channel img channel)
;done
(gimp-image-undo-enable img)
(gimp-image-undo-group-end img)
(gimp-progress-end)
(gimp-displays-flush)
(gimp-context-pop)
)
)
(script-fu-register "script_fu_MultiFill"
"<Image>/Filters/Flatting/MultiFill..."
"Fills every separate area with a different colour from the palette."
"Rob Antonishen"
"Rob Antonishen"
"Dec 2008"
"RGB* GRAY*"
SF-IMAGE "image" 0
SF-DRAWABLE "drawable" 0
SF-COLOR "MultiFill Colour" "white"
SF-ADJUSTMENT "Colour Threshold" (list 16 0 255 1 10 0 SF-SLIDER)
SF-OPTION "Fill mode" '("Random colours" "Colours from palette" "Patterns from directory")
SF-PALETTE "Choose Fill Palette" "Default"
SF-DIRNAME "Pattern directory" "*.pat"
SF-ADJUSTMENT "Ignore Areas <=" (list 3 0 255 1 10 1 SF-SLIDER)
SF-ADJUSTMENT "Flag Areas <=" (list 10 0 255 1 10 1 SF-SLIDER)
SF-COLOR "Flag Colour" "magenta"
SF-OPTION "Area Size Check Method" (list "Width & Height" "Area")
SF-TOGGLE "Copy Layer First" TRUE
SF-TOGGLE "Flatten After" TRUE
)
Sun Feb 27, 2011 11:48 pm
Sun Feb 27, 2011 11:59 pm
Mon Feb 28, 2011 2:09 am
Mon Feb 28, 2011 4:50 am
(let*
(
(width (car (gimp-image-width img)))
(height (car (gimp-image-height img)))
(bounds 0)
(countX 0)
(selectionValue 0)
(numcolours (car (gimp-palette-get-colors inPalette)))
(fill-type 0) ; not sure but I think it needed to be here.
(palettecount 0)
(nextcolour 0)
(layercopy 0)
(temp 0)
(channel 0)
(lastY 0)
(patterns (cadr (file-glob (string-append inPatternDirectory DIR-SEPARATOR "*.pat")1)))
(numpatterns (length patterns)) ; NOTE: should generate error if zero
(patterncount 0) ; index into list of patterns
)
Mon Feb 28, 2011 5:06 am
mahvin wrote:I know I am on the right track, file-glob needed a value, I just don't know why it needed a value and what that value should be. I added 1 for a value because I saw that value being used a lot on other scripts using file-glob.
mahvin wrote:"fill-type" wasn't in the let* statement, so I added it there and the script finally ran, but as promised, you (Saulgoode) said it wouldn't work, and now I agree, it doesn't work right. This is actually fun, I miss the excitement.
SF-OPTION "Label" (list "arg set to 0" "arg set to 1" "arg set to 2")
Mon Feb 28, 2011 5:35 am
Mon Feb 28, 2011 8:38 am
Mon Feb 28, 2011 2:29 pm
Mon Feb 28, 2011 3:12 pm
Mon Feb 28, 2011 3:34 pm
Mon Feb 28, 2011 3:36 pm
Mon Feb 28, 2011 3:40 pm
;flattingtools.scm
; by Rob Antonishen
; http://silent9.com
; Version 1.4 (20090213)
; Changes:
; V1.3 - Change the search logic to speed it up in some cases.
; - Converted to channel opes as they are faster
; V1.4 - Added option to randomly fill from the palette, or gradient fill as well.
; Description
; will fill each separate area with a different coulour, either randomly, from a selected palette, or with a directional gradient.
; License:
;
; 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 2 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.
;
; The GNU Public License is available at
; http://www.gnu.org/copyleft/gpl.html
(define (script_fu_Flatting img inLayer)
(let*
(
(width (car (gimp-image-width img)))
(height (car (gimp-image-height img)))
)
; it begins here
(gimp-context-push)
(gimp-image-undo-group-start img)
;logging
;(gimp-message-set-handler ERROR-CONSOLE)
;(gimp-message-set-handler CONSOLE)
;(gimp-message-set-handler MESSAGE-BOX)
;or start GIMP wwith "gimp --console-messages" to spawn a console box
;then use this:
;(gimp-message "foobar")
;testing for functions defined
;(if (defined? 'plug-in-shift) (gimp-message "It Exists") (gimp-message "Doesnt Exist"))
(gimp-by-color-select inLayer (list 0 0 0) 0 CHANNEL-OP-REPLACE FALSE TRUE 0.5 FALSE)
(while (= (car (gimp-selection-is-empty img)) FALSE)
(plug-in-dilate RUN-NONINTERACTIVE img inLayer 1 HISTOGRAM-VALUE 1.0 7 0 255)
(gimp-by-color-select inLayer (list 0 0 0) 0 CHANNEL-OP-REPLACE FALSE TRUE 0.5 FALSE)
)
;done
(gimp-image-undo-group-end img)
(gimp-progress-end)
(gimp-displays-flush)
(gimp-context-pop)
)
)
(script-fu-register "script_fu_Flatting"
"<Image>/Filters/Flatting/Flatten"
"Flattens an image that has been multicoloured"
"Rob Antonishen"
"Rob Antonishen"
"Jan 2009"
"RGB* GRAY*"
SF-IMAGE "image" 0
SF-DRAWABLE "drawable" 0
)
(define (script_fu_MultiFill img inLayer inColour inThresh inMethod inPalette inDirection inIgnoreSize inFlagSize inFlagColour inHowCheck inMakeCopy inFlatten inHide)
;helper function to compare two lists . .i.e. colours
(define (listeql? list1 list2)
(let* ((result TRUE)
(counter (length list1)))
(if (<> (length list1) (length list2))
(set! result FALSE)
(while (> counter 0)
(set! counter (- counter 1))
(if (<> (list-ref list1 counter) (list-ref list2 counter))
(set! result FALSE)
)
)
)
result
)
)
; TinyScheme lacks pi.
(define pi (acos -1))
; TinyScheme lacks a floating-point random number generator.
(define (urandom) (/ (rand 32768) 32768))
(let*
(
(img (car (gimp-image-duplicate img)))
(inLayer (car (gimp-image-get-active-layer img)))
(width (car (gimp-image-width img)))
(height (car (gimp-image-height img)))
(bounds 0)
(countX 0)
(selectionValue 0)
(numcolours (car (gimp-palette-get-colors inPalette)))
(palettecount 0)
(nextcolour 0)
(layercopy 0)
(temp 0)
(channel 0)
(lastY 0)
)
; it begins here
(gimp-context-push)
(gimp-image-undo-group-start img)
(gimp-image-undo-disable img)
;logging
;(gimp-message-set-handler ERROR-CONSOLE)
;(gimp-message-set-handler CONSOLE)
;(gimp-message-set-handler MESSAGE-BOX)
;or start GIMP wwith "gimp --console-messages" to spawn a console box
;then use this:
;(gimp-message "foobar")
;testing for functions defined
;(if (defined? 'plug-in-shift) (gimp-message "It Exists") (gimp-message "Doesnt Exist"))
(gimp-context-set-gradient (list-ref (cadr (gimp-gradients-get-list "")) 2)) ; FG to BG, RGB mapping
(if (= inHide FALSE) (gimp-display-new img))
(if (= inMakeCopy TRUE)
(begin
(set! layercopy (car (gimp-layer-copy inLayer (car (gimp-drawable-has-alpha inLayer)))))
(gimp-image-add-layer img layercopy -1)
(gimp-layer-set-mode layercopy MULTIPLY-MODE)
(gimp-image-set-active-layer img inLayer)
)
)
; Change to pure B & W
(gimp-by-color-select inLayer inColour inThresh CHANNEL-OP-REPLACE FALSE FALSE 0 FALSE)
(gimp-context-set-foreground (list 255 255 255))
(gimp-edit-fill inLayer FOREGROUND-FILL)
;Save it to a channel
(set! channel (car (gimp-selection-save img)))
(gimp-selection-invert img)
(gimp-context-set-foreground (list 0 0 0))
(gimp-edit-fill inLayer FOREGROUND-FILL)
(gimp-context-set-background (list 254 254 254))
;select white
(gimp-by-color-select inLayer (list 255 255 255) 0 CHANNEL-OP-REPLACE FALSE FALSE 0 FALSE)
(while (= (car (gimp-selection-is-empty img)) FALSE)
(set! bounds (gimp-selection-bounds img))
;get start the loop looking for this colour)
(if (<> lastY (caddr bounds)) ; there is a change in Y, get the new boundary
(begin
(set! countX (cadr bounds)) ; x1
(set! lastY (caddr bounds))
)
)
(set! lastY (caddr bounds))
(set! selectionValue (car (gimp-selection-value img countX (caddr bounds))))
;find a bg area
(while (< selectionValue 255) ; while not white
(set! countX (+ countX 1))
(set! selectionValue (car (gimp-selection-value img countX (caddr bounds))))
)
;select and fill
(gimp-fuzzy-select inLayer countX (caddr bounds) 0 CHANNEL-OP-REPLACE FALSE FALSE 0 FALSE)
(set! bounds (gimp-selection-bounds img))
(if (or (and (= inHowCheck 0) (<= (- (cadddr bounds) (cadr bounds)) inIgnoreSize) (<= (- (list-ref bounds 4) (caddr bounds)) inIgnoreSize)) ; X and Y size
(and (= inHowCheck 1) (<= (cadddr (gimp-histogram inLayer HISTOGRAM-VALUE 0 255)) inIgnoreSize))) ; area
(gimp-context-set-foreground (list 0 0 0)) ; fill with black - will be ignored in flatten step
(if (or (and (= inHowCheck 0) (> (- (cadddr bounds) (cadr bounds)) inFlagSize) (> (- (list-ref bounds 4) (caddr bounds)) inFlagSize)) ; X and Y size
(and (= inHowCheck 1) (> (cadddr (gimp-histogram inLayer HISTOGRAM-VALUE 0 255)) inFlagSize))) ; area
(begin
(cond ((= inMethod 0)
(set! nextcolour (list (+ (rand 254) 1) (+ (rand 254) 1) (+ (rand 254) 1))) ;Random colour - not B, W or flagcoulor
(while (= (listeql? nextcolour inFlagColour) TRUE)
(set! nextcolour (list (+ (rand 254) 1) (+ (rand 254) 1) (+ (rand 254) 1)))
)
)
((= inMethod 1)
(set! nextcolour (+ (rand 254) 1)) ;Random colour - not B, W or flagcoulor
(set! nextcolour (list nextcolour nextcolour nextcolour))
(while (= (listeql? nextcolour inFlagColour) TRUE)
(set! nextcolour (+ (rand 254) 1)) ;Random colour - not B, W or flagcoulor
(set! nextcolour (list nextcolour nextcolour nextcolour)) )
)
((= inMethod 2)
(set! nextcolour (car (gimp-palette-entry-get-color inPalette palettecount))) ; next palette colour, - not B, W or flagcoulor
(while (or (= (+ (car nextcolour) (cadr nextcolour) (caddr nextcolour)) 765)
(= (+ (car nextcolour) (cadr nextcolour) (caddr nextcolour)) 0)
(= (listeql? nextcolour inFlagColour) TRUE))
(set! palettecount (modulo (+ palettecount 1) numcolours)) ; increment palette
(set! nextcolour (car (gimp-palette-entry-get-color inPalette palettecount)))
)
(set! palettecount (modulo (+ palettecount 1) numcolours)) ; increment palette
)
((= inMethod 3)
(set! nextcolour (car (gimp-palette-entry-get-color inPalette (rand numcolours)))) ; random palette colour, - not B, W or flagcoulor
(while (or (< (length nextcolour) 3) ; bug
(= (listeql? nextcolour (list 0 0 0)) TRUE)
(= (listeql? nextcolour (list 255 255 255)) TRUE)
(= (listeql? nextcolour inFlagColour) TRUE))
(set! nextcolour (car (gimp-palette-entry-get-color inPalette (rand numcolours)))) ; random palette colour, - not B, W or flagcoulor
)
)
((or (= inMethod 4)(= inMethod 5))
(set! nextcolour (list 1 1 1))
)
)
(gimp-context-set-foreground nextcolour)
)
(gimp-context-set-foreground inFlagColour)
)
)
(if (< inMethod 4)
(gimp-edit-fill inLayer FOREGROUND-FILL)
(let*
((bounds (cdr (gimp-selection-bounds img)))
(xc (/ (+ (caddr bounds) (car bounds)) 2))
(yc (/ (+ (cadddr bounds) (cadr bounds)) 2))
(radius (sqrt (+ (expt (- (caddr bounds) (car bounds)) 2) (expt (- (cadddr bounds) (cadr bounds)) 2) )))
(direction (* 2 pi (if (= inMethod 4) (urandom) (/ inDirection 360.0))))
(x1 (round (+ xc (* radius (cos direction)))))
(y1 (round (- yc (* radius (sin direction)))))
(x2 (- xc (- x1 xc)))
(y2 (- yc (- y1 yc))))
(gimp-edit-blend inLayer FG-BG-RGB-MODE NORMAL-MODE GRADIENT-LINEAR 100 0 REPEAT-NONE FALSE TRUE 3 3 TRUE x1 y1 x2 y2)
)
)
(if (= inHide FALSE) (gimp-displays-flush))
(gimp-channel-combine-masks channel (car (gimp-image-get-selection img)) CHANNEL-OP-SUBTRACT 0 0)
(gimp-selection-load channel)
)
(gimp-selection-none img)
(if (= inFlatten TRUE)
(begin
(gimp-by-color-select inLayer (list 0 0 0) 0 CHANNEL-OP-REPLACE FALSE TRUE 0.5 FALSE)
(while (= (car (gimp-selection-is-empty img)) FALSE)
(plug-in-dilate RUN-NONINTERACTIVE img inLayer 1 HISTOGRAM-VALUE 1.0 7 0 255)
(gimp-by-color-select inLayer (list 0 0 0) 0 CHANNEL-OP-REPLACE FALSE TRUE 0.5 FALSE)
)
)
)
(gimp-image-remove-channel img channel)
(if (= inHide TRUE) (gimp-display-new img))
;done
(gimp-image-undo-enable img)
(gimp-image-undo-group-end img)
(gimp-progress-end)
(gimp-displays-flush)
(gimp-context-pop)
)
)
(script-fu-register "script_fu_MultiFill"
"<Image>/Filters/Flatting/MultiFill..."
"Fills every separate area with a different colour from the palette."
"Rob Antonishen"
"Rob Antonishen"
"Dec 2008"
"RGB* GRAY*"
SF-IMAGE "image" 0
SF-DRAWABLE "drawable" 0
SF-COLOR "MultiFill Colour" "white"
SF-ADJUSTMENT "Colour Threshold" (list 16 0 255 1 10 0 SF-SLIDER)
SF-OPTION "Fill Type" (list "Random Colours" "Random Greyscale" "Palette" "Palette Random" "Gradient with Random Direction" "Gradient with Specified Direction")
SF-PALETTE "Choose Fill Palette" "Default"
SF-ADJUSTMENT "Gradient Direction" (list 135 0 360 1 15 1 SF-SLIDER)
SF-ADJUSTMENT "Ignore Areas <=" (list 3 0 255 1 10 1 SF-SLIDER)
SF-ADJUSTMENT "Flag Areas <=" (list 10 0 255 1 10 1 SF-SLIDER)
SF-COLOR "Flag Colour" "magenta"
SF-OPTION "Area Size Check Method" (list "Width & Height" "Area")
SF-TOGGLE "Copy Layer First" TRUE
SF-TOGGLE "Flatten After" TRUE
SF-TOGGLE "Hide While Processing" TRUE
)
Mon Feb 28, 2011 3:49 pm
Mon Feb 28, 2011 3:54 pm
Rod wrote:I actually have the same problem with Robs original script.
I downloaded it, created a wall with a bunch of bricks, selected all the bricks with fuzzy select - ran the script and it looped endlessly forever on one brick.Then the script created one black layer, and kept looping forever after that.
I do not think its your pattern addition to the script that causes it, i believe its always been there in the original.
Mon Feb 28, 2011 3:58 pm
Mon Feb 28, 2011 5:25 pm
Mon Feb 28, 2011 5:26 pm
Mon Feb 28, 2011 5:36 pm