int main() { Image * input = readPPMImage("test.ppm"); Image * output = new Image(input->width, input->height); //Run for each color yo pi_blur(input->width, input->height, input->rd,output->rd); pi_blur(input->width, input->height, input->gd,output->gd); pi_blur(input->width, input->height, input->bd,output->bd); writePPMImage(output,"outtest.ppm"); return 0; }
//write a PNM image int writePNMImage(char *filename , unsigned char *image, int height, int width, int depth, bool color, char *comments) { int error; if(color) { error = writePPMImage(filename, image, height, width, depth, comments); } else { error = writePGMImage(filename, image, height, width, depth, comments); } return error; }
//write image data int CmCWriteImage(char *filename, unsigned char *image, int height, int width, int dim, int filetype) { int error, i; unsigned char *data; switch(filetype) { case FILE_PPM: data = image; if(dim == 1) { data = new unsigned char [height * width * 3]; if(!data) return EXE_OUT_OF_MEMORY; for(i = 0; i < height*width; i++) { data[3*i] = data[3*i+1] = data[3*i+2] = image[i]; } } error = writePPMImage(filename, data, height, width, 255, (char *) NULL); if(data != image) delete [] data; break; case FILE_PGM: data = image; if(dim == 3) { data = CmCConvertToGrayscale(image, height, width); if(!data) return EXE_OUT_OF_MEMORY; } error = writePGMImage(filename, data, height, width, 255, (char *) NULL); if(data != image) delete [] data; break; case FILE_PNM: { bool color; if(dim == 3) { color = true; } else { color = false; } error = writePNMImage(filename, image, height, width, 255, color, (char *) NULL); break; } case FILE_MATLAB_ASCII: { float *fdata = CmCConvertToFloat(image, height, width, dim); if(!fdata) return EXE_OUT_OF_MEMORY; error = CmCWriteMFile(filename, fdata, height, width, dim); delete [] fdata; break; } default: return EXE_UNSUPPORTED_FILE_FORMAT; } if(!error) return NO_ERRORS; else return EXE_FILE_WRITE_ERROR; }
int main(int argc, char** argv) { const unsigned int width = 1200; const unsigned int height = 800; const int maxIterations = 256; int numThreads = 2; float x0 = -2; float x1 = 1; float y0 = -1; float y1 = 1; // parse commandline options //////////////////////////////////////////// int opt; static struct option long_options[] = { {"threads", 1, 0, 't'}, {"view", 1, 0, 'v'}, {"help", 0, 0, '?'}, {0 ,0, 0, 0} }; while ((opt = getopt_long(argc, argv, "t:v:?", long_options, NULL)) != EOF) { switch (opt) { case 't': { numThreads = atoi(optarg); break; } case 'v': { int viewIndex = atoi(optarg); // change view settings if (viewIndex == 2) { float scaleValue = .015f; float shiftX = -.986f; float shiftY = .30f; scaleAndShift(x0, x1, y0, y1, scaleValue, shiftX, shiftY); } else if (viewIndex > 1) { fprintf(stderr, "Invalid view index\n"); return 1; } break; } case '?': default: usage(argv[0]); return 1; } } // end parsing of commandline options int* output_serial = new int[width*height]; int* output_thread = new int[width*height]; // // Run the serial implementation. Run the code three times and // take the minimum to get a good estimate. // memset(output_serial, 0, width * height * sizeof(int)); double minSerial = 1e30; for (int i = 0; i < 3; ++i) { double startTime = CycleTimer::currentSeconds(); mandelbrotSerial(x0, y0, x1, y1, width, height, 0, height, maxIterations, output_serial); double endTime = CycleTimer::currentSeconds(); minSerial = std::min(minSerial, endTime - startTime); } printf("[mandelbrot serial]:\t\t[%.3f] ms\n", minSerial * 1000); writePPMImage(output_serial, width, height, "mandelbrot-serial.ppm", maxIterations); // // Run the threaded version // memset(output_thread, 0, width * height * sizeof(int)); double minThread = 1e30; for (int i = 0; i < 3; ++i) { double startTime = CycleTimer::currentSeconds(); mandelbrotThread(numThreads, x0, y0, x1, y1, width, height, maxIterations, output_thread); double endTime = CycleTimer::currentSeconds(); minThread = std::min(minThread, endTime - startTime); } printf("[mandelbrot thread]:\t\t[%.3f] ms\n", minThread * 1000); writePPMImage(output_thread, width, height, "mandelbrot-thread.ppm", maxIterations); if (! verifyResult (output_serial, output_thread, width, height)) { printf ("Error : Output from threads does not match serial output\n"); delete[] output_serial; delete[] output_thread; return 1; } // compute speedup printf("\t\t\t\t(%.2fx speedup from %d threads)\n", minSerial/minThread, numThreads); delete[] output_serial; delete[] output_thread; return 0; }