Converted filters to using an actual factory class.

This is more readable, and more traditional.
This commit is contained in:
Arthur Moore 2015-03-14 07:13:56 -04:00
parent dc63304c69
commit 1adad0b01e
6 changed files with 66 additions and 98 deletions

View File

@ -22,9 +22,17 @@ public:
unsigned int getWidth() {return width;}
unsigned int getHeight() {return height;}
virtual std::string getName() {return "Dummy Filter";}
virtual int getScale() {return 1;}
virtual unsigned int getScale() {return 1;}
virtual bool exists() {return false;}
///Take data from srcPtr, and return the new data via dstPtr
/**
* Run the filter.
*
* All of the filters currently in use are designed to work with 32 bits (4 bytes) per pixel.
* Of important note is that the output of the filter is scaled. This means the output array must be scale * scale larger than the input array
*
* \param[in] srcPtr A pointer to the input 32 bit RGBA Pixel Array
* \param[in] dstPtr A pointer to the output 32 bit RGBA Pixel Array
*/
virtual void run(u32 *srcPtr,u32 *dstPtr)
{
//If the filter doesn't exist, then we still need to get the data to the output buffer

View File

@ -39,7 +39,7 @@ const std::map<std::string,FilterFunc> makeFilterMap()
return tempMap;
}
const std::map<std::string,FilterFunc> filters::filterMap = makeFilterMap();
const std::map<std::string,FilterFunc> filter_factory::filterMap = makeFilterMap();
//Convert a 32 bit image to a 24 bit one
void convert32To24(u32* src,u8* dst,unsigned int width, unsigned int height)

View File

@ -15,25 +15,64 @@
// Function pointer type for a filter function
typedef void(*FilterFunc)(u8*, u32, u8*, u32, int, int);
///This is the parent class of all the filters
///TODO: Actually subclass these instead of cheating
class raw_filter : public filter_base
{
private:
//No default constructor for this class
raw_filter();
///The filter's name
std::string name;
///The internal filter used
FilterFunc myFilter;
///The internal scale
unsigned int myScale;
//Don't need to calculate these every time (based off width)
///The number of pixels per horizontal row
unsigned int horiz_bytes;
///The number of pixels per output horizontal row
unsigned int horiz_bytes_out;
public:
raw_filter(std::string _name,FilterFunc _myFilter,unsigned int _scale,unsigned int _width,unsigned int _height):
filter_base(_width,_height),
name(_name), myFilter(_myFilter), myScale(_scale),
horiz_bytes(_width * 4), horiz_bytes_out(_width * 4 * _scale)
{
// std::cerr << name << std::endl;
}
std::string getName() {return name;}
unsigned int getScale() {return myScale;}
bool exists() {return true;}
///Run the filter pointed to by the internal FilterFunc
void run(u32 *srcPtr, u32 *dstPtr)
{
if(myFilter==NULL)
{
throw std::runtime_error("ERROR: Filter not properly initialized!!!");
}
myFilter(reinterpret_cast<u8 *>(srcPtr),horiz_bytes,reinterpret_cast<u8 *>(dstPtr),horiz_bytes_out,getWidth(),getHeight());
}
};
typedef std::pair<std::string,FilterFunc> namedfilter;
///A class allowing for easy access to all the filters
class filters {
class filter_factory
{
private:
//A named map of all the filters
//A named map of all the (original) filters
static const std::map<std::string,FilterFunc> filterMap;
public:
///Returns a function pointer to a 32 bit filter
static FilterFunc GetFilter(std::string filterName)
static filter_base * createFilter(std::string filterName,unsigned int width,unsigned int height)
{
std::map<std::string,FilterFunc>::const_iterator found = filterMap.find(filterName);
if(found == filterMap.end()){
//Not doing the error checking here
// throw std::runtime_error("ERROR: Filter not found!");
return NULL;
//If we found the filter:
if(found != filterMap.end()){
return new raw_filter(filterName,found->second,GetFilterScale(filterName),width,height);
}
return found->second;
};
//If nothing found, just return a default filter
return new filter_base(width,height);
}
///Returns the filter's scaling factor
///TODO: De hardcode this
static int GetFilterScale(std::string filterName)
@ -48,76 +87,10 @@ public:
}
};
///This is the parent class of all the filters
///TODO: Actually subclass these instead of cheating
class filter : public filter_base
{
private:
//No default constructor for this class
filter();
///The filter's name
std::string name;
///The internal filter used
FilterFunc myFilter;
///The internal scale
int myScale;
//Don't need to calculate these every time (based off width)
///The number of pixels per horizontal row
unsigned int horiz_bytes;
///The number of pixels per output horizontal row
unsigned int horiz_bytes_out;
public:
filter(std::string myName,unsigned int _width,unsigned int _height):
filter_base(_width,_height),
name(myName), myFilter(filters::GetFilter(myName)),
myScale(filters::GetFilterScale(myName)),
horiz_bytes(_width * 4), horiz_bytes_out(_width * 4 * filters::GetFilterScale(myName))
{
// std::cerr << name << std::endl;
}
std::string getName()
{
return name;
}
int getScale()
{
return myScale;
}
/**
* New Version: Run the filter
*
* This one is smart.
* It knows it's a 32 bit filter, and the input width will not change from when it is initialized.
*
* \param[in] srcPtr A pointer to the input 32 bit RGB Pixel Array
* \param[in] dstPtr A pointer to the output 32 bit RGB Pixel Array
*/
void run(u32 *srcPtr, u32 *dstPtr)
{
if(myFilter!=NULL)
{
myFilter(reinterpret_cast<u8 *>(srcPtr),horiz_bytes,reinterpret_cast<u8 *>(dstPtr),horiz_bytes_out,getWidth(),getHeight());
}
else
{
//If the filter doesn't exist, then we still need to get the data to the output buffer
std::memcpy(dstPtr,srcPtr, horiz_bytes_out*getHeight()*myScale);
}
}
bool exists()
{
if (myFilter==NULL)
return false;
else
return true;
}
};
//These are the available filters
//wx
// src/wx/wxvbam.h:263-278
// src/wxpanel.cpp:1100-1256
// src/wxpanel.cpp
//gtk
// src/gtk/filters.h

View File

@ -65,19 +65,6 @@ public:
break;
}
}
static bool exists(ifbfunc filter_select)
{
switch(filter_select)
{
case IFB_SMART:
case IFB_MOTION_BLUR:
return true;
break;
default:
return false;
break;
}
}
};
#endif //NEW_INTERFRAME_HPP

View File

@ -6,6 +6,7 @@
#include "../common/Patch.h"
#include <wx/dcbuffer.h>
#include "../sdl/text.h"
#include "../filters/filters.hpp"
int emulating;
@ -1038,7 +1039,7 @@ public:
unsigned int nthreads, threadno;
unsigned int width, height, scale;
u32 *dst;
filter * mainFilter;
filter_base * mainFilter;
filter_base * iFilter;
// set this param every round
@ -1104,7 +1105,7 @@ DrawingPanel::DrawingPanel(int _width, int _height) :
threads[i].width = width;
threads[i].height = band_height;
threads[i].dst = reinterpret_cast<u32 *>(&todraw);
threads[i].mainFilter=new filter(ToString(gopts.filter),width,band_height);
threads[i].mainFilter=filter_factory::createFilter(ToString(gopts.filter),width,band_height);
threads[i].iFilter=interframe_factory::createIFB((ifbfunc)gopts.ifb,width,band_height);
threads[i].done = &filt_done;
threads[i].lock.Lock();
@ -1113,7 +1114,7 @@ DrawingPanel::DrawingPanel(int _width, int _height) :
}
//Set some important variables
scale=threads[0].scale;
isFiltered = threads[0].mainFilter->exists() || interframe_factory::exists((ifbfunc)gopts.ifb);
isFiltered = threads[0].mainFilter->exists() || threads[0].iFilter->exists();
}
std::cerr << "width: " << width << " Height: " << height << std::endl;

View File

@ -27,7 +27,6 @@
#include "../gb/gbCheats.h"
#include "../gba/Cheats.h"
#include "../filters/filters.hpp"
#include "../filters/new_interframe.hpp"
template <typename T>