4 * Copyright (C) 2011 Adam Williams <broadcast at earthling dot net>
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.
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.
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
33 #include "bcpbuffer.h"
34 #include "bcresources.h"
35 #include "bcsignals.h"
36 #include "bcsynchronous.h"
37 #include "bctexture.h"
38 #include "bcwindowbase.h"
40 #include "bccmodels.h"
46 static void png_read_function(png_structp png_ptr,
47 png_bytep data, png_size_t length)
49 VFrame *frame = (VFrame*)png_get_io_ptr(png_ptr);
50 if(frame->image_size - frame->image_offset < (long)length)
52 printf("PngReadFunction::png_read_function %d: overrun\n", __LINE__);
53 length = frame->image_size - frame->image_offset;
56 memcpy(data, &frame->image[frame->image_offset], length);
57 frame->image_offset += length;
67 VFrameScene::VFrameScene()
71 VFrameScene::~VFrameScene()
81 //static BCCounter counter;
83 VFramePng::VFramePng(unsigned char *png_data, double s)
86 ((long)png_data[0] << 24) | ((long)png_data[1] << 16) |
87 ((long)png_data[2] << 8) | (long)png_data[3];
88 if( !s ) s = BC_WindowBase::get_resources()->icon_scale;
89 read_png(png_data+4, image_size, s, s);
92 VFramePng::VFramePng(unsigned char *png_data, long image_size, double xs, double ys)
94 if( !xs ) xs = BC_WindowBase::get_resources()->icon_scale;
95 if( !ys ) ys = BC_WindowBase::get_resources()->icon_scale;
96 read_png(png_data, image_size, xs, ys);
99 VFramePng::~VFramePng()
103 VFrame *VFramePng::vframe_png(int fd, double xs, double ys)
106 if( fstat(fd, &st) ) return 0;
107 long len = st.st_size;
110 unsigned char *bfr = (unsigned char *)
111 ::mmap (NULL, len, PROT_READ, MAP_SHARED, fd, 0);
112 if( bfr == MAP_FAILED ) return 0;
113 VFrame *vframe = new VFramePng(bfr, len, xs, ys);
114 if( (w=vframe->get_w()) <= 0 || (h=vframe->get_h()) <= 0 ||
115 vframe->get_data() == 0 ) { delete vframe; vframe = 0; }
119 VFrame *VFramePng::vframe_png(const char *png_path, double xs, double ys)
122 int fd = ::open(png_path, O_RDONLY);
124 vframe = vframe_png(fd, xs, ys);
131 VFrame::VFrame(VFrame &frame)
134 params = new BC_Hash;
135 allocate_data(0, -1, 0, 0, 0, frame.w, frame.h,
136 frame.color_model, frame.bytes_per_line);
137 memcpy(data, frame.data, bytes_per_line * h);
141 VFrame::VFrame(int w, int h, int color_model, long bytes_per_line)
144 params = new BC_Hash;
145 allocate_data(data, -1, 0, 0, 0, w, h,
146 color_model, bytes_per_line);
149 VFrame::VFrame(unsigned char *data, int shmid, int w, int h,
150 int color_model, long bytes_per_line)
153 params = new BC_Hash;
154 allocate_data(data, shmid, 0, 0, 0, w, h,
155 color_model, bytes_per_line);
158 VFrame::VFrame(unsigned char *data, int shmid,
159 long y_offset, long u_offset, long v_offset,
160 int w, int h, int color_model, long bytes_per_line)
163 params = new BC_Hash;
164 allocate_data(data, shmid, y_offset, u_offset, v_offset, w, h,
165 color_model, bytes_per_line);
168 VFrame::VFrame(BC_Bitmap *bitmap, int w, int h,
169 int color_model, long bytes_per_line)
172 params = new BC_Hash;
174 unsigned char *data = 0;
175 if( bitmap->is_shared() )
176 shmid = bitmap->get_shmid();
178 data = bitmap->get_data();
179 allocate_data(data, shmid,
180 bitmap->get_y_offset(),
181 bitmap->get_u_offset(),
182 bitmap->get_v_offset(),
183 w, h, color_model, bytes_per_line);
189 params = new BC_Hash;
190 this->color_model = BC_COMPRESSED;
198 // Delete effect stack
199 prev_effects.remove_all_objects();
200 next_effects.remove_all_objects();
205 int VFrame::equivalent(VFrame *src, int test_stacks)
207 return (src->get_color_model() == get_color_model() &&
208 src->get_w() == get_w() &&
209 src->get_h() == get_h() &&
210 src->bytes_per_line == bytes_per_line &&
211 (!test_stacks || equal_stacks(src)));
214 int VFrame::data_matches(VFrame *frame)
216 if(data && frame->get_data() &&
217 frame->params_match(get_w(), get_h(), get_color_model()) &&
218 get_data_size() == frame->get_data_size())
220 int data_size = get_data_size();
221 unsigned char *ptr1 = get_data();
222 unsigned char *ptr2 = frame->get_data();
223 for(int i = 0; i < data_size; i++)
225 if(*ptr1++ != *ptr2++) return 0;
232 // long VFrame::set_shm_offset(long offset)
234 // shm_offset = offset;
238 // long VFrame::get_shm_offset()
240 // return shm_offset;
243 int VFrame::get_memory_type()
248 int VFrame::params_match(int w, int h, int color_model)
250 return (this->w == w &&
252 this->color_model == color_model);
256 int VFrame::reset_parameters(int do_opengl)
261 memory_type = VFrame::PRIVATE;
269 compressed_allocated = 0;
270 compressed_size = 0; // Size of current image
277 sequence_number = -1;
281 set_pixel_color(BLACK);
286 // By default, anything is going to be done in RAM
287 opengl_state = VFrame::RAM;
292 prev_effects.set_array_delete();
293 next_effects.set_array_delete();
297 int VFrame::clear_objects(int do_opengl)
312 case VFrame::PRIVATE:
314 // if(this->w * this->h > 1500 * 1100)
315 // printf("VFrame::clear_objects 2 this=%p data=%p\n", this, data);
318 //printf("VFrame::clear_objects %d this=%p shmid=%p data=%p\n", __LINE__, this, shmid, data);
338 // Delete row pointers
362 int VFrame::get_field2_offset()
364 return field2_offset;
367 int VFrame::set_field2_offset(int value)
369 this->field2_offset = value;
373 void VFrame::set_keyframe(int value)
375 this->is_keyframe = value;
378 int VFrame::get_keyframe()
384 VFrameScene* VFrame::get_scene()
389 int VFrame::calculate_bytes_per_pixel(int color_model)
391 return BC_CModels::calculate_pixelsize(color_model);
394 long VFrame::get_bytes_per_line()
396 return bytes_per_line;
399 long VFrame::get_data_size()
401 return calculate_data_size(w, h, bytes_per_line, color_model) - 4;
404 long VFrame::calculate_data_size(int w, int h, int bytes_per_line, int color_model)
406 return BC_CModels::calculate_datasize(w, h, bytes_per_line, color_model);
409 void VFrame::create_row_pointers()
412 switch(color_model) {
414 if( this->v_offset ) break;
417 this->v_offset = sz + w / 4 * h / 4;
423 if( this->v_offset ) break;
426 this->v_offset = sz + sz / 4;
430 if( this->v_offset ) break;
433 this->v_offset = sz + sz / 2;
436 if( this->v_offset ) break;
439 this->v_offset = sz + sz;
442 if( this->v_offset || a ) break;
443 a = this->data + 3 * sz * sizeof(float);
445 if( this->v_offset ) break;
447 this->u_offset = sz * sizeof(float);
448 this->v_offset = 2 * sz * sizeof(float);
452 rows = new unsigned char*[h];
453 for(int i = 0; i < h; i++)
454 rows[i] = &this->data[i * this->bytes_per_line];
457 y = this->data + this->y_offset;
458 u = this->data + this->u_offset;
459 v = this->data + this->v_offset;
462 int VFrame::allocate_data(unsigned char *data, int shmid,
463 long y_offset, long u_offset, long v_offset, int w, int h,
464 int color_model, long bytes_per_line)
468 this->color_model = color_model;
469 this->bytes_per_pixel = calculate_bytes_per_pixel(color_model);
470 this->y_offset = this->u_offset = this->v_offset = 0;
472 // printf("VFrame::allocate_data %d shmid == 0\n", __LINE__, shmid);
475 this->bytes_per_line = bytes_per_line >= 0 ?
476 bytes_per_line : this->bytes_per_pixel * w;
478 // Allocate data + padding for MMX
480 //printf("VFrame::allocate_data %d %p\n", __LINE__, this->data);
481 memory_type = VFrame::SHARED;
484 this->y_offset = y_offset;
485 this->u_offset = u_offset;
486 this->v_offset = v_offset;
488 else if(shmid >= 0) {
489 memory_type = VFrame::SHMGET;
490 this->data = (unsigned char*)shmat(shmid, NULL, 0);
491 //printf("VFrame::allocate_data %d shmid=%d data=%p\n", __LINE__, shmid, this->data);
493 this->y_offset = y_offset;
494 this->u_offset = u_offset;
495 this->v_offset = v_offset;
498 memory_type = VFrame::PRIVATE;
499 int size = calculate_data_size(this->w, this->h,
500 this->bytes_per_line, this->color_model);
501 if(BC_WindowBase::get_resources()->use_vframe_shm() && use_shm) {
502 this->shmid = shmget(IPC_PRIVATE, size, IPC_CREAT | 0777);
503 if(this->shmid < 0) {
504 printf("VFrame::allocate_data %d could not allocate shared memory\n", __LINE__);
507 this->data = (unsigned char*)shmat(this->shmid, NULL, 0);
508 //printf("VFrame::allocate_data %d %d %d\n", __LINE__, size, this->shmid);
510 //printf("VFrame::allocate_data %d %p\n", __LINE__, this->data);
511 // This causes it to automatically delete when the program exits.
512 shmctl(this->shmid, IPC_RMID, 0);
515 // Have to use malloc for libpng
516 //printf("==vframe %d from %p\n", size, __builtin_return_address(0));
517 this->data = (unsigned char *)malloc(size);
521 // if(this->w * this->h > 1500 * 1100)
522 // printf("VFrame::allocate_data 2 this=%p w=%d h=%d this->data=%p\n",
523 // this, this->w, this->h, this->data);
526 printf("VFrame::allocate_data %dx%d: memory exhausted.\n", this->w, this->h);
528 //printf("VFrame::allocate_data %d %p data=%p %d %d\n", __LINE__, this, this->data, this->w, this->h);
529 //if(size > 1000000) printf("VFrame::allocate_data %d\n", size);
532 // Create row pointers
533 create_row_pointers();
537 void VFrame::set_memory(unsigned char *data,
547 memory_type = VFrame::SHARED;
550 this->y_offset = y_offset;
551 this->u_offset = u_offset;
552 this->v_offset = v_offset;
557 memory_type = VFrame::SHMGET;
558 this->data = (unsigned char*)shmat(shmid, NULL, 0);
562 y = this->data + this->y_offset;
563 u = this->data + this->u_offset;
564 v = this->data + this->v_offset;
566 create_row_pointers();
569 void VFrame::set_memory(BC_Bitmap *bitmap)
572 unsigned char *data = 0;
573 if( bitmap->is_shared() && !bitmap->is_zombie() )
574 shmid = bitmap->get_shmid();
576 data = bitmap->get_data();
577 set_memory(data, shmid,
578 bitmap->get_y_offset(),
579 bitmap->get_u_offset(),
580 bitmap->get_v_offset());
583 void VFrame::set_compressed_memory(unsigned char *data,
592 memory_type = VFrame::SHARED;
599 memory_type = VFrame::SHMGET;
600 this->data = (unsigned char*)shmat(shmid, NULL, 0);
604 this->compressed_allocated = data_allocated;
605 this->compressed_size = data_size;
609 // Reallocate uncompressed buffer with or without alpha
610 int VFrame::reallocate(
621 // if(shmid == 0) printf("VFrame::reallocate %d shmid=%d\n", __LINE__, shmid);
623 // reset_parameters(0);
636 int VFrame::allocate_compressed_data(long bytes)
638 if(bytes < 1) return 1;
640 // Want to preserve original contents
641 if(data && compressed_allocated < bytes)
644 unsigned char *new_data = 0;
645 if(BC_WindowBase::get_resources()->use_vframe_shm() && use_shm)
647 new_shmid = shmget(IPC_PRIVATE,
650 new_data = (unsigned char*)shmat(new_shmid, NULL, 0);
651 shmctl(new_shmid, IPC_RMID, 0);
655 // Have to use malloc for libpng
656 new_data = (unsigned char *)malloc(bytes);
659 bcopy(data, new_data, compressed_allocated);
662 if(memory_type == VFrame::PRIVATE)
672 if(memory_type == VFrame::SHMGET)
680 compressed_allocated = bytes;
685 if(BC_WindowBase::get_resources()->use_vframe_shm() && use_shm)
687 shmid = shmget(IPC_PRIVATE,
690 data = (unsigned char*)shmat(shmid, NULL, 0);
691 shmctl(shmid, IPC_RMID, 0);
695 // Have to use malloc for libpng
696 data = (unsigned char *)malloc(bytes);
699 compressed_allocated = bytes;
706 int VFramePng::read_png(const unsigned char *data, long sz, double xscale, double yscale)
708 // Test for RAW format
709 if(data[0] == 'R' && data[1] == 'A' && data[2] == 'W' && data[3] == ' ') {
710 int new_color_model = BC_RGBA8888;
711 w = data[4] | (data[5] << 8) | (data[6] << 16) | (data[7] << 24);
712 h = data[8] | (data[9] << 8) | (data[10] << 16) | (data[11] << 24);
713 int components = data[12];
714 new_color_model = components == 3 ? BC_RGB888 : BC_RGBA8888;
715 // This shares the data directly
716 // reallocate(data + 20, 0, 0, 0, w, h, new_color_model, -1);
718 // Can't use shared data for theme since button constructions overlay the
720 reallocate(NULL, -1, 0, 0, 0, w, h, new_color_model, -1);
721 memcpy(get_data(), data + 16, w * h * components);
724 else if(data[0] == 0x89 && data[1] == 'P' && data[2] == 'N' && data[3] == 'G') {
726 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
727 png_infop info_ptr = png_create_info_struct(png_ptr);
731 image = data; image_size = sz;
732 png_set_read_fn(png_ptr, this, PngReadFunction::png_read_function);
733 png_read_info(png_ptr, info_ptr);
735 w = png_get_image_width(png_ptr, info_ptr);
736 h = png_get_image_height(png_ptr, info_ptr);
738 int src_color_model = png_get_color_type(png_ptr, info_ptr);
740 /* tell libpng to strip 16 bit/color files down to 8 bits/color */
741 png_set_strip_16(png_ptr);
743 /* extract multiple pixels with bit depths of 1, 2, and 4 from a single
744 * byte into separate bytes (useful for paletted and grayscale images).
746 png_set_packing(png_ptr);
748 /* expand paletted colors into true RGB triplets */
749 if (src_color_model == PNG_COLOR_TYPE_PALETTE)
750 png_set_expand(png_ptr);
752 /* expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel */
753 if (src_color_model == PNG_COLOR_TYPE_GRAY && png_get_bit_depth(png_ptr, info_ptr) < 8)
754 png_set_expand(png_ptr);
756 if (src_color_model == PNG_COLOR_TYPE_GRAY ||
757 src_color_model == PNG_COLOR_TYPE_GRAY_ALPHA)
758 png_set_gray_to_rgb(png_ptr);
760 /* expand paletted or RGB images with transparency to full alpha channels
761 * so the data will be available as RGBA quartets */
762 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)){
764 png_set_expand(png_ptr);
767 switch(src_color_model)
769 case PNG_COLOR_TYPE_GRAY:
770 case PNG_COLOR_TYPE_RGB:
771 new_color_model = BC_RGB888;
774 case PNG_COLOR_TYPE_GRAY_ALPHA:
775 case PNG_COLOR_TYPE_RGB_ALPHA:
777 new_color_model = BC_RGBA8888;
780 case PNG_COLOR_TYPE_PALETTE:
782 new_color_model = BC_RGBA8888;
784 new_color_model = BC_RGB888;
787 reallocate(NULL, -1, 0, 0, 0, w, h, new_color_model, -1);
789 //printf("VFrame::read_png %d %d %d %p\n", __LINE__, w, h, get_rows());
790 png_read_image(png_ptr, get_rows());
791 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
794 printf("VFrame::read_png %d: unknown file format"
795 " 0x%02x 0x%02x 0x%02x 0x%02x\n",
796 __LINE__, data[4], data[5], data[6], data[7]);
799 int ww = w * xscale, hh = h * yscale;
800 if( ww != w || hh != h ) {
801 VFrame vframe(*this);
802 reallocate(NULL, -1, 0, 0, 0, ww, hh, color_model, -1);
803 transfer_from(&vframe);
808 int VFrame::write_png(const char *path)
810 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
811 png_infop info_ptr = png_create_info_struct(png_ptr);
812 FILE *out_fd = fopen(path, "w");
815 printf("VFrame::write_png %d %s %s\n", __LINE__, path, strerror(errno));
819 int png_cmodel = PNG_COLOR_TYPE_RGB;
820 switch(get_color_model())
824 png_cmodel = PNG_COLOR_TYPE_RGB;
829 png_cmodel = PNG_COLOR_TYPE_RGB_ALPHA;
833 png_cmodel = PNG_COLOR_TYPE_GRAY;
837 png_init_io(png_ptr, out_fd);
838 png_set_compression_level(png_ptr, 9);
839 png_set_IHDR(png_ptr,
846 PNG_COMPRESSION_TYPE_DEFAULT,
847 PNG_FILTER_TYPE_DEFAULT);
848 png_write_info(png_ptr, info_ptr);
849 png_write_image(png_ptr, get_rows());
850 png_write_end(png_ptr, info_ptr);
851 png_destroy_write_struct(&png_ptr, &info_ptr);
857 #define ZERO_YUV(components, type, max) \
859 for(int i = 0; i < h; i++) \
861 type *row = (type*)get_rows()[i]; \
862 for(int j = 0; j < w; j++) \
864 row[j * components] = 0; \
865 row[j * components + 1] = (max + 1) / 2; \
866 row[j * components + 2] = (max + 1) / 2; \
867 if(components == 4) row[j * components + 3] = 0; \
872 int VFrame::clear_frame()
875 //printf("VFrame::clear_frame %d\n", __LINE__);
876 switch(color_model) {
882 bzero(get_u(), w / 4 * h / 4);
883 bzero(get_v(), w / 4 * h / 4);
890 bzero(get_u(), sz / 4);
891 bzero(get_v(), sz / 4);
896 bzero(get_u(), sz / 2);
897 bzero(get_v(), sz / 2);
900 case BC_RGBA_FLOATP: if( a ) {
901 float *ap = (float *)a;
902 for( int i=sz; --i>=0; ++ap ) *ap = 1.f; }
903 case BC_RGB_FLOATP: {
904 float *rp = (float *)y;
905 for( int i=sz; --i>=0; ++rp ) *rp = 0.f;
906 float *gp = (float *)u;
907 for( int i=sz; --i>=0; ++gp ) *gp = 0.f;
908 float *bp = (float *)v;
909 for( int i=sz; --i>=0; ++bp ) *bp = 0.f;
918 ZERO_YUV(3, unsigned char, 0xff);
922 ZERO_YUV(4, unsigned char, 0xff);
926 ZERO_YUV(3, uint16_t, 0xffff);
929 case BC_YUVA16161616:
930 ZERO_YUV(4, uint16_t, 0xffff);
934 bzero(data, calculate_data_size(w, h, bytes_per_line, color_model));
940 void VFrame::rotate90()
942 // Allocate new frame
943 int new_w = h, new_h = w, new_bytes_per_line = bytes_per_pixel * new_w;
944 unsigned char *new_data = new unsigned char[calculate_data_size(new_w, new_h, new_bytes_per_line, color_model)];
945 unsigned char **new_rows = new unsigned char*[new_h];
946 for(int i = 0; i < new_h; i++)
947 new_rows[i] = &new_data[new_bytes_per_line * i];
950 for(int in_y = 0, out_x = new_w - 1; in_y < h; in_y++, out_x--)
952 for(int in_x = 0, out_y = 0; in_x < w; in_x++, out_y++)
954 for(int k = 0; k < bytes_per_pixel; k++)
956 new_rows[out_y][out_x * bytes_per_pixel + k] =
957 rows[in_y][in_x * bytes_per_pixel + k];
966 bytes_per_line = new_bytes_per_line;
971 void VFrame::rotate270()
973 // Allocate new frame
974 int new_w = h, new_h = w, new_bytes_per_line = bytes_per_pixel * new_w;
975 unsigned char *new_data = new unsigned char[calculate_data_size(new_w, new_h, new_bytes_per_line, color_model)];
976 unsigned char **new_rows = new unsigned char*[new_h];
977 for(int i = 0; i < new_h; i++)
978 new_rows[i] = &new_data[new_bytes_per_line * i];
981 for(int in_y = 0, out_x = 0; in_y < h; in_y++, out_x++)
983 for(int in_x = 0, out_y = new_h - 1; in_x < w; in_x++, out_y--)
985 for(int k = 0; k < bytes_per_pixel; k++)
987 new_rows[out_y][out_x * bytes_per_pixel + k] =
988 rows[in_y][in_x * bytes_per_pixel + k];
997 bytes_per_line = new_bytes_per_line;
1002 void VFrame::flip_vert()
1004 unsigned char *temp = new unsigned char[bytes_per_line];
1005 for(int i = 0, j = h - 1; i < j; i++, j--)
1007 memcpy(temp, rows[j], bytes_per_line);
1008 memcpy(rows[j], rows[i], bytes_per_line);
1009 memcpy(rows[i], temp, bytes_per_line);
1014 void VFrame::flip_horiz()
1016 unsigned char temp[32];
1017 for(int i = 0; i < h; i++)
1019 unsigned char *row = rows[i];
1020 for(int j = 0; j < bytes_per_line / 2; j += bytes_per_pixel)
1022 memcpy(temp, row + j, bytes_per_pixel);
1023 memcpy(row + j, row + bytes_per_line - j - bytes_per_pixel, bytes_per_pixel);
1024 memcpy(row + bytes_per_line - j - bytes_per_pixel, temp, bytes_per_pixel);
1031 int VFrame::copy_from(VFrame *frame)
1033 int w = MIN(this->w, frame->get_w());
1034 int h = MIN(this->h, frame->get_h());
1035 timestamp = frame->timestamp;
1037 switch(frame->color_model)
1040 allocate_compressed_data(frame->compressed_size);
1041 memcpy(data, frame->data, frame->compressed_size);
1042 this->compressed_size = frame->compressed_size;
1046 memcpy(get_y(), frame->get_y(), w * h);
1047 memcpy(get_u(), frame->get_u(), w / 4 * h / 4);
1048 memcpy(get_v(), frame->get_v(), w / 4 * h / 4);
1054 //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());
1055 memcpy(get_y(), frame->get_y(), w * h);
1056 memcpy(get_u(), frame->get_u(), w * h / 4);
1057 memcpy(get_v(), frame->get_v(), w * h / 4);
1061 //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());
1062 memcpy(get_y(), frame->get_y(), w * h);
1063 memcpy(get_u(), frame->get_u(), w * h / 2);
1064 memcpy(get_v(), frame->get_v(), w * h / 2);
1068 //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());
1069 memcpy(get_y(), frame->get_y(), w * h);
1070 memcpy(get_u(), frame->get_u(), w * h);
1071 memcpy(get_v(), frame->get_v(), w * h);
1074 // printf("VFrame::copy_from %d\n", calculate_data_size(w,
1077 // frame->color_model));
1078 // Copy without extra 4 bytes in case the source is a hardware device
1079 memmove(data, frame->data, get_data_size());
1086 int VFrame::transfer_from(VFrame *that, int bg_color, int in_x, int in_y, int in_w, int in_h)
1088 if( this->get_color_model() == that->get_color_model() &&
1089 this->get_w() == that->get_w() && this->get_h() == that->get_h() &&
1090 this->get_bytes_per_line() == that->get_bytes_per_line() )
1091 return this->copy_from(that);
1093 timestamp = that->timestamp;
1095 BC_CModels::transfer(
1096 this->get_rows(), that->get_rows(), // Packed data out/in
1097 this->get_y(), this->get_u(), this->get_v(), // Planar data out/in
1098 that->get_y(), that->get_u(), that->get_v(),
1099 0, 0, that->get_w(), that->get_h(), // Dimensions in/out
1100 0, 0, this->get_w(), this->get_h(),
1101 that->get_color_model(), this->get_color_model(), // Color models in/out
1102 bg_color, // alpha blend bg_color
1103 that->get_bytes_per_line(),
1104 this->get_bytes_per_line()); // rowspans (of luma for YUV)
1106 unsigned char *in_ptrs[4], *out_ptrs[4];
1107 unsigned char **inp, **outp;
1108 if( BC_CModels::is_planar(that->get_color_model()) ) {
1109 in_ptrs[0] = that->get_y();
1110 in_ptrs[1] = that->get_u();
1111 in_ptrs[2] = that->get_v();
1112 in_ptrs[3] = that->get_a();
1116 inp = that->get_rows();
1117 if( BC_CModels::is_planar(this->get_color_model()) ) {
1118 out_ptrs[0] = this->get_y();
1119 out_ptrs[1] = this->get_u();
1120 out_ptrs[2] = this->get_v();
1121 out_ptrs[3] = this->get_a();
1125 outp = this->get_rows();
1126 BC_CModels::transfer(outp, this->get_color_model(),
1127 0, 0, this->get_w(), this->get_h(),
1128 this->get_bytes_per_line(),
1129 inp, that->get_color_model(),
1130 in_x, in_y, in_w, in_h,
1131 that->get_bytes_per_line(),
1138 int VFrame::get_scale_tables(int *column_table, int *row_table,
1139 int in_x1, int in_y1, int in_x2, int in_y2,
1140 int out_x1, int out_y1, int out_x2, int out_y2)
1143 float w_in = in_x2 - in_x1;
1144 float h_in = in_y2 - in_y1;
1145 int w_out = out_x2 - out_x1;
1146 int h_out = out_y2 - out_y1;
1148 float hscale = w_in / w_out;
1149 float vscale = h_in / h_out;
1151 for(i = 0; i < w_out; i++)
1153 column_table[i] = (int)(hscale * i);
1156 for(i = 0; i < h_out; i++)
1158 row_table[i] = (int)(vscale * i) + in_y1;
1163 void VFrame::push_prev_effect(const char *name)
1166 prev_effects.append(ptr = new char[strlen(name) + 1]);
1168 if(prev_effects.total > MAX_STACK_ELEMENTS) prev_effects.remove_object(0);
1171 void VFrame::pop_prev_effect()
1173 if(prev_effects.total)
1174 prev_effects.remove_object(prev_effects.last());
1177 void VFrame::push_next_effect(const char *name)
1180 next_effects.append(ptr = new char[strlen(name) + 1]);
1182 if(next_effects.total > MAX_STACK_ELEMENTS) next_effects.remove_object(0);
1185 void VFrame::pop_next_effect()
1187 if(next_effects.total)
1188 next_effects.remove_object(next_effects.last());
1191 const char* VFrame::get_next_effect(int number)
1193 if(!next_effects.total) return "";
1195 if(number > next_effects.total - 1) number = next_effects.total - 1;
1197 return next_effects.values[next_effects.total - number - 1];
1200 const char* VFrame::get_prev_effect(int number)
1202 if(!prev_effects.total) return "";
1204 if(number > prev_effects.total - 1) number = prev_effects.total - 1;
1206 return prev_effects.values[prev_effects.total - number - 1];
1209 BC_Hash* VFrame::get_params()
1214 void VFrame::clear_stacks()
1216 next_effects.remove_all_objects();
1217 prev_effects.remove_all_objects();
1222 void VFrame::copy_params(VFrame *src)
1224 status = src->status;
1225 params->copy_from(src->params);
1228 void VFrame::copy_stacks(VFrame *src)
1232 for(int i = 0; i < src->next_effects.total; i++)
1235 next_effects.append(ptr = new char[strlen(src->next_effects.values[i]) + 1]);
1236 strcpy(ptr, src->next_effects.values[i]);
1238 for(int i = 0; i < src->prev_effects.total; i++)
1241 prev_effects.append(ptr = new char[strlen(src->prev_effects.values[i]) + 1]);
1242 strcpy(ptr, src->prev_effects.values[i]);
1248 int VFrame::equal_stacks(VFrame *src)
1250 for(int i = 0; i < src->next_effects.total && i < next_effects.total; i++)
1252 if(strcmp(src->next_effects.values[i], next_effects.values[i])) return 0;
1255 for(int i = 0; i < src->prev_effects.total && i < prev_effects.total; i++)
1257 if(strcmp(src->prev_effects.values[i], prev_effects.values[i])) return 0;
1260 if(!params->equivalent(src->params)) return 0;
1264 void VFrame::dump_stacks()
1266 printf("VFrame::dump_stacks\n");
1267 printf(" next_effects:\n");
1268 for(int i = next_effects.total - 1; i >= 0; i--)
1269 printf(" %s\n", next_effects.values[i]);
1270 printf(" prev_effects:\n");
1271 for(int i = prev_effects.total - 1; i >= 0; i--)
1272 printf(" %s\n", prev_effects.values[i]);
1275 void VFrame::dump_params()
1282 printf("VFrame::dump %d this=%p\n", __LINE__, this);
1283 printf(" w=%d h=%d colormodel=%d rows=%p use_shm=%d shmid=%d\n",
1284 w, h, color_model, rows, use_shm, shmid);
1288 int VFrame::get_memory_usage()
1290 if(get_compressed_allocated()) return get_compressed_allocated();
1291 return get_h() * get_bytes_per_line();
1294 void VFrame::set_pixel_color(int rgb)
1297 int ir = 0xff & (pixel_rgb >> 16);
1298 int ig = 0xff & (pixel_rgb >> 8);
1299 int ib = 0xff & (pixel_rgb >> 0);
1300 bc_rgb2yuv(ir,ig,ib, ir,ig,ib);
1301 pixel_yuv = (ir<<16) | (ig<<8) | (ib<<0);
1304 void VFrame::set_stiple(int mask)
1309 int VFrame::draw_pixel(int x, int y)
1311 if( x < 0 || y < 0 || x >= get_w() || y >= get_h() ) return 1;
1312 if( draw_point ) return (this->*draw_point)(x, y);
1314 #define DRAW_PIXEL(type, r, g, b) { \
1315 type **rows = (type**)get_rows(); \
1316 rows[y][x * components + 0] = r; \
1317 rows[y][x * components + 1] = g; \
1318 rows[y][x * components + 2] = b; \
1319 if( components == 4 ) \
1320 rows[y][x * components + 3] = mx; \
1322 int components = BC_CModels::components(color_model);
1323 int bch = BC_CModels::calculate_pixelsize(color_model) / components;
1324 int sz = 8*bch, mx = BC_CModels::is_float(color_model) ? 1 : (1<<sz)-1;
1325 int is_yuv = BC_CModels::is_yuv(color_model);
1326 int pixel_color = is_yuv ? pixel_yuv : pixel_rgb;
1327 int ir = 0xff & (pixel_color >> 16); float fr = 0;
1328 int ig = 0xff & (pixel_color >> 8); float fg = 0;
1329 int ib = 0xff & (pixel_color >> 0); float fb = 0;
1330 if( (x+y) & stipple ) {
1331 ir = 255 - ir; ig = 255 - ig; ib = 255 - ib;
1333 if( BC_CModels::is_float(color_model) ) {
1334 fr = ir / 255.; fg = ig / 255.; fb = ib / 255.;
1337 else if( (sz-=8) > 0 ) {
1338 ir <<= sz; ig <<= sz; ib <<= sz;
1341 switch(get_color_model()) {
1346 DRAW_PIXEL(uint8_t, ir, ig, ib);
1350 case BC_RGBA16161616:
1351 case BC_YUVA16161616:
1352 DRAW_PIXEL(uint16_t, ir, ig, ib);
1356 DRAW_PIXEL(float, fr, fg, fb);
1364 void VFrame::draw_line(int x1, int y1, int x2, int y2)
1367 int tx = x1; x1 = x2; x2 = tx;
1368 int ty = y1; y1 = y2; y2 = ty;
1372 int dx = x2-x1, dy = y2-y1;
1373 int dx2 = 2*dx, dy2 = 2*dy;
1374 if( dx < 0 ) dx = -dx;
1375 int r = dx > dy ? dx : dy, n = r;
1377 if( dx2 < 0 ) dir += 1;
1379 if( dx2 >= 0 ) do { /* +Y, +X */
1381 if( (r -= dx2) < 0 ) { r += dy2; ++x; }
1382 } while( --n >= 0 );
1383 else do { /* +Y, -X */
1385 if( (r += dx2) < 0 ) { r += dy2; --x; }
1386 } while( --n >= 0 );
1389 if( dx2 >= 0 ) do { /* +X, +Y */
1391 if( (r -= dy2) < 0 ) { r += dx2; ++y; }
1392 } while( --n >= 0 );
1393 else do { /* -X, +Y */
1395 if( (r -= dy2) < 0 ) { r -= dx2; ++y; }
1396 } while( --n >= 0 );
1400 // g++ -dD -E - < /dev/null | grep DBL_EPSILON
1401 #ifndef __DBL_EPSILON__
1402 #define __DBL_EPSILON__ ((double)2.22044604925031308085e-16L)
1404 // weakest fraction * graphics integer range
1405 #define RND_EPSILON (__DBL_EPSILON__*65536)
1408 int rnd(double v) { return round(v)+RND_EPSILON; }
1411 int sx, sy, ex, ey; /* current point, end point */
1412 int xs, ys; /* x/y quadrant sign -1/1 */
1413 int64_t A, B, C; /* quadratic coefficients */
1414 int64_t r, dx, dy; /* residual, dr/dx and dr/dy */
1415 int xmxx, xmxy; /* x,y at apex */
1418 void init0(int x1,int y1, int x2,int y2, int x3,int y3, int top);
1419 void init1(int x1,int y1, int x2,int y2, int x3,int y3);
1420 int64_t rx() { return r + xs*8*dx + 4*A; }
1421 void moveX(int64_t r) {
1422 dx += xs*A; dy -= xs*B;
1423 this->r = r; sx += xs;
1425 int64_t ry() { return r + 8*dy + 4*C; }
1426 void moveY(int64_t r) {
1432 smooth_line(VFrame *vframe) { this->vframe = vframe; this->done = 0; }
1436 void smooth_line::draw()
1439 if( abs(dy) >= abs(dx) ) {
1440 if( xs*(sx-xmxx) >= 0 ) {
1441 if( ys > 0 ) { done = 1; return; }
1442 if( dy < 0 || ry() < 0 ) { moveY(ry()); goto xit; }
1443 xmxx = ex; xmxy = ey;
1448 if( abs(rr) < abs(r) )
1453 if( ys > 0 ) { done = 1; return; }
1454 xmxx = ex; xmxy = ey;
1459 if( abs(rr) < abs(r) )
1462 xit: vframe->draw_pixel(sx, sy);
1465 void VFrame::draw_smooth(int x1, int y1, int x2, int y2, int x3, int y3)
1467 if( (x1 == x2 && y1 == y2) || (x2 == x3 && y2 == y3) )
1468 draw_line(x1,y1, x3,y3);
1469 else if( x1 == x3 && y1 == y3 )
1470 draw_line(x1,y1, x2,y2);
1471 else if( (x2-x1) * (y2-y3) == (x2-x3) * (y2-y1) ) {
1472 // co-linear, draw line from min to max
1474 if( x2 < x1 ) { x1 = x2; y1 = y2; }
1475 if( x2 > x3 ) { x3 = x2; y3 = y2; }
1478 if( x2 > x1 ) { x1 = x2; y1 = y2; }
1479 if( x2 < x3 ) { x3 = x2; y3 = y2; }
1481 draw_line(x1,y1, x3,y3);
1484 smooth_draw(x1, y1, x2, y2, x3, y3);
1488 Non-Parametric Smooth Curve Generation. Don Kelly 1984
1495 Let the starting point be P. the ending point R. and the tangent vertex Q.
1496 A general point Z on the curve is then
1497 Z = (P + R - Q) + (Q - P) sin t + (Q - R) cos t
1499 Expanding the Cartesian coordinates around (P + R - Q) gives
1500 [x y] = Z - (P + R - Q)
1503 x = a*sin(t) + b*cos(t)
1504 y = c*sin(t) + d*cos(t)
1506 from which t can now be eliminated via
1507 c*x - a*y = (c*b - a*d)*cos(t)
1508 d*x - b*y = (a*d - c*b)*sin(t)
1510 giving the Cartesian equation for the ellipse as
1511 f(x, y) = (c*x - a*y)**2 + (d*x - b*y)**2 - (a*d - c*b)**2 = 0
1513 or: f(x, y) = A*x**2 - 2*B*x*y + C*y**2 + B**2 - A*C = 0
1514 where: A = c**2 + d**2, B = a*c + b*d, C = a**2 + b**2
1516 The maximum y extent of the ellipse may now be derived as follows:
1517 let df/dx = 0, 2*A*x = 2*B*y, x = y*B/A
1518 f(x, y) == B**2 * y**2 / A - 2*B**2 * y**2 / A + C*y**2 + B**2 - A*C = 0
1519 (A*C - B**2)*y = (A*C - B**2)*A
1520 max x = sqrt(C), at y = B/sqrt(C)
1521 max y = sqrt(A), at x = B/sqrt(A)
1526 /* x1,y1 = P, x2,y2 = Q, x3,y3=R,
1527 * draw from P to Q to R if top=0
1528 * or from P to (x,ymax) if top>0
1529 * or from Q to (x,ymax) if top<0
1531 void smooth_line::init0(int x1,int y1, int x2,int y2, int x3,int y3, int top)
1533 int x0 = x1+x3-x2, y0 = y1+y3-y2; // Q'
1535 int a = x2-x1, c = y2-y1;
1536 int b = x2-x3, d = y2-y3;
1537 A = c*c + d*d; C = a*a + b*b; B = a*c + b*d;
1539 sx = top >= 0 ? x1 : x3;
1540 sy = top >= 0 ? y1 : y3;
1541 xs = x2 > sx || (x2==sx && (x1+x3-sx)>=x2) ? 1 : -1;
1542 int64_t px = sx-x0, py = sy-y0;
1543 dx = A*px - B*py; dy = C*py - B*px;
1547 double ymy = sqrt(A), ymx = B/ymy;
1555 ys = a*b > 0 && (!top || top*xs*(b*c - a*d) > 0) ? -1 : 1;
1557 double xmx = xs*sqrt(C), xmy = B/xmx;
1558 xmxx = x0 + rnd(xmx);
1559 xmxy = y0 + rnd(xmy);
1562 xmxx = ex; xmxy = ey;
1566 /* x1,y1 = P, x2,y2 = Q, x3,y3=R,
1567 * draw from (x,ymax) to P
1569 void smooth_line::init1(int x1,int y1, int x2,int y2, int x3,int y3)
1571 int x0 = x1+x3-x2, y0 = y1+y3-y2; // Q'
1573 int a = x2-x1, c = y2-y1;
1574 int b = x2-x3, d = y2-y3;
1575 A = c*c + d*d; C = a*a + b*b; B = a*c + b*d;
1577 double ymy = -sqrt(A), ymx = B/ymy;
1578 int64_t px = rnd(ymx), py = rnd(ymy);
1579 sx = x0 + px; ex = x1;
1580 sy = y0 + py; ey = y1;
1581 xs = x2 > x1 || (x2==x1 && x3>=x2) ? 1 : -1;
1582 dx = A*px - B*py; dy = C*py - B*px;
1583 r = 4 * (A*px*px - 2*B*px*py + C*py*py + B*B - A*C);
1585 ys = a*b > 0 && xs*(b*c - a*d) < 0 ? -1 : 1;
1587 double xmx = xs*sqrt(C), xmy = B/xmx;
1588 xmxx = x0 + rnd(xmx);
1589 xmxy = y0 + rnd(xmy);
1593 xmxx = ex; xmxy = ey;
1596 vframe->draw_pixel(sx, sy);
1597 while( xs*(sx-xmxx) < 0 && (xs*dx < 0 || rx() < 0) ) {
1599 vframe->draw_pixel(sx, sy);
1604 void VFrame::smooth_draw(int x1, int y1, int x2, int y2, int x3, int y3)
1606 //printf("p smooth_draw( %d,%d, %d,%d, %d,%d )\n", x1,y1,x2,y2,x3,y3);
1607 if( y1 > y3 ) { // make y3 >= y1
1608 int xt = x1; x1 = x3; x3 = xt;
1609 int yt = y1; y1 = y3; y3 = yt;
1611 if( y1 > y2 && y3 > y2 ) {
1612 smooth_line lt(this), rt(this); // Q on bottom
1613 lt.init1(x1, y1, x2, y2, x3, y3);
1614 rt.init1(x3, y3, x2, y2, x1, y1);
1615 while( !lt.done || !rt.done ) {
1620 else if( y1 < y2 && y3 < y2 ) {
1621 smooth_line lt(this), rt(this); // Q on top
1622 lt.init0(x1, y1, x2, y2, x3, y3, 1);
1623 draw_pixel(lt.sx, lt.sy);
1624 rt.init0(x1, y1, x2, y2, x3, y3, -1);
1625 draw_pixel(rt.sx, rt.sy);
1626 while( !lt.done || !rt.done ) {
1632 smooth_line pt(this); // Q in between
1633 pt.init0(x1, y1, x2, y2, x3, y3, 0);
1634 draw_pixel(pt.sx, pt.sy);
1641 void VFrame::draw_rect(int x1, int y1, int x2, int y2)
1643 draw_line(x1, y1, x2, y1);
1644 draw_line(x2, y1 + 1, x2, y2);
1645 draw_line(x2 - 1, y2, x1, y2);
1646 draw_line(x1, y2 - 1, x1, y1 + 1);
1649 void VFrame::draw_arrow(int x1, int y1, int x2, int y2, int sz)
1651 double angle = atan((float)(y2 - y1) / (float)(x2 - x1));
1652 double angle1 = angle + (float)145 / 360 * 2 * M_PI;
1653 double angle2 = angle - (float)145 / 360 * 2 * M_PI;
1654 int s = x2 < x1 ? -1 : 1;
1655 int x3 = x2 + s * (int)(sz * cos(angle1));
1656 int y3 = y2 + s * (int)(sz * sin(angle1));
1657 int x4 = x2 + s * (int)(sz * cos(angle2));
1658 int y4 = y2 + s * (int)(sz * sin(angle2));
1661 draw_line(x1, y1, x2, y2);
1662 // draw_line(x1, y1 + 1, x2, y2 + 1);
1665 if(abs(y2 - y1) || abs(x2 - x1)) draw_line(x2, y2, x3, y3);
1667 if(abs(y2 - y1) || abs(x2 - x1)) draw_line(x2, y2, x4, y4);
1670 void VFrame::draw_x(int x, int y, int sz)
1672 draw_line(x-sz,y-sz, x+sz,y+sz);
1673 draw_line(x+sz,y-sz, x-sz,y+sz);
1675 void VFrame::draw_t(int x, int y, int sz)
1677 draw_line(x,y-sz, x,y+sz);
1678 draw_line(x+sz,y, x-sz,y);