Advertisement
x1n53n

Untitled

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