Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions C_image_processing/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# File created using '.gitignore Generator' for Visual Studio Code: https://bit.ly/vscode-gig
# Created by https://www.toptal.com/developers/gitignore/api/windows,visualstudiocode,c,c++
# Edit at https://www.toptal.com/developers/gitignore?templates=windows,visualstudiocode,c,c++

### C ###
# Prerequisites
*.d

# Object files
*.o
*.ko
*.obj
*.elf

# Linker output
*.ilk
*.map
*.exp

# Precompiled Headers
*.gch
*.pch

# Libraries
*.lib
*.a
*.la
*.lo

# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib

# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex

# Debug files
*.dSYM/
*.su
*.idb
*.pdb

# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf

### C++ ###
# Prerequisites

# Compiled Object files
*.slo

# Precompiled Headers

# Compiled Dynamic libraries

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai

# Executables

### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/*.code-snippets

# Local History for Visual Studio Code
.history/

# Built Visual Studio Code Extensions
*.vsix

### VisualStudioCode Patch ###
# Ignore all local history of files
.history
.ionide

### Windows ###
# Windows thumbnail cache files
Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
ehthumbs_vista.db

# Dump file
*.stackdump

# Folder config file
[Dd]esktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp

# Windows shortcuts
*.lnk

# End of https://www.toptal.com/developers/gitignore/api/windows,visualstudiocode,c,c++

# Custom rules (everything added below won't be overriden by 'Generate .gitignore File' if you use 'Update' option)
output.bmp
Original file line number Diff line number Diff line change
@@ -1,44 +1,55 @@
#include <stdio.h>
#define THRESHOLD 128 // define tha value of the threshold for black and white
#define WHITE 255 // define the value for white pixels
#define BLACK 0 // define the value for black pixels
#define CHUNK_SIZE 1024 // define the size of the chunks to read and write

int black_and_white_filter(inputFile, outputFile) {
FILE *fileIn = fopen(inputFile, "rb"); // open the input file for reading in binary mode
FILE *fileOut = fopen(outputFile, "wb+"); // create the output file for writing in binary mode
int i; // variable to iterate through the image data
unsigned char byte[54]; // array to store the header information of the image
unsigned char colorTable[1024]; // array to store the color table of the image
// check if the input file exists
if(fileIn == NULL) {
#define THRESHOLD 128 // define value of threshold for black and white
#define WHITE 255
#define BLACK 0
#define CHUNK_SIZE 1024 // define size of chunks to read and write

int black_and_white_filter(const char *inputFile, const char *outputFile) {

FILE *fileIn = fopen(inputFile, "rb");
FILE *fileOut = fopen(outputFile, "wb+");

if (fileIn == NULL || fileOut == NULL) {
printf("File does not exist.\n");
if (fileIn != NULL) fclose(fileIn);
if (fileOut != NULL) fclose(fileOut);
return 1;
}
// read the header information of the image

int i;
unsigned char byte[54];
unsigned char colorTable[1024];

// read header info of image
for(i = 0; i < 54; i++) {
byte[i] = getc(fileIn);
}
// write the header information to the output file

// write header info to output file
fwrite(byte, sizeof(unsigned char), 54, fileOut);
// extract the height, width and bitDepth of the image from the header information

// extract height, width and bitDepth of image from the header information
int height = *(int*)&byte[18];
int width = *(int*)&byte[22];
int bitDepth = *(int*)&byte[28];
// calculate the size of the image in pixels
int size = height * width;

// check if the image has a color table
if(bitDepth <= 8) {
// read, and then write the color table from the input file
fread(colorTable, sizeof(unsigned char), 1024, fileIn);
fwrite(colorTable, sizeof(unsigned char), 1024, fileOut);
}

// array to store the image data in chunks
unsigned char buffer[CHUNK_SIZE];

// read and write the image data in chunks until the end of the file is reached
while(!feof(fileIn)) {

// read a chunk of image data from the input file
size_t bytesRead = fread(buffer, sizeof(unsigned char), CHUNK_SIZE, fileIn);

// apply the threshold to each pixel in the chunk
for(i = 0; i < bytesRead; i++) {
buffer[i] = (buffer[i] > THRESHOLD)
Expand All @@ -48,9 +59,8 @@ int black_and_white_filter(inputFile, outputFile) {
// write the thresholded image data to the output file
fwrite(buffer, sizeof(unsigned char), bytesRead, fileOut);
}
// close the input and output files
fClose(fileIn);

fclose(fileIn);
fclose(fileOut);
// exit
return 0;
}
72 changes: 72 additions & 0 deletions C_image_processing/filters/bright_filter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include <stdio.h>
#include <stdlib.h>
#define MAX_COLOR 255
#define BRIGHTNESS 25
#define CHUNK_SIZE 1024 // define size of the chunks to read and write
#define THRESHOLD 128 // define threshold value for the brightness condition

int bright_filter(inputFile, outputFile) {

FILE *fileIn = fopen(inputFile, "rb");
FILE *fileOut = fopen(outputFile, "wb+");

if (fileIn == NULL || fileOut == NULL) {
printf("File does not exist.\n");
if (fileIn != NULL) fclose(fileIn);
if (fileOut != NULL) fclose(fileOut);
return 1;
}

int i;
unsigned char byte[54]; // store header info of image
unsigned char colorTable[1024]; // store color table of image

// read the header info of image
for(i = 0; i < 54; i++) {
byte[i] = getc(fileIn);
}

// write header info to output file
fwrite(byte, sizeof(unsigned char), 54, fileOut);

// extract height, width and bitDepth of image from header info
int height = *(int*)&byte[18];
int width = *(int*)&byte[22];
int bitDepth = *(int*)&byte[28];

// calculate size of image in pixels
int size = height * width;

// check if image has a color table
if(bitDepth <= 8) {
// read, then write color table from the input file
fread(colorTable, sizeof(unsigned char), 1024, fileIn);
fwrite(colorTable, sizeof(unsigned char), 1024, fileOut);
}

// array to store image data in chunks
unsigned char buffer[CHUNK_SIZE];

// read & write image data in chunks until the end of file is reached
while(!feof(fileIn)) {

// read a chunk of image data from input file
size_t bytesRead = fread(buffer, sizeof(unsigned char), CHUNK_SIZE, fileIn);

// apply brightness factor to each pixel in the chunk
for (i = 0; i < bytesRead; i++) {
buffer[i] = buffer[i] + BRIGHTNESS;
buffer[i] = (buffer[i] > THRESHOLD) ? MAX_COLOR : buffer[i];
}

// write thresholded image data to the output file
fwrite(buffer, sizeof(unsigned char), bytesRead, fileOut);
}

// write thresholded image data to the output file
fwrite(buffer, sizeof(unsigned char), size, fileOut);

fClose(fileIn);
fclose(fileOut);
return 0;
}
69 changes: 69 additions & 0 deletions C_image_processing/filters/dark_filter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <stdio.h>
#define MAX_COLOR 255
#define THRESHOLD 40 // define threshold value for darkness
#define CHUNK_SIZE 1024 // define size of chunks to read and write

int dark_filter(inputFile, outputFile) {

FILE *fileIn = fopen(inputFile, "rb");
FILE *fileOut = fopen(outputFile, "wb+");

if (fileIn == NULL || fileOut == NULL) {
printf("File does not exist.\n");
if (fileIn != NULL) fclose(fileIn);
if (fileOut != NULL) fclose(fileOut);
return 1;
}

int i;
unsigned char byte[54]; // store header info of image
unsigned char colorTable[1024]; // store color table of image

// read header information of image
for(i = 0; i < 54; i++) {
byte[i] = getc(fileIn);
}

// write header info to output file
fwrite(byte, sizeof(unsigned char), 54, fileOut);

// extract height, width and bitDepth of the image from header information
int height = *(int*)&byte[18];
int width = *(int*)&byte[22];
int bitDepth = *(int*)&byte[28];

// calculate size of image in pixels
int size = height * width;

// check if image has a color table
if(bitDepth <= 8) {
// read, then write the color table from input file
fread(colorTable, sizeof(unsigned char), 1024, fileIn);
fwrite(colorTable, sizeof(unsigned char), 1024, fileOut);
}

// array to store image data in chunks
unsigned char buffer[CHUNK_SIZE];

// read & write image data in chunks until end of the file reached
while(!feof(fileIn)) {

// read a chunk of image data from input file
size_t bytesRead = fread(buffer, sizeof(unsigned char), CHUNK_SIZE, fileIn);

// apply darkness threshold to each pixel in chunk
for (i = 0; i < bytesRead; i++) {
buffer[i] = buffer[i] + THRESHOLD;
buffer[i] = (buffer[i] > THRESHOLD) ? MAX_COLOR : buffer[i];
}
// write thresholded image data to the output file
fwrite(buffer, sizeof(unsigned char), bytesRead, fileOut);
}

// write thresholded image data to the output file
fwrite(buffer, sizeof(unsigned char), size, fileOut);

fClose(fileIn);
fclose(fileOut);
return 0;
}
Loading