source: abuse/tags/pd/macabuse/src/gamma.c @ 49

Last change on this file since 49 was 49, checked in by Sam Hocevar, 12 years ago
  • Imported original public domain release, for future reference.
  • Property svn:keywords set to Id
File size: 4.2 KB
Line 
1#include "jwindow.hpp"
2#include "lisp.hpp"
3#include "scroller.hpp"
4#include "id.hpp"
5#include "cache.hpp"
6#include "language.hpp"
7#include "dprint.hpp"
8#include "loader2.hpp"
9#include <math.h>
10
11extern window_manager *eh;
12extern int dev_ok;
13palette *old_pal=NULL;
14
15class gray_picker : public spicker
16{
17  public :
18  int sc;
19  virtual void draw_item(window_manager *wm, image *screen, int x, int y, int num, int active)
20  {
21    long x2=x+item_width(wm)-1;
22    long y2=y+item_height(wm)-1;
23    screen->bar(x,y,x2,y2,0);
24    screen->bar(x,y,x2-3,y2,sc+num);
25    if (active) screen->rectangle(x,y,x2,y2,31);
26  }
27  void set_pos(int x) { cur_sel=x; }
28  virtual int total() { return 32; }
29  virtual int item_width(window_manager *wm) { return 12; }
30  virtual int item_height(window_manager *wm) { return 20; }
31  virtual int activate_on_mouse_move() { return 0; }
32  gray_picker(int X, int Y, int ID, int start, int current, ifield *Next) : spicker(X,Y,ID,1,20,0,0,Next)
33  { cur_sel=current; sc=start; reconfigure(); cur_sel=current; }   
34} ;
35
36
37FILE *open_FILE(char *filename, char *mode);
38
39void gamma_correct(palette *&pal, int force_menu)
40{
41  long dr=0,dg=7*4,db=0,old_dg=0;
42  int abort=0;
43
44  Cell *gs=find_symbol("darkest_gray");                   // see if user has already done this routine
45
46  if (old_pal)
47  { delete pal; pal=old_pal; old_pal=NULL; }
48
49  if (gs && DEFINEDP(symbol_value(gs)) && !force_menu)
50    dg=lnumber_value(symbol_value(gs));
51  else
52  {   
53    if (gs && DEFINEDP(symbol_value(gs)))
54      dg=old_dg=lnumber_value(symbol_value(gs));
55
56
57    palette *gray_pal=pal->copy();            // load in a fine gray palette they can chose from
58    int i=0;
59    int tc=32;
60
61    for (;i<tc;i++)
62      gray_pal->set(i,i*4,i*4,i*4);
63
64    gray_pal->load();
65
66
67    int wm_bc=eh->bright_color(),wm_mc=eh->medium_color(),wm_dc=eh->dark_color();
68
69    int br_r=pal->red(wm_bc)+20;       if (br_r>255) br_r=255;
70    int br_g=pal->green(wm_bc)+20;     if (br_g>255) br_g=255;
71    int br_b=pal->blue(wm_bc)+20;      if (br_b>255) br_b=255;
72
73    int md_r=pal->red(wm_mc)-20;       if (md_r<0) md_r=0;
74    int md_g=pal->green(wm_mc)-20;     if (md_g<0) md_g=0;
75    int md_b=pal->blue(wm_mc)-20;      if (md_b<0) md_b=0;
76
77    int dr_r=pal->red(wm_dc)-40;       if (dr_r<0) dr_r=0;
78    int dr_g=pal->green(wm_dc)-40;     if (dr_g<0) dr_g=0;
79    int dr_b=pal->blue(wm_dc)-40;      if (dr_b<0) dr_b=0;
80
81    eh->set_colors(gray_pal->find_closest(br_r,br_g,br_b),
82                   gray_pal->find_closest(md_r,md_g,md_b),
83                   gray_pal->find_closest(dr_r,dr_g,dr_b));
84                   
85
86    int wl=WINDOW_FRAME_LEFT,wh=WINDOW_FRAME_TOP;
87    int sh=eh->font()->height()+35;
88    button *but=new button(wl+5,wh+5+sh*3,ID_GAMMA_OK,cash.img(ok_button),
89                new info_field(wl+35,wh+10+sh*3,ID_NULL,lang_string("gamma_msg"),0));
90
91    gray_picker *gp=new gray_picker(wl+2,wh+5+sh*1,ID_GREEN_PICKER,0,dg/4,but);    gp->set_pos(dg/4);
92   
93    jwindow *gw=eh->new_window(xres/2-180,yres/2-90,-1,-1,gp);
94
95    event ev;
96    eh->flush_screen();
97    do
98    {
99      do { eh->get_event(ev); } while (ev.type==EV_MOUSE_MOVE && eh->event_waiting());
100      eh->flush_screen();
101      if (ev.type==EV_CLOSE_WINDOW) abort=1;
102      if (ev.type==EV_KEY && ev.key==JK_ESC) abort=1;
103    } while (!abort && (ev.type!=EV_MESSAGE || ev.message.id!=ID_GAMMA_OK));
104
105    dg=((spicker *)gw->inm->get(ID_GREEN_PICKER))->first_selected()*4;
106
107    eh->close_window(gw);
108    eh->flush_screen();
109
110    eh->set_colors(wm_bc,wm_mc,wm_dc);
111    delete gray_pal;
112
113    if (!abort)
114    {
115      FILE *fp=open_FILE("gamma.lsp","wb");
116      if (fp)
117      {
118                                fprintf(fp,"(setq darkest_gray %d)\n",dg);
119                                fclose(fp);
120                                int sp=current_space;
121                                current_space=PERM_SPACE;
122                                set_symbol_value(make_find_symbol("darkest_gray"),new_lisp_number(dg));
123                       
124                                current_space=sp;
125      } else dprintf("Unable to write to file gamma.lsp\n");
126    }
127  }
128
129  if (abort) dg=old_dg;
130
131  if (dg<1) dg=1; else if (dg>128) dg=128;
132
133  double gamma=log(dg/255.0)/log(16.0/255.0);
134
135  old_pal=pal;
136  pal=new palette;
137
138  for (int i=0;i<256;i++)
139  {
140    double r,g,b;
141    uchar or,og,ob;
142    old_pal->get(i,or,og,ob);
143    pal->set(i,(int)(pow(or/255.0,gamma)*255),
144             (int)(pow(og/255.0,gamma)*255),
145             (int)(pow(ob/255.0,gamma)*255));
146  }
147
148  pal->load();
149}
150
151
152
153
154
155
Note: See TracBrowser for help on using the repository browser.