Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.99 KB | None | 0 0
  1. #pragma once
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #include <iostream>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. struct RGB
  8. {
  9. unsigned char Red = 0;
  10. unsigned char Green = 0;
  11. unsigned char Blue = 0;
  12. };
  13.  
  14. int width_to_int(vector <char> &Width)
  15. {
  16. switch (Width.size()) //перевод из char в int
  17. {
  18. case 2:
  19. return (Width[0] - '0') * 10 + (Width[1] - '0');
  20. case 3:
  21. return (Width[0] - '0') * 100 + (Width[1] - '0') * 10 + (Width[2] - '0');
  22. case 4:
  23. return (Width[0] - '0') * 1000 + (Width[1] - '0') * 100 + (Width[2] - '0') * 10 + (Width[3] - '0');
  24. }
  25. }
  26.  
  27. int height_to_int(vector <char> &Height)
  28. {
  29. switch (Height.size()) //перевод из char в int
  30. {
  31. case 2:
  32. return (Height[0] - '0') * 10 + (Height[1] - '0');
  33. case 3:
  34. return (Height[0] - '0') * 100 + (Height[1] - '0') * 10 + (Height[2] - '0');
  35. case 4:
  36. return (Height[0] - '0') * 1000 + (Height[1] - '0') * 100 + (Height[2] - '0') * 10 + (Height[3] - '0');
  37. }
  38. }
  39. //=========================================================================================================
  40. //Реализация самих функций
  41. void inversion(vector <vector<unsigned char>>& Pixels) //для P5
  42. {
  43. for (int i = 0; i < Pixels.size(); ++i)
  44. for (int j = 0; j < Pixels[i].size(); ++j)
  45. {
  46. int buff = 0;
  47. buff = (int)Pixels[i][j];
  48. Pixels[i][j] = 255 - buff;
  49. }
  50. }
  51. void inversion(vector <vector <struct RGB>>& Pixels) //для P6
  52. {
  53. for (int i = 0; i < Pixels.size(); ++i)
  54. for (int j = 0; j < Pixels[i].size(); ++j)
  55. {
  56. int buff = 0;
  57. buff = (int)Pixels[i][j].Red;
  58. Pixels[i][j].Red = 255 - buff;
  59.  
  60. buff = (int)Pixels[i][j].Green;
  61. Pixels[i][j].Green = 255 - buff;
  62.  
  63. buff = (int)Pixels[i][j].Blue;
  64. Pixels[i][j].Blue = 255 - buff;
  65. }
  66. }
  67.  
  68. void horizontal_reflextion(vector <vector<unsigned char>>& Pixels) //для P5
  69. {
  70. for (int i = 0; i < Pixels.size(); ++i)
  71. for (int j = 0; j < Pixels[i].size() / 2; ++j)
  72. swap(Pixels[i][j], Pixels[i][Pixels[i].size() - j]);
  73. }
  74.  
  75. void horizontal_reflextion(vector <vector<struct RGB>>& Pixels) //для P6
  76. {
  77. for (int i = 0; i < Pixels.size(); ++i)
  78. for (int j = 0; j < Pixels[i].size() / 2; ++j)
  79. {
  80. swap(Pixels[i][j].Red, Pixels[i][Pixels[i].size() - j].Red);
  81. swap(Pixels[i][j].Green, Pixels[i][Pixels[i].size() - j].Green);
  82. swap(Pixels[i][j].Blue, Pixels[i][Pixels[i].size() - j].Blue);
  83. }
  84. }
  85.  
  86. void vertical_reflextion(vector <vector <unsigned char>>& Pixels) //для P5
  87. {
  88. for (int i = 0; i < Pixels.size() / 2; ++i)
  89. for (int j = 0; j < Pixels[i].size(); ++j)
  90. swap(Pixels[i][j], Pixels[Pixels.size() - i][j]);
  91. }
  92.  
  93. void vertical_reflextion(vector <vector <struct RGB>>& Pixels) //для P6
  94. {
  95. for (int i = 0; i < Pixels.size() / 2; ++i)
  96. for (int j = 0; j < Pixels[i].size(); ++j)
  97. {
  98. swap(Pixels[i][j].Red, Pixels[Pixels.size() - i][j].Red);
  99. swap(Pixels[i][j].Green, Pixels[Pixels.size() - i][j].Green);
  100. swap(Pixels[i][j].Blue, Pixels[Pixels.size() - i][j].Blue);
  101. }
  102. }
  103.  
  104. void clock_and_counter_wise_ninety(vector <vector<unsigned char>>& Pixels, int type) //для P5
  105. {
  106. vector <vector <unsigned char>> PixelsCopy(Pixels.size(), vector <unsigned char>(Pixels[0].size()));
  107. //создание копии массива пикселей
  108. for (int i = 0; i < Pixels.size(); ++i)
  109. for (int j = 0; j < Pixels[i].size(); ++j)
  110. PixelsCopy[i][j] = Pixels[i][j]; //непосредственно копирование
  111. Pixels.clear(); //чистка старого массива
  112. Pixels.assign(PixelsCopy[0].size(), vector<unsigned char>(PixelsCopy.size()));
  113. //смена ширины и высоты исходного массива местами
  114.  
  115. for (int i = 0; i < Pixels.size(); ++i)
  116. for (int j = 0; j < Pixels[i].size(); ++j)
  117. {
  118. if (type == 1)
  119. Pixels[i][j] = PixelsCopy[Pixels[i].size() - j][i]; //смена пикселей по часовой
  120. if (type == 2)
  121. Pixels[i][j] = PixelsCopy[j][Pixels[i].size() - i]; //смена пикселей против часовой
  122. }
  123.  
  124. PixelsCopy.clear(); //чистка копии
  125. }
  126.  
  127. void clock_and_counter_wise_ninety(vector <vector<struct RGB>>& Pixels, int type) //для P6
  128. {
  129. vector <vector <struct RGB>> PixelsCopy(Pixels.size(), vector <struct RGB>(Pixels[0].size()));
  130. //создание копии массива пикселей
  131. for (int i = 0; i < Pixels.size(); ++i)
  132. for (int j = 0; j < Pixels[i].size(); ++j) //непосредственно копирование
  133. {
  134. PixelsCopy[i][j].Red = Pixels[i][j].Red;
  135. PixelsCopy[i][j].Green = Pixels[i][j].Green;
  136. PixelsCopy[i][j].Blue = Pixels[i][j].Blue;
  137. }
  138. Pixels.clear(); //чистка старого массива
  139. Pixels.assign(PixelsCopy[0].size(), vector<struct RGB>(PixelsCopy.size()));
  140. //смена ширины и высоты исходного массива местами
  141.  
  142. for (int i = 0; i < Pixels.size(); ++i)
  143. for (int j = 0; j < Pixels[i].size(); ++j) //смена пикселей
  144. {
  145. if (type == 1)
  146. {
  147. Pixels[i][j].Red = PixelsCopy[Pixels[i].size() - j][i].Red;
  148. Pixels[i][j].Green = PixelsCopy[Pixels[i].size() - j][i].Green;
  149. Pixels[i][j].Blue = PixelsCopy[Pixels[i].size() - j][i].Blue;
  150. }
  151. if (type == 2)
  152. {
  153. Pixels[i][j].Red = PixelsCopy[j][Pixels[i].size() - i].Red;
  154. Pixels[i][j].Green = PixelsCopy[j][Pixels[i].size() - i].Green;
  155. Pixels[i][j].Blue = PixelsCopy[j][Pixels[i].size() - i].Blue;
  156. }
  157. }
  158.  
  159. PixelsCopy.clear(); //чистка копии
  160. }
  161.  
  162. int main(int argc, char* argv[])
  163. {
  164. //Часть 1. Считка формата файла, ширины и высоты, глубины цвета. Преобразование данных из char в int
  165. FILE* Source = fopen(argv[1], "rb");
  166. if (Source == NULL)
  167. {
  168. cout << "Warning! File didn`t open";
  169. return 1;
  170. }
  171.  
  172. if (argc != 4)
  173. {
  174. cout << "Not enough parametrs!";
  175. return 1;
  176. }
  177.  
  178. char Format[2];
  179. char buff;
  180. for (int i = 0; i < 2; ++i) //считка формата pnm файла. For exp. P5, P6 and etc.
  181. Format[i] = fgetc(Source);
  182.  
  183. buff = fgetc(Source); //символ переноса строки
  184.  
  185. vector <char> Width(1);
  186. while (true) //считка ширины в char
  187. {
  188. buff = fgetc(Source);
  189. if (buff == ' ') break;
  190. Width.push_back(buff);
  191. }
  192. buff = fgetc(Source); //считка пробела между шириной и высотой
  193. int width = width_to_int(Width);
  194. Width.clear(); //очистка памяти
  195.  
  196. vector <char> Height(1);
  197. while (true) //считка высоты в char
  198. {
  199. buff = fgetc(Source);
  200. if (buff == '0a') break;
  201. Height.push_back(buff);
  202. }
  203. buff = fgetc(Source); //считка переноса строки
  204. int height = height_to_int(Height);
  205. Height.clear(); //очистка памяти
  206.  
  207. char ColorDepth[3];
  208. for (int i = 0; i < 3; ++i)
  209. ColorDepth[i] = fgetc(Source); //считка значения глубины цвета в char
  210. if (((ColorDepth[0] - '0') * 100 + (ColorDepth[1] - '0') * 10 + (ColorDepth[2] - '0')) != 255)
  211. //перевод значения глубины цвета из char в int и сравнение с 255
  212. {
  213. cout << "Warning! Incorrect ColorDepth. Must be equal 255";
  214. return 1;
  215. }
  216. buff = fgetc(Source); //считка символа переноса строки
  217.  
  218. //Часть 2. Работа с матрицей пикселей. Считка и преобразование char в int
  219.  
  220. if (Format[1] == '5') //если файл в градации серого
  221. {
  222. vector <vector <unsigned char>> Pixels(height, vector<unsigned char>(width));
  223. for (int i = 0; i < height; ++i)
  224. for (int j = 0; j < width; ++j)
  225. {
  226. Pixels[i][j] = 0;
  227. buff = fgetc(Source);
  228. Pixels[i][j] = buff;
  229. }
  230. fclose(Source); //после считки данных, файл больше не нужен, закрываем
  231. switch ((int)argv[3])
  232. {
  233. case 0:
  234. inversion(Pixels);
  235. break;
  236. case 1:
  237. horizontal_reflextion(Pixels);
  238. break;
  239. case 2:
  240. vertical_reflextion(Pixels);
  241. break;
  242. case 3:
  243. clock_and_counter_wise_ninety(Pixels, 1);
  244. break;
  245. case 4:
  246. clock_and_counter_wise_ninety(Pixels, 2);
  247. break;
  248. }
  249.  
  250. }
  251. else if (Format[1] == '6') //если цветное
  252. {
  253. vector <vector <struct RGB>> Pixels(height, vector<struct RGB>(width));
  254.  
  255. for (int i = 0; i < height; ++i)
  256. for (int j = 0; j < width; ++j)
  257. {
  258. buff = fgetc(Source); //считка 1 байта пикселя
  259. Pixels[i][j].Red = buff;
  260.  
  261. buff = fgetc(Source); //считка 2 байта пикселя
  262. Pixels[i][j].Green = buff;
  263.  
  264. buff = fgetc(Source); //считка 3 байта пикселя
  265. Pixels[i][j].Blue = buff;
  266. }
  267.  
  268. switch ((int)argv[3])
  269. {
  270. case 0:
  271. inversion(Pixels);
  272. break;
  273. case 1:
  274. horizontal_reflextion(Pixels);
  275. break;
  276. case 2:
  277. vertical_reflextion(Pixels);
  278. break;
  279. case 3:
  280. clock_and_counter_wise_ninety(Pixels, 1);
  281. break;
  282. case 4:
  283. clock_and_counter_wise_ninety(Pixels, 2);
  284. break;
  285. }
  286. }
  287. return 0;
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement