X-Git-Url: http://git.cinelerra-gg.org/git/?a=blobdiff_plain;f=cinelerra-5.1%2Fguicast%2Fvframe.C;h=706185824d441636d7570fe78174d162d06ed3cb;hb=9adc10ce6a9e5d28b7132552024b37f7ee9bb283;hp=a0a597441876a1e42e27fc2e8074fee2a10f4fe6;hpb=9f917bc27389ebc36568a1f465b42208f7e8e46a;p=goodguy%2Fhistory.git diff --git a/cinelerra-5.1/guicast/vframe.C b/cinelerra-5.1/guicast/vframe.C index a0a59744..70618582 100644 --- a/cinelerra-5.1/guicast/vframe.C +++ b/cinelerra-5.1/guicast/vframe.C @@ -24,7 +24,9 @@ #include #include #include +#include #include +#include #include "bcbitmap.h" #include "bchash.h" @@ -78,27 +80,53 @@ VFrameScene::~VFrameScene() //static BCCounter counter; - -VFramePng::VFramePng(unsigned char *png_data, double scale) +VFramePng::VFramePng(unsigned char *png_data, double s) { long image_size = ((long)png_data[0] << 24) | ((long)png_data[1] << 16) | ((long)png_data[2] << 8) | (long)png_data[3]; - if( !scale ) scale = BC_WindowBase::get_resources()->icon_scale; - read_png(png_data+4, image_size, scale, scale); + if( !s ) s = BC_WindowBase::get_resources()->icon_scale; + read_png(png_data+4, image_size, s, s); } -VFramePng::VFramePng(unsigned char *png_data, long image_size, double xscale, double yscale) +VFramePng::VFramePng(unsigned char *png_data, long image_size, double xs, double ys) { - if( !xscale ) xscale = BC_WindowBase::get_resources()->icon_scale; - if( !yscale ) yscale = BC_WindowBase::get_resources()->icon_scale; - read_png(png_data, image_size, xscale, yscale); + if( !xs ) xs = BC_WindowBase::get_resources()->icon_scale; + if( !ys ) ys = BC_WindowBase::get_resources()->icon_scale; + read_png(png_data, image_size, xs, ys); } VFramePng::~VFramePng() { } +VFrame *VFramePng::vframe_png(int fd, double xs, double ys) +{ + struct stat st; + if( fstat(fd, &st) ) return 0; + long len = st.st_size; + if( !len ) return 0; + int w = 0, h = 0; + unsigned char *bfr = (unsigned char *) + ::mmap (NULL, len, PROT_READ, MAP_SHARED, fd, 0); + if( bfr == MAP_FAILED ) return 0; + VFrame *vframe = new VFramePng(bfr, len, xs, ys); + if( (w=vframe->get_w()) <= 0 || (h=vframe->get_h()) <= 0 || + vframe->get_data() == 0 ) { delete vframe; vframe = 0; } + ::munmap(bfr, len); + return vframe; +} +VFrame *VFramePng::vframe_png(const char *png_path, double xs, double ys) +{ + VFrame *vframe = 0; + int fd = ::open(png_path, O_RDONLY); + if( fd >= 0 ) { + vframe = vframe_png(fd, xs, ys); + ::close(fd); + } + return vframe; +} + VFrame::VFrame(VFrame &frame) { @@ -249,6 +277,9 @@ int VFrame::reset_parameters(int do_opengl) sequence_number = -1; timestamp = -1.; is_keyframe = 0; + draw_point = 0; + set_pixel_color(BLACK); + stipple = 0; if(do_opengl) { @@ -482,6 +513,7 @@ int VFrame::allocate_data(unsigned char *data, int shmid, } else { // Have to use malloc for libpng +//printf("==vframe %d from %p\n", size, __builtin_return_address(0)); this->data = (unsigned char *)malloc(size); } @@ -1048,6 +1080,7 @@ int VFrame::copy_from(VFrame *frame) break; } + params->copy_from(frame->params); return 0; } @@ -1081,7 +1114,7 @@ int VFrame::transfer_from(VFrame *that, int bg_color, int in_x, int in_y, int in inp = in_ptrs; } else - inp = that->get_rows(); + inp = that->get_rows(); if( BC_CModels::is_planar(this->get_color_model()) ) { out_ptrs[0] = this->get_y(); out_ptrs[1] = this->get_u(); @@ -1090,7 +1123,7 @@ int VFrame::transfer_from(VFrame *that, int bg_color, int in_x, int in_y, int in outp = out_ptrs; } else - outp = this->get_rows(); + outp = this->get_rows(); BC_CModels::transfer(outp, this->get_color_model(), 0, 0, this->get_w(), this->get_h(), this->get_bytes_per_line(), @@ -1099,62 +1132,11 @@ int VFrame::transfer_from(VFrame *that, int bg_color, int in_x, int in_y, int in that->get_bytes_per_line(), bg_color); #endif + params->copy_from(that->params); return 0; } - - - - -#define OVERLAY(type, max, components) \ -{ \ - type **in_rows = (type**)src->get_rows(); \ - type **out_rows = (type**)get_rows(); \ - int in_w = src->get_w(); \ - int in_h = src->get_h(); \ - \ - for(int i = 0; i < in_h; i++) \ - { \ - if(i + out_y1 >= 0 && i + out_y1 < h) \ - { \ - type *src_row = in_rows[i]; \ - type *dst_row = out_rows[i + out_y1] + out_x1 * components; \ - \ - for(int j = 0; j < in_w; j++) \ - { \ - if(j + out_x1 >= 0 && j + out_x1 < w) \ - { \ - int opacity = src_row[3]; \ - int transparency = dst_row[3] * (max - src_row[3]) / max; \ - dst_row[0] = (transparency * dst_row[0] + opacity * src_row[0]) / max; \ - dst_row[1] = (transparency * dst_row[1] + opacity * src_row[1]) / max; \ - dst_row[2] = (transparency * dst_row[2] + opacity * src_row[2]) / max; \ - dst_row[3] = MAX(dst_row[3], src_row[3]); \ - } \ - \ - dst_row += components; \ - src_row += components; \ - } \ - } \ - } \ -} - - -void VFrame::overlay(VFrame *src, - int out_x1, - int out_y1) -{ - switch(get_color_model()) - { - case BC_RGBA8888: - OVERLAY(unsigned char, 0xff, 4); - break; - } -} - - - int VFrame::get_scale_tables(int *column_table, int *row_table, int in_x1, int in_y1, int in_x2, int in_y2, int out_x1, int out_y1, int out_x2, int out_y2) @@ -1311,117 +1293,351 @@ int VFrame::get_memory_usage() return get_h() * get_bytes_per_line(); } -void VFrame::draw_pixel(int x, int y) +void VFrame::set_pixel_color(int rgb) { - if(!(x >= 0 && y >= 0 && x < get_w() && y < get_h())) return; + pixel_rgb = rgb; + int ir = 0xff & (pixel_rgb >> 16); + int ig = 0xff & (pixel_rgb >> 8); + int ib = 0xff & (pixel_rgb >> 0); + bc_rgb2yuv(ir,ig,ib, ir,ig,ib); + pixel_yuv = (ir<<16) | (ig<<8) | (ib<<0); +} -#define DRAW_PIXEL(x, y, components, do_yuv, max, type) \ -{ \ - type **rows = (type**)get_rows(); \ - rows[y][x * components] = max - rows[y][x * components]; \ - if(!do_yuv) \ - { \ - rows[y][x * components + 1] = max - rows[y][x * components + 1]; \ - rows[y][x * components + 2] = max - rows[y][x * components + 2]; \ - } \ - else \ - { \ - rows[y][x * components + 1] = (max / 2 + 1) - rows[y][x * components + 1]; \ - rows[y][x * components + 2] = (max / 2 + 1) - rows[y][x * components + 2]; \ - } \ - if(components == 4) \ - rows[y][x * components + 3] = max; \ +void VFrame::set_stiple(int mask) +{ + stipple = mask; } +int VFrame::draw_pixel(int x, int y) +{ + if( x < 0 || y < 0 || x >= get_w() || y >= get_h() ) return 1; + if( draw_point ) return (this->*draw_point)(x, y); + +#define DRAW_PIXEL(type, r, g, b) { \ + type **rows = (type**)get_rows(); \ + rows[y][x * components + 0] = r; \ + rows[y][x * components + 1] = g; \ + rows[y][x * components + 2] = b; \ + if( components == 4 ) \ + rows[y][x * components + 3] = mx; \ +} + int components = BC_CModels::components(color_model); + int bch = BC_CModels::calculate_pixelsize(color_model) / components; + int sz = 8*bch, mx = BC_CModels::is_float(color_model) ? 1 : (1<> 16); float fr = 0; + int ig = 0xff & (pixel_color >> 8); float fg = 0; + int ib = 0xff & (pixel_color >> 0); float fb = 0; + if( (x+y) & stipple ) { + ir = 255 - ir; ig = 255 - ig; ib = 255 - ib; + } + if( BC_CModels::is_float(color_model) ) { + fr = ir / 255.; fg = ig / 255.; fb = ib / 255.; + mx = 1; + } + else if( (sz-=8) > 0 ) { + ir <<= sz; ig <<= sz; ib <<= sz; + } - switch(get_color_model()) - { - case BC_RGB888: - DRAW_PIXEL(x, y, 3, 0, 0xff, unsigned char); - break; - case BC_RGBA8888: - DRAW_PIXEL(x, y, 4, 0, 0xff, unsigned char); - break; - case BC_RGB_FLOAT: - DRAW_PIXEL(x, y, 3, 0, 1.0, float); - break; - case BC_RGBA_FLOAT: - DRAW_PIXEL(x, y, 4, 0, 1.0, float); - break; - case BC_YUV888: - DRAW_PIXEL(x, y, 3, 1, 0xff, unsigned char); - break; - case BC_YUVA8888: - DRAW_PIXEL(x, y, 4, 1, 0xff, unsigned char); - break; - case BC_RGB161616: - DRAW_PIXEL(x, y, 3, 0, 0xffff, uint16_t); - break; - case BC_YUV161616: - DRAW_PIXEL(x, y, 3, 1, 0xffff, uint16_t); - break; - case BC_RGBA16161616: - DRAW_PIXEL(x, y, 4, 0, 0xffff, uint16_t); - break; - case BC_YUVA16161616: - DRAW_PIXEL(x, y, 4, 1, 0xffff, uint16_t); - break; + switch(get_color_model()) { + case BC_RGB888: + case BC_YUV888: + case BC_RGBA8888: + case BC_YUVA8888: + DRAW_PIXEL(uint8_t, ir, ig, ib); + break; + case BC_RGB161616: + case BC_YUV161616: + case BC_RGBA16161616: + case BC_YUVA16161616: + DRAW_PIXEL(uint16_t, ir, ig, ib); + break; + case BC_RGB_FLOAT: + case BC_RGBA_FLOAT: + DRAW_PIXEL(float, fr, fg, fb); + break; } + return 0; } +// Bresenham's void VFrame::draw_line(int x1, int y1, int x2, int y2) { - int w = labs(x2 - x1); - int h = labs(y2 - y1); -//printf("FindObjectMain::draw_line 1 %d %d %d %d\n", x1, y1, x2, y2); + if( y1 > y2 ) { + int tx = x1; x1 = x2; x2 = tx; + int ty = y1; y1 = y2; y2 = ty; + } - if(!w && !h) - { - draw_pixel(x1, y1); + int x = x1, y = y1; + int dx = x2-x1, dy = y2-y1; + int dx2 = 2*dx, dy2 = 2*dy; + if( dx < 0 ) dx = -dx; + int r = dx > dy ? dx : dy, n = r; + int dir = 0; + if( dx2 < 0 ) dir += 1; + if( dy >= dx ) { + if( dx2 >= 0 ) do { /* +Y, +X */ + draw_pixel(x, y++); + if( (r -= dx2) < 0 ) { r += dy2; ++x; } + } while( --n >= 0 ); + else do { /* +Y, -X */ + draw_pixel(x, y++); + if( (r += dx2) < 0 ) { r += dy2; --x; } + } while( --n >= 0 ); } - else - if(w > h) - { -// Flip coordinates so x1 < x2 - if(x2 < x1) - { - y2 ^= y1; - y1 ^= y2; - y2 ^= y1; - x1 ^= x2; - x2 ^= x1; - x1 ^= x2; + else { + if( dx2 >= 0 ) do { /* +X, +Y */ + draw_pixel(x++, y); + if( (r -= dy2) < 0 ) { r += dx2; ++y; } + } while( --n >= 0 ); + else do { /* -X, +Y */ + draw_pixel(x--, y); + if( (r -= dy2) < 0 ) { r -= dx2; ++y; } + } while( --n >= 0 ); + } +} + +// g++ -dD -E - < /dev/null | grep DBL_EPSILON +#ifndef __DBL_EPSILON__ +#define __DBL_EPSILON__ ((double)2.22044604925031308085e-16L) +#endif +// weakest fraction * graphics integer range +#define RND_EPSILON (__DBL_EPSILON__*65536) + +class smooth_line { + int rnd(double v) { return round(v)+RND_EPSILON; } + VFrame *vframe; +public: + int sx, sy, ex, ey; /* current point, end point */ + int xs, ys; /* x/y quadrant sign -1/1 */ + int64_t A, B, C; /* quadratic coefficients */ + int64_t r, dx, dy; /* residual, dr/dx and dr/dy */ + int xmxx, xmxy; /* x,y at apex */ + int done; + + void init0(int x1,int y1, int x2,int y2, int x3,int y3, int top); + void init1(int x1,int y1, int x2,int y2, int x3,int y3); + int64_t rx() { return r + xs*8*dx + 4*A; } + void moveX(int64_t r) { + dx += xs*A; dy -= xs*B; + this->r = r; sx += xs; + } + int64_t ry() { return r + 8*dy + 4*C; } + void moveY(int64_t r) { + dx -= B; dy += C; + this->r = r; ++sy; + } + void draw(); + + smooth_line(VFrame *vframe) { this->vframe = vframe; this->done = 0; } +}; + + +void smooth_line::draw() +{ + if( done ) return; + if( abs(dy) >= abs(dx) ) { + if( xs*(sx-xmxx) >= 0 ) { + if( ys > 0 ) { done = 1; return; } + if( dy < 0 || ry() < 0 ) { moveY(ry()); goto xit; } + xmxx = ex; xmxy = ey; + ys = 1; xs = -xs; } - int numerator = y2 - y1; - int denominator = x2 - x1; - for(int i = x1; i <= x2; i++) - { - int y = y1 + (int64_t)(i - x1) * (int64_t)numerator / (int64_t)denominator; - draw_pixel(i, y); + moveX(rx()); + int64_t rr = ry(); + if( abs(rr) < abs(r) ) + moveY(rr); + } + else { + if( sy >= xmxy ) { + if( ys > 0 ) { done = 1; return; } + xmxx = ex; xmxy = ey; + ys = 1; xs = -xs; + } + moveY(ry()); + int64_t rr = rx(); + if( abs(rr) < abs(r) ) + moveX(rr); + } +xit: vframe->draw_pixel(sx, sy); +} + +void VFrame::draw_smooth(int x1, int y1, int x2, int y2, int x3, int y3) +{ + if( (x1 == x2 && y1 == y2) || (x2 == x3 && y2 == y3) ) + draw_line(x1,y1, x3,y3); + else if( x1 == x3 && y1 == y3 ) + draw_line(x1,y1, x2,y2); + else if( (x2-x1) * (y2-y3) == (x2-x3) * (y2-y1) ) { + // co-linear, draw line from min to max + if( x1 < x3 ) { + if( x2 < x1 ) { x1 = x2; y1 = y2; } + if( x2 > x3 ) { x3 = x2; y3 = y2; } + } + else { + if( x2 > x1 ) { x1 = x2; y1 = y2; } + if( x2 < x3 ) { x3 = x2; y3 = y2; } } + draw_line(x1,y1, x3,y3); } else - { -// Flip coordinates so y1 < y2 - if(y2 < y1) - { - y2 ^= y1; - y1 ^= y2; - y2 ^= y1; - x1 ^= x2; - x2 ^= x1; - x1 ^= x2; + smooth_draw(x1, y1, x2, y2, x3, y3); +} + +/* + Non-Parametric Smooth Curve Generation. Don Kelly 1984 + + P+-----+Q'= virtual + / / origin + / / + Q+-----+R + + Let the starting point be P. the ending point R. and the tangent vertex Q. + A general point Z on the curve is then + Z = (P + R - Q) + (Q - P) sin t + (Q - R) cos t + + Expanding the Cartesian coordinates around (P + R - Q) gives + [x y] = Z - (P + R - Q) + [a c] = Q - P + [b d] = Q - R + x = a*sin(t) + b*cos(t) + y = c*sin(t) + d*cos(t) + + from which t can now be eliminated via + c*x - a*y = (c*b - a*d)*cos(t) + d*x - b*y = (a*d - c*b)*sin(t) + + giving the Cartesian equation for the ellipse as + f(x, y) = (c*x - a*y)**2 + (d*x - b*y)**2 - (a*d - c*b)**2 = 0 + + or: f(x, y) = A*x**2 - 2*B*x*y + C*y**2 + B**2 - A*C = 0 + where: A = c**2 + d**2, B = a*c + b*d, C = a**2 + b**2 + + The maximum y extent of the ellipse may now be derived as follows: + let df/dx = 0, 2*A*x = 2*B*y, x = y*B/A + f(x, y) == B**2 * y**2 / A - 2*B**2 * y**2 / A + C*y**2 + B**2 - A*C = 0 + (A*C - B**2)*y = (A*C - B**2)*A + max x = sqrt(C), at y = B/sqrt(C) + max y = sqrt(A), at x = B/sqrt(A) + + */ + + +/* x1,y1 = P, x2,y2 = Q, x3,y3=R, + * draw from P to Q to R if top=0 + * or from P to (x,ymax) if top>0 + * or from Q to (x,ymax) if top<0 + */ +void smooth_line::init0(int x1,int y1, int x2,int y2, int x3,int y3, int top) +{ + int x0 = x1+x3-x2, y0 = y1+y3-y2; // Q' + + int a = x2-x1, c = y2-y1; + int b = x2-x3, d = y2-y3; + A = c*c + d*d; C = a*a + b*b; B = a*c + b*d; + + sx = top >= 0 ? x1 : x3; + sy = top >= 0 ? y1 : y3; + xs = x2 > sx || (x2==sx && (x1+x3-sx)>=x2) ? 1 : -1; + int64_t px = sx-x0, py = sy-y0; + dx = A*px - B*py; dy = C*py - B*px; + r = 0; + + if( top ) { + double ymy = sqrt(A), ymx = B/ymy; + ex = x0 + rnd(ymx); + ey = y0 + rnd(ymy); + } + else { + ex = x3; ey = y3; + } + + ys = a*b > 0 && (!top || top*xs*(b*c - a*d) > 0) ? -1 : 1; + if( ys < 0 ) { + double xmx = xs*sqrt(C), xmy = B/xmx; + xmxx = x0 + rnd(xmx); + xmxy = y0 + rnd(xmy); + } + else { + xmxx = ex; xmxy = ey; + } +} + +/* x1,y1 = P, x2,y2 = Q, x3,y3=R, + * draw from (x,ymax) to P + */ +void smooth_line::init1(int x1,int y1, int x2,int y2, int x3,int y3) +{ + int x0 = x1+x3-x2, y0 = y1+y3-y2; // Q' + + int a = x2-x1, c = y2-y1; + int b = x2-x3, d = y2-y3; + A = c*c + d*d; C = a*a + b*b; B = a*c + b*d; + + double ymy = -sqrt(A), ymx = B/ymy; + int64_t px = rnd(ymx), py = rnd(ymy); + sx = x0 + px; ex = x1; + sy = y0 + py; ey = y1; + xs = x2 > x1 || (x2==x1 && x3>=x2) ? 1 : -1; + dx = A*px - B*py; dy = C*py - B*px; + r = 4 * (A*px*px - 2*B*px*py + C*py*py + B*B - A*C); + + ys = a*b > 0 && xs*(b*c - a*d) < 0 ? -1 : 1; + if( ys < 0 ) { + double xmx = xs*sqrt(C), xmy = B/xmx; + xmxx = x0 + rnd(xmx); + xmxy = y0 + rnd(xmy); + } + else { + xs = -xs; + xmxx = ex; xmxy = ey; + } + if( xs > 0 ) + vframe->draw_pixel(sx, sy); + while( xs*(sx-xmxx) < 0 && (xs*dx < 0 || rx() < 0) ) { + moveX(rx()); + vframe->draw_pixel(sx, sy); + } +} + + +void VFrame::smooth_draw(int x1, int y1, int x2, int y2, int x3, int y3) +{ +//printf("p smooth_draw( %d,%d, %d,%d, %d,%d )\n", x1,y1,x2,y2,x3,y3); + if( y1 > y3 ) { // make y3 >= y1 + int xt = x1; x1 = x3; x3 = xt; + int yt = y1; y1 = y3; y3 = yt; + } + if( y1 > y2 && y3 > y2 ) { + smooth_line lt(this), rt(this); // Q on bottom + lt.init1(x1, y1, x2, y2, x3, y3); + rt.init1(x3, y3, x2, y2, x1, y1); + while( !lt.done || !rt.done ) { + lt.draw(); + rt.draw(); } - int numerator = x2 - x1; - int denominator = y2 - y1; - for(int i = y1; i <= y2; i++) - { - int x = x1 + (int64_t)(i - y1) * (int64_t)numerator / (int64_t)denominator; - draw_pixel(x, i); + } + else if( y1 < y2 && y3 < y2 ) { + smooth_line lt(this), rt(this); // Q on top + lt.init0(x1, y1, x2, y2, x3, y3, 1); + draw_pixel(lt.sx, lt.sy); + rt.init0(x1, y1, x2, y2, x3, y3, -1); + draw_pixel(rt.sx, rt.sy); + while( !lt.done || !rt.done ) { + lt.draw(); + rt.draw(); + } + } + else { + smooth_line pt(this); // Q in between + pt.init0(x1, y1, x2, y2, x3, y3, 0); + draw_pixel(pt.sx, pt.sy); + while( !pt.done ) { + pt.draw(); } } -//printf("FindObjectMain::draw_line 2\n"); } void VFrame::draw_rect(int x1, int y1, int x2, int y2) @@ -1432,30 +1648,16 @@ void VFrame::draw_rect(int x1, int y1, int x2, int y2) draw_line(x1, y2 - 1, x1, y1 + 1); } -#define ARROW_SIZE 10 -void VFrame::draw_arrow(int x1, int y1, int x2, int y2) +void VFrame::draw_arrow(int x1, int y1, int x2, int y2, int sz) { double angle = atan((float)(y2 - y1) / (float)(x2 - x1)); - double angle1 = angle + (float)145 / 360 * 2 * 3.14159265; - double angle2 = angle - (float)145 / 360 * 2 * 3.14159265; - int x3; - int y3; - int x4; - int y4; - if(x2 < x1) - { - x3 = x2 - (int)(ARROW_SIZE * cos(angle1)); - y3 = y2 - (int)(ARROW_SIZE * sin(angle1)); - x4 = x2 - (int)(ARROW_SIZE * cos(angle2)); - y4 = y2 - (int)(ARROW_SIZE * sin(angle2)); - } - else - { - x3 = x2 + (int)(ARROW_SIZE * cos(angle1)); - y3 = y2 + (int)(ARROW_SIZE * sin(angle1)); - x4 = x2 + (int)(ARROW_SIZE * cos(angle2)); - y4 = y2 + (int)(ARROW_SIZE * sin(angle2)); - } + double angle1 = angle + (float)145 / 360 * 2 * M_PI; + double angle2 = angle - (float)145 / 360 * 2 * M_PI; + int s = x2 < x1 ? -1 : 1; + int x3 = x2 + s * (int)(sz * cos(angle1)); + int y3 = y2 + s * (int)(sz * sin(angle1)); + int x4 = x2 + s * (int)(sz * cos(angle2)); + int y4 = y2 + s * (int)(sz * sin(angle2)); // Main vector draw_line(x1, y1, x2, y2); @@ -1467,6 +1669,15 @@ void VFrame::draw_arrow(int x1, int y1, int x2, int y2) if(abs(y2 - y1) || abs(x2 - x1)) draw_line(x2, y2, x4, y4); } - +void VFrame::draw_x(int x, int y, int sz) +{ + draw_line(x-sz,y-sz, x+sz,y+sz); + draw_line(x+sz,y-sz, x-sz,y+sz); +} +void VFrame::draw_t(int x, int y, int sz) +{ + draw_line(x,y-sz, x,y+sz); + draw_line(x+sz,y, x-sz,y); +}