Quick actions

cmd+k|ctrl+k

Navigation

Languages

Lab2_Q1

Snippet info

Language

Cpp

Visibility

public

Author

chriscui14

Created

2020-06-04T20:53:07Z

Updated

2020-06-04T21:26:15Z

#include <iostream>
using namespace std;

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int xRes=256, yRes=256;
    int color = 255;
    
    float **xtable; // pointer to pointer[s]
    xtable = new float*[yRes]; // NOTE how we do this!

    for(int i=0;i<yRes;i++) {
      xtable[i] = new float[xRes]; // each row points to xRes elements ("columns")
    }

    for(int i=0;i<yRes;i++){
       for(int j=0;j<xRes;j++){
           xtable[i][j]=255; // store 255 for pixel data, "for now"
       }
    }

    ofstream pgmFile("myImg.pgm"); // output image file we're creating

    // header
    pgmFile << "P2" << endl;
    pgmFile << xRes << " " << yRes << endl; // how many columns, how many rows
    pgmFile << 255 << endl; // largest pixel value we'll be outputting (below)

    // pixel data
    for(int i=0;i<yRes;i++){
        if(i%32 == 0){
            if(color == 0){
                color = 255;
            }else{
                color = 0;
            }
        }
        for(int j=0;j<xRes;j++){
            if(i%32 == 0){
                if(color == 0){
                    color = 255;
                }else{
                    color = 0;
                }
            }
            xtable[i][j] = color;
            pgmFile << xtable[i][j] << " ";
        }// next column
        pgmFile << endl;
    }// next row

    // all done!
    pgmFile.close();
    return 0;
}// main()
INFO