Files
veejay/veejay-current/veejay-server/libvje/effects/dices.c
2019-06-15 14:55:39 +02:00

248 lines
6.2 KiB
C

/*
* Linux VeeJay
*
* Copyright(C)2002-2015 Niels Elburg <nwelburg@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License , or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307 , USA.
*
* This Effect was ported from:
* EffecTV - Realtime Digital Video Effector
* Copyright (C) 2001-2002 FUKUCHI Kentaro
*
* dice.c: a 'dicing' effect
* copyright (c) 2001 Sam Mertens. This code is subject to the provisions of
* the GNU Public License.
*
*/
#include "common.h"
#include <veejaycore/vjmem.h>
#include "dices.h"
static int g_map_width = 0;
static int g_map_height = 0;
static int g_cube_size = 0;
static int g_cube_bits = 0;
static uint8_t *g_dicemap=NULL;
static uint8_t g_orientation=0;
#define VJ_IMAGE_EFFECT_DICES_ORIENTATION_DEFAULT 4 //random
static void dice_create_map(int w, int h);
static void dice_create_map_orientation(int w, int h, int orientation);
typedef enum _dice_dir
{
Up = 0,
Right = 1,
Down = 2,
Left = 3,
Random = 4,
} DiceDir;
vj_effect *dices_init(int width, int height)
{
vj_effect *ve = (vj_effect *) vj_calloc(sizeof(vj_effect));
ve->num_params = 2;
ve->defaults = (int *) vj_calloc(sizeof(int) * ve->num_params); /* default values */
ve->limits[0] = (int *) vj_calloc(sizeof(int) * ve->num_params); /* min */
ve->limits[1] = (int *) vj_calloc(sizeof(int) * ve->num_params); /* max */
ve->defaults[0] = 4;
ve->defaults[1] = VJ_IMAGE_EFFECT_DICES_ORIENTATION_DEFAULT;
ve->limits[0][0] = 0;
ve->limits[1][0] = 32;
ve->limits[0][1] = 0;
ve->limits[1][1] = 4; //dices orientation : up, right , down, left, random
ve->description = "Dices (EffectTV)";
ve->sub_format = 1;
ve->extra_frame = 0;
ve->has_user = 0;
ve->param_description = vje_build_param_list( ve->num_params, "Dice size", "Orientation" );
ve->hints = vje_init_value_hint_list( ve->num_params );
vje_build_value_hint_list( ve->hints, ve->limits[1][1], 1,
"Up" ,"Right" ,"Down" ,"Left" ,"Random");
/* find parameter limit */
int near = (width < height ? width: height);
int next = pow(2, floor(log2(near)));
int limit = 1;
int iter = 1;
while(limit < next)
{
limit = limit * 2;
iter ++;
}
ve->limits[1][0] = iter - 1;
return ve;
}
int dices_malloc(int width, int height)
{
g_dicemap = (uint8_t *) vj_malloc(sizeof(uint8_t) * width * height);
if(!g_dicemap) return 0;
dice_create_map(width, height);
return 1;
}
void dices_free()
{
if(g_dicemap)
free(g_dicemap);
g_dicemap = NULL;
}
static void dice_create_map_orientation(int w, int h, int orientation)
{
int k,x, y, i = 0;
int maplen = (w * h);
g_map_height = h >> g_cube_bits;
g_map_width = w >> g_cube_bits;
g_cube_size = 1 << g_cube_bits;
maplen = maplen / (g_map_height * g_map_width);
for( k = 0; k < maplen;k++)
{
for (y = 0; y < g_map_height; y++)
{
for (x = 0; x < g_map_width; x++)
{
g_dicemap[i] = orientation;
i++;
}
}
}
}
static void dice_create_map(int w, int h)
{
int k,x, y, i = 0;
int maplen = (w * h);
g_map_height = h >> g_cube_bits;
g_map_width = w >> g_cube_bits;
g_cube_size = 1 << g_cube_bits;
maplen = maplen / (g_map_height * g_map_width);
for( k = 0; k < maplen;k++)
{
for (y = 0; y < g_map_height; y++)
{
for (x = 0; x < g_map_width; x++)
{
g_dicemap[i] = ((1 + (rand() * (i + y))) & 0x03);
i++;
}
}
}
}
void dices_apply( void *data, VJFrame *frame, int cube_bits, int orientation)
{
int i = 0, map_x, map_y, map_i = 0, base, dx, dy, di=0;
const unsigned int width = frame->width;
const unsigned int height = frame->height;
uint8_t *Y = frame->data[0];
uint8_t *Cb = frame->data[1];
uint8_t *Cr = frame->data[2];
if ((cube_bits != g_cube_bits) || (orientation != g_orientation))
{
g_cube_bits = cube_bits;
g_orientation = orientation;
if( orientation == VJ_IMAGE_EFFECT_DICES_ORIENTATION_DEFAULT)
dice_create_map(width,height);
else
dice_create_map_orientation(width, height, orientation);
}
//TODO dices map centering
// when dices size * dice width < frame width ,dice is shifted to be centered.
unsigned int shift_w = (width - (g_cube_size*g_map_width)) >> 1;
unsigned int shift_h = (height - (g_cube_size*g_map_height)) >> 1;
for (map_y = 0; map_y < g_map_height; map_y++)
{
for (map_x = 0; map_x < g_map_width; map_x++)
{
base = (shift_h + (map_y << g_cube_bits)) * width + (map_x << g_cube_bits) + shift_w;
switch (g_dicemap[map_i])
{
case Left:
for (dy = 0; dy < g_cube_size; dy++)
{
i = base + dy * width;
for (dx = 0; dx < g_cube_size; dx++)
{
di = base + (dx * width) + (g_cube_size - dy - 1);
Y[di] = Y[i];
Cb[di] = Cb[i];
Cr[di] = Cr[i];
i++;
}
}
break;
case Down:
for (dy = 0; dy < g_cube_size; dy++)
{
di = base + dy * width;
i = base + (g_cube_size - dy - 1) * width + g_cube_size;
for (dx = 0; dx < g_cube_size; dx++)
{
i--;
Y[di] = Y[i];
Cb[di] = Cb[i];
Cr[di] = Cr[i];
di++;
}
}
break;
case Right:
for (dy = 0; dy < g_cube_size; dy++)
{
i = base + (dy * width);
for (dx = 0; dx < g_cube_size; dx++)
{
di = base + dy + (g_cube_size - dx - 1) * width;
Y[di] = Y[i];
Cb[di] = Cb[i];
Cr[di] = Cr[i];
i++;
}
}
break;
case Up:
for( dy = 0; dy < g_cube_size ; dy ++ )
{
di = base + (g_cube_size - dy - 1) * width + g_cube_size;
i = base + dy * width;
for( dx = 0; dx < g_cube_size ; dx ++ )
{
di --;
Y[di] = Y[i];
Cb[di] = Cb[i];
Cr[di] = Cr[i];
i ++;
}
}
break;
}
map_i++;
}
}
}