Last active
February 20, 2018 09:23
-
-
Save jameslittle230/637da22c62bb257213f42d67278c51d3 to your computer and use it in GitHub Desktop.
HW2 Submission -- Code Changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void R2Image:: | |
SobelX(void) | |
{ | |
R2Image *output = new R2Image(width, height); | |
double sobel_x[3][3] = { | |
{-1, 0, 1}, | |
{-2, 0, 2}, | |
{-1, 0, 1} | |
}; | |
for (int i = 1; i < width-1; i++) { | |
for (int j = 1; j < height-1; j++) { | |
double sobelVal = | |
(sobel_x[0][0] * Pixel(i-1,j-1).Luminance()) + (sobel_x[0][1] * Pixel(i,j-1).Luminance()) + (sobel_x[0][2] * Pixel(i+1,j-1).Luminance()) + | |
(sobel_x[1][0] * Pixel(i-1,j).Luminance()) + (sobel_x[1][1] * Pixel(i,j).Luminance()) + (sobel_x[1][2] * Pixel(i+1,j).Luminance()) + | |
(sobel_x[2][0] * Pixel(i-1,j+1).Luminance()) + (sobel_x[2][1] * Pixel(i,j+1).Luminance()) + (sobel_x[2][2] * Pixel(i+1,j+1).Luminance()); | |
sobelVal = (sobelVal + 1) / 2; | |
R2Pixel *newPixel = new R2Pixel(sobelVal, sobelVal, sobelVal, 1); | |
newPixel->Clamp(); | |
output->SetPixel(i, j, *newPixel); | |
} | |
} | |
this->pixels = output->pixels; | |
output->pixels = nullptr; | |
delete output; | |
} | |
void R2Image:: | |
SobelY(void) | |
{ | |
R2Image *output = new R2Image(width, height); | |
double sobel_y[3][3] = { | |
{-1, -2, -1}, | |
{0, 0, 0}, | |
{1, 2, 1} | |
}; | |
for (int i = 1; i < width-1; i++) { | |
for (int j = 1; j < height-1; j++) { | |
double sobelVal = | |
(sobel_y[0][0] * Pixel(i-1,j-1).Luminance()) + (sobel_y[0][1] * Pixel(i,j-1).Luminance()) + (sobel_y[0][2] * Pixel(i+1,j-1).Luminance()) + | |
(sobel_y[1][0] * Pixel(i-1,j).Luminance()) + (sobel_y[1][1] * Pixel(i,j).Luminance()) + (sobel_y[1][2] * Pixel(i+1,j).Luminance()) + | |
(sobel_y[2][0] * Pixel(i-1,j+1).Luminance()) + (sobel_y[2][1] * Pixel(i,j+1).Luminance()) + (sobel_y[2][2] * Pixel(i+1,j+1).Luminance()); | |
sobelVal = (sobelVal + 1) / 2; | |
R2Pixel *newPixel = new R2Pixel(sobelVal, sobelVal, sobelVal, 1); | |
newPixel->Clamp(); | |
output->SetPixel(i, j, *newPixel); | |
} | |
} | |
this->pixels = output->pixels; | |
output->pixels = nullptr; | |
delete output; | |
} | |
void R2Image:: | |
ChangeSaturation(double factor) | |
{ | |
// http://www.niwa.nu/2013/05/math-behind-colorspace-conversions-rgb-hsl/ | |
for (int i = 0; i < width; i++) { | |
for (int j = 0; j < height; j++) { | |
R2Pixel p = Pixel(i,j); | |
// convert RGB triplet to HSL triplet | |
double h, s, l; | |
double r = p.Red(), g = p.Green(), b = p.Blue(); | |
double r2, g2, b2; | |
double min = std::min(r, std::min(g, b)); | |
double max = std::max(r, std::max(g, b)); | |
l = p.Luminance(); | |
if(l < 0.5) { | |
s = (max-min)/(max+min); | |
} else { | |
s = (max-min)/(2.0-max-min); | |
} | |
if(isnan(s)) {s=0;} | |
if(min == max) { | |
h = 0; | |
} else { | |
if(max == r) {h = (g-b)/(max-min);} | |
else if(max == g) {h = 2.0 + (b-r)/(max-min);} | |
else if(max == b) {h = 4.0 + (r-g)/(max-min);} | |
else {h = 0;} | |
h /= 6; | |
} | |
h = h>1?1:h<0?0:h; | |
s = s>1?1:s<0?0:s; | |
l = l>1?1:l<0?0:l; | |
// Change saturation by factor | |
s *= factor; | |
// HSL back to RGB | |
double t1, t2, tr, tg, tb; | |
if(s == 0) { | |
r2 = l; g2 = l; b2 = l; | |
} else { | |
if(l < 0.5) { | |
t1 = l * (1.0+s); | |
} else { | |
t1 = l+s-l*s; | |
} | |
t2 = 2.0 * l - t1; | |
tr = fmod(h + 0.333, 1); | |
tg = fmod(h, 1); | |
tb = fmod(h - 0.333, 1); | |
if(6*tr < 1) {r2 = t2 + (t1 - t2) * 6 * tr;} | |
else if(2*tr < 1) {r2 = t1;} | |
else if(3*tr < 2) {r2 = t2 + (t1-t2) * (0.666-tr) * 6;} | |
else {r2 = t2;} | |
if(6*tg < 1) {g2 = t2 + (t1 - t2) * 6 * tg;} | |
else if(2*tg < 1) {g2 = t1;} | |
else if(3*tg < 2) {g2 = t2 + (t1-t2) * (0.666-tg) * 6;} | |
else {g2 = t2;} | |
if(6*tb < 1) {b2 = t2 + (t1 - t2) * 6 * tb;} | |
else if(2*tb < 1) {b2 = t1;} | |
else if(3*tb < 2) {b2 = t2 + (t1-t2) * (0.666-tb) * 6;} | |
else {b2 = t2;} | |
} | |
Pixel(i, j).Reset(r2, g2, b2, 1); | |
Pixel(i, j).Clamp(); | |
} | |
} | |
} | |
void R2Image:: | |
Sharpen() | |
{ | |
R2Image *output = new R2Image(width, height); | |
double sharpen[3][3] = { | |
{-2, -2, -2}, | |
{-2, 16, -2}, | |
{-2, -2, -2} | |
}; | |
for (int i = 1; i < width-1; i++) { | |
for (int j = 1; j < height-1; j++) { | |
double sharpenR = | |
(sharpen[0][0] * Pixel(i-1,j-1).Red()) + (sharpen[0][1] * Pixel(i,j-1).Red()) + (sharpen[0][2] * Pixel(i+1,j-1).Red()) + | |
(sharpen[1][0] * Pixel(i-1,j).Red()) + (sharpen[1][1] * Pixel(i,j).Red()) + (sharpen[1][2] * Pixel(i+1,j).Red()) + | |
(sharpen[2][0] * Pixel(i-1,j+1).Red()) + (sharpen[2][1] * Pixel(i,j+1).Red()) + (sharpen[2][2] * Pixel(i+1,j+1).Red()); | |
double sharpenG = | |
(sharpen[0][0] * Pixel(i-1,j-1).Green()) + (sharpen[0][1] * Pixel(i,j-1).Green()) + (sharpen[0][2] * Pixel(i+1,j-1).Green()) + | |
(sharpen[1][0] * Pixel(i-1,j).Green()) + (sharpen[1][1] * Pixel(i,j).Green()) + (sharpen[1][2] * Pixel(i+1,j).Green()) + | |
(sharpen[2][0] * Pixel(i-1,j+1).Green()) + (sharpen[2][1] * Pixel(i,j+1).Green()) + (sharpen[2][2] * Pixel(i+1,j+1).Green()); | |
double sharpenB = | |
(sharpen[0][0] * Pixel(i-1,j-1).Blue()) + (sharpen[0][1] * Pixel(i,j-1).Blue()) + (sharpen[0][2] * Pixel(i+1,j-1).Blue()) + | |
(sharpen[1][0] * Pixel(i-1,j).Blue()) + (sharpen[1][1] * Pixel(i,j).Blue()) + (sharpen[1][2] * Pixel(i+1,j).Blue()) + | |
(sharpen[2][0] * Pixel(i-1,j+1).Blue()) + (sharpen[2][1] * Pixel(i,j+1).Blue()) + (sharpen[2][2] * Pixel(i+1,j+1).Blue()); | |
R2Pixel *newPixel = new R2Pixel(Pixel(i, j).Red() + sharpenR / 2, | |
Pixel(i, j).Green() + sharpenG / 2, Pixel(i, j).Blue() + sharpenB / 2, 1); | |
newPixel->Clamp(); | |
output->SetPixel(i, j, *newPixel); | |
} | |
} | |
this->pixels = output->pixels; | |
output->pixels = nullptr; | |
delete output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment