Skip to content

Instantly share code, notes, and snippets.

@theo-carr
Created October 4, 2019 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theo-carr/10b0f6fbe5afc20c82ed92c16a1b80ad to your computer and use it in GitHub Desktop.
Save theo-carr/10b0f6fbe5afc20c82ed92c16a1b80ad to your computer and use it in GitHub Desktop.
//lab5
#include <stdio.h>
#include <stdlib.h>
using namespace std;
struct Pixel{
unsigned char b;
unsigned char g;
unsigned char r;
};
#pragma pack(push, 2)
struct BitmapFileHeader {
short int type;
int size;
short int r1; //reserve
short int r2; //reserve
int offset;
};
struct BitmapInfoHeader {
int size;
int w;
int h;
short int planes;
short int bitcount;
int compression;
int x_ppm; //x pixels per meter
int y_ppm;
int color_used;
int color_important;
};
#pragma pack(pop)
int main(int argc, char** argv){
char * infile = argv[1];
char * outfile = argv[2];
FILE * ifile = fopen(infile,"rb");
if (ifile != nullptr){
//declare header structs
BitmapFileHeader fhead;
BitmapFileHeader * fpoint;
BitmapInfoHeader ihead;
BitmapInfoHeader * ipoint;
fpoint = &fhead;
ipoint = &ihead;
//read in file header
fread(fpoint,(sizeof(char)*14),1,ifile);
//read in info header
fread(ipoint,(sizeof(char)*40),1,ifile);
//calculate padding
unsigned int pad_count = 4 - ((ihead.w * 3) % 4);
//create pixel array
Pixel pixel_array[ihead.h * ihead.w];
Pixel * pp;
pp = pixel_array;
//move file pointer to offset
fseek(ifile,fhead.offset,SEEK_SET);
//copy pixels into array from ifile
for (int i = 0; i < ihead.h; i++){
fread(pp,(sizeof(char)*ihead.w),1,ifile);
//skip padding and move array pointer
fseek(ifile,pad_count,SEEK_CUR);
pp += ihead.w;
}
//open outfile
FILE * ofile = fopen(outfile,"wb");
//reset file pointers to start
rewind(ifile);
fseek(ofile,0,SEEK_SET);
//copy headers to output file
fwrite(fpoint,(sizeof(char)*14),1,ofile);
fwrite(ipoint,(sizeof(char)*40),1,ofile);
//write pixel array to output file
pp = pixel_array; //reset pp position in pixel_array
//create padding to write into outfile
if (pad_count == 3){
char temp[3];
char * padding;
padding = temp;
for (int i = 0; i < ihead.h; i++){
fwrite(pp,(sizeof(char)*ihead.w),1,ofile);
fwrite(padding,1,3,ofile);
pp += ihead.w;
}
}
else if (pad_count == 2){
char temp[2];
char * padding;
padding = temp;
for (int i = 0; i < ihead.h; i++){
fwrite(pp,(sizeof(char)*ihead.w),1,ofile);
fwrite(padding,1,2,ofile);
pp += ihead.w;
}
}
else{
unsigned char temp = 0;
unsigned char * padding;
padding = &temp;
for (int i = 0; i < ihead.h; i++){
fwrite(pp,(sizeof(char)*ihead.w),1,ofile);
fwrite(padding,1,1,ofile);
pp += ihead.w;
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment