11#include <stdio.h>
22#include <stdlib.h>
3+
34#define MAX_COLOR 255
45#define BRIGHTNESS 25
56#define CHUNK_SIZE 1024 // define size of the chunks to read and write
@@ -17,54 +18,47 @@ int bright_filter(inputFile, outputFile) {
1718 return 1 ;
1819 }
1920
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 );
21+ unsigned char headerInfo [54 ];
22+ unsigned char colorTable [1024 ];
23+
24+ for (int i = 0 ; i < 54 ; i ++ ) {
25+ headerInfo [i ] = getc (fileIn );
2726 }
2827
29- // write header info to output file
30- fwrite (byte , sizeof (unsigned char ), 54 , fileOut );
28+ fwrite (headerInfo , sizeof (unsigned char ), 54 , fileOut );
3129
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 ];
30+ // Extract.. of image from header info
31+ int height = * (int * )& headerInfo [18 ];
32+ int width = * (int * )& headerInfo [22 ];
33+ int bitDepth = * (int * )& headerInfo [28 ];
34+ int pixelsInImage = height * width ;
3635
37- // calculate size of image in pixels
38- int size = height * width ;
39-
40- // check if image has a color table
36+ // Check if image has a color table
4137 if (bitDepth <= 8 ) {
42- // read, then write color table from the input file
4338 fread (colorTable , sizeof (unsigned char ), 1024 , fileIn );
4439 fwrite (colorTable , sizeof (unsigned char ), 1024 , fileOut );
4540 }
4641
47- // array to store image data in chunks
48- unsigned char buffer [CHUNK_SIZE ];
42+ unsigned char chunkBuffer [CHUNK_SIZE ];
4943
50- // read & write image data in chunks until the end of file is reached
44+ // Read & write image data in chunks until the end of file is reached
5145 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 );
5546
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 ];
47+ // Read a chunk of image data from the input file
48+ size_t bytesRead = fread (chunkBuffer , sizeof (unsigned char ), CHUNK_SIZE , fileIn );
49+
50+ // Apply brightness factor to each pixel in the chunk
51+ for (int i = 0 ; i < bytesRead ; i ++ ) {
52+ chunkBuffer [i ] = chunkBuffer [i ] + BRIGHTNESS ;
53+ chunkBuffer [i ] = (chunkBuffer [i ] > THRESHOLD ) ? MAX_COLOR : chunkBuffer [i ];
6054 }
6155
62- // write thresholded image data to the output file
63- fwrite (buffer , sizeof (unsigned char ), bytesRead , fileOut );
56+ // Write thresholded image data to output file
57+ fwrite (chunkBuffer , sizeof (unsigned char ), bytesRead , fileOut );
6458 }
6559
66- // write thresholded image data to the output file
67- fwrite (buffer , sizeof (unsigned char ), size , fileOut );
60+ // Write thresholded image data to output file
61+ fwrite (chunkBuffer , sizeof (unsigned char ), pixelsInImage , fileOut );
6862
6963 fClose (fileIn );
7064 fclose (fileOut );
0 commit comments