Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <regex>
- #include <fstream>
- #define WIDTH 640
- #define HEIGHT 480
- #define MAX_PKT_SIZE (640*480*4)
- // Image pixels storage
- unsigned int image[WIDTH * HEIGHT] = {};
- // Exptected colors
- const std::string BLACK = "BLACK";
- const std::string RED = "RED";
- const std::string YELLOW = "YELLOW";
- const std::string GREEN = "GREEN";
- const std::string BLUE = "BLUE";
- // Expected commands
- const std::string BCKG_s = "BCKG";
- const std::string LINE_H_s = "LINE_H";
- const std::string LINE_V_s = "LINE_V";
- const std::string RECT_s = "RECT";
- // Enumerate expected commands
- enum Command_enum {INVALID_CMD = 0, BCKG = 1, LINE_H = 2, LINE_V = 3, RECT = 4};
- // Loads text lines into vector of strings
- void cmdvector_from_file (std::ifstream &ifs, std::vector<std::string> &cmd_lines)
- {
- if (ifs) {
- std::copy(std::istream_iterator<std::string>(ifs),
- std::istream_iterator<std::string>(),
- std::back_inserter(cmd_lines));
- } else {
- std::cerr << "Couldn't open file for reading\n";
- }
- }
- // Returns detected current command ID
- Command_enum get_cmd_id (std::string &cmd_str) {
- Command_enum cmd_ret;
- if (!cmd_str.compare(BCKG_s)) {
- cmd_ret = BCKG;
- } else if (!cmd_str.compare(LINE_H_s)) {
- cmd_ret = LINE_H;
- } else if (!cmd_str.compare(LINE_V_s)) {
- cmd_ret = LINE_V;
- } else if (!cmd_str.compare(RECT_s)) {
- cmd_ret = RECT;
- } else {
- cmd_ret = INVALID_CMD;
- std::cout << "\n" << cmd_str << " is invalid command! Commands are: BCKG, LINE_H, LINE_V, RECT\n" << std::endl;
- }
- return cmd_ret;
- }
- // Convert color to 16-bit hex values
- // Bitwise RGBA Values:
- // https://www.fayewilliams.com/2011/09/21/bitwise-rgba-values/
- unsigned int colorstr2hex (const std::string color_str)
- {
- unsigned int black_c = 0;
- unsigned int red_c = (1<<15) | (1<<14) | (1<<13) | (1<<12) | (1<<11);
- unsigned int green_c = (1<<10) | (1<<9) | (1<<8) | (1<<7) | (1<<6) | (1<<5);
- unsigned int blue_c = (1<<4) | (1<3) | (1<<2) | (1<<1) | (1<<0);
- // Red and green form yellow in RGB
- unsigned int yellow_c = red_c | green_c;
- if (!color_str.compare(BLACK)) {
- return black_c;
- } else if (!color_str.compare(RED)) {
- return red_c;
- } else if (!color_str.compare(GREEN)) {
- return green_c;
- } else if (!color_str.compare(BLUE)) {
- return blue_c;
- } else if (!color_str.compare(YELLOW)) {
- return yellow_c;
- } else {
- std::cout << "Colors are: BLACK, RED, GREEN, BLUE, YELLOW" << std::endl;
- }
- }
- int main()
- {
- // Vector to store command strings from command file
- std::vector<std::string> file_vec;
- file_vec.push_back("BCKG: BLACK");
- file_vec.push_back("RE_CT: 120, 240, 100, 200; BLUE");
- file_vec.push_back("LINE_H: 100, 200, 10; YELLOW");
- file_vec.push_back("LINE_V: 150, 200, 10; GREEN");
- file_vec.push_back("RECT: 120, 240, 100, 200; BLUE");
- // Filename to read the commands from
- std::ifstream ifs("filename");
- // Push all lines into the file_vec vector
- cmdvector_from_file (ifs, file_vec);
- // Stores parsed commands
- std::vector<std::vector<std::string>> parsed_cmds;
- // Matches any continuous character and digit string
- // Skips interpunction --> string explode around , ;:
- std::regex rgx("\\w+");
- std::cout << "Parsed tokens: " << std::endl;
- // For each line in a file...
- for (int lid = 0; lid < file_vec.size(); lid++) {
- // Create iterator...
- std::regex_iterator<std::string::iterator> it(file_vec[lid].begin(), file_vec[lid].end(), rgx);
- std::regex_iterator<std::string::iterator> end;
- // Storage for extracted segments of each command
- std::vector<std::string> cmd_segs;
- // Iterate through all matched segments...
- for (; it != end; ++it)
- {
- std::cout << it->str() << std::endl;
- // And store them into cammand segments vector
- cmd_segs.push_back(it->str());
- }
- // At this moment all segments are stored
- // Push their vector to the parsed commands vector
- parsed_cmds.push_back(cmd_segs);
- }
- std::cout << "\nParsed commands: " << parsed_cmds.size() << std::endl;
- // Printout parsed segments of all commands
- /*for (int cmd = 0; cmd < parsed_cmds.size(); cmd++) {
- std::cout << std::endl;
- //for (int seg = 0; seg < parsed_cmds[cmd].size(); seg++) {
- std::cout << parsed_cmds[cmd][0] << " ";
- //}
- }*/
- // Execute all parsed commands
- for (int cmd = 0; cmd < parsed_cmds.size(); cmd++) {
- // Current command ID
- Command_enum cmd_id = get_cmd_id(parsed_cmds[cmd][0]);
- int x1 = 0, x2 = 0, y1 = 0, y2 = 0;
- int color_val = 0;
- // std::cout << "Current cmd_id: " << cmd_id << std::endl;
- switch (cmd_id) {
- case BCKG:
- color_val = colorstr2hex(parsed_cmds[cmd][1]);
- // Fill in image vector with color values
- for(int y = 0; y < HEIGHT; y++) {
- for(int x = 0; x < WIDTH; x++) {
- image[y * WIDTH + x] = color_val;
- }
- }
- std::cout << "Setting background color..." << std::endl;
- break;
- case LINE_H:
- color_val = colorstr2hex(parsed_cmds[cmd][4]);
- x1 = stoi(parsed_cmds[cmd][1]);
- x2 = stoi(parsed_cmds[cmd][2]);
- y1 = stoi(parsed_cmds[cmd][3]);
- std::cout << "Drawing horizontal line "
- << " x1 = " << x1
- << " x2 = " << x2
- << " y1 = " << y1 << std::endl;
- for (int x = x1; x < x2; ++x)
- image[y1 * WIDTH + x] = color_val;
- break;
- case LINE_V:
- color_val = colorstr2hex(parsed_cmds[cmd][4]);
- x1 = stoi(parsed_cmds[cmd][1]);
- y1 = stoi(parsed_cmds[cmd][2]);
- y2 = stoi(parsed_cmds[cmd][3]);
- std::cout << "Drawing vertical line "
- << " x1 = " << x1
- << " y1 = " << y1
- << " y2 = " << y2 << std::endl;
- for (int y = y1; y < y2; ++y)
- image[y * WIDTH + x1] = color_val;
- break;
- case RECT:
- color_val = colorstr2hex(parsed_cmds[cmd][5]);
- x1 = stoi(parsed_cmds[cmd][1]);
- x2 = stoi(parsed_cmds[cmd][2]);
- y1 = stoi(parsed_cmds[cmd][3]);
- y2 = stoi(parsed_cmds[cmd][4]);
- std::cout << "Drawing rect "
- << " x1 = " << x1
- << " x2 = " << x2
- << " y1 = " << y1
- << " y2 = " << y2 << std::endl;
- // Fill in rectangle between given points
- for (int y = y1; y < y2; ++y)
- for (int x = x1; x < x2; ++x)
- image[y * WIDTH + x] = color_val;
- break;
- default:
- // Chill out
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement