####################################################################################
##                                                                                ##
##  I N T E R S T E L L A R   C O N S O L E  A tool to correct Milky Way texture  ##
##                       Copyright (C) 2025- Misato Observatory, Wakayama, Japan  ##
##                                                                                ##
##  This software is available under the GPL (General Public License)             ##
##                                                                                ##
####################################################################################

# This is required to dump canvas widget
package require Img

# file name to read
set orig_file_name "milkyway.orig.png"
if { 1 < $argc } {
  set orig_file_name [regsub -all {\\} [lindex $argv 0] {/}]
  set multiply_filter_file_name [regsub -all {\\} [lindex $argv 1] {/}]
} else {
  tk_messageBox -type ok -icon info -title "ERROR" -message "Missing args!!"
  exit
}

# size of image (size will be rescaled to 2x)
set width  1024
set height 512

#
# Parameters "bottom,top,curve" for color corrections using +level option of
# ImageMagick.  See https://usage.imagemagick.org/color_mods/#level
#
# orig -(1)-> _tmp_color_img.tiff ---+
#                                 (lighten)-> _tmp_composite_img.tiff -(3)-> _tmp_result_img.tiff
# orig -(2)-> _tmp_mono_img.tiff ----+
#

# (1) for _tmp_color_img.tiff

# v1 (2026/4/27)
#set level_bright_r "0%,90%,0.54"
#set level_bright_g "4%,80%,0.64"
#set level_bright_b "0%,80%,0.89"

# v2 (2026/6/10)
# Brightness,Saturation,Hue
#set pre_modulate   "116,100,100"
set pre_modulate   "133,100,100"
#
set level_bright_r "0%,86%,0.50"
set level_bright_g "2%,81%,0.65"
set level_bright_b "0%,86%,0.85"
#set level_bright_all "0%,100%,0.98"
set level_bright_all "0%,110%,1.08"

# (2) for _tmp_mono_img.tiff
# v1 for v24.4
#set level_dark     "0%,50%,0.35"
# v1 for v25.4
#set level_dark     "0%,48%,0.38"
# v2
set level_dark     "0%,57%,0.44"

# (3) for _tmp_result_img.tiff
# v1
#set level_all      "-1%,100%,1.0"
# v2 (3rd param is set in save_an_image proc)
set level_all      "-10%,100%"
# Brightness,Saturation,Hue
set modulate       "100,80,100"

#
# parameters for adjustment of local brightness
# (See the result: filter_data.tiff)
#
# _tmp_result_img.tiff -------+
#                        (multiply)-> milkyway.16bit.tiff  -(strip,8bit)-> milkyway.png
# (4)(5)-> filter_data.tiff --+
#

# (4) section-0 : Winter part

# filtering depth
# (max:1.0 disabled:0.0)
# v1
#set section0_depth 0.3
# v2
set section0_depth 0.4

# start posision of filtering
set section0_x_begin 632

# width of first gradient
set section0_width_grad0 340
# width of constant area
set section0_width_const 20
# width of last gradient
set section0_width_grad1 140

# (5) section-1 : Summer-Autumn part

# v1
#set section1_depth 0.0
# v2
set section1_depth 0.1

set section1_x_begin 235
set section1_width_grad0 110
set section1_width_const 90
set section1_width_grad1 60


####  P R O C E D U R E S  ####

proc save_an_image { param_curve } {

  global orig_file_name
  global multiply_filter_file_name
  global level_bright_r
  global level_bright_g
  global level_bright_b
  global level_bright_all
  global level_dark
  global level_all
  global pre_modulate
  global modulate

  wm title . "Generating param_curve = $param_curve"
  update

  # dump .c0
  image create photo src -data .c0
  image create photo dest
  dest copy src -zoom 2
  dest write _tmp_filter_data.png -format png

  if { [catch {exec -ignorestderr magick _tmp_filter_data.png -strip -background '#000000' -alpha remove -alpha off -depth 16 -type truecolor -compress LZW filter_data.tiff} result] } {
    tk_messageBox -type ok -icon info -title "ERROR" -message "Cannot exec magick!!"
  } else {
    file delete -force _tmp_filter_data.png
  }

  if { [catch {exec -ignorestderr magick $orig_file_name -strip -background '#000000' -alpha remove -alpha off -depth 16 -type truecolor -compress LZW _tmp_orig.tiff} result] } {
    tk_messageBox -type ok -icon info -title "ERROR" -message "Cannot exec magick!!"
  }

  if { [catch {exec -ignorestderr magick $multiply_filter_file_name -strip -background '#000000' -alpha remove -alpha off -depth 16 -type truecolor -compress LZW _tmp_multiply_filter.tiff} result] } {
    tk_messageBox -type ok -icon info -title "ERROR" -message "Cannot exec magick!!"
  }

  # Multiply Composition
  if { [catch {exec -ignorestderr magick composite -compose multiply _tmp_orig.tiff _tmp_multiply_filter.tiff -depth 16 -type truecolor -compress LZW _tmp0_orig_filtered.tiff} result] } {
    tk_messageBox -type ok -icon info -title "ERROR" -message "Cannot exec magick!!"
  }

  if { [catch {exec -ignorestderr magick _tmp0_orig_filtered.tiff -modulate $pre_modulate -depth 16 -type truecolor -compress LZW _tmp_orig_filtered.tiff} result] } {
    tk_messageBox -type ok -icon info -title "ERROR" -message "Cannot exec magick!!"
  }

  # Create color bright part
  if { [catch {exec -ignorestderr magick _tmp_orig_filtered.tiff -channel R +level $level_bright_r -channel G +level $level_bright_g -channel B +level $level_bright_b -depth 16 -type truecolor -compress LZW _tmp0_color_img.tiff} result] } {
    tk_messageBox -type ok -icon info -title "ERROR" -message "Cannot exec magick!!"
  }

  if { [catch {exec -ignorestderr magick _tmp0_color_img.tiff -modulate 100,100,100 +level $level_bright_all -depth 16 -type truecolor -compress LZW _tmp_color_img.tiff} result] } {
    tk_messageBox -type ok -icon info -title "ERROR" -message "Cannot exec magick!!"
  }

  # Create gray dark part
  if { [catch {exec -ignorestderr magick _tmp_orig_filtered.tiff -modulate 100,1 +level $level_dark -depth 16 -type truecolor -compress LZW _tmp_mono_img.tiff} result] } {
    tk_messageBox -type ok -icon info -title "ERROR" -message "Cannot exec magick!!"
  }

  # Lighten Composition
  if { [catch {exec -ignorestderr magick composite -compose lighten _tmp_mono_img.tiff _tmp_color_img.tiff -depth 16 -type truecolor -compress LZW _tmp_composite_img.tiff} result] } {
    tk_messageBox -type ok -icon info -title "ERROR" -message "Cannot exec magick!!"
  }

  # Adjust composite image
  if { [catch {exec -ignorestderr magick _tmp_composite_img.tiff +level ${level_all},${param_curve} -modulate $modulate -depth 16 -type truecolor -compress LZW _tmp_result_img.tiff} result] } {
    tk_messageBox -type ok -icon info -title "ERROR" -message "Cannot exec magick!!"
  }

  # Composite using "multiply" mode
  if { [catch {exec -ignorestderr magick composite -compose multiply _tmp_result_img.tiff filter_data.tiff -depth 16 -type truecolor -compress LZW milkyway.16bit.tiff} result] } {
    tk_messageBox -type ok -icon info -title "ERROR" -message "Cannot exec magick!!"
  }

  # Strip
  if { [catch {exec -ignorestderr magick milkyway.16bit.tiff -strip -define png:color-type=2 -define png:bit-depth=8 milkyway.${param_curve}.png} result] } {
    tk_messageBox -type ok -icon info -title "ERROR" -message "Cannot exec magick!!"
  }

  #set im [image create photo -data .c0]
  #$im write filter_data.png -format png

  #image create photo .photo
  #.photo configure -format window -data $c
  #.photo write filter_data.png -format gif

  #exit

}

proc save_my_image {} {

  save_an_image 0.80
  save_an_image 0.85
  save_an_image 0.95
  save_an_image 1.00
  save_an_image 1.05
  save_an_image 1.10
  save_an_image 0.90

  #file copy -force milkyway.0.90.png milkyway.png

  tk_messageBox -type ok -icon info -title "OK" -message "Processed and saved milkyway.x.xx.png"

  exit

}

####  M A I N  ####

wm title . "ESC: exit  Enter: Save and Exit"

# ウィンドゥ属性の設定
wm resizable . 0 0

#wm overrideredirect . 1
#wm attributes . -topmost yes
wm geometry . ${width}x${height}
#wm minsize . $width $height
#wm maxsize . $width $height
#wm attributes . -alpha "1"
#wm attributes . -transparentcolor black

option add *background "#ffffff"
option add *foreground "#000000"
option add *highlightThickness "0"
option add *borderWidth "0"

canvas .c0 -width $width -height $height
pack .c0

#bind  . <Key-space> "wm overrideredirect . 0"
#bind  . <Return> "wm overrideredirect . 1"
bind  . <Escape> "exit"
bind  . <KeyPress-q> "exit"
#bind  . <KeyPress-s> "save_my_image"
bind  . <Return> "save_my_image"

#

set x 0
set rgb "#ffffff"

#
# section-0
#

# start pos
set x0 $section0_x_begin
# width of first gradient
set i_len $section0_width_grad0
for {set i 0} {$i < $i_len} {incr i} {
  set x [expr ($x0 + $i) % $width]
  set id [.c0 create line $x 0 $x $height]
  set v [expr round(255.0 - 255.0 * $section0_depth * (1.0 * $i / $i_len))]
  set rgb [format "#%02x%02x%02x" $v $v $v]
  .c0 itemconfigure $id -fill $rgb
}

set x0 $x
# width of constant area
set i_len $section0_width_const
for {set i 0} {$i < $i_len} {incr i} {
  set x [expr ($x0 + $i) % $width]
  set id [.c0 create line $x 0 $x $height]
  .c0 itemconfigure $id -fill $rgb
}

set x0 $x
# width of last gradient
set i_len $section0_width_grad1
for {set i 0} {$i < $i_len} {incr i} {
  set x [expr ($x0 + $i) % $width]
  set id [.c0 create line $x 0 $x $height]
  set v [expr round(255.0 - 255.0 * $section0_depth * (1.0 * ($i_len - 1 - $i) / $i_len))]
  set rgb [format "#%02x%02x%02x" $v $v $v]
  .c0 itemconfigure $id -fill $rgb
}

#
# section-1
#

# start pos
set x0 $section1_x_begin
# width of first gradient
set i_len $section1_width_grad0
for {set i 0} {$i < $i_len} {incr i} {
  set x [expr ($x0 + $i) % $width]
  set id [.c0 create line $x 0 $x $height]
  set v [expr round(255.0 - 255.0 * $section1_depth * (1.0 * $i / $i_len))]
  set rgb [format "#%02x%02x%02x" $v $v $v]
  .c0 itemconfigure $id -fill $rgb
}

set x0 $x
# width of constant area
set i_len $section1_width_const
for {set i 0} {$i < $i_len} {incr i} {
  set x [expr ($x0 + $i) % $width]
  set id [.c0 create line $x 0 $x $height]
  .c0 itemconfigure $id -fill $rgb
}

set x0 $x
# width of last gradient
set i_len $section1_width_grad1
for {set i 0} {$i < $i_len} {incr i} {
  set x [expr ($x0 + $i) % $width]
  set id [.c0 create line $x 0 $x $height]
  set v [expr round(255.0 - 255.0 * $section1_depth * (1.0 * ($i_len - 1 - $i) / $i_len))]
  set rgb [format "#%02x%02x%02x" $v $v $v]
  .c0 itemconfigure $id -fill $rgb
}


update

