robot36/utils/blur.c

52 lines
1.5 KiB
C
Raw Normal View History

/*
Copyright 2014 Ahmet Inan <xdsopl@googlemail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <math.h>
#include <stdio.h>
double gauss(double x, double radius)
{
double sigma = radius / 3.0;
return radius ? exp(- x * x / (2.0 * sigma * sigma)) / sqrt(2.0 * M_PI * sigma * sigma) : 1.0;
}
void emit(int radius)
{
printf("\t\tif (i < %d || (buffer_length - %d) <= i)\n\t\t\treturn 0;\n", radius, radius);
int sum = 0;
for (int i = -radius; i <= radius; ++i)
sum += 16384 * gauss(i, radius);
int factor = (16384 * 16384) / (sum + 1);
for (int i = -radius; i <= radius; ++i)
printf("\t\t%s%d * value_buffer[i%s%d]%s\n",
i != -radius ? "\t" : "return (",
(int)(factor * gauss(i, radius)),
i < 0 ? "" : "+",
i,
i != radius ? " +" : ") >> 14;"
);
}
int main()
{
printf("/* code generated by 'utils/blur.c' */\n");
printf("static uchar value_blur(int i)\n{\n\tswitch (blur_power) {\n");
for (int i = 0; i < 7; ++i) {
printf("\tcase %d:\n", i);
emit((1 << i) | 1);
}
printf("\tdefault:\n\t\treturn value_buffer[i];\n\t}\n\treturn 0;\n}\n");
return 0;
}