mirror of https://github.com/me50/kukemuna.git
128 lines
3.8 KiB
C
128 lines
3.8 KiB
C
|
|
#include "helpers.h"
|
||
|
|
#include <math.h>
|
||
|
|
#include <stdio.h>
|
||
|
|
|
||
|
|
// Convert image to grayscale
|
||
|
|
void grayscale(int height, int width, RGBTRIPLE image[height][width])
|
||
|
|
{
|
||
|
|
for (int i = 0; i < height; i++)
|
||
|
|
{
|
||
|
|
for (int j = 0; j < width; j++)
|
||
|
|
{
|
||
|
|
int avg = round((image[i][j].rgbtBlue + image[i][j].rgbtGreen + image[i][j].rgbtRed) /
|
||
|
|
(float) 3);
|
||
|
|
image[i][j].rgbtBlue = image[i][j].rgbtGreen = image[i][j].rgbtRed = avg;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Reflect image horizontally
|
||
|
|
void reflect(int height, int width, RGBTRIPLE image[height][width])
|
||
|
|
{
|
||
|
|
RGBTRIPLE tmp[height][width];
|
||
|
|
|
||
|
|
for (int i = 0; i < height; i++)
|
||
|
|
{
|
||
|
|
for (int j = 0; j < width; j++)
|
||
|
|
{
|
||
|
|
tmp[i][j] = image[i][width - j - 1];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
for (int i = 0; i < height; i++)
|
||
|
|
{
|
||
|
|
for (int j = 0; j < width; j++)
|
||
|
|
{
|
||
|
|
image[i][j] = tmp[i][j];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Blur image
|
||
|
|
void blur(int height, int width, RGBTRIPLE image[height][width])
|
||
|
|
{
|
||
|
|
RGBTRIPLE copy[height][width];
|
||
|
|
|
||
|
|
for (int i = 0; i < height; i++)
|
||
|
|
{
|
||
|
|
for (int j = 0; j < width; j++)
|
||
|
|
{
|
||
|
|
copy[i][j] = image[i][j];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
for (int i = 0; i < height; i++)
|
||
|
|
{
|
||
|
|
for (int j = 0; j < width; j++)
|
||
|
|
{
|
||
|
|
int sumRed = 0, sumGreen = 0, sumBlue = 0, count = 0;
|
||
|
|
|
||
|
|
for (int k = (i - 1); k <= (i + 1); k++)
|
||
|
|
{
|
||
|
|
for (int l = (j - 1); l <= (j + 1); l++)
|
||
|
|
{
|
||
|
|
if ((k >= 0 && k <= (height - 1)) && (l >= 0 && l <= (width - 1)))
|
||
|
|
{
|
||
|
|
sumRed += copy[k][l].rgbtRed;
|
||
|
|
sumGreen += copy[k][l].rgbtGreen;
|
||
|
|
sumBlue += copy[k][l].rgbtBlue;
|
||
|
|
count++;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
image[i][j].rgbtRed = round(sumRed / (float) count);
|
||
|
|
image[i][j].rgbtGreen = round(sumGreen / (float) count);
|
||
|
|
image[i][j].rgbtBlue = round(sumBlue / (float) count);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Detect edges
|
||
|
|
void edges(int height, int width, RGBTRIPLE image[height][width])
|
||
|
|
{
|
||
|
|
int Gx[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}};
|
||
|
|
int Gy[3][3] = {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}};
|
||
|
|
|
||
|
|
RGBTRIPLE copy[height][width];
|
||
|
|
|
||
|
|
for (int i = 0; i < height; i++)
|
||
|
|
{
|
||
|
|
for (int j = 0; j < width; j++)
|
||
|
|
{
|
||
|
|
copy[i][j] = image[i][j];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
for (int i = 0; i < height; i++)
|
||
|
|
{
|
||
|
|
for (int j = 0; j < width; j++)
|
||
|
|
{
|
||
|
|
int sumRedGx = 0, sumGreenGx = 0, sumBlueGx = 0, sumRedGy = 0, sumGreenGy = 0,
|
||
|
|
sumBlueGy = 0;
|
||
|
|
|
||
|
|
for (int k = (i - 1); k <= (i + 1); k++)
|
||
|
|
{
|
||
|
|
for (int l = (j - 1); l <= (j + 1); l++)
|
||
|
|
{
|
||
|
|
if ((k >= 0 && k <= (height - 1)) && (l >= 0 && l <= (width - 1)))
|
||
|
|
{
|
||
|
|
sumRedGx += copy[k][l].rgbtRed * Gx[i - k + 1][j - l + 1];
|
||
|
|
sumGreenGx += copy[k][l].rgbtGreen * Gx[i - k + 1][j - l + 1];
|
||
|
|
sumBlueGx += copy[k][l].rgbtBlue * Gx[i - k + 1][j - l + 1];
|
||
|
|
sumRedGy += copy[k][l].rgbtRed * Gy[i - k + 1][j - l + 1];
|
||
|
|
sumGreenGy += copy[k][l].rgbtGreen * Gy[i - k + 1][j - l + 1];
|
||
|
|
sumBlueGy += copy[k][l].rgbtBlue * Gy[i - k + 1][j - l + 1];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
image[i][j].rgbtRed = fmin(round(sqrt(pow(sumRedGx, 2) + pow(sumRedGy, 2))), 255);
|
||
|
|
image[i][j].rgbtGreen = fmin(round(sqrt(pow(sumGreenGx, 2) + pow(sumGreenGy, 2))), 255);
|
||
|
|
image[i][j].rgbtBlue = fmin(round(sqrt(pow(sumBlueGx, 2) + pow(sumBlueGy, 2))), 255);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return;
|
||
|
|
}
|