Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include "pch.h"
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- // to check: <имя_входного_файла> <имя_выходного_файла> 255 1 0 10 100 20 2.0
- void to_black(vector<unsigned char> &pixel) {
- pixel.assign(pixel.size(), 0);
- }
- void draw_point(vector<unsigned char> &pixel, int &x, int &y, int &col, int &bright, int &h, int &w) {
- int now = (x * h + y) * col;
- pixel[now] = bright;
- if (col == 3) {
- pixel[now + 1] = bright;
- pixel[now + 2] = bright;
- }
- }
- void draw_line(vector<unsigned char> &pixel, int &x1, int &y1, int &x2, int &y2, int col, int &bright, int &h, int &w) {
- int dx = abs(x1 - x2),
- dy = abs(y1 - y2),
- e = 0,
- de = abs(y1 - y2) + 1,
- y = y1,
- diry = y2 - y1;
- if (diry > 0) {
- diry = 1;
- }
- else {
- diry = -1;
- }
- for (int x = x1; x <= x2; x++) {
- draw_point(pixel, x, y, col, bright);
- e += de;
- if (e >= dx + 1) {
- y += diry;
- e -= dx + 1;
- }
- }
- }
- int main(int argc, char *argv[]) {
- if (argc != 10) {
- cerr << "Invalid request";
- return 0;
- }
- string name_in = argv[1];
- string name_out = argv[2];
- int bright = atoi(argv[3]),
- thick = atoi(argv[4]),
- xs = atoi(argv[5]),
- ys = atoi(argv[6]),
- xf = atoi(argv[7]),
- yf = atoi(argv[8]);
- double gamma = atof(argv[9]);
- //kgig.exe <имя_входного_файла> <имя_выходного_файла> <яркость_линии> <толщина_линии> <x_начальный> <y_начальный> <x_конечный> <y_конечный> <гамма>
- FILE * fin = fopen(name_in.c_str(), "rb");
- if (!fin) {
- cerr << "Invalid input file";
- return 0;
- }
- if (!(thick == 1 && gamma == 2.0)) {
- cerr << "Invalid bright or gamma";
- return 0;
- }
- char p, number;
- int h, w, color;
- int count = fscanf(fin, "%c%c\n%d %d\n%d\n", &p, &number, &w, &h, &color);
- if (count != 5) {
- cerr << "Incorrect file content";
- return 0;
- }
- if (!(p == 'P' && (number == '5' || number == '6')) || h <= 0 || w <= 0 || color > 255 || color <= 0) {
- cerr << "Incorrect data in the file";
- return 0;
- }
- bool flag = 0;
- vector<unsigned char> pixel;
- if (number == '5') {
- pixel.resize(h * w, 0);
- }
- else if (number == '6') {
- pixel.resize(3 * h * w);
- flag = 1;
- }
- int cnt = fread(&pixel[0], sizeof(unsigned char), pixel.size(), fin);
- if (number == '5' && cnt != h * w || number == '6' && cnt != 3 * h * w) {
- cerr << "Incorrect data in the file";
- return 0;
- }
- fclose(fin);
- to_black(pixel);
- draw_line(pixel, xs, ys, xf, yf, (number == '5' ? 1 : 3), bright, h, w);
- FILE* fout = fopen(name_out.c_str(), "wb");
- if (!fout) {
- cerr << "Invalid output file";
- return 0;
- }
- fprintf(fout, "%c%c\n%d %d\n%d\n", p, number, w, h, color);
- fwrite(&pixel[0], sizeof(unsigned char), pixel.size(), fout);
- fclose(fout);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement