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
6 changes: 6 additions & 0 deletions C_image_processing/.gitignore → .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,9 @@ $RECYCLE.BIN/

# Custom rules (everything added below won't be overriden by 'Generate .gitignore File' if you use 'Update' option)
output.bmp
test_image_black_and_white.bmp
test_image_bright.bmp
test_image_dark.bmp
test_image_negative.bmp
test_image_rgb_to_gray.bmp
test_image_sepia.bmp
Binary file not shown.
Binary file not shown.
42 changes: 25 additions & 17 deletions C_image_processing/filters/black_and_white_filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,68 @@
#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) {
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) {
if (fileIn == NULL || fileOut == NULL)
{
printf("File does not exist.\n");
if (fileIn != NULL) fclose(fileIn);
if (fileOut != NULL) fclose(fileOut);
if (fileIn != NULL)
fclose(fileIn);
if (fileOut != NULL)
fclose(fileOut);
return 1;
}

unsigned char headerInfo[54];
unsigned char colorTable[1024];

// Read header info of image
for(int i = 0; i < 54; i++) {
for (int i = 0; i < 54; i++)
{
headerInfo[i] = getc(fileIn);
}

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

// Extract.. of image from the header information
int height = *(int*)&headerInfo[18];
int width = *(int*)&headerInfo[22];
int bitDepth = *(int*)&headerInfo[28];
int height = *(int *)&headerInfo[18];
int width = *(int *)&headerInfo[22];
int bitDepth = *(int *)&headerInfo[28];
int size = height * width;

// Check if the image has a color table
if(bitDepth <= 8) {
if (bitDepth <= 8)
{
fread(colorTable, sizeof(unsigned char), 1024, fileIn);
fwrite(colorTable, sizeof(unsigned char), 1024, fileOut);
}

unsigned char chunkBuffer[CHUNK_SIZE];

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

while (!feof(fileIn))
{

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

// Apply threshold to each pixel in the chunk
for(int i = 0; i < bytesRead; i++) {
for (int i = 0; i < bytesRead; i++)
{
chunkBuffer[i] = (chunkBuffer[i] > THRESHOLD)
? WHITE
: BLACK;
? WHITE
: BLACK;
}
// Write the thresholded image data to the output file
fwrite(chunkBuffer, sizeof(unsigned char), bytesRead, fileOut);
}

fclose(fileIn);
fclose(fileOut);

return 0;
}
44 changes: 26 additions & 18 deletions C_image_processing/filters/bright_filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,56 @@
#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) {
int bright_filter(const char *inputFile, const char *outputFile)
{
FILE *fileIn = fopen(inputFile, "rb");
FILE *fileOut = fopen(outputFile, "wb+");

if (fileIn == NULL || fileOut == NULL) {

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

unsigned char headerInfo[54];
unsigned char colorTable[1024];

for(int i = 0; i < 54; i++) {

for (int i = 0; i < 54; i++)
{
headerInfo[i] = getc(fileIn);
}

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

// Extract.. of image from header info
int height = *(int*)&headerInfo[18];
int width = *(int*)&headerInfo[22];
int bitDepth = *(int*)&headerInfo[28];
int height = *(int *)&headerInfo[18];
int width = *(int *)&headerInfo[22];
int bitDepth = *(int *)&headerInfo[28];
int pixelsInImage = height * width;

// Check if image has a color table
if(bitDepth <= 8) {
if (bitDepth <= 8)
{
fread(colorTable, sizeof(unsigned char), 1024, fileIn);
fwrite(colorTable, sizeof(unsigned char), 1024, fileOut);
}

unsigned char chunkBuffer[CHUNK_SIZE];

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

while (!feof(fileIn))
{

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

// Apply brightness factor to each pixel in the chunk
for (int i = 0; i < bytesRead; i++) {
for (int i = 0; i < bytesRead; i++)
{
chunkBuffer[i] = chunkBuffer[i] + BRIGHTNESS;
chunkBuffer[i] = (chunkBuffer[i] > THRESHOLD) ? MAX_COLOR : chunkBuffer[i];
}
Expand All @@ -59,8 +66,9 @@ int bright_filter(inputFile, outputFile) {

// Write thresholded image data to output file
fwrite(chunkBuffer, sizeof(unsigned char), pixelsInImage, fileOut);
fClose(fileIn);

fclose(fileIn);
fclose(fileOut);

return 0;
}
46 changes: 27 additions & 19 deletions C_image_processing/filters/dark_filter.c
Original file line number Diff line number Diff line change
@@ -1,56 +1,63 @@
#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
#define DARK_THRESHOLD 40
#define CHUNK_SIZE 1024

int dark_filter(inputFile, outputFile) {
int dark_filter(const char *inputFile, const char *outputFile)
{
FILE *fileIn = fopen(inputFile, "rb");
FILE *fileOut = fopen(outputFile, "wb+");

if (fileIn == NULL || fileOut == NULL) {

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

unsigned char headerInfo[54];
unsigned char colorTable[1024];

// Read header info of image
for(int i = 0; i < 54; i++) {
for (int i = 0; i < 54; i++)
{
headerInfo[i] = getc(fileIn);
}

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

// Extract.. of the image from header info
int height = *(int*)&headerInfo[18];
int width = *(int*)&headerInfo[22];
int bitDepth = *(int*)&headerInfo[28];
int height = *(int *)&headerInfo[18];
int width = *(int *)&headerInfo[22];
int bitDepth = *(int *)&headerInfo[28];
int pixelsInImage = height * width;

// Check if image has a color table
if(bitDepth <= 8) {
if (bitDepth <= 8)
{
fread(colorTable, sizeof(unsigned char), 1024, fileIn);
fwrite(colorTable, sizeof(unsigned char), 1024, fileOut);
}

unsigned char chunkBuffer[CHUNK_SIZE];

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

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

// apply darkness threshold to each pixel in chunk
for (int i = 0; i < bytesRead; i++) {
chunkBuffer[i] = chunkBuffer[i] + THRESHOLD;
chunkBuffer[i] = (chunkBuffer[i] > THRESHOLD) ? MAX_COLOR : chunkBuffer[i];
for (int i = 0; i < bytesRead; i++)
{
chunkBuffer[i] = chunkBuffer[i] + DARK_THRESHOLD;
chunkBuffer[i] = (chunkBuffer[i] > DARK_THRESHOLD) ? MAX_COLOR : chunkBuffer[i];
}
// write thresholded image data to the output file
fwrite(chunkBuffer, sizeof(unsigned char), bytesRead, fileOut);
Expand All @@ -59,7 +66,8 @@ int dark_filter(inputFile, outputFile) {
// Write thresholded image data to output file
fwrite(chunkBuffer, sizeof(unsigned char), pixelsInImage, fileOut);

fClose(fileIn);
fclose(fileIn);
fclose(fileOut);

return 0;
}
45 changes: 26 additions & 19 deletions C_image_processing/filters/negative_filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,58 @@
#include <stdlib.h>
#include <time.h>

int negative_filter(const char *inputFile, const char *outputFile) {
int negative_filter(const char *inputFile, const char *outputFile)
{
FILE *fileIn = fopen(inputFile, "rb");
FILE *fileOut = fopen(outputFile, "wb+");

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

unsigned char *imageData = NULL;
unsigned char *newImageData = NULL;
unsigned char headerInfo[54];
unsigned char colorTable[1024];

// Read image header
fread(headerInfo, sizeof(unsigned char), 54, fileIn);
int width = *(int*)&headerInfo[18];
int height = *(int*)&headerInfo[22];
int bitDepth = *(int*)&headerInfo[28];
int width = *(int *)&headerInfo[18];
int height = *(int *)&headerInfo[22];
int bitDepth = *(int *)&headerInfo[28];
int imageDataSize = width * height;

// Allocate memory for image data
imageData = (unsigned char*)malloc(imageDataSize * sizeof(unsigned char));
newImageData = (unsigned char*)malloc(imageDataSize * sizeof(unsigned char));
imageData = (unsigned char *)malloc(imageDataSize * sizeof(unsigned char));
newImageData = (unsigned char *)malloc(imageDataSize * sizeof(unsigned char));

// Read color table if present
if (bitDepth <= 8) {
if (bitDepth <= 8)
{
fread(colorTable, sizeof(unsigned char), 1024, fileIn);
}

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

// Apply negative filter to each pixel
unsigned char *p = imageData;
unsigned char *q = newImageData;
for (int i = 0; i < height * width; i++) {
for (int i = 0; i < height * width; i++)
{
*q++ = 255 - *p++;
}

// Write image data to output file
fwrite(headerInfo, sizeof(unsigned char), 54, fileOut);
if (bitDepth <= 8) {
if (bitDepth <= 8)
{
fwrite(colorTable, sizeof(unsigned char), 1024, fileOut);
}

Expand All @@ -56,8 +62,9 @@ int negative_filter(const char *inputFile, const char *outputFile) {
// Clean up and close files
fclose(fileIn);
fclose(fileOut);

free(imageData);
free(newImageData);

return 0;
}
Loading