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