8ec08327 |
if ((draw->desc->flags & PIX_FMT_RGB) && draw->nb_planes == 1 &&
ff_fill_rgba_map(rgba_map, draw->format) >= 0) {
for (i = 0; i < 4; i++)
color->comp[0].u8[rgba_map[i]] = rgba[i];
} else if (draw->nb_planes == 3 || draw->nb_planes == 4) {
/* assume YUV */
color->comp[0].u8[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
color->comp[1].u8[0] = RGB_TO_U_CCIR(rgba[0], rgba[1], rgba[2], 0);
color->comp[2].u8[0] = RGB_TO_V_CCIR(rgba[0], rgba[1], rgba[2], 0);
color->comp[3].u8[0] = rgba[3];
} else if (draw->format == PIX_FMT_GRAY8 || draw->format == PIX_FMT_GRAY8A) {
color->comp[0].u8[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
color->comp[1].u8[0] = rgba[3];
} else {
av_log(NULL, AV_LOG_WARNING,
"Color conversion not implemented for %s\n", draw->desc->name);
memset(color, 128, sizeof(*color));
}
}
static uint8_t *pointer_at(FFDrawContext *draw, uint8_t *data[], int linesize[],
int plane, int x, int y)
{
return data[plane] +
(y >> draw->vsub[plane]) * linesize[plane] +
(x >> draw->hsub[plane]) * draw->pixelstep[plane];
}
void ff_copy_rectangle2(FFDrawContext *draw,
uint8_t *dst[], int dst_linesize[],
uint8_t *src[], int src_linesize[],
int dst_x, int dst_y, int src_x, int src_y,
int w, int h)
{
int plane, y, wp, hp;
uint8_t *p, *q;
for (plane = 0; plane < draw->nb_planes; plane++) {
p = pointer_at(draw, src, src_linesize, plane, src_x, src_y);
q = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
wp = (w >> draw->hsub[plane]) * draw->pixelstep[plane];
hp = (h >> draw->vsub[plane]);
for (y = 0; y < hp; y++) {
memcpy(q, p, wp);
p += src_linesize[plane];
q += dst_linesize[plane];
}
}
}
void ff_fill_rectangle(FFDrawContext *draw, FFDrawColor *color,
uint8_t *dst[], int dst_linesize[],
int dst_x, int dst_y, int w, int h)
{
int plane, x, y, wp, hp;
uint8_t *p0, *p;
for (plane = 0; plane < draw->nb_planes; plane++) {
p0 = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
wp = (w >> draw->hsub[plane]);
hp = (h >> draw->vsub[plane]);
if (!hp)
return;
p = p0;
/* copy first line from color */
for (x = 0; x < wp; x++) {
memcpy(p, color->comp[plane].u8, draw->pixelstep[plane]);
p += draw->pixelstep[plane];
}
wp *= draw->pixelstep[plane];
/* copy next lines from first line */
p = p0 + dst_linesize[plane];
for (y = 1; y < hp; y++) {
memcpy(p, p0, wp);
p += dst_linesize[plane];
}
}
}
|
e8497a33 |
/**
* Clip interval [x; x+w[ within [0; wmax[.
* The resulting w may be negative if the final interval is empty.
* dx, if not null, return the difference between in and out value of x.
*/
static void clip_interval(int wmax, int *x, int *w, int *dx)
{
if (dx)
*dx = 0;
if (*x < 0) {
if (dx)
*dx = -*x;
*w += *x;
*x = 0;
}
if (*x + *w > wmax)
*w = wmax - *x;
}
/**
* Decompose w pixels starting at x
* into start + (w starting at x) + end
* with x and w aligned on multiples of 1<<sub.
*/
static void subsampling_bounds(int sub, int *x, int *w, int *start, int *end)
{
int mask = (1 << sub) - 1;
*start = (-*x) & mask;
*x += *start;
*start = FFMIN(*start, *w);
*w -= *start;
*end = *w & mask;
*w >>= sub;
}
static int component_used(FFDrawContext *draw, int plane, int comp)
{
return (draw->comp_mask[plane] >> comp) & 1;
}
/* If alpha is in the [ 0 ; 0x1010101 ] range,
then alpha * value is in the [ 0 ; 0xFFFFFFFF ] range,
and >> 24 gives a correct rounding. */
static void blend_line(uint8_t *dst, unsigned src, unsigned alpha,
int dx, int w, unsigned hsub, int left, int right)
{
unsigned asrc = alpha * src;
unsigned tau = 0x1010101 - alpha;
int x;
src *= alpha;
if (left) {
unsigned suba = (left * alpha) >> hsub;
*dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
dst += dx;
}
for (x = 0; x < w; x++) {
*dst = (*dst * tau + asrc) >> 24;
dst += dx;
}
if (right) {
unsigned suba = (right * alpha) >> hsub;
*dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
}
}
void ff_blend_rectangle(FFDrawContext *draw, FFDrawColor *color,
uint8_t *dst[], int dst_linesize[],
int dst_w, int dst_h,
int x0, int y0, int w, int h)
{
unsigned alpha, nb_planes, nb_comp, plane, comp;
int w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
uint8_t *p0, *p;
/* TODO optimize if alpha = 0xFF */
clip_interval(dst_w, &x0, &w, NULL);
clip_interval(dst_h, &y0, &h, NULL);
if (w <= 0 || h <= 0 || !color->rgba[3])
return;
/* 0x10203 * alpha + 2 is in the [ 2 ; 0x1010101 - 2 ] range */
alpha = 0x10203 * color->rgba[3] + 0x2;
nb_planes = (draw->nb_planes - 1) | 1; /* eliminate alpha */
for (plane = 0; plane < nb_planes; plane++) {
nb_comp = draw->pixelstep[plane];
p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
w_sub = w;
h_sub = h;
x_sub = x0;
y_sub = y0;
subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
for (comp = 0; comp < nb_comp; comp++) {
if (!component_used(draw, plane, comp))
continue;
p = p0 + comp;
if (top) {
blend_line(p, color->comp[plane].u8[comp], alpha >> 1,
draw->pixelstep[plane], w_sub,
draw->hsub[plane], left, right);
p += dst_linesize[plane];
}
for (y = 0; y < h_sub; y++) {
blend_line(p, color->comp[plane].u8[comp], alpha,
draw->pixelstep[plane], w_sub,
draw->hsub[plane], left, right);
p += dst_linesize[plane];
}
if (bottom)
blend_line(p, color->comp[plane].u8[comp], alpha >> 1,
draw->pixelstep[plane], w_sub,
draw->hsub[plane], left, right);
}
}
}
static void blend_pixel(uint8_t *dst, unsigned src, unsigned alpha,
uint8_t *mask, int mask_linesize, int l2depth,
unsigned w, unsigned h, unsigned shift, unsigned xm0)
{
unsigned xm, x, y, t = 0;
unsigned xmshf = 3 - l2depth;
unsigned xmmod = 7 >> l2depth;
unsigned mbits = (1 << (1 << l2depth)) - 1;
unsigned mmult = 255 / mbits;
for (y = 0; y < h; y++) {
xm = xm0;
for (x = 0; x < w; x++) {
t += ((mask[xm >> xmshf] >> ((~xm & xmmod) << l2depth)) & mbits)
* mmult;
xm++;
}
mask += mask_linesize;
}
alpha = (t >> shift) * alpha;
*dst = ((0x1010101 - alpha) * *dst + alpha * src) >> 24;
}
static void blend_line_hv(uint8_t *dst, int dst_delta,
unsigned src, unsigned alpha,
uint8_t *mask, int mask_linesize, int l2depth, int w,
unsigned hsub, unsigned vsub,
int xm, int left, int right, int hband)
{
int x;
if (left) {
blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
left, hband, hsub + vsub, xm);
dst += dst_delta;
xm += left;
}
for (x = 0; x < w; x++) {
blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
1 << hsub, hband, hsub + vsub, xm);
dst += dst_delta;
xm += 1 << hsub;
}
if (right)
blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
right, hband, hsub + vsub, xm);
}
void ff_blend_mask(FFDrawContext *draw, FFDrawColor *color,
uint8_t *dst[], int dst_linesize[], int dst_w, int dst_h,
uint8_t *mask, int mask_linesize, int mask_w, int mask_h,
int l2depth, unsigned endianness, int x0, int y0)
{
unsigned alpha, nb_planes, nb_comp, plane, comp;
int xm0, ym0, w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
uint8_t *p0, *p, *m;
clip_interval(dst_w, &x0, &mask_w, &xm0);
clip_interval(dst_h, &y0, &mask_h, &ym0);
mask += ym0 * mask_linesize;
if (mask_w <= 0 || mask_h <= 0 || !color->rgba[3])
return;
/* alpha is in the [ 0 ; 0x10203 ] range,
alpha * mask is in the [ 0 ; 0x1010101 - 4 ] range */
alpha = (0x10307 * color->rgba[3] + 0x3) >> 8;
nb_planes = (draw->nb_planes - 1) | 1; /* eliminate alpha */
for (plane = 0; plane < nb_planes; plane++) {
nb_comp = draw->pixelstep[plane];
p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
w_sub = mask_w;
h_sub = mask_h;
x_sub = x0;
y_sub = y0;
subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
for (comp = 0; comp < nb_comp; comp++) {
if (!component_used(draw, plane, comp))
continue;
p = p0 + comp;
m = mask;
if (top) {
blend_line_hv(p, draw->pixelstep[plane],
color->comp[plane].u8[comp], alpha,
m, mask_linesize, l2depth, w_sub,
draw->hsub[plane], draw->vsub[plane],
xm0, left, right, top);
p += dst_linesize[plane];
m += top * mask_linesize;
}
for (y = 0; y < h_sub; y++) {
blend_line_hv(p, draw->pixelstep[plane],
color->comp[plane].u8[comp], alpha,
m, mask_linesize, l2depth, w_sub,
draw->hsub[plane], draw->vsub[plane],
xm0, left, right, 1 << draw->vsub[plane]);
p += dst_linesize[plane];
m += mask_linesize << draw->vsub[plane];
}
if (bottom)
blend_line_hv(p, draw->pixelstep[plane],
color->comp[plane].u8[comp], alpha,
m, mask_linesize, l2depth, w_sub,
draw->hsub[plane], draw->vsub[plane],
xm0, left, right, bottom);
}
}
}
|