; paste-into-selection.scm
; by Rob Antonishen
; http://ffaat.pointclark.net
; Version 1 (20110926)
; Description
;
; pastes the current buffer as a new layer scaled to the current selection,
; above the current active layer, or scaled the canvas if there is no active layer.
; aspect ratio is preserved, and the current ddefault interpolation method is used.
; Changes
; 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-paste-into-selection img drawable)
(let* ((temp-image (car (gimp-edit-paste-as-new)))
(active-layer (car (gimp-image-get-active-layer img)))
(selection-info (gimp-selection-bounds img))
(new-layer 0)
(buffer-width 0)
(buffer-height 0)
(buffer-aspect 0)
(offset-x 0)
(offset-y 0)
(saved-selection 0)
(buffer-name "pis-buf"))
(cond
((= FALSE (car (gimp-image-is-valid temp-image)))
(gimp-message "There is no image data in the clipboard to paste."))
((= TRUE (car (gimp-layer-is-floating-sel active-layer)))
(gimp-message "The script doesn't work with floating selections."))
(else
(gimp-image-undo-group-start img)
(set! buffer-width (car (gimp-image-width temp-image)))
(set! buffer-height (car (gimp-image-height temp-image)))
(set! buffer-aspect (/ buffer-width buffer-height))
(if (= (car selection-info) FALSE)
(if (>= buffer-aspect (/ (car (gimp-image-width img)) (car (gimp-image-height img)))) ; if no selection
(gimp-image-scale temp-image (car (gimp-image-width img)) (trunc (/ (* buffer-height (car (gimp-image-width img))) buffer-width)))
(gimp-image-scale temp-image (trunc (/ (* buffer-width (car (gimp-image-height img))) buffer-height)) (car (gimp-image-height img)))
)
(if (>= buffer-aspect (/ (- (list-ref selection-info 3) (list-ref selection-info 1)) (- (list-ref selection-info 4) (list-ref selection-info 2)))) ; else (if selection)
(gimp-image-scale temp-image (- (list-ref selection-info 3) (list-ref selection-info 1))
(trunc (/ (* buffer-height (- (list-ref selection-info 3) (list-ref selection-info 1))) buffer-width)))
(gimp-image-scale temp-image (trunc (/ (* buffer-width (- (list-ref selection-info 4) (list-ref selection-info 2))) buffer-height))
(- (list-ref selection-info 4) (list-ref selection-info 2)))
)
)
(set! buffer-name (car (gimp-edit-named-copy (car (gimp-image-get-active-layer temp-image)) buffer-name)))
(set! buffer-width (car (gimp-image-width temp-image)))
(set! buffer-height (car (gimp-image-height temp-image)))
(gimp-image-delete temp-image)
(set! new-layer (car (gimp-layer-new img buffer-width buffer-height (+ (* (car (gimp-image-base-type img)) 2) 1) "Clipboard" 100 NORMAL-MODE)))
(gimp-image-add-layer img new-layer -1)
(if (= (car selection-info) FALSE) ; no selection
(begin
(gimp-floating-sel-anchor (car (gimp-edit-named-paste new-layer buffer-name TRUE)))
(if (= active-layer -1)
(begin
(set! offset-x (round (/ (- (car (gimp-image-width img)) buffer-width) 2)))
(set! offset-y (round (/ (- (car (gimp-image-height img)) buffer-height) 2))))
(begin
(set! offset-x (+ (round (/ (- (car (gimp-drawable-width active-layer)) buffer-width) 2)) (car (gimp-drawable-offsets active-layer))))
(set! offset-y (+ (round (/ (- (car (gimp-drawable-height active-layer)) buffer-height) 2)) (cadr (gimp-drawable-offsets active-layer)))))
)
)
(begin ;else selection
(set! saved-selection (car (gimp-selection-save img)))
(gimp-selection-none img)
(gimp-floating-sel-anchor (car (gimp-edit-named-paste new-layer buffer-name TRUE)))
(set! offset-x (+ (round (/ (- (- (list-ref selection-info 3) (list-ref selection-info 1)) buffer-width) 2)) (list-ref selection-info 1)))
(set! offset-y (+ (round (/ (- (- (list-ref selection-info 4) (list-ref selection-info 2)) buffer-height) 2)) (list-ref selection-info 2)))
(gimp-selection-load saved-selection)
(gimp-image-remove-channel img saved-selection)
)
)
(gimp-layer-translate new-layer offset-x offset-y)
(gimp-image-set-active-layer img active-layer)
(gimp-displays-flush)
(gimp-image-undo-group-end img)
)
)
)
)
(script-fu-register "script-fu-paste-into-selection"
"Paste Into Current Selection"
"Paste the clipboard contents scaled into the current selection"
"Rob Antonishen"
"Rob Antonishen"
"2011-09"
"*"
SF-IMAGE "image" 0
SF-DRAWABLE "drawable" 0
)
(script-fu-menu-register "script-fu-paste-into-selection"
"<Image>/Edit")