pytorch
162 строки · 4.1 Кб
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
6// Purposely disable a whole bunch of stuff this low-level example doesn't use.
7#define MINIZ_NO_STDIO8#define MINIZ_NO_TIME9#define MINIZ_NO_ZLIB_APIS10#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
17typedef unsigned char uint8;18typedef unsigned short uint16;19typedef unsigned int uint;20
21typedef struct22{
23uint8 r, g, b;24} rgb_t;25
26static void hsv_to_rgb(int hue, int min, int max, rgb_t *p)27{
28const int invert = 0;29const int saturation = 1;30const int color_rotate = 0;31
32if (min == max) max = min + 1;33if (invert) hue = max - (hue - min);34if (!saturation) {35p->r = p->g = p->b = 255 * (max - hue) / (max - min);36return;37}38double h = fmod(color_rotate + 1e-4 + 4.0 * (hue - min) / (max - min), 6);39double c = 255.0f * saturation;40double X = c * (1 - fabs(fmod(h, 2) - 1));41
42p->r = p->g = p->b = 0;43
44switch((int)h) {45case 0: p->r = c; p->g = X; return;46case 1: p->r = X; p->g = c; return;47case 2: p->g = c; p->b = X; return;48case 3: p->g = X; p->b = c; return;49case 4: p->r = X; p->b = c; return;50default:p->r = c; p->b = X;51}52}
53
54int main(int argc, char *argv[])55{
56(void)argc, (void)argv;57
58// Image resolution59const int iXmax = 4096;60const int iYmax = 4096;61
62// Output filename63static const char *pFilename = "mandelbrot.png";64
65int iX, iY;66const double CxMin = -2.5;67const double CxMax = 1.5;68const double CyMin = -2.0;69const double CyMax = 2.0;70
71double PixelWidth = (CxMax - CxMin) / iXmax;72double PixelHeight = (CyMax - CyMin) / iYmax;73
74// Z=Zx+Zy*i ; Z0 = 075double Zx, Zy;76double Zx2, Zy2; // Zx2=Zx*Zx; Zy2=Zy*Zy77
78int Iteration;79const int IterationMax = 200;80
81// bail-out value , radius of circle82const double EscapeRadius = 2;83double ER2=EscapeRadius * EscapeRadius;84
85uint8 *pImage = (uint8 *)malloc(iXmax * 3 * iYmax);86
87// world ( double) coordinate = parameter plane88double Cx,Cy;89
90int MinIter = 9999, MaxIter = 0;91
92for(iY = 0; iY < iYmax; iY++)93{94Cy = CyMin + iY * PixelHeight;95if (fabs(Cy) < PixelHeight/2)96Cy = 0.0; // Main antenna97
98for(iX = 0; iX < iXmax; iX++)99{100uint8 *color = pImage + (iX * 3) + (iY * iXmax * 3);101
102Cx = CxMin + iX * PixelWidth;103
104// initial value of orbit = critical point Z= 0105Zx = 0.0;106Zy = 0.0;107Zx2 = Zx * Zx;108Zy2 = Zy * Zy;109
110for (Iteration=0;Iteration<IterationMax && ((Zx2+Zy2)<ER2);Iteration++)111{112Zy = 2 * Zx * Zy + Cy;113Zx =Zx2 - Zy2 + Cx;114Zx2 = Zx * Zx;115Zy2 = Zy * Zy;116};117
118color[0] = (uint8)Iteration;119color[1] = (uint8)Iteration >> 8;120color[2] = 0;121
122if (Iteration < MinIter)123MinIter = Iteration;124if (Iteration > MaxIter)125MaxIter = Iteration;126}127}128
129for(iY = 0; iY < iYmax; iY++)130{131for(iX = 0; iX < iXmax; iX++)132{133uint8 *color = (uint8 *)(pImage + (iX * 3) + (iY * iXmax * 3));134
135uint Iterations = color[0] | (color[1] << 8U);136
137hsv_to_rgb(Iterations, MinIter, MaxIter, (rgb_t *)color);138}139}140
141// Now write the PNG image.142{143size_t png_data_size = 0;144void *pPNG_data = tdefl_write_image_to_png_file_in_memory_ex(pImage, iXmax, iYmax, 3, &png_data_size, 6, MZ_FALSE);145if (!pPNG_data)146fprintf(stderr, "tdefl_write_image_to_png_file_in_memory_ex() failed!\n");147else148{149FILE *pFile = fopen(pFilename, "wb");150fwrite(pPNG_data, 1, png_data_size, pFile);151fclose(pFile);152printf("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().156mz_free(pPNG_data);157}158
159free(pImage);160
161return EXIT_SUCCESS;162}
163