Reading into a struct
Apr 22, 2016 at 7:36pm UTC
I'm supposed to write a reading function for a PPM file that saves the r g b content to a vector vector of the pixel. Can anyone help me fix this function? Line 74 is where I've tried something (with no luck). Thanks for your help.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;
struct Pixel {
int red, green, blue;
Pixel();
Pixel(int r, int g, int b);
};
class Picture {
vector<vector<Pixel> > pic;
int rows;
int columns;
int maxIntensity;
public :
Picture();
bool readPpmFile(string inputFile);
int writePpmFile(string outputFile);
void vFlip();
void hFlip();
};
int main(int argc, char *argv[]) {
if (argc < 3){
cout << "usage: inputfile outputfile" ;
return 0;
}
return 0;
}
Pixel::Pixel(){}
void Picture::vFlip(){
Pixel tmp;
for (int i = 0; i < rows/2; i++){
for (int j = 0; j < columns/2; j++){
tmp = pic[i][j];
pic[i][j] = pic[i][columns-j-1];
pic[i][columns-j-1] = tmp;
}
}
}
void Picture::hFlip(){
vector<Pixel> tmp;
for (int i = 0; i < rows/2; i++){
tmp = pic[i];
pic[i] = pic[rows - i -1];
pic[rows - i - 1] = tmp;
}
}
bool Picture::readPpmFile(string inputFile){
ifstream infile;
string tmp;
int r, g, b;
infile.open(inputFile.c_str());
if (infile.fail()){
cout << "Failed to open " << inputFile << endl;
return false ;
}
infile >> tmp;
infile >> columns >> rows;
while (infile >> r >> g >> b){
Pixel::Pixel(int r, int g, int b);
}
return true ;
}
int Picture::writePpmFile(string outputFile) {
ofstream outfile;
if (rows == 0) return -1;
outfile.open(outputFile.c_str());
if (outfile.fail()){
cout << "Failed to open " << outputFile << endl;
return -2;
}
outfile << "P3" << endl;
outfile << columns << " " << rows << endl;
outfile << maxIntensity << endl;
for (int i = 0; i < rows-1; i++){
for (int j = 0; j < columns - 1; j++){
outfile << pic[i][j].red << " " << pic[i][j].green << " " << pic[i][j].blue << endl;
}
}
return 0;
}
Last edited on Apr 22, 2016 at 7:37pm UTC
Apr 22, 2016 at 8:25pm UTC
1 2 3 4 5
while (infile >> r >> g >> b)
{
Pixel::Pixel(int r, int g, int b);
}
What do you want to do ?
Maybe create a pixel struct ?
In this case use
1 2 3 4 5 6
while (infile >> r >> g >> b)
{
Pixel pixel(r, g, b);
// use pixel
}
BTW.
If you have a default constructor let him initialize the variables:
1 2 3 4
Pixel::Pixel()
{
red = green = blue = 0;
}
Topic archived. No new replies allowed.