Generate mandelbrot images using many clustered computers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.3 KiB

  1. #include <stdio.h>
  2. #include <png.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <string.h>
  6. #include "types.h"
  7. void savePng(char *filename, int h, int w, Pixel *buf)
  8. {
  9. FILE *fp;
  10. fp = fopen(filename, "wb");
  11. if(!fp)
  12. {
  13. printf("ERROR: Could not save image!\r\n");
  14. return;
  15. }
  16. png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  17. png_infop info_ptr = png_create_info_struct(png_ptr);
  18. png_set_IHDR (png_ptr,
  19. info_ptr,
  20. w, //image width
  21. h, //height
  22. 8, //bit depth
  23. PNG_COLOR_TYPE_RGB,
  24. PNG_INTERLACE_NONE,
  25. PNG_COMPRESSION_TYPE_DEFAULT,
  26. PNG_FILTER_TYPE_DEFAULT);
  27. png_byte **row_pointers = png_malloc (png_ptr, h * sizeof (png_byte *));
  28. for(int y = 0; y < h; y++)
  29. {
  30. png_byte *row = png_malloc (png_ptr, sizeof(uint8_t) * w * 3);
  31. row_pointers[y] = row;
  32. for(int x = 0; x < w; x++)
  33. {
  34. Pixel pixel = buf[x + w*y];
  35. *row++ = pixel.red;
  36. *row++ = pixel.green;
  37. *row++ = pixel.blue;
  38. }
  39. }
  40. png_init_io (png_ptr, fp);
  41. png_set_rows (png_ptr, info_ptr, row_pointers);
  42. png_write_png (png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
  43. fclose(fp);
  44. //Free memory
  45. for(int y = 0; y < h; y++)
  46. {
  47. png_free(png_ptr, row_pointers[y]);
  48. }
  49. png_free(png_ptr, row_pointers);
  50. png_destroy_write_struct(&png_ptr, &info_ptr);
  51. }