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);
130 VFrame::VFrame(VFrame &frame)
133 params = new BC_Hash;
134 allocate_data(0, -1, 0, 0, 0, frame.w, frame.h,
135 frame.color_model, frame.bytes_per_line);
139 VFrame::VFrame(int w, int h, int color_model, long bytes_per_line)
142 params = new BC_Hash;
143 allocate_data(data, -1, 0, 0, 0, w, h,
144 color_model, bytes_per_line);
147 VFrame::VFrame(unsigned char *data, int shmid, int w, int h,
148 int color_model, long bytes_per_line)
151 params = new BC_Hash;
152 allocate_data(data, shmid, 0, 0, 0, w, h,
153 color_model, bytes_per_line);
156 VFrame::VFrame(unsigned char *data, int shmid,
157 long y_offset, long u_offset, long v_offset,
158 int w, int h, int color_model, long bytes_per_line)
161 params = new BC_Hash;
162 allocate_data(data, shmid, y_offset, u_offset, v_offset, w, h,
163 color_model, bytes_per_line);
166 VFrame::VFrame(BC_Bitmap *bitmap, int w, int h,
167 int color_model, long bytes_per_line)
170 params = new BC_Hash;
172 unsigned char *data = 0;
173 if( bitmap->is_shared() )
174 shmid = bitmap->get_shmid();
176 data = bitmap->get_data();
177 allocate_data(data, shmid,
178 bitmap->get_y_offset(),
179 bitmap->get_u_offset(),
180 bitmap->get_v_offset(),
181 w, h, color_model, bytes_per_line);
187 params = new BC_Hash;
188 this->color_model = BC_COMPRESSED;
196 // Delete effect stack
197 prev_effects.remove_all_objects();
198 next_effects.remove_all_objects();
203 int VFrame::equivalent(VFrame *src, int test_stacks)
205 return (src->get_color_model() == get_color_model() &&
206 src->get_w() == get_w() &&
207 src->get_h() == get_h() &&
208 src->bytes_per_line == bytes_per_line &&
209 (!test_stacks || equal_stacks(src)));
212 int VFrame::data_matches(VFrame *frame)
214 if(data && frame->get_data() &&
215 frame->params_match(get_w(), get_h(), get_color_model()) &&
216 get_data_size() == frame->get_data_size())
218 int data_size = get_data_size();
219 unsigned char *ptr1 = get_data();
220 unsigned char *ptr2 = frame->get_data();
221 for(int i = 0; i < data_size; i++)
223 if(*ptr1++ != *ptr2++) return 0;
230 // long VFrame::set_shm_offset(long offset)
232 // shm_offset = offset;
236 // long VFrame::get_shm_offset()
238 // return shm_offset;
241 int VFrame::get_memory_type()
246 int VFrame::params_match(int w, int h, int color_model)
248 return (this->w == w &&
250 this->color_model == color_model);
254 int VFrame::reset_parameters(int do_opengl)
259 memory_type = VFrame::PRIVATE;
267 compressed_allocated = 0;
268 compressed_size = 0; // Size of current image
275 sequence_number = -1;
279 set_pixel_color(BLACK);
284 // By default, anything is going to be done in RAM
285 opengl_state = VFrame::RAM;
290 prev_effects.set_array_delete();
291 next_effects.set_array_delete();
295 int VFrame::clear_objects(int do_opengl)
310 case VFrame::PRIVATE:
312 // if(this->w * this->h > 1500 * 1100)
313 // printf("VFrame::clear_objects 2 this=%p data=%p\n", this, data);
316 //printf("VFrame::clear_objects %d this=%p shmid=%p data=%p\n", __LINE__, this, shmid, data);
336 // Delete row pointers
360 int VFrame::get_field2_offset()
362 return field2_offset;
365 int VFrame::set_field2_offset(int value)
367 this->field2_offset = value;
371 void VFrame::set_keyframe(int value)
373 this->is_keyframe = value;
376 int VFrame::get_keyframe()
382 VFrameScene* VFrame::get_scene()
387 int VFrame::calculate_bytes_per_pixel(int color_model)
389 return BC_CModels::calculate_pixelsize(color_model);
392 long VFrame::get_bytes_per_line()
394 return bytes_per_line;
397 long VFrame::get_data_size()
399 return calculate_data_size(w, h, bytes_per_line, color_model) - 4;
402 long VFrame::calculate_data_size(int w, int h, int bytes_per_line, int color_model)
404 return BC_CModels::calculate_datasize(w, h, bytes_per_line, color_model);
407 void VFrame::create_row_pointers()
410 switch(color_model) {
412 if( this->v_offset ) break;
415 this->v_offset = sz + w / 4 * h / 4;
421 if( this->v_offset ) break;
424 this->v_offset = sz + sz / 4;
428 if( this->v_offset ) break;
431 this->v_offset = sz + sz / 2;
434 if( this->v_offset ) break;
437 this->v_offset = sz + sz;
440 if( this->v_offset || a ) break;
441 a = this->data + 3 * sz * sizeof(float);
443 if( this->v_offset ) break;
445 this->u_offset = sz * sizeof(float);
446 this->v_offset = 2 * sz * sizeof(float);
450 rows = new unsigned char*[h];
451 for(int i = 0; i < h; i++)
452 rows[i] = &this->data[i * this->bytes_per_line];
455 y = this->data + this->y_offset;
456 u = this->data + this->u_offset;
457 v = this->data + this->v_offset;
460 int VFrame::allocate_data(unsigned char *data, int shmid,
461 long y_offset, long u_offset, long v_offset, int w, int h,
462 int color_model, long bytes_per_line)
466 this->color_model = color_model;
467 this->bytes_per_pixel = calculate_bytes_per_pixel(color_model);
468 this->y_offset = this->u_offset = this->v_offset = 0;
470 // printf("VFrame::allocate_data %d shmid == 0\n", __LINE__, shmid);
473 this->bytes_per_line = bytes_per_line >= 0 ?
474 bytes_per_line : this->bytes_per_pixel * w;
476 // Allocate data + padding for MMX
478 //printf("VFrame::allocate_data %d %p\n", __LINE__, this->data);
479 memory_type = VFrame::SHARED;
482 this->y_offset = y_offset;
483 this->u_offset = u_offset;
484 this->v_offset = v_offset;
486 else if(shmid >= 0) {
487 memory_type = VFrame::SHMGET;
488 this->data = (unsigned char*)shmat(shmid, NULL, 0);
489 //printf("VFrame::allocate_data %d shmid=%d data=%p\n", __LINE__, shmid, this->data);
491 this->y_offset = y_offset;
492 this->u_offset = u_offset;
493 this->v_offset = v_offset;
496 memory_type = VFrame::PRIVATE;
497 int size = calculate_data_size(this->w, this->h,
498 this->bytes_per_line, this->color_model);
499 if(BC_WindowBase::get_resources()->use_vframe_shm() && use_shm) {
500 this->shmid = shmget(IPC_PRIVATE, size, IPC_CREAT | 0777);
501 if(this->shmid < 0) {
502 printf("VFrame::allocate_data %d could not allocate shared memory\n", __LINE__);
505 this->data = (unsigned char*)shmat(this->shmid, NULL, 0);
506 //printf("VFrame::allocate_data %d %d %d\n", __LINE__, size, this->shmid);
508 //printf("VFrame::allocate_data %d %p\n", __LINE__, this->data);
509 // This causes it to automatically delete when the program exits.
510 shmctl(this->shmid, IPC_RMID, 0);
513 // Have to use malloc for libpng
514 //printf("==vframe %d from %p\n", size, __builtin_return_address(0));
515 this->data = (unsigned char *)malloc(size);
519 // if(this->w * this->h > 1500 * 1100)
520 // printf("VFrame::allocate_data 2 this=%p w=%d h=%d this->data=%p\n",
521 // this, this->w, this->h, this->data);
524 printf("VFrame::allocate_data %dx%d: memory exhausted.\n", this->w, this->h);
526 //printf("VFrame::allocate_data %d %p data=%p %d %d\n", __LINE__, this, this->data, this->w, this->h);
527 //if(size > 1000000) printf("VFrame::allocate_data %d\n", size);
530 // Create row pointers
531 create_row_pointers();
535 void VFrame::set_memory(unsigned char *data,
545 memory_type = VFrame::SHARED;
548 this->y_offset = y_offset;
549 this->u_offset = u_offset;
550 this->v_offset = v_offset;
555 memory_type = VFrame::SHMGET;
556 this->data = (unsigned char*)shmat(shmid, NULL, 0);
560 y = this->data + this->y_offset;
561 u = this->data + this->u_offset;
562 v = this->data + this->v_offset;
564 create_row_pointers();
567 void VFrame::set_memory(BC_Bitmap *bitmap)
570 unsigned char *data = 0;
571 if( bitmap->is_shared() && !bitmap->is_zombie() )
572 shmid = bitmap->get_shmid();
574 data = bitmap->get_data();
575 set_memory(data, shmid,
576 bitmap->get_y_offset(),
577 bitmap->get_u_offset(),
578 bitmap->get_v_offset());
581 void VFrame::set_compressed_memory(unsigned char *data,
590 memory_type = VFrame::SHARED;
597 memory_type = VFrame::SHMGET;
598 this->data = (unsigned char*)shmat(shmid, NULL, 0);
602 this->compressed_allocated = data_allocated;
603 this->compressed_size = data_size;
607 // Reallocate uncompressed buffer with or without alpha
608 int VFrame::reallocate(
619 // if(shmid == 0) printf("VFrame::reallocate %d shmid=%d\n", __LINE__, shmid);
621 // reset_parameters(0);
634 int VFrame::allocate_compressed_data(long bytes)
636 if(bytes < 1) return 1;
638 // Want to preserve original contents
639 if(data && compressed_allocated < bytes)
642 unsigned char *new_data = 0;
643 if(BC_WindowBase::get_resources()->use_vframe_shm() && use_shm)
645 new_shmid = shmget(IPC_PRIVATE,
648 new_data = (unsigned char*)shmat(new_shmid, NULL, 0);
649 shmctl(new_shmid, IPC_RMID, 0);
653 // Have to use malloc for libpng
654 new_data = (unsigned char *)malloc(bytes);
657 bcopy(data, new_data, compressed_allocated);
660 if(memory_type == VFrame::PRIVATE)
670 if(memory_type == VFrame::SHMGET)
678 compressed_allocated = bytes;
683 if(BC_WindowBase::get_resources()->use_vframe_shm() && use_shm)
685 shmid = shmget(IPC_PRIVATE,
688 data = (unsigned char*)shmat(shmid, NULL, 0);
689 shmctl(shmid, IPC_RMID, 0);
693 // Have to use malloc for libpng
694 data = (unsigned char *)malloc(bytes);
697 compressed_allocated = bytes;
704 int VFramePng::read_png(const unsigned char *data, long sz, double xscale, double yscale)
706 // Test for RAW format
707 if(data[0] == 'R' && data[1] == 'A' && data[2] == 'W' && data[3] == ' ') {
708 int new_color_model = BC_RGBA8888;
709 w = data[4] | (data[5] << 8) | (data[6] << 16) | (data[7] << 24);
710 h = data[8] | (data[9] << 8) | (data[10] << 16) | (data[11] << 24);
711 int components = data[12];
712 new_color_model = components == 3 ? BC_RGB888 : BC_RGBA8888;
713 // This shares the data directly
714 // reallocate(data + 20, 0, 0, 0, w, h, new_color_model, -1);
716 // Can't use shared data for theme since button constructions overlay the
718 reallocate(NULL, -1, 0, 0, 0, w, h, new_color_model, -1);
719 memcpy(get_data(), data + 16, w * h * components);
722 else if(data[0] == 0x89 && data[1] == 'P' && data[2] == 'N' && data[3] == 'G') {
724 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
725 png_infop info_ptr = png_create_info_struct(png_ptr);
729 image = data; image_size = sz;
730 png_set_read_fn(png_ptr, this, PngReadFunction::png_read_function);
731 png_read_info(png_ptr, info_ptr);
733 w = png_get_image_width(png_ptr, info_ptr);
734 h = png_get_image_height(png_ptr, info_ptr);
736 int src_color_model = png_get_color_type(png_ptr, info_ptr);
738 /* tell libpng to strip 16 bit/color files down to 8 bits/color */
739 png_set_strip_16(png_ptr);
741 /* extract multiple pixels with bit depths of 1, 2, and 4 from a single
742 * byte into separate bytes (useful for paletted and grayscale images).
744 png_set_packing(png_ptr);
746 /* expand paletted colors into true RGB triplets */
747 if (src_color_model == PNG_COLOR_TYPE_PALETTE)
748 png_set_expand(png_ptr);
750 /* expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel */
751 if (src_color_model == PNG_COLOR_TYPE_GRAY && png_get_bit_depth(png_ptr, info_ptr) < 8)
752 png_set_expand(png_ptr);
754 if (src_color_model == PNG_COLOR_TYPE_GRAY ||
755 src_color_model == PNG_COLOR_TYPE_GRAY_ALPHA)
756 png_set_gray_to_rgb(png_ptr);
758 /* expand paletted or RGB images with transparency to full alpha channels
759 * so the data will be available as RGBA quartets */
760 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)){
762 png_set_expand(png_ptr);
765 switch(src_color_model)
767 case PNG_COLOR_TYPE_GRAY:
768 case PNG_COLOR_TYPE_RGB:
769 new_color_model = BC_RGB888;
772 case PNG_COLOR_TYPE_GRAY_ALPHA:
773 case PNG_COLOR_TYPE_RGB_ALPHA:
775 new_color_model = BC_RGBA8888;
778 case PNG_COLOR_TYPE_PALETTE:
780 new_color_model = BC_RGBA8888;
782 new_color_model = BC_RGB888;
785 reallocate(NULL, -1, 0, 0, 0, w, h, new_color_model, -1);
787 //printf("VFrame::read_png %d %d %d %p\n", __LINE__, w, h, get_rows());
788 png_read_image(png_ptr, get_rows());
789 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
792 printf("VFrame::read_png %d: unknown file format"
793 " 0x%02x 0x%02x 0x%02x 0x%02x\n",
794 __LINE__, data[4], data[5], data[6], data[7]);
797 int ww = w * xscale, hh = h * yscale;
798 if( ww != w || hh != h ) {
799 VFrame vframe(*this);
800 reallocate(NULL, -1, 0, 0, 0, ww, hh, color_model, -1);
801 transfer_from(&vframe);
806 int VFrame::write_png(const char *path)
808 VFrame *vframe = this;
809 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
810 png_infop info_ptr = png_create_info_struct(png_ptr);
811 FILE *out_fd = fopen(path, "w");
813 printf("VFrame::write_png %d %s %s\n", __LINE__, path, strerror(errno));
817 int png_cmodel = PNG_COLOR_TYPE_RGB;
818 int bc_cmodel = get_color_model();
819 switch( bc_cmodel ) {
820 case BC_RGB888: break;
821 case BC_RGBA8888: png_cmodel = PNG_COLOR_TYPE_RGB_ALPHA; break;
822 case BC_A8: png_cmodel = PNG_COLOR_TYPE_GRAY; break;
824 bc_cmodel = BC_RGB888;
825 if( BC_CModels::has_alpha(bc_cmodel) ) {
826 bc_cmodel = BC_RGBA8888;
827 png_cmodel = PNG_COLOR_TYPE_RGB_ALPHA;
829 vframe = new VFrame(get_w(), get_h(), bc_cmodel, -1);
830 vframe->transfer_from(this);
833 png_init_io(png_ptr, out_fd);
834 png_set_compression_level(png_ptr, 9);
835 png_set_IHDR(png_ptr, info_ptr, get_w(), get_h(), 8, png_cmodel,
836 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
837 png_write_info(png_ptr, info_ptr);
838 png_write_image(png_ptr, vframe->get_rows());
839 png_write_end(png_ptr, info_ptr);
840 png_destroy_write_struct(&png_ptr, &info_ptr);
842 if( vframe != this ) delete vframe;
847 #define ZERO_YUV(components, type, max) \
849 for(int i = 0; i < h; i++) \
851 type *row = (type*)get_rows()[i]; \
852 for(int j = 0; j < w; j++) \
854 row[j * components] = 0; \
855 row[j * components + 1] = (max + 1) / 2; \
856 row[j * components + 2] = (max + 1) / 2; \
857 if(components == 4) row[j * components + 3] = 0; \
862 int VFrame::clear_frame()
865 //printf("VFrame::clear_frame %d\n", __LINE__);
866 switch(color_model) {
872 bzero(get_u(), w / 4 * h / 4);
873 bzero(get_v(), w / 4 * h / 4);
880 bzero(get_u(), sz / 4);
881 bzero(get_v(), sz / 4);
886 bzero(get_u(), sz / 2);
887 bzero(get_v(), sz / 2);
890 case BC_RGBA_FLOATP: if( a ) {
891 float *ap = (float *)a;
892 for( int i=sz; --i>=0; ++ap ) *ap = 1.f; }
893 case BC_RGB_FLOATP: {
894 float *rp = (float *)y;
895 for( int i=sz; --i>=0; ++rp ) *rp = 0.f;
896 float *gp = (float *)u;
897 for( int i=sz; --i>=0; ++gp ) *gp = 0.f;
898 float *bp = (float *)v;
899 for( int i=sz; --i>=0; ++bp ) *bp = 0.f;
908 ZERO_YUV(3, unsigned char, 0xff);
912 ZERO_YUV(4, unsigned char, 0xff);
916 ZERO_YUV(3, uint16_t, 0xffff);
919 case BC_YUVA16161616:
920 ZERO_YUV(4, uint16_t, 0xffff);
924 bzero(data, calculate_data_size(w, h, bytes_per_line, color_model));
930 void VFrame::rotate90()
932 // Allocate new frame
933 int new_w = h, new_h = w, new_bytes_per_line = bytes_per_pixel * new_w;
934 unsigned char *new_data = new unsigned char[calculate_data_size(new_w, new_h, new_bytes_per_line, color_model)];
935 unsigned char **new_rows = new unsigned char*[new_h];
936 for(int i = 0; i < new_h; i++)
937 new_rows[i] = &new_data[new_bytes_per_line * i];
940 for(int in_y = 0, out_x = new_w - 1; in_y < h; in_y++, out_x--)
942 for(int in_x = 0, out_y = 0; in_x < w; in_x++, out_y++)
944 for(int k = 0; k < bytes_per_pixel; k++)
946 new_rows[out_y][out_x * bytes_per_pixel + k] =
947 rows[in_y][in_x * bytes_per_pixel + k];
956 bytes_per_line = new_bytes_per_line;
961 void VFrame::rotate270()
963 // Allocate new frame
964 int new_w = h, new_h = w, new_bytes_per_line = bytes_per_pixel * new_w;
965 unsigned char *new_data = new unsigned char[calculate_data_size(new_w, new_h, new_bytes_per_line, color_model)];
966 unsigned char **new_rows = new unsigned char*[new_h];
967 for(int i = 0; i < new_h; i++)
968 new_rows[i] = &new_data[new_bytes_per_line * i];
971 for(int in_y = 0, out_x = 0; in_y < h; in_y++, out_x++)
973 for(int in_x = 0, out_y = new_h - 1; in_x < w; in_x++, out_y--)
975 for(int k = 0; k < bytes_per_pixel; k++)
977 new_rows[out_y][out_x * bytes_per_pixel + k] =
978 rows[in_y][in_x * bytes_per_pixel + k];
987 bytes_per_line = new_bytes_per_line;
992 void VFrame::flip_vert()
994 unsigned char *temp = new unsigned char[bytes_per_line];
995 for(int i = 0, j = h - 1; i < j; i++, j--)
997 memcpy(temp, rows[j], bytes_per_line);
998 memcpy(rows[j], rows[i], bytes_per_line);
999 memcpy(rows[i], temp, bytes_per_line);
1004 void VFrame::flip_horiz()
1006 unsigned char temp[32];
1007 for(int i = 0; i < h; i++)
1009 unsigned char *row = rows[i];
1010 for(int j = 0; j < bytes_per_line / 2; j += bytes_per_pixel)
1012 memcpy(temp, row + j, bytes_per_pixel);
1013 memcpy(row + j, row + bytes_per_line - j - bytes_per_pixel, bytes_per_pixel);
1014 memcpy(row + bytes_per_line - j - bytes_per_pixel, temp, bytes_per_pixel);
1021 int VFrame::copy_from(VFrame *frame)
1023 int w = MIN(this->w, frame->get_w());
1024 int h = MIN(this->h, frame->get_h());
1025 timestamp = frame->timestamp;
1027 switch(frame->color_model)
1030 allocate_compressed_data(frame->compressed_size);
1031 memcpy(data, frame->data, frame->compressed_size);
1032 this->compressed_size = frame->compressed_size;
1036 memcpy(get_y(), frame->get_y(), w * h);
1037 memcpy(get_u(), frame->get_u(), w / 4 * h / 4);
1038 memcpy(get_v(), frame->get_v(), w / 4 * h / 4);
1044 //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());
1045 memcpy(get_y(), frame->get_y(), w * h);
1046 memcpy(get_u(), frame->get_u(), w * h / 4);
1047 memcpy(get_v(), frame->get_v(), w * h / 4);
1051 //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());
1052 memcpy(get_y(), frame->get_y(), w * h);
1053 memcpy(get_u(), frame->get_u(), w * h / 2);
1054 memcpy(get_v(), frame->get_v(), w * h / 2);
1058 //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());
1059 memcpy(get_y(), frame->get_y(), w * h);
1060 memcpy(get_u(), frame->get_u(), w * h);
1061 memcpy(get_v(), frame->get_v(), w * h);
1064 // printf("VFrame::copy_from %d\n", calculate_data_size(w,
1067 // frame->color_model));
1068 // Copy without extra 4 bytes in case the source is a hardware device
1069 memmove(data, frame->data, get_data_size());
1073 params->copy_from(frame->params);
1077 int VFrame::transfer_from(VFrame *that, int bg_color, int in_x, int in_y, int in_w, int in_h)
1079 if( this->get_color_model() == that->get_color_model() &&
1080 this->get_w() == that->get_w() && this->get_h() == that->get_h() &&
1081 this->get_bytes_per_line() == that->get_bytes_per_line() )
1082 return this->copy_from(that);
1084 timestamp = that->timestamp;
1086 BC_CModels::transfer(
1087 this->get_rows(), that->get_rows(), // Packed data out/in
1088 this->get_y(), this->get_u(), this->get_v(), // Planar data out/in
1089 that->get_y(), that->get_u(), that->get_v(),
1090 0, 0, that->get_w(), that->get_h(), // Dimensions in/out
1091 0, 0, this->get_w(), this->get_h(),
1092 that->get_color_model(), this->get_color_model(), // Color models in/out
1093 bg_color, // alpha blend bg_color
1094 that->get_bytes_per_line(),
1095 this->get_bytes_per_line()); // rowspans (of luma for YUV)
1097 unsigned char *in_ptrs[4], *out_ptrs[4];
1098 unsigned char **inp, **outp;
1099 if( BC_CModels::is_planar(that->get_color_model()) ) {
1100 in_ptrs[0] = that->get_y();
1101 in_ptrs[1] = that->get_u();
1102 in_ptrs[2] = that->get_v();
1103 in_ptrs[3] = that->get_a();
1107 inp = that->get_rows();
1108 if( BC_CModels::is_planar(this->get_color_model()) ) {
1109 out_ptrs[0] = this->get_y();
1110 out_ptrs[1] = this->get_u();
1111 out_ptrs[2] = this->get_v();
1112 out_ptrs[3] = this->get_a();
1116 outp = this->get_rows();
1117 BC_CModels::transfer(outp, this->get_color_model(),
1118 0, 0, this->get_w(), this->get_h(),
1119 this->get_bytes_per_line(),
1120 inp, that->get_color_model(),
1121 in_x, in_y, in_w, in_h,
1122 that->get_bytes_per_line(),
1125 params->copy_from(that->params);
1130 int VFrame::get_scale_tables(int *column_table, int *row_table,
1131 int in_x1, int in_y1, int in_x2, int in_y2,
1132 int out_x1, int out_y1, int out_x2, int out_y2)
1135 float w_in = in_x2 - in_x1;
1136 float h_in = in_y2 - in_y1;
1137 int w_out = out_x2 - out_x1;
1138 int h_out = out_y2 - out_y1;
1140 float hscale = w_in / w_out;
1141 float vscale = h_in / h_out;
1143 for(i = 0; i < w_out; i++)
1145 column_table[i] = (int)(hscale * i);
1148 for(i = 0; i < h_out; i++)
1150 row_table[i] = (int)(vscale * i) + in_y1;
1155 void VFrame::push_prev_effect(const char *name)
1158 prev_effects.append(ptr = new char[strlen(name) + 1]);
1160 if(prev_effects.total > MAX_STACK_ELEMENTS) prev_effects.remove_object(0);
1163 void VFrame::pop_prev_effect()
1165 if(prev_effects.total)
1166 prev_effects.remove_object(prev_effects.last());
1169 void VFrame::push_next_effect(const char *name)
1172 next_effects.append(ptr = new char[strlen(name) + 1]);
1174 if(next_effects.total > MAX_STACK_ELEMENTS) next_effects.remove_object(0);
1177 void VFrame::pop_next_effect()
1179 if(next_effects.total)
1180 next_effects.remove_object(next_effects.last());
1183 const char* VFrame::get_next_effect(int number)
1185 if(!next_effects.total) return "";
1187 if(number > next_effects.total - 1) number = next_effects.total - 1;
1189 return next_effects.values[next_effects.total - number - 1];
1192 const char* VFrame::get_prev_effect(int number)
1194 if(!prev_effects.total) return "";
1196 if(number > prev_effects.total - 1) number = prev_effects.total - 1;
1198 return prev_effects.values[prev_effects.total - number - 1];
1201 BC_Hash* VFrame::get_params()
1206 void VFrame::clear_stacks()
1208 next_effects.remove_all_objects();
1209 prev_effects.remove_all_objects();
1214 void VFrame::copy_params(VFrame *src)
1216 status = src->status;
1217 params->copy_from(src->params);
1220 void VFrame::copy_stacks(VFrame *src)
1224 for(int i = 0; i < src->next_effects.total; i++)
1227 next_effects.append(ptr = new char[strlen(src->next_effects.values[i]) + 1]);
1228 strcpy(ptr, src->next_effects.values[i]);
1230 for(int i = 0; i < src->prev_effects.total; i++)
1233 prev_effects.append(ptr = new char[strlen(src->prev_effects.values[i]) + 1]);
1234 strcpy(ptr, src->prev_effects.values[i]);
1240 int VFrame::equal_stacks(VFrame *src)
1242 for(int i = 0; i < src->next_effects.total && i < next_effects.total; i++)
1244 if(strcmp(src->next_effects.values[i], next_effects.values[i])) return 0;
1247 for(int i = 0; i < src->prev_effects.total && i < prev_effects.total; i++)
1249 if(strcmp(src->prev_effects.values[i], prev_effects.values[i])) return 0;
1252 if(!params->equivalent(src->params)) return 0;
1256 void VFrame::dump_stacks()
1258 printf("VFrame::dump_stacks\n");
1259 printf(" next_effects:\n");
1260 for(int i = next_effects.total - 1; i >= 0; i--)
1261 printf(" %s\n", next_effects.values[i]);
1262 printf(" prev_effects:\n");
1263 for(int i = prev_effects.total - 1; i >= 0; i--)
1264 printf(" %s\n", prev_effects.values[i]);
1267 void VFrame::dump_params()
1274 printf("VFrame::dump %d this=%p\n", __LINE__, this);
1275 printf(" w=%d h=%d colormodel=%d rows=%p use_shm=%d shmid=%d\n",
1276 w, h, color_model, rows, use_shm, shmid);
1280 int VFrame::get_memory_usage()
1282 if(get_compressed_allocated()) return get_compressed_allocated();
1283 return get_h() * get_bytes_per_line();
1286 void VFrame::set_pixel_color(int rgb)
1289 int ir = 0xff & (pixel_rgb >> 16);
1290 int ig = 0xff & (pixel_rgb >> 8);
1291 int ib = 0xff & (pixel_rgb >> 0);
1292 bc_rgb2yuv(ir,ig,ib, ir,ig,ib);
1293 pixel_yuv = (ir<<16) | (ig<<8) | (ib<<0);
1296 void VFrame::set_stiple(int mask)
1301 int VFrame::draw_pixel(int x, int y)
1303 if( x < 0 || y < 0 || x >= get_w() || y >= get_h() ) return 1;
1304 if( draw_point ) return (this->*draw_point)(x, y);
1306 #define DRAW_PIXEL(type, r, g, b) { \
1307 type **rows = (type**)get_rows(); \
1308 rows[y][x * components + 0] = r; \
1309 rows[y][x * components + 1] = g; \
1310 rows[y][x * components + 2] = b; \
1311 if( components == 4 ) \
1312 rows[y][x * components + 3] = mx; \
1314 int components = BC_CModels::components(color_model);
1315 int bch = BC_CModels::calculate_pixelsize(color_model) / components;
1316 int sz = 8*bch, mx = BC_CModels::is_float(color_model) ? 1 : (1<<sz)-1;
1317 int is_yuv = BC_CModels::is_yuv(color_model);
1318 int pixel_color = is_yuv ? pixel_yuv : pixel_rgb;
1319 int ir = 0xff & (pixel_color >> 16); float fr = 0;
1320 int ig = 0xff & (pixel_color >> 8); float fg = 0;
1321 int ib = 0xff & (pixel_color >> 0); float fb = 0;
1322 if( (x+y) & stipple ) {
1323 ir = 255 - ir; ig = 255 - ig; ib = 255 - ib;
1325 if( BC_CModels::is_float(color_model) ) {
1326 fr = ir / 255.; fg = ig / 255.; fb = ib / 255.;
1329 else if( (sz-=8) > 0 ) {
1330 ir <<= sz; ig <<= sz; ib <<= sz;
1333 switch(get_color_model()) {
1338 DRAW_PIXEL(uint8_t, ir, ig, ib);
1342 case BC_RGBA16161616:
1343 case BC_YUVA16161616:
1344 DRAW_PIXEL(uint16_t, ir, ig, ib);
1348 DRAW_PIXEL(float, fr, fg, fb);
1356 void VFrame::draw_line(int x1, int y1, int x2, int y2)
1359 int tx = x1; x1 = x2; x2 = tx;
1360 int ty = y1; y1 = y2; y2 = ty;
1364 int dx = x2-x1, dy = y2-y1;
1365 int dx2 = 2*dx, dy2 = 2*dy;
1366 if( dx < 0 ) dx = -dx;
1367 int r = dx > dy ? dx : dy, n = r;
1369 if( dx2 < 0 ) dir += 1;
1371 if( dx2 >= 0 ) do { /* +Y, +X */
1373 if( (r -= dx2) < 0 ) { r += dy2; ++x; }
1374 } while( --n >= 0 );
1375 else do { /* +Y, -X */
1377 if( (r += dx2) < 0 ) { r += dy2; --x; }
1378 } while( --n >= 0 );
1381 if( dx2 >= 0 ) do { /* +X, +Y */
1383 if( (r -= dy2) < 0 ) { r += dx2; ++y; }
1384 } while( --n >= 0 );
1385 else do { /* -X, +Y */
1387 if( (r -= dy2) < 0 ) { r -= dx2; ++y; }
1388 } while( --n >= 0 );
1392 // g++ -dD -E - < /dev/null | grep DBL_EPSILON
1393 #ifndef __DBL_EPSILON__
1394 #define __DBL_EPSILON__ ((double)2.22044604925031308085e-16L)
1396 // weakest fraction * graphics integer range
1397 #define RND_EPSILON (__DBL_EPSILON__*65536)
1400 int rnd(double v) { return round(v)+RND_EPSILON; }
1403 int sx, sy, ex, ey; /* current point, end point */
1404 int xs, ys; /* x/y quadrant sign -1/1 */
1405 int64_t A, B, C; /* quadratic coefficients */
1406 int64_t r, dx, dy; /* residual, dr/dx and dr/dy */
1407 int xmxx, xmxy; /* x,y at apex */
1410 void init0(int x1,int y1, int x2,int y2, int x3,int y3, int top);
1411 void init1(int x1,int y1, int x2,int y2, int x3,int y3);
1412 int64_t rx() { return r + xs*8*dx + 4*A; }
1413 void moveX(int64_t r) {
1414 dx += xs*A; dy -= xs*B;
1415 this->r = r; sx += xs;
1417 int64_t ry() { return r + 8*dy + 4*C; }
1418 void moveY(int64_t r) {
1424 smooth_line(VFrame *vframe) { this->vframe = vframe; this->done = 0; }
1428 void smooth_line::draw()
1431 if( abs(dy) >= abs(dx) ) {
1432 if( xs*(sx-xmxx) >= 0 ) {
1433 if( ys > 0 ) { done = 1; return; }
1434 if( dy < 0 || ry() < 0 ) { moveY(ry()); goto xit; }
1435 xmxx = ex; xmxy = ey;
1440 if( abs(rr) < abs(r) )
1445 if( ys > 0 ) { done = 1; return; }
1446 xmxx = ex; xmxy = ey;
1451 if( abs(rr) < abs(r) )
1454 xit: vframe->draw_pixel(sx, sy);
1457 void VFrame::draw_smooth(int x1, int y1, int x2, int y2, int x3, int y3)
1459 if( (x1 == x2 && y1 == y2) || (x2 == x3 && y2 == y3) )
1460 draw_line(x1,y1, x3,y3);
1461 else if( x1 == x3 && y1 == y3 )
1462 draw_line(x1,y1, x2,y2);
1463 else if( (x2-x1) * (y2-y3) == (x2-x3) * (y2-y1) ) {
1464 // co-linear, draw line from min to max
1466 if( x2 < x1 ) { x1 = x2; y1 = y2; }
1467 if( x2 > x3 ) { x3 = x2; y3 = y2; }
1470 if( x2 > x1 ) { x1 = x2; y1 = y2; }
1471 if( x2 < x3 ) { x3 = x2; y3 = y2; }
1473 draw_line(x1,y1, x3,y3);
1476 smooth_draw(x1, y1, x2, y2, x3, y3);
1480 Non-Parametric Smooth Curve Generation. Don Kelly 1984
1487 Let the starting point be P. the ending point R. and the tangent vertex Q.
1488 A general point Z on the curve is then
1489 Z = (P + R - Q) + (Q - P) sin t + (Q - R) cos t
1491 Expanding the Cartesian coordinates around (P + R - Q) gives
1492 [x y] = Z - (P + R - Q)
1495 x = a*sin(t) + b*cos(t)
1496 y = c*sin(t) + d*cos(t)
1498 from which t can now be eliminated via
1499 c*x - a*y = (c*b - a*d)*cos(t)
1500 d*x - b*y = (a*d - c*b)*sin(t)
1502 giving the Cartesian equation for the ellipse as
1503 f(x, y) = (c*x - a*y)**2 + (d*x - b*y)**2 - (a*d - c*b)**2 = 0
1505 or: f(x, y) = A*x**2 - 2*B*x*y + C*y**2 + B**2 - A*C = 0
1506 where: A = c**2 + d**2, B = a*c + b*d, C = a**2 + b**2
1508 The maximum y extent of the ellipse may now be derived as follows:
1509 let df/dx = 0, 2*A*x = 2*B*y, x = y*B/A
1510 f(x, y) == B**2 * y**2 / A - 2*B**2 * y**2 / A + C*y**2 + B**2 - A*C = 0
1511 (A*C - B**2)*y = (A*C - B**2)*A
1512 max x = sqrt(C), at y = B/sqrt(C)
1513 max y = sqrt(A), at x = B/sqrt(A)
1518 /* x1,y1 = P, x2,y2 = Q, x3,y3=R,
1519 * draw from P to Q to R if top=0
1520 * or from P to (x,ymax) if top>0
1521 * or from Q to (x,ymax) if top<0
1523 void smooth_line::init0(int x1,int y1, int x2,int y2, int x3,int y3, int top)
1525 int x0 = x1+x3-x2, y0 = y1+y3-y2; // Q'
1527 int a = x2-x1, c = y2-y1;
1528 int b = x2-x3, d = y2-y3;
1529 A = c*c + d*d; C = a*a + b*b; B = a*c + b*d;
1531 sx = top >= 0 ? x1 : x3;
1532 sy = top >= 0 ? y1 : y3;
1533 xs = x2 > sx || (x2==sx && (x1+x3-sx)>=x2) ? 1 : -1;
1534 int64_t px = sx-x0, py = sy-y0;
1535 dx = A*px - B*py; dy = C*py - B*px;
1539 double ymy = sqrt(A), ymx = B/ymy;
1547 ys = a*b > 0 && (!top || top*xs*(b*c - a*d) > 0) ? -1 : 1;
1549 double xmx = xs*sqrt(C), xmy = B/xmx;
1550 xmxx = x0 + rnd(xmx);
1551 xmxy = y0 + rnd(xmy);
1554 xmxx = ex; xmxy = ey;
1558 /* x1,y1 = P, x2,y2 = Q, x3,y3=R,
1559 * draw from (x,ymax) to P
1561 void smooth_line::init1(int x1,int y1, int x2,int y2, int x3,int y3)
1563 int x0 = x1+x3-x2, y0 = y1+y3-y2; // Q'
1565 int a = x2-x1, c = y2-y1;
1566 int b = x2-x3, d = y2-y3;
1567 A = c*c + d*d; C = a*a + b*b; B = a*c + b*d;
1569 double ymy = -sqrt(A), ymx = B/ymy;
1570 int64_t px = rnd(ymx), py = rnd(ymy);
1571 sx = x0 + px; ex = x1;
1572 sy = y0 + py; ey = y1;
1573 xs = x2 > x1 || (x2==x1 && x3>=x2) ? 1 : -1;
1574 dx = A*px - B*py; dy = C*py - B*px;
1575 r = 4 * (A*px*px - 2*B*px*py + C*py*py + B*B - A*C);
1577 ys = a*b > 0 && xs*(b*c - a*d) < 0 ? -1 : 1;
1579 double xmx = xs*sqrt(C), xmy = B/xmx;
1580 xmxx = x0 + rnd(xmx);
1581 xmxy = y0 + rnd(xmy);
1585 xmxx = ex; xmxy = ey;
1588 vframe->draw_pixel(sx, sy);
1589 while( xs*(sx-xmxx) < 0 && (xs*dx < 0 || rx() < 0) ) {
1591 vframe->draw_pixel(sx, sy);
1596 void VFrame::smooth_draw(int x1, int y1, int x2, int y2, int x3, int y3)
1598 //printf("p smooth_draw( %d,%d, %d,%d, %d,%d )\n", x1,y1,x2,y2,x3,y3);
1599 if( y1 > y3 ) { // make y3 >= y1
1600 int xt = x1; x1 = x3; x3 = xt;
1601 int yt = y1; y1 = y3; y3 = yt;
1603 if( y1 > y2 && y3 > y2 ) {
1604 smooth_line lt(this), rt(this); // Q on bottom
1605 lt.init1(x1, y1, x2, y2, x3, y3);
1606 rt.init1(x3, y3, x2, y2, x1, y1);
1607 while( !lt.done || !rt.done ) {
1612 else if( y1 < y2 && y3 < y2 ) {
1613 smooth_line lt(this), rt(this); // Q on top
1614 lt.init0(x1, y1, x2, y2, x3, y3, 1);
1615 draw_pixel(lt.sx, lt.sy);
1616 rt.init0(x1, y1, x2, y2, x3, y3, -1);
1617 draw_pixel(rt.sx, rt.sy);
1618 while( !lt.done || !rt.done ) {
1624 smooth_line pt(this); // Q in between
1625 pt.init0(x1, y1, x2, y2, x3, y3, 0);
1626 draw_pixel(pt.sx, pt.sy);
1633 void VFrame::draw_rect(int x1, int y1, int x2, int y2)
1635 draw_line(x1, y1, x2, y1);
1636 draw_line(x2, y1 + 1, x2, y2);
1637 draw_line(x2 - 1, y2, x1, y2);
1638 draw_line(x1, y2 - 1, x1, y1 + 1);
1641 void VFrame::draw_arrow(int x1, int y1, int x2, int y2, int sz)
1643 double angle = atan((float)(y2 - y1) / (float)(x2 - x1));
1644 double angle1 = angle + (float)145 / 360 * 2 * M_PI;
1645 double angle2 = angle - (float)145 / 360 * 2 * M_PI;
1646 int s = x2 < x1 ? -1 : 1;
1647 int x3 = x2 + s * (int)(sz * cos(angle1));
1648 int y3 = y2 + s * (int)(sz * sin(angle1));
1649 int x4 = x2 + s * (int)(sz * cos(angle2));
1650 int y4 = y2 + s * (int)(sz * sin(angle2));
1653 draw_line(x1, y1, x2, y2);
1654 // draw_line(x1, y1 + 1, x2, y2 + 1);
1657 if(abs(y2 - y1) || abs(x2 - x1)) draw_line(x2, y2, x3, y3);
1659 if(abs(y2 - y1) || abs(x2 - x1)) draw_line(x2, y2, x4, y4);
1662 void VFrame::draw_x(int x, int y, int sz)
1664 draw_line(x-sz,y-sz, x+sz,y+sz);
1665 draw_line(x+sz,y-sz, x-sz,y+sz);
1667 void VFrame::draw_t(int x, int y, int sz)
1669 draw_line(x,y-sz, x,y+sz);
1670 draw_line(x+sz,y, x-sz,y);