; The GIMP -- an image manipulation program
; Copyright (C) 1995 Spencer Kimball and Peter Mattis
; 
; White/Black balance script  for GIMP 1.2
; Copyright (C) 2002 Iccii <iccii@hotmail.com>
; 
; This script was born of Das's idea in this BBS.
; His script enable you to specify gray point using curve in your picture.
; http://airmail.sakura.ne.jp/~komugi/gimp/bbs/treebbs.cgi?log=1963
; 
; --------------------------------------------------------------------
; version 0.1  by Iccii 2002/01/21
;     - Initial relase
;
; --------------------------------------------------------------------
; 
; 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.
;

(define (script-color-wb-balance
			image
			drawable
			target
			target-color
	)

  (define (floor x) (- x (fmod x 1)))

  (define (curve-point channel i)
    (if (= target 0)
        (max (min (floor (* (/ 255        channel)             i))  255) 0)
        (max (min (floor (* (/ 255 (- 255 channel)) (- i channel))) 255) 0)))

  (let* ((target (cond ((equal? target-color '(255 255 255)) 0)	; White
                       ((equal? target-color '(  0   0   0)) 1)	; Black
                       ((equal? target-color '(127 127 127)) 2)	; Gray
                 ))
         (color (car (gimp-palette-get-foreground)))
         (red   (car color))
         (green (cadr color))
         (blue  (caddr color))
         (i 0)
         (num_bytes 256)
         (red-curve   (cons-array num_bytes 'byte))
         (green-curve (cons-array num_bytes 'byte))
         (blue-curve  (cons-array num_bytes 'byte)))

    (gimp-undo-push-group-start image)

    (while (< i num_bytes)
      (aset red-curve   i (curve-point red i))
      (aset green-curve i (curve-point green i))
      (aset blue-curve  i (curve-point blue i))
      (set! i (+ i 1)))

    (gimp-curves-explicit drawable RED-LUT num_bytes red-curve)
    (gimp-curves-explicit drawable GREEN-LUT num_bytes green-curve)
    (gimp-curves-explicit drawable BLUE-LUT num_bytes blue-curve)

    (gimp-undo-push-group-end image)
    (gimp-displays-flush)
))

(script-fu-register
  "script-color-wb-balance"
  "<Image>/Script-Fu/Color/White-Black Balance"
  "make FG color to white or black"
  "Iccii <iccii@hotmail.com>"
  "Iccii"
  "2002, Jan"
  "RGB*"
  SF-IMAGE    "Image"         0
  SF-DRAWABLE "Drawable"      0
  SF-OPTION   "Target Color"  '("White" "Black")
  SF-COLOR    "Regard Foreground Color as This Color" '(255 255 255)
)
