Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdlib.h>
- #include <stdint.h>
- //structure defiens bitmap header
- struct BITMAPFILEHEADER{
- uint8_t type[2];//type of file (bit map)
- uint32_t size;//size of file
- uint16_t reserved1;//
- uint16_t reserved2;//
- uint32_t offsetbits;//off set bits
- } __attribute__ ((packed));
- struct BITMAPINFOHEADER{
- uint32_t size;//bitmap size
- // uint16_t w2;
- uint32_t width;//width of bitmap
- //uint16_t h2;
- uint32_t height;//hight of bitmap
- uint16_t planes;
- uint16_t bitcount;
- uint32_t compression;// compression ratio (zero for no compression)
- uint32_t sizeimage;//size of image
- long xpelspermeter;
- long ypelspermeter;
- uint32_t colorsused;
- uint32_t colorsimportant;
- } __attribute__ ((packed));
- //const char* INPUT_FILE = "/home/bogdan/bee.bmp";
- const char* INPUT_FILE = "/home/bogdan/Linux.bmp";
- const char* ENCODED_FILE = "/home/bogdan/encoded.bin";
- const char* DECODED_FILE = "/home/bogdan/decoded.bmp";
- typedef struct SINGLE_PIXEL{
- uint8_t green;//Green level 0-255
- uint8_t red; //Red level 0-255
- } PIXEL;
- int comparePixels(PIXEL, PIXEL);
- void encode();
- void decode(char*);
- int main()
- {
- encode();
- decode(ENCODED_FILE);
- return 0;
- }
- void encode() {
- uint32_t i=0;//to count pixels read
- uint32_t pixno=0;//number of pixels to read
- struct BITMAPFILEHEADER source_head;//to store file header
- struct BITMAPINFOHEADER source_info;//to store bitmap info header
- PIXEL pixel;// the current pixel
- FILE *in;// bitmap imput pointer file
- FILE *out;//output file pointer
- if(!(in=fopen(INPUT_FILE,"rb")))//open in binary read mode
- {
- printf("ncan not open file");//error at opening file
- exit(-1);
- }
- out=fopen(ENCODED_FILE,"wb");//opne in binary write mode
- //read the headers to source file
- fread(&source_head,sizeof(struct BITMAPFILEHEADER),1,in);
- fread(&source_info,sizeof(struct BITMAPINFOHEADER),1,in);
- //write the headers to the output file
- fwrite(&source_head,sizeof(struct BITMAPFILEHEADER),1,out);
- fwrite(&source_info,sizeof(struct BITMAPINFOHEADER),1,out);
- //cumpute the number of pixels to read
- pixno=source_info.width*source_info.height;
- // init list of pixels
- PIXEL pixArr[pixno];
- printf("total pixels: %d", pixno);
- //printf("w:%f h:%u pn:%lu", (source_head.size/1024.0/1024), source_info.height, pixno);
- uint32_t sum = 0;
- //read, modify and write pixels
- for(i=0;i<pixno;++i)
- {
- //read pixel form source file
- fread(&pixel,sizeof(PIXEL),1,in);
- pixArr[i] = pixel;
- }
- for (i = 0; i < pixno; i++) {
- // printf ("i = %dtred = %d green = %d blue = %dn",i, pixArr[i].red, pixArr[i].green, pixArr[i].blue);
- int runlength = 1;
- while ((i + 1) < pixno) {
- if (comparePixels(pixArr[i], pixArr[i+1]) == 0){
- // printf ("i = %dt red = %d green = %d blue = %dn",i, pixArr[i].red, pixArr[i].green, pixArr[i].blue);
- runlength++;
- i++;
- } else {
- break;
- }
- }
- //fprintf(out, "%d", runlength);
- fwrite(&runlength, sizeof(runlength), 1, out);
- fwrite(&pixel,sizeof(PIXEL),1,out);
- sum += runlength;
- runlength = 0;
- }
- //write the modification to the output file
- //close all fiels
- fclose(in);
- fclose(out);
- printf("sum = %d",sum);
- }
- void decode(char * filePath) {
- uint32_t i=0;//to count pixels read
- uint32_t j=0;
- uint32_t totalPixels=0;//number of pixels to read
- uint32_t pixelRepetition = 1;
- struct BITMAPFILEHEADER source_head;//to store file header
- struct BITMAPINFOHEADER source_info;//to store bitmap info header
- PIXEL pixel;// the current pixel
- FILE *in;// bitmap encoded pointer file
- FILE *out;//decoded bitmap file pointer
- if (!(in = fopen(filePath, "rb"))) {
- printf("ncan not open file");
- exit(-1);
- }
- out = fopen(DECODED_FILE, "wb");
- //read the headers to source file
- fread(&source_head,sizeof(struct BITMAPFILEHEADER),1,in);
- fread(&source_info,sizeof(struct BITMAPINFOHEADER),1,in);
- //write the headers to the output file
- fwrite(&source_head,sizeof(struct BITMAPFILEHEADER),1,out);
- fwrite(&source_info,sizeof(struct BITMAPINFOHEADER),1,out);
- totalPixels=source_info.width*source_info.height;
- while(i < totalPixels) {
- fread(&pixelRepetition, sizeof(pixelRepetition), 1, out);
- fread(&pixel,sizeof(PIXEL),1,in);
- for (j = 0; j < pixelRepetition; j++) {
- fwrite(&pixel,sizeof(PIXEL),1,out);
- }
- i += pixelRepetition;
- }
- fclose(in);
- fclose(out);
- }
- int comparePixels(PIXEL px1, PIXEL px2) {
- if (px1.red == px2.red && px1.green == px2.green && px1.blue == px2.blue) {
- return 0;
- } else {
- return -1;
- }
- }
- fwrite(&runlength, sizeof(runlength), 1, out);
- fwrite(&pixel,sizeof(PIXEL),1,out);
- fwrite(pixArr[i], sizeof(PIXEL),1,out);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement