Advertisement
LilChicha174

Untitled

Apr 22nd, 2022
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.14 KB | None | 0 0
  1. void printHelp(){
  2.     printf("Это программа с CLI для редактирования png файлов, версия программы 0.0001 :)\n");
  3.     printf("Поддерживаются только файлы с глубиной цвета RGB!\n");
  4.     printf("Формат ввода: ./bmpedit [имя исходного файла] [функция] -[ключ1]/--[полный ключ1] [аргумент1] ...\n\n");
  5.     printf("Функции/ключи:\n");
  6.     printf("triangle [имя файла] - рисование треугольника с возможностью его залить и выбрать цвет.\n");
  7.     printf("    -f/--first  [<x-координата>.<y-координата>] - первая вершина треугольника\n");
  8.     printf("    -s/--second  [<x-координата>.<y-координата>] - вторая вершина треугольника\n");
  9.     printf("    -t/--third [<x-координата>.<y-координата>] - третья вершина треугольника\n");
  10.     printf("    -l/--line [<число>] - толщина сторон треугольника(в пикселях)\n");
  11.     printf("    -C/--color [<число>.<число>.<число>] - цвет заливки и треугольника (RGB)\n");
  12.     printf("    -с/--cast [<число>] - заливка треугольника (по умолчанию без заливки) (1 - заливка, 0 - нет)\n");
  13.     printf("line [имя файла] - рисование прямой линии.\n");
  14.     printf("    -f/--first  [<x-координата>.<y-координата>] - начало линии\n");
  15.     printf("    -s/--second  [<x-координата>.<y-координата>] - конец линии\n");
  16.     printf("    -l/--line [<число>] - толщина линии(в пикселях)\n");
  17.     printf("    -C/--color [<число>.<число>.<число>] - цвет линии (RGB)\n");
  18.     printf("collage [имя файла] - создается коллаж из изображения.\n");
  19.     printf("    -x/--xPhoto  [<число>] - количество изображений по оси X\n");
  20.     printf("    -y/--yPhoto  [<число>] - количество изображений по оси Y\n");
  21.     printf("help - вывод справки о работе программы.\n");
  22.     printf("[имя файла] info - вывод информации об изображении.\n");
  23.     printf("-o/--output [путь] - файл для вывода (по умолчанию исходный файл)\n");
  24. }
  25.  
  26. void choice(char* func, int key, int* x0, int* y0, int* x1, int* y1, int* x2, int* y2, int* line_fat,
  27.             int* cast, char** output, int* Red, int* Green, int* Blue, int* x_photos, int* y_photos){
  28.     int i = 0;
  29.     switch (key) {
  30.         case 'f':
  31.             *x0 = atoi(optarg);
  32.             while(optarg[i] != '.'){
  33.                 i++;
  34.             }
  35.             *y0 = atoi(&optarg[i+1]);
  36.             break;
  37.         case 's':
  38.             *x1 = atoi(optarg);
  39.             while(optarg[i] != '.'){
  40.                 i++;
  41.             }
  42.             *y1 = atoi(&optarg[i+1]);
  43.             break;
  44.         case 't':
  45.             *x2 = atoi(optarg);
  46.             while(optarg[i] != '.'){
  47.                 i++;
  48.             }
  49.             *y2 = atoi(&optarg[i+1]);
  50.             break;
  51.         case 'l':
  52.             *line_fat = atoi(optarg);
  53.             break;
  54.         case 'c':
  55.             *cast = atoi(optarg);
  56.             break;
  57.         case 'C':
  58.             *Red = atoi(optarg);
  59.             while(optarg[i] != '.'){
  60.                 i++;
  61.             }
  62.             *Green = atoi(&optarg[i+1]);
  63.             while(optarg[i] != '.'){
  64.                 i++;
  65.             }
  66.             *Blue = atoi(&optarg[i+1]);
  67.             break;
  68.         case 'x':
  69.             *x_photos = atoi(optarg);
  70.             break;
  71.         case 'y':
  72.             *y_photos = atoi(optarg);
  73.             break;
  74.         case 'o':
  75.             *output = optarg;
  76.             break;
  77.         default:
  78.             printf("Нет такого ключа для %s!\n", func);
  79.             exit(-1);
  80.     }
  81. }
  82.  
  83. int main(int argc, char **argv) {
  84.     struct Png image;
  85.     int index = 0, key;
  86.     int x0 = 0, y0 = 0, x1 = 0, y1 = 0, x2 = 0, y2 = 0, line_fat = 0, flag = 0;
  87.     int Red = 0, Green = 0, Blue = 0;
  88.     int x_photos = 1, y_photos = 1;
  89.     char* output = argv[1];
  90.  
  91.     read_png_file(argv[1], &image);
  92.     int width_pixel = process_file(&image);
  93.  
  94.     const struct option firstCordStruct = {"first", required_argument, NULL, 'f'};
  95.     const struct option secondCordStruct = {"second", required_argument, NULL, 's'};
  96.     const struct option thirdCordStruct = {"third", required_argument, NULL, 't'};
  97.     const struct option fatLineStruct = {"line", required_argument, NULL, 'l'};
  98.     const struct option castStruct = {"cast", required_argument, NULL, 'c'};
  99.     const struct option xPhotosStruct = {"xPhoto", required_argument, NULL, 'x'};
  100.     const struct option yPhotosStruct = {"yPhoto", required_argument, NULL, 'y'};
  101.     const struct option colorStruct = {"color", required_argument, NULL, 'C'};
  102.     const struct option outputStruct = {"output", required_argument, NULL, 'o'};
  103.  
  104.     opterr = 0;
  105.     if(argc == 1 || !strcmp(argv[2], "help")){
  106.         printHelp();
  107.         return 0;
  108.     }else if(!strcmp(argv[2], "info")){
  109.         print_info(&image);
  110.         return 0;
  111.     }else if(!strcmp(argv[2], "triangle")) {
  112.         struct option options[] = {firstCordStruct, secondCordStruct, thirdCordStruct, fatLineStruct,
  113.                 castStruct, colorStruct, outputStruct};
  114.         while((key = getopt_long(argc, argv, "f:s:t:l:c:C:o:", options, &index)) != -1) {
  115.             choice("triangle", key, &x0, &y0, &x1, &y1, &x2, &y2, &line_fat,
  116.                    &flag, &output, &Red, &Green, &Blue, 0, 0);
  117.         }
  118.         print_triangle(&image, width_pixel, x0, y0, x1, y1, x2, y2, line_fat, flag, Red, Green, Blue);
  119.         write_png_file(output, &image);
  120.     }else if(!strcmp(argv[2], "line")) {
  121.         struct option options[] = {firstCordStruct, secondCordStruct, fatLineStruct, colorStruct,
  122.                                     outputStruct};
  123.         while((key = getopt_long(argc, argv, "f:s:l:C:o:", options, &index)) != -1) {
  124.             choice("line", key, &x0, &y0, &x1, &y1, 0, 0,  &line_fat,
  125.                    0, &output, &Red, &Green, &Blue, 0, 0);
  126.         }
  127.         paint_line(&image, width_pixel, x0, y0, x1, y1, line_fat, Red, Green, Blue);
  128.         write_png_file(output, &image);
  129.     }else if(!strcmp(argv[2], "collage")) {
  130.         struct option options[] = {xPhotosStruct, yPhotosStruct, outputStruct};
  131.         while((key = getopt_long(argc, argv, "x:y:o:", options, &index)) != -1) {
  132.             choice("collage", key, 0, 0, 0, 0, 0, 0,  0,
  133.                    0, &output, 0, 0, 0, &x_photos, &y_photos);
  134.         }
  135.         make_collage(&image, width_pixel, x_photos, y_photos);
  136.         write_png_file(output, &image);
  137.     }
  138.  
  139.     for (int y = 0; y < image.height; y++)
  140.         free(image.row_pointers[y]);
  141.     free(image.row_pointers);
  142.  
  143.     return 0;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement