a0a597441876a1e42e27fc2e8074fee2a10f4fe6
[goodguy/history.git] / cinelerra-5.1 / guicast / vframe.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 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 <errno.h>
23 #include <png.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdint.h>
27 #include <sys/shm.h>
28
29 #include "bcbitmap.h"
30 #include "bchash.h"
31 #include "bcpbuffer.h"
32 #include "bcresources.h"
33 #include "bcsignals.h"
34 #include "bcsynchronous.h"
35 #include "bctexture.h"
36 #include "bcwindowbase.h"
37 #include "clip.h"
38 #include "bccmodels.h"
39 #include "vframe.h"
40
41 class PngReadFunction
42 {
43 public:
44         static void png_read_function(png_structp png_ptr,
45                    png_bytep data, png_size_t length)
46         {
47                 VFrame *frame = (VFrame*)png_get_io_ptr(png_ptr);
48                 if(frame->image_size - frame->image_offset < (long)length)
49                 {
50                         printf("PngReadFunction::png_read_function %d: overrun\n", __LINE__);
51                         length = frame->image_size - frame->image_offset;
52                 }
53
54                 memcpy(data, &frame->image[frame->image_offset], length);
55                 frame->image_offset += length;
56         };
57 };
58
59
60
61
62
63
64
65 VFrameScene::VFrameScene()
66 {
67 }
68
69 VFrameScene::~VFrameScene()
70 {
71 }
72
73
74
75
76
77
78
79 //static BCCounter counter;
80
81
82 VFramePng::VFramePng(unsigned char *png_data, double scale)
83 {
84         long image_size =
85                 ((long)png_data[0] << 24) | ((long)png_data[1] << 16) |
86                 ((long)png_data[2] << 8)  |  (long)png_data[3];
87         if( !scale ) scale = BC_WindowBase::get_resources()->icon_scale;
88         read_png(png_data+4, image_size, scale, scale);
89 }
90
91 VFramePng::VFramePng(unsigned char *png_data, long image_size, double xscale, double yscale)
92 {
93         if( !xscale ) xscale = BC_WindowBase::get_resources()->icon_scale;
94         if( !yscale ) yscale = BC_WindowBase::get_resources()->icon_scale;
95         read_png(png_data, image_size, xscale, yscale);
96 }
97
98 VFramePng::~VFramePng()
99 {
100 }
101
102
103 VFrame::VFrame(VFrame &frame)
104 {
105         reset_parameters(1);
106         params = new BC_Hash;
107         allocate_data(0, -1, 0, 0, 0, frame.w, frame.h,
108                 frame.color_model, frame.bytes_per_line);
109         memcpy(data, frame.data, bytes_per_line * h);
110         copy_stacks(&frame);
111 }
112
113 VFrame::VFrame(int w, int h, int color_model, long bytes_per_line)
114 {
115         reset_parameters(1);
116         params = new BC_Hash;
117         allocate_data(data, -1, 0, 0, 0, w, h,
118                 color_model, bytes_per_line);
119 }
120
121 VFrame::VFrame(unsigned char *data, int shmid, int w, int h,
122         int color_model, long bytes_per_line)
123 {
124         reset_parameters(1);
125         params = new BC_Hash;
126         allocate_data(data, shmid, 0, 0, 0, w, h,
127                 color_model, bytes_per_line);
128 }
129
130 VFrame::VFrame(unsigned char *data, int shmid,
131                 long y_offset, long u_offset, long v_offset,
132                 int w, int h, int color_model, long bytes_per_line)
133 {
134         reset_parameters(1);
135         params = new BC_Hash;
136         allocate_data(data, shmid, y_offset, u_offset, v_offset, w, h,
137                 color_model, bytes_per_line);
138 }
139
140 VFrame::VFrame(BC_Bitmap *bitmap, int w, int h,
141                  int color_model, long bytes_per_line)
142 {
143         reset_parameters(1);
144         params = new BC_Hash;
145         int shmid = 0;
146         unsigned char *data = 0;
147         if( bitmap->is_shared() )
148                 shmid = bitmap->get_shmid();
149         else
150                 data = bitmap->get_data();
151         allocate_data(data, shmid,
152                 bitmap->get_y_offset(),
153                 bitmap->get_u_offset(),
154                 bitmap->get_v_offset(),
155                 w, h, color_model, bytes_per_line);
156 }
157
158 VFrame::VFrame()
159 {
160         reset_parameters(1);
161         params = new BC_Hash;
162         this->color_model = BC_COMPRESSED;
163 }
164
165
166
167 VFrame::~VFrame()
168 {
169         clear_objects(1);
170 // Delete effect stack
171         prev_effects.remove_all_objects();
172         next_effects.remove_all_objects();
173         delete params;
174         delete scene;
175 }
176
177 int VFrame::equivalent(VFrame *src, int test_stacks)
178 {
179         return (src->get_color_model() == get_color_model() &&
180                 src->get_w() == get_w() &&
181                 src->get_h() == get_h() &&
182                 src->bytes_per_line == bytes_per_line &&
183                 (!test_stacks || equal_stacks(src)));
184 }
185
186 int VFrame::data_matches(VFrame *frame)
187 {
188         if(data && frame->get_data() &&
189                 frame->params_match(get_w(), get_h(), get_color_model()) &&
190                 get_data_size() == frame->get_data_size())
191         {
192                 int data_size = get_data_size();
193                 unsigned char *ptr1 = get_data();
194                 unsigned char *ptr2 = frame->get_data();
195                 for(int i = 0; i < data_size; i++)
196                 {
197                         if(*ptr1++ != *ptr2++) return 0;
198                 }
199                 return 1;
200         }
201         return 0;
202 }
203
204 // long VFrame::set_shm_offset(long offset)
205 // {
206 //      shm_offset = offset;
207 //      return 0;
208 // }
209 //
210 // long VFrame::get_shm_offset()
211 // {
212 //      return shm_offset;
213 // }
214 //
215 int VFrame::get_memory_type()
216 {
217         return memory_type;
218 }
219
220 int VFrame::params_match(int w, int h, int color_model)
221 {
222         return (this->w == w &&
223                 this->h == h &&
224                 this->color_model == color_model);
225 }
226
227
228 int VFrame::reset_parameters(int do_opengl)
229 {
230         status = 1;
231         scene = 0;
232         field2_offset = -1;
233         memory_type = VFrame::PRIVATE;
234 //      shm_offset = 0;
235         shmid = -1;
236         use_shm = 1;
237         bytes_per_line = 0;
238         data = 0;
239         rows = 0;
240         color_model = 0;
241         compressed_allocated = 0;
242         compressed_size = 0;   // Size of current image
243         w = 0;
244         h = 0;
245         y = u = v = a = 0;
246         y_offset = 0;
247         u_offset = 0;
248         v_offset = 0;
249         sequence_number = -1;
250         timestamp = -1.;
251         is_keyframe = 0;
252
253         if(do_opengl)
254         {
255 // By default, anything is going to be done in RAM
256                 opengl_state = VFrame::RAM;
257                 pbuffer = 0;
258                 texture = 0;
259         }
260
261         prev_effects.set_array_delete();
262         next_effects.set_array_delete();
263         return 0;
264 }
265
266 int VFrame::clear_objects(int do_opengl)
267 {
268 // Remove texture
269         if(do_opengl)
270         {
271                 delete texture;
272                 texture = 0;
273
274                 delete pbuffer;
275                 pbuffer = 0;
276         }
277
278 // Delete data
279         switch(memory_type)
280         {
281                 case VFrame::PRIVATE:
282 // Memory check
283 // if(this->w * this->h > 1500 * 1100)
284 // printf("VFrame::clear_objects 2 this=%p data=%p\n", this, data);
285                         if(data)
286                         {
287 //printf("VFrame::clear_objects %d this=%p shmid=%p data=%p\n", __LINE__, this, shmid, data);
288                                 if(shmid >= 0)
289                                         shmdt(data);
290                                 else
291                                         free(data);
292 //PRINT_TRACE
293                         }
294
295                         data = 0;
296                         shmid = -1;
297                         break;
298
299                 case VFrame::SHMGET:
300                         if(data)
301                                 shmdt(data);
302                         data = 0;
303                         shmid = -1;
304                         break;
305         }
306
307 // Delete row pointers
308         switch(color_model)
309         {
310                 case BC_COMPRESSED:
311                 case BC_YUV410P:
312                 case BC_YUV411P:
313                 case BC_YUV420P:
314                 case BC_YUV420PI:
315                 case BC_YUV422P:
316                 case BC_YUV444P:
317                 case BC_RGB_FLOATP:
318                 case BC_RGBA_FLOATP:
319                         break;
320
321                 default:
322                         delete [] rows;
323                         rows = 0;
324                         break;
325         }
326
327
328         return 0;
329 }
330
331 int VFrame::get_field2_offset()
332 {
333         return field2_offset;
334 }
335
336 int VFrame::set_field2_offset(int value)
337 {
338         this->field2_offset = value;
339         return 0;
340 }
341
342 void VFrame::set_keyframe(int value)
343 {
344         this->is_keyframe = value;
345 }
346
347 int VFrame::get_keyframe()
348 {
349         return is_keyframe;
350 }
351
352
353 VFrameScene* VFrame::get_scene()
354 {
355         return scene;
356 }
357
358 int VFrame::calculate_bytes_per_pixel(int color_model)
359 {
360         return BC_CModels::calculate_pixelsize(color_model);
361 }
362
363 long VFrame::get_bytes_per_line()
364 {
365         return bytes_per_line;
366 }
367
368 long VFrame::get_data_size()
369 {
370         return calculate_data_size(w, h, bytes_per_line, color_model) - 4;
371 }
372
373 long VFrame::calculate_data_size(int w, int h, int bytes_per_line, int color_model)
374 {
375         return BC_CModels::calculate_datasize(w, h, bytes_per_line, color_model);
376 }
377
378 void VFrame::create_row_pointers()
379 {
380         int sz = w * h;
381         switch(color_model) {
382         case BC_YUV410P:
383                 if( this->v_offset ) break;
384                 this->y_offset = 0;
385                 this->u_offset = sz;
386                 this->v_offset = sz + w / 4 * h / 4;
387                 break;
388
389         case BC_YUV420P:
390         case BC_YUV420PI:
391         case BC_YUV411P:
392                 if( this->v_offset ) break;
393                 this->y_offset = 0;
394                 this->u_offset = sz;
395                 this->v_offset = sz + sz / 4;
396                 break;
397
398         case BC_YUV422P:
399                 if( this->v_offset ) break;
400                 this->y_offset = 0;
401                 this->u_offset = sz;
402                 this->v_offset = sz + sz / 2;
403                 break;
404         case BC_YUV444P:
405                 if( this->v_offset ) break;
406                 this->y_offset = 0;
407                 this->u_offset = sz;
408                 this->v_offset = sz + sz;
409                 break;
410         case BC_RGBA_FLOATP:
411                 if( this->v_offset || a ) break;
412                 a = this->data + 3 * sz * sizeof(float);
413         case BC_RGB_FLOATP:
414                 if( this->v_offset ) break;
415                 this->y_offset = 0;
416                 this->u_offset = sz * sizeof(float);
417                 this->v_offset = 2 * sz * sizeof(float);
418                 break;
419
420         default:
421                 rows = new unsigned char*[h];
422                 for(int i = 0; i < h; i++)
423                         rows[i] = &this->data[i * this->bytes_per_line];
424                 return;
425         }
426         y = this->data + this->y_offset;
427         u = this->data + this->u_offset;
428         v = this->data + this->v_offset;
429 }
430
431 int VFrame::allocate_data(unsigned char *data, int shmid,
432                 long y_offset, long u_offset, long v_offset, int w, int h,
433                 int color_model, long bytes_per_line)
434 {
435         this->w = w;
436         this->h = h;
437         this->color_model = color_model;
438         this->bytes_per_pixel = calculate_bytes_per_pixel(color_model);
439         this->y_offset = this->u_offset = this->v_offset = 0;
440 //      if(shmid == 0) {
441 //              printf("VFrame::allocate_data %d shmid == 0\n", __LINE__, shmid);
442 //      }
443
444         this->bytes_per_line = bytes_per_line >= 0 ?
445                 bytes_per_line : this->bytes_per_pixel * w;
446
447 // Allocate data + padding for MMX
448         if(data) {
449 //printf("VFrame::allocate_data %d %p\n", __LINE__, this->data);
450                 memory_type = VFrame::SHARED;
451                 this->data = data;
452                 this->shmid = -1;
453                 this->y_offset = y_offset;
454                 this->u_offset = u_offset;
455                 this->v_offset = v_offset;
456         }
457         else if(shmid >= 0) {
458                 memory_type = VFrame::SHMGET;
459                 this->data = (unsigned char*)shmat(shmid, NULL, 0);
460 //printf("VFrame::allocate_data %d shmid=%d data=%p\n", __LINE__, shmid, this->data);
461                 this->shmid = shmid;
462                 this->y_offset = y_offset;
463                 this->u_offset = u_offset;
464                 this->v_offset = v_offset;
465         }
466         else {
467                 memory_type = VFrame::PRIVATE;
468                 int size = calculate_data_size(this->w, this->h,
469                         this->bytes_per_line, this->color_model);
470                 if(BC_WindowBase::get_resources()->use_vframe_shm() && use_shm) {
471                         this->shmid = shmget(IPC_PRIVATE, size, IPC_CREAT | 0777);
472                         if(this->shmid < 0) {
473                                 printf("VFrame::allocate_data %d could not allocate shared memory\n", __LINE__);
474                         }
475
476                         this->data = (unsigned char*)shmat(this->shmid, NULL, 0);
477 //printf("VFrame::allocate_data %d %d %d\n", __LINE__, size, this->shmid);
478
479 //printf("VFrame::allocate_data %d %p\n", __LINE__, this->data);
480 // This causes it to automatically delete when the program exits.
481                         shmctl(this->shmid, IPC_RMID, 0);
482                 }
483                 else {
484 // Have to use malloc for libpng
485                         this->data = (unsigned char *)malloc(size);
486                 }
487
488 // Memory check
489 // if(this->w * this->h > 1500 * 1100)
490 // printf("VFrame::allocate_data 2 this=%p w=%d h=%d this->data=%p\n",
491 // this, this->w, this->h, this->data);
492
493                 if(!this->data)
494                         printf("VFrame::allocate_data %dx%d: memory exhausted.\n", this->w, this->h);
495
496 //printf("VFrame::allocate_data %d %p data=%p %d %d\n", __LINE__, this, this->data, this->w, this->h);
497 //if(size > 1000000) printf("VFrame::allocate_data %d\n", size);
498         }
499
500 // Create row pointers
501         create_row_pointers();
502         return 0;
503 }
504
505 void VFrame::set_memory(unsigned char *data,
506         int shmid,
507         long y_offset,
508         long u_offset,
509         long v_offset)
510 {
511         clear_objects(0);
512
513         if(data)
514         {
515                 memory_type = VFrame::SHARED;
516                 this->data = data;
517                 this->shmid = -1;
518                 this->y_offset = y_offset;
519                 this->u_offset = u_offset;
520                 this->v_offset = v_offset;
521         }
522         else
523         if(shmid >= 0)
524         {
525                 memory_type = VFrame::SHMGET;
526                 this->data = (unsigned char*)shmat(shmid, NULL, 0);
527                 this->shmid = shmid;
528         }
529
530         y = this->data + this->y_offset;
531         u = this->data + this->u_offset;
532         v = this->data + this->v_offset;
533
534         create_row_pointers();
535 }
536
537 void VFrame::set_memory(BC_Bitmap *bitmap)
538 {
539         int shmid = 0;
540         unsigned char *data = 0;
541         if( bitmap->is_shared() && !bitmap->is_zombie() )
542                 shmid = bitmap->get_shmid();
543         else
544                 data = bitmap->get_data();
545         set_memory(data, shmid,
546                 bitmap->get_y_offset(),
547                 bitmap->get_u_offset(),
548                 bitmap->get_v_offset());
549 }
550
551 void VFrame::set_compressed_memory(unsigned char *data,
552         int shmid,
553         int data_size,
554         int data_allocated)
555 {
556         clear_objects(0);
557
558         if(data)
559         {
560                 memory_type = VFrame::SHARED;
561                 this->data = data;
562                 this->shmid = -1;
563         }
564         else
565         if(shmid >= 0)
566         {
567                 memory_type = VFrame::SHMGET;
568                 this->data = (unsigned char*)shmat(shmid, NULL, 0);
569                 this->shmid = shmid;
570         }
571
572         this->compressed_allocated = data_allocated;
573         this->compressed_size = data_size;
574 }
575
576
577 // Reallocate uncompressed buffer with or without alpha
578 int VFrame::reallocate(
579         unsigned char *data,
580         int shmid,
581         long y_offset,
582         long u_offset,
583         long v_offset,
584         int w,
585         int h,
586         int color_model,
587         long bytes_per_line)
588 {
589 //      if(shmid == 0) printf("VFrame::reallocate %d shmid=%d\n", __LINE__, shmid);
590         clear_objects(0);
591 //      reset_parameters(0);
592         allocate_data(data,
593                 shmid,
594                 y_offset,
595                 u_offset,
596                 v_offset,
597                 w,
598                 h,
599                 color_model,
600                 bytes_per_line);
601         return 0;
602 }
603
604 int VFrame::allocate_compressed_data(long bytes)
605 {
606         if(bytes < 1) return 1;
607
608 // Want to preserve original contents
609         if(data && compressed_allocated < bytes)
610         {
611                 int new_shmid = -1;
612                 unsigned char *new_data = 0;
613                 if(BC_WindowBase::get_resources()->use_vframe_shm() && use_shm)
614                 {
615                         new_shmid = shmget(IPC_PRIVATE,
616                                 bytes,
617                                 IPC_CREAT | 0777);
618                         new_data = (unsigned char*)shmat(new_shmid, NULL, 0);
619                         shmctl(new_shmid, IPC_RMID, 0);
620                 }
621                 else
622                 {
623 // Have to use malloc for libpng
624                         new_data = (unsigned char *)malloc(bytes);
625                 }
626
627                 bcopy(data, new_data, compressed_allocated);
628 UNBUFFER(data);
629
630                 if(memory_type == VFrame::PRIVATE)
631                 {
632                         if(shmid > 0) {
633                                 if(data)
634                                         shmdt(data);
635                         }
636                         else
637                                 free(data);
638                 }
639                 else
640                 if(memory_type == VFrame::SHMGET)
641                 {
642                         if(data)
643                                 shmdt(data);
644                 }
645
646                 data = new_data;
647                 shmid = new_shmid;
648                 compressed_allocated = bytes;
649         }
650         else
651         if(!data)
652         {
653                 if(BC_WindowBase::get_resources()->use_vframe_shm() && use_shm)
654                 {
655                         shmid = shmget(IPC_PRIVATE,
656                                 bytes,
657                                 IPC_CREAT | 0777);
658                         data = (unsigned char*)shmat(shmid, NULL, 0);
659                         shmctl(shmid, IPC_RMID, 0);
660                 }
661                 else
662                 {
663 // Have to use malloc for libpng
664                         data = (unsigned char *)malloc(bytes);
665                 }
666
667                 compressed_allocated = bytes;
668                 compressed_size = 0;
669         }
670
671         return 0;
672 }
673
674 int VFramePng::read_png(const unsigned char *data, long sz, double xscale, double yscale)
675 {
676 // Test for RAW format
677         if(data[0] == 'R' && data[1] == 'A' && data[2] == 'W' && data[3] == ' ') {
678                 int new_color_model = BC_RGBA8888;
679                 w = data[4] | (data[5] << 8) | (data[6]  << 16) | (data[7]  << 24);
680                 h = data[8] | (data[9] << 8) | (data[10] << 16) | (data[11] << 24);
681                 int components = data[12];
682                 new_color_model = components == 3 ? BC_RGB888 : BC_RGBA8888;
683 // This shares the data directly
684 //              reallocate(data + 20, 0, 0, 0, w, h, new_color_model, -1);
685
686 // Can't use shared data for theme since button constructions overlay the
687 // images directly.
688                 reallocate(NULL, -1, 0, 0, 0, w, h, new_color_model, -1);
689                 memcpy(get_data(), data + 16, w * h * components);
690
691         }
692         else if(data[0] == 0x89 && data[1] == 'P' && data[2] == 'N' && data[3] == 'G') {
693                 int have_alpha = 0;
694                 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
695                 png_infop info_ptr = png_create_info_struct(png_ptr);
696                 int new_color_model;
697
698                 image_offset = 0;
699                 image = data;  image_size = sz;
700                 png_set_read_fn(png_ptr, this, PngReadFunction::png_read_function);
701                 png_read_info(png_ptr, info_ptr);
702
703                 w = png_get_image_width(png_ptr, info_ptr);
704                 h = png_get_image_height(png_ptr, info_ptr);
705
706                 int src_color_model = png_get_color_type(png_ptr, info_ptr);
707
708                 /* tell libpng to strip 16 bit/color files down to 8 bits/color */
709                 png_set_strip_16(png_ptr);
710
711                 /* extract multiple pixels with bit depths of 1, 2, and 4 from a single
712                  * byte into separate bytes (useful for paletted and grayscale images).
713                  */
714                 png_set_packing(png_ptr);
715
716                 /* expand paletted colors into true RGB triplets */
717                 if (src_color_model == PNG_COLOR_TYPE_PALETTE)
718                         png_set_expand(png_ptr);
719
720                 /* expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel */
721                 if (src_color_model == PNG_COLOR_TYPE_GRAY && png_get_bit_depth(png_ptr, info_ptr) < 8)
722                         png_set_expand(png_ptr);
723
724                 if (src_color_model == PNG_COLOR_TYPE_GRAY ||
725                     src_color_model == PNG_COLOR_TYPE_GRAY_ALPHA)
726                         png_set_gray_to_rgb(png_ptr);
727
728                 /* expand paletted or RGB images with transparency to full alpha channels
729                  * so the data will be available as RGBA quartets */
730                 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)){
731                         have_alpha = 1;
732                         png_set_expand(png_ptr);
733                 }
734
735                 switch(src_color_model)
736                 {
737                         case PNG_COLOR_TYPE_GRAY:
738                         case PNG_COLOR_TYPE_RGB:
739                                 new_color_model = BC_RGB888;
740                                 break;
741
742                         case PNG_COLOR_TYPE_GRAY_ALPHA:
743                         case PNG_COLOR_TYPE_RGB_ALPHA:
744                         default:
745                                 new_color_model = BC_RGBA8888;
746                                 break;
747
748                         case PNG_COLOR_TYPE_PALETTE:
749                                 if(have_alpha)
750                                         new_color_model = BC_RGBA8888;
751                                 else
752                                         new_color_model = BC_RGB888;
753                 }
754
755                 reallocate(NULL, -1, 0, 0, 0, w, h, new_color_model, -1);
756
757 //printf("VFrame::read_png %d %d %d %p\n", __LINE__, w, h, get_rows());
758                 png_read_image(png_ptr, get_rows());
759                 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
760         }
761         else {
762                 printf("VFrame::read_png %d: unknown file format"
763                         " 0x%02x 0x%02x 0x%02x 0x%02x\n",
764                         __LINE__, data[4], data[5], data[6], data[7]);
765                 return 1;
766         }
767         int ww = w * xscale, hh = h * yscale;
768         if( ww != w || hh != h ) {
769                 VFrame vframe(*this);
770                 reallocate(NULL, -1, 0, 0, 0, ww, hh, color_model, -1);
771                 transfer_from(&vframe);
772         }
773         return 0;
774 }
775
776 int VFrame::write_png(const char *path)
777 {
778         png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
779         png_infop info_ptr = png_create_info_struct(png_ptr);
780         FILE *out_fd = fopen(path, "w");
781         if(!out_fd)
782         {
783                 printf("VFrame::write_png %d %s %s\n", __LINE__, path, strerror(errno));
784                 return 1;
785         }
786
787         int png_cmodel = PNG_COLOR_TYPE_RGB;
788         switch(get_color_model())
789         {
790                 case BC_RGB888:
791                 case BC_YUV888:
792                         png_cmodel = PNG_COLOR_TYPE_RGB;
793                         break;
794
795                 case BC_RGBA8888:
796                 case BC_YUVA8888:
797                         png_cmodel = PNG_COLOR_TYPE_RGB_ALPHA;
798                         break;
799
800                 case BC_A8:
801                         png_cmodel = PNG_COLOR_TYPE_GRAY;
802                         break;
803         }
804
805         png_init_io(png_ptr, out_fd);
806         png_set_compression_level(png_ptr, 9);
807         png_set_IHDR(png_ptr,
808                 info_ptr,
809                 get_w(),
810                 get_h(),
811         8,
812                 png_cmodel,
813                 PNG_INTERLACE_NONE,
814                 PNG_COMPRESSION_TYPE_DEFAULT,
815                 PNG_FILTER_TYPE_DEFAULT);
816         png_write_info(png_ptr, info_ptr);
817         png_write_image(png_ptr, get_rows());
818         png_write_end(png_ptr, info_ptr);
819         png_destroy_write_struct(&png_ptr, &info_ptr);
820         fclose(out_fd);
821         return 0;
822 }
823
824
825 #define ZERO_YUV(components, type, max) \
826 { \
827         for(int i = 0; i < h; i++) \
828         { \
829                 type *row = (type*)get_rows()[i]; \
830                 for(int j = 0; j < w; j++) \
831                 { \
832                         row[j * components] = 0; \
833                         row[j * components + 1] = (max + 1) / 2; \
834                         row[j * components + 2] = (max + 1) / 2; \
835                         if(components == 4) row[j * components + 3] = 0; \
836                 } \
837         } \
838 }
839
840 int VFrame::clear_frame()
841 {
842         int sz = w * h;
843 //printf("VFrame::clear_frame %d\n", __LINE__);
844         switch(color_model) {
845         case BC_COMPRESSED:
846                 break;
847
848         case BC_YUV410P:
849                 bzero(get_y(), sz);
850                 bzero(get_u(), w / 4 * h / 4);
851                 bzero(get_v(), w / 4 * h / 4);
852                 break;
853
854         case BC_YUV411P:
855         case BC_YUV420P:
856         case BC_YUV420PI:
857                 bzero(get_y(), sz);
858                 bzero(get_u(), sz / 4);
859                 bzero(get_v(), sz / 4);
860                 break;
861
862         case BC_YUV422P:
863                 bzero(get_y(), sz);
864                 bzero(get_u(), sz / 2);
865                 bzero(get_v(), sz / 2);
866                 break;
867
868         case BC_RGBA_FLOATP: if( a ) {
869                 float *ap = (float *)a;
870                 for( int i=sz; --i>=0; ++ap ) *ap = 1.f; }
871         case BC_RGB_FLOATP: {
872                 float *rp = (float *)y;
873                 for( int i=sz; --i>=0; ++rp ) *rp = 0.f;
874                 float *gp = (float *)u;
875                 for( int i=sz; --i>=0; ++gp ) *gp = 0.f;
876                 float *bp = (float *)v;
877                 for( int i=sz; --i>=0; ++bp ) *bp = 0.f;
878                 break; }
879         case BC_YUV444P:
880                 bzero(get_y(), sz);
881                 bzero(get_u(), sz);
882                 bzero(get_v(), sz);
883                 break;
884
885         case BC_YUV888:
886                 ZERO_YUV(3, unsigned char, 0xff);
887                 break;
888
889         case BC_YUVA8888:
890                 ZERO_YUV(4, unsigned char, 0xff);
891                 break;
892
893         case BC_YUV161616:
894                 ZERO_YUV(3, uint16_t, 0xffff);
895                 break;
896
897         case BC_YUVA16161616:
898                 ZERO_YUV(4, uint16_t, 0xffff);
899                 break;
900
901         default:
902                 bzero(data, calculate_data_size(w, h, bytes_per_line, color_model));
903                 break;
904         }
905         return 0;
906 }
907
908 void VFrame::rotate90()
909 {
910 // Allocate new frame
911         int new_w = h, new_h = w, new_bytes_per_line = bytes_per_pixel * new_w;
912         unsigned char *new_data = new unsigned char[calculate_data_size(new_w, new_h, new_bytes_per_line, color_model)];
913         unsigned char **new_rows = new unsigned char*[new_h];
914         for(int i = 0; i < new_h; i++)
915                 new_rows[i] = &new_data[new_bytes_per_line * i];
916
917 // Copy data
918         for(int in_y = 0, out_x = new_w - 1; in_y < h; in_y++, out_x--)
919         {
920                 for(int in_x = 0, out_y = 0; in_x < w; in_x++, out_y++)
921                 {
922                         for(int k = 0; k < bytes_per_pixel; k++)
923                         {
924                                 new_rows[out_y][out_x * bytes_per_pixel + k] =
925                                         rows[in_y][in_x * bytes_per_pixel + k];
926                         }
927                 }
928         }
929
930 // Swap frames
931         clear_objects(0);
932         data = new_data;
933         rows = new_rows;
934         bytes_per_line = new_bytes_per_line;
935         w = new_w;
936         h = new_h;
937 }
938
939 void VFrame::rotate270()
940 {
941 // Allocate new frame
942         int new_w = h, new_h = w, new_bytes_per_line = bytes_per_pixel * new_w;
943         unsigned char *new_data = new unsigned char[calculate_data_size(new_w, new_h, new_bytes_per_line, color_model)];
944         unsigned char **new_rows = new unsigned char*[new_h];
945         for(int i = 0; i < new_h; i++)
946                 new_rows[i] = &new_data[new_bytes_per_line * i];
947
948 // Copy data
949         for(int in_y = 0, out_x = 0; in_y < h; in_y++, out_x++)
950         {
951                 for(int in_x = 0, out_y = new_h - 1; in_x < w; in_x++, out_y--)
952                 {
953                         for(int k = 0; k < bytes_per_pixel; k++)
954                         {
955                                 new_rows[out_y][out_x * bytes_per_pixel + k] =
956                                         rows[in_y][in_x * bytes_per_pixel + k];
957                         }
958                 }
959         }
960
961 // Swap frames
962         clear_objects(0);
963         data = new_data;
964         rows = new_rows;
965         bytes_per_line = new_bytes_per_line;
966         w = new_w;
967         h = new_h;
968 }
969
970 void VFrame::flip_vert()
971 {
972         unsigned char *temp = new unsigned char[bytes_per_line];
973         for(int i = 0, j = h - 1; i < j; i++, j--)
974         {
975                 memcpy(temp, rows[j], bytes_per_line);
976                 memcpy(rows[j], rows[i], bytes_per_line);
977                 memcpy(rows[i], temp, bytes_per_line);
978         }
979         delete [] temp;
980 }
981
982 void VFrame::flip_horiz()
983 {
984         unsigned char temp[32];
985         for(int i = 0; i < h; i++)
986         {
987                 unsigned char *row = rows[i];
988                 for(int j = 0; j < bytes_per_line / 2; j += bytes_per_pixel)
989                 {
990                         memcpy(temp, row + j, bytes_per_pixel);
991                         memcpy(row + j, row + bytes_per_line - j - bytes_per_pixel, bytes_per_pixel);
992                         memcpy(row + bytes_per_line - j - bytes_per_pixel, temp, bytes_per_pixel);
993                 }
994         }
995 }
996
997
998
999 int VFrame::copy_from(VFrame *frame)
1000 {
1001         int w = MIN(this->w, frame->get_w());
1002         int h = MIN(this->h, frame->get_h());
1003         timestamp = frame->timestamp;
1004
1005         switch(frame->color_model)
1006         {
1007                 case BC_COMPRESSED:
1008                         allocate_compressed_data(frame->compressed_size);
1009                         memcpy(data, frame->data, frame->compressed_size);
1010                         this->compressed_size = frame->compressed_size;
1011                         break;
1012
1013                 case BC_YUV410P:
1014                         memcpy(get_y(), frame->get_y(), w * h);
1015                         memcpy(get_u(), frame->get_u(), w / 4 * h / 4);
1016                         memcpy(get_v(), frame->get_v(), w / 4 * h / 4);
1017                         break;
1018
1019                 case BC_YUV420P:
1020                 case BC_YUV420PI:
1021                 case BC_YUV411P:
1022 //printf("%d %d %p %p %p %p %p %p\n", w, h, get_y(), get_u(), get_v(), frame->get_y(), frame->get_u(), frame->get_v());
1023                         memcpy(get_y(), frame->get_y(), w * h);
1024                         memcpy(get_u(), frame->get_u(), w * h / 4);
1025                         memcpy(get_v(), frame->get_v(), w * h / 4);
1026                         break;
1027
1028                 case BC_YUV422P:
1029 //printf("%d %d %p %p %p %p %p %p\n", w, h, get_y(), get_u(), get_v(), frame->get_y(), frame->get_u(), frame->get_v());
1030                         memcpy(get_y(), frame->get_y(), w * h);
1031                         memcpy(get_u(), frame->get_u(), w * h / 2);
1032                         memcpy(get_v(), frame->get_v(), w * h / 2);
1033                         break;
1034
1035                 case BC_YUV444P:
1036 //printf("%d %d %p %p %p %p %p %p\n", w, h, get_y(), get_u(), get_v(), frame->get_y(), frame->get_u(), frame->get_v());
1037                         memcpy(get_y(), frame->get_y(), w * h);
1038                         memcpy(get_u(), frame->get_u(), w * h);
1039                         memcpy(get_v(), frame->get_v(), w * h);
1040                         break;
1041                 default:
1042 // printf("VFrame::copy_from %d\n", calculate_data_size(w,
1043 //                              h,
1044 //                              -1,
1045 //                              frame->color_model));
1046 // Copy without extra 4 bytes in case the source is a hardware device
1047                         memmove(data, frame->data, get_data_size());
1048                         break;
1049         }
1050
1051         return 0;
1052 }
1053
1054 int VFrame::transfer_from(VFrame *that, int bg_color, int in_x, int in_y, int in_w, int in_h)
1055 {
1056         if( this->get_color_model() == that->get_color_model() &&
1057             this->get_w() == that->get_w() && this->get_h() == that->get_h() &&
1058             this->get_bytes_per_line() == that->get_bytes_per_line() )
1059                 return this->copy_from(that);
1060
1061         timestamp = that->timestamp;
1062 #if 0
1063         BC_CModels::transfer(
1064                 this->get_rows(), that->get_rows(),          // Packed data out/in
1065                 this->get_y(), this->get_u(), this->get_v(), // Planar data out/in
1066                 that->get_y(), that->get_u(), that->get_v(),
1067                 0, 0, that->get_w(), that->get_h(),          // Dimensions in/out
1068                 0, 0, this->get_w(), this->get_h(),
1069                 that->get_color_model(), this->get_color_model(), // Color models in/out
1070                 bg_color,                                    // alpha blend bg_color
1071                 that->get_bytes_per_line(),
1072                 this->get_bytes_per_line());                 // rowspans (of luma for YUV)
1073 #else
1074         unsigned char *in_ptrs[4], *out_ptrs[4];
1075         unsigned char **inp, **outp;
1076         if( BC_CModels::is_planar(that->get_color_model()) ) {
1077                 in_ptrs[0] = that->get_y();
1078                 in_ptrs[1] = that->get_u();
1079                 in_ptrs[2] = that->get_v();
1080                 in_ptrs[3] = that->get_a();
1081                 inp = in_ptrs;
1082         }
1083         else
1084                 inp = that->get_rows(); 
1085         if( BC_CModels::is_planar(this->get_color_model()) ) {
1086                 out_ptrs[0] = this->get_y();
1087                 out_ptrs[1] = this->get_u();
1088                 out_ptrs[2] = this->get_v();
1089                 out_ptrs[3] = this->get_a();
1090                 outp = out_ptrs;
1091         }
1092         else
1093                 outp = this->get_rows();        
1094         BC_CModels::transfer(outp, this->get_color_model(),
1095                         0, 0, this->get_w(), this->get_h(),
1096                         this->get_bytes_per_line(),
1097                 inp, that->get_color_model(),
1098                         in_x, in_y, in_w, in_h,
1099                         that->get_bytes_per_line(),
1100                 bg_color);
1101 #endif
1102         return 0;
1103 }
1104
1105
1106
1107
1108
1109
1110 #define OVERLAY(type, max, components) \
1111 { \
1112         type **in_rows = (type**)src->get_rows(); \
1113         type **out_rows = (type**)get_rows(); \
1114         int in_w = src->get_w(); \
1115         int in_h = src->get_h(); \
1116  \
1117         for(int i = 0; i < in_h; i++) \
1118         { \
1119                 if(i + out_y1 >= 0 && i + out_y1 < h) \
1120                 { \
1121                         type *src_row = in_rows[i]; \
1122                         type *dst_row = out_rows[i + out_y1] + out_x1 * components; \
1123  \
1124                         for(int j = 0; j < in_w; j++) \
1125                         { \
1126                                 if(j + out_x1 >= 0 && j + out_x1 < w) \
1127                                 { \
1128                                         int opacity = src_row[3]; \
1129                                         int transparency = dst_row[3] * (max - src_row[3]) / max; \
1130                                         dst_row[0] = (transparency * dst_row[0] + opacity * src_row[0]) / max; \
1131                                         dst_row[1] = (transparency * dst_row[1] + opacity * src_row[1]) / max; \
1132                                         dst_row[2] = (transparency * dst_row[2] + opacity * src_row[2]) / max; \
1133                                         dst_row[3] = MAX(dst_row[3], src_row[3]); \
1134                                 } \
1135  \
1136                                 dst_row += components; \
1137                                 src_row += components; \
1138                         } \
1139                 } \
1140         } \
1141 }
1142
1143
1144 void VFrame::overlay(VFrame *src,
1145                 int out_x1,
1146                 int out_y1)
1147 {
1148         switch(get_color_model())
1149         {
1150                 case BC_RGBA8888:
1151                         OVERLAY(unsigned char, 0xff, 4);
1152                         break;
1153         }
1154 }
1155
1156
1157
1158 int VFrame::get_scale_tables(int *column_table, int *row_table,
1159                         int in_x1, int in_y1, int in_x2, int in_y2,
1160                         int out_x1, int out_y1, int out_x2, int out_y2)
1161 {
1162         int i;
1163         float w_in = in_x2 - in_x1;
1164         float h_in = in_y2 - in_y1;
1165         int w_out = out_x2 - out_x1;
1166         int h_out = out_y2 - out_y1;
1167
1168         float hscale = w_in / w_out;
1169         float vscale = h_in / h_out;
1170
1171         for(i = 0; i < w_out; i++)
1172         {
1173                 column_table[i] = (int)(hscale * i);
1174         }
1175
1176         for(i = 0; i < h_out; i++)
1177         {
1178                 row_table[i] = (int)(vscale * i) + in_y1;
1179         }
1180         return 0;
1181 }
1182
1183 void VFrame::push_prev_effect(const char *name)
1184 {
1185         char *ptr;
1186         prev_effects.append(ptr = new char[strlen(name) + 1]);
1187         strcpy(ptr, name);
1188         if(prev_effects.total > MAX_STACK_ELEMENTS) prev_effects.remove_object(0);
1189 }
1190
1191 void VFrame::pop_prev_effect()
1192 {
1193         if(prev_effects.total)
1194                 prev_effects.remove_object(prev_effects.last());
1195 }
1196
1197 void VFrame::push_next_effect(const char *name)
1198 {
1199         char *ptr;
1200         next_effects.append(ptr = new char[strlen(name) + 1]);
1201         strcpy(ptr, name);
1202         if(next_effects.total > MAX_STACK_ELEMENTS) next_effects.remove_object(0);
1203 }
1204
1205 void VFrame::pop_next_effect()
1206 {
1207         if(next_effects.total)
1208                 next_effects.remove_object(next_effects.last());
1209 }
1210
1211 const char* VFrame::get_next_effect(int number)
1212 {
1213         if(!next_effects.total) return "";
1214         else
1215         if(number > next_effects.total - 1) number = next_effects.total - 1;
1216
1217         return next_effects.values[next_effects.total - number - 1];
1218 }
1219
1220 const char* VFrame::get_prev_effect(int number)
1221 {
1222         if(!prev_effects.total) return "";
1223         else
1224         if(number > prev_effects.total - 1) number = prev_effects.total - 1;
1225
1226         return prev_effects.values[prev_effects.total - number - 1];
1227 }
1228
1229 BC_Hash* VFrame::get_params()
1230 {
1231         return params;
1232 }
1233
1234 void VFrame::clear_stacks()
1235 {
1236         next_effects.remove_all_objects();
1237         prev_effects.remove_all_objects();
1238         params->clear();
1239         status = 1;
1240 }
1241
1242 void VFrame::copy_params(VFrame *src)
1243 {
1244         status = src->status;
1245         params->copy_from(src->params);
1246 }
1247
1248 void VFrame::copy_stacks(VFrame *src)
1249 {
1250         clear_stacks();
1251
1252         for(int i = 0; i < src->next_effects.total; i++)
1253         {
1254                 char *ptr;
1255                 next_effects.append(ptr = new char[strlen(src->next_effects.values[i]) + 1]);
1256                 strcpy(ptr, src->next_effects.values[i]);
1257         }
1258         for(int i = 0; i < src->prev_effects.total; i++)
1259         {
1260                 char *ptr;
1261                 prev_effects.append(ptr = new char[strlen(src->prev_effects.values[i]) + 1]);
1262                 strcpy(ptr, src->prev_effects.values[i]);
1263         }
1264
1265         copy_params(src);
1266 }
1267
1268 int VFrame::equal_stacks(VFrame *src)
1269 {
1270         for(int i = 0; i < src->next_effects.total && i < next_effects.total; i++)
1271         {
1272                 if(strcmp(src->next_effects.values[i], next_effects.values[i])) return 0;
1273         }
1274
1275         for(int i = 0; i < src->prev_effects.total && i < prev_effects.total; i++)
1276         {
1277                 if(strcmp(src->prev_effects.values[i], prev_effects.values[i])) return 0;
1278         }
1279
1280         if(!params->equivalent(src->params)) return 0;
1281         return 1;
1282 }
1283
1284 void VFrame::dump_stacks()
1285 {
1286         printf("VFrame::dump_stacks\n");
1287         printf("        next_effects:\n");
1288         for(int i = next_effects.total - 1; i >= 0; i--)
1289                 printf("                %s\n", next_effects.values[i]);
1290         printf("        prev_effects:\n");
1291         for(int i = prev_effects.total - 1; i >= 0; i--)
1292                 printf("                %s\n", prev_effects.values[i]);
1293 }
1294
1295 void VFrame::dump_params()
1296 {
1297         params->dump();
1298 }
1299
1300 void VFrame::dump()
1301 {
1302         printf("VFrame::dump %d this=%p\n", __LINE__, this);
1303         printf("    w=%d h=%d colormodel=%d rows=%p use_shm=%d shmid=%d\n",
1304                 w, h, color_model, rows, use_shm, shmid);
1305 }
1306
1307
1308 int VFrame::get_memory_usage()
1309 {
1310         if(get_compressed_allocated()) return get_compressed_allocated();
1311         return get_h() * get_bytes_per_line();
1312 }
1313
1314 void VFrame::draw_pixel(int x, int y)
1315 {
1316         if(!(x >= 0 && y >= 0 && x < get_w() && y < get_h())) return;
1317
1318 #define DRAW_PIXEL(x, y, components, do_yuv, max, type) \
1319 { \
1320         type **rows = (type**)get_rows(); \
1321         rows[y][x * components] = max - rows[y][x * components]; \
1322         if(!do_yuv) \
1323         { \
1324                 rows[y][x * components + 1] = max - rows[y][x * components + 1]; \
1325                 rows[y][x * components + 2] = max - rows[y][x * components + 2]; \
1326         } \
1327         else \
1328         { \
1329                 rows[y][x * components + 1] = (max / 2 + 1) - rows[y][x * components + 1]; \
1330                 rows[y][x * components + 2] = (max / 2 + 1) - rows[y][x * components + 2]; \
1331         } \
1332         if(components == 4) \
1333                 rows[y][x * components + 3] = max; \
1334 }
1335
1336
1337         switch(get_color_model())
1338         {
1339                 case BC_RGB888:
1340                         DRAW_PIXEL(x, y, 3, 0, 0xff, unsigned char);
1341                         break;
1342                 case BC_RGBA8888:
1343                         DRAW_PIXEL(x, y, 4, 0, 0xff, unsigned char);
1344                         break;
1345                 case BC_RGB_FLOAT:
1346                         DRAW_PIXEL(x, y, 3, 0, 1.0, float);
1347                         break;
1348                 case BC_RGBA_FLOAT:
1349                         DRAW_PIXEL(x, y, 4, 0, 1.0, float);
1350                         break;
1351                 case BC_YUV888:
1352                         DRAW_PIXEL(x, y, 3, 1, 0xff, unsigned char);
1353                         break;
1354                 case BC_YUVA8888:
1355                         DRAW_PIXEL(x, y, 4, 1, 0xff, unsigned char);
1356                         break;
1357                 case BC_RGB161616:
1358                         DRAW_PIXEL(x, y, 3, 0, 0xffff, uint16_t);
1359                         break;
1360                 case BC_YUV161616:
1361                         DRAW_PIXEL(x, y, 3, 1, 0xffff, uint16_t);
1362                         break;
1363                 case BC_RGBA16161616:
1364                         DRAW_PIXEL(x, y, 4, 0, 0xffff, uint16_t);
1365                         break;
1366                 case BC_YUVA16161616:
1367                         DRAW_PIXEL(x, y, 4, 1, 0xffff, uint16_t);
1368                         break;
1369         }
1370 }
1371
1372
1373 void VFrame::draw_line(int x1, int y1, int x2, int y2)
1374 {
1375         int w = labs(x2 - x1);
1376         int h = labs(y2 - y1);
1377 //printf("FindObjectMain::draw_line 1 %d %d %d %d\n", x1, y1, x2, y2);
1378
1379         if(!w && !h)
1380         {
1381                 draw_pixel(x1, y1);
1382         }
1383         else
1384         if(w > h)
1385         {
1386 // Flip coordinates so x1 < x2
1387                 if(x2 < x1)
1388                 {
1389                         y2 ^= y1;
1390                         y1 ^= y2;
1391                         y2 ^= y1;
1392                         x1 ^= x2;
1393                         x2 ^= x1;
1394                         x1 ^= x2;
1395                 }
1396                 int numerator = y2 - y1;
1397                 int denominator = x2 - x1;
1398                 for(int i = x1; i <= x2; i++)
1399                 {
1400                         int y = y1 + (int64_t)(i - x1) * (int64_t)numerator / (int64_t)denominator;
1401                         draw_pixel(i, y);
1402                 }
1403         }
1404         else
1405         {
1406 // Flip coordinates so y1 < y2
1407                 if(y2 < y1)
1408                 {
1409                         y2 ^= y1;
1410                         y1 ^= y2;
1411                         y2 ^= y1;
1412                         x1 ^= x2;
1413                         x2 ^= x1;
1414                         x1 ^= x2;
1415                 }
1416                 int numerator = x2 - x1;
1417                 int denominator = y2 - y1;
1418                 for(int i = y1; i <= y2; i++)
1419                 {
1420                         int x = x1 + (int64_t)(i - y1) * (int64_t)numerator / (int64_t)denominator;
1421                         draw_pixel(x, i);
1422                 }
1423         }
1424 //printf("FindObjectMain::draw_line 2\n");
1425 }
1426
1427 void VFrame::draw_rect(int x1, int y1, int x2, int y2)
1428 {
1429         draw_line(x1, y1, x2, y1);
1430         draw_line(x2, y1 + 1, x2, y2);
1431         draw_line(x2 - 1, y2, x1, y2);
1432         draw_line(x1, y2 - 1, x1, y1 + 1);
1433 }
1434
1435 #define ARROW_SIZE 10
1436 void VFrame::draw_arrow(int x1, int y1, int x2, int y2)
1437 {
1438         double angle = atan((float)(y2 - y1) / (float)(x2 - x1));
1439         double angle1 = angle + (float)145 / 360 * 2 * 3.14159265;
1440         double angle2 = angle - (float)145 / 360 * 2 * 3.14159265;
1441         int x3;
1442         int y3;
1443         int x4;
1444         int y4;
1445         if(x2 < x1)
1446         {
1447                 x3 = x2 - (int)(ARROW_SIZE * cos(angle1));
1448                 y3 = y2 - (int)(ARROW_SIZE * sin(angle1));
1449                 x4 = x2 - (int)(ARROW_SIZE * cos(angle2));
1450                 y4 = y2 - (int)(ARROW_SIZE * sin(angle2));
1451         }
1452         else
1453         {
1454                 x3 = x2 + (int)(ARROW_SIZE * cos(angle1));
1455                 y3 = y2 + (int)(ARROW_SIZE * sin(angle1));
1456                 x4 = x2 + (int)(ARROW_SIZE * cos(angle2));
1457                 y4 = y2 + (int)(ARROW_SIZE * sin(angle2));
1458         }
1459
1460 // Main vector
1461         draw_line(x1, y1, x2, y2);
1462 //      draw_line(x1, y1 + 1, x2, y2 + 1);
1463
1464 // Arrow line
1465         if(abs(y2 - y1) || abs(x2 - x1)) draw_line(x2, y2, x3, y3);
1466 // Arrow line
1467         if(abs(y2 - y1) || abs(x2 - x1)) draw_line(x2, y2, x4, y4);
1468 }
1469
1470
1471
1472