MRaster examples 21.0.0.0
Image Processing Library
Loading...
Searching...
No Matches
brownianDiffusion.cpp
Go to the documentation of this file.
1// -*- Mode:C++; Coding:us-ascii-unix; fill-column:158 -*-
2/*******************************************************************************************************************************************************.H.S.**/
3/**
4 @file brownianDiffusion.cpp
5 @author Mitch Richling <https://www.mitchr.me>
6 @brief Read a TIFF image and simulate brownian diffusion.@EOL
7 @keywords dla fractal brownian motion brown
8 @parblock
9 Copyright (c) 1988-2015, Mitchell Jay Richling <https://www.mitchr.me> All rights reserved.
10
11 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
12
13 1. Redistributions of source code must retain the above copyright notice, this list of conditions, and the following disclaimer.
14
15 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions, and the following disclaimer in the documentation
16 and/or other materials provided with the distribution.
17
18 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
19 without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 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
26 DAMAGE.
27 @endparblock
28*/
29/*******************************************************************************************************************************************************.H.E.**/
30/** @cond exj */
31
32//--------------------------------------------------------------------------------------------------------------------------------------------------------------
33#include "ramCanvas.hpp"
34
35//--------------------------------------------------------------------------------------------------------------------------------------------------------------
36int main(int argc, char *argv[]) {
37 std::chrono::time_point<std::chrono::system_clock> startTime = std::chrono::system_clock::now();
38 std::random_device rd;
39 std::minstd_rand0 rEng(rd());
40 mjr::ramCanvas3c8b theRamCanvas(7680/2, 4320/2, -2.2, 2.2, -2.2, 2.2);
41 theRamCanvas.clrCanvas(mjr::ramCanvas3c8b::colorType(255, 0, 0));
42 int MAXCOUNT = 524288; // Number of times to try and find a point
43
44 int stenX[8] = {1, -1, 0, 0, 1, 1, -1, -1};
45 int stenY[8] = {0, 0, 1, -1, 1, -1, 1, -1};
46
47
48 if((argc < 3) || (argc > 4)) {
49 std::cout << "ERROR: Useage: dlaBrownian inFile.tiff outFile_without_extension [ITERATIONS]" << std::endl;
50 }
51
52 if(theRamCanvas.readTIFFfile(argv[1])) {
53 std::cout << "ERROR: Problem with file: " << argv[2] << std::endl;
54 return 1;
55 }
56
57 if(argc > 3)
58 MAXCOUNT = std::stoi(argv[3]);
59
60 for(int tgtLumPct=1;tgtLumPct<=100;tgtLumPct+=10) {
61 double tgtLum=tgtLumPct / 100.0;
62
63 for(int numItr=1;numItr<MAXCOUNT;numItr++) {
64 mjr::ramCanvas3c8b::coordIntType xC, yC;
65 xC = static_cast<int>(rEng() % theRamCanvas.getNumPixX());
66 yC = static_cast<int>(rEng() % theRamCanvas.getNumPixY());
67
68 mjr::ramCanvas3c8b::colorType aColor = theRamCanvas.getPxColor(xC, yC);
69 double lumC = aColor.luminanceRGB();
70
71 if((lumC > 0) && (lumC < tgtLum)) {
72 double maxLum = -1;
73 int maxDir = -1;
74 int lum0ct = 0;
75 for(int curDir=0; curDir<8; curDir++) {
76 mjr::ramCanvas3c8b::coordIntType xN, yN;
77 xN = xC+stenX[curDir];
78 yN = yC+stenY[curDir];
79 if(xN>=0 && yN>=0 && xN<theRamCanvas.getNumPixX() && yN<theRamCanvas.getNumPixY()) {
80 double lumN = theRamCanvas.getPxColor(xN, yN).luminanceRGB();
81 if(lumN <= 0) {
82 lum0ct++;
83 } else {
84 if((lumN > lumC) && (maxLum < lumN)) {
85 maxLum = lumN;
86 maxDir = curDir;
87 }
88 }
89 }
90 }
91
92 // If sourounted by zeros, then random move
93 if(lum0ct==8) {
94 int curDir = rEng() % 8;
95 mjr::ramCanvas3c8b::coordIntType xN, yN;
96 xN = xC+stenX[curDir];
97 yN = yC+stenY[curDir];
98 if(xN>=0 && yN>=0 && xN<theRamCanvas.getNumPixX() && yN<theRamCanvas.getNumPixY()) {
99 maxDir = curDir;
100 }
101 }
102
103 if(maxDir > -1) {
104 mjr::ramCanvas3c8b::coordIntType xN, yN;
105 xN = xC+stenX[maxDir];
106 yN = yC+stenY[maxDir];
107
108 theRamCanvas.getPxColorRefNC(xN, yN).tfrmAdd(aColor);
109 theRamCanvas.drawPoint(xC, yC, mjr::ramCanvas3c8b::colorType(0, 0, 0));
110 }
111 }
112 }
113
114 theRamCanvas.writeTIFFfile(argv[2] + mjr::math::str::fmt_int(tgtLumPct, 3, '0') + ".tiff");
115 std::cout << "Write: " << tgtLumPct << std::endl;
116 }
117
118 std::chrono::duration<double> runTime = std::chrono::system_clock::now() - startTime;
119 std::cout << "Total Runtime " << runTime.count() << " sec" << std::endl;
120
121 return 1;
122}
123/** @endcond */
int main(int argc, char *argv[])