source: abuse/trunk/src/gamma.cpp @ 39

Last change on this file since 39 was 39, checked in by Sam Hocevar, 15 years ago
  • Fix almost 2,000 warnings by using proper "const" keywords where needed.
File size: 4.7 KB
Line 
1#include <math.h>
2
3#include "jwindow.hpp"
4#include "lisp.hpp"
5#include "game.hpp"
6#include "scroller.hpp"
7#include "id.hpp"
8#include "cache.hpp"
9#include "language.hpp"
10#include "dprint.hpp"
11#include "loader2.hpp"
12
13extern window_manager *eh;
14extern int dev_ok;
15palette *old_pal=NULL;
16
17class gray_picker : public spicker
18{
19public:
20        int sc;
21        virtual void draw_item(window_manager *wm, image *screen, int x, int y, int num, int active)
22        {
23                long x2 = x + item_width( wm ) - 1;
24                long y2 = y + item_height( wm ) - 1;
25                screen->bar( x, y, x2, y2, 0 );
26                screen->bar( x, y, x2 - 3, y2, sc + num );
27                if( active )
28                {
29                        screen->rectangle( x, y, x2, y2, 255 );
30                }
31        }
32        void set_pos( int x ) { cur_sel = x; }
33        virtual int total() { return 32; }
34        virtual int item_width( window_manager *wm ) { return 12; }
35        virtual int item_height( window_manager *wm ) { return 20; }
36        virtual int activate_on_mouse_move() { return 0; }
37
38        gray_picker( int X, int Y, int ID, int start, int current, ifield *Next) : spicker( X, Y, ID, 1, 20, 0, 0, Next )
39        {
40                cur_sel = current;
41                sc = start;
42                reconfigure();
43                cur_sel=current;
44        }
45};
46
47
48void gamma_correct(palette *&pal, int force_menu)
49{
50        long dg=0,old_dg=0;
51        int abort=0;
52
53        // see if user has already done this routine
54        Cell *gs = find_symbol("darkest_gray");
55
56        if( old_pal )
57        {
58                delete pal;
59                pal = old_pal;
60                old_pal = NULL;
61        }
62
63        if( gs && DEFINEDP(symbol_value(gs)) && !force_menu )
64        {
65                dg = lnumber_value( symbol_value( gs ) );
66        }
67        else
68        {
69                if( gs && DEFINEDP( symbol_value( gs ) ) )
70                {
71                        dg = old_dg = lnumber_value( symbol_value( gs ) );
72                }
73                // load in a fine gray palette they can chose from
74                palette *gray_pal = pal->copy();
75                int i = 0;
76                int tc = 32;
77
78                for( ;i < tc; i++ )
79                {
80                        gray_pal->set( i, i * 4, i * 4, i * 4 );
81                }
82
83                gray_pal->load();
84
85                int wm_bc = eh->bright_color(), wm_mc = eh->medium_color(), wm_dc = eh->dark_color();
86
87                int br_r = pal->red( wm_bc ) + 20;
88                if( br_r > 255)
89                {
90                        br_r = 255;
91                }
92                int br_g = pal->green( wm_bc ) + 20;
93                if( br_g > 255 )
94                {
95                        br_g = 255;
96                }
97                int br_b = pal->blue( wm_bc ) + 20;
98                if( br_b > 255 )
99                {
100                        br_b = 255;
101                }
102
103                int md_r = pal->red( wm_mc ) - 20;
104                if( md_r < 0 )
105                {
106                        md_r = 0;
107                }
108                int md_g = pal->green( wm_mc ) - 20;
109                if( md_g < 0 )
110                {
111                        md_g = 0;
112                }
113                int md_b = pal->blue( wm_mc ) - 20;
114                if( md_b < 0 )
115                {
116                        md_b = 0;
117                }
118
119                int dr_r = pal->red( wm_dc ) - 40;
120                if( dr_r < 0 )
121                {
122                        dr_r = 0;
123                }
124                int dr_g = pal->green( wm_dc ) - 40;
125                if( dr_g < 0 )
126                {
127                        dr_g = 0;
128                }
129                int dr_b = pal->blue( wm_dc ) - 40;
130                if( dr_b < 0 )
131                {
132                        dr_b = 0;
133                }
134
135                eh->set_colors( gray_pal->find_closest( br_r, br_g, br_b ),
136                        gray_pal->find_closest( md_r, md_g, md_b ),
137                        gray_pal->find_closest( dr_r, dr_g, dr_b ) );
138
139                int wl = WINDOW_FRAME_LEFT, wh = WINDOW_FRAME_TOP;
140                int sh = eh->font()->height() + 35;
141                button *but = new button( wl + 5, wh + 5 + sh * 3, ID_GAMMA_OK, cash.img( ok_button ),
142                        new info_field( wl + 35, wh + 10 + sh * 3, ID_NULL, lang_string( "gamma_msg" ), 0 ) );
143
144                gray_picker *gp = new gray_picker( wl + 2, wh + 5 + sh * 1, ID_GREEN_PICKER, 0, dg / 4, but);
145                gp->set_pos( dg / 4 );
146
147                jwindow *gw = eh->new_window( xres / 2 - 190, yres / 2 - 90, -1, -1, gp);
148
149                event ev;
150                eh->flush_screen();
151                do
152                {
153                        do
154                        {
155                                eh->get_event(ev);
156                        } while( ev.type == EV_MOUSE_MOVE && eh->event_waiting() );
157                        eh->flush_screen();
158                        if( ev.type == EV_CLOSE_WINDOW)
159                        {
160                                abort = 1;
161                        }
162                        if( ev.type == EV_KEY && ev.key == JK_ESC)
163                        {
164                                abort = 1;
165                        }
166                } while( !abort && ( ev.type != EV_MESSAGE || ev.message.id != ID_GAMMA_OK ) );
167
168                dg = ( (spicker *)gw->inm->get( ID_GREEN_PICKER ) )->first_selected() * 4;
169
170                eh->close_window( gw );
171                eh->flush_screen();
172
173                eh->set_colors( wm_bc, wm_mc, wm_dc);
174                delete gray_pal;
175
176                if( !abort )
177                {
178                        char *gammapath;
179                        FILE *fp;
180
181                        gammapath = (char *)jmalloc( strlen( get_save_filename_prefix() ) + 10, "gammapath" );
182                        sprintf( gammapath, "%sgamma.lsp", get_save_filename_prefix() );
183                        fp = open_FILE( gammapath, "wb" );
184                        if( fp )
185                        {
186                                fprintf( fp, "(setq darkest_gray %ld)\n", dg );
187                                fclose(fp);
188                                int sp = current_space;
189                                current_space = PERM_SPACE;
190                                set_symbol_value( make_find_symbol("darkest_gray"), new_lisp_number( dg ) );
191
192                                current_space = sp;
193                        }
194                        else
195                        {
196                                dprintf( "Unable to write to file gamma.lsp\n" );
197                        }
198                        jfree( gammapath);
199                }
200        }
201
202        if( abort )
203        {
204                dg = old_dg;
205        }
206
207        if( dg < 1 )
208        {
209                dg = 1;
210        }
211        else if( dg > 128 )
212        {
213                dg = 128;
214        }
215
216        double gamma = log( dg / 255.0 ) / log( 16.0 / 255.0 );
217
218        old_pal = pal;
219        pal = new palette;
220        for( int i = 0; i < 256; i++ )
221        {
222                uint8_t oldr, oldg, oldb;
223                old_pal->get( i, oldr, oldg, oldb );
224                pal->set( i, (int)( pow( oldr / 255.0, gamma ) * 255 ),
225                        (int)( pow( oldg / 255.0, gamma ) * 255 ),
226                        (int)( pow( oldb / 255.0, gamma ) * 255 ) );
227        }
228
229        pal->load();
230}
Note: See TracBrowser for help on using the repository browser.