- FILE *fp = fopen(filename, "w");
- fprintf(fp, "P6\n%d %d\n255\n", WIDTH, HEIGHT);
+ FILE *fp = fopen(filename, "wb");
+ png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+ png_infop info_ptr = png_create_info_struct(png_ptr);
+
+ if (setjmp(png_jmpbuf(png_ptr))) {
+ fclose(fp);
+ fprintf(stderr, "Write to %s failed; exiting.\n", filename);
+ exit(1);
+ }
+
+ png_set_IHDR(png_ptr, info_ptr, WIDTH, HEIGHT, 8, PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
+
+ png_bytep *row_pointers = new png_bytep[HEIGHT];