diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/benoit/archstr.cc | 31 | ||||
-rw-r--r-- | src/benoit/arghandl.cc | 94 | ||||
-rw-r--r-- | src/benoit/benoit.cc | 138 | ||||
-rw-r--r-- | src/benoit/crtcfg.cc | 8 | ||||
-rw-r--r-- | src/benoit/dat.cc | 2 | ||||
-rw-r--r-- | src/benoit/exit.cc | 31 | ||||
-rw-r--r-- | src/benoit/helpscrn.cc | 135 | ||||
-rw-r--r-- | src/benoit/kernelstr.cc | 47 | ||||
-rw-r--r-- | src/benoit/loadcfg.cc | 12 | ||||
-rw-r--r-- | src/benoit/log.cc | 16 | ||||
-rw-r--r-- | src/benoit/logfunc.cc | 8 | ||||
-rw-r--r-- | src/benoit/logfuncret.cc | 9 | ||||
-rw-r--r-- | src/benoit/main.cc | 145 | ||||
-rw-r--r-- | src/benoit/notiffunc.cc | 9 | ||||
-rw-r--r-- | src/benoit/plotmandelbrot.cc | 49 | ||||
-rw-r--r-- | src/benoit/print.cc | 8 | ||||
-rw-r--r-- | src/benoit/t/pos/pos.cc | 2 | ||||
-rw-r--r-- | src/benoit/~benoit.cc | 10 | ||||
-rw-r--r-- | src/main.cc | 4 |
19 files changed, 572 insertions, 186 deletions
diff --git a/src/benoit/archstr.cc b/src/benoit/archstr.cc new file mode 100644 index 0000000..1c6d164 --- /dev/null +++ b/src/benoit/archstr.cc @@ -0,0 +1,31 @@ +# include <benoit/archstr.hh> +# include <benoit/t/arch.hh> +# include <string> +# include <unordered_map> +using namespace std::literals::string_literals; +std::string benoit::archstr(benoit::t::arch arch) noexcept { + std::unordered_map<benoit::t::arch,std::string> map = { + { + benoit::t::arch::aarch64, + "ARM64 / AArch64"s, + }, + { + benoit::t::arch::amd64, + "AMD64 / x86-64"s, + }, + { + benoit::t::arch::ia64, + "IA-64"s, + }, + { + benoit::t::arch::ppc64, + "PPC64"s, + }, + { + benoit::t::arch::unknown, + "N/A"s, + } + }; + std::string str = map[arch]; + return str; +} diff --git a/src/benoit/arghandl.cc b/src/benoit/arghandl.cc index 059728b..2b4026b 100644 --- a/src/benoit/arghandl.cc +++ b/src/benoit/arghandl.cc @@ -1,16 +1,100 @@ -# include <benoit.hh> +# include <benoit/archstr.hh> +# include <benoit/arghandl.hh> +# include <benoit/dat.hh> +# include <benoit/exit.hh> +# include <benoit/helpscrn.hh> +# include <benoit/kernelstr.hh> +# include <benoit/log.hh> +# include <benoit/logfunc.hh> +# include <benoit/logfuncret.hh> +# include <benoit/print.hh> +# include <benoit/t/imgfmt.hh> # include <fmt/core.h> +# include <string> +# include <unordered_map> +using namespace std::literals::string_literals; void benoit::arghandl(int const & argc,char const * * & argv) { std::string const funcname = "benoit::arghandl(int const &,char const * * &)"s; - this->notiffunc(funcname); + benoit::logfunc(funcname); if(argc < 0x2) { + benoit::logfuncret(funcname); return; } for(int pos = 0x1;(pos < argc);++pos) { std::string arg = argv[pos]; - if(arg == "--help") { - this->print(fmt::format("benoit {}",this->ver)); - this->exit(EXIT_SUCCESS); + benoit::log(fmt::format("Found argument “{}”.",arg)); + std::string::size_type eqpos = arg.find("="s); + if(eqpos != std::string::npos) { + std::unordered_map<std::string,bool> strtobool = { + { + "false"s, + false, + }, + { + "true"s, + true, + } + }; + std::string invalvalforobj = "Unrecognised value “{}” for object “{}”."s; + std::string obj = arg.substr(0x0,eqpos); + benoit::log(funcname,fmt::format("Found object “{}”.",obj)); + std::string val = arg.substr(eqpos + 0x1); + benoit::log(funcname,fmt::format("Found value “{}”.",val)); + if(obj == "debug"s) { + if(!strtobool.contains(val)) { + benoit::print(fmt::format(invalvalforobj,val,obj)); + } + else { + benoit::dat.debug = strtobool[val]; + } + } + else if(obj == "format"s) { + if(val == "PNG"s) { + benoit::dat.imgfmt = benoit::t::imgfmt::png; + } + else if(val == "PPM"s) { + benoit::dat.imgfmt = benoit::t::imgfmt::ppm; + } + else if(val == "WebP"s) { + benoit::dat.imgfmt = benoit::t::imgfmt::webp; + } + else { + benoit::print(fmt::format(invalvalforobj,val,obj),true); + } + } + else if(obj == "output"s) { + benoit::dat.outimg = val; + } + else if(obj == "threads"s) { + benoit::dat.numthrds = std::stoi(val); + } + else { + benoit::print(fmt::format("Invalid object “{}”."s,obj),true); + } } + else { + if((arg == "help"s) || (arg == "--help"s)) { + benoit::helpscrn(); + } + else { + benoit::print(fmt::format("Invalid argument “{}”."s,arg),true); + } + } + } + switch(benoit::dat.imgfmt) { + case benoit::t::imgfmt::png: + benoit::dat.outimg.append(".png"s); + break; + case benoit::t::imgfmt::ppm: + benoit::dat.outimg.append(".ppm"s); + break; + case benoit::t::imgfmt::webp: + if((benoit::dat.resx > 0x1000) || (benoit::dat.resy > 0x1000)) { + benoit::exit(EXIT_FAILURE,"WebP does not support a resolution of more than 4096"s); + } + benoit::dat.outimg.append(".webp"s); + break; } + benoit::log(funcname,fmt::format("The output image will be \u201C{}\u201D."s,benoit::dat.outimg)); + benoit::logfuncret(funcname); } diff --git a/src/benoit/benoit.cc b/src/benoit/benoit.cc deleted file mode 100644 index a97e35c..0000000 --- a/src/benoit/benoit.cc +++ /dev/null @@ -1,138 +0,0 @@ -# include <benoit.hh> -# include <cstddef> -# include <cstdint> -# include <cstdlib> -# include <fcntl.h> -# include <fmt/core.h> -# include <string> -# include <unistd.h> -# include <vector> -# include <webp/encode.h> -# include <webp/types.h> -using namespace std::literals::string_literals; -[[noreturn]] benoit::benoit(int const argc,char const * * argv) noexcept { - std::string const funcname = "benoit::benoit(int const,char const * *)"s; - this->notiffunc(funcname); - this->arghandl(argc,argv); - switch(this->imgfmt) { - case benoit::t::imgfmt::png: - this->outimg.append(".png"s); - break; - case benoit::t::imgfmt::ppm: - this->outimg.append(".ppm"s); - break; - case benoit::t::imgfmt::webp: - if((this->resx > 0x1000) || (this->resy > 0x1000)) { - this->exit(EXIT_FAILURE,"WebP does not support a resolution of more than 4096"s); - } - this->outimg.append(".webp"s); - break; - } - std::vector<std::uint8_t> buf = this->plotmandelbrot(); - this->exit(EXIT_SUCCESS); - int file = ::open(this->outimg.c_str(),O_TRUNC | O_WRONLY); - std::string msg = ("P3 "s + std::to_string(this->resx) + " "s +std::to_string(this->resy) + " 255 "s); - for(auto val : buf) { - msg.append(std::to_string(val)); - msg.append(" "); - } - if(::write(file,msg.c_str(),std::strlen(msg.c_str())) < 0x0) { - this->print(fmt::format("Unable to write to \"{}\"."s,this->outimg)); - } - if(::close(file) < 0x0) { - this->print(fmt::format("Unable to close file \"{]\"."s,this->outimg)); - } - this->exit(EXIT_SUCCESS); -} - /* - auto webpconf = ::WebPConfig(); - webpconf.lossless = 0x1; - webpconf.quality = 0x64; - webpconf.method = 0x6; - webpconf.segments = 0x4; - webpconf.sns_strength = 0x4; - webpconf.filter_strength = 0x0; - webpconf.filter_sharpness = 0x0; - webpconf.autofilter = 0x0; - webpconf.alpha_compression = 0x1; - webpconf.alpha_filtering = 0x2; - webpconf.alpha_quality = 0x64; - webpconf.pass = 0xA; - webpconf.preprocessing = 0x1; - webpconf.partitions = 0x3; - webpconf.partitions = 0x0; - webpconf.use_sharp_yuv = 0x0; - auto conferr = ::WebPValidateConfig(&webpconf); - auto webpimg = ::WebPPicture(); - WebPPictureInit(&webpimg); - webpimg.use_argb = 0x1; - webpimg.resx = 0x1; - webpimg.resy = 0x1; - WebPPictureAlloc(&webpimg); - auto webpmemwrit = WebPMemoryWriter(); - WebPMemoryWriterInit(&webpmemwrit); - webpimg.writer = WebPMemoryWrite; - webpimg.custom_ptr = &webpmemwrit; - webpimg.argb = {0xFF,0xFF,0x0,0x0}; - if(WebPEncode(&webpconf,&webpimg) == 0x0) { - } - else { - - } - WebPPictureFree(&webpimg); - auto buf = std::vector<std::uint8_t>(); - buf.push_back(0xFF); - buf.push_back(0x0); - buf.push_back(0x0); - auto file = std::fstream(this->outimt,std::fstream::binary | std::fstream::out | std::fstream::trunc); - if(!file.is_open()) { - ::_exit(EXIT_FAILURE); - } - const double maxR = 2.25; - const double minR = -2.25; - const double maxI = 2.25; - const double minI = -2.25; - - for(unsigned int y = 0; y < this->resy; ++y) - for(unsigned int x = 0; x < this->resx; ++x) { - long double r = x * ((maxR + this->real * this->zoom) / this->zoom - (minR + this->real * this->zoom) / this->zoom) / this->resx + (minR + this->real * this->zoom) / this->zoom; - long double i = y * ((maxI + this->imag * this->zoom) / this->zoom - (minI + this->imag * this->zoom) / this->zoom) / this->resx + (minI + this->imag * this->zoom) / this->zoom; - long double r2 = 0.0; - long double i2 = 0.0; - - unsigned int iter; - for(iter = 0; iter < this->maxiter; ++iter) { - if(r2 * r2 + i2 * i2 >= 4.0) - break; - - long double r2Temp = r2 * r2 - i2 * i2 + r; - long double i2Temp = 2.0 * r2 * i2 + i; - - if(r2 == r2Temp && i2 == i2Temp) { - iter = this->maxiter; - break; - } - - r2 = r2Temp; - i2 = i2Temp; - } - - std::uint8_t blue = 0x0; - std::uint8_t green = 0x0; - std::uint8_t red = 0x0; - - if(iter != this->maxiter) { - float tmp = ((float)(iter) / (float)(this->maxiter)); - blue = (std::uint8_t)((1.0 - tmp) * 255.0); - green = (std::uint8_t)((1.0 - tmp) * 255.0); - red = (std::uint8_t)((1.0 - tmp) * 255.0); - //double t = (double)(iter) / (double)(this->maxiter); - //red = (int)(8.0 * (1.0 - t) * t * t * t * 255.0); - //green = (int)(16.0 * (1.0 - t) * (1.0 - t) * t * t * 255.0); - //blue = (int)(8.0 * (1.0 - t) * (1.0 - t) * (1.0 - t) * t * 255.0); - } - buf.push_back(blue); - buf.push_back(green); - buf.push_back(red); - } -*/ diff --git a/src/benoit/crtcfg.cc b/src/benoit/crtcfg.cc new file mode 100644 index 0000000..a09b0c8 --- /dev/null +++ b/src/benoit/crtcfg.cc @@ -0,0 +1,8 @@ +# include <benoit/crtcfg.hh> +# include <benoit/logfunc.hh> +# include <string> +using namespace std::literals::string_literals; +void benoit::crtcfg() { + std::string const funcname = "benoit::crtcfg()"s; + benoit::logfunc(funcname); +} diff --git a/src/benoit/dat.cc b/src/benoit/dat.cc new file mode 100644 index 0000000..5796fa1 --- /dev/null +++ b/src/benoit/dat.cc @@ -0,0 +1,2 @@ +# include <benoit/dat.hh> +benoit::t::dat benoit::dat; diff --git a/src/benoit/exit.cc b/src/benoit/exit.cc index e408b9c..98d0619 100644 --- a/src/benoit/exit.cc +++ b/src/benoit/exit.cc @@ -1,13 +1,34 @@ -# include <benoit.hh> +# include <benoit/dat.hh> +# include <benoit/exit.hh> +# include <benoit/logfunc.hh> +# include <benoit/print.hh> +# include <cstdlib> # include <fmt/core.h> # include <string> -# include <unistd.h> +# include <time.h> using namespace std::literals::string_literals; [[noreturn]] void benoit::exit(int code,std::string msg) noexcept { std::string const funcname = "benoit::exit(int,std::string)"s; - this->notiffunc(funcname); - if(msg != ""s) { - this->print(fmt::format("Exited with error code {}: {}",code,msg),true); + benoit::logfunc(funcname); + if(benoit::dat.debug) { + benoit::print(fmt::format("Exited with code {}: \"{}\"."s,code,msg),true); + } + if((code == EXIT_FAILURE) || benoit::dat.dobt || benoit::dat.debug) { + benoit::dat.printdolog = false; + benoit::print(""s); + benoit::print("+-------------"s); + benoit::print("| :Backtrace:"s); + benoit::print("+-"s); + benoit::print(""s); + for(auto entry : benoit::dat.thelog) { + { + ::timespec sleepfor; + sleepfor.tv_sec = 0x0; + sleepfor.tv_nsec = (0x3B9ACA00L / 0x3L); + ::nanosleep(&sleepfor,nullptr); + } + benoit::print(entry); + } } ::_exit(code); } diff --git a/src/benoit/helpscrn.cc b/src/benoit/helpscrn.cc new file mode 100644 index 0000000..1b9ef97 --- /dev/null +++ b/src/benoit/helpscrn.cc @@ -0,0 +1,135 @@ +# include <benoit/archstr.hh> +# include <benoit/helpscrn.hh> +# include <benoit/dat.hh> +# include <benoit/exit.hh> +# include <benoit/kernelstr.hh> +# include <benoit/logfunc.hh> +# include <benoit/print.hh> +# include <fmt/core.h> +# include <string> +# include <unordered_map> +using namespace std::literals::string_literals; +[[noreturn]] void benoit::helpscrn() noexcept { + std::string const funcname = "benoit::helpscrn()"s; + benoit::logfunc(funcname); + std::string cmdate = ""s; + { + std::string date = __DATE__; + std::string time = __TIME__; + std::unordered_map<std::string,std::string> monthmap = { + { + "Jan", + "01", + }, + { + "Feb", + "02", + }, + { + "Mar", + "03", + }, + { + "Apr", + "04", + }, + { + "May", + "05", + }, + { + "Jun", + "06", + }, + { + "Jul", + "07", + }, + { + "Aug", + "08", + }, + { + "Sep", + "09", + }, + { + "Oct", + "10", + }, + { + "Nov", + "11", + }, + { + "Dec", + "12", + }, + }; + std::string year = date.substr(0x7); + std::string month = monthmap[date.substr(0x0,0x3)]; + std::string day = date.substr(0x4,0x2); + cmdate = year + "-"s + month + "-"s + day + "T"s + time; + } + std::string cppver = ""s; + { + long constexpr cplusplus = __cplusplus; + switch(cplusplus) { + default: + cppver = fmt::format("{}"s,cplusplus); + break; + case 0x30C1Fl: + cppver = "C++98 / C++03"s; + break; + case 0x3118Fl: + cppver = "C++11"s; + break; + case 0x312BAl: + cppver = "C++14"s; + break; + case 0x313E7l: + cppver = "C++17"s; + break; + case 0x31512l: + cppver = "C++20"s; + break; + } + } + std::string datmod = fmt::format("{}/{}/{}/{}/{}",sizeof(short),sizeof(int),sizeof(long),sizeof(long long),sizeof(void *)); + if constexpr((sizeof(short) == 0x2) && (sizeof(int) == 0x4) && (sizeof(long) == 0x4) && (sizeof(long long) == 0x8) && (sizeof(void *) == 0x8)) { + datmod = fmt::format("LLP64 (noob / {})"s,datmod); + } + else if constexpr((sizeof(short) == 0x2) && (sizeof(int) == 0x4) && (sizeof(long) == 0x8) && (sizeof(long long) == 0x8) && (sizeof(void *) == 0x8)) { + datmod = fmt::format("LP64 ({})"s,datmod); + } + else if constexpr((sizeof(short) == 0x2) && (sizeof(int) == 0x8) && (sizeof(long) == 0x8) && (sizeof(long long) == 0x8) && (sizeof(void *) == 0x8)) { + datmod = fmt::format("ILP64 ({})"s,datmod); + } + else if constexpr((sizeof(short) == 0x8) && (sizeof(int) == 0x8) && (sizeof(long) == 0x8) && (sizeof(long long) == 0x8) && (sizeof(void *) == 0x8)) { + datmod = fmt::format("SILP64 (wtf? / {})"s,datmod); + } + else { + datmod = fmt::format("{} AKA how the fuck did this get compiled?"s,datmod); + } + benoit::print(""s); + benoit::print(fmt::format("benoit {}",benoit::dat.ver)); + benoit::print("Copyright 2021 Gabriel Jensen"s); + benoit::print(""s); + benoit::print("Arguments:"s); + benoit::print("\u0009debug={false,true}:"s); + benoit::print("\u0009\u0009Sets whether or not to display debug information."s); + benoit::print("\u0009\u0009Some debugging information (for example assertions) will still require building a debug build."s); + benoit::print("\u0009help, --help:"s); + benoit::print("\u0009\u0009Displays this information screen."s); + benoit::print("\u0009threads={0..65535}:"s); + benoit::print("\u0009\u0009Sets the number of threads that will be used."s); + benoit::print(""s); + benoit::print("Compilation Information:"s); + benoit::print(fmt::format("\u0009Architecture: {}"s,benoit::archstr(benoit::dat.arch))); + benoit::print(fmt::format("\u0009Compilation Date: {}"s,cmdate)); + benoit::print(fmt::format("\u0009Compiler C++ Standard: {}"s,cppver)); + benoit::print(fmt::format("\u0009Data Model: {}"s,datmod)); + benoit::print(fmt::format("\u0009System Kernel: {}"s,benoit::kernelstr(benoit::dat.kernel))); + benoit::print(""s); + benoit::exit(EXIT_SUCCESS); +} diff --git a/src/benoit/kernelstr.cc b/src/benoit/kernelstr.cc new file mode 100644 index 0000000..9239ac4 --- /dev/null +++ b/src/benoit/kernelstr.cc @@ -0,0 +1,47 @@ +# include <benoit/kernelstr.hh> +# include <benoit/t/kernel.hh> +# include <string> +# include <unordered_map> +using namespace std::literals::string_literals; +std::string benoit::kernelstr(benoit::t::kernel kernel) noexcept { + std::unordered_map<benoit::t::kernel,std::string> map = { + { + benoit::t::kernel::darwinos, + "Darwin OS"s, + }, + { + benoit::t::kernel::dragonflybsd, + "DragonFly BSD"s, + }, + { + benoit::t::kernel::freebsd, + "FreeBSD"s, + }, + { + benoit::t::kernel::hurd, + "Hurd"s, + }, + { + benoit::t::kernel::linux, + "Linux"s, + }, + { + benoit::t::kernel::minix, + "MINIX"s, + }, + { + benoit::t::kernel::netbsd, + "NetBSD"s, + }, + { + benoit::t::kernel::openbsd, + "OpenBSD"s, + }, + { + benoit::t::kernel::unknown, + "N/A"s, + } + }; + std::string str = map[kernel]; + return str; +} diff --git a/src/benoit/loadcfg.cc b/src/benoit/loadcfg.cc new file mode 100644 index 0000000..b491201 --- /dev/null +++ b/src/benoit/loadcfg.cc @@ -0,0 +1,12 @@ +# include <benoit/crtcfg.hh> +# include <benoit/dat.hh> +# include <benoit/loadcfg.hh> +# include <iostream> +# include <pugixml.hpp> +# include <unistd.h> +void benoit::loadcfg() { + if(::access(benoit::dat.cfg.c_str(),R_OK) < 0x0) { + benoit::crtcfg(); + return; + } +} diff --git a/src/benoit/log.cc b/src/benoit/log.cc new file mode 100644 index 0000000..92446ab --- /dev/null +++ b/src/benoit/log.cc @@ -0,0 +1,16 @@ +# include <benoit/dat.hh> +# include <benoit/log.hh> +# include <benoit/print.hh> +# include <fmt/core.h> +# include <string> +void benoit::log(std::string msg) { + if(benoit::dat.debug) { + benoit::print(msg,true); + } + else { + benoit::dat.thelog.insert(benoit::dat.thelog.begin(),msg); + } +} +void benoit::log(std::string const func,std::string msg) { + msg = fmt::format("[{}] {}"s,func,msg); +} diff --git a/src/benoit/logfunc.cc b/src/benoit/logfunc.cc new file mode 100644 index 0000000..54f4202 --- /dev/null +++ b/src/benoit/logfunc.cc @@ -0,0 +1,8 @@ +# include <benoit/log.hh> +# include <benoit/logfunc.hh> +# include <fmt/core.h> +# include <string> +using namespace std::literals::string_literals; +void benoit::logfunc(std::string const func) { + benoit::log(fmt::format("Function call: {}."s,func)); +} diff --git a/src/benoit/logfuncret.cc b/src/benoit/logfuncret.cc new file mode 100644 index 0000000..6e52ede --- /dev/null +++ b/src/benoit/logfuncret.cc @@ -0,0 +1,9 @@ +# include <benoit/log.hh> +# include <benoit/logfuncret.hh> +# include <fmt/core.h> +# include <string> +using namespace std::literals::string_literals; +void benoit::logfuncret(std::string const func) { + std::string const funcname = "benoit::logfuncret(std::string const)"s; + benoit::log(fmt::format("Function return: {}."s,func)); +} diff --git a/src/benoit/main.cc b/src/benoit/main.cc new file mode 100644 index 0000000..2717955 --- /dev/null +++ b/src/benoit/main.cc @@ -0,0 +1,145 @@ +static_assert(((sizeof(short) >= 0x2) && (sizeof(int) >= 0x4) && (sizeof(long) >= 0x4) && (sizeof(long long) >= 0x8) && (sizeof(void *) >= 0x8)),"Benoit expects at least an LLP64 data model."); +# include <benoit/arghandl.hh> +# include <benoit/log.hh> +# include <benoit/logfunc.hh> +# include <benoit/dat.hh> +# include <benoit/exit.hh> +# include <benoit/loadcfg.hh> +# include <benoit/log.hh> +# include <benoit/logfunc.hh> +# include <benoit/logfuncret.hh> +# include <benoit/main.hh> +# include <benoit/plotmandelbrot.hh> +# include <benoit/print.hh> +# include <cstddef> +# include <cstdint> +# include <cstdlib> +# include <fcntl.h> +# include <fmt/core.h> +# include <string> +# include <sys/types.h> +# include <thread> +# include <unistd.h> +# include <vector> +# include <webp/encode.h> +# include <webp/types.h> +using namespace std::literals::string_literals; +[[noreturn]] void benoit::main(int const argc,char const * * argv) noexcept { + std::string const funcname = "benoit::main(int const,char const * *)"s; + benoit::log(fmt::format("{}"s,argv[0x0])); + benoit::logfunc(funcname); + benoit::dat.numthrds = std::thread::hardware_concurrency(); + benoit::log(fmt::format("{} availabe threads have been detected.",benoit::dat.numthrds)); + benoit::arghandl(argc,argv); + benoit::loadcfg(); + std::vector<std::uint8_t> buf = benoit::plotmandelbrot(); + benoit::exit(EXIT_SUCCESS); + std::string msg = ("P3 "s + std::to_string(benoit::dat.resx) + " "s +std::to_string(benoit::dat.resy) + " 255 "s); + for(auto val : buf) { + benoit::print(fmt::format("Found value {} in image."s,val)); + msg.append(std::to_string(val)); + msg.append(" "); + } + int file = ::open(benoit::dat.outimg.c_str(),O_WRONLY); + //for(::ssize_t bytesleft = msg.size();(bytesleft > 0x0);) { + if(::write(file,msg.c_str(),msg.size()) < 0x0) { + benoit::print(fmt::format("Unable to write to “{}”."s,benoit::dat.outimg)); + } + //} + if(::close(file) < 0x0) { + benoit::print(fmt::format("Unable to close file “{}”."s,benoit::dat.outimg)); + } + benoit::exit(EXIT_SUCCESS); +} + /* + auto webpconf = ::WebPConfig(); + webpconf.lossless = 0x1; + webpconf.quality = 0x64; + webpconf.method = 0x6; + webpconf.segments = 0x4; + webpconf.sns_strength = 0x4; + webpconf.filter_strength = 0x0; + webpconf.filter_sharpness = 0x0; + webpconf.autofilter = 0x0; + webpconf.alpha_compression = 0x1; + webpconf.alpha_filtering = 0x2; + webpconf.alpha_quality = 0x64; + webpconf.pass = 0xA; + webpconf.preprocessing = 0x1; + webpconf.partitions = 0x3; + webpconf.partitions = 0x0; + webpconf.use_sharp_yuv = 0x0; + auto conferr = ::WebPValidateConfig(&webpconf); + auto webpimg = ::WebPPicture(); + WebPPictureInit(&webpimg); + webpimg.use_argb = 0x1; + webpimg.resx = 0x1; + webpimg.resy = 0x1; + WebPPictureAlloc(&webpimg); + auto webpmemwrit = WebPMemoryWriter(); + WebPMemoryWriterInit(&webpmemwrit); + webpimg.writer = WebPMemoryWrite; + webpimg.custom_ptr = &webpmemwrit; + webpimg.argb = {0xFF,0xFF,0x0,0x0}; + if(WebPEncode(&webpconf,&webpimg) == 0x0) { + } + else { + + } + WebPPictureFree(&webpimg); + auto buf = std::vector<std::uint8_t>(); + buf.push_back(0xFF); + buf.push_back(0x0); + buf.push_back(0x0); + auto file = std::fstream(benoit::outimt,std::fstream::binary | std::fstream::out | std::fstream::trunc); + if(!file.is_open()) { + ::_exit(EXIT_FAILURE); + } + const double maxR = 2.25; + const double minR = -2.25; + const double maxI = 2.25; + const double minI = -2.25; + + for(unsigned int y = 0; y < benoit::dat.resy; ++y) + for(unsigned int x = 0; x < benoit::dat.resx; ++x) { + long double r = x * ((maxR + benoit::real * benoit::dat.zoom) / benoit::dat.zoom - (minR + benoit::real * benoit::dat.zoom) / benoit::dat.zoom) / benoit::dat.resx + (minR + benoit::real * benoit::dat.zoom) / benoit::dat.zoom; + long double i = y * ((maxI + benoit::imag * benoit::dat.zoom) / benoit::dat.zoom - (minI + benoit::imag * benoit::dat.zoom) / benoit::dat.zoom) / benoit::dat.resx + (minI + benoit::imag * benoit::dat.zoom) / benoit::dat.zoom; + long double r2 = 0.0; + long double i2 = 0.0; + + unsigned int iter; + for(iter = 0; iter < benoit::maxiter; ++iter) { + if(r2 * r2 + i2 * i2 >= 4.0) + break; + + long double r2Temp = r2 * r2 - i2 * i2 + r; + long double i2Temp = 2.0 * r2 * i2 + i; + + if(r2 == r2Temp && i2 == i2Temp) { + iter = benoit::maxiter; + break; + } + + r2 = r2Temp; + i2 = i2Temp; + } + + std::uint8_t blue = 0x0; + std::uint8_t green = 0x0; + std::uint8_t red = 0x0; + + if(iter != benoit::maxiter) { + float tmp = ((float)(iter) / (float)(benoit::maxiter)); + blue = (std::uint8_t)((1.0 - tmp) * 255.0); + green = (std::uint8_t)((1.0 - tmp) * 255.0); + red = (std::uint8_t)((1.0 - tmp) * 255.0); + //double t = (double)(iter) / (double)(benoit::maxiter); + //red = (int)(8.0 * (1.0 - t) * t * t * t * 255.0); + //green = (int)(16.0 * (1.0 - t) * (1.0 - t) * t * t * 255.0); + //blue = (int)(8.0 * (1.0 - t) * (1.0 - t) * (1.0 - t) * t * 255.0); + } + buf.push_back(blue); + buf.push_back(green); + buf.push_back(red); + } +*/ diff --git a/src/benoit/notiffunc.cc b/src/benoit/notiffunc.cc deleted file mode 100644 index bb2152c..0000000 --- a/src/benoit/notiffunc.cc +++ /dev/null @@ -1,9 +0,0 @@ -# include <benoit.hh> -# include <fmt/core.h> -# include <string> -void benoit::notiffunc(std::string const callfuncname) { - std::string const funcname = "benoit::notiffunc(std:.string constexpr)"s; - if constexpr(benoit::debug) { - this->print(fmt::format("Function \"{}\" has been called."s,callfuncname)); - } -} diff --git a/src/benoit/plotmandelbrot.cc b/src/benoit/plotmandelbrot.cc index 2a594cd..36ea5b8 100644 --- a/src/benoit/plotmandelbrot.cc +++ b/src/benoit/plotmandelbrot.cc @@ -1,31 +1,52 @@ -# include <benoit.hh> +# include <benoit/dat.hh> +# include <benoit/log.hh> +# include <benoit/logfunc.hh> +# include <benoit/logfuncret.hh> +# include <benoit/plotmandelbrot.hh> +# include <benoit/print.hh> +# include <benoit/t/thrddat.hh> # include <boost/multiprecision/mpfr.hpp> # include <cstdint> # include <fmt/core.h> -# include <iostream> # include <pthread.h> # include <string> # include <vector> using namespace std::literals::string_literals; namespace { - void * plotarea(void * rowsptr) { - unsigned short rows = *(unsigned short *)(rowsptr); - std::cout << fmt::format("Hello there from thread!\u000AI will calculate {} rows.\u000A"s,rows); - return nullptr; + void * plotarea(void * thrddatptr) { + benoit::t::thrddat thrddat = *reinterpret_cast<benoit::t::thrddat *>(thrddatptr); + thrddat.img = new std::vector<std::uint8_t>; + pthread_exit(nullptr); } } std::vector<std::uint8_t> benoit::plotmandelbrot() { std::string const funcname = "benoit::plotmandelbrot()"s; - this->notiffunc(funcname); + benoit::logfunc(funcname); std::vector<std::uint8_t> img; - std::vector<::pthread_t> threads; - for(unsigned short thread = 0x0;(thread < this->numthreads);++thread) { - threads.emplace_back(); - unsigned short rowspthread = (this->resy / this->numthreads); - ::pthread_create(&threads[thread],nullptr,::plotarea,&rowspthread); + unsigned short remainrows = benoit::dat.resy; + std::vector<benoit::t::thrddat> thrds; + for(unsigned thrdid = 0x0;(thrdid < benoit::dat.numthrds);++thrdid) { + thrds.emplace_back(); + thrds[thrdid].id = new unsigned(thrdid); + if((thrdid + 0x1) == benoit::dat.numthrds) { + thrds[thrdid].rows = new unsigned(remainrows); + } + else { + thrds[thrdid].rows = new unsigned(benoit::dat.resy / benoit::dat.numthrds); + } + remainrows -= *thrds[thrdid].rows; + thrds[thrdid].thrd = new pthread_t; + ::pthread_create(thrds[thrdid].thrd,nullptr,::plotarea,&thrds[thrdid]); + benoit::log(fmt::format("Thread #{} has been created."s,thrdid)); } - for(auto thread : threads) { - ::pthread_join(thread,nullptr); + for(auto thrd : thrds) { + ::pthread_join(*thrd.thrd,nullptr); + benoit::log(fmt::format("Thread #{} has joined."s,*thrd.id)); + delete thrd.id; + delete thrd.img; + delete thrd.rows; + delete thrd.thrd; } + benoit::logfuncret(funcname); return img; } diff --git a/src/benoit/print.cc b/src/benoit/print.cc index 7f7901c..ab9547c 100644 --- a/src/benoit/print.cc +++ b/src/benoit/print.cc @@ -1,14 +1,18 @@ -# include <benoit.hh> +# include <benoit/dat.hh> +# include <benoit/print.hh> # include <fcntl.h> # include <stdexcept> # include <string> # include <unistd.h> void benoit::print(char const * msg,bool stderr) { std::string const funcname = "benoit::print(char const *)"s; - this->print(std::string(msg),stderr); + benoit::print(std::string(msg),stderr); } void benoit::print(std::string msg,bool stderr) { std::string const funcname = "benoit::print(std::string)"s; + if(benoit::dat.printdolog) { + benoit::dat.thelog.insert(benoit::dat.thelog.begin(),msg); + } int file = 0x0; if(stderr) { ::open("/dev/stderr",O_WRONLY); diff --git a/src/benoit/t/pos/pos.cc b/src/benoit/t/pos/pos.cc index 43941b2..f1237e0 100644 --- a/src/benoit/t/pos/pos.cc +++ b/src/benoit/t/pos/pos.cc @@ -1,4 +1,4 @@ -# include <benoit.hh> +# include <benoit/t/pos.hh> # include <boost/multiprecision/mpfr.hpp> benoit::t::pos::pos(boost::multiprecision::mpfr_float x,boost::multiprecision::mpfr_float y) { this->x = x; diff --git a/src/benoit/~benoit.cc b/src/benoit/~benoit.cc deleted file mode 100644 index b16b42d..0000000 --- a/src/benoit/~benoit.cc +++ /dev/null @@ -1,10 +0,0 @@ -# include <benoit.hh> -# include <cstdlib> -# include <string> -# include <unistd.h> -using namespace std::literals::string_literals; -[[noreturn]] benoit::~benoit() noexcept { - std::string const funcname = "benoit::~benoit()"s; - this->notiffunc(funcname); - this->exit(EXIT_SUCCESS); -} diff --git a/src/main.cc b/src/main.cc index 6c5aaa8..a6341a6 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,4 +1,4 @@ -# include <benoit.hh> +# include <benoit/main.hh> int main(int const argc,char const * * argv) { - ::benoit benoit(argc,argv); + benoit::main(argc,argv); } |