NB. Written by Cliff Reiter
NB. Lafayette College
NB. Last update July 13, 1999
NB. 
NB. Allows palette editing of images which are 8 bit 
NB. uncompressed windows bitmaps.


RGBED=: 0 : 0
pc rgbed;
xywh 190 12 34 12;cc ok button;cn "OK";
xywh 190 39 34 12;cc cancel button;cn "Cancel";
xywh 6 6 50 50;cc colsq isigraph;
xywh 60 4 100 12;cc rsb scrollbar;
xywh 60 25 100 12;cc gsb scrollbar;
xywh 60 46 100 12;cc bsb scrollbar;
xywh 165 4 20 12;cc rs static;
xywh 165 25 20 12;cc gs static;
xywh 165 46 20 12;cc bs static;
pas 6 6;pcenter;
rem form end;
)

rgbed_run=: 3 : 0
rgb=.3{.".y.,' 0 0 0'
rsb=.":0{rgb
gsb=.":1{rgb
bsb=.":2{rgb
wd RGBED
NB. initialize form here
wd 'set rsb 0 0 255 16'
wd 'set gsb 0 0 255 16'
wd 'set bsb 0 0 255 16'
require 'gl2'
wd 'set rsb ',rsb
wd 'set gsb ',gsb
wd 'set bsb ',bsb
wd 'set rs * ',rsb
wd 'set gs * ',gsb
wd 'set bs * ',bsb
paint ".rsb,' ',gsb,' ',bsb
)

paint=:3 : 0
glsel colsq
glrgb y.
glbrush ''
glpolygon ,1000*#:0 1 3 2
glshow ''
wd 'pshow;'
)

getrgb=:3 : 0
".rsb,' ',gsb,' ',bsb
)

rgbed_close=: 3 : 0
wd 'pclose'
)

rgbed_cancel_button=: 3 : 0
rgbed_close''
)


rgbed_rsb_button=: 3 : 0
wd 'set rs * ',rsb
paint getrgb ''
)

rgbed_gsb_button=: 3 : 0
wd 'set gs * ',gsb
paint getrgb ''
)

rgbed_bsb_button=: 3 : 0
wd 'set bs * ',bsb
paint getrgb ''
)

rgbed_ok_button=: 3 : 0
pa_hist=:pa,pa_hist
pa=:(getrgb '')(pal_pos sysdata)}pa
wd 'psel paledit'
display_pal ''
wd 'psel rgbed'
wd 'pclose'
)

PALEDIT=: 0 : 0
pc paledit;
menupop "File";
menu open "&Open Bitmap" "Ctrl+O" "" "";
menu save "&Save" "Ctrl+S" "" "";
menu saveas "S&ave As" "Ctrl +A" "" "";
menu loadpal "Load &Palette from bmp" "" "" "";
menusep ;
menu exit "&Exit" "" "" "";
menupopz;
menupop "Help";
menu command "&Commands" "F1" "" "";
menu about "&About" "" "" "";
menupopz;
xywh 333 158 34 12;cc cancel button;cn "Exit";
xywh 178 5 150 150;cc pal isigraph;
xywh 4 4 150 150;cc pic isipicture;
xywh 333 97 34 11;cc copy button;cn "Copy RGB";
xywh 333 115 34 11;cc paste button;cn "Paste RGB";
xywh 333 56 34 11;cc saveas button;cn "Save as";
xywh 333 10 34 11;cc open button;cn "Open";
xywh 333 39 34 11;cc save button;cn "Save";
xywh 206 181 50 10;cc label static;cn "Action on Selection";
xywh 259 179 50 50;cc seltyp combolist;
xywh 159 5 11 150;cc verpos scrollbarv;
xywh 5 158 148 11;cc horpos scrollbar;
xywh 33 181 35 11;cc label static;cn "Magnification";
xywh 70 181 67 11;cc mag scrollbar;
xywh 251 158 62 14;cc phf button;cn "palette history FORWard";
xywh 186 158 62 14;cc phb button;cn "palette history BACK";
pas 6 6;pcenter;
rem form end;
)

paledit_run=: 3 : 0
wd PALEDIT
NB. initialize form here
wd 'set seltyp blend reverse inverse random2 random3 random4 random5;setselect seltyp 0; '
wd 'set verpos 0 0 99 10'
wd 'set horpos 0 0 99 10'
wd 'set mag 0 0 99 10'
require 'gl2'
if. 0<#y. do.
  fn=:y.
  loadbmp ''
  end.
wd 'pshow;'
)

paledit_close=: 3 : 0
wd'pclose'
)

paledit_cancel_button=: 3 : 0
paledit_close''
)

paledit_exit_button=: 3 : 0
paledit_close''
)

rawwritebmp8=: 3 : 0
:
('spal';'sbmp')=.$@>"0 x.
xsbmp=.sbmp+(i.2)*4|-sbmp
h=.524289 0,(*/xsbmp),0 0,2#spal=.0{spal
h=.(54+(4*spal)+*/xsbmp),0,(54+4*spal),40,(|.sbmp),h
head=. 'BM',,a.{~,|."1 (4#256)#:h
pal=. ,(0,"1~|."1 >{.x.){a.
bmp=. ,xsbmp{.>{:x.
(head,pal,bmp) 1!:2 <y.
)

NB. read 8-bit uncompressed *.bmp files with raw data
rawreadbmp8=: 3 : 0
head=.256#.|."1 a.i. 13 4$2}.54{.x=. 1!:1 <y.
pal=. |."(1) 3{."1 (m,4)$ a.i. 54}.(54+4*m=.11{head){.x
sbmp=.5 4{head
xsbmp=.sbmp+(i.2)*4|-sbmp
bmp=. sbmp{.xsbmp$(2{head)}.x
pal;bmp
)
gpolygon=: 3 : 0
glpolygon y.
:
glbrush glrgb x.
glpolygon y.
)

display_pal=:3 : 0
(pa;bit) rawwritebmp8 'temp_pal_edit.bmp'
wd 'set pic "temp_pal_edit.bmp"'
glsel pal
glclear ''
(16 16$256{.pa) gpolygon"1 (1000%16)*,/"2 (#:0 1 3 2)+"1 _"_ 1 (|.,"0~/])i.16
glpaint ''
glshow ''
wd 'pshow;'
)

paledit_open_button=: 3 : 0
try. 1+$fndir=.((i:&'\') {. ])fn catch. fndir=.'' end.
fn=:wd 'mbopen "Bitmap files" "',fndir,'" "*.bmp" "Bitmap(*.bmp)|*.bmp|All files(*.*)|*.*"'
wd 'pn ',fn,';'
loadbmp ''
)

paledit_loadpal_button=: 3 : 0
try. 1+$tempdir=.((i:&'\') {. ])tempfn catch. tempdir=.'' end.
tempfn=:wd 'mbopen "Bitmap files" "',tempdir,'" "*.bmp" "Bitmap(*.bmp)|*.bmp|All files(*.*)|*.*"'
if. 0<#tempfn do.
  pa_hist=:pa,pa_hist
  'pa temp'=:rawreadbmp8 tempfn
  pa=:256{.pa
  display_pal ''
  end.
)

paledit_phb_button=: 3 : 0
if. 0<#pa_hist do.
'pa pa_hist'=:({.pa_hist);}.pa_hist,pa
display_pal ''
end.
)

paledit_phf_button=: 3 : 0
if. 0<#pa_hist do.
'pa pa_hist'=:({:pa_hist);pa,}:pa_hist
display_pal ''
end.
)

loadbmp=:3 : 0
if. 0<#fn do.
  'pa bitmap'=:rawreadbmp8 fn
  pa=:256{.pa
  pa_hist=:i.0 256 3
  'M N'=:$bitmap
  updatebit ''
  display_pal ''
end.
)

updatebit=: 3 : 0
MAG=.".mag
in=.<.((i.%<:)300)*((300%99)*(99-MAG))+(MAG%99)*<:M<.N
HP=.<.((".horpos)%99)*N->:{:in
VP=.<.((".verpos)%99)*M->:{:in
bit=: (HP+in){"1 ((M-1)-|.VP+in){bitmap
)


paledit_mag_button=: 3 : 0
updatebit ''
display_pal ''
)

paledit_horpos_button=:paledit_mag_button
paledit_verpos_button=:paledit_mag_button

paledit_octrl_fkey=: paledit_open_button
paledit_actrl_fkey=: paledit_saveas_button
paledit_sctrl_fkey=: paledit_save_button
paledit_f1_fkey=: paledit_command_button

pal_pos=:3 : 0
16 16 #. |.({.,15&-@{:)<.16*(0 1&{ % 2 3&{)". y.
)

paledit_pal_mbldbl=:3 : 0
pp=.pal_pos sysdata
rgbed_run ":pp{pa
)

paledit_pal_mbldown=:3 : 0
if. 0={:".sysdata do.
    PP=:pal_pos sysdata
    display_pal ''
    glrgb 255-PP{pa
    glpen 3 0
    gllines ,(1000%16)*(#:2 1)+"1 <.16*(0 1&{ % 2 3&{)". sysdata
    gllines ,(1000%16)*(#:0 3)+"1 <.16*(0 1&{ % 2 3&{)". sysdata
    glrgb 0 0 0
    glpen 1 0
    glshow ''
  else.
  qq=.pal_pos sysdata
  if. qq<PP do. 'PP qq'=.qq,PP end.
  si=. PP+i.1+qq-PP      NB. selected indices
  pa_hist=:pa,pa_hist
  select. seltyp
    case. 'blend' do.
          rr=.<.0.5+(PP{pa)+"1((qq{pa)-PP{pa)*"1 0(i.%])qq-PP
          pa=:rr (}:si)}pa
    case. 'reverse' do.
          pa=:(|.si{pa)si}pa
    case. 'inverse' do.
          pa=:(255-si{pa)si}pa
    case. 'random2' do.
          pa=:(<.0.5+(((i.%<:)2);?2 3$256)pwlin (i.%<:)#si)si}pa
    case. 'random3' do.
          pa=:(<.0.5+(((i.%<:)3);?3 3$256)pwlin (i.%<:)#si)si}pa
    case. 'random4' do.
          pa=:(<.0.5+(((i.%<:)4);?4 3$256)pwlin (i.%<:)#si)si}pa
    case. 'random5' do.
          pa=:(<.0.5+(((i.%<:)5);?5 3$256)pwlin (i.%<:)#si)si}pa
    end.
  display_pal ''
end.
)

pwlin=: 3 : 0"_ 0
:
p=.>{.x.
c=.>{:x.
i=.i.&0 p<y.
((1-r),r=.(y.-~i{p)%-/(i-0 1){p)+/ .* (i- 0 1){c
)

paledit_copy_button=: 3 : 0
Clip=:(pal_pos sysdata){pa
)

paledit_paste_button=: 3 : 0
pa_hist=:pa,pa_hist
pa=:Clip(pal_pos sysdata)}pa
display_pal ''
)

paledit_saveas_button=: 3 : 0
try. 1+$fndir=.((i:&'\') {. ])fn catch. fndir=.'' end.
out=.wd 'mbsave "Bitmap files" "',fndir,'" "*.bmp" "Bitmap(*.bmp)|*.bmp|All files(*.*)|*.*"'
if. 0<#out do.
  fn=:out
  (pa;bitmap) rawwritebmp8 fn
  wd 'pn ',fn,';pshow;'
  end.
)

paledit_save_button=: 3 : 0
ok=.wd 'mb "" "Ok to overwrite image?" mb_okcancel'
if. ok-:'OK' do. (pa;bitmap) rawwritebmp8 fn end.
)

help_commands=:0 : 0
Images opened need to be 8 bit
uncompressed windows bitmaps

Open        ctrl-o  Loads a new image
Save        ctrl-s  Save the image with modified palette
Save As     ctrl-a  Save the image with a new name

Copy            Copies selected RGB triple to a clipboard
Paste           Pastes the RGB triple to selected position

Double-left         Edit palette entry
Left-click          Select palette entry
Shift-left click    SELECTS palette from selected entry to
            current entry and performs an action on selection
     Blend ..... applies Linear merge
     Reverse ... reverses the order of the palette entries
     Inverse ... applies 255&- to each color
     Random n .. creates n uniformly spaced random color with
                 linear merge in between

Help        F1      This help
)

paledit_command_button=: 3 : 0
wdinfo help_commands
)

help_about=:0 : 0
Utility for editing the palette of
8 bit uncompressed windows bitmaps

Written by Cliff Reiter
Lafayette College
Last update July 13, 1999

Use is free; send $10 with each complaint.
)

paledit_about_button=: 3 : 0
wdinfo help_about
)

paledit_run ''