Read PPM files RGB values C++

谁都会走 提交于 2019-12-12 00:34:33

问题


I'm trying read from a PPM file. I want to read the first, second and third number from each row, in this file, but I don´t know how to read the lines.

This is what I have so far:

for (int y = 4; y <= HEIGHT; y++) { // i think it has to start on row 4 
    for (int x = 1; x <= WIDTH; x++) { // and x from 1
         int i = 4;

         int r = CurrentR(i);
         int g = CurrentG(i);
         int b = CurrentB(i);
         i++;
    }   
}

int CurrentR(int I) {
    return // the first number in row xy
}
int CurrentG(int I) {
    return // the second number in row xy
}
int CurrentB(int I) {
    return // the third number in row xy
}

回答1:


This is how I would suggest you to do it:

struct RGB {
    int R,B,G;
}
std::ifstream& operator>>(std::ifstream &in,RGB& rgb){
    in >> rgb.R;
    in >> rgb.G;
    in >> rgb.B;
    return in;
}
std::ostream& operator<<(std::ostream &out,RGB& rgb){
    out << rgb.R << " ";
    out << rgb.G << " ";
    out << rgb.B << std::endl;
    return out;
}


int main(){
    std::string filename = "test.txt";
    std::ifstream file(filename.c_str());
    if(file.is_open()) {
        std::string line;
        for (int i=0;i<4;i++) { std::getline(file,line); }
        RGB rgb;
        for (int i=0;i<LINES_TO_READ;i++) {
            file >> rgb;
            std::cout << rgb;
        }
    }
}


来源:https://stackoverflow.com/questions/33422734/read-ppm-files-rgb-values-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!