MRaster examples 22.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#include "MRMathLINM.hpp"
45
46//--------------------------------------------------------------------------------------------------------------------------------------------------------------
47const int MAXITR = 1024*1;
48
49double ranges[3][4] = { { -2.0, 1.0, -1.5, 1.5 },
50 { -0.12, -0.03, -0.92, -0.81 },
51 { 0.0353469, 0.5353469, 0.1153845, 0.6153845 }
52 };
53
54enum class whyStopMB {OUTSET, MAXCOUNT, INSET};
55
56//--------------------------------------------------------------------------------------------------------------------------------------------------------------
57typedef mjr::ramCanvas3c8b rc; // The Ram Canvas type we will use
58typedef rc::colorType rcc; // The color type in our Ram Canvas type
59
60//--------------------------------------------------------------------------------------------------------------------------------------------------------------
61int main(void) {
62 std::chrono::time_point<std::chrono::system_clock> startTime = std::chrono::system_clock::now();
63 const int CSIZE = 1024*1;
64 const rc::coordFltType BALL = 100;
65 rc binRamCanvas(CSIZE, CSIZE), grayRamCanvas(CSIZE, CSIZE), quadRamCanvas(CSIZE, CSIZE);
66 whyStopMB why;
67
68 for(int i=0; i<3; i++) {
69 binRamCanvas.newRealCoords(ranges[i][0], ranges[i][1], ranges[i][2], ranges[i][3]);
70 binRamCanvas.clrCanvasToBlack();
71 std::complex<rc::coordFltType> z;
72 for(rc::coordIntType y=0;y<binRamCanvas.getNumPixY();y++) {
73 if((y%(CSIZE/10))==0)
74 std::cout << " CASE: " << i << " LINE: " << y << "/" << CSIZE << std::endl;
75 for(rc::coordIntType x=0;x<binRamCanvas.getNumPixX();x++) {
76 int count;
77 rc::coordFltType cr = binRamCanvas.int2realX(x);
78 rc::coordFltType ci = binRamCanvas.int2realY(y);
79 std::complex<rc::coordFltType> c(cr, ci);
80 rc::coordFltType p = std::abs(c-0.25);
81 if((cr >= p-2.0*p*p+0.25) && std::abs(c+1.0) >= 0.25) {
82 z=c;
83 for(count=0; ; count++) {
84 if(count>=MAXITR) {
85 why = whyStopMB::MAXCOUNT;
86 break;
87 }
88 if(std::abs(z)>BALL) {
89 why = whyStopMB::OUTSET;
90 break;
91 }
92 z = z * z + c;
93 }
94 } else {
95 why = whyStopMB::INSET;
96 }
97
98 if(why == whyStopMB::OUTSET) {
99 rc::coordFltType zAbs = std::abs(z);
100 if(zAbs > 0.001) {
101 rcc::channelType ns = ( std::imag(z) > 0 ? 0 : 255 );
102 rcc::channelType ew = ( std::real(z) > 0 ? 0 : 255 );
103 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))));
104 binRamCanvas.drawPoint(x, y, rcc(ns, ns, ns));
105 quadRamCanvas.drawPoint(x, y, rcc(ns, static_cast<rcc::channelType>(255-(ns+ew)/2), ew));
106 }
107 } else {
108 grayRamCanvas.drawPoint(x, y, "red");
109 binRamCanvas.drawPoint(x, y, "red");
110 quadRamCanvas.drawPoint(x, y, "red");
111 }
112 }
113 }
114
115 binRamCanvas.writeTIFFfile("mandelbrot_binary_bin_" + std::to_string(i) + ".tiff");
116 grayRamCanvas.writeTIFFfile("mandelbrot_binary_gray_" + std::to_string(i) + ".tiff");
117 quadRamCanvas.writeTIFFfile("mandelbrot_binary_quad_" + std::to_string(i) + ".tiff");
118 }
119
120 std::chrono::duration<double> runTime = std::chrono::system_clock::now() - startTime;
121 std::cout << "Total Runtime " << runTime.count() << " sec" << std::endl;
122}
123/** @endcond */
int main(int argc, char *argv[])