example6.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // example6.c - Demonstrates how to miniz's PNG writer func
  2. // Public domain, April 11 2012, Rich Geldreich, richgel99@gmail.com. See "unlicense" statement at the end of tinfl.c.
  3. // Mandlebrot set code from http://rosettacode.org/wiki/Mandelbrot_set#C
  4. // Must link this example against libm on Linux.
  5. // Purposely disable a whole bunch of stuff this low-level example doesn't use.
  6. #define MINIZ_NO_STDIO
  7. #define MINIZ_NO_TIME
  8. #define MINIZ_NO_ZLIB_APIS
  9. #include "miniz.h"
  10. // Now include stdio.h because this test uses fopen(), etc. (but we still don't want miniz.c's stdio stuff, for testing).
  11. #include <stdio.h>
  12. #include <limits.h>
  13. #include <math.h>
  14. typedef unsigned char uint8;
  15. typedef unsigned short uint16;
  16. typedef unsigned int uint;
  17. typedef struct
  18. {
  19. uint8 r, g, b;
  20. } rgb_t;
  21. static void hsv_to_rgb(int hue, int min, int max, rgb_t *p)
  22. {
  23. const int invert = 0;
  24. const int saturation = 1;
  25. const int color_rotate = 0;
  26. if (min == max) max = min + 1;
  27. if (invert) hue = max - (hue - min);
  28. if (!saturation) {
  29. p->r = p->g = p->b = 255 * (max - hue) / (max - min);
  30. return;
  31. }
  32. double h = fmod(color_rotate + 1e-4 + 4.0 * (hue - min) / (max - min), 6);
  33. double c = 255.0f * saturation;
  34. double X = c * (1 - fabs(fmod(h, 2) - 1));
  35. p->r = p->g = p->b = 0;
  36. switch((int)h) {
  37. case 0: p->r = c; p->g = X; return;
  38. case 1: p->r = X; p->g = c; return;
  39. case 2: p->g = c; p->b = X; return;
  40. case 3: p->g = X; p->b = c; return;
  41. case 4: p->r = X; p->b = c; return;
  42. default:p->r = c; p->b = X;
  43. }
  44. }
  45. int main(int argc, char *argv[])
  46. {
  47. (void)argc, (void)argv;
  48. // Image resolution
  49. const int iXmax = 4096;
  50. const int iYmax = 4096;
  51. // Output filename
  52. static const char *pFilename = "mandelbrot.png";
  53. int iX, iY;
  54. const double CxMin = -2.5;
  55. const double CxMax = 1.5;
  56. const double CyMin = -2.0;
  57. const double CyMax = 2.0;
  58. double PixelWidth = (CxMax - CxMin) / iXmax;
  59. double PixelHeight = (CyMax - CyMin) / iYmax;
  60. // Z=Zx+Zy*i ; Z0 = 0
  61. double Zx, Zy;
  62. double Zx2, Zy2; // Zx2=Zx*Zx; Zy2=Zy*Zy
  63. int Iteration;
  64. const int IterationMax = 200;
  65. // bail-out value , radius of circle
  66. const double EscapeRadius = 2;
  67. double ER2=EscapeRadius * EscapeRadius;
  68. uint8 *pImage = (uint8 *)malloc(iXmax * 3 * iYmax);
  69. // world ( double) coordinate = parameter plane
  70. double Cx,Cy;
  71. int MinIter = 9999, MaxIter = 0;
  72. for(iY = 0; iY < iYmax; iY++)
  73. {
  74. Cy = CyMin + iY * PixelHeight;
  75. if (fabs(Cy) < PixelHeight/2)
  76. Cy = 0.0; // Main antenna
  77. for(iX = 0; iX < iXmax; iX++)
  78. {
  79. uint8 *color = pImage + (iX * 3) + (iY * iXmax * 3);
  80. Cx = CxMin + iX * PixelWidth;
  81. // initial value of orbit = critical point Z= 0
  82. Zx = 0.0;
  83. Zy = 0.0;
  84. Zx2 = Zx * Zx;
  85. Zy2 = Zy * Zy;
  86. for (Iteration=0;Iteration<IterationMax && ((Zx2+Zy2)<ER2);Iteration++)
  87. {
  88. Zy = 2 * Zx * Zy + Cy;
  89. Zx =Zx2 - Zy2 + Cx;
  90. Zx2 = Zx * Zx;
  91. Zy2 = Zy * Zy;
  92. };
  93. color[0] = (uint8)Iteration;
  94. color[1] = (uint8)Iteration >> 8;
  95. color[2] = 0;
  96. if (Iteration < MinIter)
  97. MinIter = Iteration;
  98. if (Iteration > MaxIter)
  99. MaxIter = Iteration;
  100. }
  101. }
  102. for(iY = 0; iY < iYmax; iY++)
  103. {
  104. for(iX = 0; iX < iXmax; iX++)
  105. {
  106. uint8 *color = (uint8 *)(pImage + (iX * 3) + (iY * iXmax * 3));
  107. uint Iterations = color[0] | (color[1] << 8U);
  108. hsv_to_rgb(Iterations, MinIter, MaxIter, (rgb_t *)color);
  109. }
  110. }
  111. // Now write the PNG image.
  112. {
  113. size_t png_data_size = 0;
  114. void *pPNG_data = tdefl_write_image_to_png_file_in_memory_ex(pImage, iXmax, iYmax, 3, &png_data_size, 6, MZ_FALSE);
  115. if (!pPNG_data)
  116. fprintf(stderr, "tdefl_write_image_to_png_file_in_memory_ex() failed!\n");
  117. else
  118. {
  119. FILE *pFile = fopen(pFilename, "wb");
  120. fwrite(pPNG_data, 1, png_data_size, pFile);
  121. fclose(pFile);
  122. printf("Wrote %s\n", pFilename);
  123. }
  124. // mz_free() is by default just an alias to free() internally, but if you've overridden miniz's allocation funcs you'll probably need to call mz_free().
  125. mz_free(pPNG_data);
  126. }
  127. free(pImage);
  128. return EXIT_SUCCESS;
  129. }