home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 February / PCWorld_2001-02_cd.bin / Software / Topware / gimp / gimp-setup-20001226.exe / Main / slide.scm < prev    next >
Encoding:
GIMP Script-Fu Script  |  2000-12-27  |  7.0 KB  |  241 lines

  1. ; The GIMP -- an image manipulation program
  2. ; Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3. ; This program is free software; you can redistribute it and/or modify
  4. ; it under the terms of the GNU General Public License as published by
  5. ; the Free Software Foundation; either version 2 of the License, or
  6. ; (at your option) any later version.
  7. ; This program is distributed in the hope that it will be useful,
  8. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10. ; GNU General Public License for more details.
  11. ; You should have received a copy of the GNU General Public License
  12. ; along with this program; if not, write to the Free Software
  13. ; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. ;
  15. ;
  16. ; slide.scm   version 0.40   1999/5/11
  17. ;
  18. ; CHANGE-LOG:
  19. ; 0.20 - first public release
  20. ; 0.30 - some code cleanup
  21. ;        now uses the rotate plug-in to improve speed
  22. ; 0.40 - changes to work with gimp-1.1 
  23. ;        if the image was rotated, rotate the whole thing back when finished
  24. ;
  25. ; !still in development!
  26. ; TODO: - change the script so that the film is rotated, not the image
  27. ;       - antialiasing
  28. ;       - make 'add background' an option
  29. ;       - ?
  30. ;
  31. ; Copyright (C) 1997-1999 Sven Neumann <sven@gimp.org>
  32. ;  
  33. ; makes your picture look like a slide
  34. ;
  35. ; The script works on RGB and grayscale images that contain only 
  36. ; one layer. The image is cropped to fit into an aspect ratio of 1:1,5.
  37. ; It creates a copy of the image or can optionally work on the original. 
  38. ; The script uses the current background color to create a background 
  39. ; layer.
  40.  
  41.  
  42. (define (crop width height ratio)
  43.   (cond ((>= width (* ratio height)) (* ratio height))
  44.     ((< width (* ratio height))   width)))
  45.  
  46.  
  47. (define (script-fu-slide img
  48.              drawable
  49.              text
  50.              number
  51.              fontname
  52.              font-color
  53.              work-on-copy)
  54.   (let* ((type (car (gimp-drawable-type-with-alpha drawable)))
  55.      (image (cond ((= work-on-copy TRUE)
  56.                (car (gimp-channel-ops-duplicate img)))
  57.               ((= work-on-copy FALSE)
  58.                img)))
  59.      (owidth (car (gimp-image-width image)))
  60.      (oheight (car (gimp-image-height image)))
  61.      (ratio (if (>= owidth oheight) (/ 3 2)
  62.                                 (/ 2 3)))
  63.      (crop-width (crop owidth oheight ratio))
  64.      (crop-height (/ crop-width ratio))
  65.      (width (* (max crop-width crop-height) 1.05))
  66.      (height (* (min crop-width crop-height) 1.5))
  67.      (hole-width (/ width 20))
  68.      (hole-space (/ width 8))
  69.      (hole-height (/ width 12))
  70.      (hole-radius (/ hole-width 4))
  71.      (hole-start (- (/ (rand 1000) 1000) 0.5))
  72.      (old-bg (car (gimp-palette-get-background)))
  73.      (old-fg (car (gimp-palette-get-foreground)))
  74.      (film-layer (car (gimp-layer-new image
  75.                       width
  76.                       height
  77.                       type
  78.                       "Film"
  79.                       100
  80.                       NORMAL)))
  81.      (bg-layer (car (gimp-layer-new image
  82.                     width
  83.                     height
  84.                     type
  85.                     "Background"
  86.                     100
  87.                     NORMAL)))
  88.      (pic-layer (car (gimp-image-active-drawable image)))
  89.      (numbera (string-append number "A")))
  90.  
  91.  
  92.   (gimp-image-undo-disable image)
  93.  
  94. ; add an alpha channel to the image
  95.   (gimp-layer-add-alpha pic-layer)
  96.  
  97. ; crop, resize and eventually rotate the image 
  98.   (gimp-crop image
  99.          crop-width
  100.          crop-height
  101.          (/ (- owidth crop-width) 2)
  102.          (/ (- oheight crop-height) 2))
  103.   (gimp-image-resize image
  104.              width
  105.              height
  106.              (/ (- width crop-width) 2)
  107.              (/ (- height crop-height) 2))
  108.   (if (< ratio 1) (plug-in-rotate 1
  109.                   image
  110.                   pic-layer
  111.                   1
  112.                   FALSE))
  113.  
  114. ; add the background layer
  115.   (gimp-drawable-fill bg-layer BG-IMAGE-FILL)
  116.   (gimp-image-add-layer image bg-layer -1)
  117.  
  118. ; add the film layer
  119.   (gimp-palette-set-background '(0 0 0))
  120.   (gimp-drawable-fill film-layer BG-IMAGE-FILL)
  121.  
  122. ; add the text
  123.   (gimp-palette-set-foreground font-color)
  124.   (gimp-floating-sel-anchor (car (gimp-text-fontname image
  125.                         film-layer
  126.                         (+ hole-start (* -0.25 width))
  127.                         (* 0.01 height)
  128.                         text
  129.                         0
  130.                         TRUE
  131.                         (* 0.040 height) PIXELS fontname)))
  132.   (gimp-floating-sel-anchor (car (gimp-text-fontname image
  133.                         film-layer
  134.                         (+ hole-start (* 0.75 width))
  135.                         (* 0.01 height)
  136.                         text
  137.                         0
  138.                         TRUE
  139.                         (* 0.040 height) PIXELS
  140.                         fontname )))
  141.   (gimp-floating-sel-anchor (car (gimp-text-fontname image
  142.                         film-layer
  143.                         (+ hole-start (* 0.35 width))
  144.                         (* 0.01 height)
  145.                         number
  146.                         0
  147.                         TRUE
  148.                         (* 0.050 height) PIXELS
  149.                         fontname )))
  150.   (gimp-floating-sel-anchor (car (gimp-text-fontname image
  151.                         film-layer
  152.                         (+ hole-start (* 0.35 width))
  153.                         (* 0.95 height)
  154.                         number
  155.                         0
  156.                         TRUE
  157.                         (* 0.050 height) PIXELS
  158.                         fontname )))
  159.   (gimp-floating-sel-anchor (car (gimp-text-fontname image
  160.                           film-layer
  161.                           (+ hole-start (* 0.85 width))
  162.                           (* 0.95 height)
  163.                           numbera
  164.                           0
  165.                           TRUE
  166.                           (* 0.045 height) PIXELS
  167.                           fontname )))
  168.  
  169. ; create a mask for the holes and cut them out
  170.   (let* ((film-mask (car (gimp-layer-create-mask film-layer WHITE-MASK)))
  171.      (hole hole-start)
  172.      (top-y (* height 0.06))
  173.      (bottom-y(* height 0.855)))
  174.     (gimp-selection-none image)
  175.     (while (< hole 8)
  176.        (gimp-rect-select image
  177.                  (* hole-space hole)
  178.                  top-y
  179.                  hole-width
  180.                  hole-height
  181.                  ADD
  182.                  FALSE
  183.                  0)
  184.        (gimp-rect-select image
  185.                  (* hole-space hole)
  186.                  bottom-y
  187.                  hole-width
  188.                  hole-height
  189.                  ADD
  190.                  FALSE
  191.                  0)
  192.        (set! hole (+ hole 1)))
  193.  
  194.     (gimp-palette-set-foreground '(0 0 0))
  195.     (gimp-edit-fill film-mask BG-IMAGE-FILL)
  196.     (gimp-selection-none image)
  197.     (plug-in-gauss-rle 1 image film-mask hole-radius TRUE TRUE)
  198.     (gimp-threshold film-mask 127 255)
  199.  
  200.     (gimp-image-add-layer image film-layer -1)
  201.     (gimp-image-add-layer-mask image film-layer film-mask)
  202.     (gimp-image-remove-layer-mask image film-layer APPLY))
  203.  
  204. ; reorder the layers
  205.   (gimp-image-raise-layer image pic-layer)
  206.   (gimp-image-raise-layer image pic-layer)
  207.  
  208. ; eventually rotate the whole thing back
  209.   (if (< ratio 1) 
  210.       (plug-in-rotate 1
  211.               image
  212.               pic-layer
  213.               3
  214.               TRUE))
  215.  
  216. ; clean up after the script
  217.   (gimp-selection-none image)
  218.   (gimp-palette-set-background old-bg)
  219.   (gimp-palette-set-foreground old-fg)
  220.   (gimp-image-undo-enable image)
  221.   (if (= work-on-copy TRUE) (gimp-display-new image))
  222.   (gimp-displays-flush)))
  223.  
  224. (script-fu-register "script-fu-slide"
  225.             _"<Image>/Script-Fu/Decor/Slide..."
  226.             "Gives the image the look of a slide"
  227.             "Sven Neumann <sven@gimp.org>"
  228.             "Sven Neumann"
  229.             "1999/05/11"
  230.             "RGB GRAY"
  231.             SF-IMAGE "Image" 0
  232.             SF-DRAWABLE "Drawable" 0
  233.             SF-STRING _"Text" "The GIMP"
  234.             SF-STRING _"Number" "32"
  235.             SF-FONT   _"Font" "-*-utopia-*-r-*-*-24-*-*-*-p-*-*-*"
  236.             SF-COLOR  _"Font Color" '(255 180 0)
  237.             SF-TOGGLE _"Work on Copy" TRUE)
  238.