76692f0d99dcb9630c40aa77085384f5b5c53817
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / colorpicker.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-2011 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "bcbutton.h"
23 #include "bccapture.h"
24 #include "bccolors.h"
25 #include "bcdisplayinfo.h"
26 #include "colorpicker.h"
27 #include "condition.h"
28 #include "keys.h"
29 #include "language.h"
30 #include "mutex.h"
31 #include "mwindow.h"
32 #include "bccolors.h"
33 #include "vframe.h"
34
35 #include <string.h>
36 #include <unistd.h>
37
38 #define PALETTE_DATA "palette.dat"
39
40 ColorPicker::ColorPicker(int do_alpha, const char *title)
41  : BC_DialogThread()
42 {
43         this->title = title;
44         this->do_alpha = do_alpha;
45         this->ok_cancel = 0;
46         this->output = this->orig_color = BLACK;
47         this->alpha = this->orig_alpha = 255;
48 }
49
50 ColorPicker::~ColorPicker()
51 {
52         close_window();
53 }
54
55 void ColorPicker::start_window(int output, int alpha, int ok_cancel)
56 {
57         if( running() ) {
58                 ColorWindow *gui = (ColorWindow *)get_gui();
59                 if( gui ) {
60                         gui->lock_window("ColorPicker::start_window");
61                         gui->raise_window(1);
62                         gui->unlock_window();
63                 }
64                 return;
65         }
66         this->orig_color = output;
67         this->orig_alpha = alpha;
68         this->output = output;
69         this->alpha = alpha;
70         this->ok_cancel = ok_cancel;
71         start();
72 }
73
74 BC_Window* ColorPicker::new_gui()
75 {
76         char window_title[BCTEXTLEN];
77         strcpy(window_title, _(PROGRAM_NAME ": "));
78         strcat(window_title, title ? title : _("Color Picker"));
79         BC_DisplayInfo display_info;
80         int x = display_info.get_abs_cursor_x() + 25;
81         int y = display_info.get_abs_cursor_y() - 100;
82         int w = 540, h = 330;
83         if( ok_cancel )
84                 h += bmax(BC_OKButton::calculate_h(),BC_CancelButton::calculate_h());
85         int root_w = display_info.get_root_w(), root_h = display_info.get_root_h();
86         if( x+w > root_w ) x = root_w - w;
87         if( y+h > root_h ) y = root_h - h;
88         if( x < 0 ) x = 0;
89         if( y < 0 ) y = 0;
90         ColorWindow *gui = new ColorWindow(this, x, y, w, h, window_title);
91         gui->create_objects();
92         return gui;
93 }
94
95 void ColorPicker::update_gui(int output, int alpha)
96 {
97         ColorWindow *gui = (ColorWindow *)get_gui();
98         if( !gui ) return;
99         gui->lock_window();
100         this->output = output;
101         this->alpha = alpha;
102         gui->change_values();
103         gui->update_display();
104         gui->unlock_window();
105 }
106
107 int ColorPicker::handle_new_color(int output, int alpha)
108 {
109         printf("ColorPicker::handle_new_color undefined.\n");
110         return 1;
111 }
112
113
114 ColorWindow::ColorWindow(ColorPicker *thread, int x, int y, int w, int h, const char *title)
115  : BC_Window(title, x, y, w, h, w, h, 0, 0, 1)
116 {
117         this->thread = thread;
118         wheel = 0;
119         wheel_value = 0;
120         output = 0;
121
122         hue = 0; sat = 0; val = 0;
123         red = 0; grn = 0; blu = 0;
124         lum = 0; c_r = 0; c_b = 0;
125         alpha = 0;
126
127         hsv_h = 0;  hsv_s = 0;  hsv_v = 0;
128         rgb_r = 0;  rgb_g = 0;  rgb_b = 0;
129         yuv_y = 0;  yuv_u = 0;  yuv_v = 0;
130         aph_a = 0;
131
132         button_grabbed = 0;
133 }
134 ColorWindow::~ColorWindow()
135 {
136         delete hsv_h;  delete hsv_s;  delete hsv_v;
137         delete rgb_r;  delete rgb_g;  delete rgb_b;
138         delete yuv_y;  delete yuv_u;  delete yuv_v;
139         delete aph_a;
140
141         if( button_grabbed ) {
142                 ungrab_buttons();
143                 ungrab_cursor();
144         }
145         update_history(rgb888());
146         save_history();
147 }
148
149 void ColorWindow::create_objects()
150 {
151         int x0 = 10, y0 = 10;
152         lock_window("ColorWindow::create_objects");
153         change_values();
154
155         int x = x0, y = y0;
156         add_tool(wheel = new PaletteWheel(this, x, y));
157         wheel->create_objects();
158
159         x += 180;  add_tool(wheel_value = new PaletteWheelValue(this, x, y));
160         wheel_value->create_objects();
161         x = x0;
162         y += 180;  add_tool(output = new PaletteOutput(this, x, y));
163         output->create_objects();
164         y += output->get_h() + 20;
165
166         load_history();  int x1 = x;
167         add_tool(hex_btn = new PaletteHexButton(this, x1, y));
168         char hex[BCSTRLEN];  sprintf(hex,"%06x",thread->output);
169         x1 += hex_btn->get_w() + 5;
170         add_tool(hex_box = new PaletteHex(this, x1, y, hex));
171         x1 += hex_box->get_w() + 15;
172         add_tool(grab_btn = new PaletteGrabButton(this, x1, y));
173         y += hex_box->get_h() + 15;
174         add_tool(history = new PaletteHistory(this, 10, y));
175
176         x += 240;
177         add_tool(new BC_Title(x, y =y0, C_("H:"), SMALLFONT));
178         add_tool(new BC_Title(x, y+=25, C_("S:"), SMALLFONT));
179         add_tool(new BC_Title(x, y+=25, D_("colorpicker_value#V:"), SMALLFONT));
180         add_tool(new BC_Title(x, y+=40, C_("R:"), SMALLFONT));
181         add_tool(new BC_Title(x, y+=25, C_("G:"), SMALLFONT));
182         add_tool(new BC_Title(x, y+=25, C_("B:"), SMALLFONT));
183         add_tool(new BC_Title(x, y+=40, C_("Y:"), SMALLFONT));
184         add_tool(new BC_Title(x, y+=25, C_("U:"), SMALLFONT));
185         add_tool(new BC_Title(x, y+=25, D_("colorpicker_Cr#V:"), SMALLFONT));
186         if( thread->do_alpha )
187                 add_tool(new BC_Title(x, y+=40, C_("A:"), SMALLFONT));
188         x += 24;
189         add_tool(hue = new PaletteHue(this, x, y= y0));
190         add_tool(sat = new PaletteSat(this, x, y+=25));
191         add_tool(val = new PaletteVal(this, x, y+=25));
192         add_tool(red = new PaletteRed(this, x, y+=40));
193         add_tool(grn = new PaletteGrn(this, x, y+=25));
194         add_tool(blu = new PaletteBlu(this, x, y+=25));
195         add_tool(lum = new PaletteLum(this, x, y+=40));
196         add_tool(c_r = new PaletteCr (this, x, y+=25));
197         add_tool(c_b = new PaletteCb (this, x, y+=25));
198         if( thread->do_alpha )
199                 add_tool(alpha = new PaletteAlpha(this, x, y+=40));
200
201         x += hue->get_w() + 10;
202         hsv_h = new PaletteHSV(this, x,y= y0, hsv.h, 0, 360);
203         hsv_h->create_objects();  hsv_h->set_tooltip(_("Hue"));
204         hsv_s = new PaletteHSV(this, x,y+=25, hsv.s, 0, 1);
205         hsv_s->create_objects();  hsv_s->set_tooltip(_("Saturation"));
206         hsv_v = new PaletteHSV(this, x,y+=25, hsv.v, 0, 1);
207         hsv_v->create_objects();  hsv_v->set_tooltip(_("Value"));
208         rgb_r = new PaletteRGB(this, x,y+=40, rgb.r, 0, 1);
209         rgb_r->create_objects();  rgb_r->set_tooltip(_("Red"));
210         rgb_g = new PaletteRGB(this, x,y+=25, rgb.g, 0, 1);
211         rgb_g->create_objects();  rgb_g->set_tooltip(_("Green"));
212         rgb_b = new PaletteRGB(this, x,y+=25, rgb.b, 0, 1);
213         rgb_b->create_objects();  rgb_b->set_tooltip(_("Blue"));
214         yuv_y = new PaletteYUV(this, x,y+=40, yuv.y, 0, 1);
215         yuv_y->create_objects();  yuv_y->set_tooltip(_("Luminance"));
216         yuv_u = new PaletteYUV(this, x,y+=25, yuv.u, 0, 1);
217         yuv_u->create_objects();  yuv_u->set_tooltip(_("Blue Luminance Difference"));
218         yuv_v = new PaletteYUV(this, x,y+=25, yuv.v, 0, 1);
219         yuv_v->create_objects();  yuv_v->set_tooltip(_("Red Luminance Difference"));
220         if( thread->do_alpha ) {
221                 aph_a = new PaletteAPH(this, x,y+=40, aph, 0, 1);
222                 aph_a->create_objects();  aph_a->set_tooltip(_("Alpha"));
223         }
224         if( thread->ok_cancel ) {
225                 add_tool(new BC_OKButton(this));
226                 add_tool(new BC_CancelButton(this));
227         }
228         thread->create_objects(this);
229
230         update_display();
231         update_history();
232         show_window(1);
233         unlock_window();
234 }
235
236
237 void ColorWindow::change_values()
238 {
239         float r = ((thread->output>>16) & 0xff) / 255.;
240         float g = ((thread->output>>8)  & 0xff) / 255.;
241         float b = ((thread->output>>0)  & 0xff) / 255.;
242         rgb.r = r;  rgb.g = g;  rgb.b = b;
243         aph = (float)thread->alpha / 255;
244         update_rgb(rgb.r, rgb.g, rgb.b);
245 }
246
247
248 int ColorWindow::close_event()
249 {
250         set_done(thread->ok_cancel ? 1 : 0);
251         return 1;
252 }
253
254
255 void ColorWindow::update_rgb()
256 {
257         update_rgb(rgb.r, rgb.g, rgb.b);
258         update_display();
259 }
260 void ColorWindow::update_hsv()
261 {
262         update_hsv(hsv.h, hsv.s, hsv.v);
263         update_display();
264 }
265 void ColorWindow::update_yuv()
266 {
267         update_yuv(yuv.y, yuv.u, yuv.v);
268         update_display();
269 }
270
271 void ColorWindow::update_display()
272 {
273         wheel->draw(wheel->oldhue, wheel->oldsaturation);
274         wheel->oldhue = hsv.h;
275         wheel->oldsaturation = hsv.s;
276         wheel->draw(hsv.h, hsv.s);
277         wheel->flash();
278         wheel_value->draw(hsv.h, hsv.s, hsv.v);
279         wheel_value->flash();
280         output->draw();
281         output->flash();
282
283         hue->update((int)hsv.h);
284         sat->update(hsv.s);
285         val->update(hsv.v);
286
287         red->update(rgb.r);
288         grn->update(rgb.g);
289         blu->update(rgb.b);
290
291         lum->update(yuv.y);
292         c_r->update(yuv.u);
293         c_b->update(yuv.v);
294
295         hsv_h->update(hsv.h);
296         hsv_s->update(hsv.s);
297         hsv_v->update(hsv.v);
298         rgb_r->update(rgb.r);
299         rgb_g->update(rgb.g);
300         rgb_b->update(rgb.b);
301         yuv_y->update(yuv.y);
302         yuv_u->update(yuv.u);
303         yuv_v->update(yuv.v);
304         hex_box->update();
305
306         if( thread->do_alpha ) {
307                 alpha->update(aph);
308                 aph_a->update(aph);
309         }
310 }
311
312 int ColorWindow::handle_event()
313 {
314         thread->handle_new_color(rgb888(), (int)(255*aph + 0.5));
315         return 1;
316 }
317
318 void ColorWindow::get_screen_sample()
319 {
320         int cx, cy;
321         get_abs_cursor(cx, cy);
322         BC_Capture capture_bitmap(1, 1, 0);
323         VFrame vframe(1,1,BC_RGB888);
324         capture_bitmap.capture_frame(&vframe, cx,cy);
325         unsigned char *data = vframe.get_data();
326         rgb.r = data[0]/255.;  rgb.g = data[1]/255.;  rgb.b = data[2]/255.;
327         update_rgb();
328 }
329
330 int ColorWindow::cursor_motion_event()
331 {
332         if( button_grabbed && get_button_down() ) {
333                 get_screen_sample();
334                 return 1;
335         }
336         return 0;
337 }
338
339 int ColorWindow::button_press_event()
340 {
341         if( button_grabbed ) {
342                 get_screen_sample();
343                 return 1;
344         }
345         return 0;
346 }
347
348 int ColorWindow::button_release_event()
349 {
350         if( button_grabbed ) {
351                 ungrab_buttons();
352                 ungrab_cursor();
353                 grab_btn->enable();
354                 button_grabbed = 0;
355                 update_history();
356                 return handle_event();
357         }
358         return 1;
359 }
360
361 void ColorWindow::update_rgb_hex(const char *hex)
362 {
363         int color;
364         if( sscanf(hex,"%x",&color) == 1 ) {
365                 float r = ((color>>16) & 0xff) / 255.;
366                 float g = ((color>>8)  & 0xff) / 255.;
367                 float b = ((color>>0)  & 0xff) / 255.;
368                 rgb.r = r;  rgb.g = g;  rgb.b = b;
369                 update_rgb();
370                 update_history();
371                 handle_event();
372         }
373 }
374
375
376 PaletteWheel::PaletteWheel(ColorWindow *window, int x, int y)
377  : BC_SubWindow(x, y, 170, 170)
378 {
379         this->window = window;
380         oldhue = 0;
381         oldsaturation = 0;
382         button_down = 0;
383 }
384
385 PaletteWheel::~PaletteWheel()
386 {
387 }
388
389 int PaletteWheel::button_press_event()
390 {
391         if( get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
392                 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
393                 is_event_win() ) {
394                 button_down = 1;
395                 cursor_motion_event();
396                 return 1;
397         }
398         return 0;
399 }
400
401 int PaletteWheel::cursor_motion_event()
402 {
403         int x1, y1, distance;
404         if( button_down && is_event_win() ) {
405                 float h = get_angle(get_w()/2, get_h()/2, get_cursor_x(), get_cursor_y());
406                 bclamp(h, 0, 359.999);  window->hsv.h = h;
407                 x1 = get_w() / 2 - get_cursor_x();
408                 y1 = get_h() / 2 - get_cursor_y();
409                 distance = (int)sqrt(x1 * x1 + y1 * y1);
410                 float s = (float)distance / (get_w() / 2);
411                 bclamp(s, 0, 1);  window->hsv.s = s;
412                 window->hsv.v = 1;
413                 window->update_hsv();
414                 window->handle_event();
415                 return 1;
416         }
417         return 0;
418 }
419
420 int PaletteWheel::button_release_event()
421 {
422         if( button_down ) {
423                 button_down = 0;
424                 return 1;
425         }
426         return 0;
427 }
428
429 void PaletteWheel::create_objects()
430 {
431 // Upper right
432 //printf("PaletteWheel::create_objects 1\n");
433         float h, s, v = 1;
434         float r, g, b;
435         float x1, y1, x2, y2;
436         float distance;
437         int default_r, default_g, default_b;
438         VFrame frame(0, -1, get_w(), get_h(), BC_RGBA8888, -1);
439         x1 = get_w() / 2;
440         y1 = get_h() / 2;
441         default_r = (get_resources()->get_bg_color() & 0xff0000) >> 16;
442         default_g = (get_resources()->get_bg_color() & 0xff00) >> 8;
443         default_b = (get_resources()->get_bg_color() & 0xff);
444 //printf("PaletteWheel::create_objects 1\n");
445
446         int highlight_r = (get_resources()->button_light & 0xff0000) >> 16;
447         int highlight_g = (get_resources()->button_light & 0xff00) >> 8;
448         int highlight_b = (get_resources()->button_light & 0xff);
449
450         for( y2 = 0; y2 < get_h(); y2++ ) {
451                 unsigned char *row = (unsigned char*)frame.get_rows()[(int)y2];
452                 for( x2 = 0; x2 < get_w(); x2++ ) {
453                         distance = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
454                         if( distance > x1 ) {
455                                 row[(int)x2 * 4] = default_r;
456                                 row[(int)x2 * 4 + 1] = default_g;
457                                 row[(int)x2 * 4 + 2] = default_b;
458                                 row[(int)x2 * 4 + 3] = 0;
459                         }
460                         else
461                         if( distance > x1 - 1 ) {
462                                 int r_i, g_i, b_i;
463                                 if( get_h() - y2 < x2 ) {
464                                         r_i = highlight_r;
465                                         g_i = highlight_g;
466                                         b_i = highlight_b;
467                                 }
468                                 else {
469                                         r_i = 0;
470                                         g_i = 0;
471                                         b_i = 0;
472                                 }
473
474                                 row[(int)x2 * 4] = r_i;
475                                 row[(int)x2 * 4 + 1] = g_i;
476                                 row[(int)x2 * 4 + 2] = b_i;
477                                 row[(int)x2 * 4 + 3] = 255;
478                         }
479                         else {
480                                 h = get_angle(x1, y1, x2, y2);
481                                 s = distance / x1;
482                                 HSV::hsv_to_rgb(r, g, b, h, s, v);
483                                 row[(int)x2 * 4] = (int)(r * 255);
484                                 row[(int)x2 * 4 + 1] = (int)(g * 255);
485                                 row[(int)x2 * 4 + 2] = (int)(b * 255);
486                                 row[(int)x2 * 4 + 3] = 255;
487                         }
488                 }
489         }
490 //printf("PaletteWheel::create_objects 1\n");
491
492         draw_vframe(&frame,
493                 0,
494                 0,
495                 get_w(),
496                 get_h(),
497                 0,
498                 0,
499                 get_w(),
500                 get_h(),
501                 0);
502 //printf("PaletteWheel::create_objects 1\n");
503
504         oldhue = window->hsv.h;
505         oldsaturation = window->hsv.s;
506 //printf("PaletteWheel::create_objects 1\n");
507         draw(oldhue, oldsaturation);
508 //printf("PaletteWheel::create_objects 1\n");
509         flash();
510 //printf("PaletteWheel::create_objects 2\n");
511 }
512
513 float PaletteWheel::torads(float angle)
514 {
515         return (float)angle / 360 * 2 * M_PI;
516 }
517
518
519 int PaletteWheel::draw(float hue, float saturation)
520 {
521         int x, y, w, h;
522         x = w = get_w() / 2;
523         y = h = get_h() / 2;
524
525         if( hue > 0 && hue < 90 ) {
526                 x = (int)(w - w * cos(torads(90 - hue)) * saturation);
527                 y = (int)(h - h * sin(torads(90 - hue)) * saturation);
528         }
529         else if( hue > 90 && hue < 180 ) {
530                 x = (int)(w - w * cos(torads(hue - 90)) * saturation);
531                 y = (int)(h + h * sin(torads(hue - 90)) * saturation);
532         }
533         else if( hue > 180 && hue < 270 ) {
534                 x = (int)(w + w * cos(torads(270 - hue)) * saturation);
535                 y = (int)(h + h * sin(torads(270 - hue)) * saturation);
536         }
537         else if( hue > 270 && hue < 360 ) {
538                 x = (int)(w + w * cos(torads(hue - 270)) * saturation);
539                 y = (int)(h - w * sin(torads(hue - 270)) * saturation);
540         }
541         else if( hue == 0 ) {
542                 x = w;
543                 y = (int)(h - h * saturation);
544         }
545         else if( hue == 90 ) {
546                 x = (int)(w - w * saturation);
547                 y = h;
548         }
549         else if( hue == 180 ) {
550                 x = w;
551                 y = (int)(h + h * saturation);
552         }
553         else if( hue == 270 ) {
554                 x = (int)(w + w * saturation);
555                 y = h;
556         }
557
558         set_inverse();
559         set_color(WHITE);
560         draw_circle(x - 5, y - 5, 10, 10);
561         set_opaque();
562         return 0;
563 }
564
565 int PaletteWheel::get_angle(float x1, float y1, float x2, float y2)
566 {
567         float result = -atan2(x2 - x1, y1 - y2) * (360 / M_PI / 2);
568         if( result < 0 )
569                 result += 360;
570         return (int)result;
571 }
572
573 PaletteWheelValue::PaletteWheelValue(ColorWindow *window, int x, int y)
574  : BC_SubWindow(x, y, 40, 170, BLACK)
575 {
576         this->window = window;
577         button_down = 0;
578 }
579 PaletteWheelValue::~PaletteWheelValue()
580 {
581         delete frame;
582 }
583
584 void PaletteWheelValue::create_objects()
585 {
586         frame = new VFrame(get_w(), get_h(), BC_RGB888);
587         draw(window->hsv.h, window->hsv.s, window->hsv.v);
588         flash();
589 }
590
591 int PaletteWheelValue::button_press_event()
592 {
593 //printf("PaletteWheelValue::button_press 1 %d\n", is_event_win());
594         if( get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
595                 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
596                 is_event_win() ) {
597 //printf("PaletteWheelValue::button_press 2\n");
598                 button_down = 1;
599                 cursor_motion_event();
600                 return 1;
601         }
602         return 0;
603 }
604
605 int PaletteWheelValue::cursor_motion_event()
606 {
607         if( button_down && is_event_win() ) {
608 //printf("PaletteWheelValue::cursor_motion 1\n");
609                 float v = 1.0 - (float)(get_cursor_y() - 2) / (get_h() - 4);
610                 bclamp(v, 0, 1);  window->hsv.v = v;
611                 window->update_hsv();
612                 window->handle_event();
613                 return 1;
614         }
615         return 0;
616 }
617
618 int PaletteWheelValue::button_release_event()
619 {
620         if( button_down ) {
621 //printf("PaletteWheelValue::button_release 1\n");
622                 button_down = 0;
623                 return 1;
624         }
625         return 0;
626 }
627
628 int PaletteWheelValue::draw(float hue, float saturation, float value)
629 {
630         float r_f, g_f, b_f;
631         int i, j, r, g, b;
632
633         for( i = get_h() - 3; i >= 2; i-- ) {
634                 unsigned char *row = (unsigned char*)frame->get_rows()[i];
635                 HSV::hsv_to_rgb(r_f, g_f, b_f, hue, saturation,
636                         1.0 - (float)(i - 2) / (get_h() - 4));
637                 r = (int)(r_f * 255);
638                 g = (int)(g_f * 255);
639                 b = (int)(b_f * 255);
640                 for( j = 0; j < get_w(); j++ ) {
641                         row[j * 3] = r;
642                         row[j * 3 + 1] = g;
643                         row[j * 3 + 2] = b;
644                 }
645         }
646
647         draw_3d_border(0, 0, get_w(), get_h(), 1);
648         draw_vframe(frame, 2, 2, get_w() - 4, get_h() - 4,
649                 2, 2, get_w() - 4, get_h() - 4, 0);
650         set_color(BLACK);
651         draw_line(2, get_h() - 3 - (int)(value * (get_h() - 5)),
652                   get_w() - 3, get_h() - 3 - (int)(value * (get_h() - 5)));
653 //printf("PaletteWheelValue::draw %d %f\n", __LINE__, value);
654
655         return 0;
656 }
657
658 PaletteOutput::PaletteOutput(ColorWindow *window, int x, int y)
659  : BC_SubWindow(x, y, 180, 30, BLACK)
660 {
661         this->window = window;
662 }
663 PaletteOutput::~PaletteOutput()
664 {
665 }
666
667
668 void PaletteOutput::create_objects()
669 {
670         draw();
671         flash();
672 }
673
674 int PaletteOutput::handle_event()
675 {
676         return 1;
677 }
678
679 int PaletteOutput::draw()
680 {
681         set_color(window->rgb888());
682         draw_box(2, 2, get_w() - 4, get_h() - 4);
683         draw_3d_border(0, 0, get_w(), get_h(), 1);
684         return 0;
685 }
686
687 PaletteHue::PaletteHue(ColorWindow *window, int x, int y)
688  : BC_ISlider(x, y, 0, 150, 200, 0, 359, (int)(window->hsv.h), 0)
689 {
690         this->window = window;
691 }
692 PaletteHue::~PaletteHue()
693 {
694 }
695
696 int PaletteHue::handle_event()
697 {
698         window->hsv.h = get_value();
699         window->update_hsv();
700         window->handle_event();
701         return 1;
702 }
703
704 PaletteSat::PaletteSat(ColorWindow *window, int x, int y)
705  : BC_FSlider(x, y, 0, 150, 200, 0, 1.0, window->hsv.s, 0)
706 {
707         this->window = window;
708         set_precision(0.01);
709 }
710 PaletteSat::~PaletteSat()
711 {
712 }
713
714 int PaletteSat::handle_event()
715 {
716         window->hsv.s = get_value();
717         window->update_hsv();
718         window->handle_event();
719         return 1;
720 }
721
722
723 PaletteVal::PaletteVal(ColorWindow *window, int x, int y)
724  : BC_FSlider(x, y, 0, 150, 200, 0, 1.0, window->hsv.v, 0)
725 {
726         this->window = window;
727         set_precision(0.01);
728 }
729 PaletteVal::~PaletteVal()
730 {
731 }
732
733 int PaletteVal::handle_event()
734 {
735         window->hsv.v = get_value();
736         window->update_hsv();
737         window->handle_event();
738         return 1;
739 }
740
741
742 PaletteRed::PaletteRed(ColorWindow *window, int x, int y)
743  : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->rgb.r, 0)
744 {
745         this->window = window;
746         set_precision(0.01);
747 }
748 PaletteRed::~PaletteRed()
749 {
750 }
751
752 int PaletteRed::handle_event()
753 {
754         window->rgb.r = get_value();
755         window->update_rgb();
756         window->handle_event();
757         return 1;
758 }
759
760 PaletteGrn::PaletteGrn(ColorWindow *window, int x, int y)
761  : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->rgb.g, 0)
762 {
763         this->window = window;
764         set_precision(0.01);
765 }
766 PaletteGrn::~PaletteGrn()
767 {
768 }
769
770 int PaletteGrn::handle_event()
771 {
772         window->rgb.g = get_value();
773         window->update_rgb();
774         window->handle_event();
775         return 1;
776 }
777
778 PaletteBlu::PaletteBlu(ColorWindow *window, int x, int y)
779  : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->rgb.b, 0)
780 {
781         this->window = window;
782         set_precision(0.01);
783 }
784 PaletteBlu::~PaletteBlu()
785 {
786 }
787
788 int PaletteBlu::handle_event()
789 {
790         window->rgb.b = get_value();
791         window->update_rgb();
792         window->handle_event();
793         return 1;
794 }
795
796 PaletteAlpha::PaletteAlpha(ColorWindow *window, int x, int y)
797  : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->aph, 0)
798 {
799         this->window = window;
800         set_precision(0.01);
801 }
802 PaletteAlpha::~PaletteAlpha()
803 {
804 }
805
806 int PaletteAlpha::handle_event()
807 {
808         window->aph = get_value();
809         window->aph_a->update(window->aph);
810         window->handle_event();
811         return 1;
812 }
813
814 PaletteLum::PaletteLum(ColorWindow *window, int x, int y)
815  : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->yuv.y, 0)
816 {
817         this->window = window;
818         set_precision(0.01);
819 }
820 PaletteLum::~PaletteLum()
821 {
822 }
823
824 int PaletteLum::handle_event()
825 {
826         window->yuv.y = get_value();
827         window->update_yuv();
828         window->handle_event();
829         return 1;
830 }
831
832 PaletteCr::PaletteCr(ColorWindow *window, int x, int y)
833  : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->yuv.u, 0)
834 {
835         this->window = window;
836         set_precision(0.01);
837 }
838 PaletteCr::~PaletteCr()
839 {
840 }
841
842 int PaletteCr::handle_event()
843 {
844         window->yuv.u = get_value();
845         window->update_yuv();
846         window->handle_event();
847         return 1;
848 }
849
850 PaletteCb::PaletteCb(ColorWindow *window, int x, int y)
851  : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->yuv.v, 0)
852 {
853         this->window = window;
854         set_precision(0.01);
855 }
856 PaletteCb::~PaletteCb()
857 {
858 }
859
860 int PaletteCb::handle_event()
861 {
862         window->yuv.v = get_value();
863         window->update_yuv();
864         window->handle_event();
865         return 1;
866 }
867
868 void ColorWindow::update_rgb(float r, float g, float b)
869 {
870         { float y, u, v;
871         YUV::yuv.rgb_to_yuv_f(r, g, b, y, u, v);
872         u += 0.5;  v += 0.5;
873         bclamp(y, 0, 1);    yuv.y = y;
874         bclamp(u, 0, 1);    yuv.u = u;
875         bclamp(v, 0, 1);    yuv.v = v; }
876         { float h, s, v;
877         HSV::rgb_to_hsv(r,g,b, h,s,v);
878         bclamp(h, 0, 360);  hsv.h = h;
879         bclamp(s, 0, 1);    hsv.s = s;
880         bclamp(v, 0, 1);    hsv.v = v; }
881 }
882
883 void ColorWindow::update_yuv(float y, float u, float v)
884 {
885         u -= 0.5;  v -= 0.5;
886         { float r, g, b;
887         YUV::yuv.yuv_to_rgb_f(r, g, b, y, u, v);
888         bclamp(r, 0, 1);   rgb.r = r;
889         bclamp(g, 0, 1);   rgb.g = g;
890         bclamp(b, 0, 1);   rgb.b = b;
891         float h, s, v;
892         HSV::rgb_to_hsv(r,g,b, h, s, v);
893         bclamp(h, 0, 360); hsv.h = h;
894         bclamp(s, 0, 1);   hsv.s = s;
895         bclamp(v, 0, 1);   hsv.v = v; }
896 }
897
898 void ColorWindow::update_hsv(float h, float s, float v)
899 {
900         { float r, g, b;
901         HSV::hsv_to_rgb(r,g,b, h,s,v);
902         bclamp(r, 0, 1);   rgb.r = r;
903         bclamp(g, 0, 1);   rgb.g = g;
904         bclamp(b, 0, 1);   rgb.b = b;
905         float y, u, v;
906         YUV::yuv.rgb_to_yuv_f(r, g, b, y, u, v);
907         u += 0.5;  v += 0.5;
908         bclamp(y, 0, 1);   yuv.y = y;
909         bclamp(u, 0, 1);   yuv.u = u;
910         bclamp(v, 0, 1);   yuv.v = v; }
911 }
912
913 void ColorWindow::load_history()
914 {
915         char history_path[BCTEXTLEN];
916         MWindow::create_defaults_path(history_path,PALETTE_DATA);
917         FILE *fp = fopen(history_path,"r");
918         int i=0;
919         if( fp ) {
920                 while( i < PALLETTE_HISTORY_SIZE ) {
921                         char line[BCSTRLEN];
922                         if( !fgets(line,sizeof(line)-1,fp) ) break;
923                         line[sizeof(line)-1] = 0;
924                         if( sscanf(line, "%x",&palette_history[i]) != 1 ) break;
925                         ++i;
926                 }
927                 fclose(fp);
928         }
929         int r = 0, g = 0, b = 0;
930         float v0 = 0, v1 = 1;
931         while( i < PALLETTE_HISTORY_SIZE ) {
932                 int grey_code = i ^ (i>>1);
933                 r = 255 * ((grey_code&4) ? v0 : v1);
934                 g = 255 * ((grey_code&2) ? v0 : v1);
935                 b = 255 * ((grey_code&1) ? v0 : v1);
936                 int color = (r<<16) | (g<<8) | (b<<0);
937                 palette_history[i++] = color;
938                 if( i & 7 ) continue;
939                 v0 = 0.5f * (v0+.5f);
940                 v1 = 0.5f * (v1+.5f);
941         }
942 }
943 void ColorWindow::save_history()
944 {
945         char history_path[BCTEXTLEN];
946         MWindow::create_defaults_path(history_path,PALETTE_DATA);
947         FILE *fp = fopen(history_path,"w");
948         if( fp ) {
949                 for( int i=0; i<PALLETTE_HISTORY_SIZE; ++i ) {
950                         fprintf(fp, "%06x\n", palette_history[i]);
951                 }
952                 fclose(fp);
953         }
954 }
955 void ColorWindow::update_history(int color)
956 {
957         int out = palette_history[0];
958         palette_history[0] = color;
959         for( int i=1; out != color && i<PALLETTE_HISTORY_SIZE; ++i ) {
960                 int in = out;
961                 out = palette_history[i];
962                 palette_history[i] = in;
963         }
964 }
965 void ColorWindow::update_history()
966 {
967         update_history(rgb888());
968         history->update(0);
969 }
970 int ColorWindow::rgb888()
971 {
972         int r = 255*rgb.r + 0.5, g = 255*rgb.g + 0.5, b = 255*rgb.b + 0.5;
973         bclamp(r, 0, 255);  bclamp(g, 0, 255);  bclamp(b, 0, 255);
974         return (r<<16) | (g<<8) | (b<<0);
975 }
976
977 PaletteNum::PaletteNum(ColorWindow *window, int x, int y,
978         float &output, float min, float max)
979  : BC_TumbleTextBox(window, output, min, max, x, y, 64)
980 {
981         this->window = window;
982         this->output = &output;
983         set_increment(0.01);
984         set_precision(2);
985 }
986
987 PaletteNum::~PaletteNum()
988 {
989 }
990
991
992 int PaletteHSV::handle_event()
993 {
994         update_output();
995         window->update_hsv();
996         window->handle_event();
997         return 1;
998 }
999
1000 int PaletteRGB::handle_event()
1001 {
1002         update_output();
1003         window->update_rgb();
1004         window->handle_event();
1005         return 1;
1006 }
1007
1008 int PaletteYUV::handle_event()
1009 {
1010         update_output();
1011         window->update_yuv();
1012         window->handle_event();
1013         return 1;
1014 }
1015
1016 int PaletteAPH::handle_event()
1017 {
1018         update_output();
1019         window->update_display();
1020         window->handle_event();
1021         return 1;
1022 }
1023
1024 PaletteHexButton::PaletteHexButton(ColorWindow *window, int x, int y)
1025  : BC_GenericButton(x, y, 50, "#")
1026 {
1027         this->window = window;
1028         set_tooltip(_("hex rgb color"));
1029 }
1030 PaletteHexButton::~PaletteHexButton()
1031 {
1032 }
1033 int PaletteHexButton::handle_event()
1034 {
1035         const char *hex = window->hex_box->get_text();
1036         window->update_rgb_hex(hex);
1037         return 1;
1038 }
1039
1040 PaletteHex::PaletteHex(ColorWindow *window, int x, int y, const char *hex)
1041  : BC_TextBox(x, y, 100, 1, hex)
1042 {
1043         this->window = window;
1044 }
1045 PaletteHex::~PaletteHex()
1046 {
1047 }
1048 void PaletteHex::update()
1049 {
1050         char hex[BCSTRLEN];  sprintf(hex,"%06x",window->rgb888());
1051         BC_TextBox::update(hex);
1052 }
1053
1054 int PaletteHex::keypress_event()
1055 {
1056         if( get_keypress() != RETURN )
1057                 return BC_TextBox::keypress_event();
1058         window->update_rgb_hex(get_text());
1059         return 1;
1060 }
1061
1062 #include "grabpick_up_png.h"
1063 #include "grabpick_hi_png.h"
1064 #include "grabpick_dn_png.h"
1065
1066 PaletteGrabButton::PaletteGrabButton(ColorWindow *window, int x, int y)
1067  : BC_Button(x, y, vframes)
1068 {
1069         this->window = window;
1070         vframes[0] = new VFramePng(grabpick_up_png);
1071         vframes[1] = new VFramePng(grabpick_hi_png);
1072         vframes[2] = new VFramePng(grabpick_dn_png);
1073         set_tooltip(_("grab from anywhere picker"));
1074 }
1075 PaletteGrabButton::~PaletteGrabButton()
1076 {
1077         for( int i=0; i<3; ++i )
1078                 delete vframes[i];
1079 }
1080 int PaletteGrabButton::handle_event()
1081 {
1082         if( window->grab_buttons() ) {
1083                 grab_cursor();
1084                 window->button_grabbed = 1;
1085                 button_press_event(); // redraw face HI
1086         }
1087         return 1;
1088 }
1089
1090 PaletteHistory::PaletteHistory(ColorWindow *window, int x, int y)
1091  : BC_SubWindow(x,y, 200, 24)
1092 {
1093         this->window = window;
1094         button_down = 0;
1095         set_tooltip(_("color history"));
1096 }
1097 PaletteHistory::~PaletteHistory()
1098 {
1099 }
1100 void PaletteHistory::update(int flush)
1101 {
1102         int x1 = 0, x2 = 0;
1103         for( int i=0; i<PALLETTE_HISTORY_SIZE; x1=x2 ) {
1104                 int rgb = window->palette_history[i];
1105                 x2 = (++i * get_w())/PALLETTE_HISTORY_SIZE;
1106                 draw_3d_box(x1,0,x2-x1,get_h(),WHITE,BLACK,rgb,LTBLUE,DKBLUE);
1107         }
1108         flash(flush);
1109 }
1110
1111 int PaletteHistory::button_press_event()
1112 {
1113         if( button_down || !is_event_win() ) return 0;
1114         button_down =  1;
1115         cursor_motion_event();
1116         return 1;
1117 }
1118 int PaletteHistory::button_release_event()
1119 {
1120         if( !button_down || !is_event_win() ) return 0;
1121         cursor_motion_event();
1122         if( button_down > 0 ) {
1123                 window->handle_event();
1124                 window->update_display();
1125                 window->update_history();
1126         }
1127         button_down =  0;
1128         return 1;
1129 }
1130 int PaletteHistory::cursor_motion_event()
1131 {
1132         if( !button_down || !is_event_win() ) return 0;
1133         hide_tooltip();
1134         int pick = (PALLETTE_HISTORY_SIZE * get_cursor_x()) / get_w();
1135         bclamp(pick, 0, PALLETTE_HISTORY_SIZE-1);
1136         int color = window->palette_history[pick];
1137         float r = ((color>>16) & 0xff) / 255.;
1138         float g = ((color>>8)  & 0xff) / 255.;
1139         float b = ((color>>0)  & 0xff) / 255.;
1140         if( window->rgb.r != r || window->rgb.g != g || window->rgb.b != b ) {
1141                 window->rgb.r = r;  window->rgb.g = g;  window->rgb.b = b;
1142                 window->update_rgb();
1143         }
1144         return 1;
1145 }
1146
1147 int PaletteHistory::cursor_leave_event()
1148 {
1149         hide_tooltip();
1150         return 0;
1151 }
1152 int PaletteHistory::repeat_event(int64_t duration)
1153 {
1154         int result = 0;
1155
1156         if( duration == get_resources()->tooltip_delay &&
1157             get_tooltip() && *get_tooltip() && cursor_above() ) {
1158                 show_tooltip();
1159                 result = 1;
1160         }
1161         return result;
1162 }
1163
1164
1165 ColorButton::ColorButton(const char *title,
1166         int x, int y, int w, int h,
1167         int color, int alpha, int ok_cancel)
1168  : BC_Button(x, y, w, vframes)
1169 {
1170         this->title = title;
1171         this->color = this->orig_color = color;
1172         this->alpha = this->orig_alpha = alpha;
1173         this->ok_cancel = ok_cancel;
1174
1175         for( int i=0; i<3; ++i ) {
1176                 vframes[i] = new VFrame(w, h, BC_RGBA8888);
1177                 vframes[i]->clear_frame();
1178         }
1179         color_picker = 0;
1180         color_thread = 0;
1181 }
1182
1183 ColorButton::~ColorButton()
1184 {
1185         delete color_thread;
1186         delete color_picker;
1187         for( int i=0; i<3; ++i )
1188                 delete vframes[i];
1189 }
1190
1191 void ColorButton::set_color(int color)
1192 {
1193         printf("ColorButton::set_color %06x\n", color);
1194 }
1195 void ColorButton::handle_done_event(int result)
1196 {
1197         color_thread->stop();
1198 }
1199 int ColorButton::handle_new_color(int color, int alpha)
1200 {
1201         printf("ColorButton::handle_new_color %02x%06x\n", alpha, color);
1202         return 1;
1203 }
1204
1205 int ColorButton::handle_event()
1206 {
1207         unlock_window();
1208         delete color_picker;
1209         color_picker = new ColorButtonPicker(this);
1210         orig_color = color;  orig_alpha = alpha;
1211         color_picker->start_window(color, alpha, ok_cancel);
1212         if( !color_thread )
1213                 color_thread = new ColorButtonThread(this);
1214         color_thread->start();
1215         lock_window("ColorButtonButton::start_color_thread");
1216         return 1;
1217 }
1218
1219 void ColorButton::close_picker()
1220 {
1221         if( color_thread ) color_thread->stop();
1222         delete color_thread;  color_thread = 0;
1223         delete color_picker;  color_picker = 0;
1224 }
1225
1226 void ColorButton::update_gui(int color)
1227 {
1228         set_color(color);
1229         draw_face();
1230 }
1231
1232 ColorButtonPicker::ColorButtonPicker(ColorButton *color_button)
1233  : ColorPicker(color_button->alpha >= 0 ? 1 : 0, color_button->title)
1234 {
1235         this->color_button = color_button;
1236 }
1237
1238 ColorButtonPicker::~ColorButtonPicker()
1239 {
1240 }
1241
1242 void ColorButtonPicker::handle_done_event(int result)
1243 {
1244         color_button->color_thread->stop();
1245         color_button->handle_done_event(result);
1246 }
1247
1248 int ColorButtonPicker::handle_new_color(int color, int alpha)
1249 {
1250         color_button->color = color;
1251         color_button->color_thread->update_lock->unlock();
1252         color_button->handle_new_color(color, alpha);
1253         return 1;
1254 }
1255
1256 void ColorButtonPicker::update_gui()
1257 {
1258         color_button->lock_window("ColorButtonPicker::update_gui");
1259         color_button->update_gui(color_button->color);
1260         color_button->unlock_window();
1261 }
1262
1263 ColorButtonThread::ColorButtonThread(ColorButton *color_button)
1264  : Thread(1, 0, 0)
1265 {
1266         this->color_button = color_button;
1267         this->update_lock = new Condition(0,"ColorButtonThread::update_lock");
1268         done = 1;
1269 }
1270
1271 ColorButtonThread::~ColorButtonThread()
1272 {
1273         stop();
1274         delete update_lock;
1275 }
1276
1277 void ColorButtonThread::start()
1278 {
1279         if( done ) {
1280                 done = 0;
1281                 Thread::start();
1282         }
1283 }
1284
1285 void ColorButtonThread::stop()
1286 {
1287         if( !done ) {
1288                 done = 1;
1289                 update_lock->unlock();
1290                 join();
1291         }
1292 }
1293
1294 void ColorButtonThread::run()
1295 {
1296         ColorButtonPicker *color_picker = color_button->color_picker;
1297         color_picker->update_gui();
1298         while( !done ) {
1299                 update_lock->lock("ColorButtonThread::run");
1300                 if( done ) break;
1301                 color_picker->update_gui();
1302         }
1303 }
1304
1305
1306 ColorBoxButton::ColorBoxButton(const char *title,
1307                 int x, int y, int w, int h,
1308                 int color, int alpha, int ok_cancel)
1309  : ColorButton(title, x, y, w, h, color, alpha, ok_cancel)
1310 {
1311 }
1312 ColorBoxButton::~ColorBoxButton()
1313 {
1314 }
1315
1316 int ColorBoxButton::handle_new_color(int color, int alpha)
1317 {
1318         return ColorButton::handle_new_color(color, alpha);
1319 }
1320 void ColorBoxButton::handle_done_event(int result)
1321 {
1322         ColorButton::handle_done_event(result);
1323 }
1324 void ColorBoxButton::create_objects()
1325 {
1326         update_gui(color);
1327 }
1328
1329 void ColorBoxButton::set_color(int color)
1330 {
1331         this->color = color;
1332         int r = (color>>16) & 0xff;
1333         int g = (color>> 8) & 0xff;
1334         int b = (color>> 0) & 0xff;
1335         int color_model = vframes[0]->get_color_model();
1336         int bpp = BC_CModels::calculate_pixelsize(color_model);
1337         for( int i=0; i<3; ++i ) {
1338                 VFrame *vframe = vframes[i];
1339                 int ww = vframe->get_w(), hh = vframe->get_h();
1340                 uint8_t **rows = vframe->get_rows();
1341                 int rr = r, gg = g, bb = b;
1342                 switch( i ) {
1343                 case BUTTON_UP:
1344                         break;
1345                 case BUTTON_UPHI:
1346                         if( (rr+=48) > 0xff ) rr = 0xff;
1347                         if( (gg+=48) > 0xff ) gg = 0xff;
1348                         if( (bb+=48) > 0xff ) bb = 0xff;
1349                         break;
1350                 case BUTTON_DOWNHI:
1351                         if( (rr-=48) < 0x00 ) rr = 0x00;
1352                         if( (gg-=48) < 0x00 ) gg = 0x00;
1353                         if( (bb-=48) < 0x00 ) bb = 0x00;
1354                         break;
1355                 }
1356                 for( int y=0; y<hh; ++y ) {
1357                         uint8_t *rp = rows[y];
1358                         for( int x=0; x<ww; ++x ) {
1359                                 rp[0] = rr;  rp[1] = gg;  rp[2] = bb;
1360                                 if( bpp > 3 ) rp[3] = 0xff;
1361                                 rp += bpp;
1362                         }
1363                 }
1364         }
1365         set_images(vframes);
1366 }
1367
1368 ColorCircleButton::ColorCircleButton(const char *title,
1369                 int x, int y, int w, int h,
1370                 int color, int alpha, int ok_cancel)
1371  : ColorButton(title, x, y, w, h, color, alpha, ok_cancel)
1372 {
1373 }
1374 ColorCircleButton::~ColorCircleButton()
1375 {
1376 }
1377 int ColorCircleButton::handle_new_color(int color, int alpha)
1378 {
1379         return ColorButton::handle_new_color(color, alpha);
1380 }
1381 void ColorCircleButton::handle_done_event(int result)
1382 {
1383         ColorButton::handle_done_event(result);
1384 }
1385 void ColorCircleButton::create_objects()
1386 {
1387         update_gui(color);
1388 }
1389
1390 void ColorCircleButton::set_color(int color)
1391 {
1392         this->color = color;
1393         int r = (color>>16) & 0xff;
1394         int g = (color>>8) & 0xff;
1395         int b = (color>>0) & 0xff;
1396         for( int i=0; i<3; ++i ) {
1397                 VFrame *vframe = vframes[i];
1398                 int ww = vframe->get_w(), hh = vframe->get_h();
1399                 int cx = (ww+1)/2, cy = hh/2;
1400                 double cc = (cx*cx + cy*cy) / 4.;
1401                 uint8_t *bp = vframe->get_data(), *dp = bp;
1402                 uint8_t *ep = dp + vframe->get_data_size();
1403                 int rr = r, gg = g, bb = b;
1404                 int bpl = vframe->get_bytes_per_line();
1405                 switch( i ) {
1406                 case BUTTON_UP:
1407                         break;
1408                 case BUTTON_UPHI:
1409                         if( (rr+=48) > 0xff ) rr = 0xff;
1410                         if( (gg+=48) > 0xff ) gg = 0xff;
1411                         if( (bb+=48) > 0xff ) bb = 0xff;
1412                         break;
1413                 case BUTTON_DOWNHI:
1414                         if( (rr-=48) < 0x00 ) rr = 0x00;
1415                         if( (gg-=48) < 0x00 ) gg = 0x00;
1416                         if( (bb-=48) < 0x00 ) bb = 0x00;
1417                         break;
1418                 }
1419                 while( dp < ep ) {
1420                         int yy = (dp-bp) / bpl, xx = ((dp-bp) % bpl) >> 2;
1421                         int dy = cy - yy, dx = cx - xx;
1422                         double s = dx*dx + dy*dy - cc;
1423                         double ss = s < 0 ? 1 : s >= cc ? 0 : 1 - s/cc;
1424                         int aa = ss * 0xff;
1425                         *dp++ = rr; *dp++ = gg; *dp++ = bb; *dp++ = aa;
1426                 }
1427         }
1428         set_images(vframes);
1429 }
1430