Advertisement
Guest User

Untitled

a guest
Jan 11th, 2020
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. struct BITMAPFILEHEADER
  7. {
  8. int bfType;
  9. int bfSize;
  10. int bfReserved1;
  11. int bfReserved2;
  12. int bfOffBits;
  13. };
  14.  
  15. struct BITMAPINFOHEADER
  16. {
  17. int biSize;
  18. int biWidth;
  19. int biHeight;
  20. int biPlanes;
  21. int biBitCount;
  22. int biCompression;
  23. int biSizeImage;
  24. int biXpelsPerMeter;
  25. int biYpelsPerMeter;
  26. int biCrlUses;
  27. int biCrlImportant;
  28. };
  29.  
  30. struct kolor
  31. {
  32. unsigned char R;
  33. unsigned char G;
  34. unsigned char B;
  35. };
  36.  
  37. void odczytaj_naglowek(fstream &obraz,BITMAPFILEHEADER &ob)
  38. {
  39.  
  40. obraz.read(reinterpret_cast<char*>(&ob.bfType), 2);
  41. obraz.read(reinterpret_cast<char*>(&ob.bfSize), 4);
  42. obraz.read(reinterpret_cast<char*>(&ob.bfReserved1), 2);
  43. obraz.read(reinterpret_cast<char*>(&ob.bfReserved2), 2);
  44. obraz.read(reinterpret_cast<char*>(&ob.bfOffBits), 4);
  45.  
  46. }
  47.  
  48. int odczytaj_naglowek_obrazu(fstream &obraz,BITMAPINFOHEADER &ob)
  49. {
  50.  
  51. obraz.read(reinterpret_cast<char*>(&ob.biSize), 4);
  52. obraz.read(reinterpret_cast<char*>(&ob.biWidth ), 4);
  53. obraz.read(reinterpret_cast<char*>(&ob.biHeight), 4);
  54. obraz.read(reinterpret_cast<char*>(&ob.biPlanes), 2);
  55. obraz.read(reinterpret_cast<char*>(&ob.biBitCount), 2);
  56. obraz.read(reinterpret_cast<char*>(&ob.biCompression), 4);
  57. obraz.read(reinterpret_cast<char*>(&ob.biSizeImage), 4);
  58. obraz.read(reinterpret_cast<char*>(&ob.biXpelsPerMeter), 4);
  59. obraz.read(reinterpret_cast<char*>(&ob.biYpelsPerMeter), 4);
  60. obraz.read(reinterpret_cast<char*>(&ob.biCrlUses), 4);
  61. obraz.read(reinterpret_cast<char*>(&ob.biCrlImportant), 4);
  62.  
  63. return obraz.tellg();
  64. }
  65.  
  66. int policz(int i, char rgb, kolor tab[])
  67. {
  68. int p;
  69. switch (rgb)
  70. {
  71. case 'r':
  72. p = tab[i-1].R+3;
  73. if(p>255)
  74. {
  75. return 255;
  76. }
  77. else return p;
  78. break;
  79. case 'g':
  80. p= tab[i-1].G+3;
  81. if(p>255)
  82. {
  83. return 255;
  84. }
  85. else return p;
  86. break;
  87. case 'b':
  88. p= tab[i-1].B+3;;
  89. if(p>255)
  90. {
  91. return 255;
  92. }
  93. else return p;
  94. break;
  95. }
  96. }
  97.  
  98. void odczytaj_obraz(fstream &obraz,unsigned char tab[], BITMAPINFOHEADER ob)
  99. {
  100.  
  101. for(int i=0; i<ob.biSizeImage/3; i++)
  102. {
  103. obraz.read(reinterpret_cast<char*>(&tab[i]),3);
  104. }
  105.  
  106. obraz.clear();
  107.  
  108. }
  109. void koloruj(unsigned char tab[], kolor tab_k[], BITMAPINFOHEADER ob)
  110. {
  111. char rgb;
  112. for(int i=0; i<ob.biSizeImage/3; i++)
  113. {
  114.  
  115.  
  116.  
  117. if(tab[i]==0)
  118. {
  119. tab_k[i].R=0;
  120. tab_k[i].G=0;
  121. tab_k[i].B=0;
  122.  
  123. }
  124. NULL;
  125. if(tab[i]>0 && tab[i]<86)
  126. {
  127. rgb = 'r';
  128. tab_k[i].R=policz(i, rgb,tab_k);
  129. tab_k[i].G=0;
  130. tab_k[i].B=0;
  131. }
  132. NULL;
  133. if(tab[i]==86)
  134. {
  135. tab_k[i].R=255;
  136. tab_k[i].G=0;
  137. tab_k[i].B=0;
  138. }
  139. NULL;
  140. if(tab[i]>86 && tab[i]<171)
  141. {
  142. rgb = 'b';
  143. tab_k[i].R=255;
  144. tab_k[i].G=policz(i, rgb,tab_k);
  145. tab_k[i].B=0;
  146. }
  147. NULL;
  148. if(tab[i]==171)
  149. {
  150. tab_k[i].R=255;
  151. tab_k[i].G=255;
  152. tab_k[i].B=0;
  153. }
  154. NULL;
  155. if(tab[i]>171 && tab[i]<256)
  156. {
  157. rgb = 'b';
  158. tab_k[i].R=255;
  159. tab_k[i].G=255;
  160. tab_k[i].B=policz(i, rgb, tab_k);
  161. }
  162. NULL;
  163. }
  164. }
  165.  
  166. void skopuj_obraz(fstream &obraz_src,fstream &obraz_des,kolor tab[], BITMAPINFOHEADER ob, BITMAPFILEHEADER ob1)
  167. {
  168. obraz_src.seekg(0);
  169. obraz_des.seekg(0);
  170.  
  171. int k;
  172.  
  173.  
  174. for(int i=0; i<ob1.bfOffBits; i++)
  175. {
  176. obraz_src.read(reinterpret_cast<char*>(&k),1);
  177. obraz_des.write(reinterpret_cast<char*>(&k),1);
  178. }
  179.  
  180. for(int i=0; i<ob.biSizeImage/3; i++)
  181. {
  182. obraz_des.put(tab[i].B);
  183. obraz_des.put(tab[i].G);
  184. obraz_des.put(tab[i].R);
  185. }
  186.  
  187. }
  188.  
  189. int main()
  190. {
  191. fstream obraz;
  192. BITMAPFILEHEADER FileInfo;
  193. BITMAPINFOHEADER PictureInfo;
  194.  
  195. obraz.open("sonar_aktywny_szary.bmp", ios::in | ios::binary);
  196. if(!obraz)
  197. {
  198. cout<<"Nie mozna otworzyc pliku!"<<endl;
  199. return 0;
  200. }
  201.  
  202. odczytaj_naglowek(obraz,FileInfo);
  203. odczytaj_naglowek_obrazu(obraz,PictureInfo);
  204.  
  205.  
  206.  
  207. unsigned char *pixmap;
  208. pixmap=new unsigned char[PictureInfo.biSizeImage/3];
  209.  
  210. kolor *kolormap;
  211. kolormap=new kolor [PictureInfo.biSizeImage/3];
  212.  
  213. odczytaj_obraz(obraz,pixmap, PictureInfo);
  214. koloruj(pixmap, kolormap, PictureInfo);
  215.  
  216. fstream obraz_des;
  217.  
  218. obraz_des.open("sonar_aktywny_kolorowy2.bmp", ios::out | ios::binary);
  219. if(!obraz_des)
  220. {
  221. cout<<"Nie mozna otworzyc pliku!"<<endl;
  222. return 0;
  223. }
  224.  
  225.  
  226. skopuj_obraz(obraz,obraz_des,kolormap,PictureInfo,FileInfo);
  227.  
  228.  
  229. obraz.close();
  230. obraz_des.close();
  231.  
  232.  
  233. return 0;
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement