Skip to content

Commit d21c06c

Browse files
committed
Refactored Codebase 2
Removed unnecessary comments, improved readability by adding comment into the variable. Comments now Include small when inside a function or for loop and Big standard when outside of it, to further improve readability. - Still Requires the reduction of repetition of code!
1 parent 950e046 commit d21c06c

File tree

7 files changed

+136
-152
lines changed

7 files changed

+136
-152
lines changed

C_image_processing/filters/black_and_white_filter.c

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <stdio.h>
2+
23
#define THRESHOLD 128 // define value of threshold for black and white
34
#define WHITE 255
45
#define BLACK 0
@@ -16,48 +17,45 @@ int black_and_white_filter(const char *inputFile, const char *outputFile) {
1617
return 1;
1718
}
1819

19-
int i;
20-
unsigned char byte[54];
20+
unsigned char headerInfo[54];
2121
unsigned char colorTable[1024];
2222

23-
// read header info of image
24-
for(i = 0; i < 54; i++) {
25-
byte[i] = getc(fileIn);
23+
// Read header info of image
24+
for(int i = 0; i < 54; i++) {
25+
headerInfo[i] = getc(fileIn);
2626
}
2727

28-
// write header info to output file
29-
fwrite(byte, sizeof(unsigned char), 54, fileOut);
28+
// Write header info to output file
29+
fwrite(headerInfo, sizeof(unsigned char), 54, fileOut);
3030

31-
// extract height, width and bitDepth of image from the header information
32-
int height = *(int*)&byte[18];
33-
int width = *(int*)&byte[22];
34-
int bitDepth = *(int*)&byte[28];
31+
// Extract.. of image from the header information
32+
int height = *(int*)&headerInfo[18];
33+
int width = *(int*)&headerInfo[22];
34+
int bitDepth = *(int*)&headerInfo[28];
3535
int size = height * width;
3636

37-
// check if the image has a color table
37+
// Check if the image has a color table
3838
if(bitDepth <= 8) {
39-
// read, and then write the color table from the input file
4039
fread(colorTable, sizeof(unsigned char), 1024, fileIn);
4140
fwrite(colorTable, sizeof(unsigned char), 1024, fileOut);
4241
}
42+
43+
unsigned char chunkBuffer[CHUNK_SIZE];
4344

44-
// array to store the image data in chunks
45-
unsigned char buffer[CHUNK_SIZE];
46-
47-
// read and write the image data in chunks until the end of the file is reached
45+
// Read & write the image data in chunks until the end of file is reached
4846
while(!feof(fileIn)) {
4947

50-
// read a chunk of image data from the input file
51-
size_t bytesRead = fread(buffer, sizeof(unsigned char), CHUNK_SIZE, fileIn);
48+
// Read a chunk of image data from input file
49+
size_t bytesRead = fread(chunkBuffer, sizeof(unsigned char), CHUNK_SIZE, fileIn);
5250

53-
// apply the threshold to each pixel in the chunk
54-
for(i = 0; i < bytesRead; i++) {
55-
buffer[i] = (buffer[i] > THRESHOLD)
51+
// Apply threshold to each pixel in the chunk
52+
for(int i = 0; i < bytesRead; i++) {
53+
chunkBuffer[i] = (chunkBuffer[i] > THRESHOLD)
5654
? WHITE
5755
: BLACK;
5856
}
59-
// write the thresholded image data to the output file
60-
fwrite(buffer, sizeof(unsigned char), bytesRead, fileOut);
57+
// Write the thresholded image data to the output file
58+
fwrite(chunkBuffer, sizeof(unsigned char), bytesRead, fileOut);
6159
}
6260

6361
fclose(fileIn);

C_image_processing/filters/bright_filter.c

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <stdio.h>
22
#include <stdlib.h>
3+
34
#define MAX_COLOR 255
45
#define BRIGHTNESS 25
56
#define CHUNK_SIZE 1024 // define size of the chunks to read and write
@@ -17,54 +18,47 @@ int bright_filter(inputFile, outputFile) {
1718
return 1;
1819
}
1920

20-
int i;
21-
unsigned char byte[54]; // store header info of image
22-
unsigned char colorTable[1024]; // store color table of image
23-
24-
// read the header info of image
25-
for(i = 0; i < 54; i++) {
26-
byte[i] = getc(fileIn);
21+
unsigned char headerInfo[54];
22+
unsigned char colorTable[1024];
23+
24+
for(int i = 0; i < 54; i++) {
25+
headerInfo[i] = getc(fileIn);
2726
}
2827

29-
// write header info to output file
30-
fwrite(byte, sizeof(unsigned char), 54, fileOut);
28+
fwrite(headerInfo, sizeof(unsigned char), 54, fileOut);
3129

32-
// extract height, width and bitDepth of image from header info
33-
int height = *(int*)&byte[18];
34-
int width = *(int*)&byte[22];
35-
int bitDepth = *(int*)&byte[28];
30+
// Extract.. of image from header info
31+
int height = *(int*)&headerInfo[18];
32+
int width = *(int*)&headerInfo[22];
33+
int bitDepth = *(int*)&headerInfo[28];
34+
int pixelsInImage = height * width;
3635

37-
// calculate size of image in pixels
38-
int size = height * width;
39-
40-
// check if image has a color table
36+
// Check if image has a color table
4137
if(bitDepth <= 8) {
42-
// read, then write color table from the input file
4338
fread(colorTable, sizeof(unsigned char), 1024, fileIn);
4439
fwrite(colorTable, sizeof(unsigned char), 1024, fileOut);
4540
}
4641

47-
// array to store image data in chunks
48-
unsigned char buffer[CHUNK_SIZE];
42+
unsigned char chunkBuffer[CHUNK_SIZE];
4943

50-
// read & write image data in chunks until the end of file is reached
44+
// Read & write image data in chunks until the end of file is reached
5145
while(!feof(fileIn)) {
52-
53-
// read a chunk of image data from input file
54-
size_t bytesRead = fread(buffer, sizeof(unsigned char), CHUNK_SIZE, fileIn);
5546

56-
// apply brightness factor to each pixel in the chunk
57-
for (i = 0; i < bytesRead; i++) {
58-
buffer[i] = buffer[i] + BRIGHTNESS;
59-
buffer[i] = (buffer[i] > THRESHOLD) ? MAX_COLOR : buffer[i];
47+
// Read a chunk of image data from the input file
48+
size_t bytesRead = fread(chunkBuffer, sizeof(unsigned char), CHUNK_SIZE, fileIn);
49+
50+
// Apply brightness factor to each pixel in the chunk
51+
for (int i = 0; i < bytesRead; i++) {
52+
chunkBuffer[i] = chunkBuffer[i] + BRIGHTNESS;
53+
chunkBuffer[i] = (chunkBuffer[i] > THRESHOLD) ? MAX_COLOR : chunkBuffer[i];
6054
}
6155

62-
// write thresholded image data to the output file
63-
fwrite(buffer, sizeof(unsigned char), bytesRead, fileOut);
56+
// Write thresholded image data to output file
57+
fwrite(chunkBuffer, sizeof(unsigned char), bytesRead, fileOut);
6458
}
6559

66-
// write thresholded image data to the output file
67-
fwrite(buffer, sizeof(unsigned char), size, fileOut);
60+
// Write thresholded image data to output file
61+
fwrite(chunkBuffer, sizeof(unsigned char), pixelsInImage, fileOut);
6862

6963
fClose(fileIn);
7064
fclose(fileOut);

C_image_processing/filters/dark_filter.c

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <stdio.h>
2+
23
#define MAX_COLOR 255
34
#define THRESHOLD 40 // define threshold value for darkness
45
#define CHUNK_SIZE 1024 // define size of chunks to read and write
@@ -15,53 +16,48 @@ int dark_filter(inputFile, outputFile) {
1516
return 1;
1617
}
1718

18-
int i;
19-
unsigned char byte[54]; // store header info of image
20-
unsigned char colorTable[1024]; // store color table of image
19+
unsigned char headerInfo[54];
20+
unsigned char colorTable[1024];
2121

22-
// read header information of image
23-
for(i = 0; i < 54; i++) {
24-
byte[i] = getc(fileIn);
22+
// Read header info of image
23+
for(int i = 0; i < 54; i++) {
24+
headerInfo[i] = getc(fileIn);
2525
}
2626

27-
// write header info to output file
28-
fwrite(byte, sizeof(unsigned char), 54, fileOut);
29-
30-
// extract height, width and bitDepth of the image from header information
31-
int height = *(int*)&byte[18];
32-
int width = *(int*)&byte[22];
33-
int bitDepth = *(int*)&byte[28];
27+
// Write header info to output file
28+
fwrite(headerInfo, sizeof(unsigned char), 54, fileOut);
3429

35-
// calculate size of image in pixels
36-
int size = height * width;
30+
// Extract.. of the image from header info
31+
int height = *(int*)&headerInfo[18];
32+
int width = *(int*)&headerInfo[22];
33+
int bitDepth = *(int*)&headerInfo[28];
34+
int pixelsInImage = height * width;
3735

38-
// check if image has a color table
36+
// Check if image has a color table
3937
if(bitDepth <= 8) {
40-
// read, then write the color table from input file
4138
fread(colorTable, sizeof(unsigned char), 1024, fileIn);
4239
fwrite(colorTable, sizeof(unsigned char), 1024, fileOut);
4340
}
4441

45-
// array to store image data in chunks
46-
unsigned char buffer[CHUNK_SIZE];
42+
unsigned char chunkBuffer[CHUNK_SIZE];
4743

48-
// read & write image data in chunks until end of the file reached
44+
// Read & write image data in chunks until end of file reached
4945
while(!feof(fileIn)) {
5046

5147
// read a chunk of image data from input file
52-
size_t bytesRead = fread(buffer, sizeof(unsigned char), CHUNK_SIZE, fileIn);
48+
size_t bytesRead = fread(chunkBuffer, sizeof(unsigned char), CHUNK_SIZE, fileIn);
5349

5450
// apply darkness threshold to each pixel in chunk
55-
for (i = 0; i < bytesRead; i++) {
56-
buffer[i] = buffer[i] + THRESHOLD;
57-
buffer[i] = (buffer[i] > THRESHOLD) ? MAX_COLOR : buffer[i];
51+
for (int i = 0; i < bytesRead; i++) {
52+
chunkBuffer[i] = chunkBuffer[i] + THRESHOLD;
53+
chunkBuffer[i] = (chunkBuffer[i] > THRESHOLD) ? MAX_COLOR : chunkBuffer[i];
5854
}
5955
// write thresholded image data to the output file
60-
fwrite(buffer, sizeof(unsigned char), bytesRead, fileOut);
56+
fwrite(chunkBuffer, sizeof(unsigned char), bytesRead, fileOut);
6157
}
6258

63-
// write thresholded image data to the output file
64-
fwrite(buffer, sizeof(unsigned char), size, fileOut);
59+
// Write thresholded image data to output file
60+
fwrite(chunkBuffer, sizeof(unsigned char), pixelsInImage, fileOut);
6561

6662
fClose(fileIn);
6763
fclose(fileOut);

C_image_processing/filters/negative_filter.c

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,45 +14,46 @@ int negative_filter(const char *inputFile, const char *outputFile) {
1414
return 1;
1515
}
1616

17-
unsigned char *imageData;
18-
unsigned char *newImageData;
19-
unsigned char imageHeader[54];
17+
unsigned char *imageData = NULL;
18+
unsigned char *newImageData = NULL;
19+
unsigned char headerInfo[54];
2020
unsigned char colorTable[1024];
21-
22-
int i, j;
2321

24-
// check if input file exists
25-
fread(imageHeader, sizeof(unsigned char), 54, fileIn);
26-
int width = *(int*)&imageHeader[18];
27-
int height = *(int*)&imageHeader[22];
28-
int bitDepth = *(int*)&imageHeader[28];
22+
// Read image header
23+
fread(headerInfo, sizeof(unsigned char), 54, fileIn);
24+
int width = *(int*)&headerInfo[18];
25+
int height = *(int*)&headerInfo[22];
26+
int bitDepth = *(int*)&headerInfo[28];
2927
int imageDataSize = width * height;
3028

29+
// Allocate memory for image data
3130
imageData = (unsigned char*)malloc(imageDataSize * sizeof(unsigned char));
3231
newImageData = (unsigned char*)malloc(imageDataSize * sizeof(unsigned char));
3332

34-
// check if image has a color table
33+
// Read color table if present
3534
if (bitDepth <= 8) {
3635
fread(colorTable, sizeof(unsigned char), 1024, fileIn);
3736
}
3837

38+
// Read original image data
3939
fread(imageData, sizeof(unsigned char), imageDataSize, fileIn);
4040

41-
// apply negative filter to each pixel
41+
// Apply negative filter to each pixel
4242
unsigned char *p = imageData;
4343
unsigned char *q = newImageData;
44-
for (i = 0; i < height * width; i++) {
44+
for (int i = 0; i < height * width; i++) {
4545
*q++ = 255 - *p++;
4646
}
4747

48-
// write image data to output file
49-
fwrite(imageHeader, sizeof(unsigned char), 54, fileOut);
48+
// Write image data to output file
49+
fwrite(headerInfo, sizeof(unsigned char), 54, fileOut);
5050
if (bitDepth <= 8) {
5151
fwrite(colorTable, sizeof(unsigned char), 1024, fileOut);
5252
}
53-
53+
5454
fwrite(newImageData, sizeof(unsigned char), imageDataSize, fileOut);
5555

56+
// Clean up and close files
5657
fclose(fileIn);
5758
fclose(fileOut);
5859

C_image_processing/filters/rgb_to_gray_filter.c

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ int rgb_to_gray_filter(inputFile, outputFile) {
44

55
FILE *fileIn = fopen(inputFile, "rb");
66
FILE *fileOut = fopen(outputFile, "wb+");
7-
8-
int i;
9-
unsigned char byte[54]; // store header info
107

118
if (fileIn == NULL || fileOut == NULL) {
129
printf("File does not exist.\n");
@@ -15,42 +12,43 @@ int rgb_to_gray_filter(inputFile, outputFile) {
1512
return 1;
1613
}
1714

18-
// read header info of image
19-
for(i = 0; i < 54; i++) {
20-
byte[i] = getc(fileIn);
21-
}
22-
23-
// write header info to output file
24-
fwrite(byte, sizeof(unsigned char), 54, fileOut);
15+
// Read and write header info
16+
unsigned char headerInfo[54];
17+
fread(headerInfo, sizeof(unsigned char), 54, fileIn);
18+
fwrite(headerInfo, sizeof(unsigned char), 54, fileOut);
2519

26-
// extract height, width & bitDepth of image from header info
27-
int height = *(int*)&byte[18];
28-
int width = *(int*)&byte[22];
29-
int bitDepth = *(int*)&byte[28];
30-
int size = height * width;
20+
// Extract image dimensions from header info
21+
int height = *(int*)&headerInfo[18];
22+
int width = *(int*)&headerInfo[22];
23+
int bitDepth = *(int*)&headerInfo[28];
24+
int pixelsInImage = height * width;
3125

32-
unsigned char (*buffer)[3] = malloc(size * sizeof(*buffer)); // store image data
26+
// Allocate Memory to Image Data
27+
unsigned char (*buffer)[3] = malloc(pixelsInImage * sizeof(*buffer));
3328
if (buffer == NULL) {
3429
printf("Memory allocation failed.\n");
30+
fclose(fileIn);
31+
fclose(fileOut);
3532
return 1;
3633
}
3734

38-
unsigned char y;
39-
for(i = 0; i < size; i ++) {
40-
y = 0;
41-
buffer[i][0] = getc(fileIn); // red
42-
buffer[i][1] = getc(fileIn); // green
43-
buffer[i][2] = getc(fileIn); // blue
44-
45-
// RGB to gray
46-
y = (buffer[i][0] * 0.3) + (buffer[i][2] * 0.11);
47-
48-
// Triplicate grayscale value for three color channels
49-
putc(y, fileOut);
50-
putc(y, fileOut);
51-
putc(y, fileOut);
35+
for (int i = 0; i < pixelsInImage; i++) {
36+
37+
// read RGB color components of current pixel from input file
38+
buffer[i][0] = getc(fileIn); // Red
39+
buffer[i][1] = getc(fileIn); // Green
40+
buffer[i][2] = getc(fileIn); // Blue
41+
42+
// calculate grayscale value using weighted average of red and blue components
43+
unsigned char grayscaleValue = (buffer[i][0] * 0.3) + (buffer[i][2] * 0.11);
44+
45+
// write the calculated grayscale value to the output file for each RGB channel
46+
putc(grayscaleValue, fileOut); // Red
47+
putc(grayscaleValue, fileOut); // Green
48+
putc(grayscaleValue, fileOut); // Blue
5249
}
5350

51+
// Clean up / Close
5452
fClose(fileIn);
5553
fclose(fileOut);
5654
return 0;

0 commit comments

Comments
 (0)