将地图数据文件读入矢量并转换为RGB值以输出到PPM文件中

时间:2016-10-03 00:47:27

标签: c++ ppm

所以我试图将.dat文件中的值读入2D矢量(加上每个表示RGB值的3-1D矢量),然后将值输出到我的代码将使用特定格式创建的.PPM文件中。我已经完成了大部分代码,但由于某些原因,它只是在获得所需的所有用户输入后停止,然后不会返回任何内容。如果你能提出更高效的建议,那就太好了。但是,我的主要问题是让代码工作。任何帮助将不胜感激!

干杯

#include <iostream>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <iomanip>
#include <vector>
#include <fstream>
#include <string>
#include <unistd.h>
#include <sys/stat.h>

using namespace std;

// Function Declaration
inline bool exists (const string& name);


 int main(int argc, const char * argv[]) {
    // Start main code
    // Variable Declaration
    double rows;
    double columns;
    string fileName;
    int min = 0;
    int max = 0;
    vector<int> red;
    vector<int> green;
    vector<int> blue;

    // Request/get rows and validate
    do {
        cin.clear();
        cout << "Enter number of rows: ";
        cin >> rows;

        if (cin.fail() || rows - static_cast<int>(rows) != 0) {
             cout << "Invalid number of rows. Please enter a positive integer." << endl;
        }
    }
    while (cin.fail() || rows - static_cast<int>(rows) != 0);
    cin.clear();

    // Request/get columns and validate
    do {
        cout << endl << "Enter number of columns: ";
        cin >> columns;

        if (cin.fail() || columns - static_cast<int>(columns) != 0) {
            cout << "Invalid number of columns. Please enter a positive integer." << endl;
        }
    }
    while (cin.fail() || columns - static_cast<int>(columns) != 0);

    // Request/get file name and validate both if input is good and if file exists
    do {
        cin.clear();
        cout << "Enter file name: ";
        cin >> fileName;

        if (cin.fail()) {
            cout << "Invalid file name. Enter a file name with extension \".dat.\"" << endl;
        }
        if (!exists(fileName)) {
            cout << "File does not exist." << endl;
        }
    }
    while (cin.fail() || !exists(fileName));

    // Open file to be read
    ifstream readFile;
    readFile.open(fileName);

    // Declare primary vector
    vector<vector<int>> dataVector(rows, vector<int>(columns));

    // Calculate minimum and maximum values in file
    while (!readFile.eof()) {
        int tempComp;
        readFile >> tempComp;
        if (tempComp > max) {
            max = tempComp;
        }
        else if (tempComp < min) {
            min = tempComp;
        }
    }

    readFile.clear();
    readFile.seekg(0, ios::beg);
    cout << max;

    // Input data from file to vector
    while (!readFile.eof() && readFile.is_open()) {
        int num;
        vector<int> tempVector;

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                readFile >> num;
                tempVector.push_back(num);
                red.push_back(static_cast<int>((static_cast<double>(num) / static_cast<double>(max)) * 255));
                green.push_back(static_cast<int>((static_cast<double>(num) / static_cast<double>(max)) * 255));
                blue.push_back(static_cast<int>((static_cast<double>(num) / static_cast<double>(max)) * 255));
            }
             dataVector.push_back(tempVector);

        }

    }

    ofstream outputFS(fileName += ".ppm");
    outputFS << "P3" << endl;
    outputFS << columns << " " << rows << endl;
    outputFS << "255" << endl;

    for (int j = 0; j < max; j++) {
        outputFS << red.at(j) << " " << green.at(j) << " " << blue.at(j);
        if (j % static_cast<int>(columns) == 0) {
            cout << endl;
        }
    }
    ofstream out(fileName += ".ppm");
    outputFS.close();

     return 0;
}

// Definition of Functions

// Checks if file exists
inline bool exists (const string& name) {
    struct stat buffer;
    return (stat (name.c_str(), &buffer) == 0);
}

0 个答案:

没有答案