Skip to content

Commit 4c3e9ec

Browse files
authored
Merge pull request #4 from Sommos/main - Stable
added basic GTK+4.0 UI, and modified README.md
2 parents 23e72a3 + 9ff0068 commit 4c3e9ec

15 files changed

+362
-128
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,9 @@ $RECYCLE.BIN/
123123

124124
# Custom rules (everything added below won't be overriden by 'Generate .gitignore File' if you use 'Update' option)
125125
output.bmp
126+
test_image_black_and_white.bmp
127+
test_image_bright.bmp
128+
test_image_dark.bmp
129+
test_image_negative.bmp
130+
test_image_rgb_to_gray.bmp
131+
test_image_sepia.bmp
26.2 KB
Binary file not shown.
27.2 KB
Binary file not shown.

C_image_processing/filters/black_and_white_filter.c

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,60 +5,68 @@
55
#define BLACK 0
66
#define CHUNK_SIZE 1024 // define size of chunks to read and write
77

8-
int black_and_white_filter(const char *inputFile, const char *outputFile) {
9-
8+
int black_and_white_filter(const char *inputFile, const char *outputFile)
9+
{
1010
FILE *fileIn = fopen(inputFile, "rb");
1111
FILE *fileOut = fopen(outputFile, "wb+");
1212

13-
if (fileIn == NULL || fileOut == NULL) {
13+
if (fileIn == NULL || fileOut == NULL)
14+
{
1415
printf("File does not exist.\n");
15-
if (fileIn != NULL) fclose(fileIn);
16-
if (fileOut != NULL) fclose(fileOut);
16+
if (fileIn != NULL)
17+
fclose(fileIn);
18+
if (fileOut != NULL)
19+
fclose(fileOut);
1720
return 1;
1821
}
1922

2023
unsigned char headerInfo[54];
2124
unsigned char colorTable[1024];
2225

2326
// Read header info of image
24-
for(int i = 0; i < 54; i++) {
27+
for (int i = 0; i < 54; i++)
28+
{
2529
headerInfo[i] = getc(fileIn);
2630
}
2731

2832
// Write header info to output file
2933
fwrite(headerInfo, sizeof(unsigned char), 54, fileOut);
3034

3135
// 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];
36+
int height = *(int *)&headerInfo[18];
37+
int width = *(int *)&headerInfo[22];
38+
int bitDepth = *(int *)&headerInfo[28];
3539
int size = height * width;
3640

3741
// Check if the image has a color table
38-
if(bitDepth <= 8) {
42+
if (bitDepth <= 8)
43+
{
3944
fread(colorTable, sizeof(unsigned char), 1024, fileIn);
4045
fwrite(colorTable, sizeof(unsigned char), 1024, fileOut);
4146
}
42-
47+
4348
unsigned char chunkBuffer[CHUNK_SIZE];
4449

4550
// Read & write the image data in chunks until the end of file is reached
46-
while(!feof(fileIn)) {
47-
51+
while (!feof(fileIn))
52+
{
53+
4854
// Read a chunk of image data from input file
4955
size_t bytesRead = fread(chunkBuffer, sizeof(unsigned char), CHUNK_SIZE, fileIn);
50-
56+
5157
// Apply threshold to each pixel in the chunk
52-
for(int i = 0; i < bytesRead; i++) {
58+
for (int i = 0; i < bytesRead; i++)
59+
{
5360
chunkBuffer[i] = (chunkBuffer[i] > THRESHOLD)
54-
? WHITE
55-
: BLACK;
61+
? WHITE
62+
: BLACK;
5663
}
5764
// Write the thresholded image data to the output file
5865
fwrite(chunkBuffer, sizeof(unsigned char), bytesRead, fileOut);
5966
}
6067

6168
fclose(fileIn);
6269
fclose(fileOut);
70+
6371
return 0;
6472
}

C_image_processing/filters/bright_filter.c

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,56 @@
66
#define CHUNK_SIZE 1024 // define size of the chunks to read and write
77
#define THRESHOLD 128 // define threshold value for the brightness condition
88

9-
int bright_filter(inputFile, outputFile) {
10-
9+
int bright_filter(const char *inputFile, const char *outputFile)
10+
{
1111
FILE *fileIn = fopen(inputFile, "rb");
1212
FILE *fileOut = fopen(outputFile, "wb+");
13-
14-
if (fileIn == NULL || fileOut == NULL) {
13+
14+
if (fileIn == NULL || fileOut == NULL)
15+
{
1516
printf("File does not exist.\n");
16-
if (fileIn != NULL) fclose(fileIn);
17-
if (fileOut != NULL) fclose(fileOut);
17+
if (fileIn != NULL)
18+
fclose(fileIn);
19+
if (fileOut != NULL)
20+
fclose(fileOut);
1821
return 1;
1922
}
2023

2124
unsigned char headerInfo[54];
2225
unsigned char colorTable[1024];
23-
24-
for(int i = 0; i < 54; i++) {
26+
27+
for (int i = 0; i < 54; i++)
28+
{
2529
headerInfo[i] = getc(fileIn);
2630
}
2731

2832
fwrite(headerInfo, sizeof(unsigned char), 54, fileOut);
2933

3034
// Extract.. of image from header info
31-
int height = *(int*)&headerInfo[18];
32-
int width = *(int*)&headerInfo[22];
33-
int bitDepth = *(int*)&headerInfo[28];
35+
int height = *(int *)&headerInfo[18];
36+
int width = *(int *)&headerInfo[22];
37+
int bitDepth = *(int *)&headerInfo[28];
3438
int pixelsInImage = height * width;
3539

3640
// Check if image has a color table
37-
if(bitDepth <= 8) {
41+
if (bitDepth <= 8)
42+
{
3843
fread(colorTable, sizeof(unsigned char), 1024, fileIn);
3944
fwrite(colorTable, sizeof(unsigned char), 1024, fileOut);
4045
}
4146

4247
unsigned char chunkBuffer[CHUNK_SIZE];
4348

4449
// Read & write image data in chunks until the end of file is reached
45-
while(!feof(fileIn)) {
46-
50+
while (!feof(fileIn))
51+
{
52+
4753
// Read a chunk of image data from the input file
4854
size_t bytesRead = fread(chunkBuffer, sizeof(unsigned char), CHUNK_SIZE, fileIn);
49-
55+
5056
// Apply brightness factor to each pixel in the chunk
51-
for (int i = 0; i < bytesRead; i++) {
57+
for (int i = 0; i < bytesRead; i++)
58+
{
5259
chunkBuffer[i] = chunkBuffer[i] + BRIGHTNESS;
5360
chunkBuffer[i] = (chunkBuffer[i] > THRESHOLD) ? MAX_COLOR : chunkBuffer[i];
5461
}
@@ -59,8 +66,9 @@ int bright_filter(inputFile, outputFile) {
5966

6067
// Write thresholded image data to output file
6168
fwrite(chunkBuffer, sizeof(unsigned char), pixelsInImage, fileOut);
62-
63-
fClose(fileIn);
69+
70+
fclose(fileIn);
6471
fclose(fileOut);
72+
6573
return 0;
6674
}
Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,63 @@
11
#include <stdio.h>
22

33
#define MAX_COLOR 255
4-
#define THRESHOLD 40 // define threshold value for darkness
5-
#define CHUNK_SIZE 1024 // define size of chunks to read and write
4+
#define DARK_THRESHOLD 40
5+
#define CHUNK_SIZE 1024
66

7-
int dark_filter(inputFile, outputFile) {
8-
7+
int dark_filter(const char *inputFile, const char *outputFile)
8+
{
99
FILE *fileIn = fopen(inputFile, "rb");
1010
FILE *fileOut = fopen(outputFile, "wb+");
11-
12-
if (fileIn == NULL || fileOut == NULL) {
11+
12+
if (fileIn == NULL || fileOut == NULL)
13+
{
1314
printf("File does not exist.\n");
14-
if (fileIn != NULL) fclose(fileIn);
15-
if (fileOut != NULL) fclose(fileOut);
15+
if (fileIn != NULL)
16+
fclose(fileIn);
17+
if (fileOut != NULL)
18+
fclose(fileOut);
1619
return 1;
1720
}
1821

1922
unsigned char headerInfo[54];
2023
unsigned char colorTable[1024];
2124

2225
// Read header info of image
23-
for(int i = 0; i < 54; i++) {
26+
for (int i = 0; i < 54; i++)
27+
{
2428
headerInfo[i] = getc(fileIn);
2529
}
2630

2731
// Write header info to output file
2832
fwrite(headerInfo, sizeof(unsigned char), 54, fileOut);
2933

3034
// 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];
35+
int height = *(int *)&headerInfo[18];
36+
int width = *(int *)&headerInfo[22];
37+
int bitDepth = *(int *)&headerInfo[28];
3438
int pixelsInImage = height * width;
3539

3640
// Check if image has a color table
37-
if(bitDepth <= 8) {
41+
if (bitDepth <= 8)
42+
{
3843
fread(colorTable, sizeof(unsigned char), 1024, fileIn);
3944
fwrite(colorTable, sizeof(unsigned char), 1024, fileOut);
4045
}
4146

4247
unsigned char chunkBuffer[CHUNK_SIZE];
4348

4449
// Read & write image data in chunks until end of file reached
45-
while(!feof(fileIn)) {
50+
while (!feof(fileIn))
51+
{
4652

4753
// read a chunk of image data from input file
4854
size_t bytesRead = fread(chunkBuffer, sizeof(unsigned char), CHUNK_SIZE, fileIn);
49-
55+
5056
// apply darkness threshold to each pixel in chunk
51-
for (int i = 0; i < bytesRead; i++) {
52-
chunkBuffer[i] = chunkBuffer[i] + THRESHOLD;
53-
chunkBuffer[i] = (chunkBuffer[i] > THRESHOLD) ? MAX_COLOR : chunkBuffer[i];
57+
for (int i = 0; i < bytesRead; i++)
58+
{
59+
chunkBuffer[i] = chunkBuffer[i] + DARK_THRESHOLD;
60+
chunkBuffer[i] = (chunkBuffer[i] > DARK_THRESHOLD) ? MAX_COLOR : chunkBuffer[i];
5461
}
5562
// write thresholded image data to the output file
5663
fwrite(chunkBuffer, sizeof(unsigned char), bytesRead, fileOut);
@@ -59,7 +66,8 @@ int dark_filter(inputFile, outputFile) {
5966
// Write thresholded image data to output file
6067
fwrite(chunkBuffer, sizeof(unsigned char), pixelsInImage, fileOut);
6168

62-
fClose(fileIn);
69+
fclose(fileIn);
6370
fclose(fileOut);
71+
6472
return 0;
6573
}

C_image_processing/filters/negative_filter.c

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,58 @@
22
#include <stdlib.h>
33
#include <time.h>
44

5-
int negative_filter(const char *inputFile, const char *outputFile) {
6-
5+
int negative_filter(const char *inputFile, const char *outputFile)
6+
{
77
FILE *fileIn = fopen(inputFile, "rb");
88
FILE *fileOut = fopen(outputFile, "wb+");
99

10-
if (fileIn == NULL || fileOut == NULL) {
10+
if (fileIn == NULL || fileOut == NULL)
11+
{
1112
printf("File does not exist.\n");
12-
if (fileIn != NULL) fclose(fileIn);
13-
if (fileOut != NULL) fclose(fileOut);
13+
if (fileIn != NULL)
14+
fclose(fileIn);
15+
if (fileOut != NULL)
16+
fclose(fileOut);
1417
return 1;
1518
}
16-
19+
1720
unsigned char *imageData = NULL;
1821
unsigned char *newImageData = NULL;
1922
unsigned char headerInfo[54];
2023
unsigned char colorTable[1024];
21-
24+
2225
// Read image header
2326
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];
27+
int width = *(int *)&headerInfo[18];
28+
int height = *(int *)&headerInfo[22];
29+
int bitDepth = *(int *)&headerInfo[28];
2730
int imageDataSize = width * height;
28-
31+
2932
// Allocate memory for image data
30-
imageData = (unsigned char*)malloc(imageDataSize * sizeof(unsigned char));
31-
newImageData = (unsigned char*)malloc(imageDataSize * sizeof(unsigned char));
32-
33+
imageData = (unsigned char *)malloc(imageDataSize * sizeof(unsigned char));
34+
newImageData = (unsigned char *)malloc(imageDataSize * sizeof(unsigned char));
35+
3336
// Read color table if present
34-
if (bitDepth <= 8) {
37+
if (bitDepth <= 8)
38+
{
3539
fread(colorTable, sizeof(unsigned char), 1024, fileIn);
3640
}
37-
41+
3842
// Read original image data
3943
fread(imageData, sizeof(unsigned char), imageDataSize, fileIn);
4044

4145
// Apply negative filter to each pixel
4246
unsigned char *p = imageData;
4347
unsigned char *q = newImageData;
44-
for (int i = 0; i < height * width; i++) {
48+
for (int i = 0; i < height * width; i++)
49+
{
4550
*q++ = 255 - *p++;
4651
}
4752

4853
// Write image data to output file
4954
fwrite(headerInfo, sizeof(unsigned char), 54, fileOut);
50-
if (bitDepth <= 8) {
55+
if (bitDepth <= 8)
56+
{
5157
fwrite(colorTable, sizeof(unsigned char), 1024, fileOut);
5258
}
5359

@@ -56,8 +62,9 @@ int negative_filter(const char *inputFile, const char *outputFile) {
5662
// Clean up and close files
5763
fclose(fileIn);
5864
fclose(fileOut);
59-
65+
6066
free(imageData);
6167
free(newImageData);
68+
6269
return 0;
6370
}

0 commit comments

Comments
 (0)