Skip to content

Commit 7a5238c

Browse files
authored
Merge pull request #1 from VerzatileDev/main
Refactoring and Structural Improvements
2 parents c168663 + cae29c5 commit 7a5238c

File tree

15 files changed

+506
-301
lines changed

15 files changed

+506
-301
lines changed

C_image_processing/.gitignore

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# File created using '.gitignore Generator' for Visual Studio Code: https://bit.ly/vscode-gig
2+
# Created by https://www.toptal.com/developers/gitignore/api/windows,visualstudiocode,c,c++
3+
# Edit at https://www.toptal.com/developers/gitignore?templates=windows,visualstudiocode,c,c++
4+
5+
### C ###
6+
# Prerequisites
7+
*.d
8+
9+
# Object files
10+
*.o
11+
*.ko
12+
*.obj
13+
*.elf
14+
15+
# Linker output
16+
*.ilk
17+
*.map
18+
*.exp
19+
20+
# Precompiled Headers
21+
*.gch
22+
*.pch
23+
24+
# Libraries
25+
*.lib
26+
*.a
27+
*.la
28+
*.lo
29+
30+
# Shared objects (inc. Windows DLLs)
31+
*.dll
32+
*.so
33+
*.so.*
34+
*.dylib
35+
36+
# Executables
37+
*.exe
38+
*.out
39+
*.app
40+
*.i*86
41+
*.x86_64
42+
*.hex
43+
44+
# Debug files
45+
*.dSYM/
46+
*.su
47+
*.idb
48+
*.pdb
49+
50+
# Kernel Module Compile Results
51+
*.mod*
52+
*.cmd
53+
.tmp_versions/
54+
modules.order
55+
Module.symvers
56+
Mkfile.old
57+
dkms.conf
58+
59+
### C++ ###
60+
# Prerequisites
61+
62+
# Compiled Object files
63+
*.slo
64+
65+
# Precompiled Headers
66+
67+
# Compiled Dynamic libraries
68+
69+
# Fortran module files
70+
*.mod
71+
*.smod
72+
73+
# Compiled Static libraries
74+
*.lai
75+
76+
# Executables
77+
78+
### VisualStudioCode ###
79+
.vscode/*
80+
!.vscode/settings.json
81+
!.vscode/tasks.json
82+
!.vscode/launch.json
83+
!.vscode/extensions.json
84+
!.vscode/*.code-snippets
85+
86+
# Local History for Visual Studio Code
87+
.history/
88+
89+
# Built Visual Studio Code Extensions
90+
*.vsix
91+
92+
### VisualStudioCode Patch ###
93+
# Ignore all local history of files
94+
.history
95+
.ionide
96+
97+
### Windows ###
98+
# Windows thumbnail cache files
99+
Thumbs.db
100+
Thumbs.db:encryptable
101+
ehthumbs.db
102+
ehthumbs_vista.db
103+
104+
# Dump file
105+
*.stackdump
106+
107+
# Folder config file
108+
[Dd]esktop.ini
109+
110+
# Recycle Bin used on file shares
111+
$RECYCLE.BIN/
112+
113+
# Windows Installer files
114+
*.cab
115+
*.msi
116+
*.msix
117+
*.msm
118+
*.msp
119+
120+
# Windows shortcuts
121+
*.lnk
122+
123+
# End of https://www.toptal.com/developers/gitignore/api/windows,visualstudiocode,c,c++
124+
125+
# Custom rules (everything added below won't be overriden by 'Generate .gitignore File' if you use 'Update' option)
126+
output.bmp
File renamed without changes.
Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,55 @@
11
#include <stdio.h>
2-
#define THRESHOLD 128 // define tha value of the threshold for black and white
3-
#define WHITE 255 // define the value for white pixels
4-
#define BLACK 0 // define the value for black pixels
5-
#define CHUNK_SIZE 1024 // define the size of the chunks to read and write
6-
7-
int black_and_white_filter(inputFile, outputFile) {
8-
FILE *fileIn = fopen(inputFile, "rb"); // open the input file for reading in binary mode
9-
FILE *fileOut = fopen(outputFile, "wb+"); // create the output file for writing in binary mode
10-
int i; // variable to iterate through the image data
11-
unsigned char byte[54]; // array to store the header information of the image
12-
unsigned char colorTable[1024]; // array to store the color table of the image
13-
// check if the input file exists
14-
if(fileIn == NULL) {
2+
#define THRESHOLD 128 // define value of threshold for black and white
3+
#define WHITE 255
4+
#define BLACK 0
5+
#define CHUNK_SIZE 1024 // define size of chunks to read and write
6+
7+
int black_and_white_filter(const char *inputFile, const char *outputFile) {
8+
9+
FILE *fileIn = fopen(inputFile, "rb");
10+
FILE *fileOut = fopen(outputFile, "wb+");
11+
12+
if (fileIn == NULL || fileOut == NULL) {
1513
printf("File does not exist.\n");
14+
if (fileIn != NULL) fclose(fileIn);
15+
if (fileOut != NULL) fclose(fileOut);
1616
return 1;
1717
}
18-
// read the header information of the image
18+
19+
int i;
20+
unsigned char byte[54];
21+
unsigned char colorTable[1024];
22+
23+
// read header info of image
1924
for(i = 0; i < 54; i++) {
2025
byte[i] = getc(fileIn);
2126
}
22-
// write the header information to the output file
27+
28+
// write header info to output file
2329
fwrite(byte, sizeof(unsigned char), 54, fileOut);
24-
// extract the height, width and bitDepth of the image from the header information
30+
31+
// extract height, width and bitDepth of image from the header information
2532
int height = *(int*)&byte[18];
2633
int width = *(int*)&byte[22];
2734
int bitDepth = *(int*)&byte[28];
28-
// calculate the size of the image in pixels
2935
int size = height * width;
36+
3037
// check if the image has a color table
3138
if(bitDepth <= 8) {
3239
// read, and then write the color table from the input file
3340
fread(colorTable, sizeof(unsigned char), 1024, fileIn);
3441
fwrite(colorTable, sizeof(unsigned char), 1024, fileOut);
3542
}
43+
3644
// array to store the image data in chunks
3745
unsigned char buffer[CHUNK_SIZE];
46+
3847
// read and write the image data in chunks until the end of the file is reached
3948
while(!feof(fileIn)) {
49+
4050
// read a chunk of image data from the input file
4151
size_t bytesRead = fread(buffer, sizeof(unsigned char), CHUNK_SIZE, fileIn);
52+
4253
// apply the threshold to each pixel in the chunk
4354
for(i = 0; i < bytesRead; i++) {
4455
buffer[i] = (buffer[i] > THRESHOLD)
@@ -48,9 +59,8 @@ int black_and_white_filter(inputFile, outputFile) {
4859
// write the thresholded image data to the output file
4960
fwrite(buffer, sizeof(unsigned char), bytesRead, fileOut);
5061
}
51-
// close the input and output files
52-
fClose(fileIn);
62+
63+
fclose(fileIn);
5364
fclose(fileOut);
54-
// exit
5565
return 0;
5666
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#define MAX_COLOR 255
4+
#define BRIGHTNESS 25
5+
#define CHUNK_SIZE 1024 // define size of the chunks to read and write
6+
#define THRESHOLD 128 // define threshold value for the brightness condition
7+
8+
int bright_filter(inputFile, outputFile) {
9+
10+
FILE *fileIn = fopen(inputFile, "rb");
11+
FILE *fileOut = fopen(outputFile, "wb+");
12+
13+
if (fileIn == NULL || fileOut == NULL) {
14+
printf("File does not exist.\n");
15+
if (fileIn != NULL) fclose(fileIn);
16+
if (fileOut != NULL) fclose(fileOut);
17+
return 1;
18+
}
19+
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);
27+
}
28+
29+
// write header info to output file
30+
fwrite(byte, sizeof(unsigned char), 54, fileOut);
31+
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];
36+
37+
// calculate size of image in pixels
38+
int size = height * width;
39+
40+
// check if image has a color table
41+
if(bitDepth <= 8) {
42+
// read, then write color table from the input file
43+
fread(colorTable, sizeof(unsigned char), 1024, fileIn);
44+
fwrite(colorTable, sizeof(unsigned char), 1024, fileOut);
45+
}
46+
47+
// array to store image data in chunks
48+
unsigned char buffer[CHUNK_SIZE];
49+
50+
// read & write image data in chunks until the end of file is reached
51+
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);
55+
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];
60+
}
61+
62+
// write thresholded image data to the output file
63+
fwrite(buffer, sizeof(unsigned char), bytesRead, fileOut);
64+
}
65+
66+
// write thresholded image data to the output file
67+
fwrite(buffer, sizeof(unsigned char), size, fileOut);
68+
69+
fClose(fileIn);
70+
fclose(fileOut);
71+
return 0;
72+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <stdio.h>
2+
#define MAX_COLOR 255
3+
#define THRESHOLD 40 // define threshold value for darkness
4+
#define CHUNK_SIZE 1024 // define size of chunks to read and write
5+
6+
int dark_filter(inputFile, outputFile) {
7+
8+
FILE *fileIn = fopen(inputFile, "rb");
9+
FILE *fileOut = fopen(outputFile, "wb+");
10+
11+
if (fileIn == NULL || fileOut == NULL) {
12+
printf("File does not exist.\n");
13+
if (fileIn != NULL) fclose(fileIn);
14+
if (fileOut != NULL) fclose(fileOut);
15+
return 1;
16+
}
17+
18+
int i;
19+
unsigned char byte[54]; // store header info of image
20+
unsigned char colorTable[1024]; // store color table of image
21+
22+
// read header information of image
23+
for(i = 0; i < 54; i++) {
24+
byte[i] = getc(fileIn);
25+
}
26+
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];
34+
35+
// calculate size of image in pixels
36+
int size = height * width;
37+
38+
// check if image has a color table
39+
if(bitDepth <= 8) {
40+
// read, then write the color table from input file
41+
fread(colorTable, sizeof(unsigned char), 1024, fileIn);
42+
fwrite(colorTable, sizeof(unsigned char), 1024, fileOut);
43+
}
44+
45+
// array to store image data in chunks
46+
unsigned char buffer[CHUNK_SIZE];
47+
48+
// read & write image data in chunks until end of the file reached
49+
while(!feof(fileIn)) {
50+
51+
// read a chunk of image data from input file
52+
size_t bytesRead = fread(buffer, sizeof(unsigned char), CHUNK_SIZE, fileIn);
53+
54+
// 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];
58+
}
59+
// write thresholded image data to the output file
60+
fwrite(buffer, sizeof(unsigned char), bytesRead, fileOut);
61+
}
62+
63+
// write thresholded image data to the output file
64+
fwrite(buffer, sizeof(unsigned char), size, fileOut);
65+
66+
fClose(fileIn);
67+
fclose(fileOut);
68+
return 0;
69+
}

0 commit comments

Comments
 (0)