Advertisement
Guest User

script-fu paste into selection

a guest
Sep 26th, 2011
612
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 5.60 KB | None | 0 0
  1. ; paste-into-selection.scm
  2. ; by Rob Antonishen
  3. ; http://ffaat.pointclark.net
  4.  
  5. ; Version 1 (20110926)
  6.  
  7. ; Description
  8. ;
  9. ; pastes the current buffer as a new layer scaled to the current selection,
  10. ; above the current active layer, or scaled the canvas if there is no active layer.
  11. ; aspect ratio is preserved, and the current ddefault interpolation method is used.
  12.  
  13. ; Changes
  14.  
  15. ; License:
  16. ;
  17. ; This program is free software; you can redistribute it and/or modify
  18. ; it under the terms of the GNU General Public License as published by
  19. ; the Free Software Foundation; either version 2 of the License, or
  20. ; (at your option) any later version.
  21. ;
  22. ; This program is distributed in the hope that it will be useful,
  23. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  25. ; GNU General Public License for more details.
  26. ;
  27. ; The GNU Public License is available at
  28. ; http://www.gnu.org/copyleft/gpl.html
  29.  
  30.  
  31. (define (script-fu-paste-into-selection img drawable)
  32.  
  33.   (let* ((temp-image (car (gimp-edit-paste-as-new)))
  34.          (active-layer (car (gimp-image-get-active-layer img)))
  35.          (selection-info (gimp-selection-bounds img))
  36.          (new-layer 0)
  37.          (buffer-width 0)
  38.          (buffer-height 0)
  39.          (buffer-aspect 0)
  40.          (offset-x 0)
  41.          (offset-y 0)
  42.          (saved-selection 0)
  43.          (buffer-name "pis-buf"))
  44.  
  45.     (cond
  46.       ((= FALSE (car (gimp-image-is-valid temp-image)))
  47.        (gimp-message "There is no image data in the clipboard to paste."))
  48.        
  49.       ((= TRUE (car (gimp-layer-is-floating-sel active-layer)))
  50.        (gimp-message "The script doesn't work with floating selections."))
  51.        
  52.       (else
  53.         (gimp-image-undo-group-start img)
  54.         (set! buffer-width (car (gimp-image-width temp-image)))
  55.         (set! buffer-height (car (gimp-image-height temp-image)))
  56.         (set! buffer-aspect (/ buffer-width buffer-height))
  57.        
  58.         (if (= (car selection-info) FALSE)
  59.           (if (>= buffer-aspect (/ (car (gimp-image-width img)) (car (gimp-image-height img)))) ; if no selection
  60.             (gimp-image-scale temp-image (car (gimp-image-width img)) (trunc (/ (* buffer-height (car (gimp-image-width img))) buffer-width)))
  61.             (gimp-image-scale temp-image (trunc (/ (* buffer-width (car (gimp-image-height img))) buffer-height)) (car (gimp-image-height img)))
  62.           )      
  63.           (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)
  64.             (gimp-image-scale temp-image (- (list-ref selection-info 3) (list-ref selection-info 1))
  65.                                          (trunc (/ (* buffer-height (- (list-ref selection-info 3) (list-ref selection-info 1))) buffer-width)))
  66.             (gimp-image-scale temp-image (trunc (/ (* buffer-width (- (list-ref selection-info 4) (list-ref selection-info 2))) buffer-height))
  67.                                          (- (list-ref selection-info 4) (list-ref selection-info 2)))
  68.           )
  69.         )
  70.         (set! buffer-name (car (gimp-edit-named-copy (car (gimp-image-get-active-layer temp-image)) buffer-name)))
  71.         (set! buffer-width (car (gimp-image-width temp-image)))
  72.         (set! buffer-height (car (gimp-image-height temp-image)))
  73.         (gimp-image-delete temp-image)
  74.         (set! new-layer (car (gimp-layer-new img buffer-width buffer-height (+ (* (car (gimp-image-base-type img)) 2) 1) "Clipboard" 100 NORMAL-MODE)))
  75.         (gimp-image-add-layer img new-layer -1)    
  76.         (if (= (car selection-info) FALSE) ; no selection
  77.           (begin
  78.             (gimp-floating-sel-anchor (car (gimp-edit-named-paste new-layer buffer-name TRUE)))
  79.             (if (= active-layer -1)
  80.               (begin
  81.                 (set! offset-x (round (/ (- (car (gimp-image-width img)) buffer-width) 2)))
  82.                 (set! offset-y (round (/ (- (car (gimp-image-height img)) buffer-height) 2))))
  83.               (begin
  84.                 (set! offset-x (+ (round (/ (- (car (gimp-drawable-width active-layer)) buffer-width) 2)) (car (gimp-drawable-offsets active-layer))))
  85.                 (set! offset-y (+ (round (/ (- (car (gimp-drawable-height active-layer)) buffer-height) 2)) (cadr (gimp-drawable-offsets active-layer)))))
  86.             )
  87.           )
  88.           (begin  ;else selection
  89.             (set! saved-selection (car (gimp-selection-save img)))
  90.             (gimp-selection-none img)
  91.             (gimp-floating-sel-anchor (car (gimp-edit-named-paste new-layer buffer-name TRUE)))
  92.             (set! offset-x (+ (round (/ (- (- (list-ref selection-info 3) (list-ref selection-info 1)) buffer-width) 2)) (list-ref selection-info 1)))
  93.             (set! offset-y (+ (round (/ (- (- (list-ref selection-info 4) (list-ref selection-info 2)) buffer-height) 2)) (list-ref selection-info 2)))
  94.             (gimp-selection-load saved-selection)
  95.             (gimp-image-remove-channel img saved-selection)
  96.           )
  97.         )
  98.         (gimp-layer-translate new-layer offset-x offset-y)
  99.         (gimp-image-set-active-layer img active-layer)
  100.         (gimp-displays-flush)
  101.         (gimp-image-undo-group-end img)
  102.       )
  103.     )
  104.   )
  105. )
  106.  
  107. (script-fu-register "script-fu-paste-into-selection"
  108.   "Paste Into Current Selection"
  109.   "Paste the clipboard contents scaled into the current selection"
  110.   "Rob Antonishen"
  111.   "Rob Antonishen"
  112.   "2011-09"
  113.   "*"
  114.   SF-IMAGE      "image"      0
  115.   SF-DRAWABLE   "drawable"   0
  116. )
  117.  
  118. (script-fu-menu-register "script-fu-paste-into-selection"
  119.                          "<Image>/Edit")
  120.  
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement