Advertisement
Guest User

Untitled

a guest
May 21st, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.64 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <cstdlib>
  3. #include <cstdio>
  4. #include <cstdarg>
  5. #include <getopt.h>
  6. #include <cstring>
  7. #include <cctype>
  8. #include <algorithm>
  9. #include <cmath>
  10. #include <string>
  11.  
  12. using namespace std;
  13.  
  14. #define PNG_DEBUG 3
  15. #include <png.h>
  16.  
  17. struct Png{
  18. int width, height;
  19. png_byte color_type;
  20. png_byte bit_depth;
  21.  
  22. png_structp png_ptr;
  23. png_infop info_ptr;
  24. int number_of_passes;
  25. png_bytep *row_pointers;
  26. };
  27.  
  28. struct args{
  29.  
  30. int start[2];
  31. int end[2];
  32. int nevv[2];
  33. int origin[4];
  34. int chosen[4];
  35. int width;
  36. int type;
  37. };
  38.  
  39.  
  40. void read_png_file(char *file_name, struct Png *image) {
  41. int x,y;
  42. unsigned char header[8]; // 8 is the maximum size that can be checked
  43.  
  44. /* open file and test for it being a png */
  45. FILE *fp = fopen(file_name, "rb");
  46. if (!fp){
  47. // Some error handling: file could not be opened
  48. }
  49.  
  50. fread(header, 1, 8, fp);
  51. if (png_sig_cmp(header, 0, 8)){
  52. // Some error handling: file is not recognized as a PNG
  53. }
  54.  
  55. /* initialize stuff */
  56.  
  57. image->png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  58.  
  59. if (!image->png_ptr){
  60. // Some error handling: png_create_read_struct failed
  61. }
  62.  
  63. image->info_ptr = png_create_info_struct(image->png_ptr);
  64. if (!image->info_ptr){
  65. // Some error handling: png_create_info_struct failed
  66. }
  67.  
  68. if (setjmp(png_jmpbuf(image->png_ptr))){
  69. // Some error handling: error during init_io
  70. //png_destroy_read_struct(image->png_ptr, image->info_ptr,
  71. // (png_infopp) NULL);
  72. //fclose(fp);
  73. //return ERROR;
  74. }
  75.  
  76. png_init_io(image->png_ptr, fp);
  77. png_set_sig_bytes(image->png_ptr, 8);
  78.  
  79. png_read_info(image->png_ptr, image->info_ptr);
  80.  
  81. image->width = png_get_image_width(image->png_ptr, image->info_ptr);
  82. image->height = png_get_image_height(image->png_ptr, image->info_ptr);
  83. image->color_type = png_get_color_type(image->png_ptr, image->info_ptr);
  84. image->bit_depth = png_get_bit_depth(image->png_ptr, image->info_ptr);
  85.  
  86. image->number_of_passes = png_set_interlace_handling(image->png_ptr);
  87. png_read_update_info(image->png_ptr, image->info_ptr);
  88.  
  89. /* read file */
  90. if (setjmp(png_jmpbuf(image->png_ptr))){
  91. // Some error handling: error during read_image
  92. }
  93.  
  94. image->row_pointers = (png_bytep *) malloc(sizeof(png_bytep) * image->height);
  95. for (y = 0; y < image->height; y++)
  96. image->row_pointers[y] = (png_byte *) malloc(png_get_rowbytes(image->png_ptr, image->info_ptr));
  97.  
  98. png_read_image(image->png_ptr, image->row_pointers);
  99.  
  100. fclose(fp);
  101. }
  102.  
  103.  
  104. void write_png_file(char *file_name, struct Png *image) {
  105. int x,y;
  106. /* create file */
  107. FILE *fp = fopen(file_name, "wb");
  108. if (!fp){
  109. // Some error handling: file could not be opened
  110. }
  111.  
  112. /* initialize stuff */
  113. image->png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  114.  
  115. if (!image->png_ptr){
  116. // Some error handling: png_create_write_struct failed
  117. }
  118.  
  119. image->info_ptr = png_create_info_struct(image->png_ptr);
  120. if (!image->info_ptr){
  121. // Some error handling: png_create_info_struct failed
  122. }
  123.  
  124. if (setjmp(png_jmpbuf(image->png_ptr))){
  125. // Some error handling: error during init_io
  126. }
  127.  
  128. png_init_io(image->png_ptr, fp);
  129.  
  130.  
  131. /* write header */
  132. if (setjmp(png_jmpbuf(image->png_ptr))){
  133. // Some error handling: error during writing header
  134. }
  135.  
  136. png_set_IHDR(image->png_ptr, image->info_ptr, image->width, image->height,
  137. image->bit_depth, image->color_type, PNG_INTERLACE_NONE,
  138. PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
  139.  
  140. png_write_info(image->png_ptr, image->info_ptr);
  141.  
  142.  
  143. /* write bytes */
  144. if (setjmp(png_jmpbuf(image->png_ptr))){
  145. // Some error handling: error during writing bytes
  146. }
  147.  
  148. png_write_image(image->png_ptr, image->row_pointers);
  149.  
  150.  
  151. /* end write */
  152. if (setjmp(png_jmpbuf(image->png_ptr))){
  153. // Some error handling: error during end of write
  154. }
  155.  
  156. png_write_end(image->png_ptr, NULL);
  157.  
  158. /* cleanup heap allocation */
  159. for (y = 0; y < image->height; y++)
  160. free(image->row_pointers[y]);
  161. free(image->row_pointers);
  162.  
  163. fclose(fp);
  164. }
  165.  
  166. void printHelp()
  167. {
  168. printf("Справка\n");
  169. printf("--copy(-c) - делает копию заданного участка. Участок задается при помощи -S и -E области-источника и -N области-назначения.\n");
  170. printf("(Пример) --copy -S 100 200 -E 500 600 -N 0 0 fileOut.png");
  171. printf("--swap_color(-s) - Заменяет все пиксели одного заданного цвета на другой цвет. Цвет задается в формате RGB. -O(буква) для исходного цвета и (-C) для нового.\n");
  172. printf("(Пример) --swap_color -O 125 10 30 -C 200 162 3 fileOut.png\n");
  173. printf("--frame(-f) - Делает рамку в виде узора. Цвет задается при помощи -C, а ширина узора - -W, а тип рамки при помощи -T. \n");
  174. printf("(Пример) --frame -C 124 32 0 -W 30 -T 2 fileOut.png\n");
  175. printf("--highlight_rect(-h) - Поиск всех залитых прямоугольников заданного цвета. Задается цвет прямоугольников -O, цвет обводки -C, толщина линии -W.\n");
  176. printf("(Пример) --highlight_rect -R 100 156 200 -F 0 0 0 -W 5 fileOut.png\n");
  177. printf("--start(-S) - считывет координаты *целый числа* верхнего левого угла прямоугольной области (-С 32 23)\n");
  178. printf("--end(-E) - считывет координаты *целый числа* нижнего правого угла прямоугольной области (-E 32 23)\n");
  179. printf("--new(-N) - считывет координаты *целый числа* верхнего левого угла области-назначения (-N 0 0)\n");
  180. printf("--origin(-O) - считывет исходный цвет в формате RGB (-O 125 25 86)\n");
  181. printf("--chosen(-C) - считывет выбранный цвет в формате RGB (-C 0 25 86)\n");
  182. printf("--width(-W) - считывает значение ширины в пикселях (-W 10)\n");
  183. printf("--type(-T) - считывает тип рамки (всего возможно 3 типа). По умолчанию стоит рамка с типом 1.\n");
  184. printf("--info(-i) - выводит информацию о входном файле\n");
  185. printf("Строка команд должна заканчиваться названием файла вывода\n");
  186. }
  187.  
  188. int isNumber(char *n)
  189. {
  190. for(int i = 0; i < strlen(n); i++)
  191. if(!isdigit(n[i]))
  192. return 0;
  193. return 1;
  194. }
  195.  
  196. void swap_colors(struct Png *image, int orr, int og, int ob, int cr, int cg, int cb) {
  197. int x,y;
  198. if (png_get_color_type(image->png_ptr, image->info_ptr) == PNG_COLOR_TYPE_RGB){
  199. // Some error handling: input file is PNG_COLOR_TYPE_RGB but must be PNG_COLOR_TYPE_RGBA
  200. }
  201.  
  202. if (png_get_color_type(image->png_ptr, image->info_ptr) != PNG_COLOR_TYPE_RGBA){
  203. // Some error handling: color_type of input file must be PNG_COLOR_TYPE_RGBA
  204. }
  205.  
  206. for (y = 0; y < image->height; y++) {
  207. png_byte *row = image->row_pointers[y];
  208. for (x = 0; x < image->width; x++) {
  209. png_byte *ptr = &(row[x * 4]);
  210. if ((int) ptr[0] == orr && (int) ptr[1] == og && (int) ptr[2] == ob) {
  211. ptr[0] = static_cast<png_byte>(cr);
  212. ptr[1] = static_cast<png_byte>(cg);
  213. ptr[2] = static_cast<png_byte>(cb);
  214. }
  215. }
  216. }
  217. }
  218.  
  219. void putPixel(png_byte *cur_row, int x, int y, int r, int g, int b, int a = 255) {
  220. png_byte *ptr = &(cur_row[(x*4)]);
  221. ptr[0] = static_cast<png_byte>(r);
  222. ptr[1] = static_cast<png_byte>(g);
  223. ptr[2] = static_cast<png_byte>(b);
  224. ptr[3] = static_cast<png_byte>(a);
  225. }
  226.  
  227. void copyAndPaste(struct Png *image, int Sx, int Sy, int Fx, int Fy, int Nx, int Ny)
  228. {
  229. int x, y;
  230. int lenX = Fx - Sx + 1;
  231. int lenY = Fy - Sy + 1;
  232. png_bytep *new_row_pointers =(png_bytep *) malloc(sizeof(png_bytep) * lenY);
  233. for(y = 0; y < lenY; y++)
  234. {
  235. new_row_pointers[y] = (png_byte *)malloc(sizeof(png_byte)*4 * lenX);
  236.  
  237. png_byte *new_row = new_row_pointers[y];
  238. png_byte *cur_row = image->row_pointers[Sy + y];
  239.  
  240. for (x = 0; x < lenX; x++)
  241. {
  242. png_byte *ptr = &(cur_row[((Sx+x)*4)]);
  243. putPixel(new_row, x, y, ptr[0], ptr[1], ptr[2], ptr[3]);
  244. }
  245. }
  246. int maxX, maxY;
  247. lenX + Nx - 1> image -> width ? maxX = image -> width : maxX = lenX + Nx - 1;
  248. lenY + Ny - 1> image -> height ? maxY = image -> height : maxY = lenY + Ny - 1;
  249. for(y = Ny; y < maxY; y++) {
  250. png_byte *new_row = new_row_pointers[y - Ny];
  251. png_byte *ch_row = image->row_pointers[y];
  252. for (x = Nx; x < maxX; x++)
  253. {
  254. png_byte *toptr = &(new_row[(x - Nx)*4]);
  255. putPixel(ch_row, x, y, toptr[0], toptr[1], toptr[2], toptr[3]);
  256. }
  257. }
  258. for (y = 0; y < lenY; y++)
  259. free(new_row_pointers[y]);
  260. free(new_row_pointers);
  261. }
  262.  
  263. void drawLine(struct Png *image, int x1, int y1, int x2, int y2, int r, int g, int b) {
  264. const int deltaX = abs(x2 - x1);
  265. const int deltaY = abs(y2 - y1);
  266. const int signX = x1 < x2 ? 1 : -1;
  267. const int signY = y1 < y2 ? 1 : -1;
  268. int error = deltaX - deltaY;
  269. if (x2 >= 0 && x2 < image -> width && y2 >= 0 && y2 < image -> height)
  270. putPixel(image -> row_pointers[y2], x2, y2, r, g, b);
  271. while(x1 != x2 || y1 != y2)
  272. {
  273. if (x1 >= 0 && x1 < image -> width && y1 >= 0 && y1 < image -> height)
  274. putPixel(image -> row_pointers[y1], x1, y1, r, g, b);
  275. const int error2 = error * 2;
  276. if(error2 > -deltaY)
  277. {
  278. error -= deltaY;
  279. x1 += signX;
  280. }
  281. if(error2 < deltaX)
  282. {
  283. error += deltaX;
  284. y1 += signY;
  285. }
  286. }
  287. }
  288.  
  289. void drawCircle(struct Png *image, int xc, int yc, int inner, int outer, int R, int G, int B)
  290. {
  291. int xo = outer;
  292. int xi = inner;
  293. int y = 0;
  294. int erro = 1 - xo;
  295. int erri = 1 - xi;
  296.  
  297. while(xo >= y) {
  298. drawLine(image, xc + xi, yc + y, xc + xo, yc + y, R, G, B);
  299. drawLine(image, xc + y, yc + xi, xc + y, yc + xo, R, G, B);
  300. drawLine(image, xc - xo, yc + y, xc - xi, yc + y, R, G, B);
  301. drawLine(image, xc - y, yc + xi, xc - y, yc + xo, R, G, B);
  302. drawLine(image, xc - xo, yc - y, xc - xi, yc - y, R, G, B);
  303. drawLine(image, xc - y, yc - xo, xc - y, yc - xi, R, G, B);
  304. drawLine(image, xc + xi, yc - y, xc + xo, yc - y, R, G, B);
  305. drawLine(image, xc + y, yc - xo, xc + y, yc - xi, R, G, B);
  306.  
  307. y++;
  308.  
  309. if (erro < 0) {
  310. erro += 2 * y + 1;
  311. } else {
  312. xo--;
  313. erro += 2 * (y - xo + 1);
  314. }
  315.  
  316. if (y > inner) {
  317. xi = y;
  318. } else {
  319. if (erri < 0) {
  320. erri += 2 * y + 1;
  321. } else {
  322. xi--;
  323. erri += 2 * (y - xi + 1);
  324. }
  325. }
  326. }
  327. }
  328.  
  329. void frameType1(struct Png *image, int width, int R, int G, int B) {
  330. for (int y = image -> height - width;;y -= width) {
  331. for (int j = y; j >= max(0, y - width); j--) {
  332. drawLine(image, 0, j, image -> width -1 - j, image -> height - 1, 255 - R, 255 - G, 255 - B);
  333. }
  334. y -= width;
  335. if (y < 0)
  336. break;
  337. }
  338. int ty = image -> height - 1;
  339. for (int j = ty; j >= max(0, ty - width + (image -> width % width)); j--) {
  340. drawLine(image, image -> width - 1, j, image -> width -1 - j, 0, 255 - R, 255 - G, 255 - B);
  341. }
  342. ty -= width + (image -> width % width);
  343. for (int y = ty;;y -= width) {
  344. for (int j = y; j >= max(0, y - width); j--) {
  345. drawLine(image, image -> width - 1, j, image -> width -1 - j, 0, 255 - R, 255 - G, 255 - B);
  346. }
  347. y -= width;
  348. if (y < 0)
  349. break;
  350. }
  351. }
  352.  
  353. void frameType2(struct Png *image, int width, int R, int G, int B) {
  354. for (int y = 0;; y += width / 2) {
  355. for (int x = 0;; x += width / 2) {
  356. drawCircle(image, x, y, width / 2 - width / 20, width / 2, 255 - R, 255 - G, 255 - B);
  357. if (x >= image -> width)
  358. break;
  359. }
  360. if (y >= image -> height)
  361. break;
  362. }
  363. }
  364.  
  365. void frameType3(struct Png *image, int width, int R, int G, int B) {
  366. for (int y = 0;; y += width / 2) {
  367. for (int x = 0;; x += width / 2) {
  368. drawCircle(image, x, y, width / 2 - width / 20, width / 2, 255 - R, 255 - G, 255 - B);
  369. if (x >= image -> width)
  370. break;
  371. }
  372. if (y >= image -> height)
  373. break;
  374. }
  375. }
  376.  
  377. void drawFrame(struct Png *image, int R, int G, int B, int width, int type) {
  378. int wid = image -> width;
  379. int hei = image -> height;
  380. png_bytep *new_row_pointers =(png_bytep *) malloc(sizeof(png_bytep) * hei);
  381. for(int y = 0; y < hei; y++)
  382. {
  383. new_row_pointers[y] = (png_byte *)malloc(sizeof(png_byte)*4 * wid);
  384. png_byte *new_row = new_row_pointers[y];
  385. png_byte *cur_row = image->row_pointers[y];
  386. for (int x = 0; x < wid; x++)
  387. {
  388. png_byte *ptr = &(cur_row[(x*4)]);
  389. putPixel(new_row, x, y, ptr[0], ptr[1], ptr[2], ptr[3]);
  390. }
  391. }
  392. int lenX = image -> width + 2 * width;
  393. int lenY = image -> height + 2 * width;
  394. image ->row_pointers = (png_bytep *) realloc(image -> row_pointers, lenY * sizeof(png_bytep));
  395. for (int i = 0; i < lenY; i++) {
  396. image->row_pointers[i] = (png_byte *) realloc(image->row_pointers[i], sizeof(png_byte)*4 * lenX);
  397. }
  398. image -> width = lenX;
  399. image -> height = lenY;
  400. for (int i = 0; i < image -> height; i++) {
  401. png_byte *cur_row = image->row_pointers[i];
  402. for (int j = 0; j < image -> width; j++) {
  403. putPixel(cur_row, j, i, R, G, B, 255);
  404. }
  405. }
  406.  
  407. switch (type) {
  408. case 1:
  409. frameType1(image, width, R, G, B);
  410. break;
  411. case 2:
  412. frameType2(image, width, R, G, B);
  413. break;
  414. case 3:
  415. frameType3(image, width, R, G, B);
  416. break;
  417. }
  418.  
  419.  
  420. for(int y = 0; y < hei; y++) {
  421. png_byte *new_row = new_row_pointers[y];
  422. png_byte *ch_row = image->row_pointers[width + y];
  423. for (int x = 0; x < wid; x++)
  424. {
  425. png_byte *toptr = &(new_row[x*4]);
  426. putPixel(ch_row, width + x, y, toptr[0], toptr[1], toptr[2], toptr[3]);
  427. }
  428. }
  429.  
  430. for (int y = 0; y < hei; y++)
  431. free(new_row_pointers[y]);
  432. free(new_row_pointers);
  433. }
  434.  
  435. int main(int argc, char **argv) {
  436.  
  437. struct Png image;
  438.  
  439. struct args arg;
  440. int longIndex = 0;
  441. int opt = 0;
  442. int flag = 0;
  443. read_png_file(argv[1], &image);
  444.  
  445. arg.end[0] = -1;
  446. arg.end[1] = -1;
  447. arg.start[0] = -1;
  448. arg.start[1] = -1;
  449. arg.width = -1;
  450. arg.origin[0] = -1;
  451. arg.origin[1] = -1;
  452. arg.origin[2] = -1;
  453. arg.origin[3] = -1;
  454. arg.chosen[0] = -1;
  455. arg.chosen[1] = -1;
  456. arg.chosen[2] = -1;
  457. arg.chosen[3] = -1;
  458. arg.type = 1;
  459.  
  460. char optstring[22] = "csfhS:E:N:O:C:W:T:in";
  461.  
  462. struct option longOpts[] = {
  463. {"swap_color", no_argument, &flag, 's'},
  464. {"copy", no_argument, &flag, 'c'},
  465. {"frame", no_argument, &flag, 'f'},
  466. {"highlight_rect", no_argument, &flag, 'h'},
  467. {"note", required_argument, &flag, 'n'},
  468. {"start", required_argument, NULL, 'S'},
  469. {"end", required_argument, NULL, 'E'},
  470. {"new", required_argument, NULL, 'N'},
  471. {"origin", required_argument, NULL, 'O'},
  472. {"width", required_argument, NULL, 'W'},
  473. {"chosen", required_argument, NULL, 'C'},
  474. {"type", required_argument, NULL, 'T'},
  475. {"info", required_argument, &flag, 'i'},
  476. {NULL, 0, NULL, 0}
  477. };
  478.  
  479. opt = getopt_long(argc, argv, optstring , longOpts, &longIndex);
  480.  
  481. while( opt != -1 ) {
  482. switch(opt) {
  483. case 'E':
  484. if(!isNumber(optarg))
  485. {
  486. printf("Ошибка! Не корректно введены аргументы для флага -E! %s - не целое число!\n", optarg);
  487. return 0;
  488. }
  489. if(!isNumber(argv[optind]))
  490. {
  491. printf("Ошибка! Не корректно введены аргументы для флага -E! %s - не целое число!\n", argv[optind]);
  492. return 0;
  493. }
  494. arg.end[0] = atoi(optarg);
  495. arg.end[1] = atoi(argv[optind]);
  496. break;
  497.  
  498. case 'T':
  499. if(!isNumber(optarg))
  500. {
  501. printf("Ошибка! Не корректно введены аргументы для флага -T! %s - не целое число!\n", optarg);
  502. return 0;
  503. }
  504. if (atoi(optarg) < 0 || atoi(optarg) > 3) {
  505. printf("Ошибка! %s - такого типа рамки не существует!\n", optarg);
  506. return 0;
  507. }
  508. arg.type = atoi(optarg);
  509. break;
  510.  
  511. case 'S':
  512. if(!isNumber(optarg))
  513. {
  514. printf("Ошибка! Не корректно введены аргументы для флага -S! %s - не целое число!\n", optarg);
  515. return 0;
  516. }
  517. if(!isNumber(argv[optind]))
  518. {
  519. printf("Ошибка! Не корректно введены аргументы для флага -S! %s - не целое число!\n", argv[optind]);
  520. return 0;
  521. }
  522. arg.start[0] = atoi(optarg);
  523. arg.start[1] = atoi(argv[optind]);
  524. break;
  525.  
  526. case 'N':
  527. if(!isNumber(optarg))
  528. {
  529. printf("Ошибка! Не корректно введены аргументы для флага -N! %s - не целое число!\n", optarg);
  530. return 0;
  531. }
  532. if(!isNumber(argv[optind]))
  533. {
  534. printf("Ошибка! Не корректно введены аргументы для флага -N! %s - не целое число!\n", argv[optind]);
  535. return 0;
  536. }
  537. arg.nevv[0] = atoi(optarg);
  538. arg.nevv[1] = atoi(argv[optind]);
  539. break;
  540.  
  541. case 'W':
  542. if(!isNumber(optarg))
  543. {
  544. printf("Ошибка! Не корректно введены аргументы для флага -W! %s - не целое число!\n", optarg);
  545. return 0;
  546. }
  547. if (atoi(optarg) >= min(image.width, image.height) / 4 || atoi(optarg) < 0) {
  548. printf("Ошибка! Слишком большая ширина узора рамки или она меньше нуля.\n");
  549. return 0;
  550. }
  551. arg.width = atoi(optarg);
  552. break;
  553.  
  554. case 'O':
  555. if(!isNumber(optarg))
  556. {
  557. printf("Ошибка! Не корректно введены аргументы для флага -O! %s - не целое число!\n", optarg);
  558. return 0;
  559. }
  560. if(!isNumber(argv[optind]))
  561. {
  562. printf("Ошибка! Не корректно введены аргументы для флага -O! %s - не целое число!\n", argv[optind]);
  563. return 0;
  564. }
  565. if(!isNumber(argv[optind]))
  566. {
  567. printf("Ошибка! Не корректно введены аргументы для флага -O! %s - не целое число!\n", argv[optind + 1]);
  568. return 0;
  569. }
  570. if (atoi(optarg) > 255 || atoi(argv[optind]) > 255 || atoi(argv[optind + 1]) > 255
  571. || atoi(optarg) < 0 || atoi(argv[optind]) < 0 || atoi(argv[optind + 1]) < 0)
  572. {
  573. printf("Ошибка! Такого цвета не существует!\n");
  574. return 0;
  575. }
  576. arg.origin[0] = atoi(optarg);
  577. arg.origin[1] = atoi(argv[optind]);
  578. arg.origin[2] = atoi(argv[optind + 1]);
  579. break;
  580.  
  581. case 'C':
  582. if(!isNumber(optarg))
  583. {
  584. printf("Ошибка! Не корректно введены аргументы для флага -C! %s - не целое число!\n", optarg);
  585. return 0;
  586. }
  587. if(!isNumber(argv[optind]))
  588. {
  589. printf("Ошибка! Не корректно введены аргументы для флага -C! %s - не целое число!\n", argv[optind]);
  590. return 0;
  591. }
  592. if(!isNumber(argv[optind]))
  593. {
  594. printf("Ошибка! Не корректно введены аргументы для флага -C! %s - не целое число!\n", argv[optind + 1]);
  595. return 0;
  596. }
  597. if (atoi(optarg) > 255 || atoi(argv[optind]) > 255 || atoi(argv[optind + 1]) > 255
  598. || atoi(optarg) < 0 || atoi(argv[optind]) < 0 || atoi(argv[optind + 1]) < 0)
  599. {
  600. printf("Ошибка! Такого цвета не существует!\n");
  601. return 0;
  602. }
  603. arg.chosen[0] = atoi(optarg);
  604. arg.chosen[1] = atoi(argv[optind]);
  605. arg.chosen[2] = atoi(argv[optind + 1]);
  606. break;
  607.  
  608. case 'n':
  609. printHelp();
  610. break;
  611.  
  612. case 'i':
  613. flag = 'i';
  614. break;
  615.  
  616. case 's':
  617. flag = 's';
  618. break;
  619.  
  620. case 'c':
  621. flag = 'c';
  622. break;
  623.  
  624. case 'f':
  625. flag = 'f';
  626. break;
  627.  
  628. case 'h':
  629. flag = 'h';
  630. break;
  631. default:
  632. printf("Такого флага не существует!\n");
  633. return 0;
  634. }
  635.  
  636. opt = getopt_long(argc, argv, optstring , longOpts, &longIndex);
  637.  
  638. }
  639.  
  640. switch(flag){
  641.  
  642. case'c':
  643. if (arg.start[0] == -1 || arg.start[1] == -1 || arg.end[0] == -1 || arg.end[1] == -1 ||
  644. arg.nevv[0] == -1 || arg.nevv[1] == -1) {
  645. printf("Неправильно заданы аргументы!\n");
  646. break;
  647. }
  648. if(arg.start[0] < 0 || arg.start[0] > image.width ||
  649. arg.start[1] < 0 || arg.start[1] > image.height ||
  650. arg.end[0] > image.width || arg.end[0] < 0 ||
  651. arg.end[1] > image.height || arg.end[1] < 0 ||
  652. arg.start[0] > arg.end[0] ||
  653. arg.start[1] > arg.end[1] ||
  654. arg.nevv[0] < 0 || arg.nevv[1] < 0 ||
  655. arg.nevv[0] > image.width || arg.nevv[1] > image.height)
  656. {
  657. printf("Область копирования не является корректной\n");
  658. break;
  659. }
  660. else
  661. {
  662. copyAndPaste(&image, arg.start[0], arg.start[1], arg.end[0], arg.end[1], arg.nevv[0], arg.nevv[1]);
  663. }
  664. break;
  665.  
  666. case 'f':
  667. if (arg.chosen[0] == -1 || arg.chosen[1] == -1 || arg.chosen[2] == -1 || arg.width == -1) {
  668. printf("Неправильно заданы аргументы!\n");
  669. break;
  670. }
  671. drawFrame(&image, arg.chosen[0], arg.chosen[1], arg.chosen[2], arg.width, arg.type);
  672. break;
  673.  
  674. case 's':
  675. if (arg.chosen[0] == -1 || arg.chosen[1] == -1 || arg.chosen[2] == -1
  676. || arg.origin[0] == -1 || arg.origin[1] == -1 || arg.origin[2] == -1) {
  677. printf("Неправильно заданы аргументы!\n");
  678. break;
  679. }
  680. swap_colors(&image, arg.origin[0], arg.origin[1], arg.origin[2], arg.chosen[0], arg.chosen[1], arg.chosen[2]);
  681. break;
  682.  
  683. case 'n':
  684. printHelp();
  685. break;
  686.  
  687. case 'i':
  688. printf("Width = %d\n", image.width);
  689. printf("Heigth = %d\n", image.height);
  690. break;
  691. }
  692.  
  693. write_png_file(argv[argc - 1], &image);
  694.  
  695. return 0;
  696. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement