/** * @file main.c * * This is the main driver of the program, i.e., * the program, which is then used by the user. */ #include #include #include #include #include #include #include #include "voronoi.h" int main(int argc, char** argv) { clock_t begin = clock(); int xDim = 10000; int yDim = 10000; //initialize point and associated color arrays int numPoints = 20; int pointArraySize = numPoints * 2; int* points = malloc((sizeof(int)*pointArraySize)); int (*colors)[3] = malloc(sizeof(int[numPoints+1][3])); srand(time(NULL)); //init base color (black) for edges and points colors[0][0] = 0; colors[0][1] = 0; colors[0][2] = 0; printf("initializing points and colors\n"); for (int i = 0; i < pointArraySize; i = i+2) { points[i] = rand() % xDim; points[i+1] = rand() % yDim; } for (int i = 0; i < numPoints; i++) { colors[i + 1][0] = rand() % 100; colors[i + 1][1] = rand() % 100; colors[i + 1][2] = rand() % 100; } printf("points initialized: \n"); for (int i = 0; i < pointArraySize; i=i+2) { printf("point %d: x: %d y: %d\n", (i/2), points[i], points[i+1]); } printf("colors initialized: \n"); for (int i = 0; i < (numPoints + 1); i++) { printf("color %d: r: %d g: %d b: %d\n", i, colors[i][0], colors[i][1], colors[i][2]); } //gather voronoi diagram image data printf("allocating image data storage space\n"); unsigned int(*imageBuffer)[yDim] = malloc(sizeof(int[xDim][yDim])); //omp_set_num_threads(20); printf("calculating voronoi diagram image data\n"); //#pragma omp parallel for shared(imageBuffer) #pragma acc parallel loop for (int y = 0; y < yDim; y++) { for (int x = 0; x < xDim; x++) { imageBuffer[x][y] = determineColor(x, y, points, numPoints); } } //create voronoi diagram image printf("creating/overwriting voronoi diagram to image file 'voronoi.ppm'\n"); FILE* fout = fopen("voronoi.ppm", "wb"); if (!fout) { printf("Unable to open output image file. \n"); return 0; } printf("writing image header\n"); (void)fprintf(fout, "P6\n%d %d\n255\n", xDim, yDim); printf("writing image data\n"); for (int y = 0; y < yDim; y++) { for (int x = 0; x < xDim; x++) { int colorIndex = imageBuffer[x][y]; int * pickedColor = colors[colorIndex]; static unsigned char color[3]; color[0] = pickedColor[0]; color[1] = pickedColor[1]; color[2] = pickedColor[2]; (void)fwrite(color, 1, 3, fout); } } printf("finished writing image data\n"); //close(out); (void)fclose(fout); free(imageBuffer); free(points); free(colors); clock_t end = clock(); double time_spent = (double)(end - begin) / CLOCKS_PER_SEC; printf("Execution lasted %lf", time_spent); return EXIT_SUCCESS; }