C ++读取具有像素颜色的txt文件并将其复制到数组中

时间:2017-03-14 13:29:00

标签: c++ arrays colors

我的问题是我有一个文本文件,里面有两条曲线的示波器图形的每个像素的颜色。

例如0表示白色像素,即背景颜色,然后是四个参考像素之一的247,然后是2个曲线的像素颜色。

我的程序应该做什么打开文件读取并保存:宽度,高度,颜色数量以及与每种颜色相关的数字。 然后保存数组中的所有像素,以便我可以读取数组的每个元素,搜索引用像素和2条曲线,并将它们的协调保存在另一个文件中。

我写下了我的代码,但我不确定它是否实际上保存了数组中的像素,因为如果我要求程序给我一个数组的元素它就什么也没给我。

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

int main(){
    ifstream f_in;
    f_in.open("Pixmap1.txt");
    if (f_in.fail()){
        cerr<<"Error during the opening of the file";
    }
    int width(0);
    int height(0);
    int numbercolors;
    int referencepixel1;
    int referencepixel2;
    int referencepixel3;
    int referencepixel4;
    int C1;
    int C2;

    f_in>>width;
        if(f_in.fail()){
        cerr<<"Error during the reading of width";
    }
    f_in>>height;
        if(f_in.fail()){
        cerr<<"Error during the reading of height";
    }
    f_in>>numbercolors;
        if(f_in.fail()){
        cerr<<"Error during the reading of numberofcolors";
    }
    f_in>>referencepixel1;
        if(f_in.fail()){
        cerr<<"Error during the reading of referencepixel1 ";
    }
    f_in>>referencepixel2;
        if(f_in.fail()){
        cerr<<"Error during the reading of nreferencepixel2";
    }
    f_in>>referencepixel3;
        if(f_in.fail()){
        cerr<<"Error during the reading of referencepixel3";
    }
    f_in>>referencepixel4;
        if(f_in.fail()){
        cerr<<"Error during the reading of referencepixel4";
    }
    f_in>>C1;
        if(f_in.fail()){
        cerr<<"Error during the reading of C1";
    }
    f_in>>C2;
        if(f_in.fail()){
        cerr<<"Error during the reading of C2";
    }
    vector<vector<int> > MyArrayPix;
    int i(0);
    while(i<height,i++){
        vector<int> Vtmp;
        int j(0);
        while(j<width,j++){
            int tmp(0);
            f_in>>tmp;
            Vtmp.push_back(tmp);
        }
        MyArrayPix.push_back(Vtmp);
    }
    f_in.close();
    ofstream f_out;
    f_out.open("traces.m");
    string stmp("P = [");
    f_out<<stmp;
    int Y(0);
    int X(0);
    while(Y<height,Y++){
        while(X<width,X++){
            if(MyArrayPix[Y][X]=referencepixel1){
                f_out<<Y<<X<<endl;              
            }
            if(MyArrayPix[Y][X]=referencepixel2){
                f_out<<Y<<X<<endl;              
            }
            if(MyArrayPix[Y][X]=referencepixel3){
                f_out<<Y<<X<<endl;              
            }
            if(MyArrayPix[Y][X]=referencepixel4){
                f_out<<Y<<X<<endl;              
            }
        }
    }
    string stmp1("] ;");
    f_out<<stmp1<<endl;
    string stmp2("C1 = [");
    f_out<<stmp2;
        while(Y<height,Y++){
            while(X<width,X++){
                if(MyArrayPix[Y][X]=C1){
                    f_out<<Y<<X<<endl;
                }
            }
        }
    string stmp3("] ;");
    f_out<<stmp3<<endl;
    string stmp4("C2 = [");
    f_out<<stmp4;
    while(Y<height,Y++){
            while(X<width,X++){
                if(MyArrayPix[Y][X]=C2){
                    f_out<<Y<<X<<endl;
                }
            }
    }
    string stmp5("] ;");
    f_out<<stmp5<<endl;
    f_out.close();
}

0 个答案:

没有答案