MRaster examples 21.0.0.0
Image Processing Library
Loading...
Searching...
No Matches
mandelbrot_binary.cpp
Go to the documentation of this file.
1// -*- Mode:C++; Coding:us-ascii-unix; fill-column:158 -*-
2/*******************************************************************************************************************************************************.H.S.**/
3/**
4 @file mandelbrot_binary.cpp
5 @author Mitch Richling <https://www.mitchr.me>
6 @brief This program draws a Mandelbrot the binary method.@EOL
7 @std C++20
8 @copyright
9 @parblock
10 Copyright (c) 1988-2015, Mitchell Jay Richling <https://www.mitchr.me> All rights reserved.
11
12 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
13
14 1. Redistributions of source code must retain the above copyright notice, this list of conditions, and the following disclaimer.
15
16 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions, and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
18
19 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software
20 without specific prior written permission.
21
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
27 DAMAGE.
28 @endparblock
29 @filedetails
30
31 We use the "binary" method with a twist.
32
33 - bin: The traditional binary method
34 - gray: Shaded by the argument
35 - quad: Like the traditional method, but different colors for each quadrant
36
37 This example demonstrates how to use some of the types defined in the ramCanvas object (integer coordinates, double coordinates, color, and color channel)
38*/
39/*******************************************************************************************************************************************************.H.E.**/
40/** @cond exj */
41
42//--------------------------------------------------------------------------------------------------------------------------------------------------------------
43#include "ramCanvas.hpp"
44
45//--------------------------------------------------------------------------------------------------------------------------------------------------------------
46const int MAXITR = 1024*1;
47
48double ranges[3][4] = { { -2.0, 1.0, -1.5, 1.5 },
49 { -0.12, -0.03, -0.92, -0.81 },
50 { 0.0353469, 0.5353469, 0.1153845, 0.6153845 }
51 };
52
53enum class whyStopMB {OUTSET, MAXCOUNT, INSET};
54
55//--------------------------------------------------------------------------------------------------------------------------------------------------------------
56typedef mjr::ramCanvas3c8b rc; // The Ram Canvas type we will use
57typedef rc::colorType rcc; // The color type in our Ram Canvas type
58
59//--------------------------------------------------------------------------------------------------------------------------------------------------------------
60int main(void) {
61 std::chrono::time_point<std::chrono::system_clock> startTime = std::chrono::system_clock::now();
62 const int CSIZE = 1024*1;
63 const rc::coordFltType BALL = 100;
64 rc binRamCanvas(CSIZE, CSIZE), grayRamCanvas(CSIZE, CSIZE), quadRamCanvas(CSIZE, CSIZE);
65 whyStopMB why;
66
67 for(int i=0; i<3; i++) {
68 binRamCanvas.newRealCoords(ranges[i][0], ranges[i][1], ranges[i][2], ranges[i][3]);
69 binRamCanvas.clrCanvasToBlack();
70 std::complex<rc::coordFltType> z;
71 for(rc::coordIntType y=0;y<binRamCanvas.getNumPixY();y++) {
72 if((y%(CSIZE/10))==0)
73 std::cout << " CASE: " << i << " LINE: " << y << "/" << CSIZE << std::endl;
74 for(rc::coordIntType x=0;x<binRamCanvas.getNumPixX();x++) {
75 int count;
76 rc::coordFltType cr = binRamCanvas.int2realX(x);
77 rc::coordFltType ci = binRamCanvas.int2realY(y);
78 std::complex<rc::coordFltType> c(cr, ci);
79 rc::coordFltType p = std::abs(c-0.25);
80 if((cr >= p-2.0*p*p+0.25) && std::abs(c+1.0) >= 0.25) {
81 z=c;
82 for(count=0; ; count++) {
83 if(count>=MAXITR) {
84 why = whyStopMB::MAXCOUNT;
85 break;
86 }
87 if(std::abs(z)>BALL) {
88 why = whyStopMB::OUTSET;
89 break;
90 }
91 z = z * z + c;
92 }
93 } else {
94 why = whyStopMB::INSET;
95 }
96
97 if(why == whyStopMB::OUTSET) {
98 rc::coordFltType zAbs = std::abs(z);
99 if(zAbs > 0.001) {
100 rcc::channelType ns = ( std::imag(z) > 0 ? 0 : 255 );
101 rcc::channelType ew = ( std::real(z) > 0 ? 0 : 255 );
102 grayRamCanvas.drawPoint(x, y, mjr::color3c8b::csCCdiag01::c(static_cast<mjr::ramCanvas3c8b::csIntType>(mjr::math::linm::scl_real_to_int((std::real(z) / zAbs + 1.0) / 2.0, 255))));
103 binRamCanvas.drawPoint(x, y, rcc(ns, ns, ns));
104 quadRamCanvas.drawPoint(x, y, rcc(ns, static_cast<rcc::channelType>(255-(ns+ew)/2), ew));
105 }
106 } else {
107 grayRamCanvas.drawPoint(x, y, "red");
108 binRamCanvas.drawPoint(x, y, "red");
109 quadRamCanvas.drawPoint(x, y, "red");
110 }
111 }
112 }
113
114 binRamCanvas.writeTIFFfile("mandelbrot_binary_bin_" + std::to_string(i) + ".tiff");
115 grayRamCanvas.writeTIFFfile("mandelbrot_binary_gray_" + std::to_string(i) + ".tiff");
116 quadRamCanvas.writeTIFFfile("mandelbrot_binary_quad_" + std::to_string(i) + ".tiff");
117 }
118
119 std::chrono::duration<double> runTime = std::chrono::system_clock::now() - startTime;
120 std::cout << "Total Runtime " << runTime.count() << " sec" << std::endl;
121}
122/** @endcond */
int main(int argc, char *argv[])