; Fire animation script for The GIMP
; Version 0.2
; Copyright (c) 1998
; Vidar Madsen <vidar@prosalg.no>
;
; --- Introduction ---
; Basically, it takes an image (with an alpha-channel), and sets
; it on fire. ;-) The user can specify how many frames should be
; used, and which gradient to use to color the flames. The defaults
; ought to work nicely most of the time, though.
;
; --- ChangeLog ---
; 5. Aug 1998: v0.2 - Made the script create a new image
; 4. Aug 1998:      - Uses 'plug-in-spread' instead of randomize
; 4. Aug 1998: v0.1 - First public release!
;
; --- Disclaimer ---
; 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-fireanim-doit image drawable mask)
  (plug-in-spread 1 image drawable 1 1)
  (gimp-levels image drawable 0 1 255 1.0 0 242)
  (gimp-selection-layer-alpha image mask)
  (plug-in-noisify 1 image drawable FALSE 2.0 2.0 2.0 0.0)
  (gimp-selection-grow image 1)
  (plug-in-noisify 1 image drawable FALSE 0.3 0.3 0.3 0.0)
  (gimp-selection-grow image 2)
  (plug-in-gauss-rle TRUE image drawable 1.0 TRUE TRUE)
  (gimp-selection-none image)
  (gimp-channel-ops-offset image drawable TRUE 0 0 -2)
  (gimp-selection-layer-alpha image mask)
  (gimp-palette-set-background '(255 255 255))
  (gimp-edit-fill image drawable)
  (gimp-selection-none image)
)

(define (script-fu-fireanim-prep image drawable orig gradient)
  (gimp-levels image drawable 0 1 200 1.0 0 255)
  (gimp-gradients-set-active gradient)
  (plug-in-gradmap TRUE image drawable)
  (gimp-selection-all image)
  (gimp-selection-layer-alpha image orig)
  (gimp-edit-copy image orig)
  (gimp-floating-sel-anchor (car (gimp-edit-paste image drawable FALSE)))
  (gimp-selection-none image)
)

(define (script-fu-fireanim origimage origdrawable nframes oframes pframes gradient framerate)
  (set! width (car (gimp-image-width origimage)))
  (set! height (car (gimp-image-height origimage)))

  (set! image (car (gimp-channel-ops-duplicate origimage)))
  (set! drawable (car (gimp-image-get-active-layer image)))

  (gimp-image-disable-undo image)

  (set! frame (car (gimp-layer-new image width height RGBA_IMAGE "frame" 100 NORMAL)))

  (gimp-palette-set-background '(0 0 0))
  (gimp-selection-all image)
  (gimp-edit-fill image frame)

  (gimp-palette-set-background '(255 255 255))
  (gimp-selection-layer-alpha image drawable)
  (gimp-edit-fill image frame)

  (set! count 0)
  (while (< count pframes)
	(script-fu-fireanim-doit image frame drawable)
	(set! count (+ count 1))
  )

  (set! larray (cons-array (+ nframes oframes 1)))
  (set! count 1)
  (while (<= count (+ nframes oframes))
	(script-fu-fireanim-doit image frame drawable)
	(set! newlayer (car (gimp-layer-copy frame TRUE)))
	(aset larray count newlayer)
	(script-fu-fireanim-prep image newlayer drawable gradient)
	(gimp-layer-set-name newlayer
		(string-append "Frame " (number->string count) " (" (number->string framerate) "ms) (replace)"))
; "(" framerate ") (replace)"))
	(gimp-image-add-layer image newlayer 0)
	(gimp-layer-set-visible newlayer FALSE)
	(set! count (+ count 1))
  )

  (gimp-layer-set-visible drawable FALSE)

  (set! count 1)
  (while (<= count oframes)
	(set! opac (* 100 (- 1.0 (/ count (+ oframes 1)))))
	(set! framea (aref larray count))
	(set! frameb (aref larray (+ count nframes)))
	(gimp-layer-set-visible framea TRUE)
	(gimp-layer-set-visible frameb TRUE)
	(gimp-layer-set-opacity frameb opac)
	(set! framea (car (gimp-image-merge-visible-layers image 2)))
	(aset larray count framea)
	(gimp-layer-set-visible framea FALSE)
	(set! count (+ count 1))
  )

  (set! count 1)
  (while (<= count nframes)
	(set! framea (aref larray count))
	(gimp-layer-set-visible framea TRUE)
	(set! count (+ count 1))
  )

  (gimp-image-set-active-layer image framea)
  (gimp-image-remove-layer image drawable)

  (gimp-display-new image)

  (gimp-selection-none image)
  (gimp-image-enable-undo image)
  (gimp-displays-flush)
)

(script-fu-register
	"script-fu-fireanim"
	"<Image>/Script-Fu/Vidar/Fireanim"
	"Animasjonstest"
	"Vidar Madsen <vidar@prosalg.no>"
	"Vidar Madsen"
	"Aug 1 1998"
	"RGBA"
	SF-IMAGE "Input Image" 0
	SF-DRAWABLE "Input Drawable" 0
	SF-VALUE "# of frames" "15"
	SF-VALUE "Overlap frames" "6"
	SF-VALUE "Prep frames" "15"
	SF-VALUE "Gradient" "\"Incandescent\""	
	SF-VALUE "Framerate (ms)" "50"
)
