www.pudn.com > hbp0.1.zip > classfilter.cpp
/***************************************************************************
classfilter.cpp
Used to implement gaussian and other types of low level filter
begin : Sat Nov 23 2002
copyright : (C) 2002 by Bob Mottram
email : fuzzgun@btinternet.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. *
* *
***************************************************************************/
#include
#include
#include "classimage.h"
#include "classfilter.h"
//image filter types
const int FILTER_GAUSSIAN = 1;
const int FILTER_VERTICAL = 2;
const int FILTER_HORIZONTAL = 3;
///
/// Constructor
///
classfilter::classfilter()
{
filtertype=-1;
filterradius=2;
initialised=false;
}
///
/// Destructor
///
classfilter::~classfilter()
{
}
///
/// Initialise
///
void classfilter::init()
{
createImage(width,height);
initialised=true;
}
///
/// Update the filter
///
/// bitmap image
/// width of the image
/// height of the image
/// The type of filter to be applied
void classfilter::update(unsigned char ***img, int wdth, int hght, int filter_type)
{
width = wdth;
height = hght;
if (filterradius>9) filterradius=9;
if (initialised==false) init();
applyFilter(img,filter_type,filterradius);
}
///
/// Update the filter
///
/// image object
void classfilter::update(classimage *img)
{
if (img->width>0)
{
width = img->width;
height = img->height;
if (initialised==false) init();
applyFilter(img->image,1,filterradius);
}
}
///
/// Applies a filter to the given image with the given radius
///
/// bitmap image
/// The type of filter to be applied
/// The radius in pixels within which to apply the filter
void classfilter::applyFilter(unsigned char ***img, int Filter_Type, int Filter_Radius)
{
int x1,y1;
int fx,fy;
int ax,ay;
int c,w;
int tx,ty,bx,by;
float p1, pixels;
long pixval[3];
unsigned char img_p;
if (Filter_Radius < 1) Filter_Radius = 1;
if (filtertype!=Filter_Type)
{
switch (Filter_Type)
{
case FILTER_GAUSSIAN: { createFilter_Gaussian(Filter_Radius*2); break; }
case FILTER_VERTICAL: { createFilter_Vertical(Filter_Radius*2); break; }
case FILTER_HORIZONTAL: { createFilter_Horizontal(Filter_Radius*2); break; }
}
}
pixels = (float)(4 * Filter_Radius * Filter_Radius * 128);
tx = Filter_Radius;
ty = Filter_Radius;
bx = width - Filter_Radius - 1;
by = height - Filter_Radius - 1;
y1=0;
for (x1=0;x1=ty) && (y1=tx) && (x1
/// Returns a gaussian normal distribution
///
/// distance from the centre
/// Radius of the filter in pixels
float classfilter::function_Gaussian(float Distance, float radius)
{
return((float)exp(-Distance / radius));
}
///
/// Creates a gaussian filter
/// Pre-calculating the filter in this way prevents having to do a lot of floating point
/// calculations later on
///
/// width of the filter in pixels
void classfilter::createFilter_Gaussian(int filter_width)
{
int x,y,cx,dx,dy;
float Dist,weight,widthSqr;
cx = filter_width/2;
widthSqr = (float)(filter_width * filter_width);
for (x=0;x<10;x++)
{
dx = x - cx;
for (y=0;y<10;y++)
{
if ((x
/// Creates a filter to detect vertical lines
///
/// width of the filter in pixels
void classfilter::createFilter_Vertical(int filter_width)
{
int x,y,cx,dx;
unsigned char weight;
cx = filter_width/2;
for (x=0;x<10;x++)
{
dx = abs(x - cx);
for (y=0;y<10;y++)
{
if ((x
/// Creates a filter to detect horizontal lines
///
/// width of the filter in pixels
void classfilter::createFilter_Horizontal(int filter_width)
{
int x,y,cx,dy;
unsigned char weight;
cx = width/2;
for (x=0;x<10;x++)
{
for (y=0;y<10;y++)
{
if ((x