GIMP Script-fu

Artifact [d36b88184b]
Login

Artifact [d36b88184b]

Artifact d36b88184be28654d6643e8bd7c9b989dd53f90c:


; 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.

(define (script-fu-sg-dejw-paste image drawable)
  (let ((width (car (gimp-image-width image)))
        (height (car (gimp-image-height image)))
        (orig-selection 0)
        (layer 0)
        (selection 0)
        (x 0)
        (y 0)
        (bounds '())     
        (orig-drawable drawable)
        (buffer "sgmfbuffer")
        (selection-drawable 0) )
    (gimp-image-undo-group-start image)
    (gimp-selection-layer-alpha drawable)
    (set! selection (car (gimp-selection-save image)))
    (gimp-selection-none image)
    (gimp-threshold selection 127 255)
    
    (gimp-image-set-active-layer image drawable)
    (set! layer (car (gimp-layer-new-from-drawable drawable image)))
    (gimp-image-add-layer image layer -1)
    (gimp-layer-add-alpha layer)
    (gimp-drawable-fill layer TRANSPARENT-FILL)
    (gimp-drawable-set-name layer "canvas")
    (gimp-selection-load selection)
    (set! selection-drawable (car (gimp-image-get-selection image)))
    
    (while (< y height)
      (while (< x width)
        (if (> (car (gimp-selection-value image x y)) 127)
          (begin
            (gimp-fuzzy-select selection x y 127 CHANNEL-OP-INTERSECT FALSE FALSE 0 FALSE)
            
            (let* ((bounds (cdr (gimp-selection-bounds image)))
                   (selection (car (gimp-selection-save image)))
                   (floating-sel (car (gimp-edit-paste layer FALSE))) )
              (gimp-layer-scale floating-sel
                                (- (caddr bounds) (car bounds))
                                (- (cadddr bounds) (cadr bounds))
                                TRUE )
              (gimp-layer-set-offsets floating-sel (car bounds) (cadr bounds)) 
              (gimp-floating-sel-anchor floating-sel)
              (gimp-selection-load selection)
              (gimp-image-remove-channel image selection)
              )
            
            (gimp-displays-flush)
            (gimp-channel-combine-masks selection selection-drawable CHANNEL-OP-SUBTRACT 0 0)
            (gimp-selection-load selection)
            (gimp-rect-select image x y width 1 CHANNEL-OP-INTERSECT FALSE 0)
            (if (= (car (set! bounds (gimp-selection-bounds image))) TRUE)
              (set! x (cadr bounds))
              (set! x width) )
            (gimp-selection-load selection)
            (if (= (car (set! bounds (gimp-selection-bounds image))) TRUE)
              (begin
                (set! width (cadddr bounds))
                (set! y (max y (caddr bounds)))
                (set! height (cadr (cdddr bounds))) )
              (begin
                (set! x width))))
          (begin
            (set! x 0)
            (gimp-rect-select image x y width 1 CHANNEL-OP-INTERSECT FALSE 0)
            (if (= (car (set! bounds (gimp-selection-bounds image))) TRUE)
              (set! x (cadr bounds))
              (set! x width) )
            (gimp-selection-load selection) 
            )
          )
        )
      (if (= (car (set! bounds (gimp-selection-bounds image))) TRUE)
        (begin
          (set! x (cadr bounds))
          (set! width (cadddr bounds))
          (set! y (max (+ y 1) (caddr bounds)))
          (set! height (cadr (cdddr bounds))) )
        (begin
          (set! x width)
          (set! y height) )))
          
    (gimp-image-remove-channel image selection)
   
    (gimp-displays-flush)
    (gimp-image-undo-group-end image)
    (gimp-image-set-active-layer image layer)))

(script-fu-register "script-fu-sg-dejw-paste"
  "Bauble paste per dejw"
  "Paste clipboard into \"dots\""
  "Saul Goode"
  "Saul Goode"
  "May 2012"
  "RGB*"
  SF-IMAGE "Image" 0
  SF-DRAWABLE "Drawable" 0
  )
(script-fu-menu-register "script-fu-sg-dejw-paste"
  "<Image>/Filters/Misc"
  )