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