; numeric-ops.scm - Perform some operations by entering arguments ; numerically. ; Version: 1.4. ; GIMP Script-Fu by Pedro Gimeno Fortea. ; Donated to the public domain. ; ; Changes: ; 1.0 (2004-04-21): First revision. ; 1.1 (2004-06-14): Added rect and ellipse selection. Merged ; horizontal/vertical guides in one single function. ; 1.2 (2004-06-15): Added two basic Bezier shapes (Rectangle and Ellipse). ; 1.3 (2005-04-08): Use local functions to map script operation codes to ; GIMP operations. New Bezier shape: Diamond. Added ; Displace Layer. Changed function name of ; numeric-layer-offset to numeric-layer-position and ; numeric-selection-offset to numeric-selection-displace ; for consistency. Added reference point to ; numeric-layer-position. Improved descriptions. ; 1.4 (2009-03-18): Fix problem with "car" in gimp-2.4 ; ; Define the global functions. This step is not required by SIOD but is ; quite recommended for generic Scheme compatibility. ; (define script-fu-numeric-guide 0) (define script-fu-numeric-layer-position 0) (define script-fu-numeric-layer-displace 0) (define script-fu-numeric-selection-displace 0) (define script-fu-numeric-rect-select 0) (define script-fu-numeric-ellipse-select 0) (define script-fu-numeric-shape 0) ; Enter local scope. ; (let () ; Local variable: Holds the option-to-operation mapping. ; (define oplist (list (list _"Replace current selection" CHANNEL-OP-REPLACE) (list _"Add to current selection" CHANNEL-OP-ADD) (list _"Subtract from current selection" CHANNEL-OP-SUBTRACT) (list _"Intersect with current selection" CHANNEL-OP-INTERSECT))) ; Local function: map script operation codes to GIMP operations. ; (define (map-op op) (cadr (nth op oplist))) ; Local function: return the list of operations for the SF-OPTION ; parameters. ; (define (get-ops-list) (define result ()) (define tempoplist (reverse oplist)) (while (not (null? tempoplist)) (set! result (cons (caar tempoplist) result)) (set! tempoplist (cdr tempoplist))) result) ; Local function: convert a list to an array of doubles. ; (define (list-to-double-array arg) (let* ((i (length arg)) (result (cons-array i 'double))) (while (> i 0) (set! i (- i 1)) (aset result i (nth i arg))) result)) ; Create a guide. NOTE: GIMP 2.2 already includes this function ; as "Image/Guides/New Guide..." ; (define (numeric-guide img drawable position orientation) (if (= orientation 0) (gimp-image-add-hguide img position) (gimp-image-add-vguide img position))) ; Set the layer's position. ; (define (numeric-layer-position img drawable xofs yofs wrt) (let* ((lx (car (gimp-drawable-width drawable))) (ly (car (gimp-drawable-height drawable))) (xref (cond ((or (= wrt 0) (= wrt 3) (= wrt 6)) 0) ((or (= wrt 1) (= wrt 4) (= wrt 7)) (* lx 0.5)) (#t lx))) (yref (cond ((or (= wrt 0) (= wrt 1) (= wrt 2)) 0) ((or (= wrt 3) (= wrt 4) (= wrt 5)) (* ly 0.5)) (#t ly)))) (gimp-layer-set-offsets drawable (- xofs xref) (- yofs yref)) (gimp-displays-flush))) ; Displace a layer. ; (define (numeric-layer-displace img drawable xofs yofs) (gimp-layer-set-offsets drawable (+ xofs (car (gimp-drawable-offsets drawable))) (+ yofs (cadr (gimp-drawable-offsets drawable)))) (gimp-displays-flush)) ; Displace a selection. ; (define (numeric-selection-displace img drawable xofs yofs) (gimp-selection-translate img xofs yofs)) ; Select a rectangle. ; (define (numeric-rect-select img drawable x1 y1 x2 y2 op) (gimp-rect-select img x1 y1 (- x2 x1) (- y2 y1) (map-op op) 0 0)) ; Select an ellipse by bounds. ; (define (numeric-ellipse-select img drawable x1 y1 x2 y2 op aa) (gimp-ellipse-select img x1 y1 (- x2 x1) (- y2 y1) (map-op op) aa 0 0)) ; Create an elliptical/rectangular path. ; (define (numeric-shape img drawable shape x1 y1 x2 y2) (let* ((tension 0.2238576250846033) (lx (- x2 x1)) (ly (- y2 y1))) (define pts (list-to-double-array (cond ; Rectangle ((= shape 0) (list x1 y1 1.0 x1 y1 2.0 x1 y2 2.0 x1 y2 1.0 x1 y2 2.0 x2 y2 2.0 x2 y2 1.0 x2 y2 2.0 x2 y1 2.0 x2 y1 1.0 x2 y1 2.0 x1 y1 2.0)) ; Ellipse ((= shape 1) (list x1 (+ y1 (* 0.5 ly)) 1.0 x1 (+ y1 (* tension ly)) 2.0 (+ x1 (* tension lx)) y1 2.0 (+ x1 (* 0.5 lx)) y1 1.0 (- x2 (* tension lx)) y1 2.0 x2 (+ y1 (* tension ly)) 2.0 x2 (+ y1 (* 0.5 ly)) 1.0 x2 (- y2 (* tension ly)) 2.0 (- x2 (* tension lx)) y2 2.0 (+ x1 (* 0.5 lx)) y2 1.0 (+ x1 (* tension lx)) y2 2.0 x1 (- y2 (* tension ly)) 2.0)) ; Diamond ((= shape 2) (list x1 (+ y1 (* 0.5 ly)) 1.0 x1 (+ y1 (* 0.5 ly)) 2.0 (+ x1 (* 0.5 lx)) y1 2.0 (+ x1 (* 0.5 lx)) y1 1.0 (+ x1 (* 0.5 lx)) y1 2.0 x2 (+ y1 (* 0.5 ly)) 2.0 x2 (+ y1 (* 0.5 ly)) 1.0 x2 (+ y1 (* 0.5 ly)) 2.0 (+ x1 (* 0.5 lx)) y2 2.0 (+ x1 (* 0.5 lx)) y2 1.0 (+ x1 (* 0.5 lx)) y2 2.0 x1 (+ y1 (* 0.5 ly)) 2.0))))) (gimp-path-set-points img _"Shape" 1 36 pts) ; Unfortunately the function gimp-path-set-visible does not exist in 2.0: ;; (gimp-path-set-visible img (car (gimp-path-get-current img)) 1) )) ; Export the functions. ; (set! script-fu-numeric-guide numeric-guide) (set! script-fu-numeric-layer-position numeric-layer-position) (set! script-fu-numeric-layer-displace numeric-layer-displace) (set! script-fu-numeric-selection-displace numeric-selection-displace) (set! script-fu-numeric-rect-select numeric-rect-select) (set! script-fu-numeric-ellipse-select numeric-ellipse-select) (set! script-fu-numeric-shape numeric-shape) ; Register the functions. ; (script-fu-register "script-fu-numeric-guide" _"/Script-Fu/Numeric/Place _Guide..." _"Place a horizontal or vertical guide by giving its coordinate." "Pedro Gimeno Fortea" _"Public Domain" "2004-06-11" "RGB*, GRAY*, INDEXED*" SF-IMAGE "Image" 0 SF-DRAWABLE "Drawable" 0 SF-VALUE _"Position" "0" SF-OPTION _"Orientation" '(_"Horizontal" _"Vertical")) (script-fu-register "script-fu-numeric-layer-position" _"/Script-Fu/Numeric/Set _Layer position..." _"Set a layer's position to the given numeric coordinates." "Pedro Gimeno Fortea" _"Public Domain" "2004-04-21" "RGB*, GRAY*, INDEXED*" SF-IMAGE "Image" 0 SF-DRAWABLE "Drawable" 0 SF-VALUE _"Position X (negative = left)" "0" SF-VALUE _"Position Y (negative = up)" "0" SF-OPTION _"Relative to" '(_"Top left" _"Top center" _"Top right" _"Center left" _"Center" _"Center right" _"Bottom left" _"Bottom center" _"Bottom right")) (script-fu-register "script-fu-numeric-layer-displace" _"/Script-Fu/Numeric/_Displace Layer..." _"Displace a layer by the given numeric amount." "Pedro Gimeno Fortea" _"Public Domain" "2005-04-09" "RGB*, GRAY*, INDEXED*" SF-IMAGE "Image" 0 SF-DRAWABLE "Drawable" 0 SF-VALUE _"Offset X (negative = left)" "0" SF-VALUE _"Offset Y (negative = up)" "0") (script-fu-register "script-fu-numeric-selection-displace" _"/Script-Fu/Numeric/D_isplace Selection..." _"Displace a selection by the given numeric amount." "Pedro Gimeno Fortea" _"Public Domain" "2004-04-22" "RGB*, GRAY*, INDEXED*" SF-IMAGE "Image" 0 SF-DRAWABLE "Drawable" 0 SF-VALUE _"Offset X (negative = left)" "0" SF-VALUE _"Offset Y (negative = up)" "0") (script-fu-register "script-fu-numeric-rect-select" _"/Script-Fu/Numeric/Select _Rectangle..." _"Select a rectangle given its left, top, right and bottom coordinates." "Pedro Gimeno Fortea" _"Public Domain" "2004-06-11" "RGB*, GRAY*, INDEXED*" SF-IMAGE "Image" 0 SF-DRAWABLE "Drawable" 0 SF-VALUE _"Left" "0" SF-VALUE _"Top" "0" SF-VALUE _"Right" "0" SF-VALUE _"Bottom" "0" SF-OPTION _"Operation" (get-ops-list)) (script-fu-register "script-fu-numeric-ellipse-select" _"/Script-Fu/Numeric/Select _Ellipse..." _"Select an ellipse numerically, given its bounds." "Pedro Gimeno Fortea" _"Public Domain" "2004-06-11" "RGB*, GRAY*, INDEXED*" SF-IMAGE "Image" 0 SF-DRAWABLE "Drawable" 0 SF-VALUE _"Left" "0" SF-VALUE _"Top" "0" SF-VALUE _"Right" "0" SF-VALUE _"Bottom" "0" SF-OPTION _"Operation" (get-ops-list) SF-TOGGLE _"Antialias" TRUE) (script-fu-register "script-fu-numeric-shape" _"/Script-Fu/Numeric/Shape as _Path..." _"Create a rectangular/elliptical/diamond-shaped path." "Pedro Gimeno Fortea" _"Public Domain" "2004-06-15" "RGB*, GRAY*, INDEXED*" SF-IMAGE "Image" 0 SF-DRAWABLE "Drawable" 0 SF-OPTION _"Shape" '(_"Rectangle" _"Ellipse" _"Diamond") SF-VALUE _"Left" "0" SF-VALUE _"Top" "0" SF-VALUE _"Right" "0" SF-VALUE _"Bottom" "0") ; Exit local scope. ; )