confirm prefs update, fix bg_pixmap sz, plugin layout tweaks
[goodguy/cinelerra.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 <fcntl.h>
28 #include <sys/shm.h>
29 #include <sys/mman.h>
30
31 #include "bcbitmap.h"
32 #include "bchash.h"
33 #include "bcpbuffer.h"
34 #include "bcresources.h"
35 #include "bcsignals.h"
36 #include "bcsynchronous.h"
37 #include "bctexture.h"
38 #include "bcwindowbase.h"
39 #include "clip.h"
40 #include "bccmodels.h"
41 #include "vframe.h"
42
43 class PngReadFunction
44 {
45 public:
46         static void png_read_function(png_structp png_ptr,
47                         png_bytep data, png_size_t length)
48         {
49                 VFrame *frame = (VFrame*)png_get_io_ptr(png_ptr);
50                 if(frame->image_size - frame->image_offset < (long)length)
51                 {
52                         printf("PngReadFunction::png_read_function %d: overrun\n", __LINE__);
53                         length = frame->image_size - frame->image_offset;
54                 }
55
56                 memcpy(data, &frame->image[frame->image_offset], length);
57                 frame->image_offset += length;
58         };
59 };
60
61
62
63
64
65
66
67 VFrameScene::VFrameScene()
68 {
69 }
70
71 VFrameScene::~VFrameScene()
72 {
73 }
74
75
76 //static BCCounter counter;
77
78 VFramePng::VFramePng(unsigned char *png_data, double s)
79 {
80         long image_size =
81                 ((long)png_data[0] << 24) | ((long)png_data[1] << 16) |
82                 ((long)png_data[2] << 8)  |  (long)png_data[3];
83         if( !s ) s = BC_WindowBase::get_resources()->icon_scale;
84         read_png(png_data+4, image_size, s, s);
85 }
86
87 VFramePng::VFramePng(unsigned char *png_data, long image_size, double xs, double ys)
88 {
89         if( !xs ) xs = BC_WindowBase::get_resources()->icon_scale;
90         if( !ys ) ys = BC_WindowBase::get_resources()->icon_scale;
91         read_png(png_data, image_size, xs, ys);
92 }
93
94 VFramePng::~VFramePng()
95 {
96 }
97
98 VFrame *VFramePng::vframe_png(int fd, double xs, double ys)
99 {
100         struct stat st;
101         if( fstat(fd, &st) ) return 0;
102         long len = st.st_size;
103         if( !len ) return 0;
104         int w = 0, h = 0;
105         unsigned char *bfr = (unsigned char *)
106                 ::mmap (NULL, len, PROT_READ, MAP_SHARED, fd, 0);
107         if( bfr == MAP_FAILED ) return 0;
108         VFrame *vframe = new VFramePng(bfr, len, xs, ys);
109         if( (w=vframe->get_w()) <= 0 || (h=vframe->get_h()) <= 0 ||
110             vframe->get_data() == 0 ) { delete vframe;  vframe = 0; }
111         ::munmap(bfr, len);
112         return vframe;
113 }
114 VFrame *VFramePng::vframe_png(const char *png_path, double xs, double ys)
115 {
116         VFrame *vframe = 0;
117         int fd = ::open(png_path, O_RDONLY);
118         if( fd >= 0 ) {
119                 vframe = vframe_png(fd, xs, ys);
120                 ::close(fd);
121         }
122         return vframe;
123 }
124
125 VFrame::VFrame(VFrame &frame)
126 {
127         reset_parameters(1);
128         params = new BC_Hash;
129         use_shm = frame.use_shm;
130         allocate_data(0, -1, 0, 0, 0, frame.w, frame.h,
131                 frame.color_model, frame.bytes_per_line);
132         copy_vframe(&frame);
133 }
134
135
136 VFrame::VFrame(int w, int h, int color_model, long bytes_per_line)
137 {
138         reset_parameters(1);
139 //  use bytes_per_line == 0 to allocate default unshared
140         if( !bytes_per_line ) { bytes_per_line = -1;  use_shm = 0; }
141         params = new BC_Hash;
142         allocate_data(data, -1, 0, 0, 0, w, h,
143                 color_model, bytes_per_line);
144 }
145
146 VFrame::VFrame(unsigned char *data, int shmid, int w, int h,
147         int color_model, long bytes_per_line)
148 {
149         reset_parameters(1);
150         params = new BC_Hash;
151         allocate_data(data, shmid, 0, 0, 0, w, h,
152                 color_model, bytes_per_line);
153 }
154
155 VFrame::VFrame(unsigned char *data, int shmid,
156                 long y_offset, long u_offset, long v_offset,
157                 int w, int h, int color_model, long bytes_per_line)
158 {
159         reset_parameters(1);
160         params = new BC_Hash;
161         allocate_data(data, shmid, y_offset, u_offset, v_offset, w, h,
162                 color_model, bytes_per_line);
163 }
164
165 VFrame::VFrame(BC_Bitmap *bitmap, int w, int h,
166                  int color_model, long bytes_per_line)
167 {
168         reset_parameters(1);
169         params = new BC_Hash;
170         int shmid = 0;
171         unsigned char *data = 0;
172         if( bitmap->is_shared() )
173                 shmid = bitmap->get_shmid();
174         else
175                 data = bitmap->get_data();
176         allocate_data(data, shmid,
177                 bitmap->get_y_offset(),
178                 bitmap->get_u_offset(),
179                 bitmap->get_v_offset(),
180                 w, h, color_model, bytes_per_line);
181 }
182
183 VFrame::VFrame()
184 {
185         reset_parameters(1);
186         params = new BC_Hash;
187         this->color_model = BC_COMPRESSED;
188 }
189
190
191
192 VFrame::~VFrame()
193 {
194         clear_objects(1);
195 // Delete effect stack
196         prev_effects.remove_all_objects();
197         next_effects.remove_all_objects();
198         delete params;
199         delete scene;
200 }
201
202 int VFrame::equivalent(VFrame *src, int test_stacks)
203 {
204         return (src->get_color_model() == get_color_model() &&
205                 src->get_w() == get_w() &&
206                 src->get_h() == get_h() &&
207                 src->bytes_per_line == bytes_per_line &&
208                 (!test_stacks || equal_stacks(src)));
209 }
210
211 int VFrame::data_matches(VFrame *frame)
212 {
213         if(data && frame->get_data() &&
214                 frame->params_match(get_w(), get_h(), get_color_model()) &&
215                 get_data_size() == frame->get_data_size())
216         {
217                 int data_size = get_data_size();
218                 unsigned char *ptr1 = get_data();
219                 unsigned char *ptr2 = frame->get_data();
220                 for(int i = 0; i < data_size; i++)
221                 {
222                         if(*ptr1++ != *ptr2++) return 0;
223                 }
224                 return 1;
225         }
226         return 0;
227 }
228
229 // long VFrame::set_shm_offset(long offset)
230 // {
231 //      shm_offset = offset;
232 //      return 0;
233 // }
234 //
235 // long VFrame::get_shm_offset()
236 // {
237 //      return shm_offset;
238 // }
239 //
240 int VFrame::get_memory_type()
241 {
242         return memory_type;
243 }
244
245 int VFrame::params_match(int w, int h, int color_model)
246 {
247         return (this->w == w &&
248                 this->h == h &&
249                 this->color_model == color_model);
250 }
251
252
253 int VFrame::reset_parameters(int do_opengl)
254 {
255         status = 1;
256         scene = 0;
257         field2_offset = -1;
258         memory_type = VFrame::PRIVATE;
259 //      shm_offset = 0;
260         shmid = -1;
261         use_shm = 1;
262         bytes_per_line = 0;
263         data = 0;
264         rows = 0;
265         color_model = 0;
266         compressed_allocated = 0;
267         compressed_size = 0;   // Size of current image
268         w = 0;
269         h = 0;
270         y = u = v = a = 0;
271         y_offset = 0;
272         u_offset = 0;
273         v_offset = 0;
274         sequence_number = -1;
275         timestamp = -1.;
276         is_keyframe = 0;
277         pixel_rgb = 0x000000; // BLACK
278         pixel_yuv = 0x008080;
279         stipple = 0;
280         clear_color = 0x000000;
281         clear_alpha = 0x00;
282
283         if(do_opengl)
284         {
285 // By default, anything is going to be done in RAM
286                 opengl_state = VFrame::RAM;
287                 pbuffer = 0;
288                 texture = 0;
289         }
290
291         prev_effects.set_array_delete();
292         next_effects.set_array_delete();
293         return 0;
294 }
295
296 int VFrame::clear_objects(int do_opengl)
297 {
298 // Remove texture
299         if(do_opengl)
300         {
301                 delete texture;
302                 texture = 0;
303
304                 delete pbuffer;
305                 pbuffer = 0;
306         }
307
308 #ifdef LEAKER
309 if( memory_type != VFrame::SHARED )
310   printf("==del %p from %p\n", data, __builtin_return_address(0));
311 #endif
312
313 // Delete data
314         switch(memory_type)
315         {
316                 case VFrame::PRIVATE:
317 // Memory check
318 // if(this->w * this->h > 1500 * 1100)
319 // printf("VFrame::clear_objects 2 this=%p data=%p\n", this, data);
320                         if(data)
321                         {
322 //printf("VFrame::clear_objects %d this=%p shmid=%p data=%p\n", __LINE__, this, shmid, data);
323                                 if(shmid >= 0)
324                                         shmdt(data);
325                                 else
326                                         free(data);
327 //PRINT_TRACE
328                         }
329
330                         data = 0;
331                         shmid = -1;
332                         break;
333
334                 case VFrame::SHMGET:
335                         if(data)
336                                 shmdt(data);
337                         data = 0;
338                         shmid = -1;
339                         break;
340         }
341
342 // Delete row pointers
343         switch(color_model)
344         {
345                 case BC_COMPRESSED:
346                 case BC_YUV410P:
347                 case BC_YUV411P:
348                 case BC_YUV420P:
349                 case BC_YUV420PI:
350                 case BC_YUV422P:
351                 case BC_YUV444P:
352                 case BC_RGB_FLOATP:
353                 case BC_RGBA_FLOATP:
354                 case BC_GBRP:
355                         break;
356
357                 default:
358                         delete [] rows;
359                         rows = 0;
360                         break;
361         }
362
363
364         return 0;
365 }
366
367 int VFrame::get_field2_offset()
368 {
369         return field2_offset;
370 }
371
372 int VFrame::set_field2_offset(int value)
373 {
374         this->field2_offset = value;
375         return 0;
376 }
377
378 void VFrame::set_keyframe(int value)
379 {
380         this->is_keyframe = value;
381 }
382
383 int VFrame::get_keyframe()
384 {
385         return is_keyframe;
386 }
387
388 void VFrame::get_temp(VFrame *&vfrm, int w, int h, int color_model)
389 {
390         if( vfrm && ( vfrm->color_model != color_model ||
391             vfrm->get_w() != w || vfrm->get_h() != h ) ) {
392                 delete vfrm;  vfrm = 0;
393         }
394         if( !vfrm ) vfrm = new VFrame(w, h, color_model, 0);
395 }
396
397
398
399 VFrameScene* VFrame::get_scene()
400 {
401         return scene;
402 }
403
404 int VFrame::calculate_bytes_per_pixel(int color_model)
405 {
406         return BC_CModels::calculate_pixelsize(color_model);
407 }
408
409 long VFrame::get_bytes_per_line()
410 {
411         return bytes_per_line;
412 }
413
414 long VFrame::get_data_size()
415 {
416         return calculate_data_size(w, h, bytes_per_line, color_model) - 4;
417 }
418
419 long VFrame::calculate_data_size(int w, int h, int bytes_per_line, int color_model)
420 {
421         return BC_CModels::calculate_datasize(w, h, bytes_per_line, color_model);
422 }
423
424 void VFrame::create_row_pointers()
425 {
426         int sz = w * h;
427         switch(color_model) {
428         case BC_YUV410P:
429                 if( this->v_offset ) break;
430                 this->y_offset = 0;
431                 this->u_offset = sz;
432                 this->v_offset = sz + w / 4 * h / 4;
433                 break;
434
435         case BC_YUV420P:
436         case BC_YUV420PI:
437         case BC_YUV411P:
438                 if( this->v_offset ) break;
439                 this->y_offset = 0;
440                 this->u_offset = sz;
441                 this->v_offset = sz + sz / 4;
442                 break;
443
444         case BC_YUV422P:
445                 if( this->v_offset ) break;
446                 this->y_offset = 0;
447                 this->u_offset = sz;
448                 this->v_offset = sz + sz / 2;
449                 break;
450         case BC_YUV444P:
451                 if( this->v_offset ) break;
452                 this->y_offset = 0;
453                 this->u_offset = sz;
454                 this->v_offset = sz + sz;
455                 break;
456         case BC_GBRP:
457                 if( this->v_offset ) break;
458                 this->y_offset = 0;
459                 this->u_offset = sz * sizeof(uint8_t);
460                 this->v_offset = 2 * sz * sizeof(uint8_t);
461                 break;
462         case BC_RGBA_FLOATP:
463                 if( this->v_offset || a ) break;
464                 a = this->data + 3 * sz * sizeof(float);
465         case BC_RGB_FLOATP:
466                 if( this->v_offset ) break;
467                 this->y_offset = 0;
468                 this->u_offset = sz * sizeof(float);
469                 this->v_offset = 2 * sz * sizeof(float);
470                 break;
471
472         default:
473                 rows = new unsigned char*[h];
474                 for(int i = 0; i < h; i++)
475                         rows[i] = &this->data[i * this->bytes_per_line];
476                 return;
477         }
478         y = this->data + this->y_offset;
479         u = this->data + this->u_offset;
480         v = this->data + this->v_offset;
481 }
482
483 int VFrame::allocate_data(unsigned char *data, int shmid,
484                 long y_offset, long u_offset, long v_offset, int w, int h,
485                 int color_model, long bytes_per_line)
486 {
487         this->w = w;
488         this->h = h;
489         this->color_model = color_model;
490         this->bytes_per_pixel = calculate_bytes_per_pixel(color_model);
491         this->y_offset = this->u_offset = this->v_offset = 0;
492 //      if(shmid == 0) {
493 //              printf("VFrame::allocate_data %d shmid == 0\n", __LINE__, shmid);
494 //      }
495
496         this->bytes_per_line = bytes_per_line >= 0 ?
497                 bytes_per_line : this->bytes_per_pixel * w;
498
499 // Allocate data + padding for MMX
500         if(data) {
501 //printf("VFrame::allocate_data %d %p\n", __LINE__, this->data);
502                 memory_type = VFrame::SHARED;
503                 this->data = data;
504                 this->shmid = -1;
505                 this->y_offset = y_offset;
506                 this->u_offset = u_offset;
507                 this->v_offset = v_offset;
508         }
509         else if(shmid >= 0) {
510                 memory_type = VFrame::SHMGET;
511                 this->data = (unsigned char*)shmat(shmid, NULL, 0);
512 //printf("VFrame::allocate_data %d shmid=%d data=%p\n", __LINE__, shmid, this->data);
513                 this->shmid = shmid;
514                 this->y_offset = y_offset;
515                 this->u_offset = u_offset;
516                 this->v_offset = v_offset;
517         }
518         else {
519                 memory_type = VFrame::PRIVATE;
520                 this->data = 0;
521                 int size = calculate_data_size(this->w, this->h,
522                         this->bytes_per_line, this->color_model);
523                 if( use_shm && size >= SHM_MIN_SIZE &&
524                     BC_WindowBase::get_resources()->use_vframe_shm() ) {
525                         this->shmid = shmget(IPC_PRIVATE, size, IPC_CREAT | 0777);
526                         if( this->shmid >= 0 ) {
527                                 this->data = (unsigned char*)shmat(this->shmid, NULL, 0);
528 //printf("VFrame::allocate_data %d %d %d %p\n", __LINE__, size, this->shmid, this->data);
529 // This causes it to automatically delete when the program exits.
530                                 shmctl(this->shmid, IPC_RMID, 0);
531                         }
532                         else {
533                                 printf("VFrame::allocate_data %d could not allocate"
534                                         " shared memory, %dx%d (model %d) size=0x%08x\n",
535                                         __LINE__, w, h, color_model, size);
536                                 BC_Trace::dump_shm_stats(stdout);
537                         }
538                 }
539 // Have to use malloc for libpng
540                 if( !this->data ) {
541                         this->data = (unsigned char *)malloc(size);
542                         this->shmid = -1;
543                 }
544 // Memory check
545 // if(this->w * this->h > 1500 * 1100)
546 // printf("VFrame::allocate_data 2 this=%p w=%d h=%d this->data=%p\n",
547 // this, this->w, this->h, this->data);
548
549                 if(!this->data)
550                         printf("VFrame::allocate_data %dx%d: memory exhausted.\n", this->w, this->h);
551 #ifdef LEAKER
552 printf("==new %p from %p sz %d\n", this->data, __builtin_return_address(0), size);
553 #endif
554
555 //printf("VFrame::allocate_data %d %p data=%p %d %d\n", __LINE__, this, this->data, this->w, this->h);
556 //if(size > 1000000) printf("VFrame::allocate_data %d\n", size);
557         }
558
559 // Create row pointers
560         create_row_pointers();
561         return 0;
562 }
563
564 void VFrame::set_memory(unsigned char *data,
565         int shmid,
566         long y_offset,
567         long u_offset,
568         long v_offset)
569 {
570         clear_objects(0);
571
572         if(data)
573         {
574                 memory_type = VFrame::SHARED;
575                 this->data = data;
576                 this->shmid = -1;
577                 this->y_offset = y_offset;
578                 this->u_offset = u_offset;
579                 this->v_offset = v_offset;
580         }
581         else
582         if(shmid >= 0)
583         {
584                 memory_type = VFrame::SHMGET;
585                 this->data = (unsigned char*)shmat(shmid, NULL, 0);
586                 this->shmid = shmid;
587         }
588
589         y = this->data + this->y_offset;
590         u = this->data + this->u_offset;
591         v = this->data + this->v_offset;
592
593         create_row_pointers();
594 }
595
596 void VFrame::set_memory(BC_Bitmap *bitmap)
597 {
598         int shmid = 0;
599         unsigned char *data = 0;
600         if( bitmap->is_shared() && !bitmap->is_zombie() )
601                 shmid = bitmap->get_shmid();
602         else
603                 data = bitmap->get_data();
604         set_memory(data, shmid,
605                 bitmap->get_y_offset(),
606                 bitmap->get_u_offset(),
607                 bitmap->get_v_offset());
608 }
609
610 void VFrame::set_compressed_memory(unsigned char *data,
611         int shmid,
612         int data_size,
613         int data_allocated)
614 {
615         clear_objects(0);
616
617         if(data)
618         {
619                 memory_type = VFrame::SHARED;
620                 this->data = data;
621                 this->shmid = -1;
622         }
623         else
624         if(shmid >= 0)
625         {
626                 memory_type = VFrame::SHMGET;
627                 this->data = (unsigned char*)shmat(shmid, NULL, 0);
628                 this->shmid = shmid;
629         }
630
631         this->compressed_allocated = data_allocated;
632         this->compressed_size = data_size;
633 }
634
635
636 // Reallocate uncompressed buffer with or without alpha
637 int VFrame::reallocate(
638         unsigned char *data,
639         int shmid,
640         long y_offset,
641         long u_offset,
642         long v_offset,
643         int w,
644         int h,
645         int color_model,
646         long bytes_per_line)
647 {
648 //      if(shmid == 0) printf("VFrame::reallocate %d shmid=%d\n", __LINE__, shmid);
649         clear_objects(0);
650 //      reset_parameters(0);
651         allocate_data(data,
652                 shmid,
653                 y_offset,
654                 u_offset,
655                 v_offset,
656                 w,
657                 h,
658                 color_model,
659                 bytes_per_line);
660         return 0;
661 }
662
663 int VFrame::allocate_compressed_data(long bytes)
664 {
665         if(bytes < 1) return 1;
666
667 // Want to preserve original contents
668         if(data && compressed_allocated < bytes)
669         {
670                 int new_shmid = -1;
671                 unsigned char *new_data = 0;
672                 if(BC_WindowBase::get_resources()->use_vframe_shm() && use_shm)
673                 {
674                         new_shmid = shmget(IPC_PRIVATE,
675                                 bytes,
676                                 IPC_CREAT | 0777);
677                         new_data = (unsigned char*)shmat(new_shmid, NULL, 0);
678                         shmctl(new_shmid, IPC_RMID, 0);
679                 }
680                 else
681                 {
682 // Have to use malloc for libpng
683                         new_data = (unsigned char *)malloc(bytes);
684                 }
685
686                 bcopy(data, new_data, compressed_allocated);
687 UNBUFFER(data);
688
689                 if(memory_type == VFrame::PRIVATE)
690                 {
691                         if(shmid > 0) {
692                                 if(data)
693                                         shmdt(data);
694                         }
695                         else
696                                 free(data);
697                 }
698                 else
699                 if(memory_type == VFrame::SHMGET)
700                 {
701                         if(data)
702                                 shmdt(data);
703                 }
704
705                 data = new_data;
706                 shmid = new_shmid;
707                 compressed_allocated = bytes;
708         }
709         else
710         if(!data)
711         {
712                 if(BC_WindowBase::get_resources()->use_vframe_shm() && use_shm)
713                 {
714                         shmid = shmget(IPC_PRIVATE,
715                                 bytes,
716                                 IPC_CREAT | 0777);
717                         data = (unsigned char*)shmat(shmid, NULL, 0);
718                         shmctl(shmid, IPC_RMID, 0);
719                 }
720                 else
721                 {
722 // Have to use malloc for libpng
723                         data = (unsigned char *)malloc(bytes);
724                 }
725
726                 compressed_allocated = bytes;
727                 compressed_size = 0;
728         }
729
730         return 0;
731 }
732
733 int VFramePng::read_png(const unsigned char *data, long sz, double xscale, double yscale)
734 {
735 // Test for RAW format
736         if(data[0] == 'R' && data[1] == 'A' && data[2] == 'W' && data[3] == ' ') {
737                 int new_color_model = BC_RGBA8888;
738                 w = data[4] | (data[5] << 8) | (data[6]  << 16) | (data[7]  << 24);
739                 h = data[8] | (data[9] << 8) | (data[10] << 16) | (data[11] << 24);
740                 int components = data[12];
741                 new_color_model = components == 3 ? BC_RGB888 : BC_RGBA8888;
742 // This shares the data directly
743 //              reallocate(data + 20, 0, 0, 0, w, h, new_color_model, -1);
744
745 // Can't use shared data for theme since button constructions overlay the
746 // images directly.
747                 reallocate(NULL, -1, 0, 0, 0, w, h, new_color_model, -1);
748                 memcpy(get_data(), data + 16, w * h * components);
749
750         }
751         else if(data[0] == 0x89 && data[1] == 'P' && data[2] == 'N' && data[3] == 'G') {
752                 int have_alpha = 0;
753                 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
754                 png_infop info_ptr = png_create_info_struct(png_ptr);
755                 int new_color_model;
756
757                 image_offset = 0;
758                 image = data;  image_size = sz;
759                 png_set_read_fn(png_ptr, this, PngReadFunction::png_read_function);
760                 png_read_info(png_ptr, info_ptr);
761
762                 w = png_get_image_width(png_ptr, info_ptr);
763                 h = png_get_image_height(png_ptr, info_ptr);
764
765                 int src_color_model = png_get_color_type(png_ptr, info_ptr);
766
767                 /* tell libpng to strip 16 bit/color files down to 8 bits/color */
768                 png_set_strip_16(png_ptr);
769
770                 /* extract multiple pixels with bit depths of 1, 2, and 4 from a single
771                  * byte into separate bytes (useful for paletted and grayscale images).
772                  */
773                 png_set_packing(png_ptr);
774
775                 /* expand paletted colors into true RGB triplets */
776                 if (src_color_model == PNG_COLOR_TYPE_PALETTE)
777                         png_set_expand(png_ptr);
778
779                 /* expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel */
780                 if (src_color_model == PNG_COLOR_TYPE_GRAY && png_get_bit_depth(png_ptr, info_ptr) < 8)
781                         png_set_expand(png_ptr);
782
783                 if (src_color_model == PNG_COLOR_TYPE_GRAY ||
784                     src_color_model == PNG_COLOR_TYPE_GRAY_ALPHA)
785                         png_set_gray_to_rgb(png_ptr);
786
787                 /* expand paletted or RGB images with transparency to full alpha channels
788                  * so the data will be available as RGBA quartets */
789                 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)){
790                         have_alpha = 1;
791                         png_set_expand(png_ptr);
792                 }
793
794                 switch(src_color_model)
795                 {
796                         case PNG_COLOR_TYPE_GRAY:
797                         case PNG_COLOR_TYPE_RGB:
798                                 new_color_model = BC_RGB888;
799                                 break;
800
801                         case PNG_COLOR_TYPE_GRAY_ALPHA:
802                         case PNG_COLOR_TYPE_RGB_ALPHA:
803                         default:
804                                 new_color_model = BC_RGBA8888;
805                                 break;
806
807                         case PNG_COLOR_TYPE_PALETTE:
808                                 if(have_alpha)
809                                         new_color_model = BC_RGBA8888;
810                                 else
811                                         new_color_model = BC_RGB888;
812                 }
813
814                 reallocate(NULL, -1, 0, 0, 0, w, h, new_color_model, -1);
815
816 //printf("VFrame::read_png %d %d %d %p\n", __LINE__, w, h, get_rows());
817                 png_read_image(png_ptr, get_rows());
818                 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
819         }
820         else {
821                 printf("VFrame::read_png %d: unknown file format"
822                         " 0x%02x 0x%02x 0x%02x 0x%02x\n",
823                         __LINE__, data[4], data[5], data[6], data[7]);
824                 return 1;
825         }
826         int ww = w * xscale, hh = h * yscale;
827         if( ww < 1 ) ww = 1;
828         if( hh < 1 ) hh = 1;
829         if( ww != w || hh != h ) {
830                 VFrame vframe(*this);
831                 reallocate(NULL, -1, 0, 0, 0, ww, hh, color_model, -1);
832                 transfer_from(&vframe);
833         }
834         return 0;
835 }
836
837 int VFrame::write_png(const char *path)
838 {
839         VFrame *vframe = this;
840         png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
841         png_infop info_ptr = png_create_info_struct(png_ptr);
842         FILE *out_fd = fopen(path, "w");
843         if(!out_fd) {
844                 printf("VFrame::write_png %d %s %s\n", __LINE__, path, strerror(errno));
845                 return 1;
846         }
847
848         int png_cmodel = PNG_COLOR_TYPE_RGB;
849         int bc_cmodel = get_color_model();
850         switch( bc_cmodel ) {
851         case BC_RGB888:                                          break;
852         case BC_RGBA8888: png_cmodel = PNG_COLOR_TYPE_RGB_ALPHA; break;
853         case BC_A8:       png_cmodel = PNG_COLOR_TYPE_GRAY;      break;
854         default:
855                 bc_cmodel = BC_RGB888;
856                 if( BC_CModels::has_alpha(bc_cmodel) ) {
857                         bc_cmodel = BC_RGBA8888;
858                         png_cmodel = PNG_COLOR_TYPE_RGB_ALPHA;
859                 }
860                 vframe = new VFrame(get_w(), get_h(), bc_cmodel, 0);
861                 vframe->transfer_from(this);
862                 break;
863         }
864         png_init_io(png_ptr, out_fd);
865         png_set_compression_level(png_ptr, 9);
866         png_set_IHDR(png_ptr, info_ptr, get_w(), get_h(), 8, png_cmodel,
867                 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
868         png_write_info(png_ptr, info_ptr);
869         png_write_image(png_ptr, vframe->get_rows());
870         png_write_end(png_ptr, info_ptr);
871         png_destroy_write_struct(&png_ptr, &info_ptr);
872         fclose(out_fd);
873         if( vframe != this ) delete vframe;
874         return 0;
875 }
876
877 void VFrame::write_ppm(VFrame *vfrm, const char *fmt, ...)
878 {
879         va_list ap;
880         va_start(ap, fmt);
881         char fn[BCTEXTLEN];
882         vsnprintf(fn, sizeof(fn), fmt, ap);
883         va_end(ap);
884         FILE *fp = fopen(fn,"w");
885         if( !fp ) { perror("write_ppm"); return; }
886         VFrame *frm = vfrm;
887         if( frm->get_color_model() != BC_RGB888 ) {
888                 frm = new VFrame(frm->get_w(), frm->get_h(), BC_RGB888);
889                 frm->transfer_from(vfrm);
890         }
891         int w = frm->get_w(), h = frm->get_h();
892         fprintf(fp,"P6\n%d %d\n255\n",w,h);
893         unsigned char **rows = frm->get_rows();
894         for( int i=0; i<h; ++i ) fwrite(rows[i],3,w,fp);
895         fclose(fp);
896         if( frm != vfrm ) delete frm;
897 }
898
899
900 #define ZERO_YUV(components, type, max) \
901 { \
902         for(int i = 0; i < h; i++) \
903         { \
904                 type *row = (type*)get_rows()[i]; \
905                 for(int j = 0; j < w; j++) \
906                 { \
907                         row[j * components] = 0; \
908                         row[j * components + 1] = (max + 1) / 2; \
909                         row[j * components + 2] = (max + 1) / 2; \
910                         if(components == 4) row[j * components + 3] = 0; \
911                 } \
912         } \
913 }
914
915 void VFrame::black_frame()
916 {
917         int sz = w * h;
918 //printf("VFrame::black_frame %d\n", __LINE__);
919         switch(color_model) {
920         case BC_COMPRESSED:
921                 break;
922
923         case BC_YUV410P:
924                 bzero(get_y(), sz);
925                 memset(get_u(), 0x80, w / 4 * h / 4);
926                 memset(get_v(), 0x80, w / 4 * h / 4);
927                 break;
928
929         case BC_YUV411P:
930         case BC_YUV420P:
931         case BC_YUV420PI:
932                 bzero(get_y(), sz);
933                 memset(get_u(), 0x80, sz / 4);
934                 memset(get_v(), 0x80, sz / 4);
935                 break;
936
937         case BC_YUV422P:
938                 bzero(get_y(), sz);
939                 memset(get_u(), 0x80, sz / 2);
940                 memset(get_v(), 0x80, sz / 2);
941                 break;
942
943         case BC_GBRP:
944                 bzero(get_y(), sz);
945                 bzero(get_u(), sz);
946                 bzero(get_b(), sz);
947                 break;
948
949         case BC_RGBA_FLOATP: if( a ) {
950                 float *ap = (float *)a;
951                 for( int i=sz; --i>=0; ++ap ) *ap = 0.f; }
952         case BC_RGB_FLOATP: {
953                 float *rp = (float *)y;
954                 for( int i=sz; --i>=0; ++rp ) *rp = 0.f;
955                 float *gp = (float *)u;
956                 for( int i=sz; --i>=0; ++gp ) *gp = 0.f;
957                 float *bp = (float *)v;
958                 for( int i=sz; --i>=0; ++bp ) *bp = 0.f;
959                 break; }
960         case BC_YUV444P:
961                 bzero(get_y(), sz);
962                 memset(get_u(), 0x80, sz);
963                 memset(get_v(), 0x80, sz);
964                 break;
965
966         case BC_YUV888:
967                 ZERO_YUV(3, unsigned char, 0xff);
968                 break;
969
970         case BC_YUVA8888:
971                 ZERO_YUV(4, unsigned char, 0xff);
972                 break;
973
974         case BC_YUV161616:
975                 ZERO_YUV(3, uint16_t, 0xffff);
976                 break;
977
978         case BC_YUVA16161616:
979                 ZERO_YUV(4, uint16_t, 0xffff);
980                 break;
981
982         default:
983                 bzero(data, calculate_data_size(w, h, bytes_per_line, color_model));
984                 break;
985         }
986 }
987
988 void VFrame::set_clear_color(int color, int alpha)
989 {
990         clear_color = color;
991         clear_alpha = alpha;
992 }
993 int VFrame::get_clear_color() { return clear_color; }
994 int VFrame::get_clear_alpha() { return clear_alpha; }
995
996 void VFrame::clear_frame()
997 {
998         if( clear_color >= 0 &&
999             !BC_CModels::init_color(clear_color, clear_alpha,
1000                         get_rows(), get_color_model(), get_y(), get_u(), get_v(),
1001                         0,0, get_w(),get_h(), get_bytes_per_line()) )
1002                 return;
1003         black_frame();
1004 }
1005
1006 void VFrame::rotate90()
1007 {
1008 // Allocate new frame
1009         int new_w = h, new_h = w;
1010         VFrame new_frame(new_w, new_h, color_model);
1011         unsigned char **new_rows = new_frame.get_rows();
1012 // Copy data
1013         for(int in_y = 0, out_x = new_w - 1; in_y < h; in_y++, out_x--)
1014         {
1015                 for(int in_x = 0, out_y = 0; in_x < w; in_x++, out_y++)
1016                 {
1017                         for(int k = 0; k < bytes_per_pixel; k++)
1018                         {
1019                                 new_rows[out_y][out_x * bytes_per_pixel + k] =
1020                                         rows[in_y][in_x * bytes_per_pixel + k];
1021                         }
1022                 }
1023         }
1024
1025 // Swap frames
1026 // swap memory
1027         unsigned char *new_data = new_frame.data;
1028         new_frame.data = data;
1029         data = new_data;
1030 // swap rows
1031         new_rows = new_frame.rows;
1032         new_frame.rows = rows;
1033         rows = new_rows;
1034 // swap shmid
1035         int new_shmid = new_frame.shmid;
1036         new_frame.shmid = shmid;
1037         shmid = new_shmid;
1038 // swap bytes_per_line
1039         int new_bpl = new_frame.bytes_per_line;
1040         new_frame.bytes_per_line = bytes_per_line;
1041         bytes_per_line = new_bpl;
1042         new_frame.clear_objects(0);
1043
1044         w = new_frame.w;
1045         h = new_frame.h;
1046 }
1047
1048 void VFrame::rotate270()
1049 {
1050 // Allocate new frame
1051         int new_w = h, new_h = w;
1052         VFrame new_frame(new_w, new_h, color_model);
1053         unsigned char **new_rows = new_frame.get_rows();
1054 // Copy data
1055         for(int in_y = 0, out_x = 0; in_y < h; in_y++, out_x++)
1056         {
1057                 for(int in_x = 0, out_y = new_h - 1; in_x < w; in_x++, out_y--)
1058                 {
1059                         for(int k = 0; k < bytes_per_pixel; k++)
1060                         {
1061                                 new_rows[out_y][out_x * bytes_per_pixel + k] =
1062                                         rows[in_y][in_x * bytes_per_pixel + k];
1063                         }
1064                 }
1065         }
1066
1067 // Swap frames
1068 // swap memory
1069         unsigned char *new_data = new_frame.data;
1070         new_frame.data = data;
1071         data = new_data;
1072 // swap rows
1073         new_rows = new_frame.rows;
1074         new_frame.rows = rows;
1075         rows = new_rows;
1076 // swap shmid
1077         int new_shmid = new_frame.shmid;
1078         new_frame.shmid = shmid;
1079         shmid = new_shmid;
1080 // swap bytes_per_line
1081         int new_bpl = new_frame.bytes_per_line;
1082         new_frame.bytes_per_line = bytes_per_line;
1083         bytes_per_line = new_bpl;
1084         new_frame.clear_objects(0);
1085
1086         w = new_frame.w;
1087         h = new_frame.h;
1088 }
1089
1090 void VFrame::flip_vert()
1091 {
1092         unsigned char temp[bytes_per_line];
1093         for( int i=0, j=h; --j>i; ++i ) {
1094                 memcpy(temp, rows[j], bytes_per_line);
1095                 memcpy(rows[j], rows[i], bytes_per_line);
1096                 memcpy(rows[i], temp, bytes_per_line);
1097         }
1098 }
1099
1100 void VFrame::flip_horiz()
1101 {
1102         unsigned char temp[32];
1103         for(int i = 0; i < h; i++)
1104         {
1105                 unsigned char *row = rows[i];
1106                 for(int j = 0; j < bytes_per_line / 2; j += bytes_per_pixel)
1107                 {
1108                         memcpy(temp, row + j, bytes_per_pixel);
1109                         memcpy(row + j, row + bytes_per_line - j - bytes_per_pixel, bytes_per_pixel);
1110                         memcpy(row + bytes_per_line - j - bytes_per_pixel, temp, bytes_per_pixel);
1111                 }
1112         }
1113 }
1114
1115
1116
1117 int VFrame::copy_from(VFrame *frame)
1118 {
1119         if(this->w != frame->get_w() ||
1120                 this->h != frame->get_h())
1121         {
1122                 printf("VFrame::copy_from %d sizes differ src %dx%d != dst %dx%d\n",
1123                         __LINE__,
1124                         frame->get_w(),
1125                         frame->get_h(),
1126                         get_w(),
1127                         get_h());
1128                 return 1;
1129         }
1130
1131         int w = MIN(this->w, frame->get_w());
1132         int h = MIN(this->h, frame->get_h());
1133         timestamp = frame->timestamp;
1134
1135         switch(frame->color_model)
1136         {
1137                 case BC_COMPRESSED:
1138                         allocate_compressed_data(frame->compressed_size);
1139                         memcpy(data, frame->data, frame->compressed_size);
1140                         this->compressed_size = frame->compressed_size;
1141                         break;
1142
1143                 case BC_YUV410P:
1144                         memcpy(get_y(), frame->get_y(), w * h);
1145                         memcpy(get_u(), frame->get_u(), w / 4 * h / 4);
1146                         memcpy(get_v(), frame->get_v(), w / 4 * h / 4);
1147                         break;
1148
1149                 case BC_YUV420P:
1150                 case BC_YUV420PI:
1151                 case BC_YUV411P:
1152 //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());
1153                         memcpy(get_y(), frame->get_y(), w * h);
1154                         memcpy(get_u(), frame->get_u(), w * h / 4);
1155                         memcpy(get_v(), frame->get_v(), w * h / 4);
1156                         break;
1157
1158                 case BC_YUV422P:
1159 //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());
1160                         memcpy(get_y(), frame->get_y(), w * h);
1161                         memcpy(get_u(), frame->get_u(), w * h / 2);
1162                         memcpy(get_v(), frame->get_v(), w * h / 2);
1163                         break;
1164
1165                 case BC_YUV444P:
1166 //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());
1167                         memcpy(get_y(), frame->get_y(), w * h);
1168                         memcpy(get_u(), frame->get_u(), w * h);
1169                         memcpy(get_v(), frame->get_v(), w * h);
1170                         break;
1171                 default:
1172 // printf("VFrame::copy_from %d\n", calculate_data_size(w,
1173 //                              h,
1174 //                              -1,
1175 //                              frame->color_model));
1176 // Copy without extra 4 bytes in case the source is a hardware device
1177                         memmove(data, frame->data, get_data_size());
1178                         break;
1179         }
1180
1181         return 0;
1182 }
1183
1184 int VFrame::transfer_from(VFrame *that, int bg_color, int in_x, int in_y, int in_w, int in_h)
1185 {
1186         timestamp = that->timestamp;
1187         copy_params(that);
1188
1189         if( in_x == 0 && in_y == 0 && in_w == that->get_w() && in_h == that->get_h() &&
1190             bg_color == 0 && this->get_color_model() == that->get_color_model() &&
1191             this->get_w() == that->get_w() && this->get_h() == that->get_h() &&
1192             this->get_bytes_per_line() == that->get_bytes_per_line() )
1193                 return this->copy_from(that);
1194
1195 #if 0
1196         BC_CModels::transfer(
1197                 this->get_rows(), that->get_rows(),          // Packed data out/in
1198                 this->get_y(), this->get_u(), this->get_v(), // Planar data out/in
1199                 that->get_y(), that->get_u(), that->get_v(),
1200                 0, 0, that->get_w(), that->get_h(),          // Dimensions in/out
1201                 0, 0, this->get_w(), this->get_h(),
1202                 that->get_color_model(), this->get_color_model(), // Color models in/out
1203                 bg_color,                                    // alpha blend bg_color
1204                 that->get_bytes_per_line(),
1205                 this->get_bytes_per_line());                 // rowspans (of luma for YUV)
1206 #else
1207         unsigned char *in_ptrs[4], *out_ptrs[4];
1208         unsigned char **inp, **outp;
1209         if( BC_CModels::is_planar(that->get_color_model()) ) {
1210                 in_ptrs[0] = that->get_y();
1211                 in_ptrs[1] = that->get_u();
1212                 in_ptrs[2] = that->get_v();
1213                 in_ptrs[3] = that->get_a();
1214                 inp = in_ptrs;
1215         }
1216         else
1217                 inp = that->get_rows();
1218         if( BC_CModels::is_planar(this->get_color_model()) ) {
1219                 out_ptrs[0] = this->get_y();
1220                 out_ptrs[1] = this->get_u();
1221                 out_ptrs[2] = this->get_v();
1222                 out_ptrs[3] = this->get_a();
1223                 outp = out_ptrs;
1224         }
1225         else
1226                 outp = this->get_rows();
1227         BC_CModels::transfer(outp, this->get_color_model(),
1228                         0, 0, this->get_w(), this->get_h(),
1229                         this->get_bytes_per_line(),
1230                 inp, that->get_color_model(),
1231                         in_x, in_y, in_w, in_h,
1232                         that->get_bytes_per_line(),
1233                 bg_color);
1234 #endif
1235         return 0;
1236 }
1237
1238
1239 int VFrame::get_scale_tables(int *column_table, int *row_table,
1240                         int in_x1, int in_y1, int in_x2, int in_y2,
1241                         int out_x1, int out_y1, int out_x2, int out_y2)
1242 {
1243         int i;
1244         float w_in = in_x2 - in_x1;
1245         float h_in = in_y2 - in_y1;
1246         int w_out = out_x2 - out_x1;
1247         int h_out = out_y2 - out_y1;
1248
1249         float hscale = w_in / w_out;
1250         float vscale = h_in / h_out;
1251
1252         for(i = 0; i < w_out; i++)
1253         {
1254                 column_table[i] = (int)(hscale * i);
1255         }
1256
1257         for(i = 0; i < h_out; i++)
1258         {
1259                 row_table[i] = (int)(vscale * i) + in_y1;
1260         }
1261         return 0;
1262 }
1263
1264 void VFrame::push_prev_effect(const char *name)
1265 {
1266         char *ptr;
1267         prev_effects.append(ptr = new char[strlen(name) + 1]);
1268         strcpy(ptr, name);
1269         if(prev_effects.total > MAX_STACK_ELEMENTS) prev_effects.remove_object(0);
1270 }
1271
1272 void VFrame::pop_prev_effect()
1273 {
1274         if(prev_effects.total)
1275                 prev_effects.remove_object(prev_effects.last());
1276 }
1277
1278 void VFrame::push_next_effect(const char *name)
1279 {
1280         char *ptr;
1281         next_effects.append(ptr = new char[strlen(name) + 1]);
1282         strcpy(ptr, name);
1283         if(next_effects.total > MAX_STACK_ELEMENTS) next_effects.remove_object(0);
1284 }
1285
1286 void VFrame::pop_next_effect()
1287 {
1288         if(next_effects.total)
1289                 next_effects.remove_object(next_effects.last());
1290 }
1291
1292 const char* VFrame::get_next_effect(int number)
1293 {
1294         if(!next_effects.total) return "";
1295         else
1296         if(number > next_effects.total - 1) number = next_effects.total - 1;
1297
1298         return next_effects.values[next_effects.total - number - 1];
1299 }
1300
1301 const char* VFrame::get_prev_effect(int number)
1302 {
1303         if(!prev_effects.total) return "";
1304         else
1305         if(number > prev_effects.total - 1) number = prev_effects.total - 1;
1306
1307         return prev_effects.values[prev_effects.total - number - 1];
1308 }
1309
1310 BC_Hash* VFrame::get_params()
1311 {
1312         return params;
1313 }
1314
1315 void VFrame::clear_stacks()
1316 {
1317         next_effects.remove_all_objects();
1318         prev_effects.remove_all_objects();
1319         params->clear();
1320         status = 1;
1321 }
1322
1323 void VFrame::copy_params(VFrame *src)
1324 {
1325         status = src->status;
1326         params->copy_from(src->params);
1327 }
1328
1329 void VFrame::copy_stacks(VFrame *src)
1330 {
1331         clear_stacks();
1332
1333         for( int i=0; i < src->next_effects.total; ++i )
1334                 next_effects.append(cstrdup(src->next_effects[i]));
1335         for( int i=0; i < src->prev_effects.total; ++i )
1336                 prev_effects.append(cstrdup(src->prev_effects[i]));
1337
1338         copy_params(src);
1339 }
1340
1341 int VFrame::copy_vframe(VFrame *frame)
1342 {
1343         copy_stacks(frame);
1344         return copy_from(frame);
1345 }
1346
1347 int VFrame::equal_stacks(VFrame *src)
1348 {
1349         for(int i = 0; i < src->next_effects.total && i < next_effects.total; i++)
1350         {
1351                 if(strcmp(src->next_effects.values[i], next_effects.values[i])) return 0;
1352         }
1353
1354         for(int i = 0; i < src->prev_effects.total && i < prev_effects.total; i++)
1355         {
1356                 if(strcmp(src->prev_effects.values[i], prev_effects.values[i])) return 0;
1357         }
1358
1359         if(!params->equivalent(src->params)) return 0;
1360         return 1;
1361 }
1362
1363 void VFrame::dump_stacks()
1364 {
1365         printf("VFrame::dump_stacks\n");
1366         printf("        next_effects:\n");
1367         for(int i = next_effects.total - 1; i >= 0; i--)
1368                 printf("                %s\n", next_effects.values[i]);
1369         printf("        prev_effects:\n");
1370         for(int i = prev_effects.total - 1; i >= 0; i--)
1371                 printf("                %s\n", prev_effects.values[i]);
1372 }
1373
1374 void VFrame::dump_params()
1375 {
1376         params->dump();
1377 }
1378
1379 void VFrame::dump()
1380 {
1381         printf("VFrame::dump %d this=%p\n", __LINE__, this);
1382         printf("    w=%d h=%d colormodel=%d rows=%p use_shm=%d shmid=%d\n",
1383                 w, h, color_model, rows, use_shm, shmid);
1384 }
1385
1386
1387 int VFrame::get_memory_usage()
1388 {
1389         if(get_compressed_allocated()) return get_compressed_allocated();
1390         return get_h() * get_bytes_per_line();
1391 }
1392
1393 // rgb component colors (eg. from colors.h)
1394 // a (~alpha) transparency, 0x00==solid .. 0xff==transparent
1395 void VFrame::set_pixel_color(int rgb, int a)
1396 {
1397         pixel_rgb = (rgb&0xffffff) | ~a<<24;
1398         int ir = 0xff & (pixel_rgb >> 16);
1399         int ig = 0xff & (pixel_rgb >> 8);
1400         int ib = 0xff & (pixel_rgb >> 0);
1401         YUV::yuv.rgb_to_yuv_8(ir, ig, ib);
1402         pixel_yuv =  (~a<<24) | (ir<<16) | (ig<<8) | (ib<<0);
1403 }
1404
1405 void VFrame::set_stiple(int mask)
1406 {
1407         stipple = mask;
1408 }
1409
1410 int VFrame::draw_pixel(int x, int y)
1411 {
1412         if( x < 0 || y < 0 || x >= get_w() || y >= get_h() ) return 1;
1413
1414 #define DRAW_PIXEL(type, r, g, b, comps, a) { \
1415         type **rows = (type**)get_rows(); \
1416         type *rp = rows[y], *bp = rp + x*comps; \
1417         bp[0] = r; \
1418         if( comps > 1 ) { bp[1] = g; bp[2] = b; } \
1419         if( comps == 4 )  bp[3] = a; \
1420 }
1421         float fr = 0, fg = 0, fb = 0, fa = 0;
1422         int pixel_color = BC_CModels::is_yuv(color_model) ? pixel_yuv : pixel_rgb;
1423         int ir = (0xff & (pixel_color >> 16));
1424         int ig = (0xff & (pixel_color >> 8));
1425         int ib = (0xff & (pixel_color >> 0));
1426         int ia = (0xff & (pixel_color >> 24)) ^ 0xff;  // transparency, not opacity
1427         if( (x+y) & stipple ) {
1428                 ir = 255 - ir;  ig = 255 - ig;  ib = 255 - ib;
1429         }
1430         int rr = (ir<<8) | ir, gg = (ig<<8) | ig, bb = (ib<<8) | ib, aa = (ia<<8) | ia;
1431         if( BC_CModels::is_float(color_model) ) {
1432                 fr = rr/65535.f;  fg = gg/65535.f;  fb = bb/65535.f;  fa = aa/65535.f;
1433         }
1434
1435         switch(get_color_model()) {
1436         case BC_A8:
1437                 DRAW_PIXEL(uint8_t, ib, 0, 0, 1, 0);
1438                 break;
1439         case BC_RGB888:
1440         case BC_YUV888:
1441                 DRAW_PIXEL(uint8_t, ir, ig, ib, 3, 0);
1442                 break;
1443         case BC_RGBA8888:
1444         case BC_YUVA8888:
1445                 DRAW_PIXEL(uint8_t, ir, ig, ib, 4, ia);
1446                 break;
1447         case BC_RGB161616:
1448         case BC_YUV161616:
1449                 DRAW_PIXEL(uint16_t, rr, gg, bb, 3, 0);
1450                 break;
1451         case BC_RGBA16161616:
1452         case BC_YUVA16161616:
1453                 DRAW_PIXEL(uint16_t, rr, gg, bb, 4, aa);
1454                 break;
1455         case BC_RGB_FLOAT:
1456                 DRAW_PIXEL(float, fr, fg, fb, 3, 0);
1457                 break;
1458         case BC_RGBA_FLOAT:
1459                 DRAW_PIXEL(float, fr, fg, fb, 4, fa);
1460                 break;
1461         }
1462         return 0;
1463 }
1464
1465
1466 // Bresenham's
1467 void VFrame::draw_line(int x1, int y1, int x2, int y2)
1468 {
1469         if( y1 > y2 ) {
1470                 int tx = x1;  x1 = x2;  x2 = tx;
1471                 int ty = y1;  y1 = y2;  y2 = ty;
1472         }
1473
1474         int x = x1, y = y1;
1475         int dx = x2-x1, dy = y2-y1;
1476         int dx2 = 2*dx, dy2 = 2*dy;
1477         if( dx < 0 ) dx = -dx;
1478         int r = dx > dy ? dx : dy, n = r;
1479         int dir = 0;
1480         if( dx2 < 0 ) dir += 1;
1481         if( dy >= dx ) {
1482                 if( dx2 >= 0 ) do {     /* +Y, +X */
1483                         draw_pixel(x, y++);
1484                         if( (r -= dx2) < 0 ) { r += dy2;  ++x; }
1485                 } while( --n >= 0 );
1486                 else do {               /* +Y, -X */
1487                         draw_pixel(x, y++);
1488                         if( (r += dx2) < 0 ) { r += dy2;  --x; }
1489                 } while( --n >= 0 );
1490         }
1491         else {
1492                 if( dx2 >= 0 ) do {     /* +X, +Y */
1493                         draw_pixel(x++, y);
1494                         if( (r -= dy2) < 0 ) { r += dx2;  ++y; }
1495                 } while( --n >= 0 );
1496                 else do {               /* -X, +Y */
1497                         draw_pixel(x--, y);
1498                         if( (r -= dy2) < 0 ) { r -= dx2;  ++y; }
1499                 } while( --n >= 0 );
1500         }
1501 }
1502
1503 // g++ -dD -E - < /dev/null | grep DBL_EPSILON
1504 #ifndef __DBL_EPSILON__
1505 #define __DBL_EPSILON__ ((double)2.22044604925031308085e-16L)
1506 #endif
1507 // weakest fraction * graphics integer range
1508 #define RND_EPSILON (__DBL_EPSILON__*65536)
1509
1510 class smooth_line {
1511         int rnd(double v) { return round(v)+RND_EPSILON; }
1512         VFrame *vframe;
1513 public:
1514         int sx, sy, ex, ey;     /* current point, end point */
1515         int xs, ys;             /* x/y quadrant sign -1/1 */
1516         int64_t A, B, C;        /* quadratic coefficients */
1517         int64_t r, dx, dy;      /* residual, dr/dx and dr/dy */
1518         int xmxx, xmxy;         /* x,y at apex */
1519         int done;
1520
1521         void init0(int x1,int y1, int x2,int y2, int x3,int y3, int top);
1522         void init1(int x1,int y1, int x2,int y2, int x3,int y3);
1523         int64_t rx() { return r + xs*8*dx + 4*A; }
1524         void moveX(int64_t r) {
1525                 dx += xs*A;   dy -= xs*B;
1526                 this->r = r;  sx += xs;
1527         }
1528         int64_t ry() { return r + 8*dy + 4*C; }
1529         void moveY(int64_t r) {
1530                 dx -= B;      dy += C;
1531                 this->r = r;  ++sy;
1532         }
1533         void draw();
1534
1535         smooth_line(VFrame *vframe) { this->vframe = vframe; this->done = 0; }
1536 };
1537
1538
1539 void smooth_line::draw()
1540 {
1541         if( done ) return;
1542         if( abs(dy) >= abs(dx) ) {
1543                 if( xs*(sx-xmxx) >= 0 ) {
1544                         if( ys > 0 ) { done = 1;  return; }
1545                         if( dy < 0 || ry() < 0 ) { moveY(ry()); goto xit; }
1546                         xmxx = ex;  xmxy = ey;
1547                         ys = 1;  xs = -xs;
1548                 }
1549                 moveX(rx());
1550                 int64_t rr = ry();
1551                 if( abs(rr) < abs(r) )
1552                         moveY(rr);
1553         }
1554         else {
1555                 if( sy >= xmxy ) {
1556                         if( ys > 0 ) { done = 1;  return; }
1557                         xmxx = ex;  xmxy = ey;
1558                         ys = 1;  xs = -xs;
1559                 }
1560                 moveY(ry());
1561                 int64_t rr = rx();
1562                 if( abs(rr) < abs(r) )
1563                         moveX(rr);
1564         }
1565 xit:    vframe->draw_pixel(sx, sy);
1566 }
1567
1568 void VFrame::draw_smooth(int x1, int y1, int x2, int y2, int x3, int y3)
1569 {
1570         if( (x1 == x2 && y1 == y2) || (x2 == x3 && y2 == y3) )
1571                 draw_line(x1,y1, x3,y3);
1572         else if( x1 == x3 && y1 == y3 )
1573                 draw_line(x1,y1, x2,y2);
1574         else if( (x2-x1) * (y2-y3) == (x2-x3) * (y2-y1) ) {
1575                 // co-linear, draw line from min to max
1576                 if( x1 < x3 ) {
1577                         if( x2 < x1 ) { x1 = x2;  y1 = y2; }
1578                         if( x2 > x3 ) { x3 = x2;  y3 = y2; }
1579                 }
1580                 else {
1581                         if( x2 > x1 ) { x1 = x2;  y1 = y2; }
1582                         if( x2 < x3 ) { x3 = x2;  y3 = y2; }
1583                 }
1584                 draw_line(x1,y1, x3,y3);
1585         }
1586         else
1587                 smooth_draw(x1, y1, x2, y2, x3, y3);
1588 }
1589
1590 /*
1591   Non-Parametric Smooth Curve Generation. Don Kelly 1984
1592
1593      P+-----+Q'= virtual
1594      /     /       origin
1595     /     /
1596   Q+-----+R
1597
1598    Let the starting point be P. the ending point R. and the tangent vertex Q.
1599    A general point Z on the curve is then
1600         Z = (P + R - Q) + (Q - P) sin t + (Q - R) cos t
1601
1602    Expanding the Cartesian coordinates around (P + R - Q) gives
1603         [x y] = Z - (P + R - Q)
1604         [a c] = Q - P
1605         [b d] = Q - R
1606         x = a*sin(t) + b*cos(t)
1607         y = c*sin(t) + d*cos(t)
1608
1609    from which t can now be eliminated via
1610         c*x - a*y = (c*b - a*d)*cos(t)
1611         d*x - b*y = (a*d - c*b)*sin(t)
1612
1613    giving the Cartesian equation for the ellipse as
1614         f(x, y) = (c*x - a*y)**2 + (d*x - b*y)**2 - (a*d - c*b)**2 = 0
1615
1616    or:  f(x, y) = A*x**2 - 2*B*x*y + C*y**2 + B**2 - A*C = 0
1617    where: A = c**2 + d**2,  B = a*c + b*d,  C = a**2 + b**2
1618
1619    The maximum y extent of the ellipse may now be derived as follows:
1620         let df/dx = 0,  2*A*x = 2*B*y,  x = y*B/A
1621         f(x, y) == B**2 * y**2 / A - 2*B**2 * y**2 / A + C*y**2 + B**2 - A*C = 0
1622            (A*C - B**2)*y = (A*C - B**2)*A
1623            max x = sqrt(C), at y = B/sqrt(C)
1624            max y = sqrt(A), at x = B/sqrt(A)
1625
1626  */
1627
1628
1629 /* x1,y1 = P, x2,y2 = Q, x3,y3=R,
1630  *  draw from P to Q to R   if top=0
1631  *    or from P to (x,ymax) if top>0
1632  *    or from Q to (x,ymax) if top<0
1633  */
1634 void smooth_line::init0(int x1,int y1, int x2,int y2, int x3,int y3, int top)
1635 {
1636         int x0 = x1+x3-x2, y0 = y1+y3-y2; // Q'
1637
1638         int a = x2-x1,  c = y2-y1;
1639         int b = x2-x3,  d = y2-y3;
1640         A = c*c + d*d;  C = a*a + b*b;  B = a*c + b*d;
1641
1642         sx = top >= 0 ? x1 : x3;
1643         sy = top >= 0 ? y1 : y3;
1644         xs = x2 > sx || (x2==sx && (x1+x3-sx)>=x2) ? 1 : -1;
1645         int64_t px = sx-x0, py = sy-y0;
1646         dx = A*px - B*py;  dy = C*py - B*px;
1647         r = 0;
1648
1649         if( top ) {
1650                 double ymy = sqrt(A), ymx = B/ymy;
1651                 ex = x0 + rnd(ymx);
1652                 ey = y0 + rnd(ymy);
1653         }
1654         else {
1655                 ex = x3;  ey = y3;
1656         }
1657
1658         ys = a*b > 0 && (!top || top*xs*(b*c - a*d) > 0) ? -1 : 1;
1659         if( ys < 0 ) {
1660                 double xmx = xs*sqrt(C), xmy = B/xmx;
1661                 xmxx = x0 + rnd(xmx);
1662                 xmxy = y0 + rnd(xmy);
1663         }
1664         else {
1665                 xmxx = ex; xmxy = ey;
1666         }
1667 }
1668
1669 /*  x1,y1 = P, x2,y2 = Q, x3,y3=R,
1670  *  draw from (x,ymax) to P
1671  */
1672 void smooth_line::init1(int x1,int y1, int x2,int y2, int x3,int y3)
1673 {
1674         int x0 = x1+x3-x2, y0 = y1+y3-y2; // Q'
1675
1676         int a = x2-x1,  c = y2-y1;
1677         int b = x2-x3,  d = y2-y3;
1678         A = c*c + d*d;  C = a*a + b*b;  B = a*c + b*d;
1679
1680         double ymy = -sqrt(A), ymx = B/ymy;
1681         int64_t px = rnd(ymx), py = rnd(ymy);
1682         sx = x0 + px;  ex = x1;
1683         sy = y0 + py;  ey = y1;
1684         xs = x2 > x1 || (x2==x1 && x3>=x2) ? 1 : -1;
1685         dx = A*px - B*py;  dy = C*py - B*px;
1686         r = 4 * (A*px*px - 2*B*px*py + C*py*py + B*B - A*C);
1687
1688         ys = a*b > 0 && xs*(b*c - a*d) < 0 ? -1 : 1;
1689         if( ys < 0 ) {
1690                 double xmx = xs*sqrt(C), xmy = B/xmx;
1691                 xmxx = x0 + rnd(xmx);
1692                 xmxy = y0 + rnd(xmy);
1693         }
1694         else {
1695                 xs = -xs;
1696                 xmxx = ex; xmxy = ey;
1697         }
1698         if( xs > 0 )
1699                 vframe->draw_pixel(sx, sy);
1700         while( xs*(sx-xmxx) < 0 && (xs*dx < 0 || rx() < 0) ) {
1701                 moveX(rx());
1702                 vframe->draw_pixel(sx, sy);
1703         }
1704 }
1705
1706
1707 void VFrame::smooth_draw(int x1, int y1, int x2, int y2, int x3, int y3)
1708 {
1709 //printf("p smooth_draw( %d,%d, %d,%d, %d,%d )\n", x1,y1,x2,y2,x3,y3);
1710         if( y1 > y3 ) {         // make y3 >= y1
1711                 int xt = x1;  x1 = x3;  x3 = xt;
1712                 int yt = y1;  y1 = y3;  y3 = yt;
1713         }
1714         if( y1 > y2 && y3 > y2 ) {
1715                 smooth_line lt(this), rt(this); // Q on bottom
1716                 lt.init1(x1, y1, x2, y2, x3, y3);
1717                 rt.init1(x3, y3, x2, y2, x1, y1);
1718                 while( !lt.done || !rt.done ) {
1719                         lt.draw();
1720                         rt.draw();
1721                 }
1722         }
1723         else if( y1 < y2 && y3 < y2 ) {
1724                 smooth_line lt(this), rt(this); // Q on top
1725                 lt.init0(x1, y1, x2, y2, x3, y3, 1);
1726                 draw_pixel(lt.sx, lt.sy);
1727                 rt.init0(x1, y1, x2, y2, x3, y3, -1);
1728                 draw_pixel(rt.sx, rt.sy);
1729                 while( !lt.done || !rt.done ) {
1730                         lt.draw();
1731                         rt.draw();
1732                 }
1733         }
1734         else {
1735                 smooth_line pt(this);           // Q in between
1736                 pt.init0(x1, y1, x2, y2, x3, y3, 0);
1737                 draw_pixel(pt.sx, pt.sy);
1738                 while( !pt.done ) {
1739                         pt.draw();
1740                 }
1741         }
1742 }
1743
1744
1745 void VFrame::draw_rect(int x1, int y1, int x2, int y2)
1746 {
1747         draw_line(x1, y1, x2, y1);
1748         draw_line(x2, y1 + 1, x2, y2);
1749         draw_line(x2 - 1, y2, x1, y2);
1750         draw_line(x1, y2 - 1, x1, y1 + 1);
1751 }
1752
1753
1754 void VFrame::draw_oval(int x1, int y1, int x2, int y2)
1755 {
1756         int w = x2 - x1;
1757         int h = y2 - y1;
1758         int center_x = (x2 + x1) / 2;
1759         int center_y = (y2 + y1) / 2;
1760         int x_table[h / 2];
1761
1762 //printf("VFrame::draw_oval %d %d %d %d %d\n", __LINE__, x1, y1, x2, y2);
1763
1764         for(int i = 0; i < h / 2; i++) {
1765 // A^2 = -(B^2) + C^2
1766                 x_table[i] = (int)(sqrt(-SQR(h / 2 - i) + SQR(h / 2)) * w / h);
1767 //printf("VFrame::draw_oval %d i=%d x=%d\n", __LINE__, i, x_table[i]);
1768         }
1769
1770         for(int i = 0; i < h / 2 - 1; i++) {
1771                 int x3 = x_table[i];
1772                 int x4 = x_table[i + 1];
1773
1774                 if(x4 > x3 + 1) {
1775                         for(int j = x3; j < x4; j++) {
1776                                 draw_pixel(center_x + j, y1 + i);
1777                                 draw_pixel(center_x - j, y1 + i);
1778                                 draw_pixel(center_x + j, y2 - i - 1);
1779                                 draw_pixel(center_x - j, y2 - i - 1);
1780                         }
1781                 }
1782                 else {
1783                         draw_pixel(center_x + x3, y1 + i);
1784                         draw_pixel(center_x - x3, y1 + i);
1785                         draw_pixel(center_x + x3, y2 - i - 1);
1786                         draw_pixel(center_x - x3, y2 - i - 1);
1787                 }
1788         }
1789         
1790         draw_pixel(center_x, y1);
1791         draw_pixel(center_x, y2 - 1);
1792         draw_pixel(x1, center_y);
1793         draw_pixel(x2 - 1, center_y);
1794         draw_pixel(x1, center_y - 1);
1795         draw_pixel(x2 - 1, center_y - 1);
1796 }
1797
1798
1799 void VFrame::draw_arrow(int x1, int y1, int x2, int y2, int sz)
1800 {
1801         double angle = atan((float)(y2 - y1) / (float)(x2 - x1));
1802         double angle1 = angle + (float)145 / 360 * 2 * M_PI;
1803         double angle2 = angle - (float)145 / 360 * 2 * M_PI;
1804         int s = x2 < x1 ? -1 : 1;
1805         int x3 = x2 + s * (int)(sz * cos(angle1));
1806         int y3 = y2 + s * (int)(sz * sin(angle1));
1807         int x4 = x2 + s * (int)(sz * cos(angle2));
1808         int y4 = y2 + s * (int)(sz * sin(angle2));
1809
1810 // Main vector
1811         draw_line(x1, y1, x2, y2);
1812 //      draw_line(x1, y1 + 1, x2, y2 + 1);
1813
1814 // Arrow line
1815         if(abs(y2 - y1) || abs(x2 - x1)) draw_line(x2, y2, x3, y3);
1816 // Arrow line
1817         if(abs(y2 - y1) || abs(x2 - x1)) draw_line(x2, y2, x4, y4);
1818 }
1819
1820 void VFrame::draw_x(int x, int y, int sz)
1821 {
1822         draw_line(x-sz,y-sz, x+sz,y+sz);
1823         draw_line(x+sz,y-sz, x-sz,y+sz);
1824 }
1825 void VFrame::draw_t(int x, int y, int sz)
1826 {
1827         draw_line(x,y-sz, x,y+sz);
1828         draw_line(x+sz,y, x-sz,y);
1829 }
1830
1831