/* * Generate gif file to visualize the microstructure */ #include #include #include /* gd library */ #include #include void generate_snapshot(const char *filename, const char *label, int nx, int ny, double *c) { FILE *fp; int i, j; int inten; gdImagePtr im; /* Declare the image */ int gray[256]; /* array of gray shades */ int red; /* Allocate the image: nx pixels across by ny pixels tall */ im = gdImageCreate(nx, ny); /* Allocate the color red. */ red = gdImageColorAllocate(im, 255, 0, 0); /* Allocate gray colors */ for (i = 0; i < 256; i++) { gray[i] = gdImageColorAllocate(im, i, i, i); } for (i = 0; i < nx; ++i) { for (j = 0; j < ny; ++j) { inten = (int) (255.0 * c[j + ny * i]); if (inten < 0) inten = 0; else if (inten > 255) inten = 255; /* Set a pixel */ gdImageSetPixel(im, i, j, gray[inten]); } } /* Horizontally centered label at the bottom of the picture */ gdImageString(im, gdFontGetLarge(), im->sx/2 - (strlen(label)*gdFontGetLarge()->w/2), im->sy - gdFontGetLarge()->h, (unsigned char *)label, red); /* Open the graphics file. "wb" means "write binary", important under MSDOS, harmless under Unix. */ if ((fp = fopen(filename, "wb")) == NULL) { printf("Unable to open the snapshot file %s for write. Exiting\n", filename); exit(0); } /* Output the image to the disk file in gif format. */ gdImageGif(im, fp); /* Destroy the image in memory. */ gdImageDestroy(im); (void) fclose(fp); }