; center-ops.scm - Perform layer/selection centering operations. ; Version: 1.1. ; GIMP Script-Fu by Pedro Gimeno Fortea. ; Donated to the public domain. ; ; Revision history: ; 1.0 (2004-04-22): First revision. ; 1.1 (2005-04-08): Changed the menu in which the layer centering ; functions are registered. Also changed the function ; names. ; ; Center the current layer with respect to the image. ; NOTE: There's a similar function in the Perl-Fu distribution. ; (define (script-fu-layer-center img drawable) (let* ((iw (car (gimp-image-width img))) (ih (car (gimp-image-height img))) (lw (car (gimp-drawable-width drawable))) (lh (car (gimp-drawable-height drawable)))) (gimp-layer-set-offsets drawable (/ (- iw lw) 2) (/ (- ih lh) 2)) (gimp-displays-flush))) ; Center the current layer with respect to another layer. ; (define (script-fu-layer-center-in-layer img drawable layer) (let* ((loffset (gimp-drawable-offsets layer)) (lx (car loffset)) (ly (cadr loffset)) (lw (car (gimp-drawable-width layer))) (lh (car (gimp-drawable-height layer))) (dw (car (gimp-drawable-width drawable))) (dh (car (gimp-drawable-height drawable)))) (if (= (car (gimp-drawable-is-layer drawable)) TRUE) (begin (gimp-layer-set-offsets drawable (+ (/ (- lw dw) 2) lx) (+ (/ (- lh dh) 2) ly)) (gimp-displays-flush))))) ; Center a selection with respect to the image ; (define (script-fu-selection-center-in-image img drawable) (let* ((bounds (gimp-selection-bounds img)) (lower-right (cdddr bounds)) (sel-present (car bounds)) (x1 (cadr bounds)) (y1 (caddr bounds)) (x2 (car lower-right)) (y2 (cadr lower-right)) (iw (car (gimp-image-width img))) (ih (car (gimp-image-height img)))) (if (= sel-present TRUE) (gimp-selection-translate img (- (/ (- iw (- x2 x1)) 2) x1) (- (/ (- ih (- y2 y1)) 2) y1))))) ; Center the selection with respect to the current layer. ; (define (script-fu-selection-center-in-layer img drawable) (let* ((bounds (gimp-selection-bounds img)) (lower-right (cdddr bounds)) (sel-present (car bounds)) (sx1 (cadr bounds)) (sy1 (caddr bounds)) (sx2 (car lower-right)) (sy2 (cadr lower-right)) (dw (car (gimp-drawable-width drawable))) (dh (car (gimp-drawable-height drawable))) (offsets (gimp-drawable-offsets drawable)) (ofsx (car offsets)) (ofsy (cadr offsets))) (if (= sel-present TRUE) (gimp-selection-translate img (+ (- (/ (- dw (- sx2 sx1)) 2) sx1) ofsx) (+ (- (/ (- dh (- sy2 sy1)) 2) sy1) ofsy))))) ; Register the functions. ; (script-fu-register "script-fu-layer-center" _"/Script-Fu/Layer/_Center in Image" _"Center a layer with respect to the image." "Pedro Gimeno Fortea" _"Public Domain" "2004-04-22" "RGB*, GRAY*, INDEXED*" SF-IMAGE "Image" 0 SF-DRAWABLE "Drawable" 0) (script-fu-register "script-fu-layer-center-in-layer" _"/Script-Fu/Layer/Center in _Layer..." _"Center the current layer with respect to a reference layer." "Pedro Gimeno Fortea" _"Public Domain" "2004-04-22" "RGB*, GRAY*, INDEXED*" SF-IMAGE "Image" 0 SF-DRAWABLE "Drawable" 0 SF-LAYER _"Reference Layer" 0) (script-fu-register "script-fu-selection-center-in-image" _"/Script-Fu/_Selection/_Center in Image" _"Center the selection with respect to the image." "Pedro Gimeno Fortea" _"Public Domain" "2004-04-22" "RGB*, GRAY*, INDEXED*" SF-IMAGE "Image" 0 SF-DRAWABLE "Drawable" 0) (script-fu-register "script-fu-selection-center-in-layer" _"/Script-Fu/_Selection/Center in _Layer" _"Center the selection with respect to the current layer." "Pedro Gimeno Fortea" _"Public Domain" "2004-04-22" "RGB*, GRAY*, INDEXED*" SF-IMAGE "Image" 0 SF-DRAWABLE "Drawable" 0)