; 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-indexed-color-cube orig-image orig-drawable)
 
  (define (plot-xy drawable color)
    (let* (
        (x 0)
        (y 0)
;        (cos-scaled (* 0.707 (cos (* 45 (/ *pi* 180))))) == 0.5
;        (sin-scaled (* 0.707 (sin (* 45 (/ *pi* 180))))) == 0.5
      )
      (set! x (+ (vector-ref color 0) (* (vector-ref color 2) 0.5)))
      (set! y (+ (vector-ref color 1) (* (vector-ref color 2) 0.5)))
      (set! x (+ x 50))
      (set! y (- 450 y))
      (gimp-drawable-set-pixel drawable x y 4 color)
      )
    )
  (let* (
      (image 0)
      (display 0)
      (orig-width (car (gimp-drawable-width orig-drawable)))
      (orig-height (car (gimp-drawable-height orig-drawable)))
      (width 500)
      (height 500)
      (max-wh (max width height))
      (x 0)
      (y 0)
      (layer 0)
      (bg 0)
      (bg-axes 0)
      (fg-axes 0)
      (i 0)
      (orig-selection 0)
      (orig-bg (car (gimp-context-get-background)))
      )
    (gimp-image-undo-group-start orig-image)
    (gimp-layer-add-alpha orig-drawable)
    (set! image (car (gimp-image-new width height RGB)))
    (gimp-image-undo-disable image)
    (set! display (car (gimp-display-new image)))
    (set! bg (car (gimp-layer-new image width height RGB-IMAGE "Background" 100 NORMAL-MODE)))
    (gimp-context-set-background '(127 127 127))
    (gimp-drawable-fill bg BACKGROUND-FILL)
    (gimp-image-add-layer image bg 0)
    (gimp-context-set-background orig-bg)

    (set! bg-axes (car (gimp-layer-new image width height RGBA-IMAGE "bg-axes" 100 NORMAL-MODE)))
    (gimp-drawable-fill bg-axes TRANSPARENT-FILL)
    (gimp-image-add-layer image bg-axes 0)

    (set! layer (car (gimp-layer-new image width height RGBA-IMAGE "Color Cube" 100 NORMAL-MODE)))
    (gimp-drawable-fill layer TRANSPARENT-FILL)
    (gimp-image-add-layer image layer 0)

    (set! fg-axes (car (gimp-layer-new image width height RGBA-IMAGE "fg-axes" 100 NORMAL-MODE)))
    (gimp-drawable-fill fg-axes TRANSPARENT-FILL)
    (gimp-image-add-layer image fg-axes 0)

    (set! i 255)
    (while (>= i 0)
      (plot-xy bg-axes (vector i 0 0 255))
      (plot-xy bg-axes (vector 0 i 0 255))
      (plot-xy bg-axes (vector 0 0 i 255))
      (plot-xy bg-axes (vector i   255 255 255))
      (plot-xy bg-axes (vector 255 i   255 255))
      (plot-xy bg-axes (vector i   0   255 255))
      (plot-xy bg-axes (vector 0   i   255 255))
      (plot-xy bg-axes (vector 0   255 i   255))
      (plot-xy bg-axes (vector 255 0   i   255))
      (plot-xy fg-axes (vector i   255 0   255))
      (plot-xy fg-axes (vector 255 i   0   255))
      (plot-xy fg-axes (vector 255 255 i   255))
      (set! i (- i 1))
      )
    (gimp-drawable-update bg-axes 0 0 width height)
    (gimp-drawable-update fg-axes 0 0 width height)
    (gimp-displays-flush)
    (set! x 0)
    (gimp-progress-init "Color Cubing" display)
    (while (< x orig-width)
      (set! y 0)
      (while (< y orig-height)
        (plot-xy layer (cadr (gimp-drawable-get-pixel orig-drawable x y)))
        (set! y (+ y 1))
        )
      (gimp-progress-update (/ x orig-width))
      (set! x (+ x 1))
      )
    (gimp-progress-end)
    (gimp-drawable-update layer 0 0 width height)
    (gimp-image-undo-enable image)
    (gimp-image-undo-group-end orig-image)
    (gimp-displays-flush)
    )
  )

(script-fu-register
  "script-fu-indexed-color-cube"
  "<Image>/Image/Color Cube"
  "Create a 3-D plot of color data for an Indexed image"
  "Saul Goode"
  "Saul Goode"
  "April 2008"
  "RGB*"
  SF-IMAGE "" 0
  SF-DRAWABLE "" 0
  )
