Advertisement
x1n53n

Untitled

Nov 19th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.13 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 long long int W = 0;
  19.     unsigned long long int H = 0;
  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.  
  29.     printf("Enter path of a bitmap image :\n");
  30.  
  31.     if(fgets(input, 69, stdin) == NULL){
  32.         printf("Error - Unable to get input\n");
  33.         return 1;
  34.     }
  35.  
  36.     if (input[strlen(input)-1] != '\n') {
  37.         printf("Error - What have you done with the input ?\n");
  38.         return 2;
  39.     }
  40.     else
  41.         input[strlen(input)-1] = '\0';
  42.  
  43.     if (!(fin = fopen(input, "rb"))) {
  44.         printf("Error - Unable to open the file given (errno:%d)\n", errno);
  45.         return 1;
  46.     }
  47.     
  48.     fseek(fin,0,0);
  49.     fread(type,1,2,fin);
  50.     type[2] = '\0';
  51.  
  52.     fseek(fin,28,0);
  53.     fread(&bpp,1,2,fin);
  54.  
  55.     if (strcmp(type,"BM") || (bpp != 24)){
  56.         printf("Error - The file is not BMP format or is not 24 bits\n");
  57.         exit(0);
  58.     }
  59.  
  60.     fin = fopen(input, "rb");
  61.  
  62.     printf("Enter a file name for output file (max 20 char) :\n");
  63.     if(fgets(output, 20, stdin) == NULL){
  64.         printf("Error - Unable to get output file name\n");
  65.         return 1;
  66.     }
  67.  
  68.     if(output == input){
  69.         printf("Error - Input file can't be the output file !\n");
  70.         return 1;
  71.     }
  72.  
  73.     if (output[strlen(output)-1] != '\n') {
  74.         printf("Error - Bad output file !\n");
  75.         return 2;
  76.     }
  77.     else
  78.         output[strlen(output)-1] = '\0';
  79.  
  80.     if (!(fout = fopen(output, "wb"))) {
  81.         printf("Error - Unable to open the output file (errno:%d)\n", errno);
  82.         return 1;
  83.     }
  84.  
  85.     // lecture du header
  86.  
  87.     c = fread(header_tab, 1, HEADER_SIZE ,fin);
  88.     if (c<HEADER_SIZE){
  89.         printf("Warning - Header to small\n Exiting properly ...\n");
  90.         fclose(fin);
  91.         fclose(fout);
  92.         exit(1);
  93.     }
  94.  
  95.     // écriture du header
  96.     fwrite(header_tab, 1, HEADER_SIZE, fout);
  97.  
  98.     // passage en gris
  99.     while (keep_going == 1)
  100.     {
  101.         c = fread(pixel_tab, 1,PIXEL_SIZE, fin);
  102.         if (c!=3){
  103.             keep_going = 0;
  104.         } else {
  105.             median = (char) ((pixel_tab[0] + pixel_tab[1] + pixel_tab[2]) / 3);
  106.             pixel_write_tab[0] = median;
  107.             pixel_write_tab[1] = median;
  108.             pixel_write_tab[2] = median;
  109.             fwrite(pixel_write_tab, 1, PIXEL_SIZE, fout);
  110.         }
  111.     }
  112.  
  113.  
  114.     if (fin  != NULL)  fclose(fin);
  115.     if (fout != NULL)  fclose(fout);
  116.  
  117.     printf("Ending without errors\n");    
  118.     return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement