├── .github ├── FUNDING.yml └── workflows │ └── test.yml ├── logo ├── dlib-logo.png └── dlib-logo-doxygen.png ├── .gitignore ├── .editorconfig ├── Doxyfile ├── CODE_OF_CONDUCT.md ├── dub.json ├── COPYING.md ├── dlib ├── random │ ├── package.d │ └── random.d ├── text │ ├── common.d │ ├── package.d │ ├── utils.d │ └── encodings.d ├── coding │ ├── package.d │ └── varint.d ├── serialization │ └── package.d ├── image │ ├── render │ │ ├── package.d │ │ └── cosplasma.d │ ├── resampling │ │ ├── package.d │ │ ├── nearest.d │ │ ├── bicubic.d │ │ ├── lanczos.d │ │ └── bilinear.d │ ├── package.d │ ├── filters │ │ ├── package.d │ │ ├── sharpen.d │ │ ├── boxblur.d │ │ ├── desaturate.d │ │ ├── histogram.d │ │ ├── contrast.d │ │ ├── lens.d │ │ ├── median.d │ │ ├── binarization.d │ │ └── normalmap.d │ ├── io │ │ └── utils.d │ └── fthread.d ├── concurrency │ ├── package.d │ ├── workerthread.d │ └── taskqueue.d ├── network │ ├── package.d │ └── errno.d ├── filesystem │ ├── dirrange.d │ ├── package.d │ ├── posix │ │ ├── common.d │ │ ├── directory.d │ │ └── file.d │ ├── windows │ │ ├── common.d │ │ └── directory.d │ └── delegaterange.d ├── math │ ├── interpolation │ │ ├── linear.d │ │ ├── nearest.d │ │ ├── package.d │ │ ├── catmullrom.d │ │ ├── hermite.d │ │ ├── smoothstep.d │ │ └── easing.d │ ├── diff.d │ ├── package.d │ ├── decomposition.d │ ├── fft.d │ ├── linsolve.d │ └── hof.d ├── core │ ├── package.d │ ├── compound.d │ ├── bitio.d │ └── tuple.d ├── audio │ ├── package.d │ ├── io │ │ └── package.d │ ├── unmanaged.d │ └── sample.d ├── geometry │ ├── package.d │ ├── sphere.d │ └── obb.d ├── memory │ ├── package.d │ ├── gcallocator.d │ └── allocator.d └── container │ ├── package.d │ ├── mappedlist.d │ ├── spscqueue.d │ ├── queue.d │ └── stack.d └── AUTHORS.md /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | patreon: gecko0307 2 | liberapay: gecko0307 3 | -------------------------------------------------------------------------------- /logo/dlib-logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gecko0307/dlib/HEAD/logo/dlib-logo.png -------------------------------------------------------------------------------- /logo/dlib-logo-doxygen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gecko0307/dlib/HEAD/logo/dlib-logo-doxygen.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.o 3 | *.obj 4 | 5 | # Compiled Static libraries 6 | *.a 7 | *.lib 8 | 9 | # Executables 10 | *.exe 11 | 12 | # Dub files 13 | .dub 14 | dub.selections.json 15 | 16 | # Dub test files 17 | __test__library__ 18 | tests/ 19 | 20 | # Documentation 21 | docs/ 22 | docs-doxygen/ 23 | docs.json 24 | __dummy.html 25 | documentation.chm 26 | 27 | # Coverage files 28 | *.lst 29 | 30 | # Debug info 31 | *.pdb 32 | 33 | # Backup files 34 | *~ 35 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*.{c,h,d,di,dd,sh}] 4 | end_of_line = crlf 5 | insert_final_newline = true 6 | indent_style = space 7 | indent_size = 4 8 | trim_trailing_whitespace = true 9 | charset = utf-8 10 | dfmt_brace_style = allman 11 | dfmt_soft_max_line_length = 120 12 | dfmt_align_switch_statements = true 13 | dfmt_outdent_attributes = true 14 | dfmt_outdent_labels = true 15 | dfmt_split_operator_at_line_end = true 16 | dfmt_space_after_cast = false 17 | dfmt_space_after_keywords = true 18 | dfmt_space_before_function_parameters = false 19 | dfmt_selective_import_space = false 20 | dfmt_keep_line_breaks = true 21 | -------------------------------------------------------------------------------- /Doxyfile: -------------------------------------------------------------------------------- 1 | DOXYFILE_ENCODING = UTF-8 2 | PROJECT_NAME = "dlib" 3 | PROJECT_NUMBER = 1.1 4 | PROJECT_BRIEF = "Allocators, I/O streams, math, geometry, image and audio processing for D" 5 | PROJECT_LOGO = "logo/dlib-logo-doxygen.png" 6 | OUTPUT_DIRECTORY = "docs-doxygen" 7 | CREATE_SUBDIRS = YES 8 | CREATE_SUBDIRS_LEVEL = 8 9 | ALLOW_UNICODE_NAMES = NO 10 | OUTPUT_LANGUAGE = English 11 | GENERATE_HTML = YES 12 | HTML_OUTPUT = html 13 | HTML_FILE_EXTENSION = .html 14 | GENERATE_HTMLHELP = YES 15 | CHM_FILE = "documentation.chm" 16 | INPUT = "dlib" 17 | RECURSIVE = YES 18 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | paths: 7 | - 'dlib/**' 8 | - 'dub.json' 9 | - '.github/workflows/**' 10 | pull_request: 11 | branches: [ master ] 12 | paths: 13 | - 'dlib/**' 14 | - 'dub.json' 15 | - '.github/workflows/**' 16 | 17 | jobs: 18 | test: 19 | name: Dub Tests 20 | strategy: 21 | matrix: 22 | os: [ubuntu-latest, windows-latest] 23 | dc: [dmd-latest, ldc-latest] 24 | runs-on: ${{ matrix.os }} 25 | steps: 26 | - uses: actions/checkout@v2 27 | - uses: dlang-community/setup-dlang@v1 28 | with: 29 | compiler: ${{ matrix.dc }} 30 | - name: Run tests 31 | run: dub test --build=unittest-cov 32 | - name: Run code coverage 33 | if: success() 34 | run: | 35 | curl https://codecov.io/bash > codecov.sh 36 | bash codecov.sh 37 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Code of Conduct 2 | 3 | This project welcomes contributions in any form and encourages open discussion and exchange of views on its features, architecture, and implementation details. To maintain a proper culture of communication, this Code of Conduct has been formulated. It applies to the project's issue tracker, chat, and possibly other communication channels. 4 | 5 | 1. We do not tolerate obscene language, insulting, rude and/or disparaging messages, the use of sexualized, violent and otherwise offensive speech and imagery. 6 | 2. Any criticism should be constructive and reasonable. Mere personal discontent without any objective reasoning is not enough to make critical statements and influence the development of dlib. 7 | 3. This project stays away from non-technological issues and topics. We welcome everyone, regardless of age, gender identity, religion, ethnicity, citizenship, or cultural background, and our community do not conduct any specific ideology. Sites, repositories, communication channels and other resources associated with dlib should not be used as a platform for propaganda. 8 | -------------------------------------------------------------------------------- /dub.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dlib", 3 | "description": "D language utility library", 4 | "homepage": "http://github.com/gecko0307/dlib", 5 | "license": "BSL-1.0", 6 | "authors": [ 7 | "Timur Gafarov", 8 | "Martin Cejp", 9 | "Andrey Penechko", 10 | "Vadim Lopatin", 11 | "Nick Papanastasiou", 12 | "Oleg Baharev", 13 | "Roman Chistokhodov", 14 | "Eugene Wissner", 15 | "Roman Vlasov", 16 | "Basile Burg", 17 | "Valeriy Fedotov", 18 | "Ferhat Kurtulmuş" 19 | ], 20 | "importPaths": [ 21 | "." 22 | ], 23 | "buildRequirements":[ 24 | "allowWarnings" 25 | ], 26 | "lflags-linux-gdc": ["-lz"], 27 | "configurations": [ 28 | { 29 | "name": "library", 30 | "targetType": "library", 31 | "sourcePaths": ["dlib"] 32 | }, 33 | { 34 | "name": "import", 35 | "targetType": "sourceLibrary", 36 | "sourceFiles-posix": ["libdlib.a"], 37 | "sourceFiles-windows": ["dlib.lib"] 38 | } 39 | ] 40 | } 41 | -------------------------------------------------------------------------------- /COPYING.md: -------------------------------------------------------------------------------- 1 | Boost Software License - Version 1.0 - August 17th, 2003 2 | 3 | Permission is hereby granted, free of charge, to any person or organization 4 | obtaining a copy of the software and accompanying documentation covered by 5 | this license (the "Software") to use, reproduce, display, distribute, 6 | execute, and transmit the Software, and to prepare derivative works of the 7 | Software, and to permit third-parties to whom the Software is furnished to 8 | do so, all subject to the following: 9 | 10 | The copyright notices in the Software and this entire statement, including 11 | the above license grant, this restriction and the following disclaimer, 12 | must be included in all copies of the Software, in whole or in part, and 13 | all derivative works of the Software, unless such copies or derivative 14 | works are solely in the form of machine-executable object code generated by 15 | a source language processor. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 20 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 21 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 22 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 23 | DEALINGS IN THE SOFTWARE. 24 | -------------------------------------------------------------------------------- /dlib/random/package.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | module dlib.random; 30 | 31 | public 32 | { 33 | import dlib.random.random; 34 | } 35 | -------------------------------------------------------------------------------- /dlib/text/common.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2018-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | module dlib.text.common; 30 | 31 | /// Denotes an end of data 32 | enum DECODE_END = -1; 33 | 34 | /// Denotes an error while decoding 35 | enum DECODE_ERROR = -2; 36 | -------------------------------------------------------------------------------- /AUTHORS.md: -------------------------------------------------------------------------------- 1 | # dlib authors and contributors 2 | * Core library - [Timur Gafarov aka gecko0307](https://github.com/gecko0307) 3 | * Filesystem package, stream module - [Martin Cejp aka minexew](https://github.com/minexew) 4 | * Memory package, socket module - [Eugene Wissner aka belka-ew](https://github.com/belka-ew) 5 | * Image binarization, histogram generation, median filter - [LightHouse Software](http://lhs-blog.info/) / [Oleg Baharev aka aquaratixc](https://github.com/aquaratixc) 6 | * TGA and BMP encoder, improved BMP decoder, bugfixes, unittests - [Roman Chistokhodov aka FreeSlave](https://github.com/FreeSlave) 7 | * PNG decoder improvements - [Vadim Lopatin](https://github.com/buggins) 8 | * Combinatorics module - Nick Papanastasiou 9 | * Rectangle drawing - [Aaron Nédélec aka ReactiveAlkali](https://github.com/ReactiveAlkali) 10 | * Vector swizzling assignment - [João Lourenço aka iK4tsu](https://github.com/iK4tsu) 11 | * SSE vector math port for GDC - [Alexander Perfilyev](https://github.com/aperfilev) 12 | * Bugfixes - [Andrey Penechko aka MrSmith33](https://github.com/MrSmith33), [Valeriy Fedotov](https://github.com/Valera), [Basile Burg aka SixthDot](https://github.com/SixthDot), [Ate Eskola aka dukc](https://github.com/dukc), [Martin Nowak aka dawg](https://github.com/MartinNowak), [Mathias Lang aka Geod24](https://github.com/Geod24), [Nick Treleaven aka ntrel](https://github.com/ntrel), [Nikolay Krasheninnikov aka GoodNike](https://github.com/GoodNike), [ijet](https://github.com/my-ijet), [TETYYS](https://github.com/TETYYS), [Razvan Nitu aka RazvanN7](https://github.com/RazvanN7), [Denis Feklushkin aka denizzzka](https://github.com/denizzzka) 13 | * Additional unittests - [Roman Vlasov](https://github.com/VlasovRoman) 14 | -------------------------------------------------------------------------------- /dlib/coding/package.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2015-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Data coding and compression algorithms 31 | * 32 | * Copyright: Timur Gafarov 2015-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.coding; 37 | 38 | public 39 | { 40 | import dlib.coding.zlib; 41 | import dlib.coding.varint; 42 | } 43 | -------------------------------------------------------------------------------- /dlib/serialization/package.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2017-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Data serialization 31 | * 32 | * Copyright: Timur Gafarov 2017-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.serialization; 37 | 38 | public 39 | { 40 | import dlib.serialization.json; 41 | import dlib.serialization.xml; 42 | } 43 | -------------------------------------------------------------------------------- /dlib/image/render/package.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2022-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Image synthesis tools 31 | * 32 | * Copyright: Timur Gafarov 2022-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.image.render; 37 | 38 | public 39 | { 40 | import dlib.image.render.cosplasma; 41 | import dlib.image.render.shapes; 42 | import dlib.image.render.text; 43 | } 44 | -------------------------------------------------------------------------------- /dlib/concurrency/package.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2019-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * A thread pool for running tasks in parallel 31 | * 32 | * Copyright: Timur Gafarov 2019-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.concurrency; 37 | 38 | public 39 | { 40 | import dlib.concurrency.threadpool; 41 | import dlib.concurrency.workerthread; 42 | import dlib.concurrency.taskqueue; 43 | } -------------------------------------------------------------------------------- /dlib/network/package.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2016-2025 Eugene Wissner, Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Networking and web functionality 31 | * 32 | * Copyright: Eugene Wissner, Timur Gafarov 2016-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Eugene Wissner, Timur Gafarov 35 | */ 36 | module dlib.network; 37 | 38 | public 39 | { 40 | import dlib.network.socket; 41 | import dlib.network.url; 42 | } 43 | -------------------------------------------------------------------------------- /dlib/image/resampling/package.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2022-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Image resizing algorithms 31 | * 32 | * Copyright: Timur Gafarov 2022-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.image.resampling; 37 | 38 | public 39 | { 40 | import dlib.image.resampling.nearest; 41 | import dlib.image.resampling.bilinear; 42 | import dlib.image.resampling.bicubic; 43 | import dlib.image.resampling.lanczos; 44 | } 45 | -------------------------------------------------------------------------------- /dlib/filesystem/dirrange.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2014-2025 Martin Cejp, Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Copyright: Martin Cejp, Timur Gafarov 2014-2025. 31 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 32 | * Authors: Martin Cejp, Timur Gafarov 33 | */ 34 | module dlib.filesystem.dirrange; 35 | 36 | import dlib.filesystem.delegaterange; 37 | import dlib.filesystem.filesystem; 38 | 39 | /// A DelegateInputRange of DirEntry values 40 | alias DirRange = DelegateInputRange!DirEntry; 41 | -------------------------------------------------------------------------------- /dlib/text/package.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2015-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Text processing 31 | * 32 | * Copyright: Timur Gafarov 2015-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.text; 37 | 38 | public 39 | { 40 | import dlib.text.str; 41 | import dlib.text.utils; 42 | import dlib.text.utf8; 43 | import dlib.text.utf16; 44 | import dlib.text.encodings; 45 | import dlib.text.lexer; 46 | } 47 | -------------------------------------------------------------------------------- /dlib/filesystem/package.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2014-2025 Martin Cejp, Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Abstract FS interface and its implementations for Windows and POSIX filesystems 31 | * 32 | * Copyright: Martin Cejp, Timur Gafarov 2014-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Martin Cejp, Timur Gafarov 35 | */ 36 | module dlib.filesystem; 37 | 38 | public 39 | { 40 | import dlib.filesystem.filesystem; 41 | import dlib.filesystem.local; 42 | import dlib.filesystem.stdfs; 43 | } 44 | -------------------------------------------------------------------------------- /dlib/math/interpolation/linear.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Linear interpolation 31 | * 32 | * Copyright: Timur Gafarov 2013-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.math.interpolation.linear; 37 | 38 | import std.math; 39 | 40 | /// Linear interpolation 41 | T interpLinear(T) (T a, T b, float t) 42 | { 43 | return a + (b - a) * t; 44 | } 45 | 46 | /// ditto 47 | alias lerp = interpLinear; 48 | 49 | /// 50 | unittest 51 | { 52 | assert(lerp(0.0f, 2.0f, 0.5f) == 1.0f); 53 | } 54 | -------------------------------------------------------------------------------- /dlib/math/interpolation/nearest.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Nearest-neighbour interpolation 31 | * 32 | * Copyright: Timur Gafarov 2011-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.math.interpolation.nearest; 37 | 38 | import std.math; 39 | 40 | /// Nearest-neighbour interpolation 41 | T interpNearest(T) (T x, T y, float t) 42 | { 43 | if (t < 0.5f) 44 | return x; 45 | else 46 | return y; 47 | } 48 | 49 | /// 50 | unittest 51 | { 52 | assert(interpNearest(0.0f, 2.0f, 0.3f) == 0.0f); 53 | } 54 | -------------------------------------------------------------------------------- /dlib/math/interpolation/package.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2020-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Interpolation functions 31 | * 32 | * Copyright: Timur Gafarov 2020-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.math.interpolation; 37 | 38 | public 39 | { 40 | import dlib.math.interpolation.bezier; 41 | import dlib.math.interpolation.catmullrom; 42 | import dlib.math.interpolation.easing; 43 | import dlib.math.interpolation.hermite; 44 | import dlib.math.interpolation.linear; 45 | import dlib.math.interpolation.nearest; 46 | import dlib.math.interpolation.smoothstep; 47 | } 48 | -------------------------------------------------------------------------------- /dlib/core/package.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2013-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Provides functionality that is used by all other dlib packages 31 | * 32 | * Copyright: Timur Gafarov 2013-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.core; 37 | 38 | public 39 | { 40 | import dlib.core.bitio; 41 | import dlib.core.compound; 42 | import dlib.core.memory; 43 | import dlib.core.mutex; 44 | import dlib.core.oop; 45 | import dlib.core.ownership; 46 | import dlib.core.stream; 47 | import dlib.core.tuple; 48 | import dlib.core.thread; 49 | } 50 | -------------------------------------------------------------------------------- /dlib/math/diff.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2013-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Automatic differentiation 31 | * 32 | * Copyright: Timur Gafarov 2013-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.math.diff; 37 | 38 | import dlib.math.dual; 39 | import dlib.core.compound; 40 | 41 | /** 42 | * Differentiate a function of a single argument 43 | * Examples: 44 | * --- 45 | * auto r = diff!f(x); 46 | * r[0] // = f(x) 47 | * r[1] // = f'(x) 48 | * --- 49 | */ 50 | auto diff(alias F, T)(T x) 51 | { 52 | auto eval = F(Dual!(T)(x, 1.0)); 53 | return compound(eval.re, eval.du); 54 | } 55 | -------------------------------------------------------------------------------- /dlib/filesystem/posix/common.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2014-2025 Martin Cejp 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | module dlib.filesystem.posix.common; 30 | 31 | version(Posix) 32 | { 33 | public 34 | { 35 | import core.sys.posix.dirent; 36 | import core.sys.posix.fcntl; 37 | import core.sys.posix.sys.stat; 38 | import core.sys.posix.sys.types; 39 | import core.sys.posix.unistd; 40 | } 41 | 42 | // Rename stat to stat_ because of method name collision 43 | version(linux) 44 | { 45 | alias lseek = lseek64; 46 | alias open = open64; 47 | alias stat_ = stat64; 48 | } 49 | else 50 | { 51 | alias stat_ = stat; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /dlib/audio/package.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2016-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * This package provides basic tools for sound processing. It currently supports 31 | * bit depths 8 and 16 (signed and unsigned), as well as arbitrary sample rate and 32 | * number of channels. Design principles of dlib.audio are closely akin to dlib.image. 33 | * 34 | * Copyright: Timur Gafarov 2016-2025. 35 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 36 | * Authors: Timur Gafarov 37 | */ 38 | module dlib.audio; 39 | 40 | public 41 | { 42 | import dlib.audio.sample; 43 | import dlib.audio.sound; 44 | import dlib.audio.synth; 45 | import dlib.audio.unmanaged; 46 | import dlib.audio.io; 47 | import dlib.audio.io.wav; 48 | } 49 | -------------------------------------------------------------------------------- /dlib/geometry/package.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2013-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Computational geometry 31 | * 32 | * Copyright: Timur Gafarov 2013-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.geometry; 37 | 38 | public 39 | { 40 | import dlib.geometry.aabb; 41 | import dlib.geometry.frustum; 42 | import dlib.geometry.intersection; 43 | import dlib.geometry.mpr; 44 | import dlib.geometry.obb; 45 | import dlib.geometry.plane; 46 | import dlib.geometry.ray; 47 | import dlib.geometry.sphere; 48 | import dlib.geometry.support; 49 | import dlib.geometry.triangle; 50 | import dlib.geometry.trimesh; 51 | import dlib.geometry.utils; 52 | } 53 | -------------------------------------------------------------------------------- /dlib/image/package.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2013-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Image processing 31 | * 32 | * Copyright: Timur Gafarov 2013-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.image; 37 | 38 | public 39 | { 40 | import dlib.image.animation; 41 | import dlib.image.arithmetics; 42 | import dlib.image.canvas; 43 | import dlib.image.color; 44 | import dlib.image.fthread; 45 | import dlib.image.hdri; 46 | import dlib.image.hsv; 47 | import dlib.image.image; 48 | import dlib.image.signal2d; 49 | import dlib.image.unmanaged; 50 | import dlib.image.transform; 51 | import dlib.image.filters; 52 | import dlib.image.io; 53 | import dlib.image.render; 54 | import dlib.image.resampling; 55 | } 56 | -------------------------------------------------------------------------------- /dlib/image/filters/package.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2020-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Image filtering 31 | * 32 | * Copyright: Timur Gafarov 2020-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.image.filters; 37 | 38 | public 39 | { 40 | import dlib.image.filters.boxblur; 41 | import dlib.image.filters.chromakey; 42 | import dlib.image.filters.convolution; 43 | import dlib.image.filters.desaturate; 44 | import dlib.image.filters.edgedetect; 45 | import dlib.image.filters.lens; 46 | import dlib.image.filters.median; 47 | import dlib.image.filters.morphology; 48 | import dlib.image.filters.normalmap; 49 | import dlib.image.filters.sharpen; 50 | import dlib.image.filters.contrast; 51 | import dlib.image.filters.histogram; 52 | import dlib.image.filters.binarization; 53 | } 54 | -------------------------------------------------------------------------------- /dlib/memory/package.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2016-2025 Eugene Wissner 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Allocators and memory management functions 31 | * 32 | * Copyright: Eugene Wissner 2016-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Eugene Wissner 35 | */ 36 | module dlib.memory; 37 | 38 | public 39 | { 40 | import dlib.memory.allocator; 41 | import dlib.memory.gcallocator; 42 | import dlib.memory.mallocator; 43 | import dlib.memory.mmappool; 44 | import dlib.memory.arena; 45 | 46 | import std.experimental.allocator: make, dispose, shrinkArray, expandArray, makeArray, dispose; 47 | } 48 | 49 | Allocator allocator; 50 | 51 | /// Get default allocator (Mallocator) 52 | @property Allocator defaultAllocator() 53 | { 54 | if (allocator is null) 55 | { 56 | allocator = Mallocator.instance; 57 | } 58 | return allocator; 59 | } 60 | -------------------------------------------------------------------------------- /dlib/image/render/cosplasma.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Render cosine plasma pattern 31 | * 32 | * Copyright: Timur Gafarov 2011-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.image.render.cosplasma; 37 | 38 | private 39 | { 40 | import std.math; 41 | import dlib.image.image; 42 | import dlib.image.color; 43 | } 44 | 45 | /// Render cosine plasma pattern 46 | SuperImage renderCosPlasma(SuperImage img, float factor) 47 | in 48 | { 49 | assert (img.data.length); 50 | } 51 | do 52 | { 53 | foreach (y; 0..img.height) 54 | foreach (x; 0..img.width) 55 | { 56 | float value = 0.5f + 0.25f * cos(x * factor) + 0.25f * cos(y * factor); 57 | img[x, y] = Color4f(value, value, value); 58 | } 59 | 60 | return img; 61 | } 62 | -------------------------------------------------------------------------------- /dlib/filesystem/windows/common.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2014-2025 Martin Cejp 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | module dlib.filesystem.windows.common; 30 | 31 | public 32 | { 33 | version(Windows) 34 | { 35 | import core.stdc.wchar_; 36 | import core.sys.windows.windows; 37 | import core.sys.windows.accctrl; 38 | import core.sys.windows.aclapi; 39 | import std.utf; 40 | import std.windows.syserror; 41 | 42 | enum DWORD NO_ERROR = 0; 43 | enum DWORD INVALID_FILE_ATTRIBUTES = cast(DWORD)0xFFFFFFFF; 44 | 45 | static T wenforce(T)(T cond, string str = null) 46 | { 47 | import std.array; 48 | 49 | if (cond) 50 | return cond; 51 | 52 | string err = sysErrorString(GetLastError()); 53 | throw new Exception(!str.empty ? (str ~ ": " ~ err) : err); 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /dlib/geometry/sphere.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Copyright: Timur Gafarov 2011-2025. 31 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 32 | * Authors: Timur Gafarov 33 | */ 34 | module dlib.geometry.sphere; 35 | 36 | import dlib.math.vector; 37 | 38 | /// Sphere object 39 | struct Sphere 40 | { 41 | Vector3f center; 42 | float radius; 43 | 44 | this(Vector3f newCenter, float newRadius) 45 | { 46 | center = newCenter; 47 | radius = newRadius; 48 | } 49 | 50 | bool containsPoint(Vector3f pt) 51 | { 52 | float dist = (pt - center).lengthsqr; 53 | return dist < (radius * radius) ? true : false; 54 | } 55 | } 56 | 57 | /// 58 | unittest 59 | { 60 | Sphere sphere = Sphere(Vector3f(0, 0, 0), 1.0f); 61 | assert(sphere.containsPoint(Vector3f(0.1f, 0, 0))); 62 | } 63 | -------------------------------------------------------------------------------- /dlib/container/package.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2013-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Containers 31 | * 32 | * Description: 33 | * This package implements generic GC-free data containers, such as linked list, 34 | * dynamic array, dictionary, etc. They are based on dlib.core.memory allocators. 35 | * dlib.container is useful when writing applications with manual memory management. 36 | * 37 | * Copyright: Timur Gafarov 2013-2025. 38 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 39 | * Authors: Timur Gafarov 40 | */ 41 | module dlib.container; 42 | 43 | public 44 | { 45 | import dlib.container.array; 46 | import dlib.container.buffer; 47 | import dlib.container.bst; 48 | import dlib.container.dict; 49 | import dlib.container.linkedlist; 50 | import dlib.container.mappedlist; 51 | import dlib.container.queue; 52 | import dlib.container.stack; 53 | import dlib.container.spscqueue; 54 | } 55 | -------------------------------------------------------------------------------- /dlib/math/interpolation/catmullrom.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Catmull-Rom interpolation functions 31 | * 32 | * Copyright: Timur Gafarov 2011-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.math.interpolation.catmullrom; 37 | 38 | import std.math; 39 | 40 | /** 41 | * Catmull-Rom curve 42 | */ 43 | T interpCatmullRom(T) (T p0, T p1, T p2, T p3, float t) 44 | { 45 | return 0.5 * ((2 * p1) + 46 | (-p0 + p2) * t + 47 | (2 * p0 - 5 * p1 + 4 * p2 - p3) * t^^2 + 48 | (-p0 + 3 * p1 - 3 * p2 + p3) * t^^3); 49 | } 50 | 51 | /** 52 | * Catmull-Rom curve derivative 53 | */ 54 | T interpCatmullRomDerivative(T) (T p0, T p1, T p2, T p3, float t) 55 | { 56 | return 0.5 * ((2 * p1) + 57 | (-p0 + p2) + 58 | 2 * (2 * p0 - 5 * p1 + 4 * p2 - p3) * t + 59 | 3 * (-p0 + 3 * p1 - 3 * p2 + p3) * t^^2); 60 | } 61 | -------------------------------------------------------------------------------- /dlib/math/interpolation/hermite.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2013-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Hermite interpolation functions 31 | * 32 | * Copyright: Timur Gafarov 2013-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.math.interpolation.hermite; 37 | 38 | /** 39 | * Hermite curve 40 | */ 41 | T interpHermite(T) (T x, T tx, T y, T ty, float t) 42 | { 43 | float t2 = t * t; 44 | float t3 = t2 * t; 45 | float h1 = t3 * 2.0 - t2 * 3.0 + 1.0; 46 | float h2 = t2 * 3.0 - t3 * 2.0; 47 | float h3 = t3 - t2 * 2.0 + t; 48 | float h4 = t3 - t2; 49 | return x * h1 + tx * h3 + y * h2 + ty * h4; 50 | } 51 | 52 | /** 53 | * Hermite curve derivative 54 | */ 55 | T interpHermiteDerivative(T) (T x, T tx, T y, T ty, float t) 56 | { 57 | float t2 = t * t; 58 | float h1 = t2 * 6.0 - t * 6.0; 59 | float h2 = t * 6.0 - t2 * 6.0; 60 | float h3 = t2 * 3.0 - t * 4.0 + 1.0; 61 | float h4 = t2 * 3.0 - t * 2.0; 62 | return x * h1 + tx * h3 + y * h2 + ty * h4; 63 | } 64 | -------------------------------------------------------------------------------- /dlib/container/mappedlist.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2018-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | Permission is hereby granted, free of charge, to any person or organization 6 | obtaining a copy of the software and accompanying documentation covered by 7 | this license (the "Software") to use, reproduce, display, distribute, 8 | execute, and transmit the Software, and to prepare derivative works of the 9 | Software, and to permit third-parties to whom the Software is furnished to 10 | do so, all subject to the following: 11 | 12 | The copyright notices in the Software and this entire statement, including 13 | the above license grant, this restriction and the following disclaimer, 14 | must be included in all copies of the Software, in whole or in part, and 15 | all derivative works of the Software, unless such copies or derivative 16 | works are solely in the form of machine-executable object code generated by 17 | a source language processor. 18 | 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 22 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 23 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 24 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | /** 29 | * A list with string-mapped indices. 30 | * 31 | * Copyright: Timur Gafarov 2018-2025. 32 | * License: $(LINK2 https://boost.org/LICENSE_1_0.txt, Boost License 1.0). 33 | * Authors: Timur Gafarov 34 | */ 35 | module dlib.container.mappedlist; 36 | 37 | import dlib.core.ownership; 38 | import dlib.core.memory; 39 | import dlib.container.array; 40 | import dlib.container.dict; 41 | 42 | /** 43 | * A list with string-mapped indices. 44 | */ 45 | class MappedList(T): Owner 46 | { 47 | Array!T data; 48 | Dict!(size_t, string) indices; 49 | 50 | this(Owner owner) 51 | { 52 | super(owner); 53 | indices = New!(Dict!(size_t, string))(); 54 | } 55 | 56 | void set(string name, T val) 57 | { 58 | data.append(val); 59 | indices[name] = data.length - 1; 60 | } 61 | 62 | T get(string name) 63 | { 64 | return data[indices[name]]; 65 | } 66 | 67 | ~this() 68 | { 69 | data.free(); 70 | Delete(indices); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /dlib/math/interpolation/smoothstep.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2018-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Sigmoid-like functions for clamping and non-linear interpolation of values in [0, 1] range. 31 | * 32 | * Copyright: Timur Gafarov 2018-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.math.interpolation.smoothstep; 37 | 38 | import std.math; 39 | import dlib.math.utils; 40 | 41 | /** 42 | * Hermite polynomial, analogous to GLSL smoothstep. 43 | * e0 and e1 define lower and upper edges of Hermite function. 44 | */ 45 | T hermiteSmoothstep(T)(T x, float e0, float e1) 46 | { 47 | T t = clamp((x - e0) / (e1 - e0), 0.0, 1.0); 48 | return t * t * (3.0 - 2.0 * t); 49 | } 50 | 51 | /** 52 | * Rational sigmoid that becomes linear at k=0 and discrete at k=1. 53 | * Allows varying between linear and nearest-neighbour interpolation. 54 | */ 55 | T rationalSmoothstep(T)(T x, float k) 56 | { 57 | T s = (x + x * k - k * 0.5 - 0.5) / (abs(x * k * 4.0 - k * 2.0) - k + 1.0) + 0.5; 58 | return clamp(s, 0.0, 1.0); 59 | } 60 | -------------------------------------------------------------------------------- /dlib/image/filters/sharpen.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Image sharpening 31 | * 32 | * Copyright: Timur Gafarov 2011-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.image.filters.sharpen; 37 | 38 | import dlib.image.image; 39 | import dlib.image.color; 40 | import dlib.image.arithmetics; 41 | import dlib.image.filters.contrast; 42 | import dlib.image.filters.boxblur; 43 | 44 | /// Sharpen an image 45 | SuperImage sharpen(SuperImage src, SuperImage outp, int radius, float amount) 46 | { 47 | if (outp is null) 48 | outp = src.dup; 49 | 50 | auto blurred = boxBlur(src, outp, radius); 51 | auto mask = subtract(src, blurred, outp, 1.0f); 52 | auto highcon = contrast(mask, outp, amount, ContrastMethod.AverageImage); 53 | return add(src, highcon, outp, 0.25f); 54 | } 55 | 56 | /// ditto 57 | SuperImage sharpen(SuperImage src, int radius, float amount) 58 | { 59 | return sharpen(src, null, radius, amount); 60 | } 61 | -------------------------------------------------------------------------------- /dlib/geometry/obb.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2013-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Copyright: Timur Gafarov 2013-2025. 31 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 32 | * Authors: Timur Gafarov 33 | */ 34 | module dlib.geometry.obb; 35 | 36 | import dlib.math.vector; 37 | import dlib.math.matrix; 38 | import dlib.math.transformation; 39 | 40 | /// Oriented bounding box 41 | struct OBB 42 | { 43 | Vector3f extent; 44 | Matrix4x4f transform; 45 | 46 | this(Vector3f position, Vector3f size) 47 | { 48 | transform = Matrix4x4f.identity; 49 | center = position; 50 | extent = size; 51 | } 52 | 53 | @property 54 | { 55 | Vector3f center() 56 | { 57 | return transform.translation; 58 | } 59 | 60 | Vector3f center(Vector3f v) 61 | { 62 | transform.a41 = v.x; 63 | transform.a42 = v.y; 64 | transform.a43 = v.z; 65 | return v; 66 | } 67 | } 68 | 69 | @property Matrix3x3f orient() 70 | { 71 | return matrix4x4to3x3(transform); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /dlib/concurrency/workerthread.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2019-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Copyright: Timur Gafarov 2019-2025. 31 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 32 | * Authors: Timur Gafarov 33 | */ 34 | module dlib.concurrency.workerthread; 35 | 36 | import dlib.core.thread; 37 | import dlib.concurrency.threadpool; 38 | import dlib.concurrency.taskqueue; 39 | 40 | /** 41 | * A thread that is created by ThreadPool 42 | */ 43 | class WorkerThread: Thread 44 | { 45 | size_t id; 46 | ThreadPool pool; 47 | protected bool _busy = false; 48 | 49 | /// Constructor 50 | this(size_t id, ThreadPool pool) 51 | { 52 | super(&threadFunc); 53 | this.id = id; 54 | this.pool = pool; 55 | } 56 | 57 | bool busy() 58 | { 59 | return _busy; 60 | } 61 | 62 | protected void threadFunc() 63 | { 64 | while(pool.isRunning) 65 | { 66 | Task task = pool.request(); 67 | if (task.state != TaskState.Invalid) 68 | { 69 | _busy = true; 70 | task.run(); 71 | _busy = false; 72 | } 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /dlib/image/resampling/nearest.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Nearest-neighbor resampling 31 | * 32 | * Copyright: Timur Gafarov 2011-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.image.resampling.nearest; 37 | 38 | import dlib.image.image; 39 | import dlib.image.color; 40 | 41 | /// Resize image with nearest-neighbor filter 42 | SuperImage resampleNearestNeighbor(SuperImage img, in uint newWidth, in uint newHeight) 43 | in 44 | { 45 | assert (img.data.length); 46 | } 47 | do 48 | { 49 | SuperImage res = img.createSameFormat(newWidth, newHeight); 50 | 51 | float scaleWidth = cast(float)newWidth / cast(float)img.width; 52 | float scaleHeight = cast(float)newHeight / cast(float)img.height; 53 | 54 | uint nearest_x, nearest_y; 55 | 56 | foreach(y; 0..res.height) 57 | foreach(x; 0..res.width) 58 | { 59 | nearest_x = cast(uint)(x / scaleWidth); 60 | nearest_y = cast(uint)(y / scaleHeight); 61 | res[x, y] = img[nearest_x, nearest_y]; 62 | } 63 | 64 | return res; 65 | } 66 | -------------------------------------------------------------------------------- /dlib/network/errno.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2020-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | module dlib.network.errno; 30 | 31 | public import core.stdc.errno; 32 | 33 | version(Windows) 34 | { 35 | import core.sys.windows.winsock2; 36 | 37 | enum: int 38 | { 39 | ECONNABORTED = core.sys.windows.winsock2.ECONNABORTED, 40 | ENOBUFS = core.sys.windows.winsock2.ENOBUFS, 41 | EOPNOTSUPP = core.sys.windows.winsock2.EOPNOTSUPP, 42 | EPROTONOSUPPORT = core.sys.windows.winsock2.EPROTONOSUPPORT, 43 | EPROTOTYPE = core.sys.windows.winsock2.EPROTOTYPE, 44 | ESOCKTNOSUPPORT = core.sys.windows.winsock2.ESOCKTNOSUPPORT, 45 | ETIMEDOUT = core.sys.windows.winsock2.ETIMEDOUT, 46 | EWOULDBLOCK = core.sys.windows.winsock2.EWOULDBLOCK 47 | } 48 | } 49 | else version (OSX) 50 | { 51 | version = MacBSD; 52 | } 53 | else version (iOS) 54 | { 55 | version = MacBSD; 56 | } 57 | else version (FreeBSD) 58 | { 59 | version = MacBSD; 60 | } 61 | else version (OpenBSD) 62 | { 63 | version = MacBSD; 64 | } 65 | else version (DragonFlyBSD) 66 | { 67 | version = MacBSD; 68 | } 69 | 70 | version (MacBSD) 71 | { 72 | enum: int 73 | { 74 | ESOCKTNOSUPPORT = 44 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /dlib/core/compound.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Compile-time sequence (aka tuple) + struct hybrid 31 | * 32 | * Description: 33 | * This template can be used to construct data types on the fly 34 | * and return them from functions, which cannot be done with pure compile-time sequence. 35 | * One possible use case for such types is returning result and error message 36 | * from function instead of throwing an exception. 37 | * 38 | * Copyright: Timur Gafarov 2011-2025. 39 | * License: $(LINK2 htpps://boost.org/LICENSE_1_0.txt, Boost License 1.0). 40 | * Authors: Timur Gafarov 41 | */ 42 | module dlib.core.compound; 43 | 44 | /** 45 | * A struct that consists of a compile-time sequence T. Allows square bracket access to the members of a sequence 46 | */ 47 | struct Compound(T...) 48 | { 49 | T tuple; 50 | alias tuple this; 51 | } 52 | 53 | /** 54 | * Returns a Compound consisting of args 55 | */ 56 | Compound!(T) compound(T...)(T args) 57 | { 58 | return Compound!(T)(args); 59 | } 60 | 61 | /// 62 | unittest 63 | { 64 | auto c = compound(true, 0.5f, "hello"); 65 | assert(c[0] == true); 66 | assert(c[1] == 0.5f); 67 | assert(c[2] == "hello"); 68 | } 69 | -------------------------------------------------------------------------------- /dlib/math/package.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2013-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Linear algebra and calculus 31 | * 32 | * Description: 33 | * dlib.math brings vector and matrix types to D, as well as some calculus 34 | * functionality. dlib.math is great as a math library for games, 35 | * graphics/physics engines and rendering pipelines. All types are POD and 36 | * OpenGL-friendly: you can pass your 4x4 matrices to OpenGL functions directly, 37 | * without any conversion. 38 | * 39 | * Copyright: Timur Gafarov 2013-2025. 40 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 41 | * Authors: Timur Gafarov 42 | */ 43 | module dlib.math; 44 | 45 | public 46 | { 47 | import dlib.math.combinatorics; 48 | import dlib.math.complex; 49 | import dlib.math.decomposition; 50 | import dlib.math.diff; 51 | import dlib.math.dual; 52 | import dlib.math.dualquaternion; 53 | import dlib.math.fft; 54 | import dlib.math.hof; 55 | import dlib.math.interpolation; 56 | import dlib.math.linsolve; 57 | import dlib.math.matrix; 58 | import dlib.math.quaternion; 59 | import dlib.math.sse; 60 | import dlib.math.tensor; 61 | import dlib.math.transformation; 62 | import dlib.math.utils; 63 | import dlib.math.vector; 64 | } 65 | -------------------------------------------------------------------------------- /dlib/audio/io/package.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2022-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Copyright: Timur Gafarov 2022-2025. 31 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 32 | * Authors: Timur Gafarov 33 | */ 34 | module dlib.audio.io; 35 | 36 | import std.path: extension; 37 | import dlib.audio.sound; 38 | 39 | public 40 | { 41 | import dlib.audio.io.wav; 42 | } 43 | 44 | class SoundLoadException: Exception 45 | { 46 | this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null) 47 | { 48 | super(msg, file, line, next); 49 | } 50 | } 51 | 52 | /** 53 | * Saves a sound to file, selects encoder by filename extension 54 | */ 55 | void saveSound(GenericSound snd, string filename) 56 | { 57 | switch(filename.extension) 58 | { 59 | case ".wav", ".WAV": 60 | snd.saveWAV(filename); 61 | break; 62 | default: 63 | assert(0, "Sound I/O error: unsupported sound format or illegal extension"); 64 | } 65 | } 66 | 67 | /** 68 | * Loads sound from a file, selects decoder by filename extension 69 | */ 70 | GenericSound loadSound(string filename) 71 | { 72 | switch(filename.extension) 73 | { 74 | case ".wav", ".WAV": 75 | return loadWAV(filename); 76 | default: 77 | assert(0, "Sound I/O error: unsupported sound format or illegal extension"); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /dlib/image/filters/boxblur.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Box blur 31 | * 32 | * Copyright: Timur Gafarov 2011-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.image.filters.boxblur; 37 | 38 | import dlib.image.color; 39 | import dlib.image.image; 40 | 41 | /// Blur an image 42 | SuperImage boxBlur(SuperImage img, SuperImage outp, int radius) 43 | { 44 | SuperImage res; 45 | if (outp) 46 | res = outp; 47 | else 48 | res = img.dup; 49 | 50 | immutable int boxSide = radius * 2 + 1; 51 | immutable int boxSide2 = boxSide * boxSide; 52 | 53 | foreach(y; 0..img.height) 54 | foreach(x; 0..img.width) 55 | { 56 | float alpha = Color4f(img[x, y]).a; 57 | 58 | Color4f total = Color4f(0, 0, 0); 59 | 60 | foreach(ky; 0..boxSide) 61 | foreach(kx; 0..boxSide) 62 | { 63 | int iy = y + (ky - radius); 64 | int ix = x + (kx - radius); 65 | 66 | total += img[ix, iy]; 67 | } 68 | 69 | total /= boxSide2; 70 | total.a = alpha; 71 | 72 | res[x,y] = total; 73 | } 74 | 75 | return res; 76 | } 77 | 78 | /// ditto 79 | SuperImage boxBlur(SuperImage img, int radius) 80 | { 81 | return boxBlur(img, null, radius); 82 | } 83 | -------------------------------------------------------------------------------- /dlib/image/io/utils.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2014-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Image I/O utility functions 31 | * 32 | * Copyright: Timur Gafarov, Roman Chistokhodov 2014-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov, Roman Chistokhodov 35 | */ 36 | module dlib.image.io.utils; 37 | 38 | import std.stdio; 39 | import std.conv; 40 | import dlib.core.stream; 41 | 42 | T readStruct(T)(File* f, bool bigEndian = false) 43 | { 44 | T res; 45 | foreach(ref field; res.tupleof) 46 | { 47 | alias FieldType = typeof(field); 48 | 49 | ubyte[FieldType.sizeof] bytes; 50 | f.rawRead(bytes); 51 | 52 | FieldType val = *cast(FieldType*)bytes.ptr; 53 | 54 | // TODO: 55 | //if (bigEndian) 56 | // val = val.bigEndianToNative; 57 | 58 | field = val; 59 | } 60 | return res; 61 | } 62 | 63 | T readStruct(T)(InputStream input, bool bigEndian = false) 64 | { 65 | T res; 66 | foreach(ref field; res.tupleof) 67 | { 68 | alias FieldType = typeof(field); 69 | 70 | ubyte[FieldType.sizeof] bytes; 71 | input.fillArray(bytes); 72 | 73 | FieldType val = *cast(FieldType*)bytes.ptr; 74 | 75 | // TODO: 76 | //if (bigEndian) 77 | // val = val.bigEndianToNative; 78 | 79 | field = val; 80 | } 81 | return res; 82 | } 83 | -------------------------------------------------------------------------------- /dlib/random/random.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Pseudo-random numbers based on C rand function 31 | * 32 | * Copyright: Timur Gafarov 2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.random.random; 37 | 38 | import core.stdc.stdlib; 39 | import core.stdc.time; 40 | import core.thread.osthread: getpid; 41 | import std.math; 42 | import std.algorithm: sum; 43 | 44 | static this() 45 | { 46 | srand(cast(uint)seed()); 47 | } 48 | 49 | auto seed() 50 | { 51 | return mix(clock(), time(null), getpid()); 52 | } 53 | 54 | /** 55 | * Bob Jenkins' 96 bit mix function 56 | */ 57 | ulong mix(ulong a, ulong b, ulong c) 58 | { 59 | a=a-b; a=a-c; a=a^(c >> 13); 60 | b=b-c; b=b-a; b=b^(a << 8); 61 | c=c-a; c=c-b; c=c^(b >> 13); 62 | a=a-b; a=a-c; a=a^(c >> 12); 63 | b=b-c; b=b-a; b=b^(a << 16); 64 | c=c-a; c=c-b; c=c^(b >> 5); 65 | a=a-b; a=a-c; a=a^(c >> 3); 66 | b=b-c; b=b-a; b=b^(a << 10); 67 | c=c-a; c=c-b; c=c^(b >> 15); 68 | return c; 69 | } 70 | 71 | /** 72 | * Returns pseudo-random integer between mi (inclusive) and ma (exclusive) 73 | */ 74 | int randomInRange(int mi, int ma) 75 | { 76 | return (rand() % (ma - mi)) + mi; 77 | } 78 | 79 | /** 80 | * Returns pseudo-random floating-point number in 0..1 range 81 | */ 82 | T random(T)() 83 | { 84 | T res = (rand() % RAND_MAX) / cast(T)RAND_MAX; 85 | return res; 86 | } 87 | -------------------------------------------------------------------------------- /dlib/image/filters/desaturate.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2015-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Turn image to black and white 31 | * 32 | * Copyright: Timur Gafarov 2015-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.image.filters.desaturate; 37 | 38 | import dlib.image.image; 39 | import dlib.image.color; 40 | 41 | /// Default desaturate filter 42 | alias desaturate = desaturate709; 43 | 44 | /// ITU-R recommendation BT.709 45 | SuperImage desaturate709(SuperImage img, SuperImage outp = null) 46 | { 47 | SuperImage res; 48 | if (outp) 49 | res = outp; 50 | else 51 | res = img.dup; 52 | 53 | foreach(y; 0..img.height) 54 | foreach(x; 0..img.width) 55 | { 56 | auto color = img[x, y]; 57 | float l = color.luminance709; 58 | res[x, y] = Color4f(l, l, l, color.a); 59 | } 60 | 61 | return res; 62 | } 63 | 64 | /// ITU-R recommendation BT.601 65 | SuperImage desaturate601(SuperImage img, SuperImage outp = null) 66 | { 67 | SuperImage res; 68 | if (outp) 69 | res = outp; 70 | else 71 | res = img.dup; 72 | 73 | foreach(y; 0..img.height) 74 | foreach(x; 0..img.width) 75 | { 76 | auto color = img[x, y]; 77 | float l = color.luminance601; 78 | res[x, y] = Color4f(l, l, l, color.a); 79 | } 80 | 81 | return res; 82 | } 83 | -------------------------------------------------------------------------------- /dlib/image/fthread.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2013-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Threaded image filtering 31 | * 32 | * Copyright: Timur Gafarov 2013-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.image.fthread; 37 | 38 | import dlib.core.memory; 39 | import dlib.core.thread; 40 | import dlib.image.image; 41 | 42 | /** 43 | * An object that applies a filter function to an image in a separate thread 44 | */ 45 | class FilteringThread 46 | { 47 | Thread thread; 48 | protected SuperImage image; 49 | protected SuperImage output; 50 | 51 | this(SuperImage img) 52 | { 53 | thread = New!Thread(&threadFunc); 54 | image = img; 55 | output = img; 56 | } 57 | 58 | ~this() 59 | { 60 | Delete(thread); 61 | } 62 | 63 | void threadFunc() 64 | { 65 | run(); 66 | } 67 | 68 | SuperImage filtered() 69 | { 70 | thread.start(); 71 | while(thread.isRunning) 72 | onRunning(); 73 | onFinished(); 74 | return output; 75 | } 76 | 77 | /// Called in a second thread. Override it 78 | void run() {} 79 | 80 | /// Called in main thread in a loop while second thread is running. Override it 81 | void onRunning() {} 82 | 83 | /// Called in main thread once when second thread finishes. Override it 84 | void onFinished() {} 85 | } 86 | -------------------------------------------------------------------------------- /dlib/image/filters/histogram.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2018-2025 Oleg Baharev, Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Generate histogram of an image 31 | * 32 | * Copyright: Oleg Baharev, Timur Gafarov 2018-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Oleg Baharev, Timur Gafarov 35 | */ 36 | module dlib.image.filters.histogram; 37 | 38 | import dlib.image.image; 39 | import dlib.image.color; 40 | 41 | /// Obtain histogram 42 | int[256] createHistogram(SuperImage img) 43 | { 44 | int[256] histogram; 45 | 46 | foreach (x; 0..img.width) 47 | foreach (y; 0..img.height) 48 | { 49 | int luma = cast(int)(img[x,y].luminance * 255); 50 | histogram[luma] += 1; 51 | } 52 | 53 | return histogram; 54 | } 55 | 56 | /// Generate histogram image 57 | SuperImage histogramImage(SuperImage img, Color4f background, Color4f diagram) 58 | { 59 | SuperImage res = img.createSameFormat(256, 256); 60 | int[256] h = createHistogram(img); 61 | 62 | int vmax = 0; 63 | foreach(v; h) 64 | { 65 | if (v > vmax) 66 | vmax = v; 67 | } 68 | 69 | foreach(ref v; h) 70 | { 71 | v = cast(int)(cast(float)v / cast(float)vmax * 255.0f); 72 | } 73 | 74 | foreach (x; 0..res.width) 75 | foreach (y; 0..res.height) 76 | { 77 | int v = h[x]; 78 | if (y < 255 - v) 79 | res[x, y] = background; 80 | else 81 | res[x, y] = diagram; 82 | } 83 | return res; 84 | } 85 | -------------------------------------------------------------------------------- /dlib/image/filters/contrast.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Adjust contrast 31 | * 32 | * Copyright: Timur Gafarov 2011-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.image.filters.contrast; 37 | 38 | import dlib.image.image; 39 | import dlib.image.color; 40 | 41 | /// Contrast method 42 | enum ContrastMethod 43 | { 44 | AverageGray, 45 | AverageImage, 46 | } 47 | 48 | /// Adjust contrast 49 | SuperImage contrast(SuperImage img, SuperImage outp, float k, ContrastMethod method = ContrastMethod.AverageGray) 50 | { 51 | SuperImage res; 52 | if (outp) 53 | res = outp; 54 | else 55 | res = img.dup; 56 | 57 | Color4f aver = Color4f(0.0f, 0.0f, 0.0f); 58 | 59 | if (method == ContrastMethod.AverageGray) 60 | { 61 | aver = Color4f(0.5f, 0.5f, 0.5f); 62 | } 63 | else if (method == ContrastMethod.AverageImage) 64 | { 65 | foreach(y; 0..res.height) 66 | foreach(x; 0..res.width) 67 | { 68 | aver += img[x, y]; 69 | } 70 | 71 | aver /= (res.height * res.width); 72 | } 73 | 74 | foreach(y; 0..res.height) 75 | foreach(x; 0..res.width) 76 | { 77 | auto col = img[x, y]; 78 | col = ((col - aver) * k + aver); 79 | res[x, y] = col; 80 | } 81 | 82 | return res; 83 | } 84 | 85 | /// ditto 86 | SuperImage contrast(SuperImage a, float k, ContrastMethod method = ContrastMethod.AverageGray) 87 | { 88 | return contrast(a, null, k, method); 89 | } 90 | -------------------------------------------------------------------------------- /dlib/core/bitio.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2015-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Bit-level manipulations 31 | * 32 | * Copyright: Timur Gafarov 2015-2025. 33 | * License: $(LINK2 https://boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.core.bitio; 37 | 38 | /** 39 | * Endianness 40 | */ 41 | enum Endian 42 | { 43 | Little, 44 | Big 45 | } 46 | 47 | /** 48 | * Returns high 4 bits of a byte 49 | */ 50 | T hiNibble(T)(T b) 51 | { 52 | return ((b >> 4) & 0x0F); 53 | } 54 | 55 | /// 56 | unittest 57 | { 58 | assert(hiNibble(0xFE) == 0x0F); 59 | } 60 | 61 | /** 62 | * Returns low 4 bits of a byte 63 | */ 64 | T loNibble(T)(T b) 65 | { 66 | return (b & 0x0F); 67 | } 68 | 69 | /// 70 | unittest 71 | { 72 | assert(loNibble(0xFE) == 0x0E); 73 | } 74 | 75 | /** 76 | * Sets bit at position pos in integer b to state 77 | */ 78 | T setBit(T)(T b, uint pos, bool state) 79 | { 80 | if (state) 81 | return cast(T)(b | (1 << pos)); 82 | else 83 | return cast(T)(b & ~(1 << pos)); 84 | } 85 | 86 | /// 87 | unittest 88 | { 89 | assert(setBit(0, 0, true) == 1); 90 | assert(setBit(1, 0, false) == 0); 91 | } 92 | 93 | /** 94 | * Returns bit at position pos in integer b 95 | */ 96 | bool getBit(T)(T b, uint pos) 97 | { 98 | return ((b & (1 << pos)) != 0); 99 | } 100 | 101 | /// 102 | unittest 103 | { 104 | assert(getBit(1, 0) == 1); 105 | } 106 | -------------------------------------------------------------------------------- /dlib/container/spscqueue.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Wait-free single-producer single-consumer queue. 31 | * 32 | * Copyright: Timur Gafarov 2025 33 | * License: $(LINK2 https://boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.container.spscqueue; 37 | 38 | import core.atomic; 39 | 40 | import dlib.core.memory; 41 | import dlib.core.ownership; 42 | import dlib.core.thread; 43 | import dlib.container.array; 44 | 45 | /** 46 | * Generic, wait-free single-producer single-consumer queue. 47 | * 48 | * Params: 49 | * T = element type. 50 | * capacity = Maximum number of elements in the queue. 51 | */ 52 | struct SPSCQueue(T, size_t capacity) 53 | { 54 | /// Circular buffer. 55 | T[capacity] buffer; 56 | 57 | /// Producer write index. 58 | shared size_t head = 0; 59 | 60 | /// Consumer read index. 61 | shared size_t tail = 0; 62 | 63 | /// Add an element to the queue. Returns false if full. 64 | bool push(T value) 65 | { 66 | auto next = (head + 1) % capacity; 67 | if (next == tail) // queue full 68 | return false; 69 | 70 | buffer[head] = value; 71 | atomicStore!(MemoryOrder.rel)(head, next); 72 | return true; 73 | } 74 | 75 | /// Remove an element from the queue. Returns false if empty. 76 | bool pop(out T value) 77 | { 78 | if (tail == atomicLoad!(MemoryOrder.acq)(head)) 79 | return false; // queue empty 80 | 81 | value = buffer[tail]; 82 | tail = (tail + 1) % capacity; 83 | return true; 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /dlib/text/utils.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2016-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Text processing utils 31 | * 32 | * Copyright: Timur Gafarov 2016-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.text.utils; 37 | 38 | import dlib.core.memory; 39 | 40 | /// Make an copy of a string in unmanaged memory 41 | T[] copy(T)(T[] b) 42 | { 43 | auto res = New!(T[])(b.length); 44 | foreach(i, c; b) 45 | res[i] = c; 46 | return res; 47 | } 48 | 49 | /// 50 | unittest 51 | { 52 | auto str = "hello".dup; 53 | auto c = copy(str); 54 | assert(c == str); 55 | Delete(c); 56 | } 57 | 58 | /// Make an immutable copy of a string in unmanaged memory 59 | immutable(T)[] immutableCopy(T)(immutable(T)[] b) 60 | { 61 | auto res = New!(T[])(b.length); 62 | foreach(i, c; b) 63 | res[i] = c; 64 | return cast(immutable(T)[])res; 65 | } 66 | 67 | /// Concatenates two strings to a new string in unmanaged memory 68 | string catStr(string s1, string s2) 69 | { 70 | char[] buffer = New!(char[])(s1.length + s2.length); 71 | size_t i, j; 72 | for(i = 0; i < s1.length; i++) 73 | { 74 | buffer[i] = s1[i]; 75 | } 76 | for(j = 0; j < s2.length; j++) 77 | { 78 | buffer[i+j] = s2[j]; 79 | } 80 | return cast(string)buffer; 81 | } 82 | 83 | /// 84 | unittest 85 | { 86 | auto str1 = "hello"; 87 | auto str2 = " world"; 88 | 89 | auto cat = catStr(str1, str2); 90 | assert(cat == "hello world"); 91 | Delete(cat); 92 | } 93 | -------------------------------------------------------------------------------- /dlib/memory/gcallocator.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2017-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Allocator based on D's built-in garbage collector 31 | * 32 | * Copyright: Timur Gafarov 2017-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.memory.gcallocator; 37 | 38 | import core.exception; 39 | import core.memory; 40 | import std.algorithm.comparison; 41 | import dlib.memory.allocator; 42 | 43 | /** 44 | * Allocator based on D's built-in garbage collector 45 | */ 46 | class GCallocator: Allocator 47 | { 48 | void[] allocate(size_t size) 49 | { 50 | return GC.malloc(size)[0..size]; 51 | } 52 | 53 | bool deallocate(void[] p) 54 | { 55 | GC.free(p.ptr); 56 | return true; 57 | } 58 | 59 | bool reallocate(ref void[] p, size_t size) 60 | { 61 | GC.realloc(p.ptr, size); 62 | return true; 63 | } 64 | 65 | @property immutable(uint) alignment() const 66 | { 67 | return cast(uint) max(double.alignof, real.alignof); 68 | } 69 | 70 | static @property GCallocator instance() nothrow 71 | { 72 | if (instance_ is null) 73 | { 74 | immutable size = __traits(classInstanceSize, GCallocator); 75 | void* p = GC.malloc(size); 76 | 77 | if (p is null) 78 | { 79 | onOutOfMemoryError(); 80 | } 81 | p[0..size] = typeid(GCallocator).initializer[]; 82 | instance_ = cast(GCallocator)p[0..size].ptr; 83 | 84 | } 85 | return instance_; 86 | } 87 | 88 | private static __gshared GCallocator instance_; 89 | } 90 | -------------------------------------------------------------------------------- /dlib/core/tuple.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Templates that construct tuples of numeric values 31 | * 32 | * Copyright: Timur Gafarov 2011-2025. 33 | * License: $(LINK2 https://boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.core.tuple; 37 | 38 | /// Create a tuple 39 | template Tuple(E...) 40 | { 41 | alias Tuple = E; 42 | } 43 | 44 | /// Yields a tuple of integer literals from 0 to stop 45 | template RangeTuple(int stop) 46 | { 47 | static if (stop <= 0) 48 | alias RangeTuple = Tuple!(); 49 | else 50 | alias RangeTuple = Tuple!(RangeTuple!(stop-1), stop-1); 51 | } 52 | 53 | /// Yields a tuple of integer literals from start to stop 54 | template RangeTuple(int start, int stop) 55 | { 56 | static if (stop <= start) 57 | alias RangeTuple = Tuple!(); 58 | else 59 | alias RangeTuple = Tuple!(RangeTuple!(start, stop-1), stop-1); 60 | } 61 | 62 | /// Yields a tuple of integer literals from start to stop with defined step 63 | template RangeTuple(int start, int stop, int step) 64 | { 65 | static assert(step != 0, "RangeTuple: step must be != 0"); 66 | 67 | static if (step > 0) 68 | { 69 | static if (stop <= start) 70 | alias RangeTuple = Tuple!(); 71 | else 72 | alias RangeTuple = Tuple!(RangeTuple!(start, stop-step, step), stop-step); 73 | } 74 | else 75 | { 76 | static if (stop >= start) 77 | alias RangeTuple = Tuple!(); 78 | else 79 | alias RangeTuple = Tuple!(RangeTuple!(start, stop-step, step), stop-step); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /dlib/text/encodings.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2018-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Generic encoding tools 31 | * 32 | * Description: 33 | * This module works with any encoder and decoder structs that implement 34 | * the following basic interfaces: 35 | * --- 36 | * struct Encoder 37 | * { 38 | * // Encodes a Unicode code point to user-provided buffer. 39 | * // Should return bytes written or 0 at error 40 | * size_t encode(uint codePoint, char[] buffer) 41 | * } 42 | * 43 | * struct Decoder 44 | * { 45 | * // An input range that iterates characters of a string, 46 | * // decoding them to Unicode code points 47 | * auto decode(string input) 48 | * } 49 | * --- 50 | * 51 | * Copyright: Timur Gafarov 2018-2025. 52 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 53 | * Authors: Timur Gafarov 54 | */ 55 | module dlib.text.encodings; 56 | 57 | import dlib.container.array; 58 | import dlib.text.utils; 59 | 60 | public 61 | { 62 | import dlib.text.utf8; 63 | import dlib.text.utf16; 64 | } 65 | 66 | /** 67 | * Converts a string from one encoding to another. 68 | * Decoder and encoder are specified at compile time 69 | * 70 | * Examples: 71 | * --- 72 | * string s = transcode!(UTF16Decoder, UTF8Encoder)(input); 73 | * --- 74 | */ 75 | string transcode(Decoder, Encoder)(string input) 76 | { 77 | DynamicArray!char array; 78 | 79 | auto decoder = Decoder(); 80 | auto encoder = Encoder(); 81 | 82 | foreach(c; decoder.decode(input)) 83 | { 84 | char[4] buffer; 85 | size_t len = encoder.encode(c, buffer); 86 | if (len) 87 | array.append(buffer[0..len]); 88 | } 89 | 90 | auto output = copy(array.data); 91 | array.free(); 92 | return cast(string)output; 93 | } 94 | -------------------------------------------------------------------------------- /dlib/math/decomposition.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2013-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Matrix decomposition 31 | * 32 | * Copyright: Timur Gafarov 2013-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.math.decomposition; 37 | 38 | import std.math; 39 | import dlib.math.matrix; 40 | 41 | /** 42 | * LUP decomposition. 43 | * Factors a matrix A into PA = LU, 44 | * where L is a lower triangular matrix, 45 | * U is an upper triangular matrix, 46 | * and P is a permutation matrix. 47 | */ 48 | void decomposeLUP(T, size_t N)( 49 | Matrix!(T,N) A, 50 | ref Matrix!(T,N) L, 51 | ref Matrix!(T,N) U, 52 | ref Matrix!(T,N) P) 53 | { 54 | Matrix!(T,N) C = A; 55 | P = Matrix!(T,N).identity; 56 | 57 | for (size_t i = 0; i < N; i++) 58 | { 59 | T pivotValue = 0.0; 60 | size_t pivot; 61 | for (size_t row = i; row < N; row++) 62 | { 63 | if (abs(C[row, i]) > pivotValue) 64 | { 65 | pivotValue = abs(C[row, i]); 66 | pivot = row; 67 | } 68 | } 69 | 70 | assert(pivotValue != 0.0); 71 | 72 | P.swapRows(pivot, i); 73 | C.swapRows(pivot, i); 74 | 75 | for (size_t j = i + 1; j < N; j++) 76 | { 77 | C[j, i] = C[j, i] / C[i, i]; 78 | for (size_t k = i + 1; k < N; k++) 79 | C[j, k] = C[j, k] - C[j, i] * C[i, k]; 80 | } 81 | } 82 | 83 | L = Matrix!(T,N).identity; 84 | U = Matrix!(T,N).zero; 85 | for (size_t i = 0; i < N; i++) 86 | { 87 | for (size_t j = 0; j < N-1-i; j++) 88 | L[N-1-i, j] = C[N-1-i, j]; 89 | for (size_t j = i; j < N; j++) 90 | U[i, j] = C[i, j]; 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /dlib/filesystem/delegaterange.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2014-2025 Martin Cejp 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Copyright: Martin Cejp 2014-2025. 31 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 32 | * Authors: Martin Cejp 33 | */ 34 | module dlib.filesystem.delegaterange; 35 | 36 | private import std.range; 37 | 38 | /** 39 | * An input range that enumerates items by obtaining them from a delegate until it returns 0 40 | */ 41 | class DelegateInputRange(T): InputRange!T 42 | { 43 | bool delegate(out T t) fetch; 44 | bool have = false; 45 | T front_; 46 | 47 | this(bool delegate(out T t) fetch) 48 | { 49 | this.fetch = fetch; 50 | } 51 | 52 | override bool empty() 53 | { 54 | if (!have) 55 | have = fetch(front_); 56 | 57 | return !have; 58 | } 59 | 60 | override T front() 61 | { 62 | return front_; 63 | } 64 | 65 | override void popFront() 66 | { 67 | have = false; 68 | } 69 | 70 | override T moveFront() 71 | { 72 | have = false; 73 | return front_; 74 | } 75 | 76 | override int opApply(scope int delegate(T) dg) 77 | { 78 | int result = 0; 79 | 80 | for (size_t i = 0; !empty; i++) 81 | { 82 | result = dg(moveFront); 83 | 84 | if (result != 0) 85 | break; 86 | } 87 | 88 | return result; 89 | } 90 | 91 | override int opApply(scope int delegate(size_t, T) dg) 92 | { 93 | int result = 0; 94 | 95 | for (size_t i = 0; !empty; i++) 96 | { 97 | result = dg(i, moveFront); 98 | 99 | if (result != 0) 100 | break; 101 | } 102 | 103 | return result; 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /dlib/image/filters/lens.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2014-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Lens distortion filter 31 | * 32 | * Copyright: Timur Gafarov 2014-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.image.filters.lens; 37 | 38 | import std.math; 39 | import dlib.image.image; 40 | 41 | /// Apply lens distortion filter 42 | SuperImage lensDistortion( 43 | SuperImage img, 44 | SuperImage outp, 45 | float strength, 46 | float zoom, 47 | bool interpolation = true) 48 | { 49 | SuperImage res; 50 | if (outp) 51 | res = outp; 52 | else 53 | res = img.dup; 54 | 55 | float halfWidth = cast(float)img.width / 2.0f; 56 | float halfHeight = cast(float)img.height / 2.0f; 57 | 58 | float correctionRadius = sqrt(cast(float)(img.width ^^ 2 + img.height ^^ 2)) / strength; 59 | 60 | foreach(y; 0..img.height) 61 | foreach(x; 0..img.width) 62 | { 63 | float newX = x - halfWidth; 64 | float newY = y - halfHeight; 65 | 66 | float distance = sqrt(newX ^^ 2 + newY ^^ 2); 67 | float r = distance / correctionRadius; 68 | 69 | float theta; 70 | if (r == 0) 71 | theta = 1; 72 | else 73 | theta = atan(r) / r; 74 | 75 | float sourceX = (halfWidth + theta * newX * zoom); 76 | float sourceY = (halfHeight + theta * newY * zoom); 77 | 78 | if (interpolation) 79 | res[x, y] = img.bilinearPixel(sourceX, sourceY); 80 | else 81 | res[x, y] = img[cast(int)sourceX, cast(int)sourceY]; 82 | } 83 | 84 | return res; 85 | } 86 | 87 | /// ditto 88 | SuperImage lensDistortion( 89 | SuperImage img, 90 | float strength, 91 | float zoom, 92 | bool interpolation = true) 93 | { 94 | return lensDistortion(img, null, strength, zoom, interpolation); 95 | } 96 | -------------------------------------------------------------------------------- /dlib/math/interpolation/easing.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2019-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Easing functions 31 | * 32 | * Copyright: Timur Gafarov 2019-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.math.interpolation.easing; 37 | 38 | /// Quadratic ease in 39 | T easeInQuad(T)(T t) 40 | { 41 | return t * t; 42 | } 43 | 44 | /// Quadratic ease out 45 | T easeOutQuad(T)(T t) 46 | { 47 | return -t * (t - 2); 48 | } 49 | 50 | /// Quadratic ease in-out 51 | T easeInOutQuad(T)(T t) 52 | { 53 | t /= 0.5; 54 | if (t < 1.0) 55 | return 0.5 * t * t; 56 | t--; 57 | return -0.5 * (t * (t - 2.0) - 1.0); 58 | } 59 | 60 | /// Back ease in 61 | T easeInBack(T)(T t) 62 | { 63 | enum T s = 1.70158; 64 | return t * t * ((s + 1) * t - s); 65 | } 66 | 67 | /// Back ease out 68 | T easeOutBack(T)(T t) 69 | { 70 | enum T s = 1.70158; 71 | t = t - 1.0; 72 | return (t * t * ((s + 1) * t + s) + 1.0); 73 | } 74 | 75 | /// Back ease in-out 76 | T easeInOutBack(T)(T t) 77 | { 78 | float s = 1.70158; 79 | t /= 0.5; 80 | if (t < 1.0) 81 | { 82 | s *= 1.525; 83 | return 0.5 * (t * t * ((s + 1.0) * t - s)); 84 | } 85 | t -= 2.0; 86 | s *= 1.525; 87 | return 0.5 * (t * t * ((s + 1.0) * t + s) + 2.0); 88 | } 89 | 90 | /// Bounce ease out 91 | T easeOutBounce(T)(T t) 92 | { 93 | if (t < (1.0 / 2.75)) 94 | { 95 | return (7.5625 * t * t); 96 | } 97 | else if (t < (2.0 / 2.75)) 98 | { 99 | t -= (1.5 / 2.75); 100 | return (7.5625 * (t) * t + 0.75); 101 | } 102 | else if (t < (2.5 / 2.75)) 103 | { 104 | t -= (2.25 / 2.75); 105 | return (7.5625 * (t) * t + 0.9375); 106 | } 107 | else 108 | { 109 | t -= (2.625 / 2.75); 110 | return (7.5625 * (t) * t + 0.984375); 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /dlib/image/filters/median.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2022-2025 Oleg Baharev, Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Median filter. 31 | * 32 | * Copyright: Oleg Baharev, Timur Gafarov 2022-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Oleg Baharev, Timur Gafarov 35 | */ 36 | module dlib.image.filters.median; 37 | 38 | import std.algorithm: sort; 39 | import dlib.core.memory; 40 | import dlib.image.image; 41 | import dlib.image.color; 42 | 43 | /// Median filter 44 | auto medianFilter(SuperImage img, SuperImage outp, uint windowWidth, uint windowHeight) 45 | { 46 | SuperImage res; 47 | if (outp) 48 | res = outp; 49 | else 50 | res = img.dup; 51 | 52 | struct ColorAndLuma 53 | { 54 | Color4f color; 55 | float luminance; 56 | } 57 | 58 | ColorAndLuma[] window = New!(ColorAndLuma[])(windowWidth * windowHeight); 59 | 60 | uint halfWindowWidth = windowWidth / 2; 61 | uint halfWindowHeight = windowHeight / 2; 62 | 63 | foreach(y; 0..img.height) 64 | foreach(x; 0..img.width) 65 | { 66 | foreach(wy; 0..windowHeight) 67 | foreach(wx; 0..windowWidth) 68 | { 69 | int sampleX = x - halfWindowWidth + wx; 70 | int sampleY = y - halfWindowHeight + wy; 71 | if (sampleX < 0) sampleX = 0; 72 | else if (sampleX >= img.width) sampleX = img.width - 1; 73 | if (sampleY < 0) sampleY = 0; 74 | else if (sampleY >= img.height) sampleY = img.height - 1; 75 | 76 | auto sample = img[sampleX, sampleY]; 77 | 78 | auto windowSample = &window[wy * windowWidth + wx]; 79 | windowSample.color = sample; 80 | windowSample.luminance = sample.luminance; 81 | } 82 | 83 | sort!("a.luminance > b.luminance")(window); 84 | res[x, y] = window[$ / 2].color; 85 | } 86 | 87 | Delete(window); 88 | 89 | return res; 90 | } 91 | 92 | /// ditto 93 | SuperImage medianFilter(SuperImage img, uint windowWidth, uint windowHeight) 94 | { 95 | return medianFilter(img, null, windowWidth, windowHeight); 96 | } 97 | -------------------------------------------------------------------------------- /dlib/concurrency/taskqueue.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2019-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Copyright: Timur Gafarov 2019-2025. 31 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 32 | * Authors: Timur Gafarov 33 | */ 34 | module dlib.concurrency.taskqueue; 35 | 36 | import dlib.core.mutex; 37 | import dlib.container.array; 38 | import dlib.concurrency.workerthread; 39 | 40 | /** 41 | * State of a Task 42 | */ 43 | enum TaskState 44 | { 45 | Valid = 0, 46 | Invalid = 1 47 | } 48 | 49 | /** 50 | * Task object that encapsulates a delegate 51 | */ 52 | struct Task 53 | { 54 | TaskState state = TaskState.Invalid; 55 | void delegate() func; 56 | 57 | void run() 58 | { 59 | if (func !is null) 60 | { 61 | func(); 62 | } 63 | } 64 | } 65 | 66 | /** 67 | * Asynchronous task queue 68 | */ 69 | class TaskQueue 70 | { 71 | protected: 72 | enum size_t MaxTasks = 64; 73 | Array!(Task, MaxTasks) tasks; 74 | Mutex mutex; 75 | 76 | public: 77 | 78 | /// Constructor 79 | this() 80 | { 81 | mutex.init(); 82 | } 83 | 84 | ~this() 85 | { 86 | tasks.free(); 87 | mutex.destroy(); 88 | } 89 | 90 | /// Number of queued tasks 91 | size_t count() 92 | { 93 | return tasks.length; 94 | } 95 | 96 | /// Add a task to queue 97 | bool enqueue(Task task) 98 | { 99 | if (tasks.length < MaxTasks) 100 | { 101 | mutex.lock(); 102 | tasks.insertFront(task); 103 | mutex.unlock(); 104 | return true; 105 | } 106 | else 107 | return false; 108 | } 109 | 110 | /// Remove a task from queue 111 | Task dequeue() 112 | { 113 | if (tasks.length) 114 | { 115 | mutex.lock(); 116 | Task t = tasks[tasks.length-1]; 117 | tasks.removeBack(1); 118 | mutex.unlock(); 119 | return t; 120 | } 121 | else 122 | return Task(TaskState.Invalid, null); 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /dlib/filesystem/posix/directory.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2014-2025 Martin Cejp 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | module dlib.filesystem.posix.directory; 30 | 31 | version(Posix) 32 | { 33 | import std.conv; 34 | import std.range; 35 | 36 | import dlib.filesystem.filesystem; 37 | import dlib.filesystem.dirrange; 38 | import dlib.filesystem.posix.common; 39 | 40 | class PosixDirectory: Directory 41 | { 42 | FileSystem fs; 43 | DIR* dir; 44 | string prefix; 45 | 46 | this(FileSystem fs, DIR* dir, string prefix) 47 | { 48 | this.fs = fs; 49 | this.dir = dir; 50 | this.prefix = prefix; 51 | } 52 | 53 | ~this() 54 | { 55 | close(); 56 | } 57 | 58 | void close() 59 | { 60 | if (dir != null) 61 | { 62 | closedir(dir); 63 | dir = null; 64 | } 65 | } 66 | 67 | InputRange!DirEntry contents() 68 | { 69 | if (dir == null) 70 | return null; // FIXME: throw an error 71 | 72 | return new DirRange(delegate bool(out DirEntry de) 73 | { 74 | dirent entry_buf; 75 | dirent* entry; 76 | 77 | for (;;) 78 | { 79 | readdir_r(dir, &entry_buf, &entry); 80 | 81 | if (entry == null) 82 | return false; 83 | else 84 | { 85 | string name = to!string(cast(const char*) entry.d_name); 86 | 87 | if (name == "." || name == "..") 88 | continue; 89 | 90 | de.name = name; 91 | de.isFile = (entry.d_type & DT_REG) != 0; 92 | de.isDirectory = (entry.d_type & DT_DIR) != 0; 93 | 94 | return true; 95 | } 96 | } 97 | }); 98 | } 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /dlib/container/queue.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Queue (based on linked list) 31 | * 32 | * Copyright: Timur Gafarov 2011-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov, Andrey Penechko, Roman Chistokhodov 35 | */ 36 | module dlib.container.queue; 37 | 38 | import dlib.container.array; 39 | 40 | /** 41 | * Queue implementation based on Array. 42 | */ 43 | struct Queue(T) 44 | { 45 | private: 46 | Array!T array; 47 | 48 | public: 49 | /** 50 | * Check if stack has no elements. 51 | */ 52 | @property bool empty() 53 | { 54 | return array.length == 0; 55 | } 56 | 57 | /** 58 | * Add element to queue. 59 | */ 60 | void enqueue(const(T) v) 61 | { 62 | array.insertBack(v); 63 | } 64 | 65 | /** 66 | * Remove element from queue. 67 | * Returns: Removed element. 68 | * Throws: Exception if queue is empty. 69 | */ 70 | T dequeue() 71 | { 72 | if (empty) 73 | throw new Exception("Queue!(T): queue is empty"); 74 | 75 | T res = array[0]; 76 | array.removeFront(1); 77 | return res; 78 | } 79 | 80 | /** 81 | * Non-throwing version of dequeue. 82 | * Returns: true on success, false on failure. 83 | * Element is stored in value. 84 | */ 85 | bool dequeue(ref T value) nothrow 86 | { 87 | if (empty) 88 | return false; 89 | 90 | value = array[0]; 91 | array.removeFront(1); 92 | return true; 93 | } 94 | 95 | /** 96 | * Free memory allocated by Queue. 97 | */ 98 | void free() 99 | { 100 | array.free(); 101 | } 102 | } 103 | 104 | /// 105 | unittest 106 | { 107 | import std.exception: assertThrown; 108 | 109 | Queue!int q; 110 | assertThrown(q.dequeue()); 111 | assert(q.empty); 112 | 113 | q.enqueue(50); 114 | q.enqueue(30); 115 | q.enqueue(900); 116 | 117 | int v; 118 | q.dequeue(v); 119 | assert(v == 50); 120 | assert(q.dequeue() == 30); 121 | assert(q.dequeue() == 900); 122 | assert(q.empty); 123 | 124 | q.free(); 125 | } 126 | -------------------------------------------------------------------------------- /dlib/audio/unmanaged.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2016-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * GC-free sound class 31 | * 32 | * Copyright: Timur Gafarov 2016-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.audio.unmanaged; 37 | 38 | import dlib.core.memory; 39 | import dlib.audio.sample; 40 | import dlib.audio.sound; 41 | 42 | /** 43 | * An implementation of GenericSound that doesn't use garbage collector 44 | */ 45 | class UnmanagedGenericSound: GenericSound 46 | { 47 | this(Sound ras) 48 | { 49 | super(ras); 50 | } 51 | 52 | this(double dur, 53 | uint freq, 54 | uint numChannels, 55 | SampleFormat f) 56 | { 57 | super(dur, freq, numChannels, f); 58 | } 59 | 60 | this(size_t dataSize, 61 | ulong numSamples, 62 | double dur, 63 | uint numChannels, 64 | uint freq, 65 | uint bitdepth, 66 | SampleFormat f) 67 | { 68 | super(dataSize, numSamples, dur, numChannels, freq, bitdepth, f); 69 | } 70 | 71 | protected override void allocateData(size_t size) 72 | { 73 | _data = New!(ubyte[])(size); 74 | } 75 | 76 | override @property Sound dup() 77 | { 78 | return New!UnmanagedGenericSound(this); 79 | } 80 | 81 | override Sound createSameFormat(uint ch, double dur) 82 | { 83 | return New!UnmanagedGenericSound(dur, _sampleRate, ch, _format); 84 | } 85 | 86 | ~this() 87 | { 88 | if (_data) 89 | Delete(_data); 90 | } 91 | } 92 | 93 | /** 94 | * GenericSoundFactory implementation for UnmanagedGenericSound class 95 | */ 96 | class UnmanagedGenericSoundFactory: GenericSoundFactory 97 | { 98 | UnmanagedGenericSound createSound( 99 | size_t dataSize, 100 | ulong numSamples, 101 | double dur, 102 | uint numChannels, 103 | uint freq, 104 | uint bitdepth, 105 | SampleFormat f) 106 | { 107 | return New!UnmanagedGenericSound( 108 | dataSize, 109 | numSamples, 110 | dur, 111 | numChannels, 112 | freq, 113 | bitdepth, 114 | f 115 | ); 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /dlib/math/fft.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2013-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Fast Fourier transform 31 | * 32 | * Copyright: Timur Gafarov 2013-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.math.fft; 37 | 38 | import std.math; 39 | import dlib.math.utils; 40 | import dlib.math.complex; 41 | 42 | /// Forward or backward fast Fourier transform. Data must be power of two in length 43 | void fastFourierTransform(Complexf[] data, bool forward) 44 | { 45 | assert(isPowerOfTwo(data.length)); 46 | 47 | uint target = 0; 48 | 49 | for (uint pos = 0; pos < data.length; ++pos) 50 | { 51 | if (target > pos) 52 | { 53 | Complexf temp = data[target]; 54 | data[target] = data[pos]; 55 | data[pos] = temp; 56 | } 57 | uint mask = cast(uint)data.length; 58 | while (target & (mask >>= 1)) 59 | target &= ~mask; 60 | target |= mask; 61 | } 62 | 63 | float pi = forward? -PI : PI; 64 | 65 | for (uint step = 1; step < data.length; step <<= 1) 66 | { 67 | uint jump = step << 1; 68 | float delta = pi / cast(float)step; 69 | float sine = sin(delta * 0.5f); 70 | Complexf multiplier = Complexf(-2.0f * sine * sine, sin(delta)); 71 | Complexf factor = Complexf(1.0f); 72 | 73 | for (uint group = 0; group < step; ++group) 74 | { 75 | for (uint pair = group; pair < data.length; pair += jump) 76 | { 77 | uint match = pair + step; 78 | Complexf product = factor * data[match]; 79 | data[match] = data[pair] - product; 80 | data[pair] += product; 81 | } 82 | 83 | factor = multiplier * factor + factor; 84 | } 85 | } 86 | } 87 | 88 | /// 89 | unittest 90 | { 91 | Complexf[4] data = 92 | [ 93 | Complexf(1.0f, 0.0f), 94 | Complexf(2.0f, 0.0f), 95 | Complexf(3.0f, 0.0f), 96 | Complexf(4.0f, 0.0f) 97 | ]; 98 | 99 | fastFourierTransform(data, true); 100 | 101 | assert(data[0].toString == "10 + 0i"); 102 | assert(data[1].toString == "-2 + 2i"); 103 | assert(data[2].toString == "-2 + 0i"); 104 | assert(data[3].toString == "-2 + -2i"); 105 | } 106 | -------------------------------------------------------------------------------- /dlib/image/filters/binarization.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2018-2025 Oleg Baharev, Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Image binarization 31 | * 32 | * Copyright: Oleg Baharev, Timur Gafarov 2018-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Oleg Baharev, Timur Gafarov 35 | */ 36 | module dlib.image.filters.binarization; 37 | 38 | import dlib.image.image; 39 | import dlib.image.color; 40 | import dlib.image.filters.histogram; 41 | 42 | int otsuThreshold(SuperImage img) 43 | { 44 | auto histogram = createHistogram(img); 45 | 46 | int sumOfLuminances; 47 | 48 | foreach (x; 0..img.width) 49 | foreach (y; 0..img.height) 50 | { 51 | sumOfLuminances += cast(int)(img[x, y].luminance * 255); 52 | } 53 | 54 | auto allPixelCount = cast(double)(img.width * img.height); 55 | 56 | int bestThreshold = 0; 57 | int firstClassPixelCount = 0; 58 | int firstClassLuminanceSum = 0; 59 | 60 | double bestSigma = 0.0; 61 | 62 | for (int threshold = 0; threshold < 255; threshold++) 63 | { 64 | firstClassPixelCount += histogram[threshold]; 65 | firstClassLuminanceSum += threshold * histogram[threshold]; 66 | 67 | double firstClassProbability = firstClassPixelCount / allPixelCount; 68 | double secondClassProbability = 1.0 - firstClassProbability; 69 | 70 | double firstClassMean = (firstClassPixelCount == 0) ? 0 : firstClassLuminanceSum / firstClassPixelCount; 71 | double secondClassMean = (sumOfLuminances - firstClassLuminanceSum) / (allPixelCount - firstClassPixelCount); 72 | 73 | double meanDelta = firstClassMean - secondClassMean; 74 | double sigma = firstClassProbability * secondClassProbability * meanDelta * meanDelta; 75 | 76 | if (sigma > bestSigma) 77 | { 78 | bestSigma = sigma; 79 | bestThreshold = threshold; 80 | } 81 | } 82 | 83 | return bestThreshold; 84 | } 85 | 86 | /// Otsu binarization 87 | auto otsuBinarization(SuperImage img) 88 | { 89 | SuperImage res = img.createSameFormat(img.width, img.height); 90 | auto threshold = otsuThreshold(img); 91 | 92 | foreach (x; 0..img.width) 93 | foreach (y; 0..img.height) 94 | { 95 | auto luminance = cast(int)(img[x,y].luminance * 255); 96 | 97 | if (luminance > threshold) 98 | res[x, y] = Color4f(1.0f, 1.0f, 1.0f, 1.0f); 99 | else 100 | res[x, y] = Color4f(0.0f, 0.0f, 0.0f, 1.0f); 101 | } 102 | 103 | return res; 104 | } 105 | -------------------------------------------------------------------------------- /dlib/audio/sample.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2016-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Sample types and conversion utilities 31 | * 32 | * Copyright: Timur Gafarov 2016-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.audio.sample; 37 | 38 | /** 39 | * dlib.audio defines four integer sample formats: signed 8-bit, signed 16-bit, 40 | * unsigned 8-bit, unsigned 16-bit. 41 | * They are for storage only. All sound processing and sample I/O 42 | * should be done in floating point numbers. Floating point sample is signed and 43 | * ranges from -1.0f to 1.0f. 44 | */ 45 | enum SampleFormat 46 | { 47 | /// signed 8-bit 48 | S8, 49 | /// signed 16-bit 50 | S16, 51 | /// unsigned 8-bit 52 | U8, 53 | /// unsigned 16-bit 54 | U16 55 | } 56 | 57 | /** 58 | * Convert integer sample to floating point sample 59 | */ 60 | float toFloatSample(ubyte* ptr, SampleFormat format) 61 | { 62 | float res; 63 | switch (format) 64 | { 65 | case SampleFormat.S8: 66 | res = *cast(byte*)ptr; 67 | res /= byte.max; 68 | break; 69 | case SampleFormat.S16: 70 | res = *cast(short*)ptr; 71 | res /= short.max; 72 | break; 73 | case SampleFormat.U8: 74 | res = *ptr; 75 | res /= ubyte.max; 76 | res = res * 2.0f - 1.0f; // normalize to range -1..1 77 | break; 78 | case SampleFormat.U16: 79 | res = *cast(ushort*)ptr; 80 | res /= ushort.max; 81 | res = res * 2.0f - 1.0f; // normalize to range -1..1 82 | break; 83 | default: 84 | break; 85 | } 86 | return res; 87 | } 88 | 89 | /** 90 | * Convert floating point sample to integer sample 91 | */ 92 | void fromFloatSample(ubyte* ptr, SampleFormat format, float s) 93 | { 94 | switch (format) 95 | { 96 | case SampleFormat.S8: 97 | *cast(byte*)ptr = cast(byte)(s * byte.max); 98 | break; 99 | case SampleFormat.S16: 100 | *cast(short*)ptr = cast(short)(s * short.max); 101 | break; 102 | case SampleFormat.U8: 103 | *cast(ubyte*)ptr = cast(ubyte)((s + 1.0f) * 0.5f * ubyte.max); 104 | break; 105 | case SampleFormat.U16: 106 | *cast(ushort*)ptr = cast(ushort)((s + 1.0f) * 0.5f * ushort.max); 107 | break; 108 | default: 109 | break; 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /dlib/container/stack.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Stack (based on Array) 31 | * 32 | * Copyright: Timur Gafarov 2011-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov, Andrey Penechko, Roman Chistokhodov 35 | */ 36 | module dlib.container.stack; 37 | 38 | import dlib.container.array; 39 | 40 | /** 41 | * Stack implementation based on Array. 42 | */ 43 | struct Stack(T) 44 | { 45 | private Array!T array; 46 | 47 | public: 48 | /** 49 | * Push element to stack. 50 | */ 51 | void push(T v) 52 | { 53 | array.insertBack(v); 54 | } 55 | 56 | /** 57 | * Pop top element out. 58 | * Returns: Removed element. 59 | * Throws: Exception on underflow. 60 | */ 61 | T pop() 62 | { 63 | if (empty) 64 | throw new Exception("Stack!(T): underflow"); 65 | 66 | T res = array[$-1]; 67 | array.removeBack(1); 68 | return res; 69 | } 70 | 71 | /** 72 | * Non-throwing version of pop. 73 | * Returns: true on success, false on failure. 74 | * Element is stored in value. 75 | */ 76 | bool pop(ref T value) 77 | { 78 | if (empty) 79 | return false; 80 | 81 | value = array[$-1]; 82 | array.removeBack(1); 83 | return true; 84 | } 85 | 86 | /** 87 | * Top stack element. 88 | * Note: Stack must be non-empty. 89 | */ 90 | T top() 91 | { 92 | return array[$-1]; 93 | } 94 | 95 | /** 96 | * Pointer to top stack element. 97 | * Note: Stack must be non-empty. 98 | */ 99 | T* topPtr() 100 | { 101 | return &array.data[$-1]; 102 | } 103 | 104 | /** 105 | * Check if stack has no elements. 106 | */ 107 | @property bool empty() nothrow 108 | { 109 | return array.length == 0; 110 | } 111 | 112 | /** 113 | * Free memory allocated by Stack. 114 | */ 115 | void free() 116 | { 117 | array.free(); 118 | } 119 | } 120 | 121 | /// 122 | unittest 123 | { 124 | import std.exception: assertThrown; 125 | 126 | Stack!int s; 127 | assertThrown(s.pop()); 128 | s.push(100); 129 | s.push(3); 130 | s.push(76); 131 | assert(s.top() == 76); 132 | int v; 133 | s.pop(v); 134 | assert(v == 76); 135 | assert(s.pop() == 3); 136 | assert(s.pop() == 100); 137 | assert(s.empty); 138 | s.free(); 139 | } 140 | -------------------------------------------------------------------------------- /dlib/image/resampling/bicubic.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Bicubic resampling 31 | * 32 | * Copyright: Timur Gafarov 2011-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.image.resampling.bicubic; 37 | 38 | import std.math; 39 | import dlib.image.image; 40 | import dlib.image.color; 41 | 42 | T bicubic(T) (T x) 43 | { 44 | if (x > 2.0) 45 | return 0.0; 46 | 47 | T a, b, c, d; 48 | T xm1 = x - 1.0; 49 | T xp1 = x + 1.0; 50 | T xp2 = x + 2.0; 51 | 52 | a = ( xp2 <= 0.0 ) ? 0.0 : xp2 * xp2 * xp2; 53 | b = ( xp1 <= 0.0 ) ? 0.0 : xp1 * xp1 * xp1; 54 | c = ( x <= 0.0 ) ? 0.0 : x * x * x; 55 | d = ( xm1 <= 0.0 ) ? 0.0 : xm1 * xm1 * xm1; 56 | 57 | return ( 0.16666666666666666667 * 58 | ( a - ( 4.0 * b ) + ( 6.0 * c ) - ( 4.0 * d ) ) ); 59 | } 60 | 61 | /// Resize image with bicubic filter 62 | SuperImage resampleBicubic(SuperImage img, uint newWidth, uint newHeight) 63 | in 64 | { 65 | assert (img.data.length); 66 | } 67 | do 68 | { 69 | SuperImage res = img.createSameFormat(newWidth, newHeight); 70 | 71 | float xFactor = cast(float)img.width / cast(float)newWidth; 72 | float yFactor = cast(float)img.height / cast(float)newHeight; 73 | 74 | foreach(x; 0..res.width) 75 | { 76 | float ox = x * xFactor - 0.5f; 77 | int ox1 = cast(int)ox; 78 | float dx = ox - ox1; 79 | 80 | foreach(y; 0..res.height) 81 | { 82 | float oy = y * yFactor - 0.5f; 83 | int oy1 = cast(int)oy; 84 | float dy = oy - oy1; 85 | 86 | Color4f colSum = Color4f(0, 0, 0); 87 | 88 | foreach(kx; -1..3) 89 | { 90 | int ix = ox1 + kx; 91 | 92 | if (ix < 0) ix = 0; 93 | if (ix >= img.width) ix = img.width - 1; 94 | 95 | foreach(ky; -1..3) 96 | { 97 | int iy = oy1 + ky; 98 | 99 | if (iy < 0) iy = 0; 100 | if (iy >= img.height) iy = img.height - 1; 101 | 102 | auto col = img[ix, iy]; 103 | 104 | float k1 = bicubic(dy - cast(float)ky); 105 | float k2 = k1 * bicubic(cast(float)kx - dx); 106 | 107 | colSum += col * k2; 108 | } 109 | } 110 | 111 | res[x, y] = colSum; 112 | } 113 | } 114 | 115 | return res; 116 | } 117 | -------------------------------------------------------------------------------- /dlib/image/resampling/lanczos.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Lanczos resampling 31 | * 32 | * Copyright: Timur Gafarov 2011-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.image.resampling.lanczos; 37 | 38 | import std.math; 39 | import dlib.image.image; 40 | import dlib.image.color; 41 | 42 | T lanczos(T) (T x, int filterSize) 43 | { 44 | if (x <= -filterSize || x >= filterSize) 45 | return 0.0; // Outside of the window 46 | if (x > -T.epsilon && x < T.epsilon) 47 | return 1.0; // Special case the discontinuity at the origin 48 | 49 | auto sinc = (T x) => sin(PI * x) / (PI * x); 50 | return sinc(x) * sinc(x / filterSize); 51 | } 52 | 53 | /// Resize image with Lanczos filter 54 | SuperImage resampleLanczos(SuperImage img, in uint newWidth, in uint newHeight) 55 | in 56 | { 57 | assert (img.data.length); 58 | } 59 | do 60 | { 61 | SuperImage res = img.createSameFormat(newWidth, newHeight); 62 | 63 | float xFactor = cast(float)img.width / cast(float)newWidth; 64 | float yFactor = cast(float)img.height / cast(float)newHeight; 65 | 66 | foreach(x; 0..res.width) 67 | { 68 | float ox = x * xFactor - 0.5f; 69 | int ox1 = cast(int)ox; 70 | float dx = ox - ox1; 71 | 72 | foreach(y; 0..res.height) 73 | { 74 | float oy = y * yFactor - 0.5f; 75 | int oy1 = cast(int)oy; 76 | float dy = oy - oy1; 77 | 78 | Color4f colSum = Color4f(0, 0, 0); 79 | float kSum; 80 | 81 | foreach(kx; -3..4) 82 | { 83 | int ix = ox1 + kx; 84 | 85 | if (ix < 0) ix = 0; 86 | if (ix >= img.width) ix = img.width - 1; 87 | 88 | foreach(ky; -3..4) 89 | { 90 | int iy = oy1 + ky; 91 | 92 | if (iy < 0) iy = 0; 93 | if (iy >= img.height) iy = img.height - 1; 94 | 95 | auto col = img[ix, iy]; 96 | 97 | float k1 = lanczos((cast(float)ky - dy), 3); 98 | float k2 = k1 * lanczos((cast(float)kx - dx), 3); 99 | 100 | kSum += k2; 101 | 102 | colSum += col * k2; 103 | } 104 | } 105 | 106 | if (kSum > 0.0f) 107 | colSum /= kSum; 108 | 109 | res[x, y] = colSum; 110 | } 111 | } 112 | 113 | return res; 114 | } 115 | -------------------------------------------------------------------------------- /dlib/filesystem/posix/file.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2014-2025 Martin Cejp 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | module dlib.filesystem.posix.file; 30 | 31 | version (Posix) 32 | { 33 | import dlib.core.stream; 34 | import dlib.core.memory; 35 | import dlib.filesystem.filesystem; 36 | import dlib.filesystem.posix.common; 37 | 38 | static import core.sys.posix.unistd; 39 | 40 | class PosixFile: IOStream 41 | { 42 | int fd; 43 | uint accessFlags; 44 | bool eof = false; 45 | 46 | this(int fd, uint accessFlags) 47 | { 48 | this.fd = fd; 49 | this.accessFlags = accessFlags; 50 | } 51 | 52 | ~this() 53 | { 54 | close(); 55 | } 56 | 57 | override void close() 58 | { 59 | if (fd != -1) 60 | { 61 | core.sys.posix.unistd.close(fd); 62 | fd = -1; 63 | } 64 | } 65 | 66 | override bool seekable() 67 | { 68 | return true; 69 | } 70 | 71 | override StreamPos getPosition() 72 | { 73 | import core.sys.posix.stdio; 74 | 75 | return lseek(fd, 0, SEEK_CUR); 76 | } 77 | 78 | override bool setPosition(StreamPos pos) 79 | { 80 | import core.sys.posix.stdio; 81 | 82 | return lseek(fd, pos, SEEK_SET) == pos; 83 | } 84 | 85 | override StreamSize size() 86 | { 87 | import core.sys.posix.stdio; 88 | 89 | auto off = lseek(fd, 0, SEEK_CUR); 90 | auto end = lseek(fd, 0, SEEK_END); 91 | lseek(fd, off, SEEK_SET); 92 | return end; 93 | } 94 | 95 | override bool readable() 96 | { 97 | return fd != -1 && (accessFlags & FileSystem.read) && !eof; 98 | } 99 | 100 | override size_t readBytes(void* buffer, size_t count) 101 | { 102 | immutable size_t got = core.sys.posix.unistd.read(fd, buffer, count); 103 | 104 | if (count > got) 105 | eof = true; 106 | 107 | return got; 108 | } 109 | 110 | override bool writeable() 111 | { 112 | return fd != -1 && (accessFlags & FileSystem.write); 113 | } 114 | 115 | override size_t writeBytes(const void* buffer, size_t count) 116 | { 117 | return core.sys.posix.unistd.write(fd, buffer, count); 118 | } 119 | 120 | override void flush() 121 | { 122 | } 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /dlib/math/linsolve.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2013-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Linear equation system solvers 31 | * 32 | * Description: 33 | * A system is given in matrix form: 34 | * --- 35 | * Ax = b 36 | * --- 37 | * For example: 38 | * --- 39 | * x + 3y - 2z = 5 40 | * 3x + 5y + 6z = 7 41 | * 2x + 4y + 3z = 8 42 | * --- 43 | * For this system, A (coefficient matrix) will be 44 | * --- 45 | * [1, 3, -2] 46 | * [3, 5, 6] 47 | * [2, 4, 3] 48 | * --- 49 | * And b (right side vector) will be 50 | * --- 51 | * [5, 7, 8] 52 | * --- 53 | * x is a vector of unknowns: 54 | * --- 55 | * [x, y, z] 56 | * --- 57 | * 58 | * Copyright: Timur Gafarov 2013-2025. 59 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 60 | * Authors: Timur Gafarov 61 | */ 62 | module dlib.math.linsolve; 63 | 64 | import std.math; 65 | import dlib.math.matrix; 66 | import dlib.math.vector; 67 | import dlib.math.decomposition; 68 | 69 | // TODO: use arrays instead of Matrix structs to support big systems stored in heap 70 | 71 | /// Solve Ax = b using LUP decomposition 72 | void solve(T, size_t N)( 73 | Matrix!(T,N) a, 74 | ref Vector!(T,N) x, 75 | Vector!(T,N) b) 76 | { 77 | Matrix!(T,N) L, U, P; 78 | decomposeLUP(a, L, U, P); 79 | solveLU(L, U, x, b * P); 80 | } 81 | 82 | /// 83 | unittest 84 | { 85 | bool isConsiderZeroTolerant(T) (T f) nothrow 86 | { 87 | return (abs(f) < 0.0001f); 88 | } 89 | 90 | Matrix3f a = matrixf( 91 | 1, 3, -2, 92 | 3, 5, 6, 93 | 2, 4, 3 94 | ); 95 | Vector3f b = Vector3f(5, 7, 8); 96 | Vector3f x = Vector3f(0, 0, 0); 97 | 98 | solve(a, x, b); 99 | 100 | assert(isConsiderZeroTolerant(-15 - x[0])); 101 | assert(isConsiderZeroTolerant(8 - x[1])); 102 | assert(isConsiderZeroTolerant(2 - x[2])); 103 | } 104 | 105 | /// Solve LUx = b 106 | void solveLU(T, size_t N)( 107 | Matrix!(T,N) L, 108 | Matrix!(T,N) U, 109 | ref Vector!(T,N) x, 110 | Vector!(T,N) b) 111 | { 112 | int i = 0; 113 | int j = 0; 114 | Vector!(T,N) y; 115 | 116 | // Forward solve Ly = b 117 | for (i = 0; i < N; i++) 118 | { 119 | y[i] = b[i]; 120 | for (j = 0; j < i; j++) 121 | y[i] -= L[i, j] * y[j]; 122 | y[i] /= L[i, i]; 123 | } 124 | 125 | // Backward solve Ux = y 126 | for (i = N - 1; i >= 0; i--) 127 | { 128 | x[i] = y[i]; 129 | for (j = i + 1; j < N; j++) 130 | x[i] -= U[i, j] * x[j]; 131 | x[i] /= U[i, i]; 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /dlib/image/filters/normalmap.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Normal map generation 31 | * 32 | * Copyright: Timur Gafarov 2011-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.image.filters.normalmap; 37 | 38 | import dlib.image.image; 39 | import dlib.image.color; 40 | import dlib.math.vector; 41 | 42 | /// Generate normal map from height map using Sobel operator 43 | SuperImage heightToNormal( 44 | SuperImage img, 45 | SuperImage outp, 46 | Channel channel = Channel.R, 47 | float strength = 2.0f) 48 | in 49 | { 50 | assert (img.data.length); 51 | } 52 | do 53 | { 54 | // TODO: optionally transfer height data to alpha channel 55 | SuperImage res; 56 | if (outp) 57 | res = outp; 58 | else 59 | res = img.dup; 60 | 61 | if (img.channels == 1) 62 | channel = Channel.R; 63 | 64 | float[8] sobelTaps; 65 | 66 | foreach(y; 0..img.height) 67 | foreach(x; 0..img.width) 68 | { 69 | sobelTaps[0] = img[x-1, y-1][channel]; 70 | sobelTaps[1] = img[x, y-1][channel]; 71 | sobelTaps[2] = img[x+1, y-1][channel]; 72 | sobelTaps[3] = img[x-1, y+1][channel]; 73 | sobelTaps[4] = img[x, y+1][channel]; 74 | sobelTaps[5] = img[x+1, y+1][channel]; 75 | sobelTaps[6] = img[x-1, y ][channel]; 76 | sobelTaps[7] = img[x+1, y ][channel]; 77 | 78 | float dx, dy; 79 | 80 | // Do y sobel filter 81 | dy = sobelTaps[0] * +1.0f; 82 | dy += sobelTaps[1] * +2.0f; 83 | dy += sobelTaps[2] * +1.0f; 84 | dy += sobelTaps[3] * -1.0f; 85 | dy += sobelTaps[4] * -2.0f; 86 | dy += sobelTaps[5] * -1.0f; 87 | 88 | // Do x sobel filter 89 | dx = sobelTaps[0] * -1.0f; 90 | dx += sobelTaps[6] * -2.0f; 91 | dx += sobelTaps[3] * -1.0f; 92 | dx += sobelTaps[2] * +1.0f; 93 | dx += sobelTaps[7] * +2.0f; 94 | dx += sobelTaps[5] * +1.0f; 95 | 96 | // pack normal into floating-point RGBA 97 | Vector3f normal = Vector3f(-dx, -dy, 1.0f / strength); 98 | Color4f col = packNormal(normal); 99 | col.a = 1.0f; 100 | 101 | // write result 102 | res[x, y] = col; 103 | } 104 | 105 | return res; 106 | } 107 | 108 | /// ditto 109 | SuperImage heightToNormal( 110 | SuperImage img, 111 | Channel channel = Channel.R, 112 | float strength = 2.0f) 113 | { 114 | return heightToNormal(img, null, channel, strength); 115 | } 116 | -------------------------------------------------------------------------------- /dlib/coding/varint.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2015-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Variable-sized integer type 31 | * 32 | * Copyright: Timur Gafarov 2015-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.coding.varint; 37 | 38 | /** 39 | * Protobuf-style variable-sized integer 40 | */ 41 | struct Varint 42 | { 43 | ubyte[8] buffer; 44 | size_t size; 45 | 46 | /// Returns bytes of an integer 47 | ubyte[] bytes() return 48 | { 49 | return buffer[0..size]; 50 | } 51 | } 52 | 53 | /** 54 | * Returns size in bytes necessary to store an integer 55 | */ 56 | size_t getVarintSize(ulong n) 57 | { 58 | enum ulong N1 = 128; 59 | enum ulong N2 = 16384; 60 | enum ulong N3 = 2097152; 61 | enum ulong N4 = 268435456; 62 | enum ulong N5 = 34359738368; 63 | enum ulong N6 = 4398046511104; 64 | enum ulong N7 = 562949953421312; 65 | enum ulong N8 = 72057594037927936; 66 | 67 | return ( 68 | n < N1 ? 1 69 | : n < N2 ? 2 70 | : n < N3 ? 3 71 | : n < N4 ? 4 72 | : n < N5 ? 5 73 | : n < N6 ? 6 74 | : n < N7 ? 7 75 | : 8 76 | ); 77 | } 78 | 79 | /// 80 | unittest 81 | { 82 | assert(getVarintSize(4637734) == 4); 83 | } 84 | 85 | /** 86 | * Encode a fixed-size integer to Varint 87 | */ 88 | Varint encodeVarint(ulong n) 89 | { 90 | Varint res; 91 | res.size = getVarintSize(n); 92 | ubyte* ptr = res.buffer.ptr; 93 | 94 | for (uint i = 0; i < res.size; i++) 95 | { 96 | if (i == res.size - 1) 97 | *(ptr++) = n & 0xFF; 98 | else 99 | *(ptr++) = cast(ubyte)(n | 0x80); 100 | n >>= 7; 101 | } 102 | 103 | return res; 104 | } 105 | 106 | /// 107 | unittest 108 | { 109 | Varint v = encodeVarint(4783); 110 | assert(v.size == 2); 111 | assert(v.bytes == [175, 37]); 112 | } 113 | 114 | /** 115 | * Decode Varing to fixed-size integer 116 | */ 117 | ulong decodeVarint(Varint vint) 118 | { 119 | ulong result = 0; 120 | int bits = 0; 121 | ubyte* ptr = vint.buffer.ptr; 122 | ulong ll; 123 | 124 | while (*ptr & 0x80) 125 | { 126 | ll = *ptr; 127 | result += ((ll & 0x7F) << bits); 128 | ptr++; 129 | bits += 7; 130 | } 131 | 132 | ll = *ptr; 133 | result += ((ll & 0x7F) << bits); 134 | 135 | return result; 136 | } 137 | 138 | /// 139 | unittest 140 | { 141 | Varint v = encodeVarint(2345); 142 | assert(decodeVarint(v) == 2345); 143 | } 144 | -------------------------------------------------------------------------------- /dlib/image/resampling/bilinear.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Bilinear resampling 31 | * 32 | * Copyright: Timur Gafarov 2011-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.image.resampling.bilinear; 37 | 38 | import std.math; 39 | import dlib.image.image; 40 | import dlib.image.color; 41 | 42 | /// Resize image with bilinear filter 43 | SuperImage resampleBilinear(SuperImage img, in uint newWidth, in uint newHeight) 44 | in 45 | { 46 | assert (img.data.length); 47 | } 48 | do 49 | { 50 | SuperImage res = img.createSameFormat(newWidth, newHeight); 51 | 52 | float xFactor = cast(float)img.width / cast(float)newWidth; 53 | float yFactor = cast(float)img.height / cast(float)newHeight; 54 | 55 | int floor_x, floor_y, ceil_x, ceil_y; 56 | float fraction_x, fraction_y, one_minus_x, one_minus_y; 57 | 58 | Color4f c1, c2, c3, c4; 59 | Color4f col; 60 | float b1, b2; 61 | 62 | foreach(y; 0..res.height) 63 | foreach(x; 0..res.width) 64 | { 65 | floor_x = cast(int)floor(x * xFactor); 66 | floor_y = cast(int)floor(y * yFactor); 67 | 68 | ceil_x = floor_x + 1; 69 | if (ceil_x >= img.width) 70 | ceil_x = floor_x; 71 | 72 | ceil_y = floor_y + 1; 73 | if (ceil_y >= img.height) 74 | ceil_y = floor_y; 75 | 76 | fraction_x = x * xFactor - floor_x; 77 | fraction_y = y * yFactor - floor_y; 78 | one_minus_x = 1.0f - fraction_x; 79 | one_minus_y = 1.0f - fraction_y; 80 | 81 | c1 = img[floor_x, floor_y]; 82 | c2 = img[ceil_x, floor_y]; 83 | c3 = img[floor_x, ceil_y]; 84 | c4 = img[ceil_x, ceil_y]; 85 | 86 | // Red 87 | b1 = one_minus_x * c1.r + fraction_x * c2.r; 88 | b2 = one_minus_x * c3.r + fraction_x * c4.r; 89 | col.r = one_minus_y * b1 + fraction_y * b2; 90 | 91 | // Green 92 | b1 = one_minus_x * c1.g + fraction_x * c2.g; 93 | b2 = one_minus_x * c3.g + fraction_x * c4.g; 94 | col.g = one_minus_y * b1 + fraction_y * b2; 95 | 96 | // Blue 97 | b1 = one_minus_x * c1.b + fraction_x * c2.b; 98 | b2 = one_minus_x * c3.b + fraction_x * c4.b; 99 | col.b = one_minus_y * b1 + fraction_y * b2; 100 | 101 | // Alpha 102 | b1 = one_minus_x * c1.a + fraction_x * c2.a; 103 | b2 = one_minus_x * c3.a + fraction_x * c4.a; 104 | col.a = one_minus_y * b1 + fraction_y * b2; 105 | 106 | res[x, y] = col; 107 | } 108 | 109 | return res; 110 | } 111 | -------------------------------------------------------------------------------- /dlib/math/hof.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011-2025 Timur Gafarov 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Functions that return other functions 31 | * 32 | * Copyright: Timur Gafarov 2011-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Timur Gafarov 35 | */ 36 | module dlib.math.hof; 37 | 38 | /** 39 | * Functional composition. 40 | * Description: 41 | * Returns a function that applies function f to the return value of function g 42 | */ 43 | T delegate(S) compose(T, U, S)(T function(U) f, U function(S) g) 44 | { 45 | return (S s) => f(g(s)); 46 | } 47 | 48 | /** 49 | Y combinator 50 | Description: 51 | We're all familiar with the idea of a function as something that takes some 52 | input value and returns some output value. Say, the function for squaring numbers: 53 | --- 54 | f(x) = x*x; 55 | --- 56 | The fixed points of a function are any input values for which f(x) is equal to x. 57 | So, the fixed points of f(x) = x*x are 0 and 1. 58 | 59 | Now, we have things called higher-order functions. These are functions that take another 60 | function as input, or return a function as output, or both. 61 | 62 | The fixed point of a higher-order function f is another function p such that f(p) = p. 63 | It may be more helpful to think in terms of functions actually being executed. 64 | The previous statement is equivalent to the statement that f(p)(x) = p(x) for all values of x. 65 | 66 | Y (the Y combinator) is a special function that returns the fixed points of higher-order 67 | functions, that is to say: 68 | --- 69 | f(Y(f)) = Y(f) 70 | --- 71 | Y combinator is commonly use to allow anonymous recursion without assuming your host 72 | language supports it. 73 | */ 74 | auto Y(R, P...) (R delegate(P) delegate(R delegate(P)) lambda) 75 | { 76 | struct RFunc {R delegate(P) delegate(RFunc) f;} 77 | auto r = RFunc((RFunc w) => lambda((P x) => w.f(w)(x))); 78 | return r.f(r); 79 | } 80 | 81 | /// 82 | unittest 83 | { 84 | import std.algorithm; 85 | import std.range; 86 | 87 | auto factorial = Y((int delegate(int) self) => 88 | (int n) => 0 == n ? 1 : n * self(n - 1)); 89 | 90 | auto ackermann = Y((ulong delegate(ulong, ulong) self) => 91 | (ulong m, ulong n) => (!m && n)? 92 | (n+1) : (m && !n)? self(m-1, 1) : self(m-1, self(m, n-1))); 93 | 94 | auto qsort = Y((int[] delegate(int[]) self) => 95 | (int[] arr) => arr.length? 96 | self(arr.filter!(a => a < arr[0]).array) 97 | ~ arr[0] 98 | ~ self(arr.filter!(a => a > arr[0]).array) : []); 99 | 100 | assert(factorial(6) == 720); 101 | assert(ackermann(3, 5) == 253); 102 | assert(qsort([8, 5, 10, 2, 16, 9, 1, 100, 3]) 103 | == [1, 2, 3, 5, 8, 9, 10, 16, 100]); 104 | } 105 | -------------------------------------------------------------------------------- /dlib/filesystem/windows/directory.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2014-2025 Martin Cejp 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | module dlib.filesystem.windows.directory; 30 | 31 | version(Windows) 32 | { 33 | import dlib.filesystem.filesystem; 34 | import dlib.filesystem.dirrange; 35 | import dlib.filesystem.windows.common; 36 | 37 | import std.conv; 38 | import std.range; 39 | 40 | class WindowsDirectory: Directory 41 | { 42 | FileSystem fs; 43 | HANDLE find = INVALID_HANDLE_VALUE; 44 | string prefix; 45 | 46 | WIN32_FIND_DATAW entry; 47 | bool entryValid = false; 48 | 49 | this(FileSystem fs, string path, string prefix) 50 | { 51 | this.fs = fs; 52 | this.prefix = prefix; 53 | 54 | find = FindFirstFileW(toUTF16z(path ~ `\*.*`), &entry); 55 | 56 | if (find != INVALID_HANDLE_VALUE) 57 | entryValid = true; 58 | } 59 | 60 | ~this() 61 | { 62 | close(); 63 | } 64 | 65 | void close() 66 | { 67 | if (find != INVALID_HANDLE_VALUE) 68 | { 69 | FindClose(find); 70 | find = INVALID_HANDLE_VALUE; 71 | } 72 | } 73 | 74 | InputRange!DirEntry contents() 75 | { 76 | return new DirRange(delegate bool(out DirEntry de) 77 | { 78 | for (;;) 79 | { 80 | WIN32_FIND_DATAW* entry = nextEntry(); 81 | 82 | if (entry == null) 83 | return false; 84 | else 85 | { 86 | size_t len = wcslen(entry.cFileName.ptr); 87 | string name = to!string(entry.cFileName[0..len]); 88 | 89 | if (name == "." || name == "..") 90 | continue; 91 | 92 | de.name = name; 93 | 94 | if (entry.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 95 | de.isDirectory = true; 96 | else 97 | de.isFile = true; 98 | 99 | return true; 100 | } 101 | } 102 | }); 103 | } 104 | 105 | private WIN32_FIND_DATAW* nextEntry() 106 | { 107 | if (entryValid) 108 | { 109 | entryValid = false; 110 | return &entry; 111 | } 112 | 113 | if (find == INVALID_HANDLE_VALUE || !FindNextFileW(find, &entry)) 114 | return null; 115 | 116 | return &entry; 117 | } 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /dlib/memory/allocator.d: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2016-2025 Eugene Wissner 3 | 4 | Boost Software License - Version 1.0 - August 17th, 2003 5 | 6 | Permission is hereby granted, free of charge, to any person or organization 7 | obtaining a copy of the software and accompanying documentation covered by 8 | this license (the "Software") to use, reproduce, display, distribute, 9 | execute, and transmit the Software, and to prepare derivative works of the 10 | Software, and to permit third-parties to whom the Software is furnished to 11 | do so, all subject to the following: 12 | 13 | The copyright notices in the Software and this entire statement, including 14 | the above license grant, this restriction and the following disclaimer, 15 | must be included in all copies of the Software, in whole or in part, and 16 | all derivative works of the Software, unless such copies or derivative 17 | works are solely in the form of machine-executable object code generated by 18 | a source language processor. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | /** 30 | * Abstract allocator interface 31 | * 32 | * Copyright: Eugene Wissner 2016-2025. 33 | * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 | * Authors: Eugene Wissner 35 | */ 36 | module dlib.memory.allocator; 37 | 38 | version (unittest) 39 | { 40 | import dlib.memory : defaultAllocator; 41 | } 42 | 43 | /** 44 | * Allocator interface. 45 | */ 46 | interface Allocator 47 | { 48 | /** 49 | * Allocates $(D_PARAM size) bytes of memory. 50 | * 51 | * Params: 52 | * size = Amount of memory to allocate. 53 | * 54 | * Returns: The pointer to the new allocated memory. 55 | */ 56 | void[] allocate(size_t size); 57 | 58 | /** 59 | * Deallocates a memory block. 60 | * 61 | * Params: 62 | * p = A pointer to the memory block to be freed. 63 | * 64 | * Returns: Whether the deallocation was successful. 65 | */ 66 | bool deallocate(void[] p); 67 | 68 | /** 69 | * Increases or decreases the size of a memory block. 70 | * 71 | * Params: 72 | * p = A pointer to the memory block. 73 | * size = Size of the reallocated block. 74 | * 75 | * Returns: Whether the reallocation was successful. 76 | */ 77 | bool reallocate(ref void[] p, size_t size); 78 | 79 | /** 80 | * Returns: The alignment offered. 81 | */ 82 | @property immutable(uint) alignment() const @safe pure nothrow; 83 | } 84 | 85 | /** 86 | * Params: 87 | * T = Element type of the array being created. 88 | * allocator = The allocator used for getting memory. 89 | * array = A reference to the array being changed. 90 | * length = New array length. 91 | * 92 | * Returns: $(D_KEYWORD true) upon success, $(D_KEYWORD false) if memory could 93 | * not be reallocated. In the latter 94 | */ 95 | bool resizeArray(T)(Allocator allocator, 96 | ref T[] array, 97 | in size_t length) 98 | { 99 | void[] buf = array; 100 | 101 | if (!allocator.reallocate(buf, length * T.sizeof)) 102 | { 103 | return false; 104 | } 105 | array = cast(T[]) buf; 106 | 107 | return true; 108 | } 109 | 110 | /// 111 | unittest 112 | { 113 | int[] p; 114 | 115 | defaultAllocator.resizeArray(p, 20); 116 | assert(p.length == 20); 117 | 118 | defaultAllocator.resizeArray(p, 30); 119 | assert(p.length == 30); 120 | 121 | defaultAllocator.resizeArray(p, 10); 122 | assert(p.length == 10); 123 | 124 | defaultAllocator.resizeArray(p, 0); 125 | assert(p is null); 126 | } 127 | 128 | enum bool isFinalizable(T) = 129 | is(T == class) || 130 | is(T == interface) || 131 | hasElaborateDestructor!T || 132 | isDynamicArray!T; 133 | --------------------------------------------------------------------------------