example6.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. } else {
  32. const double h_dbl = fmod(color_rotate + 1e-4 + 4.0 * (hue - min) / (max - min), 6);
  33. const double c_dbl = 255 * saturation;
  34. const double X_dbl = c_dbl * (1 - fabs(fmod(h_dbl, 2) - 1));
  35. const int h = (int)h_dbl;
  36. const int c = (int)c_dbl;
  37. const int X = (int)X_dbl;
  38. p->r = p->g = p->b = 0;
  39. switch(h) {
  40. case 0: p->r = c; p->g = X; return;
  41. case 1: p->r = X; p->g = c; return;
  42. case 2: p->g = c; p->b = X; return;
  43. case 3: p->g = X; p->b = c; return;
  44. case 4: p->r = X; p->b = c; return;
  45. default:p->r = c; p->b = X;
  46. }
  47. }
  48. }
  49. int main(int argc, char *argv[])
  50. {
  51. // Image resolution
  52. const int iXmax = 4096;
  53. const int iYmax = 4096;
  54. // Output filename
  55. static const char *pFilename = "mandelbrot.png";
  56. int iX, iY;
  57. const double CxMin = -2.5;
  58. const double CxMax = 1.5;
  59. const double CyMin = -2.0;
  60. const double CyMax = 2.0;
  61. double PixelWidth = (CxMax - CxMin) / iXmax;
  62. double PixelHeight = (CyMax - CyMin) / iYmax;
  63. // Z=Zx+Zy*i ; Z0 = 0
  64. double Zx, Zy;
  65. double Zx2, Zy2; // Zx2=Zx*Zx; Zy2=Zy*Zy
  66. int Iteration;
  67. const int IterationMax = 200;
  68. // bail-out value , radius of circle
  69. const double EscapeRadius = 2;
  70. double ER2=EscapeRadius * EscapeRadius;
  71. uint8 *pImage = (uint8 *)malloc(iXmax * 3 * iYmax);
  72. // world ( double) coordinate = parameter plane
  73. double Cx,Cy;
  74. int MinIter = 9999, MaxIter = 0;
  75. (void)argc, (void)argv;
  76. for(iY = 0; iY < iYmax; iY++)
  77. {
  78. Cy = CyMin + iY * PixelHeight;
  79. if (fabs(Cy) < PixelHeight/2)
  80. Cy = 0.0; // Main antenna
  81. for(iX = 0; iX < iXmax; iX++)
  82. {
  83. uint8 *color = pImage + (iX * 3) + (iY * iXmax * 3);
  84. Cx = CxMin + iX * PixelWidth;
  85. // initial value of orbit = critical point Z= 0
  86. Zx = 0.0;
  87. Zy = 0.0;
  88. Zx2 = Zx * Zx;
  89. Zy2 = Zy * Zy;
  90. for (Iteration=0;Iteration<IterationMax && ((Zx2+Zy2)<ER2);Iteration++)
  91. {
  92. Zy = 2 * Zx * Zy + Cy;
  93. Zx =Zx2 - Zy2 + Cx;
  94. Zx2 = Zx * Zx;
  95. Zy2 = Zy * Zy;
  96. };
  97. color[0] = (uint8)Iteration;
  98. color[1] = (uint8)Iteration >> 8;
  99. color[2] = 0;
  100. if (Iteration < MinIter)
  101. MinIter = Iteration;
  102. if (Iteration > MaxIter)
  103. MaxIter = Iteration;
  104. }
  105. }
  106. for(iY = 0; iY < iYmax; iY++)
  107. {
  108. for(iX = 0; iX < iXmax; iX++)
  109. {
  110. uint8 *color = (uint8 *)(pImage + (iX * 3) + (iY * iXmax * 3));
  111. uint Iterations = color[0] | (color[1] << 8U);
  112. hsv_to_rgb((int)Iterations, MinIter, MaxIter, (rgb_t *)color);
  113. }
  114. }
  115. // Now write the PNG image.
  116. {
  117. size_t png_data_size = 0;
  118. void *pPNG_data = tdefl_write_image_to_png_file_in_memory_ex(pImage, iXmax, iYmax, 3, &png_data_size, 6, MZ_FALSE);
  119. if (!pPNG_data)
  120. fprintf(stderr, "tdefl_write_image_to_png_file_in_memory_ex() failed!\n");
  121. else
  122. {
  123. FILE *pFile = fopen(pFilename, "wb");
  124. fwrite(pPNG_data, 1, png_data_size, pFile);
  125. fclose(pFile);
  126. printf("Wrote %s\n", pFilename);
  127. }
  128. // 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().
  129. mz_free(pPNG_data);
  130. }
  131. free(pImage);
  132. return EXIT_SUCCESS;
  133. }