GIMP Script-fu

Hex Artifact Content
Login

Artifact 2200b3f934fa1806ded9cf9b9e3270325be13e3f:


0000: 3b 20 54 68 69 73 20 70 72 6f 67 72 61 6d 20 69  ; This program i
0010: 73 20 66 72 65 65 20 73 6f 66 74 77 61 72 65 3b  s free software;
0020: 20 79 6f 75 20 63 61 6e 20 72 65 64 69 73 74 72   you can redistr
0030: 69 62 75 74 65 20 69 74 20 61 6e 64 2f 6f 72 20  ibute it and/or 
0040: 6d 6f 64 69 66 79 0a 3b 20 69 74 20 75 6e 64 65  modify.; it unde
0050: 72 20 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74  r the terms of t
0060: 68 65 20 47 4e 55 20 47 65 6e 65 72 61 6c 20 50  he GNU General P
0070: 75 62 6c 69 63 20 4c 69 63 65 6e 73 65 20 61 73  ublic License as
0080: 20 70 75 62 6c 69 73 68 65 64 20 62 79 0a 3b 20   published by.; 
0090: 74 68 65 20 46 72 65 65 20 53 6f 66 74 77 61 72  the Free Softwar
00a0: 65 20 46 6f 75 6e 64 61 74 69 6f 6e 3b 20 65 69  e Foundation; ei
00b0: 74 68 65 72 20 76 65 72 73 69 6f 6e 20 32 20 6f  ther version 2 o
00c0: 66 20 74 68 65 20 4c 69 63 65 6e 73 65 2c 20 6f  f the License, o
00d0: 72 0a 3b 20 28 61 74 20 79 6f 75 72 20 6f 70 74  r.; (at your opt
00e0: 69 6f 6e 29 20 61 6e 79 20 6c 61 74 65 72 20 76  ion) any later v
00f0: 65 72 73 69 6f 6e 2e 0a 3b 0a 3b 20 54 68 69 73  ersion..;.; This
0100: 20 70 72 6f 67 72 61 6d 20 69 73 20 64 69 73 74   program is dist
0110: 72 69 62 75 74 65 64 20 69 6e 20 74 68 65 20 68  ributed in the h
0120: 6f 70 65 20 74 68 61 74 20 69 74 20 77 69 6c 6c  ope that it will
0130: 20 62 65 20 75 73 65 66 75 6c 2c 0a 3b 20 62 75   be useful,.; bu
0140: 74 20 57 49 54 48 4f 55 54 20 41 4e 59 20 57 41  t WITHOUT ANY WA
0150: 52 52 41 4e 54 59 3b 20 77 69 74 68 6f 75 74 20  RRANTY; without 
0160: 65 76 65 6e 20 74 68 65 20 69 6d 70 6c 69 65 64  even the implied
0170: 20 77 61 72 72 61 6e 74 79 20 6f 66 0a 3b 20 4d   warranty of.; M
0180: 45 52 43 48 41 4e 54 41 42 49 4c 49 54 59 20 6f  ERCHANTABILITY o
0190: 72 20 46 49 54 4e 45 53 53 20 46 4f 52 20 41 20  r FITNESS FOR A 
01a0: 50 41 52 54 49 43 55 4c 41 52 20 50 55 52 50 4f  PARTICULAR PURPO
01b0: 53 45 2e 20 20 53 65 65 20 74 68 65 0a 3b 20 47  SE.  See the.; G
01c0: 4e 55 20 47 65 6e 65 72 61 6c 20 50 75 62 6c 69  NU General Publi
01d0: 63 20 4c 69 63 65 6e 73 65 20 66 6f 72 20 6d 6f  c License for mo
01e0: 72 65 20 64 65 74 61 69 6c 73 2e 0a 0a 3b 3b 20  re details...;; 
01f0: 54 68 69 73 20 73 63 72 69 70 74 20 61 64 64 73  This script adds
0200: 20 61 20 6c 61 79 65 72 6d 61 73 6b 20 74 6f 20   a layermask to 
0210: 74 68 65 20 61 63 74 69 76 65 20 6c 61 79 65 72  the active layer
0220: 20 62 61 73 65 64 20 75 70 6f 6e 20 0a 3b 3b 20   based upon .;; 
0230: 74 68 65 20 6c 61 79 65 72 6d 61 73 6b 20 6f 66  the layermask of
0240: 20 74 68 65 20 76 69 73 69 62 6c 65 20 6c 61 79   the visible lay
0250: 65 72 20 61 62 6f 76 65 20 69 74 20 69 6e 20 74  er above it in t
0260: 68 65 20 6c 61 79 65 72 20 73 74 61 63 6b 2e 20  he layer stack. 
0270: 0a 3b 3b 20 53 70 65 63 69 66 69 63 61 6c 6c 79  .;; Specifically
0280: 2c 0a 3b 3b 20 20 20 49 66 20 74 68 65 20 61 63  ,.;;   If the ac
0290: 74 69 76 65 20 6c 61 79 65 72 20 61 6c 72 65 61  tive layer alrea
02a0: 64 79 20 68 61 73 20 61 20 6d 61 73 6b 2c 20 69  dy has a mask, i
02b0: 74 20 69 73 20 72 65 70 6c 61 63 65 64 2e 0a 3b  t is replaced..;
02c0: 3b 20 20 20 49 66 20 74 68 65 20 61 62 6f 76 65  ;   If the above
02d0: 20 6c 61 79 65 72 20 68 61 73 20 61 20 6d 61 73   layer has a mas
02e0: 6b 2c 20 69 74 20 69 73 20 61 64 64 65 64 20 74  k, it is added t
02f0: 6f 20 74 68 65 20 61 63 74 69 76 65 20 6c 61 79  o the active lay
0300: 65 72 2e 0a 3b 3b 20 20 20 49 66 20 74 68 65 20  er..;;   If the 
0310: 61 62 6f 76 65 20 6c 61 79 65 72 20 64 6f 65 73  above layer does
0320: 20 6e 6f 74 20 68 61 76 65 20 61 20 6d 61 73 6b   not have a mask
0330: 2c 20 61 20 67 72 61 79 73 63 61 6c 65 20 63 6f  , a grayscale co
0340: 70 79 20 6f 66 20 74 68 65 20 0a 3b 3b 20 20 20  py of the .;;   
0350: 20 20 61 62 6f 76 65 20 6c 61 79 65 72 20 69 73    above layer is
0360: 20 61 64 64 65 64 20 61 73 20 61 20 6d 61 73 6b   added as a mask
0370: 20 74 6f 20 74 68 65 20 61 63 74 69 76 65 20 6c   to the active l
0380: 61 79 65 72 20 2d 2d 20 75 6e 6c 65 73 73 0a 3b  ayer -- unless.;
0390: 3b 20 20 20 20 20 74 68 65 20 61 62 6f 76 65 20  ;     the above 
03a0: 6c 61 79 65 72 20 69 73 20 61 20 74 65 78 74 20  layer is a text 
03b0: 6c 61 79 65 72 2c 20 69 6e 20 77 68 69 63 68 20  layer, in which 
03c0: 63 61 73 65 20 74 68 65 20 74 65 78 74 20 0a 3b  case the text .;
03d0: 3b 20 20 20 20 20 69 73 20 75 73 65 64 20 66 6f  ;     is used fo
03e0: 72 20 74 68 65 20 6d 61 73 6b 2e 0a 3b 3b 20 20  r the mask..;;  
03f0: 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   If there is no 
0400: 76 69 73 69 62 6c 65 20 6c 61 79 65 72 20 61 62  visible layer ab
0410: 6f 76 65 20 74 68 65 20 61 63 74 69 76 65 20 6f  ove the active o
0420: 6e 65 2c 20 61 20 6d 61 73 6b 20 69 73 20 61 64  ne, a mask is ad
0430: 64 65 64 20 0a 3b 3b 20 20 20 20 20 69 6e 69 74  ded .;;     init
0440: 69 61 6c 69 7a 65 64 20 66 72 6f 6d 20 74 68 65  ialized from the
0450: 20 73 65 6c 65 63 74 69 6f 6e 20 28 61 76 6f 69   selection (avoi
0460: 64 69 6e 67 20 74 68 65 20 64 69 61 6c 6f 67 29  ding the dialog)
0470: 2e 0a 3b 0a 0a 28 64 65 66 69 6e 65 20 28 73 63  ..;..(define (sc
0480: 72 69 70 74 2d 66 75 2d 73 67 2d 63 6f 70 79 2d  ript-fu-sg-copy-
0490: 6d 61 73 6b 2d 66 72 6f 6d 2d 61 62 6f 76 65 20  mask-from-above 
04a0: 69 6d 61 67 65 20 6c 61 79 65 72 29 0a 20 20 28  image layer).  (
04b0: 67 69 6d 70 2d 69 6d 61 67 65 2d 75 6e 64 6f 2d  gimp-image-undo-
04c0: 67 72 6f 75 70 2d 73 74 61 72 74 20 69 6d 61 67  group-start imag
04d0: 65 29 0a 20 20 28 73 65 74 21 20 6c 61 79 65 72  e).  (set! layer
04e0: 20 28 63 61 72 20 28 67 69 6d 70 2d 69 6d 61 67   (car (gimp-imag
04f0: 65 2d 67 65 74 2d 61 63 74 69 76 65 2d 6c 61 79  e-get-active-lay
0500: 65 72 20 69 6d 61 67 65 29 29 29 0a 20 20 28 75  er image))).  (u
0510: 6e 6c 65 73 73 20 28 3d 20 28 63 61 72 20 28 67  nless (= (car (g
0520: 69 6d 70 2d 6c 61 79 65 72 2d 67 65 74 2d 6d 61  imp-layer-get-ma
0530: 73 6b 20 6c 61 79 65 72 29 29 20 2d 31 29 0a 20  sk layer)) -1). 
0540: 20 20 20 28 67 69 6d 70 2d 69 6d 61 67 65 2d 72     (gimp-image-r
0550: 65 6d 6f 76 65 2d 6c 61 79 65 72 2d 6d 61 73 6b  emove-layer-mask
0560: 20 69 6d 61 67 65 20 6c 61 79 65 72 20 4d 41 53   image layer MAS
0570: 4b 2d 44 49 53 43 41 52 44 29 20 29 0a 20 20 28  K-DISCARD) ).  (
0580: 6c 65 74 20 28 28 61 62 6f 76 65 2d 6c 61 79 65  let ((above-laye
0590: 72 20 0a 20 20 20 20 20 20 20 20 20 20 28 6c 65  r .          (le
05a0: 74 20 6c 6f 6f 70 20 28 28 61 62 6f 76 65 2d 6c  t loop ((above-l
05b0: 61 79 65 72 20 23 66 29 0a 20 20 20 20 20 20 20  ayer #f).       
05c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 6c                (l
05d0: 61 79 65 72 73 20 28 76 65 63 74 6f 72 2d 3e 6c  ayers (vector->l
05e0: 69 73 74 20 28 63 61 64 72 20 28 67 69 6d 70 2d  ist (cadr (gimp-
05f0: 69 6d 61 67 65 2d 67 65 74 2d 6c 61 79 65 72 73  image-get-layers
0600: 20 69 6d 61 67 65 29 29 29 29 20 29 0a 20 20 20   image)))) ).   
0610: 20 20 20 20 20 20 20 20 20 28 69 66 20 28 6e 75           (if (nu
0620: 6c 6c 3f 20 6c 61 79 65 72 73 29 0a 20 20 20 20  ll? layers).    
0630: 20 20 20 20 20 20 20 20 20 20 61 62 6f 76 65 2d            above-
0640: 6c 61 79 65 72 0a 20 20 20 20 20 20 20 20 20 20  layer.          
0650: 20 20 20 20 28 62 65 67 69 6e 0a 20 20 20 20 20      (begin.     
0660: 20 20 20 20 20 20 20 20 20 20 20 28 69 66 20 28             (if (
0670: 3d 20 28 63 61 72 20 6c 61 79 65 72 73 29 20 6c  = (car layers) l
0680: 61 79 65 72 29 0a 20 20 20 20 20 20 20 20 20 20  ayer).          
0690: 20 20 20 20 20 20 20 20 28 6c 6f 6f 70 20 61 62          (loop ab
06a0: 6f 76 65 2d 6c 61 79 65 72 20 27 28 29 29 0a 20  ove-layer '()). 
06b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
06c0: 20 28 6c 6f 6f 70 20 28 69 66 20 28 7a 65 72 6f   (loop (if (zero
06d0: 3f 20 28 63 61 72 20 28 67 69 6d 70 2d 64 72 61  ? (car (gimp-dra
06e0: 77 61 62 6c 65 2d 67 65 74 2d 76 69 73 69 62 6c  wable-get-visibl
06f0: 65 20 28 63 61 72 20 6c 61 79 65 72 73 29 29 29  e (car layers)))
0700: 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ).              
0710: 20 20 20 20 20 20 20 20 20 20 20 20 61 62 6f 76              abov
0720: 65 2d 6c 61 79 65 72 0a 20 20 20 20 20 20 20 20  e-layer.        
0730: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
0740: 20 20 28 63 61 72 20 6c 61 79 65 72 73 29 20 29    (car layers) )
0750: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
0760: 20 20 20 20 20 20 20 20 20 28 63 64 72 20 6c 61           (cdr la
0770: 79 65 72 73 29 20 29 29 29 29 29 29 0a 20 20 20  yers) )))))).   
0780: 20 20 20 20 20 28 6f 72 69 67 2d 73 65 6c 20 28       (orig-sel (
0790: 63 61 72 20 28 67 69 6d 70 2d 73 65 6c 65 63 74  car (gimp-select
07a0: 69 6f 6e 2d 73 61 76 65 20 69 6d 61 67 65 29 29  ion-save image))
07b0: 29 20 29 0a 20 20 20 20 28 77 68 65 6e 20 61 62  ) ).    (when ab
07c0: 6f 76 65 2d 6c 61 79 65 72 0a 20 20 20 20 20 20  ove-layer.      
07d0: 28 6c 65 74 20 28 28 61 62 6f 76 65 2d 6d 61 73  (let ((above-mas
07e0: 6b 20 28 63 61 72 20 28 67 69 6d 70 2d 6c 61 79  k (car (gimp-lay
07f0: 65 72 2d 67 65 74 2d 6d 61 73 6b 20 61 62 6f 76  er-get-mask abov
0800: 65 2d 6c 61 79 65 72 29 29 29 29 0a 20 20 20 20  e-layer)))).    
0810: 20 20 20 20 28 69 66 20 28 3d 20 61 62 6f 76 65      (if (= above
0820: 2d 6d 61 73 6b 20 2d 31 29 0a 20 20 20 20 20 20  -mask -1).      
0830: 20 20 20 20 28 69 66 20 28 7a 65 72 6f 3f 20 28      (if (zero? (
0840: 63 61 72 20 28 67 69 6d 70 2d 64 72 61 77 61 62  car (gimp-drawab
0850: 6c 65 2d 69 73 2d 74 65 78 74 2d 6c 61 79 65 72  le-is-text-layer
0860: 20 61 62 6f 76 65 2d 6c 61 79 65 72 29 29 29 0a   above-layer))).
0870: 20 20 20 20 20 20 20 20 20 20 20 20 28 62 65 67              (beg
0880: 69 6e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  in.             
0890: 20 28 73 65 74 21 20 61 62 6f 76 65 2d 6d 61 73   (set! above-mas
08a0: 6b 20 28 63 61 72 20 28 67 69 6d 70 2d 6c 61 79  k (car (gimp-lay
08b0: 65 72 2d 63 72 65 61 74 65 2d 6d 61 73 6b 20 61  er-create-mask a
08c0: 62 6f 76 65 2d 6c 61 79 65 72 20 41 44 44 2d 43  bove-layer ADD-C
08d0: 4f 50 59 2d 4d 41 53 4b 29 29 29 0a 20 20 20 20  OPY-MASK))).    
08e0: 20 20 20 20 20 20 20 20 20 20 28 67 69 6d 70 2d            (gimp-
08f0: 6c 61 79 65 72 2d 61 64 64 2d 6d 61 73 6b 20 61  layer-add-mask a
0900: 62 6f 76 65 2d 6c 61 79 65 72 20 61 62 6f 76 65  bove-layer above
0910: 2d 6d 61 73 6b 29 0a 20 20 20 20 20 20 20 20 20  -mask).         
0920: 20 20 20 20 20 28 67 69 6d 70 2d 73 65 6c 65 63       (gimp-selec
0930: 74 69 6f 6e 2d 6c 6f 61 64 20 61 62 6f 76 65 2d  tion-load above-
0940: 6d 61 73 6b 29 0a 20 20 20 20 20 20 20 20 20 20  mask).          
0950: 20 20 20 20 28 67 69 6d 70 2d 6c 61 79 65 72 2d      (gimp-layer-
0960: 72 65 6d 6f 76 65 2d 6d 61 73 6b 20 61 62 6f 76  remove-mask abov
0970: 65 2d 6c 61 79 65 72 20 4d 41 53 4b 2d 44 49 53  e-layer MASK-DIS
0980: 43 41 52 44 29 20 29 0a 20 20 20 20 20 20 20 20  CARD) ).        
0990: 20 20 20 20 28 67 69 6d 70 2d 73 65 6c 65 63 74      (gimp-select
09a0: 69 6f 6e 2d 6c 61 79 65 72 2d 61 6c 70 68 61 20  ion-layer-alpha 
09b0: 61 62 6f 76 65 2d 6c 61 79 65 72 29 20 29 0a 20  above-layer) ). 
09c0: 20 20 20 20 20 20 20 20 20 28 67 69 6d 70 2d 73           (gimp-s
09d0: 65 6c 65 63 74 69 6f 6e 2d 6c 6f 61 64 20 61 62  election-load ab
09e0: 6f 76 65 2d 6d 61 73 6b 29 20 29 29 29 0a 20 20  ove-mask) ))).  
09f0: 20 20 28 67 69 6d 70 2d 6c 61 79 65 72 2d 61 64    (gimp-layer-ad
0a00: 64 2d 6d 61 73 6b 20 6c 61 79 65 72 20 28 63 61  d-mask layer (ca
0a10: 72 20 28 67 69 6d 70 2d 6c 61 79 65 72 2d 63 72  r (gimp-layer-cr
0a20: 65 61 74 65 2d 6d 61 73 6b 20 6c 61 79 65 72 20  eate-mask layer 
0a30: 41 44 44 2d 53 45 4c 45 43 54 49 4f 4e 2d 4d 41  ADD-SELECTION-MA
0a40: 53 4b 29 29 29 0a 20 20 20 20 28 67 69 6d 70 2d  SK))).    (gimp-
0a50: 73 65 6c 65 63 74 69 6f 6e 2d 6c 6f 61 64 20 6f  selection-load o
0a60: 72 69 67 2d 73 65 6c 29 0a 20 20 20 20 28 67 69  rig-sel).    (gi
0a70: 6d 70 2d 69 6d 61 67 65 2d 72 65 6d 6f 76 65 2d  mp-image-remove-
0a80: 63 68 61 6e 6e 65 6c 20 69 6d 61 67 65 20 6f 72  channel image or
0a90: 69 67 2d 73 65 6c 29 0a 20 20 20 20 28 67 69 6d  ig-sel).    (gim
0aa0: 70 2d 69 6d 61 67 65 2d 73 65 74 2d 61 63 74 69  p-image-set-acti
0ab0: 76 65 2d 6c 61 79 65 72 20 69 6d 61 67 65 20 6c  ve-layer image l
0ac0: 61 79 65 72 29 0a 20 20 20 20 28 67 69 6d 70 2d  ayer).    (gimp-
0ad0: 6c 61 79 65 72 2d 73 65 74 2d 65 64 69 74 2d 6d  layer-set-edit-m
0ae0: 61 73 6b 20 6c 61 79 65 72 20 54 52 55 45 29 20  ask layer TRUE) 
0af0: 0a 20 20 20 20 29 0a 20 20 28 67 69 6d 70 2d 69  .    ).  (gimp-i
0b00: 6d 61 67 65 2d 75 6e 64 6f 2d 67 72 6f 75 70 2d  mage-undo-group-
0b10: 65 6e 64 20 69 6d 61 67 65 29 0a 20 20 28 67 69  end image).  (gi
0b20: 6d 70 2d 64 69 73 70 6c 61 79 73 2d 66 6c 75 73  mp-displays-flus
0b30: 68 29 0a 20 20 29 0a 0a 28 73 63 72 69 70 74 2d  h).  )..(script-
0b40: 66 75 2d 72 65 67 69 73 74 65 72 20 22 73 63 72  fu-register "scr
0b50: 69 70 74 2d 66 75 2d 73 67 2d 63 6f 70 79 2d 6d  ipt-fu-sg-copy-m
0b60: 61 73 6b 2d 66 72 6f 6d 2d 61 62 6f 76 65 22 0a  ask-from-above".
0b70: 20 20 22 43 6f 70 79 20 4c 61 79 65 72 20 4d 61    "Copy Layer Ma
0b80: 73 6b 20 46 72 6f 6d 20 41 62 6f 76 65 22 0a 20  sk From Above". 
0b90: 20 22 43 6f 70 79 20 6d 61 73 6b 20 66 72 6f 6d   "Copy mask from
0ba0: 20 61 62 6f 76 65 20 6c 61 79 65 72 20 61 20 6c   above layer a l
0bb0: 61 20 47 41 50 27 73 20 4d 6f 64 69 66 79 20 46  a GAP's Modify F
0bc0: 72 61 6d 65 73 22 0a 20 20 22 53 61 75 6c 20 47  rames".  "Saul G
0bd0: 6f 6f 64 65 22 0a 20 20 22 73 61 75 6c 67 6f 6f  oode".  "saulgoo
0be0: 64 65 22 0a 20 20 22 46 65 62 72 75 61 72 79 20  de".  "February 
0bf0: 32 30 31 32 22 0a 20 20 22 2a 22 0a 20 20 53 46  2012".  "*".  SF
0c00: 2d 49 4d 41 47 45 20 20 20 20 22 49 6d 61 67 65  -IMAGE    "Image
0c10: 22 20 20 20 20 30 0a 20 20 53 46 2d 44 52 41 57  "    0.  SF-DRAW
0c20: 41 42 4c 45 20 22 44 72 61 77 61 62 6c 65 22 20  ABLE "Drawable" 
0c30: 30 20 3b 20 74 6f 20 61 6c 6c 6f 77 20 72 65 67  0 ; to allow reg
0c40: 69 73 74 65 72 69 6e 67 20 69 6e 20 3c 4c 61 79  istering in <Lay
0c50: 65 72 73 3e 20 6d 65 6e 75 0a 20 20 29 0a 20 20  ers> menu.  ).  
0c60: 0a 28 73 63 72 69 70 74 2d 66 75 2d 6d 65 6e 75  .(script-fu-menu
0c70: 2d 72 65 67 69 73 74 65 72 20 22 73 63 72 69 70  -register "scrip
0c80: 74 2d 66 75 2d 73 67 2d 63 6f 70 79 2d 6d 61 73  t-fu-sg-copy-mas
0c90: 6b 2d 66 72 6f 6d 2d 61 62 6f 76 65 22 0a 20 20  k-from-above".  
0ca0: 22 3c 4c 61 79 65 72 73 3e 22 0a 20 20 29 0a 28  "<Layers>".  ).(
0cb0: 73 63 72 69 70 74 2d 66 75 2d 6d 65 6e 75 2d 72  script-fu-menu-r
0cc0: 65 67 69 73 74 65 72 20 22 73 63 72 69 70 74 2d  egister "script-
0cd0: 66 75 2d 73 67 2d 63 6f 70 79 2d 6d 61 73 6b 2d  fu-sg-copy-mask-
0ce0: 66 72 6f 6d 2d 61 62 6f 76 65 22 0a 20 20 22 3c  from-above".  "<
0cf0: 49 6d 61 67 65 3e 2f 4c 61 79 65 72 2f 4d 61 73  Image>/Layer/Mas
0d00: 6b 2f 4d 6f 64 69 66 79 22 0a 20 20 29 0a        k/Modify".  ).