SHARE
TWEET

Untitled

a guest Oct 22nd, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //lab5
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. using namespace std;
  5.  
  6. struct Pixel{
  7.     unsigned char b;
  8.     unsigned char g;
  9.     unsigned char r;
  10. };
  11. #pragma pack(push, 2)
  12. struct BitmapFileHeader {
  13.     short int type;
  14.     int size;
  15.     short int r1; //reserve
  16.     short int r2; //reserve
  17.     int offset;
  18.  
  19. };
  20. struct BitmapInfoHeader {
  21.     int size;
  22.     int  w;
  23.     int  h;
  24.     short int planes;
  25.     short int bitcount;
  26.     int compression;
  27.     int x_ppm; //x pixels per meter
  28.     int y_ppm;
  29.     int color_used;
  30.     int color_important;
  31. };
  32. #pragma pack(pop)
  33.  
  34. int main(int argc, char** argv){
  35.   char * infile = argv[1];
  36.   char * outfile = argv[2];
  37.   FILE * ifile = fopen(infile,"rb");
  38.   if (ifile != nullptr){
  39.       //declare header structs
  40.       BitmapFileHeader fhead;
  41.       BitmapFileHeader * fpoint;
  42.       BitmapInfoHeader ihead;
  43.       BitmapInfoHeader * ipoint;
  44.       fpoint = &fhead;
  45.       ipoint = &ihead;
  46.       //read in file header
  47.       fread(fpoint,(sizeof(char)*14),1,ifile);
  48.       //read in info header
  49.       fread(ipoint,(sizeof(char)*40),1,ifile);
  50.       //calculate padding
  51.       unsigned int pad_count =  4 - ((ihead.w * 3) % 4);
  52.       //create pixel array
  53.       Pixel pixel_array[ihead.h * ihead.w];
  54.       Pixel * pp;
  55.       pp = pixel_array;
  56.       //move file pointer to offset
  57.       fseek(ifile,fhead.offset,SEEK_SET);
  58.       //copy pixels into array from ifile
  59.       for (int i = 0; i < ihead.h; i++){
  60.           fread(pp,(sizeof(char)*ihead.w),1,ifile);
  61.           //skip padding and move array pointer
  62.           fseek(ifile,pad_count,SEEK_CUR);
  63.           pp += ihead.w;
  64.       }
  65.       //open outfile
  66.       FILE * ofile = fopen(outfile,"wb");
  67.       //reset file pointers to start
  68.       rewind(ifile);
  69.       fseek(ofile,0,SEEK_SET);
  70.       //copy headers to output file
  71.       fwrite(fpoint,(sizeof(char)*14),1,ofile);
  72.       fwrite(ipoint,(sizeof(char)*40),1,ofile);
  73.       //write pixel array to output file
  74.       pp = pixel_array; //reset pp position in pixel_array
  75.       //create padding to write into outfile
  76.       if (pad_count == 3){
  77.           char temp[3];
  78.           char *  padding;
  79.           padding = temp;
  80.           for (int i = 0; i < ihead.h; i++){
  81.               fwrite(pp,(sizeof(char)*ihead.w),1,ofile);
  82.               fwrite(padding,1,3,ofile);
  83.               pp += ihead.w;
  84.           }
  85.         }
  86.       else if (pad_count == 2){
  87.           char temp[2];
  88.           char * padding;
  89.           padding = temp;
  90.           for (int i = 0; i < ihead.h; i++){
  91.               fwrite(pp,(sizeof(char)*ihead.w),1,ofile);
  92.               fwrite(padding,1,2,ofile);
  93.               pp += ihead.w;
  94.           }
  95.       }
  96.       else{
  97.           unsigned char temp = 0;
  98.           unsigned char * padding;
  99.           padding = &temp;
  100.           for (int i = 0; i < ihead.h; i++){
  101.               fwrite(pp,(sizeof(char)*ihead.w),1,ofile);
  102.               fwrite(padding,1,1,ofile);
  103.               pp += ihead.w;
  104.           }
  105.       }
  106.      
  107.   }
  108.     return 0;
  109. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top