├── .clang-format ├── .github └── workflows │ ├── CI.yml │ └── Integration.yml ├── .gitignore ├── CMakeLists.txt ├── LICENSE.md ├── README.md ├── examples ├── CMakeLists.txt ├── bernoulli_dist.cpp ├── chess_board.cpp ├── draw_primitives.cpp ├── fractals │ ├── color_maps.inl │ ├── julia.cpp │ └── mandelbrot.cpp ├── polymorphic_shapes.cpp ├── read_bitmap.cpp ├── rotation.cpp ├── variant_shapes.cpp └── write_bitmap.cpp ├── images ├── chess_board.bmp ├── julia.bmp ├── mandelbrot.bmp ├── modified-penguin.bmp ├── penguin.bmp ├── primitives.bmp ├── random.bmp ├── rotated │ ├── penguin_flipped_h.bmp │ ├── penguin_flipped_v.bmp │ ├── penguin_rotated_left.bmp │ └── penguin_rotated_right.bmp └── shapes.bmp ├── integration_tests ├── cpm │ ├── CMakeLists.txt │ ├── CPM.cmake │ ├── Main.cpp │ └── README.md └── fetchcontent │ ├── CMakeLists.txt │ ├── Main.cpp │ └── README.md └── lib ├── CMakeLists.txt └── include └── BitmapPlusPlus.hpp /.clang-format: -------------------------------------------------------------------------------- 1 | BasedOnStyle: Google 2 | AlignAfterOpenBracket: Align 3 | AlignOperands: Align 4 | AllowAllArgumentsOnNextLine: false 5 | AllowAllConstructorInitializersOnNextLine: false 6 | AllowAllParametersOfDeclarationOnNextLine: false 7 | AllowShortBlocksOnASingleLine: Always 8 | AllowShortCaseLabelsOnASingleLine: false 9 | AllowShortFunctionsOnASingleLine: All 10 | AllowShortIfStatementsOnASingleLine: AllIfsAndElse 11 | AllowShortLambdasOnASingleLine: All 12 | AllowShortLoopsOnASingleLine: true 13 | AlwaysBreakAfterReturnType: None 14 | AlwaysBreakTemplateDeclarations: Yes 15 | BraceWrapping: 16 | AfterCaseLabel: false 17 | AfterClass: false 18 | AfterControlStatement: Never 19 | AfterEnum: false 20 | AfterFunction: false 21 | AfterNamespace: false 22 | AfterUnion: false 23 | BeforeCatch: false 24 | BeforeElse: false 25 | IndentBraces: false 26 | SplitEmptyFunction: false 27 | SplitEmptyRecord: true 28 | BreakBeforeBinaryOperators: None 29 | BreakBeforeTernaryOperators: true 30 | BreakConstructorInitializers: BeforeColon 31 | BreakInheritanceList: BeforeColon 32 | ColumnLimit: 0 33 | CompactNamespaces: false 34 | ContinuationIndentWidth: 2 35 | IndentCaseLabels: true 36 | IndentPPDirectives: None 37 | IndentWidth: 2 38 | BracedInitializerIndentWidth: 2 39 | PPIndentWidth: 2 40 | ConstructorInitializerIndentWidth: 2 41 | KeepEmptyLinesAtTheStartOfBlocks: true 42 | MaxEmptyLinesToKeep: 2 43 | NamespaceIndentation: All 44 | ObjCSpaceAfterProperty: false 45 | ObjCSpaceBeforeProtocolList: true 46 | PointerAlignment: Right 47 | ReflowComments: false 48 | SpaceAfterCStyleCast: true 49 | SpaceAfterLogicalNot: false 50 | SpaceAfterTemplateKeyword: false 51 | SpaceBeforeAssignmentOperators: true 52 | SpaceBeforeCpp11BracedList: false 53 | SpaceBeforeCtorInitializerColon: true 54 | SpaceBeforeInheritanceColon: true 55 | SpaceBeforeParens: ControlStatements 56 | SpaceBeforeRangeBasedForLoopColon: false 57 | SpaceInEmptyParentheses: false 58 | SpacesBeforeTrailingComments: 1 59 | SpacesInAngles: Never 60 | SpacesInCStyleCastParentheses: false 61 | SpacesInContainerLiterals: false 62 | SpacesInParentheses: false 63 | SpacesInSquareBrackets: false 64 | TabWidth: 2 65 | UseTab: Never 66 | AccessModifierOffset: -2 67 | ReferenceAlignment: Left 68 | FixNamespaceComments: false 69 | IndentAccessModifiers: false 70 | AlignConsecutiveAssignments: false 71 | AlignConsecutiveDeclarations: false 72 | AlignEscapedNewlines: Left 73 | AlignTrailingComments: true 74 | BreakBeforeBraces: Attach 75 | SortIncludes: Never 76 | -------------------------------------------------------------------------------- /.github/workflows/CI.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: [ "master" ] 6 | pull_request: 7 | branches: [ "master" ] 8 | workflow_dispatch: 9 | 10 | env: 11 | BUILD_DIR: ${{ github.workspace}}/vsbuild 12 | 13 | jobs: 14 | build: 15 | strategy: 16 | matrix: 17 | os: [ ubuntu-latest, windows-2022 ] 18 | runs-on: ${{ matrix.os }} 19 | 20 | steps: 21 | - uses: actions/checkout@v4 22 | 23 | - name: Check CMake version 24 | run: cmake --version 25 | 26 | - name: Configure CMake 27 | run: | 28 | mkdir "${{ env.BUILD_DIR }}" 29 | cd "${{ env.BUILD_DIR }}" 30 | cmake .. 31 | 32 | - name: Build 33 | working-directory: ${{ env.BUILD_DIR }} 34 | run: | 35 | cmake --build . --config Release 36 | 37 | - name: Test 38 | working-directory: ${{ env.BUILD_DIR }} 39 | run: ctest -C Release 40 | -------------------------------------------------------------------------------- /.github/workflows/Integration.yml: -------------------------------------------------------------------------------- 1 | name: IntegrationTests 2 | 3 | on: 4 | push: 5 | branches: [ "master" ] 6 | pull_request: 7 | branches: [ "master" ] 8 | workflow_dispatch: 9 | 10 | env: 11 | IT_DIR: ${{github.workspace}}/integration_tests 12 | 13 | jobs: 14 | build: 15 | runs-on: windows-2022 16 | 17 | steps: 18 | - uses: actions/checkout@v4 19 | 20 | - name: Get branch names. 21 | id: branch-names 22 | uses: tj-actions/branch-names@v8 23 | 24 | - name: Configure 25 | working-directory: ${{env.IT_DIR}}/cpm 26 | run: | 27 | mkdir _build 28 | cd _build 29 | cmake .. 30 | shell: cmd 31 | 32 | - name: Build 33 | working-directory: ${{env.IT_DIR}}/cpm/_build 34 | run: | 35 | cmake --build . --config Debug 36 | shell: cmd 37 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | _build 2 | build 3 | cmake-build-* 4 | .idea -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | 3 | project(BitmapPlusPlus 4 | VERSION "1.1.1" 5 | DESCRIPTION "Simple and Fast header only Bitmap (BMP) C++ library" 6 | LANGUAGES CXX 7 | ) 8 | 9 | if (${CMAKE_SOURCE_DIR} STREQUAL ${PROJECT_SOURCE_DIR}) 10 | set(BUILD_EXAMPLES_DEFAULT ON) 11 | else () 12 | set(BUILD_EXAMPLES_DEFAULT OFF) 13 | endif () 14 | 15 | option(BPP_BUILD_EXAMPLES "Requires BPP to build all examples inside examples/ folder." ${BUILD_EXAMPLES_DEFAULT}) 16 | 17 | set(CMAKE_CXX_STANDARD 17) 18 | 19 | add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/lib") 20 | 21 | if (BPP_BUILD_EXAMPLES) 22 | enable_testing() 23 | add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/examples") 24 | endif () 25 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Bader-Eddine Ouaich and other contributors 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Bitmap Plus Plus 2 | Simple and Fast header only Bitmap (BMP) library 3 | 4 | ## Bitmap Type Supported 5 | - 24 Bits Per Pixel (RGB) 6 | 7 | ## Integration 8 | 9 | You can either download the header file and use it your project directly, or you can leverage CMake for this. 10 | The library can be easily integrated with FetchContent or through CPM.
11 | An example of CPM integration can be found [here](integration_tests/cpm).
12 | An example of FetchContent integration can be found [here](integration_tests/fetchcontent). 13 | 14 | 15 | ## Examples 16 | Random Pixel Colors 17 |
18 | ```cpp 19 | #include "BitmapPlusPlus.hpp" 20 | #include 21 | #include 22 | 23 | static bmp::Pixel random_color() { 24 | static std::random_device seed{}; 25 | static std::default_random_engine engine{seed()}; 26 | std::uniform_int_distribution dist(0, 255); 27 | bmp::Pixel color{}; 28 | color.r = dist(engine); 29 | color.g = dist(engine); 30 | color.b = dist(engine); 31 | return color; 32 | } 33 | 34 | int main(void) { 35 | try { 36 | // Create a 512x512 bitmap 37 | bmp::Bitmap image(512, 512); 38 | 39 | // Assign a random color to each pixel in the image 40 | for (bmp::Pixel &pixel: image) { 41 | pixel = random_color(); 42 | } 43 | 44 | // Save bitmap to new file image.bmp 45 | image.save("image.bmp"); 46 | 47 | // And Voila! 48 | return EXIT_SUCCESS; 49 | } 50 | catch (const bmp::Exception &e) { 51 | std::cerr << "[BMP ERROR]: " << e.what() << std::endl; 52 | return EXIT_FAILURE; 53 | } 54 | } 55 | ``` 56 | ![random](images/random.bmp) 57 |

58 | 59 | Draw Primitives 60 | 61 | ```cpp 62 | #include 63 | #include "BitmapPlusPlus.hpp" 64 | 65 | using namespace bmp; 66 | 67 | int main() { 68 | // Create a 512x240 blank image 69 | Bitmap image(512, 240); 70 | image.clear(Pixel(0x25292e)); 71 | 72 | /** Line **/ 73 | // Draw a yellow line from position (250, 50) to position (500, 50) 74 | image.draw_line(250, 50, 500, 50, Yellow); 75 | 76 | /** Rectangle **/ 77 | // Draw a red rectangle in position (10, 10) with size 100x100 78 | image.draw_rect(10, 10, 100, 100, Red); 79 | // Draw a white filled rectangle in position (120, 10) with size 100x100 80 | image.fill_rect(120, 10, 100, 100, White); 81 | 82 | /** Triangle **/ 83 | image.draw_triangle(60, 120, 10, 220, 120, 220, Cyan); 84 | image.fill_triangle(180, 120, 130, 220, 245, 220, Magenta); 85 | 86 | /** Circle **/ 87 | // Draw a non-filled Gray circle in position (300, 170) with 50 pixels radius 88 | image.draw_circle(300, 170, 50, Gray); 89 | // Draw a filled Lime circle in position (300, 170) with 50 pixels radius 90 | image.fill_circle(420, 170, 50, Lime); 91 | 92 | // Save bitmap 93 | image.save("primitives.bmp"); 94 | 95 | return EXIT_SUCCESS; 96 | } 97 | ``` 98 | ![primitives](images/primitives.bmp) 99 | 100 |

101 | 102 | 103 | Mandelbrot Fractal Set 104 |
105 | ```cpp 106 | #include "BitmapPlusPlus.hpp" 107 | #include "color_maps.inl" 108 | #include 109 | 110 | int main(void) { 111 | bmp::Bitmap image(1280, 960); 112 | 113 | double cr, ci; 114 | double nextr, nexti; 115 | double prevr, previ; 116 | constexpr const std::uint16_t max_iterations = 3000; 117 | 118 | for (std::int32_t y = 0; y < image.height(); ++y) { 119 | for (std::int32_t x = 0; x < image.width(); ++x) { 120 | cr = 1.5 * (2.0 * x / image.width() - 1.0) - 0.5; 121 | ci = (2.0 * y / image.height() - 1.0); 122 | 123 | nextr = nexti = 0; 124 | prevr = previ = 0; 125 | 126 | for (std::uint16_t i = 0; i < max_iterations; ++i) { 127 | prevr = nextr; 128 | previ = nexti; 129 | 130 | nextr = prevr * prevr - previ * previ + cr; 131 | nexti = 2 * prevr * previ + ci; 132 | 133 | if (((nextr * nextr) + (nexti * nexti)) > 4) { 134 | const double z = sqrt(nextr * nextr + nexti * nexti); 135 | 136 | // https://en.wikipedia.org/wiki/Mandelbrot_set#Continuous_.28smooth.29_coloring 137 | const std::uint32_t index = static_cast(1000.0 * log2(1.75 + i - log2(log2(z))) / 138 | log2(max_iterations)); 139 | 140 | image.set(x, y, jet_colormap[index]); 141 | 142 | break; 143 | } 144 | } 145 | } 146 | } 147 | 148 | image.save("mandelbrot.bmp"); 149 | 150 | return EXIT_SUCCESS; 151 | } 152 | ``` 153 | ![mandelbrot](images/mandelbrot.bmp) 154 | 155 |

156 | 157 | Julia Fractal Set 158 |
159 | 160 | ```cpp 161 | #include "BitmapPlusPlus.hpp" 162 | #include "color_maps.inl" 163 | 164 | int main(void) { 165 | bmp::Bitmap image(1280, 960); 166 | 167 | constexpr const std::uint16_t max_iterations = 300; 168 | 169 | constexpr const double cr = -0.70000; 170 | constexpr const double ci = 0.27015; 171 | 172 | double prevr, previ; 173 | 174 | for (std::int32_t y = 0; y < image.height(); ++y) { 175 | for (std::int32_t x = 0; x < image.width(); ++x) { 176 | double nextr = 1.5 * (2.0 * x / image.width() - 1.0); 177 | double nexti = (2.0 * y / image.height() - 1.0); 178 | 179 | for (std::uint16_t i = 0; i < max_iterations; ++i) { 180 | prevr = nextr; 181 | previ = nexti; 182 | 183 | nextr = prevr * prevr - previ * previ + cr; 184 | nexti = 2 * prevr * previ + ci; 185 | 186 | if (((nextr * nextr) + (nexti * nexti)) > 4) { 187 | const bmp::Pixel color = hsv_colormap[static_cast((1000.0 * i) / max_iterations)]; 188 | image.set(x, y, color); 189 | break; 190 | } 191 | } 192 | } 193 | } 194 | 195 | image.save("julia.bmp"); 196 | 197 | return EXIT_SUCCESS; 198 | } 199 | ``` 200 | ![julia](images/julia.bmp) 201 | 202 | 203 |

204 | 205 | Modify The Penguin 206 |
207 | 208 | ```cpp 209 | #include "BitmapPlusPlus.hpp" 210 | 211 | int main(void) { 212 | try { 213 | bmp::Bitmap image; 214 | 215 | // Load penguin.bmp bitmap 216 | image.load("penguin.bmp"); 217 | 218 | // Modify loaded image (makes half of the image black) 219 | for (std::int32_t y = 0; y < image.height(); ++y) { 220 | for (std::int32_t x = 0; x < image.width() / 2; ++x) { 221 | image.set(x, y, bmp::Black); 222 | } 223 | } 224 | 225 | // Save 226 | image.save("modified-penguin.bmp"); 227 | 228 | return EXIT_SUCCESS; 229 | } 230 | catch (const bmp::Exception &e) { 231 | return EXIT_FAILURE; 232 | } 233 | } 234 | ``` 235 | ![penguin](images/penguin.bmp) 236 | ![modified-penguin](images/modified-penguin.bmp) 237 | 238 | 239 |

240 | Rotate, Flip The Penguin 241 |
242 | 243 | ```cpp 244 | #include "BitmapPlusPlus.hpp" 245 | #include 246 | 247 | int main(void) { 248 | try { 249 | bmp::Bitmap image; 250 | 251 | // Load the original bitmap 252 | image.load(std::filesystem::path(ROOT_DIR) / "images" / "penguin.bmp"); 253 | 254 | // Test vertical flip 255 | bmp::Bitmap flipped_v = image.flip_v(); 256 | flipped_v.save(std::filesystem::path(ROOT_DIR) / "images" / "rotated" / "penguin_flipped_v.bmp"); 257 | std::cout << "Vertical flip saved as penguin_flipped_v.bmp" << std::endl; 258 | 259 | // Test horizontal flip 260 | bmp::Bitmap flipped_h = image.flip_h(); 261 | flipped_h.save(std::filesystem::path(ROOT_DIR) / "images" / "rotated" / "penguin_flipped_h.bmp"); 262 | std::cout << "Horizontal flip saved as penguin_flipped_h.bmp" << std::endl; 263 | 264 | // Test rotate 90 degrees to the right 265 | bmp::Bitmap rotated_right = image.rotate_90_right(); 266 | rotated_right.save(std::filesystem::path(ROOT_DIR) / "images" / "rotated" / "penguin_rotated_right.bmp"); 267 | std::cout << "Rotated 90 degrees right saved as penguin_rotated_right.bmp" << std::endl; 268 | 269 | // Test rotate 90 degrees to the left 270 | bmp::Bitmap rotated_left = image.rotate_90_left(); 271 | rotated_left.save(std::filesystem::path(ROOT_DIR) / "images" / "rotated" / "penguin_rotated_left.bmp"); 272 | std::cout << "Rotated 90 degrees left saved as penguin_rotated_left.bmp" << std::endl; 273 | 274 | return EXIT_SUCCESS; 275 | } 276 | catch (const bmp::Exception& e) { 277 | std::cerr << "Error: " << e.what() << std::endl; 278 | return EXIT_FAILURE; 279 | } 280 | } 281 | 282 | ``` 283 | ![penguin](images/penguin.bmp) 284 | ![penguin_flipped_v](images/rotated/penguin_flipped_v.bmp) 285 | ![penguin_flipped_h](images/rotated/penguin_flipped_h.bmp) 286 | ![penguin_rotated_right](images/rotated/penguin_rotated_right.bmp) 287 | ![penguin_rotated_left](images/rotated/penguin_rotated_left.bmp) 288 | 289 | 290 |

291 | 292 | Chess Board 293 |
294 | 295 | ```cpp 296 | #include 297 | #include "BitmapPlusPlus.hpp" 298 | 299 | int main() { 300 | try { 301 | // 8x8 chess board 302 | bmp::Bitmap image(640, 640); 303 | const std::size_t board_dims = 8; 304 | const std::size_t rect_w = image.width() / board_dims; 305 | const std::size_t rect_h = image.height() / board_dims; 306 | 307 | // Iterate over rects 308 | bool is_white = true; 309 | for (std::size_t x = 0; x < image.width(); x += rect_w) { 310 | for (std::size_t y = 0; y < image.height(); y += rect_h) { 311 | bmp::Pixel color = is_white ? bmp::White : bmp::Black; 312 | // Fill rect 313 | image.fill_rect(x, y, rect_w, rect_h, color); 314 | // Next rect in will be the opposite color 315 | is_white = !is_white; 316 | } 317 | is_white = !is_white; 318 | } 319 | 320 | // Save bitmap to file 321 | image.save("chess_board.bmp"); 322 | 323 | return EXIT_SUCCESS; 324 | } 325 | catch (const bmp::Exception &e) { 326 | std::cerr << "[BMP ERROR]: " << e.what() << '\n'; 327 | return EXIT_FAILURE; 328 | } 329 | } 330 | ``` 331 | ![chess_board](images/chess_board.bmp) 332 | 333 | 334 |

335 | 336 | Draw multiple shapes using Polymorphism 337 |
338 | 339 | ```cpp 340 | #include 341 | #include "BitmapPlusPlus.hpp" 342 | 343 | struct Shape { 344 | int x, y; 345 | bmp::Pixel color; 346 | 347 | Shape(int x, int y, bmp::Pixel color) : x(x), y(y), color(color) {} 348 | 349 | virtual void draw(bmp::Bitmap &image) = 0; 350 | }; 351 | 352 | struct Rectangle : Shape { 353 | int width, height; 354 | 355 | Rectangle(int x, int y, int w, int h, bmp::Pixel color) : width(w), height(h), Shape(x, y, color) {} 356 | 357 | void draw(bmp::Bitmap &image) override { 358 | image.fill_rect(x, y, width, height, color); 359 | } 360 | }; 361 | 362 | struct Triangle : Shape { 363 | int x2, y2, x3, y3; 364 | 365 | Triangle(int x1, int y1, int x2, int y2, int x3, int y3, bmp::Pixel color) : x2(x2), y2(y2), x3(x3), y3(y3), 366 | Shape(x1, y1, color) {} 367 | 368 | void draw(bmp::Bitmap &image) override { 369 | image.fill_triangle(x, y, x2, y2, x3, y3, color); 370 | } 371 | }; 372 | 373 | struct Circle : Shape { 374 | int radius; 375 | 376 | Circle(int x, int y, int radius, bmp::Pixel color) : radius(radius), Shape(x, y, color) {} 377 | 378 | void draw(bmp::Bitmap &image) override { 379 | image.fill_circle(x, y, radius, color); 380 | } 381 | }; 382 | 383 | int main() { 384 | try { 385 | bmp::Bitmap image(640, 256); 386 | bmp::Pixel background_color{bmp::Silver}; 387 | image.clear(background_color); 388 | 389 | std::vector shapes 390 | { 391 | new Rectangle(20, 20, 180, 180, bmp::Pixel(0xa31d3a)), 392 | new Triangle(310, 20, 230, 200, 400, 200, bmp::Pixel(0x1a5096)), 393 | new Circle(500, 110, 90, bmp::Pixel(0x228035)) 394 | }; 395 | 396 | for (Shape *shape: shapes) { 397 | shape->draw(image); 398 | delete shape; 399 | } 400 | image.save("shapes.bmp"); 401 | 402 | return EXIT_SUCCESS; 403 | } 404 | catch (const bmp::Exception &e) { 405 | std::cerr << "[BMP ERROR]: " << e.what() << '\n'; 406 | return EXIT_FAILURE; 407 | } 408 | } 409 | ``` 410 | ![shapes](images/shapes.bmp) 411 | 412 | ## Features and bugs 413 | If you face any problems feel free to open an issue at the [issue tracker][tracker]. If you feel the library is missing a feature, please raise a ticket on Github. Pull request are also welcome. 414 | 415 | [tracker]: https://github.com/baderouaich/BitmapPlusPlus/issues 416 | 417 | 418 | -------------------------------------------------------------------------------- /examples/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | 3 | file(GLOB_RECURSE EXAMPLES_FILES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp") 4 | 5 | foreach (example_file ${EXAMPLES_FILES}) 6 | message(STATUS "Adding example ${example_file}") 7 | get_filename_component(target_name ${example_file} NAME_WE) 8 | 9 | add_executable(${target_name} ${example_file}) 10 | target_link_libraries(${target_name} bmp::BitmapPlusPlus) 11 | target_compile_definitions(${target_name} PRIVATE "ROOT_DIR=\"${PROJECT_SOURCE_DIR}\"") 12 | target_compile_definitions(${target_name} PRIVATE "BIN_DIR=\"${PROJECT_BINARY_DIR}\"") 13 | 14 | add_test( 15 | NAME ${target_name} 16 | COMMAND ${target_name} 17 | ) 18 | endforeach () 19 | -------------------------------------------------------------------------------- /examples/bernoulli_dist.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "BitmapPlusPlus.hpp" 3 | #include 4 | #include 5 | 6 | int main() { 7 | try { 8 | // Generate a bitmap of 10% white and 90% black pixels 9 | bmp::Bitmap image(512, 512); 10 | 11 | std::random_device seed{}; 12 | std::default_random_engine eng{seed()}; 13 | std::bernoulli_distribution dist(0.10); // 10% White, 90% Black 14 | 15 | for (bmp::Pixel& pixel: image) { 16 | const bmp::Pixel color = dist(eng) ? bmp::White : bmp::Black; 17 | pixel = color; 18 | } 19 | 20 | image.save(std::filesystem::path(BIN_DIR) / "bernoulli.bmp"); 21 | 22 | return EXIT_SUCCESS; 23 | } catch (const bmp::Exception& e) { 24 | std::cerr << "[BMP ERROR]: " << e.what() << '\n'; 25 | return EXIT_FAILURE; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /examples/chess_board.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "BitmapPlusPlus.hpp" 4 | 5 | int main() { 6 | try { 7 | // 8x8 chess board 8 | bmp::Bitmap image(640, 640); 9 | const std::size_t board_dims = 8; 10 | const std::int32_t rect_w = image.width() / board_dims; 11 | const std::int32_t rect_h = image.height() / board_dims; 12 | 13 | // Iterate over rects 14 | bool is_white = true; 15 | for (std::size_t x = 0; x < image.width(); x += rect_w) { 16 | for (std::size_t y = 0; y < image.height(); y += rect_h) { 17 | const bmp::Pixel color = is_white ? bmp::White : bmp::Black; 18 | // Fill rect 19 | image.fill_rect(x, y, rect_w, rect_h, color); 20 | // Next rect in will be the opposite color 21 | is_white = !is_white; 22 | } 23 | is_white = !is_white; 24 | } 25 | 26 | // Save bitmap to file 27 | image.save(std::filesystem::path(BIN_DIR) / "chess_board.bmp"); 28 | 29 | return EXIT_SUCCESS; 30 | } catch (const bmp::Exception &e) { 31 | std::cerr << "[BMP ERROR]: " << e.what() << '\n'; 32 | return EXIT_FAILURE; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /examples/draw_primitives.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "BitmapPlusPlus.hpp" 3 | 4 | using namespace bmp; 5 | 6 | int main() { 7 | // Create a 512x240 blank image 8 | Bitmap image(512, 240); 9 | image.clear(Pixel(0x25292e)); 10 | 11 | /** Line **/ 12 | // Draw a yellow line from position (250, 50) to position (500, 50) 13 | image.draw_line(250, 50, 500, 50, Yellow); 14 | 15 | /** Rectangle **/ 16 | // Draw a red rectangle in position (10, 10) with size 100x100 17 | image.draw_rect(10, 10, 100, 100, Red); 18 | // Draw a white filled rectangle in position (120, 10) with size 100x100 19 | image.fill_rect(120, 10, 100, 100, White); 20 | 21 | /** Triangle **/ 22 | image.draw_triangle(60, 120, 10, 220, 120, 220, Cyan); 23 | image.fill_triangle(180, 120, 130, 220, 245, 220, Magenta); 24 | 25 | /** Circle **/ 26 | // Draw a non-filled Gray circle in position (300, 170) with 50 pixels radius 27 | image.draw_circle(300, 170, 50, Gray); 28 | // Draw a filled Lime circle in position (300, 170) with 50 pixels radius 29 | image.fill_circle(420, 170, 50, Lime); 30 | 31 | // Save bitmap 32 | image.save(std::filesystem::path(BIN_DIR) / "primitives.bmp"); 33 | 34 | return EXIT_SUCCESS; 35 | } 36 | -------------------------------------------------------------------------------- /examples/fractals/julia.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "BitmapPlusPlus.hpp" 3 | #include "color_maps.inl" 4 | 5 | int main() { 6 | bmp::Bitmap image(1280, 960); 7 | 8 | constexpr std::uint16_t max_iterations = 300; 9 | 10 | constexpr double cr = -0.70000; 11 | constexpr double ci = 0.27015; 12 | 13 | double prevr, previ; 14 | 15 | for (std::int32_t y = 0; y < image.height(); ++y) { 16 | for (std::int32_t x = 0; x < image.width(); ++x) { 17 | double nextr = 1.5 * (2.0 * x / image.width() - 1.0); 18 | double nexti = (2.0 * y / image.height() - 1.0); 19 | 20 | for (std::uint16_t i = 0; i < max_iterations; ++i) { 21 | prevr = nextr; 22 | previ = nexti; 23 | 24 | nextr = prevr * prevr - previ * previ + cr; 25 | nexti = 2 * prevr * previ + ci; 26 | 27 | if (((nextr * nextr) + (nexti * nexti)) > 4) { 28 | const bmp::Pixel color = hsv_colormap[static_cast((1000.0 * i) / max_iterations)]; 29 | image.set(x, y, color); 30 | break; 31 | } 32 | } 33 | } 34 | } 35 | 36 | image.save(std::filesystem::path(BIN_DIR) / "julia.bmp"); 37 | 38 | return EXIT_SUCCESS; 39 | } 40 | -------------------------------------------------------------------------------- /examples/fractals/mandelbrot.cpp: -------------------------------------------------------------------------------- 1 | #include "BitmapPlusPlus.hpp" 2 | #include "color_maps.inl" 3 | #include 4 | #include 5 | 6 | int main() { 7 | bmp::Bitmap image(1280, 960); 8 | 9 | double cr, ci; 10 | double nextr, nexti; 11 | double prevr, previ; 12 | constexpr std::uint16_t max_iterations = 3000; 13 | 14 | for (std::int32_t y = 0; y < image.height(); ++y) { 15 | for (std::int32_t x = 0; x < image.width(); ++x) { 16 | cr = 1.5 * (2.0 * x / image.width() - 1.0) - 0.5; 17 | ci = (2.0 * y / image.height() - 1.0); 18 | 19 | nextr = nexti = 0; 20 | prevr = previ = 0; 21 | 22 | for (std::uint16_t i = 0; i < max_iterations; ++i) { 23 | prevr = nextr; 24 | previ = nexti; 25 | 26 | nextr = prevr * prevr - previ * previ + cr; 27 | nexti = 2 * prevr * previ + ci; 28 | 29 | if (((nextr * nextr) + (nexti * nexti)) > 4) { 30 | const double z = sqrt(nextr * nextr + nexti * nexti); 31 | 32 | // https://en.wikipedia.org/wiki/Mandelbrot_set#Continuous_.28smooth.29_coloring 33 | const std::uint32_t index = static_cast(1000.0 * log2(1.75 + i - log2(log2(z))) / log2(max_iterations)); 34 | 35 | image.set(x, y, jet_colormap[index]); 36 | 37 | break; 38 | } 39 | } 40 | } 41 | } 42 | 43 | image.save(std::filesystem::path(BIN_DIR) / "mandelbrot.bmp"); 44 | 45 | return EXIT_SUCCESS; 46 | } 47 | -------------------------------------------------------------------------------- /examples/polymorphic_shapes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "BitmapPlusPlus.hpp" 4 | 5 | struct Shape { 6 | virtual ~Shape() = default; 7 | 8 | int x, y; 9 | bmp::Pixel color; 10 | 11 | Shape(int x, int y, bmp::Pixel color) : x(x), y(y), color(color) { 12 | } 13 | 14 | virtual void draw(bmp::Bitmap& image) = 0; 15 | }; 16 | 17 | struct Rectangle : Shape { 18 | int width, height; 19 | 20 | Rectangle(int x, int y, int w, int h, bmp::Pixel color) : width(w), height(h), Shape(x, y, color) { 21 | } 22 | 23 | void draw(bmp::Bitmap& image) override { 24 | image.fill_rect(x, y, width, height, color); 25 | } 26 | }; 27 | 28 | struct Triangle : Shape { 29 | int x2, y2, x3, y3; 30 | 31 | Triangle(int x1, int y1, int x2, int y2, int x3, int y3, bmp::Pixel color) : x2(x2), y2(y2), x3(x3), y3(y3), Shape(x1, y1, color) { 32 | } 33 | 34 | void draw(bmp::Bitmap& image) override { 35 | image.fill_triangle(x, y, x2, y2, x3, y3, color); 36 | } 37 | }; 38 | 39 | struct Circle : Shape { 40 | int radius; 41 | 42 | Circle(int x, int y, int radius, bmp::Pixel color) : radius(radius), Shape(x, y, color) { 43 | } 44 | 45 | void draw(bmp::Bitmap& image) override { 46 | image.fill_circle(x, y, radius, color); 47 | } 48 | }; 49 | 50 | int main() { 51 | try { 52 | bmp::Bitmap image(640, 256); 53 | bmp::Pixel background_color{bmp::Silver}; 54 | image.clear(background_color); 55 | 56 | std::vector shapes{ 57 | new Rectangle(20, 20, 180, 180, bmp::Pixel(0xa31d3a)), 58 | new Triangle(310, 20, 230, 200, 400, 200, bmp::Pixel(0x1a5096)), 59 | new Circle(500, 110, 90, bmp::Pixel(0x228035))}; 60 | 61 | for (Shape* shape: shapes) { 62 | shape->draw(image); 63 | delete shape; 64 | } 65 | image.save(std::filesystem::path(BIN_DIR) / "polymorphic_shapes.bmp"); 66 | 67 | return EXIT_SUCCESS; 68 | } catch (const bmp::Exception& e) { 69 | std::cerr << "[BMP ERROR]: " << e.what() << '\n'; 70 | return EXIT_FAILURE; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /examples/read_bitmap.cpp: -------------------------------------------------------------------------------- 1 | #include "BitmapPlusPlus.hpp" 2 | #include 3 | #include 4 | 5 | int main() { 6 | try { 7 | bmp::Bitmap image; 8 | 9 | // Load penguin.bmp bitmap 10 | image.load(std::filesystem::path(ROOT_DIR) / "images" / "penguin.bmp"); 11 | 12 | // Modify loaded image (makes half of the image black) 13 | for (std::int32_t y = 0; y < image.height(); ++y) { 14 | for (std::int32_t x = 0; x < image.width() / 2; ++x) { 15 | image.set(x, y, bmp::Black); 16 | } 17 | } 18 | 19 | // Save 20 | image.save(std::filesystem::path(BIN_DIR) / "modified-penguin.bmp"); 21 | 22 | return EXIT_SUCCESS; 23 | } catch (const bmp::Exception &e) { 24 | std::cerr << e.what() << std::endl; 25 | return EXIT_FAILURE; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /examples/rotation.cpp: -------------------------------------------------------------------------------- 1 | #include "BitmapPlusPlus.hpp" 2 | #include 3 | 4 | int main(void) { 5 | try { 6 | bmp::Bitmap image; 7 | 8 | // Load the original bitmap 9 | image.load(std::filesystem::path(ROOT_DIR) / "images" / "penguin.bmp"); 10 | 11 | // Test vertical flip 12 | bmp::Bitmap flipped_v = image.flip_v(); 13 | flipped_v.save(std::filesystem::path(ROOT_DIR) / "images" / "rotated" / "penguin_flipped_v.bmp"); 14 | std::cout << "Vertical flip saved as penguin_flipped_v.bmp" << std::endl; 15 | 16 | // Test horizontal flip 17 | bmp::Bitmap flipped_h = image.flip_h(); 18 | flipped_h.save(std::filesystem::path(ROOT_DIR) / "images" / "rotated" / "penguin_flipped_h.bmp"); 19 | std::cout << "Horizontal flip saved as penguin_flipped_h.bmp" << std::endl; 20 | 21 | // Test rotate 90 degrees to the right 22 | bmp::Bitmap rotated_right = image.rotate_90_right(); 23 | rotated_right.save(std::filesystem::path(ROOT_DIR) / "images" / "rotated" / "penguin_rotated_right.bmp"); 24 | std::cout << "Rotated 90 degrees right saved as penguin_rotated_right.bmp" << std::endl; 25 | 26 | // Test rotate 90 degrees to the left 27 | bmp::Bitmap rotated_left = image.rotate_90_left(); 28 | rotated_left.save(std::filesystem::path(ROOT_DIR) / "images" / "rotated" / "penguin_rotated_left.bmp"); 29 | std::cout << "Rotated 90 degrees left saved as penguin_rotated_left.bmp" << std::endl; 30 | 31 | return EXIT_SUCCESS; 32 | } catch (const bmp::Exception& e) { 33 | std::cerr << "Error: " << e.what() << std::endl; 34 | return EXIT_FAILURE; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /examples/variant_shapes.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "BitmapPlusPlus.hpp" 3 | #include 4 | #include 5 | #include 6 | 7 | struct Rectangle { 8 | int x, y; 9 | int width, height; 10 | bmp::Pixel color; 11 | 12 | Rectangle(int x, int y, int w, int h, bmp::Pixel color) 13 | : width(w), height(h), x(x), y(y), color(color) { 14 | } 15 | 16 | void draw(bmp::Bitmap& image) const { 17 | image.fill_rect(x, y, width, height, color); 18 | } 19 | }; 20 | 21 | struct Triangle { 22 | int x1, y1, x2, y2, x3, y3; 23 | bmp::Pixel color; 24 | 25 | 26 | Triangle(int x1, int y1, int x2, int y2, int x3, int y3, bmp::Pixel color) 27 | : x1(x1), y1(y1), x2(x2), y2(y2), x3(x3), y3(y3), color(color) { 28 | } 29 | 30 | void draw(bmp::Bitmap& image) const { 31 | image.fill_triangle(x1, y1, x2, y2, x3, y3, color); 32 | } 33 | }; 34 | 35 | struct Circle { 36 | int x, y; 37 | int radius; 38 | bmp::Pixel color; 39 | 40 | Circle(int x, int y, int radius, bmp::Pixel color) 41 | : radius(radius), x(x), y(y), color(color) { 42 | } 43 | 44 | void draw(bmp::Bitmap& image) const { 45 | image.fill_circle(x, y, radius, color); 46 | } 47 | }; 48 | 49 | struct ShapeDrawer { 50 | bmp::Bitmap& bitmap; 51 | 52 | explicit ShapeDrawer(bmp::Bitmap& bitmap) 53 | : bitmap(bitmap) { 54 | } 55 | 56 | void operator()(const Rectangle& rectangle) const { 57 | const auto& [x, y, width, height, color] = rectangle; 58 | bitmap.fill_rect(x, y, width, height, color); 59 | } 60 | 61 | void operator()(const Triangle& triangle) const { 62 | const auto& [x1, y1, x2, y2, x3, y3, color] = triangle; 63 | bitmap.fill_triangle(x1, y1, x2, y2, x3, y3, color); 64 | } 65 | 66 | void operator()(const Circle& circle) const { 67 | const auto& [x, y, radius, color] = circle; 68 | bitmap.fill_circle(x, y, radius, color); 69 | } 70 | }; 71 | 72 | 73 | int main() { 74 | try { 75 | bmp::Bitmap image(640, 256); 76 | image.clear(bmp::Teal); 77 | 78 | ShapeDrawer drawer{image}; 79 | 80 | // No OOP polymorphism + faster 81 | // for more details, watch this great talk by Klaus Iglberger: https://www.youtube.com/watch?v=m3UmABVf55g 82 | using Shape = std::variant; 83 | Shape shapes[] = { 84 | Rectangle(20, 20, 180, 180, bmp::Gold), 85 | Triangle(310, 20, 230, 200, 400, 200, bmp::Violet), 86 | Circle(500, 110, 90, bmp::Coral)}; 87 | 88 | for (Shape& shape: shapes) { 89 | std::visit(drawer, shape); // visit will call the appropriate ShapeDrawer::operator() for each shape 90 | } 91 | image.save(std::filesystem::path(BIN_DIR) / "variant_shapes.bmp"); 92 | 93 | return EXIT_SUCCESS; 94 | } catch (const bmp::Exception& e) { 95 | std::cerr << "[BMP ERROR]: " << e.what() << '\n'; 96 | return EXIT_FAILURE; 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /examples/write_bitmap.cpp: -------------------------------------------------------------------------------- 1 | #include "BitmapPlusPlus.hpp" 2 | #include 3 | #include 4 | #include 5 | 6 | static bmp::Pixel random_color() { 7 | static std::random_device seed{}; 8 | static std::default_random_engine engine{seed()}; 9 | std::uniform_int_distribution dist(0, 255); 10 | bmp::Pixel color{}; 11 | color.r = dist(engine); 12 | color.g = dist(engine); 13 | color.b = dist(engine); 14 | return color; 15 | } 16 | 17 | int main() { 18 | try { 19 | // Create a 512x512 bitmap 20 | bmp::Bitmap image(512, 512); 21 | 22 | // Assign a random color to each pixel in the image 23 | for (bmp::Pixel &pixel: image) { 24 | pixel = random_color(); 25 | } 26 | 27 | // Save bitmap to new file image.bmp 28 | image.save(std::filesystem::path(BIN_DIR) / "image.bmp"); 29 | 30 | // And Voila! 31 | return EXIT_SUCCESS; 32 | } catch (const bmp::Exception &e) { 33 | std::cerr << "[BMP ERROR]: " << e.what() << std::endl; 34 | return EXIT_FAILURE; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /images/chess_board.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/baderouaich/BitmapPlusPlus/f41f55cf93e0945fb98ab98940174138712e235e/images/chess_board.bmp -------------------------------------------------------------------------------- /images/julia.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/baderouaich/BitmapPlusPlus/f41f55cf93e0945fb98ab98940174138712e235e/images/julia.bmp -------------------------------------------------------------------------------- /images/mandelbrot.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/baderouaich/BitmapPlusPlus/f41f55cf93e0945fb98ab98940174138712e235e/images/mandelbrot.bmp -------------------------------------------------------------------------------- /images/modified-penguin.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/baderouaich/BitmapPlusPlus/f41f55cf93e0945fb98ab98940174138712e235e/images/modified-penguin.bmp -------------------------------------------------------------------------------- /images/penguin.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/baderouaich/BitmapPlusPlus/f41f55cf93e0945fb98ab98940174138712e235e/images/penguin.bmp -------------------------------------------------------------------------------- /images/primitives.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/baderouaich/BitmapPlusPlus/f41f55cf93e0945fb98ab98940174138712e235e/images/primitives.bmp -------------------------------------------------------------------------------- /images/random.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/baderouaich/BitmapPlusPlus/f41f55cf93e0945fb98ab98940174138712e235e/images/random.bmp -------------------------------------------------------------------------------- /images/rotated/penguin_flipped_h.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/baderouaich/BitmapPlusPlus/f41f55cf93e0945fb98ab98940174138712e235e/images/rotated/penguin_flipped_h.bmp -------------------------------------------------------------------------------- /images/rotated/penguin_flipped_v.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/baderouaich/BitmapPlusPlus/f41f55cf93e0945fb98ab98940174138712e235e/images/rotated/penguin_flipped_v.bmp -------------------------------------------------------------------------------- /images/rotated/penguin_rotated_left.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/baderouaich/BitmapPlusPlus/f41f55cf93e0945fb98ab98940174138712e235e/images/rotated/penguin_rotated_left.bmp -------------------------------------------------------------------------------- /images/rotated/penguin_rotated_right.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/baderouaich/BitmapPlusPlus/f41f55cf93e0945fb98ab98940174138712e235e/images/rotated/penguin_rotated_right.bmp -------------------------------------------------------------------------------- /images/shapes.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/baderouaich/BitmapPlusPlus/f41f55cf93e0945fb98ab98940174138712e235e/images/shapes.bmp -------------------------------------------------------------------------------- /integration_tests/cpm/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(demo) 3 | set(CMAKE_CXX_STANDARD 17) # C++17 or newer is required 4 | 5 | include(CPM.cmake) 6 | CPMAddPackage("gh:baderouaich/BitmapPlusPlus#master") 7 | 8 | add_executable(${PROJECT_NAME} "${CMAKE_CURRENT_SOURCE_DIR}/Main.cpp") 9 | 10 | target_link_libraries(${PROJECT_NAME} LINK_PRIVATE bmp::BitmapPlusPlus) 11 | -------------------------------------------------------------------------------- /integration_tests/cpm/CPM.cmake: -------------------------------------------------------------------------------- 1 | # CPM.cmake - CMake's missing package manager 2 | # =========================================== 3 | # See https://github.com/cpm-cmake/CPM.cmake for usage and update instructions. 4 | # 5 | # MIT License 6 | # ----------- 7 | #[[ 8 | Copyright (c) 2019-2023 Lars Melchior and contributors 9 | 10 | Permission is hereby granted, free of charge, to any person obtaining a copy 11 | of this software and associated documentation files (the "Software"), to deal 12 | in the Software without restriction, including without limitation the rights 13 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 | copies of the Software, and to permit persons to whom the Software is 15 | furnished to do so, subject to the following conditions: 16 | 17 | The above copyright notice and this permission notice shall be included in all 18 | copies or substantial portions of the Software. 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 AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | SOFTWARE. 27 | ]] 28 | 29 | cmake_minimum_required(VERSION 3.14 FATAL_ERROR) 30 | 31 | # Initialize logging prefix 32 | if(NOT CPM_INDENT) 33 | set(CPM_INDENT 34 | "CPM:" 35 | CACHE INTERNAL "" 36 | ) 37 | endif() 38 | 39 | if(NOT COMMAND cpm_message) 40 | function(cpm_message) 41 | message(${ARGV}) 42 | endfunction() 43 | endif() 44 | 45 | set(CURRENT_CPM_VERSION 0.40.5) 46 | 47 | get_filename_component(CPM_CURRENT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}" REALPATH) 48 | if(CPM_DIRECTORY) 49 | if(NOT CPM_DIRECTORY STREQUAL CPM_CURRENT_DIRECTORY) 50 | if(CPM_VERSION VERSION_LESS CURRENT_CPM_VERSION) 51 | message( 52 | AUTHOR_WARNING 53 | "${CPM_INDENT} \ 54 | A dependency is using a more recent CPM version (${CURRENT_CPM_VERSION}) than the current project (${CPM_VERSION}). \ 55 | It is recommended to upgrade CPM to the most recent version. \ 56 | See https://github.com/cpm-cmake/CPM.cmake for more information." 57 | ) 58 | endif() 59 | if(${CMAKE_VERSION} VERSION_LESS "3.17.0") 60 | include(FetchContent) 61 | endif() 62 | return() 63 | endif() 64 | 65 | get_property( 66 | CPM_INITIALIZED GLOBAL "" 67 | PROPERTY CPM_INITIALIZED 68 | SET 69 | ) 70 | if(CPM_INITIALIZED) 71 | return() 72 | endif() 73 | endif() 74 | 75 | if(CURRENT_CPM_VERSION MATCHES "development-version") 76 | message( 77 | WARNING "${CPM_INDENT} Your project is using an unstable development version of CPM.cmake. \ 78 | Please update to a recent release if possible. \ 79 | See https://github.com/cpm-cmake/CPM.cmake for details." 80 | ) 81 | endif() 82 | 83 | set_property(GLOBAL PROPERTY CPM_INITIALIZED true) 84 | 85 | macro(cpm_set_policies) 86 | # the policy allows us to change options without caching 87 | cmake_policy(SET CMP0077 NEW) 88 | set(CMAKE_POLICY_DEFAULT_CMP0077 NEW) 89 | 90 | # the policy allows us to change set(CACHE) without caching 91 | if(POLICY CMP0126) 92 | cmake_policy(SET CMP0126 NEW) 93 | set(CMAKE_POLICY_DEFAULT_CMP0126 NEW) 94 | endif() 95 | 96 | # The policy uses the download time for timestamp, instead of the timestamp in the archive. This 97 | # allows for proper rebuilds when a projects url changes 98 | if(POLICY CMP0135) 99 | cmake_policy(SET CMP0135 NEW) 100 | set(CMAKE_POLICY_DEFAULT_CMP0135 NEW) 101 | endif() 102 | 103 | # treat relative git repository paths as being relative to the parent project's remote 104 | if(POLICY CMP0150) 105 | cmake_policy(SET CMP0150 NEW) 106 | set(CMAKE_POLICY_DEFAULT_CMP0150 NEW) 107 | endif() 108 | endmacro() 109 | cpm_set_policies() 110 | 111 | option(CPM_USE_LOCAL_PACKAGES "Always try to use `find_package` to get dependencies" 112 | $ENV{CPM_USE_LOCAL_PACKAGES} 113 | ) 114 | option(CPM_LOCAL_PACKAGES_ONLY "Only use `find_package` to get dependencies" 115 | $ENV{CPM_LOCAL_PACKAGES_ONLY} 116 | ) 117 | option(CPM_DOWNLOAD_ALL "Always download dependencies from source" $ENV{CPM_DOWNLOAD_ALL}) 118 | option(CPM_DONT_UPDATE_MODULE_PATH "Don't update the module path to allow using find_package" 119 | $ENV{CPM_DONT_UPDATE_MODULE_PATH} 120 | ) 121 | option(CPM_DONT_CREATE_PACKAGE_LOCK "Don't create a package lock file in the binary path" 122 | $ENV{CPM_DONT_CREATE_PACKAGE_LOCK} 123 | ) 124 | option(CPM_INCLUDE_ALL_IN_PACKAGE_LOCK 125 | "Add all packages added through CPM.cmake to the package lock" 126 | $ENV{CPM_INCLUDE_ALL_IN_PACKAGE_LOCK} 127 | ) 128 | option(CPM_USE_NAMED_CACHE_DIRECTORIES 129 | "Use additional directory of package name in cache on the most nested level." 130 | $ENV{CPM_USE_NAMED_CACHE_DIRECTORIES} 131 | ) 132 | 133 | set(CPM_VERSION 134 | ${CURRENT_CPM_VERSION} 135 | CACHE INTERNAL "" 136 | ) 137 | set(CPM_DIRECTORY 138 | ${CPM_CURRENT_DIRECTORY} 139 | CACHE INTERNAL "" 140 | ) 141 | set(CPM_FILE 142 | ${CMAKE_CURRENT_LIST_FILE} 143 | CACHE INTERNAL "" 144 | ) 145 | set(CPM_PACKAGES 146 | "" 147 | CACHE INTERNAL "" 148 | ) 149 | set(CPM_DRY_RUN 150 | OFF 151 | CACHE INTERNAL "Don't download or configure dependencies (for testing)" 152 | ) 153 | 154 | if(DEFINED ENV{CPM_SOURCE_CACHE}) 155 | set(CPM_SOURCE_CACHE_DEFAULT $ENV{CPM_SOURCE_CACHE}) 156 | else() 157 | set(CPM_SOURCE_CACHE_DEFAULT OFF) 158 | endif() 159 | 160 | set(CPM_SOURCE_CACHE 161 | ${CPM_SOURCE_CACHE_DEFAULT} 162 | CACHE PATH "Directory to download CPM dependencies" 163 | ) 164 | 165 | if(NOT CPM_DONT_UPDATE_MODULE_PATH AND NOT DEFINED CMAKE_FIND_PACKAGE_REDIRECTS_DIR) 166 | set(CPM_MODULE_PATH 167 | "${CMAKE_BINARY_DIR}/CPM_modules" 168 | CACHE INTERNAL "" 169 | ) 170 | # remove old modules 171 | file(REMOVE_RECURSE ${CPM_MODULE_PATH}) 172 | file(MAKE_DIRECTORY ${CPM_MODULE_PATH}) 173 | # locally added CPM modules should override global packages 174 | set(CMAKE_MODULE_PATH "${CPM_MODULE_PATH};${CMAKE_MODULE_PATH}") 175 | endif() 176 | 177 | if(NOT CPM_DONT_CREATE_PACKAGE_LOCK) 178 | set(CPM_PACKAGE_LOCK_FILE 179 | "${CMAKE_BINARY_DIR}/cpm-package-lock.cmake" 180 | CACHE INTERNAL "" 181 | ) 182 | file(WRITE ${CPM_PACKAGE_LOCK_FILE} 183 | "# CPM Package Lock\n# This file should be committed to version control\n\n" 184 | ) 185 | endif() 186 | 187 | include(FetchContent) 188 | 189 | # Try to infer package name from git repository uri (path or url) 190 | function(cpm_package_name_from_git_uri URI RESULT) 191 | if("${URI}" MATCHES "([^/:]+)/?.git/?$") 192 | set(${RESULT} 193 | ${CMAKE_MATCH_1} 194 | PARENT_SCOPE 195 | ) 196 | else() 197 | unset(${RESULT} PARENT_SCOPE) 198 | endif() 199 | endfunction() 200 | 201 | # Try to infer package name and version from a url 202 | function(cpm_package_name_and_ver_from_url url outName outVer) 203 | if(url MATCHES "[/\\?]([a-zA-Z0-9_\\.-]+)\\.(tar|tar\\.gz|tar\\.bz2|zip|ZIP)(\\?|/|$)") 204 | # We matched an archive 205 | set(filename "${CMAKE_MATCH_1}") 206 | 207 | if(filename MATCHES "([a-zA-Z0-9_\\.-]+)[_-]v?(([0-9]+\\.)*[0-9]+[a-zA-Z0-9]*)") 208 | # We matched - (ie foo-1.2.3) 209 | set(${outName} 210 | "${CMAKE_MATCH_1}" 211 | PARENT_SCOPE 212 | ) 213 | set(${outVer} 214 | "${CMAKE_MATCH_2}" 215 | PARENT_SCOPE 216 | ) 217 | elseif(filename MATCHES "(([0-9]+\\.)+[0-9]+[a-zA-Z0-9]*)") 218 | # We couldn't find a name, but we found a version 219 | # 220 | # In many cases (which we don't handle here) the url would look something like 221 | # `irrelevant/ACTUAL_PACKAGE_NAME/irrelevant/1.2.3.zip`. In such a case we can't possibly 222 | # distinguish the package name from the irrelevant bits. Moreover if we try to match the 223 | # package name from the filename, we'd get bogus at best. 224 | unset(${outName} PARENT_SCOPE) 225 | set(${outVer} 226 | "${CMAKE_MATCH_1}" 227 | PARENT_SCOPE 228 | ) 229 | else() 230 | # Boldly assume that the file name is the package name. 231 | # 232 | # Yes, something like `irrelevant/ACTUAL_NAME/irrelevant/download.zip` will ruin our day, but 233 | # such cases should be quite rare. No popular service does this... we think. 234 | set(${outName} 235 | "${filename}" 236 | PARENT_SCOPE 237 | ) 238 | unset(${outVer} PARENT_SCOPE) 239 | endif() 240 | else() 241 | # No ideas yet what to do with non-archives 242 | unset(${outName} PARENT_SCOPE) 243 | unset(${outVer} PARENT_SCOPE) 244 | endif() 245 | endfunction() 246 | 247 | function(cpm_find_package NAME VERSION) 248 | string(REPLACE " " ";" EXTRA_ARGS "${ARGN}") 249 | find_package(${NAME} ${VERSION} ${EXTRA_ARGS} QUIET) 250 | if(${CPM_ARGS_NAME}_FOUND) 251 | if(DEFINED ${CPM_ARGS_NAME}_VERSION) 252 | set(VERSION ${${CPM_ARGS_NAME}_VERSION}) 253 | endif() 254 | cpm_message(STATUS "${CPM_INDENT} Using local package ${CPM_ARGS_NAME}@${VERSION}") 255 | CPMRegisterPackage(${CPM_ARGS_NAME} "${VERSION}") 256 | set(CPM_PACKAGE_FOUND 257 | YES 258 | PARENT_SCOPE 259 | ) 260 | else() 261 | set(CPM_PACKAGE_FOUND 262 | NO 263 | PARENT_SCOPE 264 | ) 265 | endif() 266 | endfunction() 267 | 268 | # Create a custom FindXXX.cmake module for a CPM package This prevents `find_package(NAME)` from 269 | # finding the system library 270 | function(cpm_create_module_file Name) 271 | if(NOT CPM_DONT_UPDATE_MODULE_PATH) 272 | if(DEFINED CMAKE_FIND_PACKAGE_REDIRECTS_DIR) 273 | # Redirect find_package calls to the CPM package. This is what FetchContent does when you set 274 | # OVERRIDE_FIND_PACKAGE. The CMAKE_FIND_PACKAGE_REDIRECTS_DIR works for find_package in CONFIG 275 | # mode, unlike the Find${Name}.cmake fallback. CMAKE_FIND_PACKAGE_REDIRECTS_DIR is not defined 276 | # in script mode, or in CMake < 3.24. 277 | # https://cmake.org/cmake/help/latest/module/FetchContent.html#fetchcontent-find-package-integration-examples 278 | string(TOLOWER ${Name} NameLower) 279 | file(WRITE ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/${NameLower}-config.cmake 280 | "include(\"${CMAKE_CURRENT_LIST_DIR}/${NameLower}-extra.cmake\" OPTIONAL)\n" 281 | "include(\"${CMAKE_CURRENT_LIST_DIR}/${Name}Extra.cmake\" OPTIONAL)\n" 282 | ) 283 | file(WRITE ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/${NameLower}-version.cmake 284 | "set(PACKAGE_VERSION_COMPATIBLE TRUE)\n" "set(PACKAGE_VERSION_EXACT TRUE)\n" 285 | ) 286 | else() 287 | file(WRITE ${CPM_MODULE_PATH}/Find${Name}.cmake 288 | "include(\"${CPM_FILE}\")\n${ARGN}\nset(${Name}_FOUND TRUE)" 289 | ) 290 | endif() 291 | endif() 292 | endfunction() 293 | 294 | # Find a package locally or fallback to CPMAddPackage 295 | function(CPMFindPackage) 296 | set(oneValueArgs NAME VERSION GIT_TAG FIND_PACKAGE_ARGUMENTS) 297 | 298 | cmake_parse_arguments(CPM_ARGS "" "${oneValueArgs}" "" ${ARGN}) 299 | 300 | if(NOT DEFINED CPM_ARGS_VERSION) 301 | if(DEFINED CPM_ARGS_GIT_TAG) 302 | cpm_get_version_from_git_tag("${CPM_ARGS_GIT_TAG}" CPM_ARGS_VERSION) 303 | endif() 304 | endif() 305 | 306 | set(downloadPackage ${CPM_DOWNLOAD_ALL}) 307 | if(DEFINED CPM_DOWNLOAD_${CPM_ARGS_NAME}) 308 | set(downloadPackage ${CPM_DOWNLOAD_${CPM_ARGS_NAME}}) 309 | elseif(DEFINED ENV{CPM_DOWNLOAD_${CPM_ARGS_NAME}}) 310 | set(downloadPackage $ENV{CPM_DOWNLOAD_${CPM_ARGS_NAME}}) 311 | endif() 312 | if(downloadPackage) 313 | CPMAddPackage(${ARGN}) 314 | cpm_export_variables(${CPM_ARGS_NAME}) 315 | return() 316 | endif() 317 | 318 | cpm_find_package(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}" ${CPM_ARGS_FIND_PACKAGE_ARGUMENTS}) 319 | 320 | if(NOT CPM_PACKAGE_FOUND) 321 | CPMAddPackage(${ARGN}) 322 | cpm_export_variables(${CPM_ARGS_NAME}) 323 | endif() 324 | 325 | endfunction() 326 | 327 | # checks if a package has been added before 328 | function(cpm_check_if_package_already_added CPM_ARGS_NAME CPM_ARGS_VERSION) 329 | if("${CPM_ARGS_NAME}" IN_LIST CPM_PACKAGES) 330 | CPMGetPackageVersion(${CPM_ARGS_NAME} CPM_PACKAGE_VERSION) 331 | if("${CPM_PACKAGE_VERSION}" VERSION_LESS "${CPM_ARGS_VERSION}") 332 | message( 333 | WARNING 334 | "${CPM_INDENT} Requires a newer version of ${CPM_ARGS_NAME} (${CPM_ARGS_VERSION}) than currently included (${CPM_PACKAGE_VERSION})." 335 | ) 336 | endif() 337 | cpm_get_fetch_properties(${CPM_ARGS_NAME}) 338 | set(${CPM_ARGS_NAME}_ADDED NO) 339 | set(CPM_PACKAGE_ALREADY_ADDED 340 | YES 341 | PARENT_SCOPE 342 | ) 343 | cpm_export_variables(${CPM_ARGS_NAME}) 344 | else() 345 | set(CPM_PACKAGE_ALREADY_ADDED 346 | NO 347 | PARENT_SCOPE 348 | ) 349 | endif() 350 | endfunction() 351 | 352 | # Parse the argument of CPMAddPackage in case a single one was provided and convert it to a list of 353 | # arguments which can then be parsed idiomatically. For example gh:foo/bar@1.2.3 will be converted 354 | # to: GITHUB_REPOSITORY;foo/bar;VERSION;1.2.3 355 | function(cpm_parse_add_package_single_arg arg outArgs) 356 | # Look for a scheme 357 | if("${arg}" MATCHES "^([a-zA-Z]+):(.+)$") 358 | string(TOLOWER "${CMAKE_MATCH_1}" scheme) 359 | set(uri "${CMAKE_MATCH_2}") 360 | 361 | # Check for CPM-specific schemes 362 | if(scheme STREQUAL "gh") 363 | set(out "GITHUB_REPOSITORY;${uri}") 364 | set(packageType "git") 365 | elseif(scheme STREQUAL "gl") 366 | set(out "GITLAB_REPOSITORY;${uri}") 367 | set(packageType "git") 368 | elseif(scheme STREQUAL "bb") 369 | set(out "BITBUCKET_REPOSITORY;${uri}") 370 | set(packageType "git") 371 | # A CPM-specific scheme was not found. Looks like this is a generic URL so try to determine 372 | # type 373 | elseif(arg MATCHES ".git/?(@|#|$)") 374 | set(out "GIT_REPOSITORY;${arg}") 375 | set(packageType "git") 376 | else() 377 | # Fall back to a URL 378 | set(out "URL;${arg}") 379 | set(packageType "archive") 380 | 381 | # We could also check for SVN since FetchContent supports it, but SVN is so rare these days. 382 | # We just won't bother with the additional complexity it will induce in this function. SVN is 383 | # done by multi-arg 384 | endif() 385 | else() 386 | if(arg MATCHES ".git/?(@|#|$)") 387 | set(out "GIT_REPOSITORY;${arg}") 388 | set(packageType "git") 389 | else() 390 | # Give up 391 | message(FATAL_ERROR "${CPM_INDENT} Can't determine package type of '${arg}'") 392 | endif() 393 | endif() 394 | 395 | # For all packages we interpret @... as version. Only replace the last occurrence. Thus URIs 396 | # containing '@' can be used 397 | string(REGEX REPLACE "@([^@]+)$" ";VERSION;\\1" out "${out}") 398 | 399 | # Parse the rest according to package type 400 | if(packageType STREQUAL "git") 401 | # For git repos we interpret #... as a tag or branch or commit hash 402 | string(REGEX REPLACE "#([^#]+)$" ";GIT_TAG;\\1" out "${out}") 403 | elseif(packageType STREQUAL "archive") 404 | # For archives we interpret #... as a URL hash. 405 | string(REGEX REPLACE "#([^#]+)$" ";URL_HASH;\\1" out "${out}") 406 | # We don't try to parse the version if it's not provided explicitly. cpm_get_version_from_url 407 | # should do this at a later point 408 | else() 409 | # We should never get here. This is an assertion and hitting it means there's a problem with the 410 | # code above. A packageType was set, but not handled by this if-else. 411 | message(FATAL_ERROR "${CPM_INDENT} Unsupported package type '${packageType}' of '${arg}'") 412 | endif() 413 | 414 | set(${outArgs} 415 | ${out} 416 | PARENT_SCOPE 417 | ) 418 | endfunction() 419 | 420 | # Check that the working directory for a git repo is clean 421 | function(cpm_check_git_working_dir_is_clean repoPath gitTag isClean) 422 | 423 | find_package(Git REQUIRED) 424 | 425 | if(NOT GIT_EXECUTABLE) 426 | # No git executable, assume directory is clean 427 | set(${isClean} 428 | TRUE 429 | PARENT_SCOPE 430 | ) 431 | return() 432 | endif() 433 | 434 | # check for uncommitted changes 435 | execute_process( 436 | COMMAND ${GIT_EXECUTABLE} status --porcelain 437 | RESULT_VARIABLE resultGitStatus 438 | OUTPUT_VARIABLE repoStatus 439 | OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET 440 | WORKING_DIRECTORY ${repoPath} 441 | ) 442 | if(resultGitStatus) 443 | # not supposed to happen, assume clean anyway 444 | message(WARNING "${CPM_INDENT} Calling git status on folder ${repoPath} failed") 445 | set(${isClean} 446 | TRUE 447 | PARENT_SCOPE 448 | ) 449 | return() 450 | endif() 451 | 452 | if(NOT "${repoStatus}" STREQUAL "") 453 | set(${isClean} 454 | FALSE 455 | PARENT_SCOPE 456 | ) 457 | return() 458 | endif() 459 | 460 | # check for committed changes 461 | execute_process( 462 | COMMAND ${GIT_EXECUTABLE} diff -s --exit-code ${gitTag} 463 | RESULT_VARIABLE resultGitDiff 464 | OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_QUIET 465 | WORKING_DIRECTORY ${repoPath} 466 | ) 467 | 468 | if(${resultGitDiff} EQUAL 0) 469 | set(${isClean} 470 | TRUE 471 | PARENT_SCOPE 472 | ) 473 | else() 474 | set(${isClean} 475 | FALSE 476 | PARENT_SCOPE 477 | ) 478 | endif() 479 | 480 | endfunction() 481 | 482 | # Add PATCH_COMMAND to CPM_ARGS_UNPARSED_ARGUMENTS. This method consumes a list of files in ARGN 483 | # then generates a `PATCH_COMMAND` appropriate for `ExternalProject_Add()`. This command is appended 484 | # to the parent scope's `CPM_ARGS_UNPARSED_ARGUMENTS`. 485 | function(cpm_add_patches) 486 | # Return if no patch files are supplied. 487 | if(NOT ARGN) 488 | return() 489 | endif() 490 | 491 | # Find the patch program. 492 | find_program(PATCH_EXECUTABLE patch) 493 | if(CMAKE_HOST_WIN32 AND NOT PATCH_EXECUTABLE) 494 | # The Windows git executable is distributed with patch.exe. Find the path to the executable, if 495 | # it exists, then search `../usr/bin` and `../../usr/bin` for patch.exe. 496 | find_package(Git QUIET) 497 | if(GIT_EXECUTABLE) 498 | get_filename_component(extra_search_path ${GIT_EXECUTABLE} DIRECTORY) 499 | get_filename_component(extra_search_path_1up ${extra_search_path} DIRECTORY) 500 | get_filename_component(extra_search_path_2up ${extra_search_path_1up} DIRECTORY) 501 | find_program( 502 | PATCH_EXECUTABLE patch HINTS "${extra_search_path_1up}/usr/bin" 503 | "${extra_search_path_2up}/usr/bin" 504 | ) 505 | endif() 506 | endif() 507 | if(NOT PATCH_EXECUTABLE) 508 | message(FATAL_ERROR "Couldn't find `patch` executable to use with PATCHES keyword.") 509 | endif() 510 | 511 | # Create a temporary 512 | set(temp_list ${CPM_ARGS_UNPARSED_ARGUMENTS}) 513 | 514 | # Ensure each file exists (or error out) and add it to the list. 515 | set(first_item True) 516 | foreach(PATCH_FILE ${ARGN}) 517 | # Make sure the patch file exists, if we can't find it, try again in the current directory. 518 | if(NOT EXISTS "${PATCH_FILE}") 519 | if(NOT EXISTS "${CMAKE_CURRENT_LIST_DIR}/${PATCH_FILE}") 520 | message(FATAL_ERROR "Couldn't find patch file: '${PATCH_FILE}'") 521 | endif() 522 | set(PATCH_FILE "${CMAKE_CURRENT_LIST_DIR}/${PATCH_FILE}") 523 | endif() 524 | 525 | # Convert to absolute path for use with patch file command. 526 | get_filename_component(PATCH_FILE "${PATCH_FILE}" ABSOLUTE) 527 | 528 | # The first patch entry must be preceded by "PATCH_COMMAND" while the following items are 529 | # preceded by "&&". 530 | if(first_item) 531 | set(first_item False) 532 | list(APPEND temp_list "PATCH_COMMAND") 533 | else() 534 | list(APPEND temp_list "&&") 535 | endif() 536 | # Add the patch command to the list 537 | list(APPEND temp_list "${PATCH_EXECUTABLE}" "-p1" "<" "${PATCH_FILE}") 538 | endforeach() 539 | 540 | # Move temp out into parent scope. 541 | set(CPM_ARGS_UNPARSED_ARGUMENTS 542 | ${temp_list} 543 | PARENT_SCOPE 544 | ) 545 | 546 | endfunction() 547 | 548 | # method to overwrite internal FetchContent properties, to allow using CPM.cmake to overload 549 | # FetchContent calls. As these are internal cmake properties, this method should be used carefully 550 | # and may need modification in future CMake versions. Source: 551 | # https://github.com/Kitware/CMake/blob/dc3d0b5a0a7d26d43d6cfeb511e224533b5d188f/Modules/FetchContent.cmake#L1152 552 | function(cpm_override_fetchcontent contentName) 553 | cmake_parse_arguments(PARSE_ARGV 1 arg "" "SOURCE_DIR;BINARY_DIR" "") 554 | if(NOT "${arg_UNPARSED_ARGUMENTS}" STREQUAL "") 555 | message(FATAL_ERROR "${CPM_INDENT} Unsupported arguments: ${arg_UNPARSED_ARGUMENTS}") 556 | endif() 557 | 558 | string(TOLOWER ${contentName} contentNameLower) 559 | set(prefix "_FetchContent_${contentNameLower}") 560 | 561 | set(propertyName "${prefix}_sourceDir") 562 | define_property( 563 | GLOBAL 564 | PROPERTY ${propertyName} 565 | BRIEF_DOCS "Internal implementation detail of FetchContent_Populate()" 566 | FULL_DOCS "Details used by FetchContent_Populate() for ${contentName}" 567 | ) 568 | set_property(GLOBAL PROPERTY ${propertyName} "${arg_SOURCE_DIR}") 569 | 570 | set(propertyName "${prefix}_binaryDir") 571 | define_property( 572 | GLOBAL 573 | PROPERTY ${propertyName} 574 | BRIEF_DOCS "Internal implementation detail of FetchContent_Populate()" 575 | FULL_DOCS "Details used by FetchContent_Populate() for ${contentName}" 576 | ) 577 | set_property(GLOBAL PROPERTY ${propertyName} "${arg_BINARY_DIR}") 578 | 579 | set(propertyName "${prefix}_populated") 580 | define_property( 581 | GLOBAL 582 | PROPERTY ${propertyName} 583 | BRIEF_DOCS "Internal implementation detail of FetchContent_Populate()" 584 | FULL_DOCS "Details used by FetchContent_Populate() for ${contentName}" 585 | ) 586 | set_property(GLOBAL PROPERTY ${propertyName} TRUE) 587 | endfunction() 588 | 589 | # Download and add a package from source 590 | function(CPMAddPackage) 591 | cpm_set_policies() 592 | 593 | list(LENGTH ARGN argnLength) 594 | if(argnLength EQUAL 1) 595 | cpm_parse_add_package_single_arg("${ARGN}" ARGN) 596 | 597 | # The shorthand syntax implies EXCLUDE_FROM_ALL and SYSTEM 598 | set(ARGN "${ARGN};EXCLUDE_FROM_ALL;YES;SYSTEM;YES;") 599 | endif() 600 | 601 | set(oneValueArgs 602 | NAME 603 | FORCE 604 | VERSION 605 | GIT_TAG 606 | DOWNLOAD_ONLY 607 | GITHUB_REPOSITORY 608 | GITLAB_REPOSITORY 609 | BITBUCKET_REPOSITORY 610 | GIT_REPOSITORY 611 | SOURCE_DIR 612 | FIND_PACKAGE_ARGUMENTS 613 | NO_CACHE 614 | SYSTEM 615 | GIT_SHALLOW 616 | EXCLUDE_FROM_ALL 617 | SOURCE_SUBDIR 618 | CUSTOM_CACHE_KEY 619 | ) 620 | 621 | set(multiValueArgs URL OPTIONS DOWNLOAD_COMMAND PATCHES) 622 | 623 | cmake_parse_arguments(CPM_ARGS "" "${oneValueArgs}" "${multiValueArgs}" "${ARGN}") 624 | 625 | # Set default values for arguments 626 | 627 | if(NOT DEFINED CPM_ARGS_VERSION) 628 | if(DEFINED CPM_ARGS_GIT_TAG) 629 | cpm_get_version_from_git_tag("${CPM_ARGS_GIT_TAG}" CPM_ARGS_VERSION) 630 | endif() 631 | endif() 632 | 633 | if(CPM_ARGS_DOWNLOAD_ONLY) 634 | set(DOWNLOAD_ONLY ${CPM_ARGS_DOWNLOAD_ONLY}) 635 | else() 636 | set(DOWNLOAD_ONLY NO) 637 | endif() 638 | 639 | if(DEFINED CPM_ARGS_GITHUB_REPOSITORY) 640 | set(CPM_ARGS_GIT_REPOSITORY "https://github.com/${CPM_ARGS_GITHUB_REPOSITORY}.git") 641 | elseif(DEFINED CPM_ARGS_GITLAB_REPOSITORY) 642 | set(CPM_ARGS_GIT_REPOSITORY "https://gitlab.com/${CPM_ARGS_GITLAB_REPOSITORY}.git") 643 | elseif(DEFINED CPM_ARGS_BITBUCKET_REPOSITORY) 644 | set(CPM_ARGS_GIT_REPOSITORY "https://bitbucket.org/${CPM_ARGS_BITBUCKET_REPOSITORY}.git") 645 | endif() 646 | 647 | if(DEFINED CPM_ARGS_GIT_REPOSITORY) 648 | list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_REPOSITORY ${CPM_ARGS_GIT_REPOSITORY}) 649 | if(NOT DEFINED CPM_ARGS_GIT_TAG) 650 | set(CPM_ARGS_GIT_TAG v${CPM_ARGS_VERSION}) 651 | endif() 652 | 653 | # If a name wasn't provided, try to infer it from the git repo 654 | if(NOT DEFINED CPM_ARGS_NAME) 655 | cpm_package_name_from_git_uri(${CPM_ARGS_GIT_REPOSITORY} CPM_ARGS_NAME) 656 | endif() 657 | endif() 658 | 659 | set(CPM_SKIP_FETCH FALSE) 660 | 661 | if(DEFINED CPM_ARGS_GIT_TAG) 662 | list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_TAG ${CPM_ARGS_GIT_TAG}) 663 | # If GIT_SHALLOW is explicitly specified, honor the value. 664 | if(DEFINED CPM_ARGS_GIT_SHALLOW) 665 | list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_SHALLOW ${CPM_ARGS_GIT_SHALLOW}) 666 | endif() 667 | endif() 668 | 669 | if(DEFINED CPM_ARGS_URL) 670 | # If a name or version aren't provided, try to infer them from the URL 671 | list(GET CPM_ARGS_URL 0 firstUrl) 672 | cpm_package_name_and_ver_from_url(${firstUrl} nameFromUrl verFromUrl) 673 | # If we fail to obtain name and version from the first URL, we could try other URLs if any. 674 | # However multiple URLs are expected to be quite rare, so for now we won't bother. 675 | 676 | # If the caller provided their own name and version, they trump the inferred ones. 677 | if(NOT DEFINED CPM_ARGS_NAME) 678 | set(CPM_ARGS_NAME ${nameFromUrl}) 679 | endif() 680 | if(NOT DEFINED CPM_ARGS_VERSION) 681 | set(CPM_ARGS_VERSION ${verFromUrl}) 682 | endif() 683 | 684 | list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS URL "${CPM_ARGS_URL}") 685 | endif() 686 | 687 | # Check for required arguments 688 | 689 | if(NOT DEFINED CPM_ARGS_NAME) 690 | message( 691 | FATAL_ERROR 692 | "${CPM_INDENT} 'NAME' was not provided and couldn't be automatically inferred for package added with arguments: '${ARGN}'" 693 | ) 694 | endif() 695 | 696 | # Check if package has been added before 697 | cpm_check_if_package_already_added(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}") 698 | if(CPM_PACKAGE_ALREADY_ADDED) 699 | cpm_export_variables(${CPM_ARGS_NAME}) 700 | return() 701 | endif() 702 | 703 | # Check for manual overrides 704 | if(NOT CPM_ARGS_FORCE AND NOT "${CPM_${CPM_ARGS_NAME}_SOURCE}" STREQUAL "") 705 | set(PACKAGE_SOURCE ${CPM_${CPM_ARGS_NAME}_SOURCE}) 706 | set(CPM_${CPM_ARGS_NAME}_SOURCE "") 707 | CPMAddPackage( 708 | NAME "${CPM_ARGS_NAME}" 709 | SOURCE_DIR "${PACKAGE_SOURCE}" 710 | EXCLUDE_FROM_ALL "${CPM_ARGS_EXCLUDE_FROM_ALL}" 711 | SYSTEM "${CPM_ARGS_SYSTEM}" 712 | PATCHES "${CPM_ARGS_PATCHES}" 713 | OPTIONS "${CPM_ARGS_OPTIONS}" 714 | SOURCE_SUBDIR "${CPM_ARGS_SOURCE_SUBDIR}" 715 | DOWNLOAD_ONLY "${DOWNLOAD_ONLY}" 716 | FORCE True 717 | ) 718 | cpm_export_variables(${CPM_ARGS_NAME}) 719 | return() 720 | endif() 721 | 722 | # Check for available declaration 723 | if(NOT CPM_ARGS_FORCE AND NOT "${CPM_DECLARATION_${CPM_ARGS_NAME}}" STREQUAL "") 724 | set(declaration ${CPM_DECLARATION_${CPM_ARGS_NAME}}) 725 | set(CPM_DECLARATION_${CPM_ARGS_NAME} "") 726 | CPMAddPackage(${declaration}) 727 | cpm_export_variables(${CPM_ARGS_NAME}) 728 | # checking again to ensure version and option compatibility 729 | cpm_check_if_package_already_added(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}") 730 | return() 731 | endif() 732 | 733 | if(NOT CPM_ARGS_FORCE) 734 | if(CPM_USE_LOCAL_PACKAGES OR CPM_LOCAL_PACKAGES_ONLY) 735 | cpm_find_package(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}" ${CPM_ARGS_FIND_PACKAGE_ARGUMENTS}) 736 | 737 | if(CPM_PACKAGE_FOUND) 738 | cpm_export_variables(${CPM_ARGS_NAME}) 739 | return() 740 | endif() 741 | 742 | if(CPM_LOCAL_PACKAGES_ONLY) 743 | message( 744 | SEND_ERROR 745 | "${CPM_INDENT} ${CPM_ARGS_NAME} not found via find_package(${CPM_ARGS_NAME} ${CPM_ARGS_VERSION})" 746 | ) 747 | endif() 748 | endif() 749 | endif() 750 | 751 | CPMRegisterPackage("${CPM_ARGS_NAME}" "${CPM_ARGS_VERSION}") 752 | 753 | if(DEFINED CPM_ARGS_GIT_TAG) 754 | set(PACKAGE_INFO "${CPM_ARGS_GIT_TAG}") 755 | elseif(DEFINED CPM_ARGS_SOURCE_DIR) 756 | set(PACKAGE_INFO "${CPM_ARGS_SOURCE_DIR}") 757 | else() 758 | set(PACKAGE_INFO "${CPM_ARGS_VERSION}") 759 | endif() 760 | 761 | if(DEFINED FETCHCONTENT_BASE_DIR) 762 | # respect user's FETCHCONTENT_BASE_DIR if set 763 | set(CPM_FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR}) 764 | else() 765 | set(CPM_FETCHCONTENT_BASE_DIR ${CMAKE_BINARY_DIR}/_deps) 766 | endif() 767 | 768 | cpm_add_patches(${CPM_ARGS_PATCHES}) 769 | 770 | if(DEFINED CPM_ARGS_DOWNLOAD_COMMAND) 771 | list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS DOWNLOAD_COMMAND ${CPM_ARGS_DOWNLOAD_COMMAND}) 772 | elseif(DEFINED CPM_ARGS_SOURCE_DIR) 773 | list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS SOURCE_DIR ${CPM_ARGS_SOURCE_DIR}) 774 | if(NOT IS_ABSOLUTE ${CPM_ARGS_SOURCE_DIR}) 775 | # Expand `CPM_ARGS_SOURCE_DIR` relative path. This is important because EXISTS doesn't work 776 | # for relative paths. 777 | get_filename_component( 778 | source_directory ${CPM_ARGS_SOURCE_DIR} REALPATH BASE_DIR ${CMAKE_CURRENT_BINARY_DIR} 779 | ) 780 | else() 781 | set(source_directory ${CPM_ARGS_SOURCE_DIR}) 782 | endif() 783 | if(NOT EXISTS ${source_directory}) 784 | string(TOLOWER ${CPM_ARGS_NAME} lower_case_name) 785 | # remove timestamps so CMake will re-download the dependency 786 | file(REMOVE_RECURSE "${CPM_FETCHCONTENT_BASE_DIR}/${lower_case_name}-subbuild") 787 | endif() 788 | elseif(CPM_SOURCE_CACHE AND NOT CPM_ARGS_NO_CACHE) 789 | string(TOLOWER ${CPM_ARGS_NAME} lower_case_name) 790 | set(origin_parameters ${CPM_ARGS_UNPARSED_ARGUMENTS}) 791 | list(SORT origin_parameters) 792 | if(CPM_ARGS_CUSTOM_CACHE_KEY) 793 | # Application set a custom unique directory name 794 | set(download_directory ${CPM_SOURCE_CACHE}/${lower_case_name}/${CPM_ARGS_CUSTOM_CACHE_KEY}) 795 | elseif(CPM_USE_NAMED_CACHE_DIRECTORIES) 796 | string(SHA1 origin_hash "${origin_parameters};NEW_CACHE_STRUCTURE_TAG") 797 | set(download_directory ${CPM_SOURCE_CACHE}/${lower_case_name}/${origin_hash}/${CPM_ARGS_NAME}) 798 | else() 799 | string(SHA1 origin_hash "${origin_parameters}") 800 | set(download_directory ${CPM_SOURCE_CACHE}/${lower_case_name}/${origin_hash}) 801 | endif() 802 | # Expand `download_directory` relative path. This is important because EXISTS doesn't work for 803 | # relative paths. 804 | get_filename_component(download_directory ${download_directory} ABSOLUTE) 805 | list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS SOURCE_DIR ${download_directory}) 806 | 807 | if(CPM_SOURCE_CACHE) 808 | file(LOCK ${download_directory}/../cmake.lock) 809 | endif() 810 | 811 | if(EXISTS ${download_directory}) 812 | if(CPM_SOURCE_CACHE) 813 | file(LOCK ${download_directory}/../cmake.lock RELEASE) 814 | endif() 815 | 816 | cpm_store_fetch_properties( 817 | ${CPM_ARGS_NAME} "${download_directory}" 818 | "${CPM_FETCHCONTENT_BASE_DIR}/${lower_case_name}-build" 819 | ) 820 | cpm_get_fetch_properties("${CPM_ARGS_NAME}") 821 | 822 | if(DEFINED CPM_ARGS_GIT_TAG AND NOT (PATCH_COMMAND IN_LIST CPM_ARGS_UNPARSED_ARGUMENTS)) 823 | # warn if cache has been changed since checkout 824 | cpm_check_git_working_dir_is_clean(${download_directory} ${CPM_ARGS_GIT_TAG} IS_CLEAN) 825 | if(NOT ${IS_CLEAN}) 826 | message( 827 | WARNING "${CPM_INDENT} Cache for ${CPM_ARGS_NAME} (${download_directory}) is dirty" 828 | ) 829 | endif() 830 | endif() 831 | 832 | cpm_add_subdirectory( 833 | "${CPM_ARGS_NAME}" 834 | "${DOWNLOAD_ONLY}" 835 | "${${CPM_ARGS_NAME}_SOURCE_DIR}/${CPM_ARGS_SOURCE_SUBDIR}" 836 | "${${CPM_ARGS_NAME}_BINARY_DIR}" 837 | "${CPM_ARGS_EXCLUDE_FROM_ALL}" 838 | "${CPM_ARGS_SYSTEM}" 839 | "${CPM_ARGS_OPTIONS}" 840 | ) 841 | set(PACKAGE_INFO "${PACKAGE_INFO} at ${download_directory}") 842 | 843 | # As the source dir is already cached/populated, we override the call to FetchContent. 844 | set(CPM_SKIP_FETCH TRUE) 845 | cpm_override_fetchcontent( 846 | "${lower_case_name}" SOURCE_DIR "${${CPM_ARGS_NAME}_SOURCE_DIR}/${CPM_ARGS_SOURCE_SUBDIR}" 847 | BINARY_DIR "${${CPM_ARGS_NAME}_BINARY_DIR}" 848 | ) 849 | 850 | else() 851 | # Enable shallow clone when GIT_TAG is not a commit hash. Our guess may not be accurate, but 852 | # it should guarantee no commit hash get mis-detected. 853 | if(NOT DEFINED CPM_ARGS_GIT_SHALLOW) 854 | cpm_is_git_tag_commit_hash("${CPM_ARGS_GIT_TAG}" IS_HASH) 855 | if(NOT ${IS_HASH}) 856 | list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_SHALLOW TRUE) 857 | endif() 858 | endif() 859 | 860 | # remove timestamps so CMake will re-download the dependency 861 | file(REMOVE_RECURSE ${CPM_FETCHCONTENT_BASE_DIR}/${lower_case_name}-subbuild) 862 | set(PACKAGE_INFO "${PACKAGE_INFO} to ${download_directory}") 863 | endif() 864 | endif() 865 | 866 | cpm_create_module_file(${CPM_ARGS_NAME} "CPMAddPackage(\"${ARGN}\")") 867 | 868 | if(CPM_PACKAGE_LOCK_ENABLED) 869 | if((CPM_ARGS_VERSION AND NOT CPM_ARGS_SOURCE_DIR) OR CPM_INCLUDE_ALL_IN_PACKAGE_LOCK) 870 | cpm_add_to_package_lock(${CPM_ARGS_NAME} "${ARGN}") 871 | elseif(CPM_ARGS_SOURCE_DIR) 872 | cpm_add_comment_to_package_lock(${CPM_ARGS_NAME} "local directory") 873 | else() 874 | cpm_add_comment_to_package_lock(${CPM_ARGS_NAME} "${ARGN}") 875 | endif() 876 | endif() 877 | 878 | cpm_message( 879 | STATUS "${CPM_INDENT} Adding package ${CPM_ARGS_NAME}@${CPM_ARGS_VERSION} (${PACKAGE_INFO})" 880 | ) 881 | 882 | if(NOT CPM_SKIP_FETCH) 883 | # CMake 3.28 added EXCLUDE, SYSTEM (3.25), and SOURCE_SUBDIR (3.18) to FetchContent_Declare. 884 | # Calling FetchContent_MakeAvailable will then internally forward these options to 885 | # add_subdirectory. Up until these changes, we had to call FetchContent_Populate and 886 | # add_subdirectory separately, which is no longer necessary and has been deprecated as of 3.30. 887 | # A Bug in CMake prevents us to use the non-deprecated functions until 3.30.3. 888 | set(fetchContentDeclareExtraArgs "") 889 | if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.30.3") 890 | if(${CPM_ARGS_EXCLUDE_FROM_ALL}) 891 | list(APPEND fetchContentDeclareExtraArgs EXCLUDE_FROM_ALL) 892 | endif() 893 | if(${CPM_ARGS_SYSTEM}) 894 | list(APPEND fetchContentDeclareExtraArgs SYSTEM) 895 | endif() 896 | if(DEFINED CPM_ARGS_SOURCE_SUBDIR) 897 | list(APPEND fetchContentDeclareExtraArgs SOURCE_SUBDIR ${CPM_ARGS_SOURCE_SUBDIR}) 898 | endif() 899 | # For CMake version <3.28 OPTIONS are parsed in cpm_add_subdirectory 900 | if(CPM_ARGS_OPTIONS AND NOT DOWNLOAD_ONLY) 901 | foreach(OPTION ${CPM_ARGS_OPTIONS}) 902 | cpm_parse_option("${OPTION}") 903 | set(${OPTION_KEY} "${OPTION_VALUE}") 904 | endforeach() 905 | endif() 906 | endif() 907 | cpm_declare_fetch( 908 | "${CPM_ARGS_NAME}" ${fetchContentDeclareExtraArgs} "${CPM_ARGS_UNPARSED_ARGUMENTS}" 909 | ) 910 | 911 | cpm_fetch_package("${CPM_ARGS_NAME}" ${DOWNLOAD_ONLY} populated ${CPM_ARGS_UNPARSED_ARGUMENTS}) 912 | if(CPM_SOURCE_CACHE AND download_directory) 913 | file(LOCK ${download_directory}/../cmake.lock RELEASE) 914 | endif() 915 | if(${populated} AND ${CMAKE_VERSION} VERSION_LESS "3.30.3") 916 | cpm_add_subdirectory( 917 | "${CPM_ARGS_NAME}" 918 | "${DOWNLOAD_ONLY}" 919 | "${${CPM_ARGS_NAME}_SOURCE_DIR}/${CPM_ARGS_SOURCE_SUBDIR}" 920 | "${${CPM_ARGS_NAME}_BINARY_DIR}" 921 | "${CPM_ARGS_EXCLUDE_FROM_ALL}" 922 | "${CPM_ARGS_SYSTEM}" 923 | "${CPM_ARGS_OPTIONS}" 924 | ) 925 | endif() 926 | cpm_get_fetch_properties("${CPM_ARGS_NAME}") 927 | endif() 928 | 929 | set(${CPM_ARGS_NAME}_ADDED YES) 930 | cpm_export_variables("${CPM_ARGS_NAME}") 931 | endfunction() 932 | 933 | # Fetch a previously declared package 934 | macro(CPMGetPackage Name) 935 | if(DEFINED "CPM_DECLARATION_${Name}") 936 | CPMAddPackage(NAME ${Name}) 937 | else() 938 | message(SEND_ERROR "${CPM_INDENT} Cannot retrieve package ${Name}: no declaration available") 939 | endif() 940 | endmacro() 941 | 942 | # export variables available to the caller to the parent scope expects ${CPM_ARGS_NAME} to be set 943 | macro(cpm_export_variables name) 944 | set(${name}_SOURCE_DIR 945 | "${${name}_SOURCE_DIR}" 946 | PARENT_SCOPE 947 | ) 948 | set(${name}_BINARY_DIR 949 | "${${name}_BINARY_DIR}" 950 | PARENT_SCOPE 951 | ) 952 | set(${name}_ADDED 953 | "${${name}_ADDED}" 954 | PARENT_SCOPE 955 | ) 956 | set(CPM_LAST_PACKAGE_NAME 957 | "${name}" 958 | PARENT_SCOPE 959 | ) 960 | endmacro() 961 | 962 | # declares a package, so that any call to CPMAddPackage for the package name will use these 963 | # arguments instead. Previous declarations will not be overridden. 964 | macro(CPMDeclarePackage Name) 965 | if(NOT DEFINED "CPM_DECLARATION_${Name}") 966 | set("CPM_DECLARATION_${Name}" "${ARGN}") 967 | endif() 968 | endmacro() 969 | 970 | function(cpm_add_to_package_lock Name) 971 | if(NOT CPM_DONT_CREATE_PACKAGE_LOCK) 972 | cpm_prettify_package_arguments(PRETTY_ARGN false ${ARGN}) 973 | file(APPEND ${CPM_PACKAGE_LOCK_FILE} "# ${Name}\nCPMDeclarePackage(${Name}\n${PRETTY_ARGN})\n") 974 | endif() 975 | endfunction() 976 | 977 | function(cpm_add_comment_to_package_lock Name) 978 | if(NOT CPM_DONT_CREATE_PACKAGE_LOCK) 979 | cpm_prettify_package_arguments(PRETTY_ARGN true ${ARGN}) 980 | file(APPEND ${CPM_PACKAGE_LOCK_FILE} 981 | "# ${Name} (unversioned)\n# CPMDeclarePackage(${Name}\n${PRETTY_ARGN}#)\n" 982 | ) 983 | endif() 984 | endfunction() 985 | 986 | # includes the package lock file if it exists and creates a target `cpm-update-package-lock` to 987 | # update it 988 | macro(CPMUsePackageLock file) 989 | if(NOT CPM_DONT_CREATE_PACKAGE_LOCK) 990 | get_filename_component(CPM_ABSOLUTE_PACKAGE_LOCK_PATH ${file} ABSOLUTE) 991 | if(EXISTS ${CPM_ABSOLUTE_PACKAGE_LOCK_PATH}) 992 | include(${CPM_ABSOLUTE_PACKAGE_LOCK_PATH}) 993 | endif() 994 | if(NOT TARGET cpm-update-package-lock) 995 | add_custom_target( 996 | cpm-update-package-lock COMMAND ${CMAKE_COMMAND} -E copy ${CPM_PACKAGE_LOCK_FILE} 997 | ${CPM_ABSOLUTE_PACKAGE_LOCK_PATH} 998 | ) 999 | endif() 1000 | set(CPM_PACKAGE_LOCK_ENABLED true) 1001 | endif() 1002 | endmacro() 1003 | 1004 | # registers a package that has been added to CPM 1005 | function(CPMRegisterPackage PACKAGE VERSION) 1006 | list(APPEND CPM_PACKAGES ${PACKAGE}) 1007 | set(CPM_PACKAGES 1008 | ${CPM_PACKAGES} 1009 | CACHE INTERNAL "" 1010 | ) 1011 | set("CPM_PACKAGE_${PACKAGE}_VERSION" 1012 | ${VERSION} 1013 | CACHE INTERNAL "" 1014 | ) 1015 | endfunction() 1016 | 1017 | # retrieve the current version of the package to ${OUTPUT} 1018 | function(CPMGetPackageVersion PACKAGE OUTPUT) 1019 | set(${OUTPUT} 1020 | "${CPM_PACKAGE_${PACKAGE}_VERSION}" 1021 | PARENT_SCOPE 1022 | ) 1023 | endfunction() 1024 | 1025 | # declares a package in FetchContent_Declare 1026 | function(cpm_declare_fetch PACKAGE) 1027 | if(${CPM_DRY_RUN}) 1028 | cpm_message(STATUS "${CPM_INDENT} Package not declared (dry run)") 1029 | return() 1030 | endif() 1031 | 1032 | FetchContent_Declare(${PACKAGE} ${ARGN}) 1033 | endfunction() 1034 | 1035 | # returns properties for a package previously defined by cpm_declare_fetch 1036 | function(cpm_get_fetch_properties PACKAGE) 1037 | if(${CPM_DRY_RUN}) 1038 | return() 1039 | endif() 1040 | 1041 | set(${PACKAGE}_SOURCE_DIR 1042 | "${CPM_PACKAGE_${PACKAGE}_SOURCE_DIR}" 1043 | PARENT_SCOPE 1044 | ) 1045 | set(${PACKAGE}_BINARY_DIR 1046 | "${CPM_PACKAGE_${PACKAGE}_BINARY_DIR}" 1047 | PARENT_SCOPE 1048 | ) 1049 | endfunction() 1050 | 1051 | function(cpm_store_fetch_properties PACKAGE source_dir binary_dir) 1052 | if(${CPM_DRY_RUN}) 1053 | return() 1054 | endif() 1055 | 1056 | set(CPM_PACKAGE_${PACKAGE}_SOURCE_DIR 1057 | "${source_dir}" 1058 | CACHE INTERNAL "" 1059 | ) 1060 | set(CPM_PACKAGE_${PACKAGE}_BINARY_DIR 1061 | "${binary_dir}" 1062 | CACHE INTERNAL "" 1063 | ) 1064 | endfunction() 1065 | 1066 | # adds a package as a subdirectory if viable, according to provided options 1067 | function( 1068 | cpm_add_subdirectory 1069 | PACKAGE 1070 | DOWNLOAD_ONLY 1071 | SOURCE_DIR 1072 | BINARY_DIR 1073 | EXCLUDE 1074 | SYSTEM 1075 | OPTIONS 1076 | ) 1077 | 1078 | if(NOT DOWNLOAD_ONLY AND EXISTS ${SOURCE_DIR}/CMakeLists.txt) 1079 | set(addSubdirectoryExtraArgs "") 1080 | if(EXCLUDE) 1081 | list(APPEND addSubdirectoryExtraArgs EXCLUDE_FROM_ALL) 1082 | endif() 1083 | if("${SYSTEM}" AND "${CMAKE_VERSION}" VERSION_GREATER_EQUAL "3.25") 1084 | # https://cmake.org/cmake/help/latest/prop_dir/SYSTEM.html#prop_dir:SYSTEM 1085 | list(APPEND addSubdirectoryExtraArgs SYSTEM) 1086 | endif() 1087 | if(OPTIONS) 1088 | foreach(OPTION ${OPTIONS}) 1089 | cpm_parse_option("${OPTION}") 1090 | set(${OPTION_KEY} "${OPTION_VALUE}") 1091 | endforeach() 1092 | endif() 1093 | set(CPM_OLD_INDENT "${CPM_INDENT}") 1094 | set(CPM_INDENT "${CPM_INDENT} ${PACKAGE}:") 1095 | add_subdirectory(${SOURCE_DIR} ${BINARY_DIR} ${addSubdirectoryExtraArgs}) 1096 | set(CPM_INDENT "${CPM_OLD_INDENT}") 1097 | endif() 1098 | endfunction() 1099 | 1100 | # downloads a previously declared package via FetchContent and exports the variables 1101 | # `${PACKAGE}_SOURCE_DIR` and `${PACKAGE}_BINARY_DIR` to the parent scope 1102 | function(cpm_fetch_package PACKAGE DOWNLOAD_ONLY populated) 1103 | set(${populated} 1104 | FALSE 1105 | PARENT_SCOPE 1106 | ) 1107 | if(${CPM_DRY_RUN}) 1108 | cpm_message(STATUS "${CPM_INDENT} Package ${PACKAGE} not fetched (dry run)") 1109 | return() 1110 | endif() 1111 | 1112 | FetchContent_GetProperties(${PACKAGE}) 1113 | 1114 | string(TOLOWER "${PACKAGE}" lower_case_name) 1115 | 1116 | if(NOT ${lower_case_name}_POPULATED) 1117 | if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.30.3") 1118 | if(DOWNLOAD_ONLY) 1119 | # MakeAvailable will call add_subdirectory internally which is not what we want when 1120 | # DOWNLOAD_ONLY is set. Populate will only download the dependency without adding it to the 1121 | # build 1122 | FetchContent_Populate( 1123 | ${PACKAGE} 1124 | SOURCE_DIR "${CPM_FETCHCONTENT_BASE_DIR}/${lower_case_name}-src" 1125 | BINARY_DIR "${CPM_FETCHCONTENT_BASE_DIR}/${lower_case_name}-build" 1126 | SUBBUILD_DIR "${CPM_FETCHCONTENT_BASE_DIR}/${lower_case_name}-subbuild" 1127 | ${ARGN} 1128 | ) 1129 | else() 1130 | FetchContent_MakeAvailable(${PACKAGE}) 1131 | endif() 1132 | else() 1133 | FetchContent_Populate(${PACKAGE}) 1134 | endif() 1135 | set(${populated} 1136 | TRUE 1137 | PARENT_SCOPE 1138 | ) 1139 | endif() 1140 | 1141 | cpm_store_fetch_properties( 1142 | ${CPM_ARGS_NAME} ${${lower_case_name}_SOURCE_DIR} ${${lower_case_name}_BINARY_DIR} 1143 | ) 1144 | 1145 | set(${PACKAGE}_SOURCE_DIR 1146 | ${${lower_case_name}_SOURCE_DIR} 1147 | PARENT_SCOPE 1148 | ) 1149 | set(${PACKAGE}_BINARY_DIR 1150 | ${${lower_case_name}_BINARY_DIR} 1151 | PARENT_SCOPE 1152 | ) 1153 | endfunction() 1154 | 1155 | # splits a package option 1156 | function(cpm_parse_option OPTION) 1157 | string(REGEX MATCH "^[^ ]+" OPTION_KEY "${OPTION}") 1158 | string(LENGTH "${OPTION}" OPTION_LENGTH) 1159 | string(LENGTH "${OPTION_KEY}" OPTION_KEY_LENGTH) 1160 | if(OPTION_KEY_LENGTH STREQUAL OPTION_LENGTH) 1161 | # no value for key provided, assume user wants to set option to "ON" 1162 | set(OPTION_VALUE "ON") 1163 | else() 1164 | math(EXPR OPTION_KEY_LENGTH "${OPTION_KEY_LENGTH}+1") 1165 | string(SUBSTRING "${OPTION}" "${OPTION_KEY_LENGTH}" "-1" OPTION_VALUE) 1166 | endif() 1167 | set(OPTION_KEY 1168 | "${OPTION_KEY}" 1169 | PARENT_SCOPE 1170 | ) 1171 | set(OPTION_VALUE 1172 | "${OPTION_VALUE}" 1173 | PARENT_SCOPE 1174 | ) 1175 | endfunction() 1176 | 1177 | # guesses the package version from a git tag 1178 | function(cpm_get_version_from_git_tag GIT_TAG RESULT) 1179 | string(LENGTH ${GIT_TAG} length) 1180 | if(length EQUAL 40) 1181 | # GIT_TAG is probably a git hash 1182 | set(${RESULT} 1183 | 0 1184 | PARENT_SCOPE 1185 | ) 1186 | else() 1187 | string(REGEX MATCH "v?([0123456789.]*).*" _ ${GIT_TAG}) 1188 | set(${RESULT} 1189 | ${CMAKE_MATCH_1} 1190 | PARENT_SCOPE 1191 | ) 1192 | endif() 1193 | endfunction() 1194 | 1195 | # guesses if the git tag is a commit hash or an actual tag or a branch name. 1196 | function(cpm_is_git_tag_commit_hash GIT_TAG RESULT) 1197 | string(LENGTH "${GIT_TAG}" length) 1198 | # full hash has 40 characters, and short hash has at least 7 characters. 1199 | if(length LESS 7 OR length GREATER 40) 1200 | set(${RESULT} 1201 | 0 1202 | PARENT_SCOPE 1203 | ) 1204 | else() 1205 | if(${GIT_TAG} MATCHES "^[a-fA-F0-9]+$") 1206 | set(${RESULT} 1207 | 1 1208 | PARENT_SCOPE 1209 | ) 1210 | else() 1211 | set(${RESULT} 1212 | 0 1213 | PARENT_SCOPE 1214 | ) 1215 | endif() 1216 | endif() 1217 | endfunction() 1218 | 1219 | function(cpm_prettify_package_arguments OUT_VAR IS_IN_COMMENT) 1220 | set(oneValueArgs 1221 | NAME 1222 | FORCE 1223 | VERSION 1224 | GIT_TAG 1225 | DOWNLOAD_ONLY 1226 | GITHUB_REPOSITORY 1227 | GITLAB_REPOSITORY 1228 | BITBUCKET_REPOSITORY 1229 | GIT_REPOSITORY 1230 | SOURCE_DIR 1231 | FIND_PACKAGE_ARGUMENTS 1232 | NO_CACHE 1233 | SYSTEM 1234 | GIT_SHALLOW 1235 | EXCLUDE_FROM_ALL 1236 | SOURCE_SUBDIR 1237 | ) 1238 | set(multiValueArgs URL OPTIONS DOWNLOAD_COMMAND) 1239 | cmake_parse_arguments(CPM_ARGS "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) 1240 | 1241 | foreach(oneArgName ${oneValueArgs}) 1242 | if(DEFINED CPM_ARGS_${oneArgName}) 1243 | if(${IS_IN_COMMENT}) 1244 | string(APPEND PRETTY_OUT_VAR "#") 1245 | endif() 1246 | if(${oneArgName} STREQUAL "SOURCE_DIR") 1247 | string(REPLACE ${CMAKE_SOURCE_DIR} "\${CMAKE_SOURCE_DIR}" CPM_ARGS_${oneArgName} 1248 | ${CPM_ARGS_${oneArgName}} 1249 | ) 1250 | endif() 1251 | string(APPEND PRETTY_OUT_VAR " ${oneArgName} ${CPM_ARGS_${oneArgName}}\n") 1252 | endif() 1253 | endforeach() 1254 | foreach(multiArgName ${multiValueArgs}) 1255 | if(DEFINED CPM_ARGS_${multiArgName}) 1256 | if(${IS_IN_COMMENT}) 1257 | string(APPEND PRETTY_OUT_VAR "#") 1258 | endif() 1259 | string(APPEND PRETTY_OUT_VAR " ${multiArgName}\n") 1260 | foreach(singleOption ${CPM_ARGS_${multiArgName}}) 1261 | if(${IS_IN_COMMENT}) 1262 | string(APPEND PRETTY_OUT_VAR "#") 1263 | endif() 1264 | string(APPEND PRETTY_OUT_VAR " \"${singleOption}\"\n") 1265 | endforeach() 1266 | endif() 1267 | endforeach() 1268 | 1269 | if(NOT "${CPM_ARGS_UNPARSED_ARGUMENTS}" STREQUAL "") 1270 | if(${IS_IN_COMMENT}) 1271 | string(APPEND PRETTY_OUT_VAR "#") 1272 | endif() 1273 | string(APPEND PRETTY_OUT_VAR " ") 1274 | foreach(CPM_ARGS_UNPARSED_ARGUMENT ${CPM_ARGS_UNPARSED_ARGUMENTS}) 1275 | string(APPEND PRETTY_OUT_VAR " ${CPM_ARGS_UNPARSED_ARGUMENT}") 1276 | endforeach() 1277 | string(APPEND PRETTY_OUT_VAR "\n") 1278 | endif() 1279 | 1280 | set(${OUT_VAR} 1281 | ${PRETTY_OUT_VAR} 1282 | PARENT_SCOPE 1283 | ) 1284 | 1285 | endfunction() 1286 | -------------------------------------------------------------------------------- /integration_tests/cpm/Main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | bmp::Bitmap bitmap(512, 512); 5 | for (auto& pixel : bitmap) { 6 | pixel = bmp::Pixel(42, 69, 96); 7 | } 8 | bitmap.save("demo.bmp"); 9 | } -------------------------------------------------------------------------------- /integration_tests/cpm/README.md: -------------------------------------------------------------------------------- 1 | # Integration example using CPM 2 | 3 | CPM stands for [CMake Package Manager](https://github.com/cpm-cmake) which is a convenience wrapper over CMake's 4 | FetchContent module. You can download the CPM.cmake from the 5 | project [releases page](https://github.com/cpm-cmake/CPM.cmake/releases/latest). 6 | 7 | With CPM enabled, you can bring this library in using the following command: 8 | 9 | ```cmake 10 | CPMAddPackage("gh:baderouaich/BitmapPlusPlus#master") 11 | ``` 12 | 13 | And link it to your target using: 14 | 15 | ```cmake 16 | target_link_libraries(${TARGET} LINK_PRIVATE bpp::BitmapPlusPlus) 17 | ``` 18 | 19 | Note that your target needs to compile with C++17 or newer. After that, you can simply include the library in your code: 20 | 21 | ```cpp 22 | #include 23 | ``` 24 | -------------------------------------------------------------------------------- /integration_tests/fetchcontent/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(demo_fc) 3 | 4 | set(CMAKE_CXX_STANDARD 17) # C++17 or newer is required 5 | 6 | include(FetchContent) 7 | FetchContent_Declare(BitmapPlusPlus 8 | GIT_REPOSITORY "https://github.com/baderouaich/BitmapPlusPlus" 9 | GIT_TAG "master" 10 | ) 11 | FetchContent_MakeAvailable(BitmapPlusPlus) 12 | 13 | add_executable(${PROJECT_NAME} "${CMAKE_CURRENT_SOURCE_DIR}/Main.cpp") 14 | 15 | target_link_libraries(${PROJECT_NAME} LINK_PRIVATE bmp::BitmapPlusPlus) 16 | -------------------------------------------------------------------------------- /integration_tests/fetchcontent/Main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | bmp::Bitmap bitmap(512, 512); 5 | for (auto& pixel : bitmap) { 6 | pixel = bmp::Pixel(42, 69, 96); 7 | } 8 | bitmap.save("demo.bmp"); 9 | } -------------------------------------------------------------------------------- /integration_tests/fetchcontent/README.md: -------------------------------------------------------------------------------- 1 | # Integration example using FetchContent 2 | 3 | [FetchContent](https://cmake.org/cmake/help/latest/module/FetchContent.html) is a built-in CMake module that allows you to fetch and incorporate external dependencies directly into your project. 4 | It eliminates the need for additional package managers and simplifies dependency management. 5 | 6 | You can bring this library in using the following command: 7 | 8 | ```cmake 9 | include(FetchContent) 10 | FetchContent_Declare(BitmapPlusPlus 11 | GIT_REPOSITORY "https://github.com/baderouaich/BitmapPlusPlus" 12 | GIT_TAG "master" 13 | ) 14 | FetchContent_MakeAvailable(BitmapPlusPlus) 15 | ``` 16 | 17 | And link it to your target using: 18 | 19 | ```cmake 20 | target_link_libraries(${TARGET} LINK_PRIVATE bpp::BitmapPlusPlus) 21 | ``` 22 | 23 | Note that your target needs to compile with C++17 or newer. After that, you can simply include the library in your code: 24 | 25 | ```cpp 26 | #include 27 | ``` 28 | -------------------------------------------------------------------------------- /lib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | 3 | add_library(BitmapPlusPlus INTERFACE) 4 | target_include_directories(BitmapPlusPlus INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include") 5 | 6 | add_library(bmp::BitmapPlusPlus ALIAS BitmapPlusPlus) 7 | -------------------------------------------------------------------------------- /lib/include/BitmapPlusPlus.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include // std::*fstream 4 | #include // std::vector 5 | #include // std::unique_ptr 6 | #include // std::fill 7 | #include // std::int*_t 8 | #include // std::size_t 9 | #include // std::string 10 | #include // std::memcmp 11 | #include // std::filesystem::path 12 | #include // std::runtime_error 13 | #include // std::exchange 14 | 15 | namespace bmp { 16 | // Magic number for Bitmap .bmp 24 bpp files (24/8 = 3 = rgb colors only) 17 | static constexpr std::uint16_t BITMAP_BUFFER_MAGIC = 0x4D42; 18 | 19 | #pragma pack(push, 1) 20 | struct BitmapHeader { 21 | /* Bitmap file header structure */ 22 | std::uint16_t magic; /* Magic number for file always BM which is 0x4D42 */ 23 | std::uint32_t file_size; /* Size of file */ 24 | std::uint16_t reserved1; /* Reserved */ 25 | std::uint16_t reserved2; /* Reserved */ 26 | std::uint32_t offset_bits; /* Offset to bitmap data */ 27 | /* Bitmap file info structure */ 28 | std::uint32_t size; /* Size of info header */ 29 | std::int32_t width; /* Width of image */ 30 | std::int32_t height; /* Height of image */ 31 | std::uint16_t planes; /* Number of color planes */ 32 | std::uint16_t bits_per_pixel; /* Number of bits per pixel */ 33 | std::uint32_t compression; /* Type of compression to use */ 34 | std::uint32_t size_image; /* Size of image data */ 35 | std::int32_t x_pixels_per_meter; /* X pixels per meter */ 36 | std::int32_t y_pixels_per_meter; /* Y pixels per meter */ 37 | std::uint32_t clr_used; /* Number of colors used */ 38 | std::uint32_t clr_important; /* Number of important colors */ 39 | }; 40 | // Sanity check 41 | static_assert(sizeof(BitmapHeader) == 54, "Bitmap header size must be 54 bytes"); 42 | 43 | struct Pixel { 44 | std::uint8_t r; /* Blue value */ 45 | std::uint8_t g; /* Green value */ 46 | std::uint8_t b; /* Red value */ 47 | 48 | constexpr Pixel() noexcept : r(0), g(0), b(0) { 49 | } 50 | 51 | explicit constexpr Pixel(const std::int32_t rgb) noexcept : r((rgb >> 16) & 0xff), g((rgb >> 8) & 0xff), b((rgb >> 0x0) & 0xff) { 52 | } 53 | 54 | constexpr Pixel(const std::uint8_t red, const std::uint8_t green, const std::uint8_t blue) noexcept : r(red), g(green), b(blue) { 55 | } 56 | 57 | constexpr bool operator==(const Pixel &other) const noexcept { 58 | if (this == std::addressof(other)) 59 | return true; 60 | return r == other.r && g == other.g && b == other.b; 61 | } 62 | 63 | constexpr bool operator!=(const Pixel &other) const noexcept { return !((*this) == other); } 64 | }; 65 | 66 | static_assert(sizeof(Pixel) == 3, "Bitmap Pixel size must be 3 bytes"); 67 | #pragma pack(pop) 68 | 69 | static constexpr Pixel Aqua{0, 255, 255}; 70 | static constexpr Pixel Beige{245, 245, 220}; 71 | static constexpr Pixel Black{0, 0, 0}; 72 | static constexpr Pixel Blue{0, 0, 255}; 73 | static constexpr Pixel Brown{165, 42, 42}; 74 | static constexpr Pixel Chocolate{210, 105, 30}; 75 | static constexpr Pixel Coral{255, 127, 80}; 76 | static constexpr Pixel Crimson{220, 20, 60}; 77 | static constexpr Pixel Cyan{0, 255, 255}; 78 | static constexpr Pixel Firebrick{178, 34, 34}; 79 | static constexpr Pixel Gold{255, 215, 0}; 80 | static constexpr Pixel Gray{128, 128, 128}; 81 | static constexpr Pixel Green{0, 255, 0}; 82 | static constexpr Pixel Indigo{75, 0, 130}; 83 | static constexpr Pixel Lavender{230, 230, 250}; 84 | static constexpr Pixel Lime{0, 255, 0}; 85 | static constexpr Pixel Magenta{255, 0, 255}; 86 | static constexpr Pixel Maroon{128, 0, 0}; 87 | static constexpr Pixel Navy{0, 0, 128}; 88 | static constexpr Pixel Olive{128, 128, 0}; 89 | static constexpr Pixel Orange{255, 165, 0}; 90 | static constexpr Pixel Pink{255, 192, 203}; 91 | static constexpr Pixel Purple{128, 0, 128}; 92 | static constexpr Pixel Red{255, 0, 0}; 93 | static constexpr Pixel Salmon{250, 128, 114}; 94 | static constexpr Pixel Silver{192, 192, 192}; 95 | static constexpr Pixel Snow{255, 250, 250}; 96 | static constexpr Pixel Teal{0, 128, 128}; 97 | static constexpr Pixel Tomato{255, 99, 71}; 98 | static constexpr Pixel Turquoise{64, 224, 208}; 99 | static constexpr Pixel Violet{238, 130, 238}; 100 | static constexpr Pixel White{255, 255, 255}; 101 | static constexpr Pixel Wheat{245, 222, 179}; 102 | static constexpr Pixel Yellow{255, 255, 0}; 103 | 104 | class Exception : public std::runtime_error { 105 | public: 106 | explicit Exception(const std::string &message) : std::runtime_error(message) { 107 | } 108 | }; 109 | 110 | class Bitmap { 111 | public: 112 | Bitmap() noexcept : m_pixels(), m_width(0), m_height(0) { 113 | } 114 | 115 | explicit Bitmap(const std::string &filename) : m_pixels(), m_width(0), m_height(0) { 116 | this->load(filename); 117 | } 118 | 119 | Bitmap(const std::int32_t width, const std::int32_t height) 120 | : m_pixels(static_cast(width) * static_cast(height)), 121 | m_width(width), 122 | m_height(height) { 123 | if (width == 0 || height == 0) 124 | throw Exception("Bitmap width and height must be > 0"); 125 | } 126 | 127 | Bitmap(const Bitmap &other) = default; // Copy Constructor 128 | 129 | Bitmap(Bitmap &&other) noexcept 130 | : m_pixels(std::move(other.m_pixels)), 131 | m_width(std::exchange(other.m_width, 0)), 132 | m_height(std::exchange(other.m_height, 0)) { 133 | } 134 | 135 | virtual ~Bitmap() noexcept = default; 136 | 137 | public: /* Draw Primitives */ 138 | /** 139 | * Draw a line form (x1, y1) to (x2, y2) 140 | */ 141 | void draw_line(std::int32_t x1, std::int32_t y1, std::int32_t x2, std::int32_t y2, const Pixel color) { 142 | const std::int32_t dx = std::abs(x2 - x1); 143 | const std::int32_t dy = std::abs(y2 - y1); 144 | const std::int32_t sx = (x1 < x2) ? 1 : -1; 145 | const std::int32_t sy = (y1 < y2) ? 1 : -1; 146 | std::int32_t err = dx - dy; 147 | while (true) { 148 | m_pixels[IX(x1, y1)] = color; 149 | 150 | if (x1 == x2 && y1 == y2) { 151 | break; 152 | } 153 | 154 | int e2 = 2 * err; 155 | if (e2 > -dy) { 156 | err -= dy; 157 | x1 += sx; 158 | } 159 | if (e2 < dx) { 160 | err += dx; 161 | y1 += sy; 162 | } 163 | } 164 | } 165 | 166 | /** 167 | * Draw a filled rect 168 | */ 169 | void fill_rect(const std::int32_t x, const std::int32_t y, const std::int32_t width, const std::int32_t height, 170 | const Pixel color) { 171 | if (!in_bounds(x, y) || !in_bounds(x + (width - 1), y + (height - 1))) 172 | throw Exception( 173 | "Bitmap::fill_rect(" + std::to_string(x) + ", " + std::to_string(y) + ", " + std::to_string(width) + ", " + 174 | std::to_string(height) + "): x,y,w or h out of bounds"); 175 | 176 | for (std::int32_t dx = x; dx < x + width; ++dx) { 177 | for (std::int32_t dy = y; dy < y + height; ++dy) { 178 | m_pixels[IX(dx, dy)] = color; 179 | } 180 | } 181 | } 182 | 183 | /** 184 | * Draw a rect (not filled, border only) 185 | */ 186 | void draw_rect(const std::int32_t x, const std::int32_t y, const std::int32_t width, const std::int32_t height, 187 | const Pixel color) { 188 | if (!in_bounds(x, y) || !in_bounds(x + (width - 1), y + (height - 1))) 189 | throw Exception( 190 | "Bitmap::draw_rect(" + std::to_string(x) + ", " + std::to_string(y) + ", " + std::to_string(width) + ", " + 191 | std::to_string(height) + "): x,y,w or h out of bounds"); 192 | 193 | for (std::int32_t dx = x; dx < x + width; ++dx) { 194 | m_pixels[IX(dx, y)] = color; // top 195 | m_pixels[IX(dx, y + height - 1)] = color; // bottom 196 | } 197 | for (std::int32_t dy = y; dy < y + height; ++dy) { 198 | m_pixels[IX(x, dy)] = color; // left 199 | m_pixels[IX(x + width - 1, dy)] = color; // right 200 | } 201 | } 202 | 203 | 204 | /** 205 | * Draw a triangle (not filled, border only) 206 | */ 207 | void draw_triangle(const std::int32_t x1, const std::int32_t y1, 208 | const std::int32_t x2, const std::int32_t y2, 209 | const std::int32_t x3, const std::int32_t y3, 210 | const Pixel color) { 211 | if (!in_bounds(x1, y1) || !in_bounds(x2, y2) || !in_bounds(x3, y3)) 212 | throw Exception("Bitmap::draw_triangle: One or more points are out of bounds"); 213 | 214 | draw_line(x1, y1, x2, y2, color); 215 | draw_line(x2, y2, x3, y3, color); 216 | draw_line(x3, y3, x1, y1, color); 217 | } 218 | 219 | /** 220 | * Draw a filled triangle 221 | */ 222 | void fill_triangle(const std::int32_t x1, const std::int32_t y1, 223 | const std::int32_t x2, const std::int32_t y2, 224 | const std::int32_t x3, const std::int32_t y3, 225 | const Pixel color) { 226 | if (!in_bounds(x1, y1) || !in_bounds(x2, y2) || !in_bounds(x3, y3)) 227 | throw Exception("Bitmap::fill_triangle: One or more points are out of bounds"); 228 | 229 | // Sort the vertices by y-coordinate (top to bottom) 230 | std::vector > vertices = { 231 | {x1, y1}, 232 | {x2, y2}, 233 | {x3, y3}}; 234 | std::sort(vertices.begin(), vertices.end(), [](const auto &a, const auto &b) { 235 | return a.second < b.second; 236 | }); 237 | 238 | const auto [x_top, y_top] = vertices[0]; 239 | const auto [x_mid, y_mid] = vertices[1]; 240 | const auto [x_bot, y_bot] = vertices[2]; 241 | 242 | // Calculate the slopes of the left and right edges 243 | const float slope_left = static_cast(x_mid - x_top) / (y_mid - y_top); 244 | const float slope_right = static_cast(x_bot - x_top) / (y_bot - y_top); 245 | 246 | // Initialize the starting and ending x-coordinates for each scanline 247 | std::vector > scanlines(y_mid - y_top + 1); 248 | for (std::int32_t y = y_top; y <= y_mid; ++y) { 249 | const auto x_start = static_cast(x_top + (y - y_top) * slope_left); 250 | const auto x_end = static_cast(x_top + (y - y_top) * slope_right); 251 | scanlines[y - y_top] = {x_start, x_end}; 252 | } 253 | 254 | // Fill the upper part of the triangle 255 | for (std::int32_t y = y_top; y <= y_mid; ++y) { 256 | const std::int32_t x_start = scanlines[y - y_top].first; 257 | const std::int32_t x_end = scanlines[y - y_top].second; 258 | draw_line(x_start, y, x_end, y, color); 259 | } 260 | 261 | // Update the slope for the right edge of the triangle 262 | const float new_slope_right = static_cast(x_bot - x_mid) / (y_bot - y_mid); 263 | 264 | // Update the x-coordinates for the scanlines in the lower part of the triangle 265 | for (std::int32_t y = y_mid + 1; y <= y_bot; ++y) { 266 | const auto x_start = static_cast(x_mid + (y - y_mid) * slope_left); 267 | const auto x_end = static_cast(x_top + (y - y_top) * new_slope_right); 268 | scanlines[y - y_top] = {x_start, x_end}; 269 | } 270 | 271 | // Fill the lower part of the triangle 272 | for (std::int32_t y = y_mid + 1; y <= y_bot; ++y) { 273 | const std::int32_t x_start = scanlines[y - y_top].first; 274 | const std::int32_t x_end = scanlines[y - y_top].second; 275 | draw_line(x_start, y, x_end, y, color); 276 | } 277 | } 278 | 279 | /** 280 | * Draw a circle with a given center and radius 281 | */ 282 | void draw_circle(const std::int32_t center_x, const std::int32_t center_y, const std::int32_t radius, 283 | const Pixel color) { 284 | if (!in_bounds(center_x - radius, center_y - radius) || !in_bounds(center_x + radius, center_y + radius)) 285 | throw Exception("Bitmap::draw_circle: Circle exceeds bounds"); 286 | 287 | std::int32_t x = radius; 288 | std::int32_t y = 0; 289 | std::int32_t err = 0; 290 | 291 | while (x >= y) { 292 | // Draw pixels in all octants 293 | m_pixels[IX(center_x + x, center_y + y)] = color; 294 | m_pixels[IX(center_x + y, center_y + x)] = color; 295 | m_pixels[IX(center_x - y, center_y + x)] = color; 296 | m_pixels[IX(center_x - x, center_y + y)] = color; 297 | m_pixels[IX(center_x - x, center_y - y)] = color; 298 | m_pixels[IX(center_x - y, center_y - x)] = color; 299 | m_pixels[IX(center_x + y, center_y - x)] = color; 300 | m_pixels[IX(center_x + x, center_y - y)] = color; 301 | 302 | // Update error and y for the next pixel 303 | if (err <= 0) { 304 | y += 1; 305 | err += 2 * y + 1; 306 | } 307 | 308 | // Update error and x for the next pixel 309 | if (err > 0) { 310 | x -= 1; 311 | err -= 2 * x + 1; 312 | } 313 | } 314 | } 315 | 316 | /** 317 | * Fill a circle with a given center and radius 318 | */ 319 | void fill_circle(const std::int32_t center_x, const std::int32_t center_y, const std::int32_t radius, 320 | const Pixel color) { 321 | if (!in_bounds(center_x - radius, center_y - radius) || !in_bounds(center_x + radius, center_y + radius)) 322 | throw Exception("Bitmap::fill_circle: Circle exceeds bounds"); 323 | 324 | std::int32_t x = radius; 325 | std::int32_t y = 0; 326 | std::int32_t err = 0; 327 | 328 | while (x >= y) { 329 | // Fill scanlines in all octants 330 | for (std::int32_t i = center_x - x; i <= center_x + x; ++i) { 331 | m_pixels[IX(i, center_y + y)] = color; 332 | m_pixels[IX(i, center_y - y)] = color; 333 | } 334 | 335 | for (std::int32_t i = center_x - y; i <= center_x + y; ++i) { 336 | m_pixels[IX(i, center_y + x)] = color; 337 | m_pixels[IX(i, center_y - x)] = color; 338 | } 339 | 340 | // Update error and y for the next pixel 341 | if (err <= 0) { 342 | y += 1; 343 | err += 2 * y + 1; 344 | } 345 | 346 | // Update error and x for the next pixel 347 | if (err > 0) { 348 | x -= 1; 349 | err -= 2 * x + 1; 350 | } 351 | } 352 | } 353 | 354 | public: /* Accessors */ 355 | /** 356 | * Get pixel at position x,y 357 | */ 358 | Pixel &get(const std::int32_t x, const std::int32_t y) { 359 | if (!in_bounds(x, y)) 360 | throw Exception("Bitmap::get(" + std::to_string(x) + ", " + std::to_string(y) + "): x,y out of bounds"); 361 | return m_pixels[IX(x, y)]; 362 | } 363 | 364 | /** 365 | * Get const pixel at position x,y 366 | */ 367 | [[nodiscard]] const Pixel &get(const std::int32_t x, const std::int32_t y) const { 368 | if (!in_bounds(x, y)) 369 | throw Exception("Bitmap::get(" + std::to_string(x) + ", " + std::to_string(y) + "): x,y out of bounds"); 370 | return m_pixels[IX(x, y)]; 371 | } 372 | 373 | /** 374 | * Returns the width of the Bitmap image 375 | */ 376 | [[nodiscard]] std::int32_t width() const noexcept { return m_width; } 377 | 378 | /** 379 | * Returns the height of the Bitmap image 380 | */ 381 | [[nodiscard]] std::int32_t height() const noexcept { return m_height; } 382 | 383 | /** 384 | * Clears Bitmap pixels with an rgb color 385 | */ 386 | void clear(const Pixel pixel = Black) { 387 | std::fill(m_pixels.begin(), m_pixels.end(), pixel); 388 | } 389 | 390 | public: /* Operators */ 391 | const Pixel &operator[](const std::size_t i) const { return m_pixels[i]; } 392 | 393 | Pixel &operator[](const std::size_t i) { return m_pixels[i]; } 394 | 395 | bool operator!() const noexcept { return (m_pixels.empty()) || (m_width == 0) || (m_height == 0); } 396 | 397 | explicit operator bool() const noexcept { return !(*this); } 398 | 399 | bool operator==(const Bitmap &image) const { 400 | if (this == std::addressof(image)) { 401 | return true; 402 | } 403 | return (m_width == image.m_width) && 404 | (m_height == image.m_height) && 405 | (std::memcmp(m_pixels.data(), image.m_pixels.data(), sizeof(Pixel) * m_pixels.size()) == 0); 406 | } 407 | 408 | bool operator!=(const Bitmap &image) const { return !(*this == image); } 409 | 410 | Bitmap &operator=(const Bitmap &image) // Copy assignment operator 411 | { 412 | if (this != std::addressof(image)) { 413 | m_width = image.m_width; 414 | m_height = image.m_height; 415 | m_pixels = image.m_pixels; 416 | } 417 | return *this; 418 | } 419 | 420 | Bitmap &operator=(Bitmap &&image) noexcept { 421 | if (this != std::addressof(image)) { 422 | m_pixels = std::move(image.m_pixels); 423 | m_width = std::exchange(image.m_width, 0); 424 | m_height = std::exchange(image.m_height, 0); 425 | } 426 | return *this; 427 | } 428 | 429 | public: /** foreach iterators access */ 430 | [[nodiscard]] std::vector::iterator begin() noexcept { return m_pixels.begin(); } 431 | 432 | [[nodiscard]] std::vector::iterator end() noexcept { return m_pixels.end(); } 433 | 434 | [[nodiscard]] std::vector::const_iterator cbegin() const noexcept { return m_pixels.cbegin(); } 435 | 436 | [[nodiscard]] std::vector::const_iterator cend() const noexcept { return m_pixels.cend(); } 437 | 438 | [[nodiscard]] std::vector::reverse_iterator rbegin() noexcept { return m_pixels.rbegin(); } 439 | 440 | [[nodiscard]] std::vector::reverse_iterator rend() noexcept { return m_pixels.rend(); } 441 | 442 | [[nodiscard]] std::vector::const_reverse_iterator crbegin() const noexcept { return m_pixels.crbegin(); } 443 | 444 | [[nodiscard]] std::vector::const_reverse_iterator crend() const noexcept { return m_pixels.crend(); } 445 | 446 | public: /* Modifiers */ 447 | /** 448 | * Sets rgb color to pixel at position x,y 449 | * @throws bmp::Exception on error 450 | */ 451 | void set(const std::int32_t x, const std::int32_t y, const Pixel color) { 452 | if (!in_bounds(x, y)) { 453 | throw Exception("Bitmap::set(" + std::to_string(x) + ", " + std::to_string(y) + "): x,y out of bounds"); 454 | } 455 | m_pixels[IX(x, y)] = color; 456 | } 457 | 458 | 459 | /** 460 | * Vertically flips the bitmap and returns the flipped version 461 | * 462 | */ 463 | [[nodiscard("Bitmap::flip_v() is immutable")]] 464 | Bitmap flip_v() const { 465 | Bitmap finished(m_width, m_height); 466 | for (std::int32_t x = 0; x < m_width; ++x) { 467 | for (std::int32_t y = 0; y < m_height; ++y) { 468 | // Calculate the reverse y-index 469 | finished.m_pixels[IX(x, y)] = m_pixels[IX(x, m_height - 1 - y)]; 470 | } 471 | } 472 | return finished; 473 | } 474 | 475 | /** 476 | * Horizontally flips the bitmap and returns the flipped version 477 | * 478 | */ 479 | [[nodiscard("Bitmap::flip_h() is immutable")]] 480 | Bitmap flip_h() const { 481 | Bitmap finished(m_width, m_height); 482 | for (std::int32_t y = 0; y < m_height; ++y) { 483 | for (std::int32_t x = 0; x < m_width; ++x) { 484 | // Calculate the reverse x-index 485 | finished.m_pixels[IX(x, y)] = m_pixels[IX(m_width - 1 - x, y)]; 486 | } 487 | } 488 | return finished; 489 | } 490 | 491 | /** 492 | * Rotates the bitmap to the right and returns the rotated version 493 | * 494 | */ 495 | [[nodiscard("Bitmap::rotate_90_left() is immutable")]] 496 | Bitmap rotate_90_left() const { 497 | Bitmap finished(m_height, m_width); // Swap dimensions 498 | 499 | for (std::int32_t y = 0; y < m_height; ++y) { 500 | const std::int32_t y_offset = y * m_width; // Precompute row start index 501 | for (std::int32_t x = 0; x < m_width; ++x) { 502 | // Original pixel at (x, y) moves to (y, m_width - 1 - x) 503 | finished.m_pixels[(m_width - 1 - x) * m_height + y] = m_pixels[y_offset + x]; 504 | } 505 | } 506 | 507 | return finished; 508 | } 509 | 510 | /** 511 | * Rotates the bitmap to the left and returns the rotated version 512 | * 513 | */ 514 | [[nodiscard("Bitmap::rotate_90_right() is immutable")]] 515 | Bitmap rotate_90_right() const { 516 | Bitmap finished(m_height, m_width); // Swap dimensions 517 | for (std::int32_t y = 0; y < m_height; ++y) { 518 | const std::int32_t y_offset = y * m_width; // Precompute row start index 519 | for (std::int32_t x = 0; x < m_width; ++x) { 520 | finished.m_pixels[x * m_height + (m_height - 1 - y)] = m_pixels[y_offset + x]; 521 | } 522 | } 523 | 524 | return finished; 525 | } 526 | 527 | /** 528 | * Saves Bitmap pixels into a file 529 | * @throws bmp::Exception on error 530 | */ 531 | void save(const std::filesystem::path &filename) const { 532 | // Calculate row and bitmap size 533 | const std::int32_t row_size = m_width * 3 + m_width % 4; 534 | const std::uint32_t bitmap_size = row_size * m_height; 535 | 536 | // Construct bitmap header 537 | BitmapHeader header{}; 538 | /* Bitmap file header structure */ 539 | header.magic = BITMAP_BUFFER_MAGIC; 540 | header.file_size = bitmap_size + sizeof(BitmapHeader); 541 | header.reserved1 = 0; 542 | header.reserved2 = 0; 543 | header.offset_bits = sizeof(BitmapHeader); 544 | /* Bitmap file info structure */ 545 | header.size = 40; 546 | header.width = m_width; 547 | header.height = m_height; 548 | header.planes = 1; 549 | header.bits_per_pixel = sizeof(Pixel) * 8; // 24bpp 550 | header.compression = 0; 551 | header.size_image = bitmap_size; 552 | header.x_pixels_per_meter = 0; 553 | header.y_pixels_per_meter = 0; 554 | header.clr_used = 0; 555 | header.clr_important = 0; 556 | 557 | // Save bitmap to output file 558 | if (std::ofstream ofs{filename, std::ios::binary}; ofs.good()) { 559 | // Write Header 560 | ofs.write(reinterpret_cast(&header), sizeof(BitmapHeader)); 561 | if (!ofs.good()) { 562 | throw Exception("Bitmap::save(\"" + filename.string() + "\"): Failed to write bitmap header to file."); 563 | } 564 | 565 | // Write Pixels 566 | std::vector line(row_size); 567 | for (std::int32_t y = m_height - 1; y >= 0; --y) { 568 | std::size_t i = 0; 569 | for (std::int32_t x = 0; x < m_width; ++x) { 570 | const Pixel &color = m_pixels[IX(x, y)]; 571 | line[i++] = color.b; 572 | line[i++] = color.g; 573 | line[i++] = color.r; 574 | } 575 | ofs.write(reinterpret_cast(line.data()), line.size()); 576 | if (!ofs.good()) { 577 | throw Exception("Bitmap::save(\"" + filename.string() + "\"): Failed to write bitmap pixels to file."); 578 | } 579 | } 580 | } else 581 | throw Exception("Bitmap::save(\"" + filename.string() + "\"): Failed to open file."); 582 | } 583 | 584 | /** 585 | * Loads Bitmap from file 586 | * @throws bmp::Exception on error 587 | */ 588 | void load(const std::filesystem::path &filename) { 589 | m_pixels.clear(); 590 | 591 | if (std::ifstream ifs{filename, std::ios::binary}; ifs.good()) { 592 | // Read Header 593 | std::unique_ptr header(new BitmapHeader()); 594 | ifs.read(reinterpret_cast(header.get()), sizeof(BitmapHeader)); 595 | 596 | // Check if Bitmap file is valid 597 | if (header->magic != BITMAP_BUFFER_MAGIC) { 598 | throw Exception("Bitmap::load(\"" + filename.string() + "\"): Unrecognized file format."); 599 | } 600 | // Check if the Bitmap file has 24 bits per pixel (for now supporting only 24bpp bitmaps) 601 | if (header->bits_per_pixel != 24) { 602 | throw Exception("Bitmap::load(\"" + filename.string() + "\"): Only 24 bits per pixel bitmaps supported."); 603 | } 604 | 605 | // Seek the beginning of the pixels data 606 | // Note: We can't just assume we're there right after we read the BitmapHeader 607 | // Because some editors like Gimp might put extra information after the header. 608 | // Thanks to @seeliger-ec 609 | ifs.seekg(header->offset_bits); 610 | 611 | // Set width & height 612 | m_width = header->width; 613 | m_height = header->height; 614 | 615 | // Resize pixels size 616 | m_pixels.resize(static_cast(m_width) * static_cast(m_height), Black); 617 | 618 | // Read Bitmap pixels 619 | const std::int32_t row_size = m_width * 3 + m_width % 4; 620 | std::vector line(row_size); 621 | for (std::int32_t y = m_height - 1; y >= 0; --y) { 622 | ifs.read(reinterpret_cast(line.data()), line.size()); 623 | if (!ifs.good()) 624 | throw Exception("Bitmap::load(\"" + filename.string() + "\"): Failed to read bitmap pixels from file."); 625 | std::size_t i = 0; 626 | for (std::int32_t x = 0; x < m_width; ++x) { 627 | Pixel color{}; 628 | color.b = line[i++]; 629 | color.g = line[i++]; 630 | color.r = line[i++]; 631 | m_pixels[IX(x, y)] = color; 632 | } 633 | } 634 | } else 635 | throw Exception("Bitmap::load(\"" + filename.string() + "\"): Failed to load bitmap pixels from file."); 636 | } 637 | 638 | private: /* Utils */ 639 | /** 640 | * Converts 2D x,y coords into 1D index 641 | */ 642 | [[nodiscard]] constexpr std::size_t IX(const std::int32_t x, const std::int32_t y) const noexcept { 643 | return static_cast(x) + static_cast(m_width) * static_cast(y); 644 | } 645 | /** 646 | * Returns true if x,y coords are within boundaries 647 | */ 648 | [[nodiscard]] constexpr bool in_bounds(const std::int32_t x, const std::int32_t y) const noexcept { 649 | return (x >= 0) && (x < m_width) && (y >= 0) && (y < m_height); 650 | } 651 | 652 | private: 653 | std::vector m_pixels; 654 | std::int32_t m_width; 655 | std::int32_t m_height; 656 | }; 657 | } 658 | --------------------------------------------------------------------------------