int* SharpenFilter::highBoostSharpen() { int boostFactor = 1; int *blurPixels = new int[width * height]; memcpy(blurPixels, pixels, width * height * sizeof(int)); AverageSmoothFilter *smoothFilter = new AverageSmoothFilter(blurPixels, width, height); blurPixels = smoothFilter->procImage(); int *edgePixels = new int[width * height]; for (int i = 0; i < width * height; i++) { Color pixColor(pixels[i]); Color blurColor(blurPixels[i]); int edgeR = min(255, max(0, pixColor.R() - blurColor.R())); int edgeG = min(255, max(0, pixColor.G() - blurColor.G())); int edgeB = min(255, max(0, pixColor.B() - blurColor.B())); edgePixels[i] = RGB2Color(edgeR, edgeG, edgeB); } for (int i = 0; i < width * height; i++) { Color pixColor(pixels[i]); Color edgeColor(edgePixels[i]); int r = min(255, max(0, pixColor.R() + boostFactor * edgeColor.R())); int g = min(255, max(0, pixColor.G() + boostFactor * edgeColor.G())); int b = min(255, max(0, pixColor.B() + boostFactor * edgeColor.B())); pixels[i] = RGB2Color(r, g, b); } delete smoothFilter; delete [] edgePixels; return pixels; }
int* SketchFilter::procImage() { changeImageToGray(pixels, width, height); int *originPixels = new int[width * height]; memcpy(originPixels, pixels, width * height * sizeof(int)); int threshold = 7; for (int i = 1; i < height - 1; i++) { for (int j = 1; j < width - 1; j++) { Color centerColor(originPixels[i * width + j]); int centerGray = centerColor.R(); int rightBottomIndex = (i + 1) * width + j + 1; if (rightBottomIndex < width * height) { Color rightBottomColor(originPixels[rightBottomIndex]); int rightBottomGray = rightBottomColor.R(); if (abs(centerGray - rightBottomGray) >= threshold) { pixels[i * width + j] = RGB2Color(0, 0, 0); // black } else { pixels[i * width + j] = RGB2Color(255, 255, 255); // white } } } } delete[] originPixels; return pixels; }
int* SharpenFilter::procImage() { int pixR, pixG, pixB, newR, newG, newB, index; int laplacian[] = {0, -1, 0, -1, 4, -1, 0, -1, 0}; // 3 * 3 laplacian int maskSize = 3; int halfMaskSize = maskSize / 2; int *edgePixels = new int[width * height]; memset(edgePixels, 0, width * height * sizeof(int)); for (int i = halfMaskSize; i < height - halfMaskSize; i++) { for (int k = halfMaskSize; k < width - halfMaskSize; k++) { index = 0; newR = newG = newB = 0; for (int m = -halfMaskSize; m <= halfMaskSize; m++) { for (int n = -halfMaskSize; n <= halfMaskSize; n++) { Color pixColor(pixels[(i + n) * width + k + m]); pixR = pixColor.R(); pixG = pixColor.G(); pixB = pixColor.B(); newR += pixR * laplacian[index]; newG += pixG * laplacian[index]; newB += pixB * laplacian[index]; index++; } } newR = min(255, max(0, newR)); newG = min(255, max(0, newG)); newB = min(255, max(0, newB)); edgePixels[i * width + k] = RGB2Color(newR, newG, newB); } } for (int i = 0; i < width * height; i++) { Color edgeColor(edgePixels[i]); Color originColor(pixels[i]); int r = min(255, max(0, edgeColor.R() + originColor.R())); int g = min(255, max(0, edgeColor.G() + originColor.G())); int b = min(255, max(0, edgeColor.B() + originColor.B())); pixels[i] = RGB2Color(r, g, b); } delete [] edgePixels; return pixels; }
int* AverageSmoothFilter::procImage() { int sumR = 0; int sumG = 0; int sumB = 0; int div = maskSize * maskSize; int halfMaskSize = maskSize / 2; for (int row = halfMaskSize; row < height - halfMaskSize; row++) { for (int col = halfMaskSize; col < width - halfMaskSize; col++) { sumR = sumG = sumB = 0; for (int m = -halfMaskSize; m <= halfMaskSize; m++) { for (int n = -halfMaskSize; n <= halfMaskSize; n++) { int index = (row + m) * width + col + n; if (index < width * height) { Color color(pixels[(row + m) * width + col + n]); sumR += color.R(); sumG += color.G(); sumB += color.B(); } } } pixels[row * width + col] = RGB2Color(sumR / div, sumG / div, sumB / div); } } return this->pixels; }
int* HueSaturationFilter::setHueSaturationIntesity(double hue, double saturation, double intensity) { for (int i = 0; i < width * height; i++) { HSI hsi = pixelsHSI[i]; double h = hsi.h; h = hue; if (h > 360) { h = h - 360; } else if (h < 0) { h = h + 360; } double s = hsi.s; s = saturation; s = min(1.0, max(0.0, s)); double newI = hsi.i; newI = intensity; newI = min(1.0, max(0.0, newI)); pixelsHSI[i] = HSI(h, s, newI); RGB rgb = ColorTranslator::HSI2RGB(h, s, hsi.i); if (ColorTranslator::checkRGB(rgb)) { pixels[i] = RGB2Color(rgb.r, rgb.g, rgb.b); } } return pixels; }
int* GammaCorrectionFilter::procImage() { for (int i = 0; i < width * height; i++) { Color color(pixels[i]); int r = gammaTable[color.R()]; int g = gammaTable[color.G()]; int b = gammaTable[color.B()]; pixels[i] = RGB2Color(r, g, b); } return pixels; }
int* TvFilter::procImage() { int r, g, b; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y += gap) { r = g = b = 0; for (int w = 0; w < 4; w++) { int index = (y + w) * width + x; if (index < width * height) { Color color(pixels[index]); r += color.R() / gap; g += color.G() / gap; b += color.B() / gap; } } r = min(255, max(0, r)); g = min(255, max(0, g)); b = min(255, min(0, b)); for (int w = 0; w < gap; w++) { int index = (y + w) * width + x; if (index < width * height) { if (w == 0) { pixels[(y + w) * width + x] = RGB2Color(r, 0, 0); } if (w == 1) { pixels[(y + w) * width + x] = RGB2Color(0, g, 0); } if (w == 2) { pixels[(y + w) * width + x] = RGB2Color(0, 0, b); } // if (w == 3) { // pixels[(y + w) * width + x] = RGB2Color(r, 0, 0); // } } } } } return pixels; }
int* HueSaturationFilter::setSaturation(double saturation) { for (int i = 0; i < width * height; i++) { HSI hsi = pixelsHSI[i]; double s = hsi.s; s = saturation; s = min(1.0, max(0.0, s)); pixelsHSI[i] = HSI(hsi.h, s, hsi.i); RGB rgb = ColorTranslator::HSI2RGB(hsi.h, s, hsi.i); if (ColorTranslator::checkRGB(rgb)) { pixels[i] = RGB2Color(rgb.r, rgb.g, rgb.b); } } return pixels; }
int* GaussianBlurFilter::procImage() { if (maskSize == 1) { return NULL; } int* tempPixels = new int[width * height]; memcpy(tempPixels, pixels, width * height * sizeof(int)); double sumR = 0, sumG = 0, sumB = 0; int index = 0; int bound = maskSize / 2; long startTime = getCurrentTime(); for (int row = bound; row < height - bound; row++) { for (int col = bound; col < width - bound; col++) { index = 0; sumR = sumG = sumB = 0; for (int i = -bound; i <= bound; i++) { for (int j = -bound; j <= bound; j++) { int pixel_index = (row + i) * width + col + j; if (pixel_index < width * height) { Color color(tempPixels[pixel_index]); sumR += color.R() * kernel[index]; sumG += color.G() * kernel[index]; sumB += color.B() * kernel[index]; index++; } } } pixels[row * width + col] = RGB2Color(int(sumR), int(sumG), int(sumB)); } } long endTime = getCurrentTime(); LOGI("guassian blur use %ld ms, maskSize: %d, sigma: %f", endTime - startTime, maskSize, sigma); delete [] tempPixels; return this->pixels; }
int* HueSaturationFilter::setHue(double hue) { for (int i = 0; i < width * height; i++) { HSI hsi = pixelsHSI[i]; double h = hsi.h; h = hue; //h = min(360.0, max(0.0, h)); if (h > 360) { h = h - 360; } else if (h < 0) { h = h + 360; } pixelsHSI[i] = HSI(h, hsi.s, hsi.i); RGB rgb = ColorTranslator::HSI2RGB(h, hsi.s, hsi.i); if (ColorTranslator::checkRGB(rgb)) { pixels[i] = RGB2Color(rgb.r, rgb.g, rgb.b); } } return pixels; }
int* OilFilter::procImage() { int *originPixels = new int[width * height]; memcpy(originPixels, pixels, width * height * sizeof(int)); int rHis[OIL_FILTER_LEVEL], gHis[OIL_FILTER_LEVEL], bHis[OIL_FILTER_LEVEL]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { memset(rHis, 0, OIL_FILTER_LEVEL * sizeof(int)); memset(gHis, 0, OIL_FILTER_LEVEL * sizeof(int)); memset(bHis, 0, OIL_FILTER_LEVEL * sizeof(int)); int rowOffset, colOffset; for (int row = -this->oilRange; row < this->oilRange; row++) { rowOffset = y + row; if (rowOffset >= 0 && rowOffset < height) { for (int col = -this->oilRange; col < this->oilRange; col++) { colOffset = x + col; if (colOffset >= 0 && colOffset < width) { Color color = originPixels[rowOffset * width + colOffset]; int r = color.R(); int g = color.G(); int b = color.B(); rHis[r]++; gHis[g]++; bHis[b]++; } } } } int maxR = 0, maxG = 0, maxB = 0; for (int i = 1; i < OIL_FILTER_LEVEL; i++) { if (rHis[i] > rHis[maxR]) { maxR = i; } if (gHis[i] > gHis[maxG]) { maxG = i; } if (bHis[i] > bHis[maxB]) { maxB = i; } } if (rHis[maxR] != 0 && gHis[maxG] != 0 && bHis[maxB] != 0) { int finalR = maxR; int finalG = maxG; int finalB = maxB; finalR = min(255, max(0, finalR)); finalG = min(255, max(0, finalG)); finalB = min(255, max(0, finalB)); pixels[y * width + x] = RGB2Color(finalR, finalG, finalB); } } } delete [] originPixels; return pixels; }