1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
/*
* Dynamic Noise Reduction (based on the VirtualDub filter by Steven Don)
*
* Copyright (c) 2005 Peter Schlaile
*
* 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.
*
*/
#include "math.h"
#include "plugin.h"
#include <stdio.h>
char name[]= "Dynamic Noise Reduction";
VarStruct varstr[]= {
{ NUMSLI|INT, "Level:", 10.0, 0.0, 15.0, "Level"},
};
typedef struct Cast {
int level;
} Cast;
float cfra;
void * plugin_private_data;
struct my_data {
unsigned char lookup_table[65536];
int last_level;
float last_cfra;
int last_width;
int last_height;
unsigned char * last_frame;
};
void plugin_seq_doit(Cast *, float, float, int, int,
ImBuf *, ImBuf *, ImBuf *, ImBuf *);
int plugin_seq_getversion(void) { return B_PLUGIN_VERSION;}
static void precalculate(unsigned char * table, int level)
{
int ap_, bp;
for (ap_ = 0; ap_ < 256; ap_++) {
for (bp = 0; bp < 256; bp++) {
int ap = ap_;
int diff = ap - bp;
if (diff < 0) {
diff = -diff;
}
if (diff < level) {
if (diff > (level >> 1)) {
ap = (ap + ap + bp)/3;
} else {
ap = bp;
}
}
*table++ = ap;
}
}
}
void plugin_but_changed(int but) { }
void plugin_init() { }
void * plugin_seq_alloc_private_data()
{
struct my_data * result = (struct my_data*) calloc(
sizeof(struct my_data), 1);
result->last_cfra = -1;
return result;
}
void plugin_seq_free_private_data(void * data)
{
struct my_data * d = (struct my_data*) data;
if (d->last_frame) {
free(d->last_frame);
}
free(d);
}
void plugin_getinfo(PluginInfo *info) {
info->name= name;
info->nvars= sizeof(varstr)/sizeof(VarStruct);
info->cfra= &cfra;
info->varstr= varstr;
info->init= plugin_init;
info->seq_doit= (SeqDoit) plugin_seq_doit;
info->callback= plugin_but_changed;
}
static void doit(unsigned char * src_, unsigned char * dst_,
unsigned char * table, int width, int height)
{
int count = width * height;
unsigned char * src = src_;
unsigned char * dst = dst_;
while (count--) {
*dst++ = table[(*src++ << 8) | *dst];
*dst++ = table[(*src++ << 8) | *dst];
*dst++ = table[(*src++ << 8) | *dst];
*dst++ = *src++;
}
memcpy(src_, dst_, width * height * 4);
}
void plugin_seq_doit(Cast *cast, float facf0, float facf1, int width,
int height, ImBuf *ibuf1, ImBuf *ibuf2, ImBuf *out, ImBuf *use) {
struct my_data * d = (struct my_data*) plugin_private_data;
if (!ibuf1) return;
if (cast->level != d->last_level) {
precalculate(d->lookup_table, cast->level);
d->last_level = cast->level;
}
if (width != d->last_width || height != d->last_height
|| cfra != d->last_cfra + 1) {
free(d->last_frame);
d->last_frame = (unsigned char*) calloc(width * height, 4);
d->last_width = width;
d->last_height = height;
}
memcpy(out->rect, ibuf1->rect, width * height * 4);
doit((unsigned char*) out->rect,
d->last_frame, d->lookup_table, width, height);
d->last_cfra = cfra;
}
|