-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.c
140 lines (127 loc) · 3.12 KB
/
helpers.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "helpers.h"
#include <math.h>
int check(int a)
{
if ( a > 255)
{
return 255;
}
else
{
return a;
}
}
// 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].rgbtRed+image[i][j].rgbtBlue+image[i][j].rgbtGreen)/3.0);
image[i][j].rgbtRed = avg;
image[i][j].rgbtBlue = avg;
image[i][j].rgbtGreen = avg;
}
}
return;
}
// Convert image to sepia
void sepia(int height, int width, RGBTRIPLE image[height][width])
{
for ( int i = 0 ; i < height ; i++ )
{
for( int j = 0 ; j < width ; j++ )
{
int red=round(0.393*image[i][j].rgbtRed+0.769*image[i][j].rgbtGreen+0.189*image[i][j].rgbtBlue);
int green=round(0.349*image[i][j].rgbtRed+0.686*image[i][j].rgbtGreen+0.168*image[i][j].rgbtBlue);
int blue=round(0.272*image[i][j].rgbtRed+0.534*image[i][j].rgbtGreen+0.131*image[i][j].rgbtBlue);
red=check(red);
green=check(green);
blue=check(blue);
image[i][j].rgbtRed=red;
image[i][j].rgbtBlue=blue;
image[i][j].rgbtGreen=green;
}
}
return;
}
// Reflect image horizontally
void reflect(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/2 ; j++)
{
image[i][j]=copy[i][width-1-j];
image[i][width-1-j]=copy[i][j];
}
}
}
// Blur image
int get_blur(int height, int width, RGBTRIPLE copy[height][width],int c,int i,int j)
{
int sum=0;
float count=0.0;
for ( int row=0 ; row < height ; row++ )
{
if (row< i-1 || row >i+1)
{
continue;
}
for ( int col=0 ; col < width ; col++ ) {
if( col<j-1 || col> j+1)
{
continue;
}
if(c==1)
{
sum+=copy[row][col].rgbtRed;
count++;
}
if(c==2)
{
sum+=copy[row][col].rgbtBlue;
count++;
}
if(c==3)
{
sum+=copy[row][col].rgbtGreen;
count++;
}
}
}
if(count==0.0){
return 0;
}
return round(sum/count);
}
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++)
{
image[i][j].rgbtRed=get_blur(height,width,copy,1, i, j);
image[i][j].rgbtBlue=get_blur(height,width,copy,2, i, j);
image[i][j].rgbtGreen=get_blur(height,width,copy,3, i, j);
}
}
return;
}