lundi 2 décembre 2019

c++ if statement only runs when value is extremely negative

I'm doing a school assignment, where im trying to make an image scaling funcion. I'm trying to run the chunk of code in the 'scaleimage' function. I've placed several cout's in the code to see how the program runs. im using mingw compiler, Ive compiled the code with no errors and when i type in "[exe file] images/mona_lisa.pgm images/monascaled.pgm scale 0.5" into command prompt. i get

increment: -2147483648
second loop clear
second loop clear
second loop clear
second loop clear
second loop clear
second loop clear
second loop clear
second loop clear
second loop clear
second loop clear

Im expecting the first line to say "increment: 2" but i get -2147483648 instead. when i manually set the value of scalefactor to 0.5 , making increment = 2, i get just

increment: 2

with nothing after and the program just ends there. What is going on, and how do i get this to run properly

ImageType scaleimage(const ImageType &image, double scalefactor) {
    // Create a new image that is the required new size
    ImageType origimage = image;
    int rows = origimage.getHeight();
    int cols = origimage.getWidth();
    int **grid = origimage.getPixels();
    int scaledr=-1;
    int scaledc=-1;
    int avgx=0;
    int avgy=0;
    int avg=0;
    int increment =0;

    int scaledheight = rows * scalefactor;
    int scaledwidth = cols * scalefactor;
    increment = 1/scalefactor;

    // change this statement: it currently creates a useless image object.
    ImageType scaled(scaledheight,scaledwidth,255);
    int **scaledgrid = scaled.getPixels();

    cout<<"increment: "<<increment<<endl;

    if(scalefactor<1){
        for(int r=0; r<rows; r+=increment){
            while(scaledr<scaledheight){
                scaledr++;
            }
            for(int c=0; c<cols; c+=increment){
                while(scaledc<scaledwidth){
                    scaledc++;
                }
                for(avgx=c;avgx<increment;avgx++){
                    for(avgy=r;avgy<increment;avgy++){
                        avg+=grid[avgy][avgx];
                    }
                }
                scaledgrid[scaledr][scaledc] = avg/pow(increment,2);
                avg=0;
                cout<<"second loop clear"<<endl;
            }
            cout<<"first loop clear"<<endl;
        }
    }
    else{
        cout<<"else";
    }

    return scaled;
}


int main(int argc, char* argv[]) {

/********************************************/
// THiS PART IS ALL DONE FOR YOU:
    if (argc < 4) {
        cout << "Invalid program execution, please use:\n";
        cout << "./go <inputfile> <outputfile> <operation>\n";
        return(0);
    }

    string infile = argv[1];
    string outfile = argv[2];
    string task = argv[3];
    string scale = argv[4];

    double scalefactor;
    // converts string 'scale' to int
    scalefactor = stoi(scale);
    //scalefactor = 0.5;



    // Create an image object from a file
    ImageType image(infile);

//FOR DEBUG PURPOSES:
//image.print();

/****************** END ***********************/

// YOU WILL NEED TO ADD STATEMENTS TO THE CODE THAT FOLLOWS
// THE SET UP FOR TASK invert HAS BEEN SET UP FOR YOU.
// SIMILAR PROCEDURES ARE REQUIRED FOR THE REMAINING TASKS

    // Depending on the task, do what is necessary with the image,
    // by calling the appropriate function.
    if (task == "invert") {
        ImageType inverted = invert(image);
        inverted.writeToFile(outfile);
    }
    else if (task == "verticalFlip"){
        ImageType vflip = verticalFlip(image);
        vflip.writeToFile(outfile);
    }
    else if (task == "horizontalFlip"){
        ImageType hflip = horizontalFlip(image);
        hflip.writeToFile(outfile);
    }
    else if (task == "rotate"){
        ImageType rotated = rotate(image);
        rotated.writeToFile(outfile);
    }
    else if (task == "makeAscii"){
        makeAscii(image,outfile);
    }
    else if (task == "scale"){
        ImageType scaled = scaleimage(image,scalefactor);
        scaled.writeToFile(outfile);
    }
    else {
        cout << "Error in the task: Do not know what " << task << " means.\n";
        return (0);
    }

    cout << "All done\n";
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire