-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathhelpers.c
More file actions
464 lines (410 loc) · 15.2 KB
/
helpers.c
File metadata and controls
464 lines (410 loc) · 15.2 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#include "helpers.h"
#include <stdint.h>
#include <stdlib.h>
#include <math.h>
#include "bmp.h"
#include <string.h>
#define LEVELS 16
#define RADIUS 8
int min(int a,int b){
if(a<b) return a;
return b;
}
int max(int a,int b){
if(a>b) return a;
return b;
}
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 red = image[i][j].rgbtRed;
int green = image[i][j].rgbtGreen;
int blue = image[i][j].rgbtBlue;
int avg = (red + green + blue) / 3;
image[i][j].rgbtRed = avg;
image[i][j].rgbtGreen = avg;
image[i][j].rgbtBlue = avg;
}
}
}
void invert(int height, int width, RGBTRIPLE image[height][width]){
for(int i = 0; i<height; i++){
for(int j = 0; j<width; j++){
//Original RGB
int red = image[i][j].rgbtRed;
int green = image[i][j].rgbtGreen;
int blue = image[i][j].rgbtBlue;
//Normal inversion
int invRed = 255 - red;
int invGreen = 255 - green;
int invBlue = 255 - blue;
//for maintain average brightness
int originalBrightness = 0.299*red + 0.587*green + 0.114*blue;
int invertedBrightness = 0.299*invRed + 0.587*invGreen + 0.114*invBlue;
int brightnessDiff = originalBrightness - invertedBrightness;
invRed = min(max(invRed + brightnessDiff, 0), 255);
invGreen = min(max(invGreen + brightnessDiff, 0), 255);
invBlue = min(max(invBlue + brightnessDiff, 0), 255);
//Assign
image[i][j].rgbtRed = invRed;
image[i][j].rgbtGreen = invGreen;
image[i][j].rgbtBlue = invBlue;
}
}
return;
// Convert image to grayscale
}
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 originalRed = image[i][j].rgbtRed;
int originalGreen = image[i][j].rgbtGreen;
int originalBlue = image[i][j].rgbtBlue;
int sepiaRed = round(0.393 * originalRed + 0.769 * originalGreen + 0.189 * originalBlue);
int sepiaGreen = round(0.349 * originalRed + 0.686 * originalGreen + 0.168 * originalBlue);
int sepiaBlue = round(0.272 * originalRed + 0.534 * originalGreen + 0.131 * originalBlue);
if (sepiaRed > 255)
sepiaRed = 255;
if (sepiaGreen > 255)
sepiaGreen = 255;
if (sepiaBlue > 255)
sepiaBlue = 255;
image[i][j].rgbtRed = sepiaRed;
image[i][j].rgbtGreen = sepiaGreen;
image[i][j].rgbtBlue = sepiaBlue;
}
}
}
void reflect(int height, int width, RGBTRIPLE image[height][width]){
// Loop over each row
for (int i = 0; i < height; i++)
{
// Swap pixels horizontally (mirror)
for (int j = 0; j < width / 2; j++)
{
// Swap left pixel with right pixel
RGBTRIPLE temp = image[i][j];
image[i][j] = image[i][width - 1 - j];
image[i][width - 1 - j] = temp;
}
}
}
void blur(int height, int width, RGBTRIPLE image[height][width]){
// Allocate temporary array on heap
RGBTRIPLE **temp = malloc(height * sizeof(RGBTRIPLE *));
for (int i = 0; i < height; i++)
temp[i] = malloc(width * sizeof(RGBTRIPLE));
int kernelSize = 21; // large kernel for heavy blur
int offset = kernelSize / 2;
// Repeating blur 2-3 times for ultra blur effect
//because in single time effect not much visible
for(int repeat = 0; repeat < 3; repeat++){
for(int i = 0; i < height; i++){
for (int j = 0; j < width; j++){
int sumRed = 0, sumGreen = 0, sumBlue = 0;
int count = 0;
for (int ki = -offset; ki <= offset; ki++){
for(int kj = -offset; kj <= offset; kj++){
int ni = i + ki;
int nj = j + kj;
if(ni >= 0 && ni < height && nj >= 0 && nj < width){
sumRed += image[ni][nj].rgbtRed;
sumGreen += image[ni][nj].rgbtGreen;
sumBlue += image[ni][nj].rgbtBlue;
count++;
}
}
}
temp[i][j].rgbtRed = (uint8_t)(sumRed / count);
temp[i][j].rgbtGreen = (uint8_t)(sumGreen / count);
temp[i][j].rgbtBlue = (uint8_t)(sumBlue / count);
}
}
// Copy blurred array back to orig
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
image[i][j] = temp[i][j];
}
for (int i = 0; i < height; i++)
free(temp[i]);
free(temp);
}
void brightness(int height, int width, RGBTRIPLE image[height][width], int value) {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int r = image[i][j].rgbtRed + value;
int g = image[i][j].rgbtGreen + value;
int b = image[i][j].rgbtBlue + value;
if (r > 255) r = 255;
if (g > 255) g = 255;
if (b > 255) b = 255;
if (r < 0) r = 0;
if (g < 0) g = 0;
if (b < 0) b = 0;
image[i][j].rgbtRed = r;
image[i][j].rgbtGreen = g;
image[i][j].rgbtBlue = b;
}
}
}
void vignette(int height, int width, RGBTRIPLE image[height][width]){
float cx = width / 2.0; // center of the image
float cy= height / 2.0;
float max_dis= sqrt(cx * cx + cy * cy);
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
float disx = j - cx;
float disy = i - cy;
float dist= sqrt(disx * disx + disy * disy);
// (0.0 = dark, 1.0 = og)
float vig = 1.0 - (dist/ max_dis);
if(vig< 0.0) vig = 0.0;
if(vig > 1.0) vig = 1.0;
image[i][j].rgbtRed = (int)(image[i][j].rgbtRed * vig);
image[i][j].rgbtGreen = (int)(image[i][j].rgbtGreen * vig);
image[i][j].rgbtBlue = (int)(image[i][j].rgbtBlue * vig);
}
}
}
//Threshold Filter(Black & White)
void threshold(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].rgbtGreen +
image[i][j].rgbtBlue) / 3.0);
if (avg >= 128)
{
image[i][j].rgbtRed = 255;
image[i][j].rgbtGreen = 255;
image[i][j].rgbtBlue = 255;
}
else
{
image[i][j].rgbtRed = 0;
image[i][j].rgbtGreen = 0;
image[i][j].rgbtBlue = 0;
}
}
}
}
void detect_edges(int height, int width, RGBTRIPLE image[height][width])
{
// Temporary copy of the image
RGBTRIPLE **copy = malloc(height * sizeof(RGBTRIPLE *));
for (int i = 0; i < height; i++)
copy[i] = malloc(width * sizeof(RGBTRIPLE));
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
copy[i][j] = image[i][j];
// Sobel kernels
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}
};
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int sumRx = 0, sumGx = 0, sumBx = 0;
int sumRy = 0, sumGy = 0, sumBy = 0;
for (int di = -1; di <= 1; di++)
{
for (int dj = -1; dj <= 1; dj++)
{
int ni = i + di;
int nj = j + dj;
if (ni >= 0 && ni < height && nj >= 0 && nj < width)
{
RGBTRIPLE pixel = copy[ni][nj];
int kx = Gx[di + 1][dj + 1];
int ky = Gy[di + 1][dj + 1];
sumRx += pixel.rgbtRed * kx;
sumGx += pixel.rgbtGreen * kx;
sumBx += pixel.rgbtBlue * kx;
sumRy += pixel.rgbtRed * ky;
sumGy += pixel.rgbtGreen * ky;
sumBy += pixel.rgbtBlue * ky;
}
}
}
// Calculate gradient magnitude and clamp
int red = min(max((int)round(sqrt(sumRx*sumRx + sumRy*sumRy)), 0), 255);
int green = min(max((int)round(sqrt(sumGx*sumGx + sumGy*sumGy)), 0), 255);
int blue = min(max((int)round(sqrt(sumBx*sumBx + sumBy*sumBy)), 0), 255);
image[i][j].rgbtRed = red;
image[i][j].rgbtGreen = green;
image[i][j].rgbtBlue = blue;
}
}
// Free temporary array
for (int i = 0; i < height; i++)
free(copy[i]);
free(copy);
}
void glow(int height, int width, RGBTRIPLE image[height][width])
{
// Step 1: make a copy of the original
RGBTRIPLE **original = malloc(height * sizeof(RGBTRIPLE *));
RGBTRIPLE **blurred = malloc(height * sizeof(RGBTRIPLE *));
for (int i = 0; i < height; i++)
{
original[i] = malloc(width * sizeof(RGBTRIPLE));
blurred[i] = malloc(width * sizeof(RGBTRIPLE));
for (int j = 0; j < width; j++)
{
original[i][j] = image[i][j];
}
}
// Step 2: apply a *mild* blur to copy (smaller kernel)
int kernelSize = 11;
int offset = kernelSize / 2;
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 ki = -offset; ki <= offset; ki++)
{
for (int kj = -offset; kj <= offset; kj++)
{
int ni = i + ki, nj = j + kj;
if (ni >= 0 && ni < height && nj >= 0 && nj < width)
{
sumRed += original[ni][nj].rgbtRed;
sumGreen += original[ni][nj].rgbtGreen;
sumBlue += original[ni][nj].rgbtBlue;
count++;
}
}
}
blurred[i][j].rgbtRed = sumRed / count;
blurred[i][j].rgbtGreen = sumGreen / count;
blurred[i][j].rgbtBlue = sumBlue / count;
}
}
// Step 3: blend original + blurred to produce glow
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
float blend = 0.3; // glow intensity
int newRed = (1 - blend) * original[i][j].rgbtRed + blend * blurred[i][j].rgbtRed;
int newGreen = (1 - blend) * original[i][j].rgbtGreen + blend * blurred[i][j].rgbtGreen;
int newBlue = (1 - blend) * original[i][j].rgbtBlue + blend * blurred[i][j].rgbtBlue;
image[i][j].rgbtRed = min(255, newRed);
image[i][j].rgbtGreen = min(255, newGreen);
image[i][j].rgbtBlue = min(255, newBlue);
}
}
// Step 4: cleanup
for (int i = 0; i < height; i++)
{
free(original[i]);
free(blurred[i]);
}
free(original);
free(blurred);
}
// Oil Paint filter
void oilpaint(int height, int width, RGBTRIPLE image[height][width]){
RGBTRIPLE **copy = malloc(height * sizeof(RGBTRIPLE *));
for (int i = 0; i < height; i++){
copy[i] = malloc(width * sizeof(RGBTRIPLE));
for (int j = 0; j < width; j++){
copy[i][j] = image[i][j];
}
}
// logic
for (int i = 0; i < height; i++){
for (int j = 0; j < width; j++){
int intensityCount[LEVELS];
long sumRed[LEVELS], sumGreen[LEVELS], sumBlue[LEVELS];
memset(intensityCount, 0, sizeof(intensityCount));
memset(sumRed, 0, sizeof(sumRed));
memset(sumGreen, 0, sizeof(sumGreen));
memset(sumBlue, 0, sizeof(sumBlue));
// Analyze neighborhood
for (int di = -RADIUS; di <= RADIUS; di++){
for (int dj = -RADIUS; dj <= RADIUS; dj++){
int ni = i + di;
int nj = j + dj;
if (ni < 0 || ni >= height || nj < 0 || nj >= width)
continue;
RGBTRIPLE pixel = copy[ni][nj];
int intensity = ((pixel.rgbtRed + pixel.rgbtGreen + pixel.rgbtBlue) / 3) * LEVELS / 256;
if (intensity >= LEVELS)
intensity = LEVELS - 1;
intensityCount[intensity]++;
sumRed[intensity] += pixel.rgbtRed;
sumGreen[intensity] += pixel.rgbtGreen;
sumBlue[intensity] += pixel.rgbtBlue;
}
}
int dominant = 0, maxCount = 0;
for (int k = 0; k < LEVELS; k++){
if (intensityCount[k] > maxCount){
maxCount = intensityCount[k];
dominant = k;
}
}
if (maxCount > 0){
int newRed = sumRed[dominant] / maxCount;
int newGreen = sumGreen[dominant] / maxCount;
int newBlue = sumBlue[dominant] / maxCount;
image[i][j].rgbtRed = (newRed / 32) * 32;
image[i][j].rgbtGreen = (newGreen / 32) * 32;
image[i][j].rgbtBlue = (newBlue / 32) * 32;
}
}
}
for (int i = 0; i < height; i++)
free(copy[i]);
free(copy);
}
// Pixelate filter
void pixelate(int height, int width, RGBTRIPLE image[height][width]){
int blockSize = 8;
if (width > 1000 || height > 1000) {
blockSize = 16;
} else if (width > 500 || height > 500) {
blockSize = 12;
}
for (int blockY = 0; blockY < height; blockY += blockSize){
for (int blockX = 0; blockX < width; blockX += blockSize){
int blockEndY = min(blockY + blockSize, height);
int blockEndX = min(blockX + blockSize, width);
long sumRed = 0, sumGreen = 0, sumBlue = 0;
int pixelCount = 0;
for (int y = blockY; y < blockEndY; y++){
for (int x = blockX; x < blockEndX; x++){
sumRed += image[y][x].rgbtRed;
sumGreen += image[y][x].rgbtGreen;
sumBlue += image[y][x].rgbtBlue;
pixelCount++;
}
}
uint8_t avgRed = (uint8_t)(sumRed / pixelCount);
uint8_t avgGreen = (uint8_t)(sumGreen / pixelCount);
uint8_t avgBlue = (uint8_t)(sumBlue / pixelCount);
for (int y = blockY; y < blockEndY; y++){
for (int x = blockX; x < blockEndX; x++){
image[y][x].rgbtRed = avgRed;
image[y][x].rgbtGreen = avgGreen;
image[y][x].rgbtBlue = avgBlue;
}
}
}
}
}