颜色为sm ++,c ++为ppm

时间:2016-10-19 02:23:57

标签: c++

我正在尝试执行以下操作: 我必须用c ++创建一个ppm文件。此文件必须使颜色变亮(如颜色渐变)。 到目前为止,我已经能够创建该文件,它返回了我几乎与我想要的颜色smoosh的ppm。我认为问题出在for循环中。提前谢谢。

#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct Color
{
    int red;
    int green;
    int blue;
};

void smoosh(int rows, int cols, Color leftColor, Color rightColor, string filename);

int main()
{
    Color leftColor;
    Color rightColor;
    int rows;
    int cols;

    cout << "Left Color: " << endl;
    cin >> leftColor.red >> leftColor.green >> leftColor.blue;
    cout << "Right Color: " << endl;
    cin >> rightColor.red >> rightColor.green >> rightColor.blue;
    cout << "Height: " << endl;
    cin >> rows;
    cout << "Width: " << endl;
    cin >> cols;

    string filename;
    cout << "File Name: " << endl;
    cin >> filename;
    smoosh(rows, cols, leftColor, rightColor, filename);


    return 0;
}

void smoosh(int rows, int cols, Color leftColor, Color rightColor, string filename)
{
    ofstream fout;
    fout.open(filename);

    fout << "P3" << endl;
    fout << cols << " " << rows << endl;
    fout << "255" << endl;

    int i;
    int j;

    for (i = 0; i <= rows; i++)
    {
        for (j = 0; j <= cols; j++)
        {
            int r;
            int g;
            int b;
            r=static_cast<float>(leftColor.red)+((static_cast<float>(j)/cols)*(static_cast<float>(rightColor.red)-static_cast<float>(leftColor.red)));
            g=static_cast<float>(leftColor.green)+((static_cast<float>(j)/cols)*(static_cast<float>(rightColor.green)-static_cast<float>(leftColor.green)));
            b=static_cast<float>(leftColor.blue)+((static_cast<float>(j)/cols)*(static_cast<float>(rightColor.blue)-static_cast<float>(leftColor.blue)));
            fout<<r<<" "<<g<<" "<<b<<" ";
        }
    }
}

我想让它看起来像:

enter image description here

1 个答案:

答案 0 :(得分:0)

好的,问题解决了,只需删除&#34; =&#34;来自for循环。感谢