私は、PPMファイルに対してさまざまな効果と操作を実行できるプログラムを開発中です。しかし、テストのために、入力ファイルではなくcinを使用します。一度に複数のエフェクトを実行できるようになっていますが、1つでも問題が発生しています。動作する行でremoveBlue()を実行し、別の値で再試行して、代わりに赤または緑を削除します。そんなこと。コードがたくさんあるので、必要なものだけを含めるようにします。C++:PPMファイルの操作に関する問題
#include <vector>
#include <stdlib.h>
#include <cstdlib>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std;
class SimpleImageEffect
{
public:
virtual void processImage(vector<Point> &points) = 0;
};
class RemoveRed : public SimpleImageEffect
{
public:
virtual void processImage(vector<Point> &points)
{
for (Point& p : points)
{
p.setRed(0);
}
}
};
//Just an example of one of the effect classes.
//The code in them is correct, so I won't include the others unless needed.
vector<Point> parse_line(string line)
{
istringstream scanner{line};
vector<Point> result{};
int red = -1;
int green = -1;
int blue = -1;
int counter = 0;
while(scanner.good())
{
if (counter == 0)
{
counter++;
scanner >> red;
}
else if (counter == 1)
{
counter++;
scanner >> green;
}
else if (counter == 2)
{
scanner >> blue;
Point p{ red, green, blue };
result.push_back(p);
counter = 0;
}
}
return result;
}
void readFromCin()
{
string line = "";
vector<string> lines_in_file{};
int i, effect_choice;
SimpleImageEffect *effect = nullptr;
getline(cin, line);
while (line.length() > 0)
{
lines_in_file.push_back(line);
getline(cin, line);
}
for (int i = 0; i < lines_in_file.size(); i++)
{
if (lines_in_file[i] != "P3")
{
effect_choice = strToInt(lines_in_file[i]);
}
else if (lines_in_file[i] == "P3")
{
cout << lines_in_file[i] << endl;
cout << lines_in_file[i+1] << endl;
cout << lines_in_file[i+2] << endl;
}
vector<Point> points = parse_line(lines_in_file[i]);
if (effect_choice == 1) effect = new RemoveRed;
if (effect_choice == 2) effect = new RemoveGreen;
if (effect_choice == 3) effect = new RemoveBlue;
if (effect_choice == 4) effect = new NegateRed;
if (effect_choice == 5) effect = new NegateGreen;
if (effect_choice == 6) effect = new NegateBlue;
if (effect_choice == 7) effect = new AddNoise;
if (effect_choice == 8) effect = new HighContrast;
if (effect_choice == 9) effect = new ConvertToGrayscale;
effect->processImage(points);
for (auto p : points)
{
cout << p;
cout << endl;
}
}
}
int main(int argc, char** argv)
{
string menu_choice;
getline(cin, menu_choice);
if (menu_choice == "1")
{
readFromFile();
}
else
{
readFromCin();
}
return 0;
}
だから例えば、
2
1
P3
1 1
255
50 50 50
の入力とそれを実行すると、
P3
1 1
255
0 50 50
を返しますが、私は
2
3
P3
1 2
255
50 50 50
1 2 3
でそれを実行した場合、それは
を返します。P3
1 2
255
0 50 50
0 2 3
私はこの問題を引き起こしていることは全く考えていないので、助けを借りていただければ幸いです。ありがとう。