Debian Codegraph @

case_studies/fractal/tilegen/mandelbrot.cpp

raw
1 /*
2 Copyright 2015 Google Inc. All rights reserved.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17
18 #include <cstdlib>
19 #include <cmath>
20 #include <iostream>
21
22 #include <string>
23 #include <vector>
24
25 #include <png.h>
26
27
28 int main(int argc, const char **argv)
29 {
30 if (argc != 8) {
31 std::cerr <<"Usage: <width> <height> <iters> <left> <bottom> <right> <top>" << std::endl;
32 std::cerr <<"PNG file is written to stdout." << std::endl;
33 exit(EXIT_FAILURE);
34 }
35
36 unsigned long width = strtoul(argv[1], NULL, 10);
37 unsigned long height = strtoul(argv[2], NULL, 10);
38 unsigned long iterations = strtoul(argv[3], NULL, 10);
39 double left = strtod(argv[4], NULL);
40 double bottom = strtod(argv[5], NULL);
41 double right = strtod(argv[6], NULL);
42 double top = strtod(argv[7], NULL);
43
44 struct Exception {
45 const std::string &msg;
46 Exception(const std::string &msg) : msg(msg) { }
47 };
48
49 int code = EXIT_SUCCESS;
50 png_structp png_write_struct = NULL;
51 volatile png_infop info_ptr = NULL;
52
53 try {
54 png_write_struct = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
55 if (png_write_struct == NULL)
56 throw Exception("Could not allocate libpng write_struct.");
57
58 info_ptr = png_create_info_struct(png_write_struct);
59 if (info_ptr == NULL)
60 throw Exception("Could not allocate libpng info_struct.");
61
62 if (setjmp(png_jmpbuf(png_write_struct)))
63 throw Exception("Exception from libpng");
64
65 png_init_io(png_write_struct, stdout);
66
67 png_set_IHDR(png_write_struct, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB,
68 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
69
70 png_write_info(png_write_struct, info_ptr);
71
72 std::vector<png_byte> row(3 * width);
73 for (unsigned long y=0 ; y<height ; y++) {
74 const double scalar_y = double(y) / height;
75 const double mb_y = scalar_y * bottom + (1-scalar_y) * top; // libpng y==0 is top
76 for (unsigned long x=0 ; x<width ; x++) {
77 const double scalar_x = double(x) / width;
78 const double mb_x = scalar_x * right + (1-scalar_x) * left;
79 row[3*x + 0] = 255;
80 row[3*x + 1] = 255;
81 row[3*x + 2] = 255;
82 double c_x = mb_x;
83 double c_y = mb_y;
84 for (unsigned i=1 ; i<iterations ; ++i) {
85 const double len2 = c_x*c_x + c_y*c_y;
86 if (len2 > 4) {
87 // escaped
88 // smooth with fractional escape velocity
89 float escape_vel = i + 1 - ::log2f(::logf(::sqrtf(len2)));
90 float tonemapped = escape_vel / (20 + escape_vel);
91 row[3*x + 0] = png_byte(powf(tonemapped, 10) * 255);
92 row[3*x + 1] = png_byte(powf(tonemapped, 2.5) * 255);
93 row[3*x + 2] = png_byte(tonemapped * 255);
94 /*
95 row[3*x + 0] = png_byte(escape_vel);
96 row[3*x + 1] = png_byte(escape_vel);
97 row[3*x + 2] = png_byte(escape_vel);
98 */
99 break;
100 }
101 const double o_x = c_x;
102 c_x = c_x*c_x - c_y*c_y + mb_x;
103 c_y = 2 * o_x * c_y + mb_y;
104 }
105 }
106 png_write_row(png_write_struct, &row[0]);
107 }
108
109 png_write_end(png_write_struct, NULL);
110
111 } catch (const Exception &e) {
112 std::cerr << e.msg << std::endl;
113 code = EXIT_FAILURE;
114 }
115
116 if (png_write_struct != NULL) png_destroy_write_struct(&png_write_struct, (png_infopp)NULL);
117 if (info_ptr != NULL) png_free_data(png_write_struct, info_ptr, PNG_FREE_ALL, -1);
118
119 return code;
120 }