1. 定义一个与待处理图像同等大小的数组,赋值1时表示做滤镜处理,赋值-1时表示不做滤镜处理:
enum {
NO_FILTER = -1,
DEFAULT = 0,
DO_FILTER = 1,
};
2. 定义一个Area实现相关操作,保存宽、高、以及一个数组指针:
class Area{
int width;
int heigh;
char* flag;
};
3. 在构造函数中,分配flag数组空间,并设置初始化值DO_FILTER。
Area::Area(int w, int h)
:width(w),
heigh(h)
{
flag = (char *)malloc(sizeof(char) * width * heigh);
if (NULL == flag) {
printf("malloc failed\n");
return;
}
for (int i = 0; i < heigh; i++) {
for (int j = 0; j < width; j++) {
flag[i*width +j] = DO_FILTER;
}
}
}
4. 数组需要支持设置和获取,设置需要的属性有:起始坐标、设置区域宽高、待设置数组,新增一个类封装这些属性:
class AddArea{
int startw;
int starth;
int width;
int heigh;
char* newFlag;
AddArea(int sw, int sh, int w, int h, char flag);
AddArea(int sw, int sh, int w, int h, char* flag);
};
预设两种初始化方式:
(1) 将新增区域全部初始化为NO_FILTER或DO_FILTER
(2) 根据已有数组初始化新增区域
将AddArea的区域合入Area时,如果获取到值是DEFAULT则不做任何操作,否则更新Area数值的值:
void Area::set(AddArea *addArea)
{
int copy_w = (width - addArea->getStartWidth() > addArea->getWidth()) ? addArea->getWidth() : width - addArea->getStartWidth();
int copy_h = (heigh - addArea->getStartHeigh() > addArea->getHeigh()) ? addArea->getHeigh() : heigh - addArea->getStartHeigh();
for (int i = 0; i < copy_h; i++) {
for (int j = 0; j < copy_w; j++) {
if (addArea->get(j, i) != DEFAULT) {
flag[(addArea->getStartHeigh() + i) * width + (addArea->getStartWidth() + j)] = addArea->get(j, i);
}
}
}
}
get函数设定为返回指定位置数值的方式:
int Area::get(int x, int y)
{
return flag[y * width + x];
}
5. 修改滤镜处理算法:
void InverseFilter::doJPGFilter(uchar* inputbuf, uchar* outputbuf)
{
for (int y = 0; y < picData.heigh; y++)
{
uchar* P0 = inputbuf + (y * picData.width * 3);
uchar* P1 = outputbuf + (y * picData.width * 3);
for (int x = 0; x < picData.width; x++)
{
if (NO_FILTER == area->get(x, y)){
P1[3 * x] = P0[3 * x];
P1[3 * x + 1] = P0[3 * x + 1];
P1[3 * x + 2] = P0[3 * x + 2];
} else {
P1[3 * x] = 255 - P0[3 * x];
P1[3 * x + 1] = 255 - P0[3 * x + 1];
P1[3 * x + 2] = 255 - P0[3 * x + 2];
}
}
}
}