Merge CV, ver=5.1; ops/methods from HV, and interface from CV where possible
[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() )
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_A8:
794                         png_cmodel = PNG_COLOR_TYPE_GRAY;
795                         break;
796         }
797
798         png_init_io(png_ptr, out_fd);
799         png_set_compression_level(png_ptr, 9);
800         png_set_IHDR(png_ptr,
801                 info_ptr,
802                 get_w(),
803                 get_h(),
804         8,
805                 png_cmodel,
806                 PNG_INTERLACE_NONE,
807                 PNG_COMPRESSION_TYPE_DEFAULT,
808                 PNG_FILTER_TYPE_DEFAULT);
809         png_write_info(png_ptr, info_ptr);
810         png_write_image(png_ptr, get_rows());
811         png_write_end(png_ptr, info_ptr);
812         png_destroy_write_struct(&png_ptr, &info_ptr);
813         fclose(out_fd);
814         return 0;
815 }
816
817
818 #define ZERO_YUV(components, type, max) \
819 { \
820         for(int i = 0; i < h; i++) \
821         { \
822                 type *row = (type*)get_rows()[i]; \
823                 for(int j = 0; j < w; j++) \
824                 { \
825                         row[j * components] = 0; \
826                         row[j * components + 1] = (max + 1) / 2; \
827                         row[j * components + 2] = (max + 1) / 2; \
828                         if(components == 4) row[j * components + 3] = 0; \
829                 } \
830         } \
831 }
832
833 int VFrame::clear_frame()
834 {
835         int sz = w * h;
836 //printf("VFrame::clear_frame %d\n", __LINE__);
837         switch(color_model) {
838         case BC_COMPRESSED:
839                 break;
840
841         case BC_YUV410P:
842                 bzero(get_y(), sz);
843                 bzero(get_u(), w / 4 * h / 4);
844                 bzero(get_v(), w / 4 * h / 4);
845                 break;
846
847         case BC_YUV411P:
848         case BC_YUV420P:
849                 bzero(get_y(), sz);
850                 bzero(get_u(), sz / 4);
851                 bzero(get_v(), sz / 4);
852                 break;
853
854         case BC_YUV422P:
855                 bzero(get_y(), sz);
856                 bzero(get_u(), sz / 2);
857                 bzero(get_v(), sz / 2);
858                 break;
859
860         case BC_RGBA_FLOATP: if( a ) {
861                 float *ap = (float *)a;
862                 for( int i=sz; --i>=0; ++ap ) *ap = 1.f; }
863         case BC_RGB_FLOATP: {
864                 float *rp = (float *)y;
865                 for( int i=sz; --i>=0; ++rp ) *rp = 0.f;
866                 float *gp = (float *)u;
867                 for( int i=sz; --i>=0; ++gp ) *gp = 0.f;
868                 float *bp = (float *)v;
869                 for( int i=sz; --i>=0; ++bp ) *bp = 0.f;
870                 break; }
871         case BC_YUV444P:
872                 bzero(get_y(), sz);
873                 bzero(get_u(), sz);
874                 bzero(get_v(), sz);
875                 break;
876
877         case BC_YUV888:
878                 ZERO_YUV(3, unsigned char, 0xff);
879                 break;
880
881         case BC_YUVA8888:
882                 ZERO_YUV(4, unsigned char, 0xff);
883                 break;
884
885         case BC_YUV161616:
886                 ZERO_YUV(3, uint16_t, 0xffff);
887                 break;
888
889         case BC_YUVA16161616:
890                 ZERO_YUV(4, uint16_t, 0xffff);
891                 break;
892
893         default:
894                 bzero(data, calculate_data_size(w, h, bytes_per_line, color_model));
895                 break;
896         }
897         return 0;
898 }
899
900 void VFrame::rotate90()
901 {
902 // Allocate new frame
903         int new_w = h, new_h = w, new_bytes_per_line = bytes_per_pixel * new_w;
904         unsigned char *new_data = new unsigned char[calculate_data_size(new_w, new_h, new_bytes_per_line, color_model)];
905         unsigned char **new_rows = new unsigned char*[new_h];
906         for(int i = 0; i < new_h; i++)
907                 new_rows[i] = &new_data[new_bytes_per_line * i];
908
909 // Copy data
910         for(int in_y = 0, out_x = new_w - 1; in_y < h; in_y++, out_x--)
911         {
912                 for(int in_x = 0, out_y = 0; in_x < w; in_x++, out_y++)
913                 {
914                         for(int k = 0; k < bytes_per_pixel; k++)
915                         {
916                                 new_rows[out_y][out_x * bytes_per_pixel + k] =
917                                         rows[in_y][in_x * bytes_per_pixel + k];
918                         }
919                 }
920         }
921
922 // Swap frames
923         clear_objects(0);
924         data = new_data;
925         rows = new_rows;
926         bytes_per_line = new_bytes_per_line;
927         w = new_w;
928         h = new_h;
929 }
930
931 void VFrame::rotate270()
932 {
933 // Allocate new frame
934         int new_w = h, new_h = w, new_bytes_per_line = bytes_per_pixel * new_w;
935         unsigned char *new_data = new unsigned char[calculate_data_size(new_w, new_h, new_bytes_per_line, color_model)];
936         unsigned char **new_rows = new unsigned char*[new_h];
937         for(int i = 0; i < new_h; i++)
938                 new_rows[i] = &new_data[new_bytes_per_line * i];
939
940 // Copy data
941         for(int in_y = 0, out_x = 0; in_y < h; in_y++, out_x++)
942         {
943                 for(int in_x = 0, out_y = new_h - 1; in_x < w; in_x++, out_y--)
944                 {
945                         for(int k = 0; k < bytes_per_pixel; k++)
946                         {
947                                 new_rows[out_y][out_x * bytes_per_pixel + k] =
948                                         rows[in_y][in_x * bytes_per_pixel + k];
949                         }
950                 }
951         }
952
953 // Swap frames
954         clear_objects(0);
955         data = new_data;
956         rows = new_rows;
957         bytes_per_line = new_bytes_per_line;
958         w = new_w;
959         h = new_h;
960 }
961
962 void VFrame::flip_vert()
963 {
964         unsigned char *temp = new unsigned char[bytes_per_line];
965         for(int i = 0, j = h - 1; i < j; i++, j--)
966         {
967                 memcpy(temp, rows[j], bytes_per_line);
968                 memcpy(rows[j], rows[i], bytes_per_line);
969                 memcpy(rows[i], temp, bytes_per_line);
970         }
971         delete [] temp;
972 }
973
974 void VFrame::flip_horiz()
975 {
976         unsigned char temp[32];
977         for(int i = 0; i < h; i++)
978         {
979                 unsigned char *row = rows[i];
980                 for(int j = 0; j < bytes_per_line / 2; j += bytes_per_pixel)
981                 {
982                         memcpy(temp, row + j, bytes_per_pixel);
983                         memcpy(row + j, row + bytes_per_line - j - bytes_per_pixel, bytes_per_pixel);
984                         memcpy(row + bytes_per_line - j - bytes_per_pixel, temp, bytes_per_pixel);
985                 }
986         }
987 }
988
989
990
991 int VFrame::copy_from(VFrame *frame)
992 {
993         int w = MIN(this->w, frame->get_w());
994         int h = MIN(this->h, frame->get_h());
995         timestamp = frame->timestamp;
996
997         switch(frame->color_model)
998         {
999                 case BC_COMPRESSED:
1000                         allocate_compressed_data(frame->compressed_size);
1001                         memcpy(data, frame->data, frame->compressed_size);
1002                         this->compressed_size = frame->compressed_size;
1003                         break;
1004
1005                 case BC_YUV410P:
1006                         memcpy(get_y(), frame->get_y(), w * h);
1007                         memcpy(get_u(), frame->get_u(), w / 4 * h / 4);
1008                         memcpy(get_v(), frame->get_v(), w / 4 * h / 4);
1009                         break;
1010
1011                 case BC_YUV420P:
1012                 case BC_YUV411P:
1013 //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());
1014                         memcpy(get_y(), frame->get_y(), w * h);
1015                         memcpy(get_u(), frame->get_u(), w * h / 4);
1016                         memcpy(get_v(), frame->get_v(), w * h / 4);
1017                         break;
1018
1019                 case BC_YUV422P:
1020 //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());
1021                         memcpy(get_y(), frame->get_y(), w * h);
1022                         memcpy(get_u(), frame->get_u(), w * h / 2);
1023                         memcpy(get_v(), frame->get_v(), w * h / 2);
1024                         break;
1025
1026                 case BC_YUV444P:
1027 //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());
1028                         memcpy(get_y(), frame->get_y(), w * h);
1029                         memcpy(get_u(), frame->get_u(), w * h);
1030                         memcpy(get_v(), frame->get_v(), w * h);
1031                         break;
1032                 default:
1033 // printf("VFrame::copy_from %d\n", calculate_data_size(w,
1034 //                              h,
1035 //                              -1,
1036 //                              frame->color_model));
1037 // Copy without extra 4 bytes in case the source is a hardware device
1038                         memmove(data, frame->data, get_data_size());
1039                         break;
1040         }
1041
1042         return 0;
1043 }
1044
1045 int VFrame::transfer_from(VFrame *that, int bg_color)
1046 {
1047         if( this->get_color_model() == that->get_color_model() &&
1048             this->get_w() == that->get_w() && this->get_h() == that->get_h() )
1049                 return this->copy_from(that);
1050
1051         timestamp = that->timestamp;
1052 #if 0
1053         BC_CModels::transfer(
1054                 this->get_rows(), that->get_rows(),          // Packed data out/in
1055                 this->get_y(), this->get_u(), this->get_v(), // Planar data out/in
1056                 that->get_y(), that->get_u(), that->get_v(),
1057                 0, 0, that->get_w(), that->get_h(),          // Dimensions in/out
1058                 0, 0, this->get_w(), this->get_h(),
1059                 that->get_color_model(), this->get_color_model(), // Color models in/out
1060                 bg_color,                                    // alpha blend bg_color
1061                 that->get_w(), this->get_w());               // rowspans (of luma for YUV)
1062 #else
1063         unsigned char *in_ptrs[4], *out_ptrs[4];
1064         unsigned char **inp, **outp;
1065         if( BC_CModels::is_planar(that->get_color_model()) ) {
1066                 in_ptrs[0] = that->get_y();
1067                 in_ptrs[1] = that->get_u();
1068                 in_ptrs[2] = that->get_v();
1069                 in_ptrs[3] = that->get_a();
1070                 inp = in_ptrs;
1071         }
1072         else
1073                 inp = that->get_rows(); 
1074         if( BC_CModels::is_planar(this->get_color_model()) ) {
1075                 out_ptrs[0] = this->get_y();
1076                 out_ptrs[1] = this->get_u();
1077                 out_ptrs[2] = this->get_v();
1078                 out_ptrs[3] = this->get_a();
1079                 outp = out_ptrs;
1080         }
1081         else
1082                 outp = this->get_rows();        
1083         BC_CModels::transfer(outp, this->get_color_model(),
1084                         0, 0, this->get_w(), this->get_h(), this->get_w(),
1085                 inp, that->get_color_model(),
1086                         0, 0, that->get_w(), that->get_h(), that->get_w(),
1087                 bg_color);
1088 #endif
1089         return 0;
1090 }
1091
1092
1093
1094
1095
1096
1097 #define OVERLAY(type, max, components) \
1098 { \
1099         type **in_rows = (type**)src->get_rows(); \
1100         type **out_rows = (type**)get_rows(); \
1101         int in_w = src->get_w(); \
1102         int in_h = src->get_h(); \
1103  \
1104         for(int i = 0; i < in_h; i++) \
1105         { \
1106                 if(i + out_y1 >= 0 && i + out_y1 < h) \
1107                 { \
1108                         type *src_row = in_rows[i]; \
1109                         type *dst_row = out_rows[i + out_y1] + out_x1 * components; \
1110  \
1111                         for(int j = 0; j < in_w; j++) \
1112                         { \
1113                                 if(j + out_x1 >= 0 && j + out_x1 < w) \
1114                                 { \
1115                                         int opacity = src_row[3]; \
1116                                         int transparency = dst_row[3] * (max - src_row[3]) / max; \
1117                                         dst_row[0] = (transparency * dst_row[0] + opacity * src_row[0]) / max; \
1118                                         dst_row[1] = (transparency * dst_row[1] + opacity * src_row[1]) / max; \
1119                                         dst_row[2] = (transparency * dst_row[2] + opacity * src_row[2]) / max; \
1120                                         dst_row[3] = MAX(dst_row[3], src_row[3]); \
1121                                 } \
1122  \
1123                                 dst_row += components; \
1124                                 src_row += components; \
1125                         } \
1126                 } \
1127         } \
1128 }
1129
1130
1131 void VFrame::overlay(VFrame *src,
1132                 int out_x1,
1133                 int out_y1)
1134 {
1135         switch(get_color_model())
1136         {
1137                 case BC_RGBA8888:
1138                         OVERLAY(unsigned char, 0xff, 4);
1139                         break;
1140         }
1141 }
1142
1143
1144
1145 int VFrame::get_scale_tables(int *column_table, int *row_table,
1146                         int in_x1, int in_y1, int in_x2, int in_y2,
1147                         int out_x1, int out_y1, int out_x2, int out_y2)
1148 {
1149         int i;
1150         float w_in = in_x2 - in_x1;
1151         float h_in = in_y2 - in_y1;
1152         int w_out = out_x2 - out_x1;
1153         int h_out = out_y2 - out_y1;
1154
1155         float hscale = w_in / w_out;
1156         float vscale = h_in / h_out;
1157
1158         for(i = 0; i < w_out; i++)
1159         {
1160                 column_table[i] = (int)(hscale * i);
1161         }
1162
1163         for(i = 0; i < h_out; i++)
1164         {
1165                 row_table[i] = (int)(vscale * i) + in_y1;
1166         }
1167         return 0;
1168 }
1169
1170 void VFrame::push_prev_effect(const char *name)
1171 {
1172         char *ptr;
1173         prev_effects.append(ptr = new char[strlen(name) + 1]);
1174         strcpy(ptr, name);
1175         if(prev_effects.total > MAX_STACK_ELEMENTS) prev_effects.remove_object(0);
1176 }
1177
1178 void VFrame::pop_prev_effect()
1179 {
1180         if(prev_effects.total)
1181                 prev_effects.remove_object(prev_effects.last());
1182 }
1183
1184 void VFrame::push_next_effect(const char *name)
1185 {
1186         char *ptr;
1187         next_effects.append(ptr = new char[strlen(name) + 1]);
1188         strcpy(ptr, name);
1189         if(next_effects.total > MAX_STACK_ELEMENTS) next_effects.remove_object(0);
1190 }
1191
1192 void VFrame::pop_next_effect()
1193 {
1194         if(next_effects.total)
1195                 next_effects.remove_object(next_effects.last());
1196 }
1197
1198 const char* VFrame::get_next_effect(int number)
1199 {
1200         if(!next_effects.total) return "";
1201         else
1202         if(number > next_effects.total - 1) number = next_effects.total - 1;
1203
1204         return next_effects.values[next_effects.total - number - 1];
1205 }
1206
1207 const char* VFrame::get_prev_effect(int number)
1208 {
1209         if(!prev_effects.total) return "";
1210         else
1211         if(number > prev_effects.total - 1) number = prev_effects.total - 1;
1212
1213         return prev_effects.values[prev_effects.total - number - 1];
1214 }
1215
1216 BC_Hash* VFrame::get_params()
1217 {
1218         return params;
1219 }
1220
1221 void VFrame::clear_stacks()
1222 {
1223         next_effects.remove_all_objects();
1224         prev_effects.remove_all_objects();
1225         params->clear();
1226         status = 1;
1227 }
1228
1229 void VFrame::copy_params(VFrame *src)
1230 {
1231         status = src->status;
1232         params->copy_from(src->params);
1233 }
1234
1235 void VFrame::copy_stacks(VFrame *src)
1236 {
1237         clear_stacks();
1238
1239         for(int i = 0; i < src->next_effects.total; i++)
1240         {
1241                 char *ptr;
1242                 next_effects.append(ptr = new char[strlen(src->next_effects.values[i]) + 1]);
1243                 strcpy(ptr, src->next_effects.values[i]);
1244         }
1245         for(int i = 0; i < src->prev_effects.total; i++)
1246         {
1247                 char *ptr;
1248                 prev_effects.append(ptr = new char[strlen(src->prev_effects.values[i]) + 1]);
1249                 strcpy(ptr, src->prev_effects.values[i]);
1250         }
1251
1252         copy_params(src);
1253 }
1254
1255 int VFrame::equal_stacks(VFrame *src)
1256 {
1257         for(int i = 0; i < src->next_effects.total && i < next_effects.total; i++)
1258         {
1259                 if(strcmp(src->next_effects.values[i], next_effects.values[i])) return 0;
1260         }
1261
1262         for(int i = 0; i < src->prev_effects.total && i < prev_effects.total; i++)
1263         {
1264                 if(strcmp(src->prev_effects.values[i], prev_effects.values[i])) return 0;
1265         }
1266
1267         if(!params->equivalent(src->params)) return 0;
1268         return 1;
1269 }
1270
1271 void VFrame::dump_stacks()
1272 {
1273         printf("VFrame::dump_stacks\n");
1274         printf("        next_effects:\n");
1275         for(int i = next_effects.total - 1; i >= 0; i--)
1276                 printf("                %s\n", next_effects.values[i]);
1277         printf("        prev_effects:\n");
1278         for(int i = prev_effects.total - 1; i >= 0; i--)
1279                 printf("                %s\n", prev_effects.values[i]);
1280 }
1281
1282 void VFrame::dump_params()
1283 {
1284         params->dump();
1285 }
1286
1287 void VFrame::dump()
1288 {
1289         printf("VFrame::dump %d this=%p\n", __LINE__, this);
1290         printf("    w=%d h=%d colormodel=%d rows=%p use_shm=%d shmid=%d\n",
1291                 w, h, color_model, rows, use_shm, shmid);
1292 }
1293
1294
1295 int VFrame::get_memory_usage()
1296 {
1297         if(get_compressed_allocated()) return get_compressed_allocated();
1298         return get_h() * get_bytes_per_line();
1299 }
1300
1301 void VFrame::draw_pixel(int x, int y)
1302 {
1303         if(!(x >= 0 && y >= 0 && x < get_w() && y < get_h())) return;
1304
1305 #define DRAW_PIXEL(x, y, components, do_yuv, max, type) \
1306 { \
1307         type **rows = (type**)get_rows(); \
1308         rows[y][x * components] = max - rows[y][x * components]; \
1309         if(!do_yuv) \
1310         { \
1311                 rows[y][x * components + 1] = max - rows[y][x * components + 1]; \
1312                 rows[y][x * components + 2] = max - rows[y][x * components + 2]; \
1313         } \
1314         else \
1315         { \
1316                 rows[y][x * components + 1] = (max / 2 + 1) - rows[y][x * components + 1]; \
1317                 rows[y][x * components + 2] = (max / 2 + 1) - rows[y][x * components + 2]; \
1318         } \
1319         if(components == 4) \
1320                 rows[y][x * components + 3] = max; \
1321 }
1322
1323
1324         switch(get_color_model())
1325         {
1326                 case BC_RGB888:
1327                         DRAW_PIXEL(x, y, 3, 0, 0xff, unsigned char);
1328                         break;
1329                 case BC_RGBA8888:
1330                         DRAW_PIXEL(x, y, 4, 0, 0xff, unsigned char);
1331                         break;
1332                 case BC_RGB_FLOAT:
1333                         DRAW_PIXEL(x, y, 3, 0, 1.0, float);
1334                         break;
1335                 case BC_RGBA_FLOAT:
1336                         DRAW_PIXEL(x, y, 4, 0, 1.0, float);
1337                         break;
1338                 case BC_YUV888:
1339                         DRAW_PIXEL(x, y, 3, 1, 0xff, unsigned char);
1340                         break;
1341                 case BC_YUVA8888:
1342                         DRAW_PIXEL(x, y, 4, 1, 0xff, unsigned char);
1343                         break;
1344                 case BC_RGB161616:
1345                         DRAW_PIXEL(x, y, 3, 0, 0xffff, uint16_t);
1346                         break;
1347                 case BC_YUV161616:
1348                         DRAW_PIXEL(x, y, 3, 1, 0xffff, uint16_t);
1349                         break;
1350                 case BC_RGBA16161616:
1351                         DRAW_PIXEL(x, y, 4, 0, 0xffff, uint16_t);
1352                         break;
1353                 case BC_YUVA16161616:
1354                         DRAW_PIXEL(x, y, 4, 1, 0xffff, uint16_t);
1355                         break;
1356         }
1357 }
1358
1359
1360 void VFrame::draw_line(int x1, int y1, int x2, int y2)
1361 {
1362         int w = labs(x2 - x1);
1363         int h = labs(y2 - y1);
1364 //printf("FindObjectMain::draw_line 1 %d %d %d %d\n", x1, y1, x2, y2);
1365
1366         if(!w && !h)
1367         {
1368                 draw_pixel(x1, y1);
1369         }
1370         else
1371         if(w > h)
1372         {
1373 // Flip coordinates so x1 < x2
1374                 if(x2 < x1)
1375                 {
1376                         y2 ^= y1;
1377                         y1 ^= y2;
1378                         y2 ^= y1;
1379                         x1 ^= x2;
1380                         x2 ^= x1;
1381                         x1 ^= x2;
1382                 }
1383                 int numerator = y2 - y1;
1384                 int denominator = x2 - x1;
1385                 for(int i = x1; i <= x2; i++)
1386                 {
1387                         int y = y1 + (int64_t)(i - x1) * (int64_t)numerator / (int64_t)denominator;
1388                         draw_pixel(i, y);
1389                 }
1390         }
1391         else
1392         {
1393 // Flip coordinates so y1 < y2
1394                 if(y2 < y1)
1395                 {
1396                         y2 ^= y1;
1397                         y1 ^= y2;
1398                         y2 ^= y1;
1399                         x1 ^= x2;
1400                         x2 ^= x1;
1401                         x1 ^= x2;
1402                 }
1403                 int numerator = x2 - x1;
1404                 int denominator = y2 - y1;
1405                 for(int i = y1; i <= y2; i++)
1406                 {
1407                         int x = x1 + (int64_t)(i - y1) * (int64_t)numerator / (int64_t)denominator;
1408                         draw_pixel(x, i);
1409                 }
1410         }
1411 //printf("FindObjectMain::draw_line 2\n");
1412 }
1413
1414 void VFrame::draw_rect(int x1, int y1, int x2, int y2)
1415 {
1416         draw_line(x1, y1, x2, y1);
1417         draw_line(x2, y1 + 1, x2, y2);
1418         draw_line(x2 - 1, y2, x1, y2);
1419         draw_line(x1, y2 - 1, x1, y1 + 1);
1420 }
1421
1422 #define ARROW_SIZE 10
1423 void VFrame::draw_arrow(int x1, int y1, int x2, int y2)
1424 {
1425         double angle = atan((float)(y2 - y1) / (float)(x2 - x1));
1426         double angle1 = angle + (float)145 / 360 * 2 * 3.14159265;
1427         double angle2 = angle - (float)145 / 360 * 2 * 3.14159265;
1428         int x3;
1429         int y3;
1430         int x4;
1431         int y4;
1432         if(x2 < x1)
1433         {
1434                 x3 = x2 - (int)(ARROW_SIZE * cos(angle1));
1435                 y3 = y2 - (int)(ARROW_SIZE * sin(angle1));
1436                 x4 = x2 - (int)(ARROW_SIZE * cos(angle2));
1437                 y4 = y2 - (int)(ARROW_SIZE * sin(angle2));
1438         }
1439         else
1440         {
1441                 x3 = x2 + (int)(ARROW_SIZE * cos(angle1));
1442                 y3 = y2 + (int)(ARROW_SIZE * sin(angle1));
1443                 x4 = x2 + (int)(ARROW_SIZE * cos(angle2));
1444                 y4 = y2 + (int)(ARROW_SIZE * sin(angle2));
1445         }
1446
1447 // Main vector
1448         draw_line(x1, y1, x2, y2);
1449 //      draw_line(x1, y1 + 1, x2, y2 + 1);
1450
1451 // Arrow line
1452         if(abs(y2 - y1) || abs(x2 - x1)) draw_line(x2, y2, x3, y3);
1453 // Arrow line
1454         if(abs(y2 - y1) || abs(x2 - x1)) draw_line(x2, y2, x4, y4);
1455 }
1456
1457
1458
1459