int main(int argc, char ** argv) { struct ppm * picture; color_t pixel; int i, j; picture = ppm_init(WIDTH, HEIGHT); for(i = 0; i < WIDTH; i++) { for(j = 0; j < HEIGHT; j++) { pixel.red = (float)i * j / (WIDTH * HEIGHT) * 255; //i == 2 && j == 2 ? 255 : 0; pixel.green = 0; pixel.blue = pixel.green; ppm_write(picture, i, j, pixel); } } ppm_save(picture, "picture.ppm"); return 0; }
int main(int argc,char *argv[]) { if (argc==2 && (strcmp(argv[1],"-help")==0)) { showHelp(); return 0; } if (argc!=4) { Warning("Zle zadane argumenty\n"); showHelp(); return 1; } struct ppm *ppmFile=ppm_read(argv[1]); if (ppmFile==NULL) { FatalError("Chyba citania zo suboru"); return 1; } EncodeMessage(ppmFile,argv[3]); if (ppm_write(ppmFile,argv[2])!=0) { free(ppmFile); Warning("Zapis do suboru sa nepodaril"); return 1; } free(ppmFile); return 0; }
static void compute_chunk(struct mandelbrot_param *args) { int i, j, val; float Cim, Cre; color_t pixel; // Iterate through lines for (i = args->begin_h; i < args->end_h; i++) { // Iterate through pixels in a line for (j = args->begin_w; j < args->end_w; j++) { // Convert the coordinate of the pixel to be calculated to both // real and imaginary parts of the complex number to be checked Cim = (float) i / args->height * (args->upper_i - args->lower_i) + args->lower_i; Cre = (float) j / args->width * (args->upper_r - args->lower_r) + args->lower_r; // Gets the value returned by is_in_mandelbrot() and scale it // from 0 to 255, or -1 if (Cre, Cim) is in the mandelbrot set. val = is_in_Mandelbrot(Cre, Cim, args->maxiter); // Change a negative value to 0 in val to make mandelbrot // elements to appear black in the final picture. pixel = val > args->maxiter ? args->mandelbrot_color : color[val % num_colors(args)]; ppm_write(args->picture, j, i, pixel); } } }
int icv_write(icv_image_t *bif, const char *filename, ICV_IMAGE_FORMAT format) { /* FIXME: should not be introducing fixed size buffers */ char buf[BUFSIZ] = {0}; if (format == ICV_IMAGE_AUTO) { format = (ICV_IMAGE_FORMAT)icv_guess_file_format(filename, buf); } ICV_IMAGE_VAL_INT(bif); switch (format) { /* case ICV_IMAGE_BMP: return bmp_write(bif, filename); */ case ICV_IMAGE_PPM: return ppm_write(bif, filename); case ICV_IMAGE_PNG: return png_write(bif, filename); case ICV_IMAGE_PIX: return pix_write(bif, filename); case ICV_IMAGE_BW: return bw_write(bif, filename); case ICV_IMAGE_DPIX : return dpix_write(bif, filename); default: bu_log("Unrecognized format. Outputting in PIX format.\n"); } return pix_write(bif, filename); }
int main(int argc, char **argv) { uint32_t w = 600; uint32_t h = 400; uint32_t rs = w * 3; uint8_t *img = (uint8_t*)malloc( h * rs ); memset(img, 0, h*rs); draw_line_rgb(img, 0, 0, 600, 400, 0xFFFF00, w, h, rs); draw_hline_rgb(img, 0, 200, 200, 0xFF00FF, w, h, rs); draw_hline_rgb(img, 200, 300, 0, 0xFF00FF, w, h, rs); int triangle[] = { 300, 200, 300, 100, 400, 200 }; draw_polygon_rgb(img, 3, triangle, 0x00FF00, 0, w, h, rs); draw_rect_rgb(img, 300, 300, 390, 390, 0xFFFFFF, 1, w, h, rs); draw_rect_rgb(img, 350, 250, 450, 330, 0x80FF0000, 1, w, h, rs); draw_rect_rgb(img, 400, 200, 500, 300, 0xa00000FF, 1, w, h, rs); draw_ellipse_rgb(img, 500, 100, 575, 150, 0x00FFFF, 1, 0, w, h, rs); draw_ellipse_rgb(img, 450, 25, 525, 100, 0x00FFFF, 0, 0, w, h, rs); draw_ellipse_rgb(img, -50,-50,50,50, 0xFFFF00, 1, 0, w, h, rs); FILE *fp = fopen("out.ppm", "wb"); ppm_write( fp, img, w, h, w*3 ); fclose( fp ); free( img ); printf("created out.ppm\n"); return 0; }
/* * pic_write: write pic to file in the specified format * returns TRUE on success, FALSE on failure */ int pic_write(char *file, Pic *pic, Pic_file_format format) { switch( format ) { case PIC_TIFF_FILE: //return tiff_write(file, pic); break; case PIC_PPM_FILE: return ppm_write(file, pic); break; case PIC_JPEG_FILE: //return jpeg_write(file, pic); break; default: fprintf(stderr, "pic_write: can't write %s, unknown format\n", file); return FALSE; } }
// Write a screen-shot, in the PPM format, to the specified filename, in PPM format void saveScreenshot(int windowWidth, int windowHeight, char * filename) { if (filename == NULL) return; // Allocate a picture buffer Pic * in = pic_alloc(windowWidth, windowHeight, 3, NULL); printf("File to save to: %s\n", filename); for (int i=windowHeight-1; i>=0; i--) { glReadPixels(0, windowHeight-i-1, windowWidth, 1, GL_RGB, GL_UNSIGNED_BYTE, &in->pix[i*in->nx*in->bpp]); } if (ppm_write(filename, in)) printf("File saved Successfully\n"); else printf("Error in Saving\n"); pic_free(in); }
/***** You may modify this portion *****/ static void compute_chunk(struct mandelbrot_param *args, int offseti, int offsetj) { int i, j, val; float Cim, Cre; color_t pixel; //#pragma omp parallel for schedule(dynamic, 1) private(i,j,pixel,Cim,Cre,val) for (i = 0; i < args->height; i++) { for (j = 0; j < args->width; j++) { // Convert the coordinate of the pixel to be calculated to both // real and imaginary parts of the complex number to be checked Cim = (float) i / args->height * (args->upper_i - args->lower_i) + args->lower_i; Cre = (float) j / args->width * (args->upper_r - args->lower_r) + args->lower_r; // Gets the value returned by is_in_mandelbrot() and scale it // from 0 to 255, or -1 if (Cre, Cim) is in the mandelbrot set. val = is_in_Mandelbrot(Cre, Cim, args->maxiter); // Change a negative value to 0 in val to make mandelbrot // elements to appear black in the final picture. val = val > args->maxiter ? args->mandelbrot_color : color[val % num_colors(args)]; // Permute green, red and blue to get different fancy colors pixel.green = val >> 16 & 255; pixel.red = val >> 8 & 255; pixel.blue = val & 255; ppm_write(args->picture, j + offsetj, i + offseti, pixel); } } }
int main(int argc, char **argv) { Timer *render_timer; Sdl *sdl; FILE *out; Colour *buffer; Pixel *pixels; int num_pixels; SDL_Event event = {0}; if (argc < 2) return 1; sdl = sdl_load(argv[1]); if (sdl == NULL) return 1; if (!init_SDL()) return 1; num_pixels = config->width * config->height; buffer = calloc(num_pixels, sizeof(Colour)); pixels = calloc(num_pixels, sizeof(Pixel)); for (int j = 0; j < config->height; j++) for (int i = 0; i < config->width; i++) { pixels[j*config->width + i].x = i; pixels[j*config->width + i].y = j; } srand(time(NULL)); shuffle_pixels(pixels, config->width, config->height); srand(0x20071208); /* START */ render_timer = timer_start("Rendering"); for (int i = 0; i < num_pixels; i++) { Camera *cam = scene->camera; Colour c; Ray r; int x = pixels[i].x, y = pixels[i].y; /* The last parameter is the near plane, which is irrelevant for * the moment. */ r = camera_ray(cam, x, y, 1); c = ray_colour(r, 0); buffer[config->width*y + x] = c; put_pixel(display_surface, x, y, c); if (i % config->width == 0) { SDL_Flip(display_surface); while (SDL_PollEvent(&event)) if (event.type == SDL_QUIT) return 0; } } /* STOP */ timer_stop(render_timer); timer_diff_print(render_timer); printf("%.2f kilopixels per second\n", num_pixels/1000./(timer_diff(render_timer))); out = fopen("ray.ppm", "w"); ppm_write(buffer, config->width, config->height, out); free(buffer); fclose(out); SDL_Flip(display_surface); while(1) { while (SDL_WaitEvent(&event)) if (event.type == SDL_QUIT) return 0; else if (event.type == SDL_VIDEOEXPOSE) SDL_Flip(display_surface); } return 0; }
int main(int argc, char* argv[]) { /* Arguments */ if (argc != 3 ) { printf("\nUsage: %s <file> <clusters count> \n\n", argv[0]); return 0; } ppm_t* src = ppm_read(argv[1]); int clusters_count = atoi(argv[2]); if (clusters_count < 2) { printf("\nParameter <clusters count> must be greater than 1 (got %s)\n", argv[2]); return 0; } cluster_data* clusters = malloc(clusters_count * sizeof(cluster_data)); for (int i = 0; i < clusters_count; i++) { clusters[i].count = 0; clusters[i].red_sum = 0; clusters[i].green_sum = 0; clusters[i].blue_sum = 0; // Choose initial cluster centers randomly clusters[i].center = (color) { rand() % src->maxval, rand() % src->maxval, rand() % src->maxval }; } /* // Forced initial three clusters[0].center = (color) { 255, 83, 201 }; clusters[1].center = (color) { 222, 219, 64 }; clusters[2].center = (color) { 0, 0, 0 }; */ int* pixel_cluster = malloc(src->cols * src->rows * sizeof(int)); for (;;) { bool modified = false; for (int i = 0; i < src->rows; i++) { for (int j = 0; j < src->cols; j++) { color c = PPM_AT(src, i, j); int min_distance_sq = INT_MAX; int min_cluster = -1; for (int cluster = 0; cluster < clusters_count; cluster++) { color center = clusters[cluster].center; int dred = center.red - c.red; int dgreen = center.green - c.green; int dblue = center.blue - c.blue; int distance_sq = dred * dred + dgreen * dgreen + dblue * dblue; if (distance_sq < min_distance_sq) { min_cluster = cluster; min_distance_sq = distance_sq; } } if (pixel_cluster[i * src->cols + j] != min_cluster) { pixel_cluster[i * src->cols + j] = min_cluster; modified = true; } clusters[min_cluster].count++; clusters[min_cluster].red_sum += c.red; clusters[min_cluster].green_sum += c.green; clusters[min_cluster].blue_sum += c.blue; } } if (!modified) { break; } for (int i = 0; i < clusters_count; i++) { if (clusters[i].count > 0) { clusters[i].center.red = clusters[i].red_sum; clusters[i].center.red /= clusters[i].count; clusters[i].center.green = clusters[i].green_sum / clusters[i].count; clusters[i].center.blue = clusters[i].blue_sum / clusters[i].count; } clusters[i].count = 0; clusters[i].red_sum = 0; clusters[i].green_sum = 0; clusters[i].blue_sum = 0; } } ppm_t* dst = ppm_create_empty(src->cols, src->rows, src->maxval); for (int i = 0; i < src->rows; i++) { for (int j = 0; j < src->cols; j++) { int cluster = pixel_cluster[i * src->cols + j]; PPM_AT(dst, i, j) = clusters[cluster].center; } } ppm_write(dst, true); return 0; }