Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include "pch.h"
  3. #include <iostream>
  4. #include <string>  
  5. #include <vector>
  6. using namespace std;
  7.  
  8. //  to check: <имя_входного_файла> <имя_выходного_файла> 255 1 0 10 100 20 2.0
  9.  
  10. void to_black(vector<unsigned char> &pixel) {
  11.     pixel.assign(pixel.size(), 0);
  12. }
  13.  
  14. void draw_point(vector<unsigned char> &pixel, int &x, int &y, int &col, int &bright, int &h, int &w) {
  15.     int now = (x * h + y) * col;
  16.     pixel[now] = bright;
  17.     if (col == 3) {
  18.         pixel[now + 1] = bright;
  19.         pixel[now + 2] = bright;
  20.     }
  21. }
  22.  
  23. void draw_line(vector<unsigned char> &pixel, int &x1, int &y1, int &x2, int &y2, int col, int &bright, int &h, int &w) {
  24.     int dx = abs(x1 - x2),
  25.         dy = abs(y1 - y2),
  26.         e = 0,
  27.         de = abs(y1 - y2) + 1,
  28.         y = y1,
  29.         diry = y2 - y1;
  30.     if (diry > 0) {
  31.         diry = 1;
  32.     }
  33.     else {
  34.         diry = -1;
  35.     }
  36.     for (int x = x1; x <= x2; x++) {
  37.         draw_point(pixel, x, y, col, bright);
  38.         e += de;
  39.         if (e >= dx + 1) {
  40.             y += diry;
  41.             e -= dx + 1;
  42.         }
  43.     }
  44. }
  45.  
  46. int main(int argc, char *argv[]) {
  47.  
  48.     if (argc != 10) {
  49.         cerr << "Invalid request";
  50.         return 0;
  51.     }
  52.     string name_in = argv[1];
  53.     string name_out = argv[2];
  54.     int bright = atoi(argv[3]),
  55.         thick = atoi(argv[4]),
  56.         xs = atoi(argv[5]),
  57.         ys = atoi(argv[6]),
  58.         xf = atoi(argv[7]),
  59.         yf = atoi(argv[8]);
  60.     double gamma = atof(argv[9]);
  61.     //kgig.exe <имя_входного_файла> <имя_выходного_файла> <яркость_линии> <толщина_линии> <x_начальный> <y_начальный> <x_конечный> <y_конечный> <гамма>
  62.    
  63.     FILE * fin = fopen(name_in.c_str(), "rb");
  64.     if (!fin) {
  65.         cerr << "Invalid input file";
  66.         return 0;
  67.     }
  68.  
  69.     if (!(thick == 1 && gamma == 2.0)) {
  70.         cerr << "Invalid bright or gamma";
  71.         return 0;
  72.     }
  73.  
  74.     char p, number;
  75.     int h, w, color;
  76.     int count = fscanf(fin, "%c%c\n%d %d\n%d\n", &p, &number, &w, &h, &color);
  77.     if (count != 5) {
  78.         cerr << "Incorrect file content";
  79.         return 0;
  80.     }
  81.  
  82.     if (!(p == 'P' && (number == '5' || number == '6')) || h <= 0 || w <= 0 || color > 255 || color <= 0) {
  83.         cerr << "Incorrect data in the file";
  84.         return 0;
  85.     }
  86.     bool flag = 0;
  87.     vector<unsigned char> pixel;
  88.     if (number == '5') {
  89.         pixel.resize(h * w, 0);
  90.     }
  91.     else if (number == '6') {
  92.         pixel.resize(3 * h * w);
  93.         flag = 1;
  94.     }
  95.  
  96.     int cnt = fread(&pixel[0], sizeof(unsigned char), pixel.size(), fin);
  97.     if (number == '5' && cnt != h * w || number == '6' && cnt != 3 * h * w) {
  98.         cerr << "Incorrect data in the file";
  99.         return 0;
  100.     }
  101.  
  102.     fclose(fin);
  103.     to_black(pixel);
  104.     draw_line(pixel, xs, ys, xf, yf, (number == '5' ? 1 : 3), bright, h, w);
  105.  
  106.    
  107.     FILE* fout = fopen(name_out.c_str(), "wb");
  108.  
  109.     if (!fout) {
  110.         cerr << "Invalid output file";
  111.         return 0;
  112.     }
  113.  
  114.     fprintf(fout, "%c%c\n%d %d\n%d\n", p, number, w, h, color);
  115.     fwrite(&pixel[0], sizeof(unsigned char), pixel.size(), fout);
  116.     fclose(fout);
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement