diff --git a/C_image_processing/.gitignore b/C_image_processing/.gitignore new file mode 100644 index 0000000..eb3c204 --- /dev/null +++ b/C_image_processing/.gitignore @@ -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 diff --git a/assets/images/test_image.bmp b/C_image_processing/assets/images/test_image.bmp similarity index 100% rename from assets/images/test_image.bmp rename to C_image_processing/assets/images/test_image.bmp diff --git a/filters/black_and_white_filter.c b/C_image_processing/filters/black_and_white_filter.c similarity index 55% rename from filters/black_and_white_filter.c rename to C_image_processing/filters/black_and_white_filter.c index f4cdf50..12403f0 100644 --- a/filters/black_and_white_filter.c +++ b/C_image_processing/filters/black_and_white_filter.c @@ -1,44 +1,55 @@ #include -#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) @@ -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; } \ No newline at end of file diff --git a/C_image_processing/filters/bright_filter.c b/C_image_processing/filters/bright_filter.c new file mode 100644 index 0000000..e09be8a --- /dev/null +++ b/C_image_processing/filters/bright_filter.c @@ -0,0 +1,72 @@ +#include +#include +#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; +} \ No newline at end of file diff --git a/C_image_processing/filters/dark_filter.c b/C_image_processing/filters/dark_filter.c new file mode 100644 index 0000000..ff6444d --- /dev/null +++ b/C_image_processing/filters/dark_filter.c @@ -0,0 +1,69 @@ +#include +#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; +} \ No newline at end of file diff --git a/C_image_processing/filters/negative_filter.c b/C_image_processing/filters/negative_filter.c new file mode 100644 index 0000000..8954ec8 --- /dev/null +++ b/C_image_processing/filters/negative_filter.c @@ -0,0 +1,62 @@ +#include +#include +#include + +int negative_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; + } + + unsigned char *imageData; + unsigned char *newImageData; + unsigned char imageHeader[54]; + unsigned char colorTable[1024]; + + int i, j; + + // check if input file exists + fread(imageHeader, sizeof(unsigned char), 54, fileIn); + int width = *(int*)&imageHeader[18]; + int height = *(int*)&imageHeader[22]; + int bitDepth = *(int*)&imageHeader[28]; + int imageDataSize = width * height; + + imageData = (unsigned char*)malloc(imageDataSize * sizeof(unsigned char)); + newImageData = (unsigned char*)malloc(imageDataSize * sizeof(unsigned char)); + + // check if image has a color table + if (bitDepth <= 8) { + fread(colorTable, sizeof(unsigned char), 1024, fileIn); + } + + fread(imageData, sizeof(unsigned char), imageDataSize, fileIn); + + // apply negative filter to each pixel + unsigned char *p = imageData; + unsigned char *q = newImageData; + for (i = 0; i < height * width; i++) { + *q++ = 255 - *p++; + } + + // write image data to output file + fwrite(imageHeader, sizeof(unsigned char), 54, fileOut); + if (bitDepth <= 8) { + fwrite(colorTable, sizeof(unsigned char), 1024, fileOut); + } + + fwrite(newImageData, sizeof(unsigned char), imageDataSize, fileOut); + + fclose(fileIn); + fclose(fileOut); + + free(imageData); + free(newImageData); + return 0; +} \ No newline at end of file diff --git a/C_image_processing/filters/rgb_to_gray_filter.c b/C_image_processing/filters/rgb_to_gray_filter.c new file mode 100644 index 0000000..8a16d2c --- /dev/null +++ b/C_image_processing/filters/rgb_to_gray_filter.c @@ -0,0 +1,57 @@ +#include + +int rgb_to_gray_filter(inputFile, outputFile) { + + FILE *fileIn = fopen(inputFile, "rb"); + FILE *fileOut = fopen(outputFile, "wb+"); + + int i; + unsigned char byte[54]; // store header info + + if (fileIn == NULL || fileOut == NULL) { + printf("File does not exist.\n"); + if (fileIn != NULL) fclose(fileIn); + if (fileOut != NULL) fclose(fileOut); + return 1; + } + + // read 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 & bitDepth of image from header info + int height = *(int*)&byte[18]; + int width = *(int*)&byte[22]; + int bitDepth = *(int*)&byte[28]; + int size = height * width; + + unsigned char (*buffer)[3] = malloc(size * sizeof(*buffer)); // store image data + if (buffer == NULL) { + printf("Memory allocation failed.\n"); + return 1; + } + + unsigned char y; + for(i = 0; i < size; i ++) { + y = 0; + buffer[i][0] = getc(fileIn); // red + buffer[i][1] = getc(fileIn); // green + buffer[i][2] = getc(fileIn); // blue + + // RGB to gray + y = (buffer[i][0] * 0.3) + (buffer[i][2] * 0.11); + + // Triplicate grayscale value for three color channels + putc(y, fileOut); + putc(y, fileOut); + putc(y, fileOut); + } + + fClose(fileIn); + fclose(fileOut); + return 0; +} \ No newline at end of file diff --git a/C_image_processing/filters/sepia_filter.c b/C_image_processing/filters/sepia_filter.c new file mode 100644 index 0000000..23d192b --- /dev/null +++ b/C_image_processing/filters/sepia_filter.c @@ -0,0 +1,65 @@ +#include +#define MAX_VALUE 255 + +int sepia_filter(inputFile, outputFile) { + + FILE *fileIn = fopen(inputFile, "rb"); + FILE *fileOut = fopen(outputFile, "wb+"); + + int i, r, g, b; + unsigned char byte[54]; + + if (fileIn == NULL || fileOut == NULL) { + printf("File does not exist.\n"); + if (fileIn != NULL) fclose(fileIn); + if (fileOut != NULL) fclose(fileOut); + return 1; + } + + // read 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; + + unsigned char (*buffer)[3] = malloc(size * sizeof(*buffer)); // store the image data + if (buffer == NULL) { + printf("Memory allocation failed.\n"); + return 1; + } + + for(i = 0; i < size; i++) { + r = 0; g = 0; b = 0; + + buffer[i][0] = getc(fileIn); // red + buffer[i][1] = getc(fileIn); // green + buffer[i][2] = getc(fileIn); // blue + + // each pixel value is above 255? + if(r > MAX_VALUE) { + r = MAX_VALUE; + } else if (g > MAX_VALUE) { + g = MAX_VALUE; + } else if (b > MAX_VALUE) { + b = MAX_VALUE; + } + // Triplicate grayscale value for three color channels + putc(b, fileOut); + putc(b, fileOut); + putc(b, fileOut); + } + + fClose(fileIn); + fclose(fileOut); + return 0; +} \ No newline at end of file diff --git a/C_image_processing/main.c b/C_image_processing/main.c new file mode 100644 index 0000000..5bc5629 --- /dev/null +++ b/C_image_processing/main.c @@ -0,0 +1,25 @@ +#include "filters/black_and_white_filter.c" +#include "filters/negative_filter.c" + +int main() { + const char* inputFileName = "assets/images/test_image.bmp"; + const char* outputFileName = "assets/images/output.bmp"; + + int result = black_and_white_filter(inputFileName, outputFileName); + + switch (result) { + case 0: + printf("Black and white filtering completed successfully.\n"); + break; + case 1: + printf("Error: Input file \"%s\" not found.\n", inputFileName); + break; + case 2: + printf("Error: Unable to create output file \"%s\".\n", outputFileName); + break; + default: + printf("Error: An unknown error occurred during filtering.\n"); + } + + return 0; +} \ No newline at end of file diff --git a/filters/bright_filter.c b/filters/bright_filter.c deleted file mode 100644 index 0bbeb3e..0000000 --- a/filters/bright_filter.c +++ /dev/null @@ -1,59 +0,0 @@ -#include -#include -#define MAX_COLOR 255 // define the maximum color value -#define BRIGHTNESS 25 // define the brightness factor -#define CHUNK_SIZE 1024 // define the size of the chunks to read and write - -int bright_filter(inputFile, outputFile) { - FILE *fileIn = fopen(inputFile, "rb"); // open the input file - File *fileOut = fopen(outputFile, "wb+"); // create the output file - int i; // variable to iterate - 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) { - printf("File does not exist.\n"); - return 1; - } - // read the header information of the image - for(i = 0; i < 54; i++) { - byte[i] = getc(fileIn); - } - // write the header information to the output file - fwrite(byte, sizeof(unsigned char), 54, fileOut); - // extract the height, width and bitDepth of the 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 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 - : ; - } - // write the thresholded image data to the output file - fwrite(buffer, sizeof(unsigned char), bytesRead, fileOut); - } - // write the thresholded image data to the output file - fwrite(buffer, sizeof(unsigned char), size, fileOut); - // close the input and output files - fClose(fileIn); - fclose(fileOut); - // exit - return 0; -} \ No newline at end of file diff --git a/filters/dark_filter.c b/filters/dark_filter.c deleted file mode 100644 index 91092e4..0000000 --- a/filters/dark_filter.c +++ /dev/null @@ -1,56 +0,0 @@ -#include -#define THRESHOLD 40 // define the threshold value for darkness - -int dark_filter(inputFile, outputFile) { - FILE *fileIn = fopen(inputFile, "rb"); // open the input file - File *fileOut = fopen(outputFile, "wb+"); // create the output file - int i; // variable to iterate - 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) { - printf("File does not exist.\n"); - return 1; - } - // read the header information of the image - for(i = 0; i < 54; i++) { - byte[i] = getc(fileIn); - } - // write the header information to the output file - fwrite(byte, sizeof(unsigned char), 54, fileOut); - // extract the height, width and bitDepth of the 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 darkness threshold to each pixel in the chunk - for(i = 0; i < bytesRead; i++) { - buffer[i] = buffer[i] + THRESHOLD; - buffer[i] = (buffer[i] > THRESHOLD) - ? MAX_COLOR - : ; - } - // write the thresholded image data to the output file - fwrite(buffer, sizeof(unsigned char), bytesRead, fileOut); - } - // write the thresholded image data to the output file - fwrite(buffer, sizeof(unsigned char), size, fileOut); - // close the input and output files - fClose(fileIn); - fclose(fileOut); - // exit - return 0; -} \ No newline at end of file diff --git a/filters/negative_filter.c b/filters/negative_filter.c deleted file mode 100644 index 1b0977b..0000000 --- a/filters/negative_filter.c +++ /dev/null @@ -1,54 +0,0 @@ -#include -#include -#include - -int negative_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 - // declare variables to store the image data and header information - unsigned char *imageData; - unsigned char *newImageData; - unsigned char imageHeader[54]; - unsigned char colorTable[1024]; - // declare loop variables - int i, j; - // read the header information of the input image file - fread(imageHeader, sizeof(unsigned char), 54, fileIn); - // extract the height, width and bitDepth of the image from the header information - int width = *(int*)&imageHeader[18]; - int height = *(int*)&imageHeader[22]; - int bitDepth = *(int*)&imageHeader[28]; - // calculate the size of the new image data in bytes - int imageDataSize = width * height; - // allocate memory to store the pixel data of the input and output images - imageData = (unsigned char*)malloc (imageDataSize * sizeof(unsignedchar)); - newImageData = (unsigned char*)malloc (imageDataSize * sizeof(unsignedchar)); - // check if the image has a color table - if(bitDepth <= 8) { - fread(colorTable, sizeof(unsigned char), 1024, fileIn); - } - // read the pixel data of the input image from the input file - fread(imageData, sizeof(unsigned char), imageDataSize, fileIn); - - // apply the negative filter to each pixel of the image - unsigned char *p = imageData; - unsigned char *q = newImageData; - for(i = 0; i < height * width; i++) { - *q++ = 255 - *p++; - } - // open the output image file in binary mode. - FILE *fileOut = fopen(outputFile, "wb"); - // write the header information of the output image to the output file - fwrite(imageHeader, sizeof(unsigned char), 54, fileOut); - // if the image has a color table, write it to the output file - if(bitDepth <= 8) { - fwrite(colorTable, sizeof(unsigned char), 1024, fileOut); - } - // write the pixel data of the negative image to the output file - fwrite(newImageData, sizeof(unsigned char), imageDataSize, fileOut); - // close the input and output files - fclose(fileIn); - fclose(fileOut); - // exit - return 0; -} \ No newline at end of file diff --git a/filters/rgb_to_gray_filter.c b/filters/rgb_to_gray_filter.c deleted file mode 100644 index 6b14162..0000000 --- a/filters/rgb_to_gray_filter.c +++ /dev/null @@ -1,48 +0,0 @@ -#include - -int rgb_to_gray_filter(inputFile, outputFile) { - FILE *fileIn = fopen(inputFile, "rb"); // open the input file - File *fileOut = fopen(outputFile, "wb+"); // create the output file - int i, n, m; // i is the iterator, n is the height, m is the width - unsigned char byte[54]; // store the header information - - // check if the input file exists - if(fileIn == NULL) { - printf("File does not exist.\n"); - return 1; - } - // read the header information of the image - for(i = 0; i < 54; i++) { - byte[i] = getc(fileIn); - } - // write the header information to the output file - fwrite(byte, sizeof(unsigned char), 54, fileOut); - // extract the height, width and bitDepth of the 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; - // store the pixel data in buffer - unsigned char buffer[size][3]; - for(i = 0; i < size; i ++) { - y = 0; - // red - buffer[i][0] = getc(fileIn); - // green - buffer[i][1] = getc(fileIn); - // blue - buffer[i][2] = getc(fileIn); - // convert the RGB value to gray - y = (buffer[i][0] * 0.3) + (buffer[i][2] * 0.11); - // each pixel has three color channels, write the gray value three times - putc(y, fileOut); - putc(y, fileOut); - putc(y, fileOut); - } - // close the input and output files - fClose(fileIn); - fclose(fileOut); - // exit - return 0; -} \ No newline at end of file diff --git a/filters/sepia_filter.c b/filters/sepia_filter.c deleted file mode 100644 index 3624d43..0000000 --- a/filters/sepia_filter.c +++ /dev/null @@ -1,55 +0,0 @@ -#include -#define MAX_VALUE 255 // max pixel value - -int sepia_filter(inputFile, outputFile) { - FILE *fileIn = fopen(inputFile, "rb"); // open the input file - File *fileOut = fopen(outputFile, "wb+"); // create the output file - int i, r, g, b; - unsigned char byte[54]; - // check if the input file exists - if(fileIn == NULL) { - printf("File does not exist.\n"); - return 1; - } - // read the header information of the image - for(i = 0; i < 54; i++) { - byte[i] = getc(fileIn); - } - // write the header information to the output file - fwrite(byte, sizeof(unsigned char), 54, fileOut); - // extract the height, width and bitDepth of the 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; - unsigned char buffer[size][3]; - for(i = 0; i < size; i++) { - r = 0; - g = 0; - b = 0; - // red - buffer[i][0] = getc(fileIn); - // green - buffer[i][1] = getc(fileIn); - // blue - buffer[i][2] = getc(fileIn); - // check to see if each pixel value is above 255 - if(r > MAX_VALUE) { - r = MAX_VALUE; - } else if (g > MAX_VALUE) { - g = MAX_VALUE; - } else if (b > MAX_VALUE) { - b = MAX_VALUE; - } - // each pixel has three color channels, write the gray value three times - putc(b, fileOut); - putc(b, fileOut); - putc(b, fileOut); - } - // close the input and output files - fClose(fileIn); - fclose(fileOut); - // exit - return 0; -} \ No newline at end of file diff --git a/main.c b/main.c deleted file mode 100644 index a862d6d..0000000 --- a/main.c +++ /dev/null @@ -1,9 +0,0 @@ -#include "filters/black_and_white_filter.c" -#include "filters/negative_filter.c" - -int main() { - black_and_white_filter("assets/images/test_image.bmp", "assets/images/black_and_white_test_image.bmp"); - negative_filter("assets/images/test_image.bmp", "assets/images/negative_test_image.bmp"); - - return 0; -} \ No newline at end of file