Код, который я получил прямо сейчас:
Код: Выделить всё
while(getline(file, line_pixels)) {
if(line_pixels.size() > 0 && line_pixels.at(0) != '#') {
std::stringstream ss(line_pixels);
std::string value;
while(getline(ss, value, ' ')) {
pixel p;
p.r = p.g = p.b = stoi(value) / this->max_value;
v.emplace_back(p);
}
}
}
Код: Выделить всё
p.r = p.g = p.b = stoi(value) / this->max_value;
Код: Выделить всё
#include
#include
#include
#include
void read(std::string file_name);
struct Pixel {
float r, g, b;
};
typedef struct Pixel pixel;
class Image {
private:
int max_value;
std::vector pixels;
public:
void read(std::string file_name) {
std::ifstream file(file_name);
std::string line_one, line_two, line_three, line_pixels;
char magicNumber;
std::string width, height;
while(getline(file, line_one)) {
if(line_one.size() > 0 && line_one.at(0) != '#') {
magicNumber = line_one.at(1);
break;
}
}
while(getline(file, line_two)) {
if(line_two.size() > 0 && line_two.at(0) != '#') {
std::stringstream ss(line_two);
getline(ss, width, ' ');
getline(ss, height, ' ');
break;
}
}
while(getline(file, line_three)) {
if(line_three.size() > 0 && line_three.at(0) != '#') {
this->max_value = stoi(line_three);
break;
}
}
if(magicNumber == '2') {
std::vector v;
while(getline(file, line_pixels)) {
if(line_pixels.size() > 0 && line_pixels.at(0) != '#') {
std::stringstream ss(line_pixels);
std::string value;
while(getline(ss, value, ' ')) {
pixel p;
p.r = p.g = p.b = stoi(value) / this->max_value;
v.emplace_back(p);
}
}
}
int h = stoi(height), w = stoi(width);
int index = 0;
for(int i=0; i
Подробнее здесь: [url]https://stackoverflow.com/questions/74800013/error-when-trying-read-pixel-data-from-pgm-file[/url]