Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. #include<stdlib.h>
  2. #include <stdint.h>
  3.  
  4. //structure defiens bitmap header
  5. struct BITMAPFILEHEADER{
  6. uint8_t type[2];//type of file (bit map)
  7. uint32_t size;//size of file
  8. uint16_t reserved1;//
  9. uint16_t reserved2;//
  10. uint32_t offsetbits;//off set bits
  11. } __attribute__ ((packed));
  12.  
  13. struct BITMAPINFOHEADER{
  14. uint32_t size;//bitmap size
  15. // uint16_t w2;
  16. uint32_t width;//width of bitmap
  17. //uint16_t h2;
  18. uint32_t height;//hight of bitmap
  19.  
  20. uint16_t planes;
  21. uint16_t bitcount;
  22. uint32_t compression;// compression ratio (zero for no compression)
  23. uint32_t sizeimage;//size of image
  24. long xpelspermeter;
  25. long ypelspermeter;
  26. uint32_t colorsused;
  27. uint32_t colorsimportant;
  28. } __attribute__ ((packed));
  29.  
  30.  
  31. //const char* INPUT_FILE = "/home/bogdan/bee.bmp";
  32. const char* INPUT_FILE = "/home/bogdan/Linux.bmp";
  33. const char* ENCODED_FILE = "/home/bogdan/encoded.bin";
  34. const char* DECODED_FILE = "/home/bogdan/decoded.bmp";
  35.  
  36. typedef struct SINGLE_PIXEL{
  37. uint8_t green;//Green level 0-255
  38. uint8_t red; //Red level 0-255
  39. } PIXEL;
  40.  
  41. int comparePixels(PIXEL, PIXEL);
  42. void encode();
  43. void decode(char*);
  44.  
  45. int main()
  46. {
  47. encode();
  48. decode(ENCODED_FILE);
  49. return 0;
  50. }
  51.  
  52. void encode() {
  53. uint32_t i=0;//to count pixels read
  54. uint32_t pixno=0;//number of pixels to read
  55.  
  56. struct BITMAPFILEHEADER source_head;//to store file header
  57. struct BITMAPINFOHEADER source_info;//to store bitmap info header
  58. PIXEL pixel;// the current pixel
  59.  
  60. FILE *in;// bitmap imput pointer file
  61. FILE *out;//output file pointer
  62.  
  63. if(!(in=fopen(INPUT_FILE,"rb")))//open in binary read mode
  64. {
  65. printf("ncan not open file");//error at opening file
  66. exit(-1);
  67. }
  68.  
  69.  
  70. out=fopen(ENCODED_FILE,"wb");//opne in binary write mode
  71. //read the headers to source file
  72. fread(&source_head,sizeof(struct BITMAPFILEHEADER),1,in);
  73. fread(&source_info,sizeof(struct BITMAPINFOHEADER),1,in);
  74.  
  75. //write the headers to the output file
  76. fwrite(&source_head,sizeof(struct BITMAPFILEHEADER),1,out);
  77. fwrite(&source_info,sizeof(struct BITMAPINFOHEADER),1,out);
  78.  
  79. //cumpute the number of pixels to read
  80. pixno=source_info.width*source_info.height;
  81.  
  82. // init list of pixels
  83. PIXEL pixArr[pixno];
  84. printf("total pixels: %d", pixno);
  85.  
  86. //printf("w:%f h:%u pn:%lu", (source_head.size/1024.0/1024), source_info.height, pixno);
  87. uint32_t sum = 0;
  88. //read, modify and write pixels
  89. for(i=0;i<pixno;++i)
  90. {
  91. //read pixel form source file
  92. fread(&pixel,sizeof(PIXEL),1,in);
  93. pixArr[i] = pixel;
  94. }
  95. for (i = 0; i < pixno; i++) {
  96. // printf ("i = %dtred = %d green = %d blue = %dn",i, pixArr[i].red, pixArr[i].green, pixArr[i].blue);
  97. int runlength = 1;
  98. while ((i + 1) < pixno) {
  99. if (comparePixels(pixArr[i], pixArr[i+1]) == 0){
  100. // printf ("i = %dt red = %d green = %d blue = %dn",i, pixArr[i].red, pixArr[i].green, pixArr[i].blue);
  101. runlength++;
  102. i++;
  103. } else {
  104. break;
  105. }
  106. }
  107.  
  108. //fprintf(out, "%d", runlength);
  109. fwrite(&runlength, sizeof(runlength), 1, out);
  110. fwrite(&pixel,sizeof(PIXEL),1,out);
  111. sum += runlength;
  112. runlength = 0;
  113. }
  114. //write the modification to the output file
  115. //close all fiels
  116. fclose(in);
  117. fclose(out);
  118. printf("sum = %d",sum);
  119. }
  120.  
  121. void decode(char * filePath) {
  122. uint32_t i=0;//to count pixels read
  123. uint32_t j=0;
  124. uint32_t totalPixels=0;//number of pixels to read
  125. uint32_t pixelRepetition = 1;
  126. struct BITMAPFILEHEADER source_head;//to store file header
  127. struct BITMAPINFOHEADER source_info;//to store bitmap info header
  128. PIXEL pixel;// the current pixel
  129.  
  130. FILE *in;// bitmap encoded pointer file
  131. FILE *out;//decoded bitmap file pointer
  132. if (!(in = fopen(filePath, "rb"))) {
  133. printf("ncan not open file");
  134. exit(-1);
  135. }
  136. out = fopen(DECODED_FILE, "wb");
  137. //read the headers to source file
  138. fread(&source_head,sizeof(struct BITMAPFILEHEADER),1,in);
  139. fread(&source_info,sizeof(struct BITMAPINFOHEADER),1,in);
  140.  
  141. //write the headers to the output file
  142. fwrite(&source_head,sizeof(struct BITMAPFILEHEADER),1,out);
  143. fwrite(&source_info,sizeof(struct BITMAPINFOHEADER),1,out);
  144.  
  145. totalPixels=source_info.width*source_info.height;
  146.  
  147. while(i < totalPixels) {
  148.  
  149. fread(&pixelRepetition, sizeof(pixelRepetition), 1, out);
  150. fread(&pixel,sizeof(PIXEL),1,in);
  151.  
  152. for (j = 0; j < pixelRepetition; j++) {
  153. fwrite(&pixel,sizeof(PIXEL),1,out);
  154. }
  155. i += pixelRepetition;
  156. }
  157.  
  158. fclose(in);
  159. fclose(out);
  160. }
  161.  
  162. int comparePixels(PIXEL px1, PIXEL px2) {
  163. if (px1.red == px2.red && px1.green == px2.green && px1.blue == px2.blue) {
  164. return 0;
  165. } else {
  166. return -1;
  167. }
  168. }
  169.  
  170. fwrite(&runlength, sizeof(runlength), 1, out);
  171. fwrite(&pixel,sizeof(PIXEL),1,out);
  172.  
  173. fwrite(pixArr[i], sizeof(PIXEL),1,out);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement