; The GIMP -- an image manipulation program
; Copyright (C) 1995 Spencer Kimball and Peter Mattis
; 
; Make a CD label shape.
; Copyright (C) 2002 by Akkana Peck, akkana@shallowsky.com.
; 
; 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.
; 
; You should have received a copy of the GNU General Public License
; along with this program; if not, write to the Free Software
; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

;; Utility to calculate the inner radius
(define (inner-diam diameter mini)
  (if (= mini TRUE) (/ diameter 2.2) (/ diameter 3.1)))

;; Select the CD shape.  Then you can cut, or whatever.
(define (CD-select img diameter mini)
    (gimp-ellipse-select img 0 0 diameter diameter CHANNEL-OP-REPLACE TRUE FALSE 0)
    (let* (
	   (inner (inner-diam diameter mini))
	   (offset (/ (- diameter inner) 2))
	   )
      (gimp-ellipse-select img offset offset inner inner CHANNEL-OP-SUBTRACT TRUE FALSE 0)
      ))

;; Actually make a CD shape, in a solid color.
(define (script-fu-CD-label diameter color mini)
  (let* ((old-fg-color (car (gimp-palette-get-foreground)))
	 (img (car (gimp-image-new diameter diameter RGB)))
	 (cdlayer (car (gimp-layer-new img diameter diameter
				       RGBA-IMAGE "CD" 100 NORMAL-MODE))))
    (gimp-image-undo-disable img)
    (gimp-image-add-layer img cdlayer -1)
    (gimp-selection-all img)
    (gimp-edit-clear cdlayer)

    (gimp-palette-set-foreground color)
    (CD-select img diameter mini)
    (gimp-edit-bucket-fill cdlayer FG-BUCKET-FILL NORMAL-MODE 100 0 FALSE 0 0)

    ;; Clean up
    (gimp-selection-none img)
    (gimp-image-set-active-layer img cdlayer)
    (gimp-palette-set-foreground old-fg-color)
    (gimp-image-undo-enable img)
    (gimp-display-new img)))

;; Cut out a CD shape from the current image.
(define (script-fu-CD-mask img drawable mini)
  (gimp-image-undo-group-start img)
  (CD-select img
	     (min (car (gimp-image-width img)) (car (gimp-image-height img)))
	     mini)
  (gimp-selection-invert img)
  (gimp-edit-clear drawable)
  (gimp-image-undo-group-end img)
  (gimp-displays-flush)
  )

(script-fu-register "script-fu-CD-label"
		    _"<Toolbox>/Xtns/Script-Fu/Misc/CD label..."
		    "CD label shape"
		    "Akkana Peck"
		    "Akkana Peck"
		    "December 2002"
		    ""
		    SF-ADJUSTMENT _"Diameter"      '(800 1 2000 10 50 0 1)
		    SF-COLOR      _"Color"         '(170 240 240)
		    SF-TOGGLE     _"Mini CD"       FALSE)

(script-fu-register "script-fu-CD-mask"
		    _"<Image>/Script-Fu/Utils/CD mask..."
		    "Select a CD label shape out of the current layer"
		    "Akkana Peck"
		    "Akkana Peck"
		    "December 2002"
		    "RGB* GRAY* INDEXED*"
		    SF-IMAGE      "Image"        0
		    SF-DRAWABLE "Drawable" 0
		    SF-TOGGLE     _"Mini CD"     FALSE)