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
31 #include "bcpbuffer.h"
32 #include "bcresources.h"
33 #include "bcsignals.h"
34 #include "bcsynchronous.h"
35 #include "bctexture.h"
36 #include "bcwindowbase.h"
38 #include "bccmodels.h"
44 static void png_read_function(png_structp png_ptr,
45 png_bytep data, png_size_t length)
47 VFrame *frame = (VFrame*)png_get_io_ptr(png_ptr);
48 if(frame->image_size - frame->image_offset < (long)length)
50 printf("PngReadFunction::png_read_function %d: overrun\n", __LINE__);
51 length = frame->image_size - frame->image_offset;
54 memcpy(data, &frame->image[frame->image_offset], length);
55 frame->image_offset += length;
65 VFrameScene::VFrameScene()
69 VFrameScene::~VFrameScene()
79 //static BCCounter counter;
82 VFramePng::VFramePng(unsigned char *png_data, double scale)
85 ((long)png_data[0] << 24) | ((long)png_data[1] << 16) |
86 ((long)png_data[2] << 8) | (long)png_data[3];
87 if( !scale ) scale = BC_WindowBase::get_resources()->icon_scale;
88 read_png(png_data+4, image_size, scale, scale);
91 VFramePng::VFramePng(unsigned char *png_data, long image_size, double xscale, double yscale)
93 if( !xscale ) xscale = BC_WindowBase::get_resources()->icon_scale;
94 if( !yscale ) yscale = BC_WindowBase::get_resources()->icon_scale;
95 read_png(png_data, image_size, xscale, yscale);
98 VFramePng::~VFramePng()
103 VFrame::VFrame(VFrame &frame)
106 params = new BC_Hash;
107 allocate_data(0, -1, 0, 0, 0, frame.w, frame.h,
108 frame.color_model, frame.bytes_per_line);
109 memcpy(data, frame.data, bytes_per_line * h);
113 VFrame::VFrame(int w, int h, int color_model, long bytes_per_line)
116 params = new BC_Hash;
117 allocate_data(data, -1, 0, 0, 0, w, h,
118 color_model, bytes_per_line);
121 VFrame::VFrame(unsigned char *data, int shmid, int w, int h,
122 int color_model, long bytes_per_line)
125 params = new BC_Hash;
126 allocate_data(data, shmid, 0, 0, 0, w, h,
127 color_model, bytes_per_line);
130 VFrame::VFrame(unsigned char *data, int shmid,
131 long y_offset, long u_offset, long v_offset,
132 int w, int h, int color_model, long bytes_per_line)
135 params = new BC_Hash;
136 allocate_data(data, shmid, y_offset, u_offset, v_offset, w, h,
137 color_model, bytes_per_line);
140 VFrame::VFrame(BC_Bitmap *bitmap, int w, int h,
141 int color_model, long bytes_per_line)
144 params = new BC_Hash;
146 unsigned char *data = 0;
147 if( bitmap->is_shared() )
148 shmid = bitmap->get_shmid();
150 data = bitmap->get_data();
151 allocate_data(data, shmid,
152 bitmap->get_y_offset(),
153 bitmap->get_u_offset(),
154 bitmap->get_v_offset(),
155 w, h, color_model, bytes_per_line);
161 params = new BC_Hash;
162 this->color_model = BC_COMPRESSED;
170 // Delete effect stack
171 prev_effects.remove_all_objects();
172 next_effects.remove_all_objects();
177 int VFrame::equivalent(VFrame *src, int test_stacks)
179 return (src->get_color_model() == get_color_model() &&
180 src->get_w() == get_w() &&
181 src->get_h() == get_h() &&
182 src->bytes_per_line == bytes_per_line &&
183 (!test_stacks || equal_stacks(src)));
186 int VFrame::data_matches(VFrame *frame)
188 if(data && frame->get_data() &&
189 frame->params_match(get_w(), get_h(), get_color_model()) &&
190 get_data_size() == frame->get_data_size())
192 int data_size = get_data_size();
193 unsigned char *ptr1 = get_data();
194 unsigned char *ptr2 = frame->get_data();
195 for(int i = 0; i < data_size; i++)
197 if(*ptr1++ != *ptr2++) return 0;
204 // long VFrame::set_shm_offset(long offset)
206 // shm_offset = offset;
210 // long VFrame::get_shm_offset()
212 // return shm_offset;
215 int VFrame::get_memory_type()
220 int VFrame::params_match(int w, int h, int color_model)
222 return (this->w == w &&
224 this->color_model == color_model);
228 int VFrame::reset_parameters(int do_opengl)
233 memory_type = VFrame::PRIVATE;
241 compressed_allocated = 0;
242 compressed_size = 0; // Size of current image
249 sequence_number = -1;
255 // By default, anything is going to be done in RAM
256 opengl_state = VFrame::RAM;
261 prev_effects.set_array_delete();
262 next_effects.set_array_delete();
266 int VFrame::clear_objects(int do_opengl)
281 case VFrame::PRIVATE:
283 // if(this->w * this->h > 1500 * 1100)
284 // printf("VFrame::clear_objects 2 this=%p data=%p\n", this, data);
287 //printf("VFrame::clear_objects %d this=%p shmid=%p data=%p\n", __LINE__, this, shmid, data);
307 // Delete row pointers
330 int VFrame::get_field2_offset()
332 return field2_offset;
335 int VFrame::set_field2_offset(int value)
337 this->field2_offset = value;
341 void VFrame::set_keyframe(int value)
343 this->is_keyframe = value;
346 int VFrame::get_keyframe()
352 VFrameScene* VFrame::get_scene()
357 int VFrame::calculate_bytes_per_pixel(int color_model)
359 return BC_CModels::calculate_pixelsize(color_model);
362 long VFrame::get_bytes_per_line()
364 return bytes_per_line;
367 long VFrame::get_data_size()
369 return calculate_data_size(w, h, bytes_per_line, color_model) - 4;
372 long VFrame::calculate_data_size(int w, int h, int bytes_per_line, int color_model)
374 return BC_CModels::calculate_datasize(w, h, bytes_per_line, color_model);
377 void VFrame::create_row_pointers()
380 switch(color_model) {
382 if( this->v_offset ) break;
385 this->v_offset = sz + w / 4 * h / 4;
390 if( this->v_offset ) break;
393 this->v_offset = sz + sz / 4;
397 if( this->v_offset ) break;
400 this->v_offset = sz + sz / 2;
403 if( this->v_offset ) break;
406 this->v_offset = sz + sz;
409 if( this->v_offset || a ) break;
410 a = this->data + 3 * sz * sizeof(float);
412 if( this->v_offset ) break;
414 this->u_offset = sz * sizeof(float);
415 this->v_offset = 2 * sz * sizeof(float);
419 rows = new unsigned char*[h];
420 for(int i = 0; i < h; i++)
421 rows[i] = &this->data[i * this->bytes_per_line];
424 y = this->data + this->y_offset;
425 u = this->data + this->u_offset;
426 v = this->data + this->v_offset;
429 int VFrame::allocate_data(unsigned char *data, int shmid,
430 long y_offset, long u_offset, long v_offset, int w, int h,
431 int color_model, long bytes_per_line)
435 this->color_model = color_model;
436 this->bytes_per_pixel = calculate_bytes_per_pixel(color_model);
437 this->y_offset = this->u_offset = this->v_offset = 0;
439 // printf("VFrame::allocate_data %d shmid == 0\n", __LINE__, shmid);
442 this->bytes_per_line = bytes_per_line >= 0 ?
443 bytes_per_line : this->bytes_per_pixel * w;
445 // Allocate data + padding for MMX
447 //printf("VFrame::allocate_data %d %p\n", __LINE__, this->data);
448 memory_type = VFrame::SHARED;
451 this->y_offset = y_offset;
452 this->u_offset = u_offset;
453 this->v_offset = v_offset;
455 else if(shmid >= 0) {
456 memory_type = VFrame::SHMGET;
457 this->data = (unsigned char*)shmat(shmid, NULL, 0);
458 //printf("VFrame::allocate_data %d shmid=%d data=%p\n", __LINE__, shmid, this->data);
460 this->y_offset = y_offset;
461 this->u_offset = u_offset;
462 this->v_offset = v_offset;
465 memory_type = VFrame::PRIVATE;
466 int size = calculate_data_size(this->w, this->h,
467 this->bytes_per_line, this->color_model);
468 if(BC_WindowBase::get_resources()->use_vframe_shm() && use_shm) {
469 this->shmid = shmget(IPC_PRIVATE, size, IPC_CREAT | 0777);
470 if(this->shmid < 0) {
471 printf("VFrame::allocate_data %d could not allocate shared memory\n", __LINE__);
474 this->data = (unsigned char*)shmat(this->shmid, NULL, 0);
475 //printf("VFrame::allocate_data %d %d %d\n", __LINE__, size, this->shmid);
477 //printf("VFrame::allocate_data %d %p\n", __LINE__, this->data);
478 // This causes it to automatically delete when the program exits.
479 shmctl(this->shmid, IPC_RMID, 0);
482 // Have to use malloc for libpng
483 this->data = (unsigned char *)malloc(size);
487 // if(this->w * this->h > 1500 * 1100)
488 // printf("VFrame::allocate_data 2 this=%p w=%d h=%d this->data=%p\n",
489 // this, this->w, this->h, this->data);
492 printf("VFrame::allocate_data %dx%d: memory exhausted.\n", this->w, this->h);
494 //printf("VFrame::allocate_data %d %p data=%p %d %d\n", __LINE__, this, this->data, this->w, this->h);
495 //if(size > 1000000) printf("VFrame::allocate_data %d\n", size);
498 // Create row pointers
499 create_row_pointers();
503 void VFrame::set_memory(unsigned char *data,
513 memory_type = VFrame::SHARED;
516 this->y_offset = y_offset;
517 this->u_offset = u_offset;
518 this->v_offset = v_offset;
523 memory_type = VFrame::SHMGET;
524 this->data = (unsigned char*)shmat(shmid, NULL, 0);
528 y = this->data + this->y_offset;
529 u = this->data + this->u_offset;
530 v = this->data + this->v_offset;
532 create_row_pointers();
535 void VFrame::set_memory(BC_Bitmap *bitmap)
538 unsigned char *data = 0;
539 if( bitmap->is_shared() && !bitmap->is_zombie() )
540 shmid = bitmap->get_shmid();
542 data = bitmap->get_data();
543 set_memory(data, shmid,
544 bitmap->get_y_offset(),
545 bitmap->get_u_offset(),
546 bitmap->get_v_offset());
549 void VFrame::set_compressed_memory(unsigned char *data,
558 memory_type = VFrame::SHARED;
565 memory_type = VFrame::SHMGET;
566 this->data = (unsigned char*)shmat(shmid, NULL, 0);
570 this->compressed_allocated = data_allocated;
571 this->compressed_size = data_size;
575 // Reallocate uncompressed buffer with or without alpha
576 int VFrame::reallocate(
587 // if(shmid == 0) printf("VFrame::reallocate %d shmid=%d\n", __LINE__, shmid);
589 // reset_parameters(0);
602 int VFrame::allocate_compressed_data(long bytes)
604 if(bytes < 1) return 1;
606 // Want to preserve original contents
607 if(data && compressed_allocated < bytes)
610 unsigned char *new_data = 0;
611 if(BC_WindowBase::get_resources()->use_vframe_shm() && use_shm)
613 new_shmid = shmget(IPC_PRIVATE,
616 new_data = (unsigned char*)shmat(new_shmid, NULL, 0);
617 shmctl(new_shmid, IPC_RMID, 0);
621 // Have to use malloc for libpng
622 new_data = (unsigned char *)malloc(bytes);
625 bcopy(data, new_data, compressed_allocated);
628 if(memory_type == VFrame::PRIVATE)
638 if(memory_type == VFrame::SHMGET)
646 compressed_allocated = bytes;
651 if(BC_WindowBase::get_resources()->use_vframe_shm() && use_shm)
653 shmid = shmget(IPC_PRIVATE,
656 data = (unsigned char*)shmat(shmid, NULL, 0);
657 shmctl(shmid, IPC_RMID, 0);
661 // Have to use malloc for libpng
662 data = (unsigned char *)malloc(bytes);
665 compressed_allocated = bytes;
672 int VFramePng::read_png(const unsigned char *data, long sz, double xscale, double yscale)
674 // Test for RAW format
675 if(data[0] == 'R' && data[1] == 'A' && data[2] == 'W' && data[3] == ' ') {
676 int new_color_model = BC_RGBA8888;
677 w = data[4] | (data[5] << 8) | (data[6] << 16) | (data[7] << 24);
678 h = data[8] | (data[9] << 8) | (data[10] << 16) | (data[11] << 24);
679 int components = data[12];
680 new_color_model = components == 3 ? BC_RGB888 : BC_RGBA8888;
681 // This shares the data directly
682 // reallocate(data + 20, 0, 0, 0, w, h, new_color_model, -1);
684 // Can't use shared data for theme since button constructions overlay the
686 reallocate(NULL, -1, 0, 0, 0, w, h, new_color_model, -1);
687 memcpy(get_data(), data + 16, w * h * components);
690 else if(data[0] == 0x89 && data[1] == 'P' && data[2] == 'N' && data[3] == 'G') {
692 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
693 png_infop info_ptr = png_create_info_struct(png_ptr);
697 image = data; image_size = sz;
698 png_set_read_fn(png_ptr, this, PngReadFunction::png_read_function);
699 png_read_info(png_ptr, info_ptr);
701 w = png_get_image_width(png_ptr, info_ptr);
702 h = png_get_image_height(png_ptr, info_ptr);
704 int src_color_model = png_get_color_type(png_ptr, info_ptr);
706 /* tell libpng to strip 16 bit/color files down to 8 bits/color */
707 png_set_strip_16(png_ptr);
709 /* extract multiple pixels with bit depths of 1, 2, and 4 from a single
710 * byte into separate bytes (useful for paletted and grayscale images).
712 png_set_packing(png_ptr);
714 /* expand paletted colors into true RGB triplets */
715 if (src_color_model == PNG_COLOR_TYPE_PALETTE)
716 png_set_expand(png_ptr);
718 /* expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel */
719 if (src_color_model == PNG_COLOR_TYPE_GRAY && png_get_bit_depth(png_ptr, info_ptr) < 8)
720 png_set_expand(png_ptr);
722 if (src_color_model == PNG_COLOR_TYPE_GRAY ||
723 src_color_model == PNG_COLOR_TYPE_GRAY_ALPHA)
724 png_set_gray_to_rgb(png_ptr);
726 /* expand paletted or RGB images with transparency to full alpha channels
727 * so the data will be available as RGBA quartets */
728 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)){
730 png_set_expand(png_ptr);
733 switch(src_color_model)
735 case PNG_COLOR_TYPE_GRAY:
736 case PNG_COLOR_TYPE_RGB:
737 new_color_model = BC_RGB888;
740 case PNG_COLOR_TYPE_GRAY_ALPHA:
741 case PNG_COLOR_TYPE_RGB_ALPHA:
743 new_color_model = BC_RGBA8888;
746 case PNG_COLOR_TYPE_PALETTE:
748 new_color_model = BC_RGBA8888;
750 new_color_model = BC_RGB888;
753 reallocate(NULL, -1, 0, 0, 0, w, h, new_color_model, -1);
755 //printf("VFrame::read_png %d %d %d %p\n", __LINE__, w, h, get_rows());
756 png_read_image(png_ptr, get_rows());
757 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
760 printf("VFrame::read_png %d: unknown file format"
761 " 0x%02x 0x%02x 0x%02x 0x%02x\n",
762 __LINE__, data[4], data[5], data[6], data[7]);
765 int ww = w * xscale, hh = h * yscale;
766 if( ww != w || hh != h ) {
767 VFrame vframe(*this);
768 reallocate(NULL, -1, 0, 0, 0, ww, hh, color_model, -1);
769 transfer_from(&vframe);
774 int VFrame::write_png(const char *path)
776 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
777 png_infop info_ptr = png_create_info_struct(png_ptr);
778 FILE *out_fd = fopen(path, "w");
781 printf("VFrame::write_png %d %s %s\n", __LINE__, path, strerror(errno));
785 int png_cmodel = PNG_COLOR_TYPE_RGB;
786 switch(get_color_model())
790 png_cmodel = PNG_COLOR_TYPE_RGB;
795 png_cmodel = PNG_COLOR_TYPE_RGB_ALPHA;
799 png_cmodel = PNG_COLOR_TYPE_GRAY;
803 png_init_io(png_ptr, out_fd);
804 png_set_compression_level(png_ptr, 9);
805 png_set_IHDR(png_ptr,
812 PNG_COMPRESSION_TYPE_DEFAULT,
813 PNG_FILTER_TYPE_DEFAULT);
814 png_write_info(png_ptr, info_ptr);
815 png_write_image(png_ptr, get_rows());
816 png_write_end(png_ptr, info_ptr);
817 png_destroy_write_struct(&png_ptr, &info_ptr);
823 #define ZERO_YUV(components, type, max) \
825 for(int i = 0; i < h; i++) \
827 type *row = (type*)get_rows()[i]; \
828 for(int j = 0; j < w; j++) \
830 row[j * components] = 0; \
831 row[j * components + 1] = (max + 1) / 2; \
832 row[j * components + 2] = (max + 1) / 2; \
833 if(components == 4) row[j * components + 3] = 0; \
838 int VFrame::clear_frame()
841 //printf("VFrame::clear_frame %d\n", __LINE__);
842 switch(color_model) {
848 bzero(get_u(), w / 4 * h / 4);
849 bzero(get_v(), w / 4 * h / 4);
855 bzero(get_u(), sz / 4);
856 bzero(get_v(), sz / 4);
861 bzero(get_u(), sz / 2);
862 bzero(get_v(), sz / 2);
865 case BC_RGBA_FLOATP: if( a ) {
866 float *ap = (float *)a;
867 for( int i=sz; --i>=0; ++ap ) *ap = 1.f; }
868 case BC_RGB_FLOATP: {
869 float *rp = (float *)y;
870 for( int i=sz; --i>=0; ++rp ) *rp = 0.f;
871 float *gp = (float *)u;
872 for( int i=sz; --i>=0; ++gp ) *gp = 0.f;
873 float *bp = (float *)v;
874 for( int i=sz; --i>=0; ++bp ) *bp = 0.f;
883 ZERO_YUV(3, unsigned char, 0xff);
887 ZERO_YUV(4, unsigned char, 0xff);
891 ZERO_YUV(3, uint16_t, 0xffff);
894 case BC_YUVA16161616:
895 ZERO_YUV(4, uint16_t, 0xffff);
899 bzero(data, calculate_data_size(w, h, bytes_per_line, color_model));
905 void VFrame::rotate90()
907 // Allocate new frame
908 int new_w = h, new_h = w, new_bytes_per_line = bytes_per_pixel * new_w;
909 unsigned char *new_data = new unsigned char[calculate_data_size(new_w, new_h, new_bytes_per_line, color_model)];
910 unsigned char **new_rows = new unsigned char*[new_h];
911 for(int i = 0; i < new_h; i++)
912 new_rows[i] = &new_data[new_bytes_per_line * i];
915 for(int in_y = 0, out_x = new_w - 1; in_y < h; in_y++, out_x--)
917 for(int in_x = 0, out_y = 0; in_x < w; in_x++, out_y++)
919 for(int k = 0; k < bytes_per_pixel; k++)
921 new_rows[out_y][out_x * bytes_per_pixel + k] =
922 rows[in_y][in_x * bytes_per_pixel + k];
931 bytes_per_line = new_bytes_per_line;
936 void VFrame::rotate270()
938 // Allocate new frame
939 int new_w = h, new_h = w, new_bytes_per_line = bytes_per_pixel * new_w;
940 unsigned char *new_data = new unsigned char[calculate_data_size(new_w, new_h, new_bytes_per_line, color_model)];
941 unsigned char **new_rows = new unsigned char*[new_h];
942 for(int i = 0; i < new_h; i++)
943 new_rows[i] = &new_data[new_bytes_per_line * i];
946 for(int in_y = 0, out_x = 0; in_y < h; in_y++, out_x++)
948 for(int in_x = 0, out_y = new_h - 1; in_x < w; in_x++, out_y--)
950 for(int k = 0; k < bytes_per_pixel; k++)
952 new_rows[out_y][out_x * bytes_per_pixel + k] =
953 rows[in_y][in_x * bytes_per_pixel + k];
962 bytes_per_line = new_bytes_per_line;
967 void VFrame::flip_vert()
969 unsigned char *temp = new unsigned char[bytes_per_line];
970 for(int i = 0, j = h - 1; i < j; i++, j--)
972 memcpy(temp, rows[j], bytes_per_line);
973 memcpy(rows[j], rows[i], bytes_per_line);
974 memcpy(rows[i], temp, bytes_per_line);
979 void VFrame::flip_horiz()
981 unsigned char temp[32];
982 for(int i = 0; i < h; i++)
984 unsigned char *row = rows[i];
985 for(int j = 0; j < bytes_per_line / 2; j += bytes_per_pixel)
987 memcpy(temp, row + j, bytes_per_pixel);
988 memcpy(row + j, row + bytes_per_line - j - bytes_per_pixel, bytes_per_pixel);
989 memcpy(row + bytes_per_line - j - bytes_per_pixel, temp, bytes_per_pixel);
996 int VFrame::copy_from(VFrame *frame)
998 int w = MIN(this->w, frame->get_w());
999 int h = MIN(this->h, frame->get_h());
1000 timestamp = frame->timestamp;
1002 switch(frame->color_model)
1005 allocate_compressed_data(frame->compressed_size);
1006 memcpy(data, frame->data, frame->compressed_size);
1007 this->compressed_size = frame->compressed_size;
1011 memcpy(get_y(), frame->get_y(), w * h);
1012 memcpy(get_u(), frame->get_u(), w / 4 * h / 4);
1013 memcpy(get_v(), frame->get_v(), w / 4 * h / 4);
1018 //printf("%d %d %p %p %p %p %p %p\n", w, h, get_y(), get_u(), get_v(), frame->get_y(), frame->get_u(), frame->get_v());
1019 memcpy(get_y(), frame->get_y(), w * h);
1020 memcpy(get_u(), frame->get_u(), w * h / 4);
1021 memcpy(get_v(), frame->get_v(), w * h / 4);
1025 //printf("%d %d %p %p %p %p %p %p\n", w, h, get_y(), get_u(), get_v(), frame->get_y(), frame->get_u(), frame->get_v());
1026 memcpy(get_y(), frame->get_y(), w * h);
1027 memcpy(get_u(), frame->get_u(), w * h / 2);
1028 memcpy(get_v(), frame->get_v(), w * h / 2);
1032 //printf("%d %d %p %p %p %p %p %p\n", w, h, get_y(), get_u(), get_v(), frame->get_y(), frame->get_u(), frame->get_v());
1033 memcpy(get_y(), frame->get_y(), w * h);
1034 memcpy(get_u(), frame->get_u(), w * h);
1035 memcpy(get_v(), frame->get_v(), w * h);
1038 // printf("VFrame::copy_from %d\n", calculate_data_size(w,
1041 // frame->color_model));
1042 // Copy without extra 4 bytes in case the source is a hardware device
1043 memmove(data, frame->data, get_data_size());
1050 int VFrame::transfer_from(VFrame *that, int bg_color, int in_x, int in_y, int in_w, int in_h)
1052 if( this->get_color_model() == that->get_color_model() &&
1053 this->get_w() == that->get_w() && this->get_h() == that->get_h() )
1054 return this->copy_from(that);
1056 timestamp = that->timestamp;
1058 BC_CModels::transfer(
1059 this->get_rows(), that->get_rows(), // Packed data out/in
1060 this->get_y(), this->get_u(), this->get_v(), // Planar data out/in
1061 that->get_y(), that->get_u(), that->get_v(),
1062 0, 0, that->get_w(), that->get_h(), // Dimensions in/out
1063 0, 0, this->get_w(), this->get_h(),
1064 that->get_color_model(), this->get_color_model(), // Color models in/out
1065 bg_color, // alpha blend bg_color
1066 that->get_w(), this->get_w()); // rowspans (of luma for YUV)
1068 unsigned char *in_ptrs[4], *out_ptrs[4];
1069 unsigned char **inp, **outp;
1070 if( BC_CModels::is_planar(that->get_color_model()) ) {
1071 in_ptrs[0] = that->get_y();
1072 in_ptrs[1] = that->get_u();
1073 in_ptrs[2] = that->get_v();
1074 in_ptrs[3] = that->get_a();
1078 inp = that->get_rows();
1079 if( BC_CModels::is_planar(this->get_color_model()) ) {
1080 out_ptrs[0] = this->get_y();
1081 out_ptrs[1] = this->get_u();
1082 out_ptrs[2] = this->get_v();
1083 out_ptrs[3] = this->get_a();
1087 outp = this->get_rows();
1088 BC_CModels::transfer(outp, this->get_color_model(),
1089 0, 0, this->get_w(), this->get_h(), this->get_w(),
1090 inp, that->get_color_model(),
1091 in_x, in_y, in_w, in_h, that->get_w(),
1102 #define OVERLAY(type, max, components) \
1104 type **in_rows = (type**)src->get_rows(); \
1105 type **out_rows = (type**)get_rows(); \
1106 int in_w = src->get_w(); \
1107 int in_h = src->get_h(); \
1109 for(int i = 0; i < in_h; i++) \
1111 if(i + out_y1 >= 0 && i + out_y1 < h) \
1113 type *src_row = in_rows[i]; \
1114 type *dst_row = out_rows[i + out_y1] + out_x1 * components; \
1116 for(int j = 0; j < in_w; j++) \
1118 if(j + out_x1 >= 0 && j + out_x1 < w) \
1120 int opacity = src_row[3]; \
1121 int transparency = dst_row[3] * (max - src_row[3]) / max; \
1122 dst_row[0] = (transparency * dst_row[0] + opacity * src_row[0]) / max; \
1123 dst_row[1] = (transparency * dst_row[1] + opacity * src_row[1]) / max; \
1124 dst_row[2] = (transparency * dst_row[2] + opacity * src_row[2]) / max; \
1125 dst_row[3] = MAX(dst_row[3], src_row[3]); \
1128 dst_row += components; \
1129 src_row += components; \
1136 void VFrame::overlay(VFrame *src,
1140 switch(get_color_model())
1143 OVERLAY(unsigned char, 0xff, 4);
1150 int VFrame::get_scale_tables(int *column_table, int *row_table,
1151 int in_x1, int in_y1, int in_x2, int in_y2,
1152 int out_x1, int out_y1, int out_x2, int out_y2)
1155 float w_in = in_x2 - in_x1;
1156 float h_in = in_y2 - in_y1;
1157 int w_out = out_x2 - out_x1;
1158 int h_out = out_y2 - out_y1;
1160 float hscale = w_in / w_out;
1161 float vscale = h_in / h_out;
1163 for(i = 0; i < w_out; i++)
1165 column_table[i] = (int)(hscale * i);
1168 for(i = 0; i < h_out; i++)
1170 row_table[i] = (int)(vscale * i) + in_y1;
1175 void VFrame::push_prev_effect(const char *name)
1178 prev_effects.append(ptr = new char[strlen(name) + 1]);
1180 if(prev_effects.total > MAX_STACK_ELEMENTS) prev_effects.remove_object(0);
1183 void VFrame::pop_prev_effect()
1185 if(prev_effects.total)
1186 prev_effects.remove_object(prev_effects.last());
1189 void VFrame::push_next_effect(const char *name)
1192 next_effects.append(ptr = new char[strlen(name) + 1]);
1194 if(next_effects.total > MAX_STACK_ELEMENTS) next_effects.remove_object(0);
1197 void VFrame::pop_next_effect()
1199 if(next_effects.total)
1200 next_effects.remove_object(next_effects.last());
1203 const char* VFrame::get_next_effect(int number)
1205 if(!next_effects.total) return "";
1207 if(number > next_effects.total - 1) number = next_effects.total - 1;
1209 return next_effects.values[next_effects.total - number - 1];
1212 const char* VFrame::get_prev_effect(int number)
1214 if(!prev_effects.total) return "";
1216 if(number > prev_effects.total - 1) number = prev_effects.total - 1;
1218 return prev_effects.values[prev_effects.total - number - 1];
1221 BC_Hash* VFrame::get_params()
1226 void VFrame::clear_stacks()
1228 next_effects.remove_all_objects();
1229 prev_effects.remove_all_objects();
1234 void VFrame::copy_params(VFrame *src)
1236 status = src->status;
1237 params->copy_from(src->params);
1240 void VFrame::copy_stacks(VFrame *src)
1244 for(int i = 0; i < src->next_effects.total; i++)
1247 next_effects.append(ptr = new char[strlen(src->next_effects.values[i]) + 1]);
1248 strcpy(ptr, src->next_effects.values[i]);
1250 for(int i = 0; i < src->prev_effects.total; i++)
1253 prev_effects.append(ptr = new char[strlen(src->prev_effects.values[i]) + 1]);
1254 strcpy(ptr, src->prev_effects.values[i]);
1260 int VFrame::equal_stacks(VFrame *src)
1262 for(int i = 0; i < src->next_effects.total && i < next_effects.total; i++)
1264 if(strcmp(src->next_effects.values[i], next_effects.values[i])) return 0;
1267 for(int i = 0; i < src->prev_effects.total && i < prev_effects.total; i++)
1269 if(strcmp(src->prev_effects.values[i], prev_effects.values[i])) return 0;
1272 if(!params->equivalent(src->params)) return 0;
1276 void VFrame::dump_stacks()
1278 printf("VFrame::dump_stacks\n");
1279 printf(" next_effects:\n");
1280 for(int i = next_effects.total - 1; i >= 0; i--)
1281 printf(" %s\n", next_effects.values[i]);
1282 printf(" prev_effects:\n");
1283 for(int i = prev_effects.total - 1; i >= 0; i--)
1284 printf(" %s\n", prev_effects.values[i]);
1287 void VFrame::dump_params()
1294 printf("VFrame::dump %d this=%p\n", __LINE__, this);
1295 printf(" w=%d h=%d colormodel=%d rows=%p use_shm=%d shmid=%d\n",
1296 w, h, color_model, rows, use_shm, shmid);
1300 int VFrame::get_memory_usage()
1302 if(get_compressed_allocated()) return get_compressed_allocated();
1303 return get_h() * get_bytes_per_line();
1306 void VFrame::draw_pixel(int x, int y)
1308 if(!(x >= 0 && y >= 0 && x < get_w() && y < get_h())) return;
1310 #define DRAW_PIXEL(x, y, components, do_yuv, max, type) \
1312 type **rows = (type**)get_rows(); \
1313 rows[y][x * components] = max - rows[y][x * components]; \
1316 rows[y][x * components + 1] = max - rows[y][x * components + 1]; \
1317 rows[y][x * components + 2] = max - rows[y][x * components + 2]; \
1321 rows[y][x * components + 1] = (max / 2 + 1) - rows[y][x * components + 1]; \
1322 rows[y][x * components + 2] = (max / 2 + 1) - rows[y][x * components + 2]; \
1324 if(components == 4) \
1325 rows[y][x * components + 3] = max; \
1329 switch(get_color_model())
1332 DRAW_PIXEL(x, y, 3, 0, 0xff, unsigned char);
1335 DRAW_PIXEL(x, y, 4, 0, 0xff, unsigned char);
1338 DRAW_PIXEL(x, y, 3, 0, 1.0, float);
1341 DRAW_PIXEL(x, y, 4, 0, 1.0, float);
1344 DRAW_PIXEL(x, y, 3, 1, 0xff, unsigned char);
1347 DRAW_PIXEL(x, y, 4, 1, 0xff, unsigned char);
1350 DRAW_PIXEL(x, y, 3, 0, 0xffff, uint16_t);
1353 DRAW_PIXEL(x, y, 3, 1, 0xffff, uint16_t);
1355 case BC_RGBA16161616:
1356 DRAW_PIXEL(x, y, 4, 0, 0xffff, uint16_t);
1358 case BC_YUVA16161616:
1359 DRAW_PIXEL(x, y, 4, 1, 0xffff, uint16_t);
1365 void VFrame::draw_line(int x1, int y1, int x2, int y2)
1367 int w = labs(x2 - x1);
1368 int h = labs(y2 - y1);
1369 //printf("FindObjectMain::draw_line 1 %d %d %d %d\n", x1, y1, x2, y2);
1378 // Flip coordinates so x1 < x2
1388 int numerator = y2 - y1;
1389 int denominator = x2 - x1;
1390 for(int i = x1; i <= x2; i++)
1392 int y = y1 + (int64_t)(i - x1) * (int64_t)numerator / (int64_t)denominator;
1398 // Flip coordinates so y1 < y2
1408 int numerator = x2 - x1;
1409 int denominator = y2 - y1;
1410 for(int i = y1; i <= y2; i++)
1412 int x = x1 + (int64_t)(i - y1) * (int64_t)numerator / (int64_t)denominator;
1416 //printf("FindObjectMain::draw_line 2\n");
1419 void VFrame::draw_rect(int x1, int y1, int x2, int y2)
1421 draw_line(x1, y1, x2, y1);
1422 draw_line(x2, y1 + 1, x2, y2);
1423 draw_line(x2 - 1, y2, x1, y2);
1424 draw_line(x1, y2 - 1, x1, y1 + 1);
1427 #define ARROW_SIZE 10
1428 void VFrame::draw_arrow(int x1, int y1, int x2, int y2)
1430 double angle = atan((float)(y2 - y1) / (float)(x2 - x1));
1431 double angle1 = angle + (float)145 / 360 * 2 * 3.14159265;
1432 double angle2 = angle - (float)145 / 360 * 2 * 3.14159265;
1439 x3 = x2 - (int)(ARROW_SIZE * cos(angle1));
1440 y3 = y2 - (int)(ARROW_SIZE * sin(angle1));
1441 x4 = x2 - (int)(ARROW_SIZE * cos(angle2));
1442 y4 = y2 - (int)(ARROW_SIZE * sin(angle2));
1446 x3 = x2 + (int)(ARROW_SIZE * cos(angle1));
1447 y3 = y2 + (int)(ARROW_SIZE * sin(angle1));
1448 x4 = x2 + (int)(ARROW_SIZE * cos(angle2));
1449 y4 = y2 + (int)(ARROW_SIZE * sin(angle2));
1453 draw_line(x1, y1, x2, y2);
1454 // draw_line(x1, y1 + 1, x2, y2 + 1);
1457 if(abs(y2 - y1) || abs(x2 - x1)) draw_line(x2, y2, x3, y3);
1459 if(abs(y2 - y1) || abs(x2 - x1)) draw_line(x2, y2, x4, y4);