I am working in C on a physics experiment, Young\'s interference experiment and i made a program who prints to file a huge bunch of pixels :
for
Here's a minimal example that writes your image file with a minimal PPM header:
#include
#include
#include // compile with gcc -lm
int main(){
/* Setup for Young's interference image */
#define width 256
unsigned char raster_matrix[width*width], h[3];
#define WAVE(x,y) sin(sqrt( (x)*(x)+(y)*(y) ) / 3.0)
#define hue(c) (h[0] = c, h[1] = 128, h[2] = 255-c, h)
int x, y, i = 0;
for (y = 0; y < width; y++) for (x = 0; x < width; x++)
raster_matrix[i++] = 128 + 64*(WAVE(x,y) + WAVE(x,width-y));
/* Open PPM File */
FILE *file = fopen("young.ppm", "wb"); if (!file) return -1;
/* Write PPM Header */
fprintf(file, "P6 %d %d %d\n", width, width, 255); /* width, height, maxval */
/* Write Image Data */
for (i=0; i < width*width; i++)
fwrite(hue(raster_matrix[i]), 1, 3, file);
/* Close PPM File */
fclose(file);
/* All done */
return 0;
}
I wrote the header code based on the specs at http://netpbm.sourceforge.net/doc/ppm.html. For this image, the header is just a string of fifteen bytes: "P6 256 256 255\n".
The setup code here is slightly hackish, but makes it possible to concisely demonstrate the code with the exact for loop body given in the question.