Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. struct BitMap {
  6.  
  7. short int Signature;
  8. int FileSize;
  9. short int Reserved1;
  10. short int Reserved2;
  11. int Offset;
  12.  
  13. int HeaderSize;
  14. int Width;
  15. int Height;
  16. short int Planes;
  17. short int BitsPerPixel;
  18. int Compression;
  19. int ImageSize;
  20. int XPixelsPerMeter;
  21. int YPixelsPerMeter;
  22. int ColorsInColorTable;
  23. int ImportantColorCount;
  24.  
  25. char *rest;
  26.  
  27.  
  28. char *ColorTable;
  29.  
  30. char *GAP1;
  31. struct Pix*PixelArray;
  32.  
  33.  
  34.  
  35. };
  36.  
  37. struct Pix {
  38. char R;
  39. char G;
  40. char B;
  41.  
  42.  
  43. };
  44.  
  45. /*void PixOne (char r0, char g0, char b0, struct Pix p){
  46. p.R=r0;
  47. p.G=g0;
  48. p.B=b0;
  49. };*/
  50.  
  51. struct Pix getPix (int x, int y, struct BitMap bm)
  52. {
  53. //struct Pix p;
  54.  
  55. return bm.PixelArray [(bm.Height - y - 1)*bm.Width*3 + x];
  56.  
  57. }
  58.  
  59. void setPix (int x, int y, struct BitMap bm, struct Pix p)
  60. {
  61. y = bm.Height - y - 1;
  62. bm.PixelArray [y*bm.Width*3 + x].R = p.R;
  63. (bm.PixelArray [y*bm.Width*3 + x]).G = p.G;
  64. (bm.PixelArray [y*bm.Width*3 + x]).B = p.B;
  65. }
  66.  
  67. struct BitMap readBitMap(FILE *BMPFile) {
  68. struct BitMap x;
  69.  
  70. fread(&x.Signature,2,1,BMPFile);
  71. fread(&x.FileSize,4,1,BMPFile);
  72. fread(&x.Reserved1,2,1,BMPFile);
  73. fread(&x.Reserved2,2,1,BMPFile);
  74. fread(&x.Offset,4,1,BMPFile);
  75.  
  76. fread(&x.HeaderSize,4,1,BMPFile);
  77. fread(&x.Width,4,1,BMPFile);
  78. fread(&x.Height,4,1,BMPFile);
  79. fread(&x.Planes,2,1,BMPFile);
  80. fread(&x.BitsPerPixel,2,1,BMPFile);
  81. fread(&x.Compression,4,1,BMPFile);
  82. fread(&x.ImageSize,4,1,BMPFile);
  83. fread(&x.XPixelsPerMeter,4,1,BMPFile);
  84. fread(&x.YPixelsPerMeter,4,1,BMPFile);
  85. fread(&x.ColorsInColorTable,4,1,BMPFile);
  86. fread(&x.ImportantColorCount,4,1,BMPFile);
  87.  
  88. x.rest = (char*) malloc(x.HeaderSize-40);
  89.  
  90. //printf("%d%n",x.ImageSize);
  91.  
  92. //printf("%d%n",(x.Width*3+2)*x.Height);
  93. fread(x.rest,x.HeaderSize-40,1,BMPFile);
  94.  
  95.  
  96. x.ColorTable = (char*) malloc(x.ColorsInColorTable*4);
  97.  
  98. fread(x.ColorTable,x.ColorsInColorTable*4,1,BMPFile);
  99.  
  100. x.GAP1 = (char*) malloc(x.Offset-x.HeaderSize-14-x.ColorsInColorTable*4);
  101.  
  102. fread(x.GAP1,x.Offset-x.HeaderSize-14-x.ColorsInColorTable*4,1,BMPFile);
  103.  
  104. int JunkBytes = 0;
  105.  
  106. if ((4-(x.Width*3)%4) !=4)
  107. JunkBytes = 4-(x.Width*3)%4;
  108.  
  109. fseek(BMPFile, x.Offset, SEEK_SET);
  110. x.PixelArray = (struct Pix*) malloc(x.Width*x.Height*3);
  111. int i;
  112. for (i = 0; i < x.Height; i++){
  113. fread(x.PixelArray+3*x.Width*i,3,x.Width,BMPFile);
  114. fseek(BMPFile,JunkBytes,SEEK_CUR);
  115. }
  116. return x;
  117.  
  118. }
  119.  
  120. void writeBitMap(FILE *BMPFile,struct BitMap bm) {
  121.  
  122. fwrite(&bm.Signature,2,1,BMPFile);
  123. fwrite(&bm.FileSize,4,1,BMPFile);
  124. fwrite(&bm.Reserved1,2,1,BMPFile);
  125. fwrite(&bm.Reserved2,2,1,BMPFile);
  126. fwrite(&bm.Offset,4,1,BMPFile);
  127.  
  128. fwrite(&bm.HeaderSize,4,1,BMPFile);
  129. fwrite(&bm.Width,4,1,BMPFile);
  130. fwrite(&bm.Height,4,1,BMPFile);
  131. fwrite(&bm.Planes,2,1,BMPFile);
  132. fwrite(&bm.BitsPerPixel,2,1,BMPFile);
  133. fwrite(&bm.Compression,4,1,BMPFile);
  134. fwrite(&bm.ImageSize,4,1,BMPFile);
  135. fwrite(&bm.XPixelsPerMeter,4,1,BMPFile);
  136. fwrite(&bm.YPixelsPerMeter,4,1,BMPFile);
  137. fwrite(&bm.ColorsInColorTable,4,1,BMPFile);
  138. fwrite(&bm.ImportantColorCount,4,1,BMPFile);
  139. fwrite(bm.rest,bm.HeaderSize-40,1,BMPFile);
  140.  
  141.  
  142.  
  143. fwrite(bm.ColorTable,bm.ColorsInColorTable*4,1,BMPFile);
  144.  
  145.  
  146.  
  147. fwrite(bm.GAP1,bm.Offset-bm.HeaderSize-14-bm.ColorsInColorTable*4,1,BMPFile);
  148.  
  149. int JunkBytes = 0;
  150.  
  151. if ((4-(bm.Width*3)%4) !=4)
  152. JunkBytes = 4-(bm.Width*3)%4;
  153. fseek(BMPFile, bm.Offset, SEEK_SET);
  154. int i;
  155. for (i = 0; i < bm.Height; i++){
  156. fwrite(bm.PixelArray+3*bm.Width*i,3,bm.Width,BMPFile);
  157.  
  158. int x=0;
  159.  
  160. int j=0;
  161. for (j = 0; j<JunkBytes;j++)
  162. fwrite(&x,1,1,BMPFile);
  163. }
  164.  
  165.  
  166.  
  167.  
  168. }
  169.  
  170.  
  171.  
  172.  
  173. int main(void) {
  174.  
  175. if (sizeof(int)!=4) {
  176. printf ("error");
  177. }
  178.  
  179. else {
  180. struct BitMap x;
  181.  
  182. FILE *f;
  183. FILE *f1;
  184. f = fopen("mapa.bmp","rb");
  185.  
  186. if(f!= NULL) {
  187. x = readBitMap(f);
  188.  
  189. fclose(f);
  190. }
  191.  
  192. else {
  193. printf ("bad file");
  194.  
  195. }
  196. struct Pix p;
  197. p = getPix (6,6,x);
  198. setPix(0,13,x,p);
  199.  
  200. f1 = fopen("mapa.bmp","wb");
  201. writeBitMap(f1,x);
  202.  
  203. fclose(f1);
  204.  
  205. }
  206.  
  207. return 0;
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement