读取PPM文件RGB值C ++ [英] Read PPM files RGB values C++

查看:103
本文介绍了读取PPM文件RGB值C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从PPM文件中读取. 我想读取此文件中每一行的第一个,第二个和第三个数字,但我不知道该怎么读.

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
}

推荐答案

这是我建议您这样做的方式:

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;
        }
    }
}

这篇关于读取PPM文件RGB值C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆