11#include <stdio.h>
2- #define THRESHOLD 128 // define tha value of the threshold for black and white
3- #define WHITE 255 // define the value for white pixels
4- #define BLACK 0 // define the value for black pixels
5- #define CHUNK_SIZE 1024 // define the size of the chunks to read and write
6-
7- int black_and_white_filter (inputFile , outputFile ) {
8- FILE * fileIn = fopen (inputFile , "rb" ); // open the input file for reading in binary mode
9- FILE * fileOut = fopen (outputFile , "wb+" ); // create the output file for writing in binary mode
10- int i ; // variable to iterate through the image data
11- unsigned char byte [54 ]; // array to store the header information of the image
12- unsigned char colorTable [1024 ]; // array to store the color table of the image
13- // check if the input file exists
14- if (fileIn == NULL ) {
2+ #define THRESHOLD 128 // define value of threshold for black and white
3+ #define WHITE 255
4+ #define BLACK 0
5+ #define CHUNK_SIZE 1024 // define size of chunks to read and write
6+
7+ int black_and_white_filter (const char * inputFile , const char * outputFile ) {
8+
9+ FILE * fileIn = fopen (inputFile , "rb" );
10+ FILE * fileOut = fopen (outputFile , "wb+" );
11+
12+ if (fileIn == NULL || fileOut == NULL ) {
1513 printf ("File does not exist.\n" );
14+ if (fileIn != NULL ) fclose (fileIn );
15+ if (fileOut != NULL ) fclose (fileOut );
1616 return 1 ;
1717 }
18- // read the header information of the image
18+
19+ int i ;
20+ unsigned char byte [54 ];
21+ unsigned char colorTable [1024 ];
22+
23+ // read header info of image
1924 for (i = 0 ; i < 54 ; i ++ ) {
2025 byte [i ] = getc (fileIn );
2126 }
22- // write the header information to the output file
27+
28+ // write header info to output file
2329 fwrite (byte , sizeof (unsigned char ), 54 , fileOut );
24- // extract the height, width and bitDepth of the image from the header information
30+
31+ // extract height, width and bitDepth of image from the header information
2532 int height = * (int * )& byte [18 ];
2633 int width = * (int * )& byte [22 ];
2734 int bitDepth = * (int * )& byte [28 ];
28- // calculate the size of the image in pixels
2935 int size = height * width ;
36+
3037 // check if the image has a color table
3138 if (bitDepth <= 8 ) {
3239 // read, and then write the color table from the input file
3340 fread (colorTable , sizeof (unsigned char ), 1024 , fileIn );
3441 fwrite (colorTable , sizeof (unsigned char ), 1024 , fileOut );
3542 }
43+
3644 // array to store the image data in chunks
3745 unsigned char buffer [CHUNK_SIZE ];
46+
3847 // read and write the image data in chunks until the end of the file is reached
3948 while (!feof (fileIn )) {
49+
4050 // read a chunk of image data from the input file
4151 size_t bytesRead = fread (buffer , sizeof (unsigned char ), CHUNK_SIZE , fileIn );
52+
4253 // apply the threshold to each pixel in the chunk
4354 for (i = 0 ; i < bytesRead ; i ++ ) {
4455 buffer [i ] = (buffer [i ] > THRESHOLD )
@@ -48,9 +59,8 @@ int black_and_white_filter(inputFile, outputFile) {
4859 // write the thresholded image data to the output file
4960 fwrite (buffer , sizeof (unsigned char ), bytesRead , fileOut );
5061 }
51- // close the input and output files
52- fClose (fileIn );
62+
63+ fclose (fileIn );
5364 fclose (fileOut );
54- // exit
5565 return 0 ;
5666}
0 commit comments