void write_frame(int time) { if (rank != 0) { MPI_Send(u+1, nxl, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD); } else { int source, i, j, count, global_index; MPI_Status status; im = gdImageCreate(nx*PWIDTH,PHEIGHT); if (time == 0) { for (j=0; j<MAXCOLORS; j++) colors[j] = gdImageColorAllocate (im, j, 0, MAXCOLORS-j-1); gdImageGifAnimBegin(im, file, 1, -1); } else { gdImagePaletteCopy(im, previm); } global_index = 0; for (source = 0; source < nprocs; source++) { if (source != 0) { MPI_Recv(buf, 1+nx/nprocs, MPI_DOUBLE, source, 0, MPI_COMM_WORLD, &status); MPI_Get_count(&status, MPI_DOUBLE, &count); } else { for (i = 1; i <= nxl; i++) buf[i-1] = u[i]; count = nxl; } for (i = 0; i < count; i++) { int color = (int)(buf[i]*MAXCOLORS/M); assert(color >= 0); if (color >= MAXCOLORS) color = MAXCOLORS-1; gdImageFilledRectangle (im, global_index*PWIDTH, 0, (global_index+1)*PWIDTH-1, PHEIGHT-1, colors[color]); global_index++; } } if (time == 0) { gdImageGifAnimAdd(im, file, 0, 0, 0, 0, gdDisposalNone, NULL); } else { gdImageGifAnimAdd(im, file, 0, 0, 0, 5, gdDisposalNone, previm); gdImageDestroy(previm); } previm=im; im=NULL; } #ifdef DEBUG write_plain(time); #endif }
int main(void) { int i; FILE * out; gdImagePtr im; gdImagePtr prev =NULL; int black; im = gdImageCreate(100, 100); if (!im) { fprintf(stderr, "can't create image"); return 1; } out = fopen("anim.gif", "wb"); if (!out) { fprintf(stderr, "can't create file %s", "anim.gif"); return 1; } gdImageColorAllocate(im, 255, 255, 255); /* allocate white as side effect */ gdImageGifAnimBegin(im, out, 1, -1); for(i = 0; i < 20; i++) { int r,g,b; im = gdImageCreate(100, 100); r = rand() % 255; g = rand() % 255; b = rand() % 255; gdImageColorAllocate(im, 255, 255, 255); /* allocate white as side effect */ black = gdImageColorAllocate(im, r, g, b); printf("(%i, %i, %i)\n",r, g, b); gdImageFilledRectangle(im, rand() % 100, rand() % 100, rand() % 100, rand() % 100, black); gdImageGifAnimAdd(im, out, 1, 0, 0, 10, 1, prev); if(prev) { gdImageDestroy(prev); } prev = im; } gdImageGifAnimEnd(out); fclose(out); return 0; }
void write_animated_gif(universe *u, const char *filename) { FILE *out = fopen(filename, "wb"); gdImagePtr im; int l, r, t, b; int ll, rr, tt, bb; generation *g = u->first; generation_find_bounds(g, &l, &r, &t, &b); for(g = g->next; g; g = g->next) { generation_find_bounds(g, &ll, &rr, &tt, &bb); if(ll < l) l = ll; if(rr > r) r = rr; if(tt < t) t = tt; if(bb > b) b = bb; } int scale = 4; int delay = 10; im = gdImageCreate((r - l) * scale, (b - t) * scale); int background = gdImageColorAllocate(im, 230, 230, 255); gdImageRectangle(im, 0, 0, (r-l) * scale, (b-t) * scale, background); int tile_bg = gdImageColorAllocate(im, 255, 255, 255); int tile_frame = gdImageColorAllocate(im, 230, 230, 230); int on = gdImageColorAllocate(im, 0, 0, 0); int unknown = gdImageColorAllocate(im, 255, 255, 0); int unknown_stable = gdImageColorAllocate(im, 192, 255, 0); gdImageGifAnimBegin(im, out, 1, 0); for(g = u->first; g; g = g->next) { tile *tp; for(tp = g->all_first; tp; tp = tp->all_next) { if(!(tp->flags & IS_LIVE)) { continue; } gdImageFilledRectangle(im, GX(tp->xpos), GY(tp->ypos), GX(tp->xpos + TILE_WIDTH)-1, GY(tp->ypos + TILE_HEIGHT)-1, tile_bg); gdImageRectangle(im, GX(tp->xpos), GY(tp->ypos), GX(tp->xpos + TILE_WIDTH)-1, GY(tp->ypos + TILE_HEIGHT)-1, tile_frame); int xx, yy; for(xx = 0; xx < TILE_WIDTH; xx++) { for(yy = 0; yy < TILE_HEIGHT; yy++) { cellvalue cv = tile_get_cell(tp, xx, yy); int colour = -1; if(cv == ON) { colour = on; } else if(cv == UNKNOWN) { colour = unknown; } else if(cv == UNKNOWN_STABLE) { colour = unknown_stable; } if(colour >= 0) gdImageFilledRectangle(im, GX(tp->xpos + xx), GY(tp->ypos + yy), GX(tp->xpos + xx + 1) - 1, GY(tp->ypos + yy + 1) - 1, colour); } } } gdImageGifAnimAdd(im, out, 0, 0, 0, delay, gdDisposalNone, NULL); } gdImageGifAnimEnd(out); fclose(out); gdImageDestroy(im); }