Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <errno.h>
  5.  
  6. #define HEADER_SIZE 54
  7. #define PIXEL_SIZE  3
  8. #define MAX_PATH_SIZE 4096
  9.  
  10. int main(int argc, char *argv[]) {
  11.  
  12.     printf("Starting ...\n");
  13.  
  14.     int c = 0;
  15.     char input[MAX_PATH_SIZE];
  16.     char output[MAX_PATH_SIZE];
  17.     char * ptr;
  18.     char type[3];
  19.     int keep_going = 1;
  20.     unsigned short int bpp;
  21.     char median = 0;
  22.     char header_tab[HEADER_SIZE];
  23.     char pixel_tab[PIXEL_SIZE];
  24.     char pixel_write_tab[PIXEL_SIZE];
  25.  
  26.     FILE * fin;
  27.     FILE * fout;
  28.     FILE * temp;
  29.  
  30.     printf("Enter the file name of a 24 bit bitmap image :\n");
  31.     if(fgets(input, MAX_PATH_SIZE, stdin) == NULL){
  32.         printf("Error - Unable to get the char\n");
  33.         return 1;
  34.     }
  35.     if((ptr = strchr(input, '\n')) != NULL) *ptr = '\0';
  36.  
  37.     printf("Enter a file name for output file:\n");
  38.     if(fgets(output, MAX_PATH_SIZE, stdin) == NULL){
  39.         printf("Error - Unable to get the char\n");
  40.         return 1;
  41.     }
  42.     if((ptr = strchr(output, '\n')) != NULL) *ptr = '\0';
  43.  
  44.     if(!(temp = fopen(input, "rb"))) {
  45.         printf("Error - Unable to open the file given (errno:%d)\n", errno);
  46.         return 1;
  47.     } else{
  48.         fseek(temp,0,0);
  49.         fread(type,1,2,temp);
  50.         type[2] = '\0';
  51.         fseek(temp,28,0);
  52.         fread(&bpp,1,2,temp);
  53.         fclose(temp);
  54.     }
  55.  
  56.     if(strcmp(type,"BM") || (bpp != 24)){
  57.         printf("Error - The file is not BMP format or is not 24 bits\n");
  58.         return 1;
  59.     }
  60.  
  61.     if(strcmp(input, output) != 0){
  62.         fin = fopen(input, "rb");
  63.         //Read header
  64.         c = fread(header_tab, 1, HEADER_SIZE ,fin);
  65.         if (c<HEADER_SIZE){
  66.             printf("Warning - BMP Header to small\n Exiting properly ...\n");
  67.             fclose(fin);
  68.             return 1;
  69.         }
  70.         if(!(fout = fopen(output, "wb"))) {
  71.             printf("Error - Unable to open/create the output file (errno:%d)\n", errno);
  72.             fclose(fin);
  73.             return 1;
  74.         }
  75.         //Write header
  76.         fwrite(header_tab, 1, HEADER_SIZE, fout);
  77.  
  78.         //To shade of grey
  79.         do
  80.         {
  81.             c = fread(pixel_tab, 1,PIXEL_SIZE, fin);
  82.             if(c!=3){
  83.                 keep_going = 0;
  84.             } else{
  85.                 median = (char) ((pixel_tab[0] + pixel_tab[1] + pixel_tab[2]) / 3);
  86.                 pixel_write_tab[0] = median;
  87.                 pixel_write_tab[1] = median;
  88.                 pixel_write_tab[2] = median;
  89.                 fwrite(pixel_write_tab, 1, PIXEL_SIZE, fout);
  90.             }
  91.         }  
  92.         while(keep_going == 1);
  93.     }
  94.     else{
  95.         printf("Error - Unable to write the output in the same file !\n");
  96.         return 1;
  97.     }
  98.  
  99.     fclose(fin);
  100.     fclose(fout);
  101.  
  102.     printf("Ending without errors\n");    
  103.     return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement