#include #include #include #include #include #include using namespace std; using ColorT = unsigned char; struct PixelT { ColorT color[3]; }; using ImageT = vector >; void WriteImage(string baseFileName, const ImageT & image); int main(int argc, char * argv[]) { double startTime, loopTime, writeTime; int xdim{100}; int ydim{100}; int threads{1}; bool doWrite{true}; bool verbose{false}; string fileName{"foo"}; int opt; while((opt = getopt(argc, argv, "w:h:t:f:xv")) != -1) { switch(opt) { case 'w': xdim = atoi(optarg); if (xdim < 100) { xdim = 100; } break; case 'h': ydim = atoi(optarg); if (ydim < 100) { ydim = 100; } break; case 'f': fileName = optarg; break; case 't': threads = atoi(optarg); if (threads < 1) { threads = 1; } break; case 'v': verbose = true; break; case 'x': doWrite = false; break; } } omp_set_num_threads(threads); if(verbose) { cout << "\tCreating a " << xdim << " x " << ydim << " image." << endl; cout << "\tUsing " << threads << " threads. " << endl; if (doWrite) { cout << "\tWriting the output file to " << fileName << endl; } else { cout << "\tNot writing the output file." << endl; } } vector> image(xdim,vector(ydim)); startTime = omp_get_wtime(); #pragma omp parallel { int id = omp_get_thread_num()+1; unsigned int seed; #pragma omp for for(int i = 0; i < xdim; ++i) { for(int j = 0; j < ydim; ++j) { for(int k =0; k < 3; ++k) { image[i][j].color[k] = rand_r(&seed)%255; //image[i][j].color[k] = 255/id; } } } } loopTime = omp_get_wtime(); if (doWrite) { WriteImage(fileName, image); } writeTime = omp_get_wtime(); if (verbose) { cout << fixed; cout << endl; cout << "Total time: "<< setprecision(2) << writeTime - startTime << " sec " << endl; cout << "Loop time: " << setprecision(2) << loopTime - startTime << " sec " << endl; if (doWrite) { cout << "Write time: " << setprecision(2) << writeTime - loopTime << " sec " << endl; } } return 0; } void WriteImage(string baseFileName, const ImageT & image){ string pngName {baseFileName + ".ppm"}; // open a file for writing int flags = O_CREAT | O_WRONLY | O_TRUNC; int mode {S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR}; int fd {open(pngName.c_str(), flags, mode)}; if (-1 == fd) { perror("Error Open:"); } string tmp; tmp = "P6\n"; // write the PBM header // the magic number P6 (in ascii) followed by a newline write(fd, tmp.c_str(), sizeof(char) * tmp.size()); // width space height newline in ascii tmp = to_string(image.size()) + " " + to_string(image[0].size()) + "\n"; write(fd, tmp.c_str(), sizeof(char) * tmp.size()); // the maximum value (255) newline in ascii tmp = "255\n"; write(fd, tmp.c_str(), sizeof(char) * tmp.size()); // write the rgb values row by row in binary format for(int i = 0; i < image.size(); ++i) { for(int j = 0; j < image[0].size(); ++j) { write(fd, image[i][j].color, sizeof(ColorT) * 3); } } // close the file close(fd); // run ppmtogif or ppmtojpeg string jpegName{baseFileName + ".jpeg"}; string cmd { "ppmtojpeg < " + pngName + " > " + jpegName}; system(cmd.c_str()); unlink(pngName.c_str()); }