|
| 1 | +title: Hello Pipeline World! |
| 2 | +--- |
| 3 | + |
| 4 | +This example introduces the `itk::wasm::Pipeline`. An `itk::wasm::Pipeline` transforms elegant standalone C++ command line programs into powerful [WebAssembly](https://webassembly.org/) (WASM) modules with a simple, efficient interface for execution in the browser, other programming languages, and on the command line. |
| 5 | + |
| 6 | +First, let's create a new directory to house our project. |
| 7 | + |
| 8 | +```sh |
| 9 | +mkdir HelloPipeline |
| 10 | +cd HelloPipeline |
| 11 | +``` |
| 12 | + |
| 13 | +Let's write some code! Populate *HelloPipeline.cxx* first with the headers we need: |
| 14 | + |
| 15 | +```c++ |
| 16 | +#include "itkPipeline.h" |
| 17 | +#include "itkInputImage.h" |
| 18 | +#include "itkImage.h" |
| 19 | +``` |
| 20 | + |
| 21 | +The *itkImage.h* header is [ITK](https://itk.org)'s standard n-dimensional image data structure. |
| 22 | + |
| 23 | +The *itkPipeline.h* and *itkInputImage.h* headers come from the itk-wasm *WebAssemblyInterface* [ITK module](https://www.kitware.com/advance-itk-with-modules/). |
| 24 | + |
| 25 | +Next, create a standard `main` C command line interface function and an `itk::wasm::Pipeline`: |
| 26 | + |
| 27 | + |
| 28 | +```c++ |
| 29 | +int main(int argc, char * argv[]) { |
| 30 | + // Create the pipeline for parsing arguments. Provide a description. |
| 31 | + itk::wasm::Pipeline pipeline("A hello world itk::wasm::Pipeline", argc, argv); |
| 32 | + |
| 33 | + return EXIT_SUCCESS; |
| 34 | +} |
| 35 | +``` |
| 36 | +
|
| 37 | +The `itk::wasm::Pipeline` extends the most-excellent [CLI11 modern C++ command line parser](https://github.com/CLIUtils/CLI11). In addition to all of CLI11's functionality, `itk::wasm::Pipeline`'s adds: |
| 38 | +
|
| 39 | +- Support for execution in WASM modules along with command line execution |
| 40 | +- Support for spatial data structures such as `Image`'s, `Mesh`'s, `PolyData`, and `Transform`'s |
| 41 | +- Support for multiple dimensions and pixel types |
| 42 | +- Colored help output |
| 43 | +
|
| 44 | +Add a standard CLI11 flag to the pipeline: |
| 45 | +
|
| 46 | +```c++ |
| 47 | + itk::wasm::Pipeline pipeline("A hello world itk::wasm::Pipeline", argc, argv); |
| 48 | +
|
| 49 | +
|
| 50 | + bool quiet = false; |
| 51 | + pipeline.add_flag("-q,--quiet", quiet, "Do not print image information"); |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +Add an input image argument to the pipeline: |
| 56 | + |
| 57 | +```c++ |
| 58 | + pipeline.add_flag("-q,--quiet", quiet, "Do not print image information"); |
| 59 | + |
| 60 | + |
| 61 | + constexpr unsigned int Dimension = 2; |
| 62 | + using PixelType = unsigned char; |
| 63 | + using ImageType = itk::Image<PixelType, Dimension>; |
| 64 | + |
| 65 | + // Add a input image argument. |
| 66 | + using InputImageType = itk::wasm::InputImage<ImageType>; |
| 67 | + InputImageType inputImage; |
| 68 | + pipeline.add_option("InputImage", inputImage, "The input image")->required(); |
| 69 | +``` |
| 70 | + |
| 71 | +The `inputImage` variable is populated from the filesystem if built as a native executable. When running in the browser or in a wrapped language, `inputImage` is read from WebAssembly memory without file IO. |
| 72 | + |
| 73 | +Parse the command line arguments with the `ITK_WASM_PARSE` macro: |
| 74 | + |
| 75 | +```c++ |
| 76 | + pipeline.add_option("InputImage", inputImage, "The input image")->required(); |
| 77 | + |
| 78 | + |
| 79 | + ITK_WASM_PARSE(pipeline); |
| 80 | +``` |
| 81 | +
|
| 82 | +This parses the command line arguments. If `-q` or `--quiet` is set, the `quiet` variable will be set to `true`. Missing or invalid arguments will print an error and exit. The `-h` and `--help` flags are automatically generated from pipeline arguments to print usage information. |
| 83 | +
|
| 84 | +Finally, run our pipeline: |
| 85 | +```c++ |
| 86 | + std::cout << "Hello pipeline world!\n" << std::endl; |
| 87 | +
|
| 88 | + if (!quiet) |
| 89 | + { |
| 90 | + // Obtain the itk::Image * from the itk::wasm::InputImage with `.Get()`. |
| 91 | + std::cout << "Input image: " << *inputImage.Get() << std::endl; |
| 92 | + } |
| 93 | +
|
| 94 | + return EXIT_SUCCESS; |
| 95 | +``` |
| 96 | + |
| 97 | +Next, provide a [CMake](https://cmake.org/) build configuration at *CMakeLists.txt*: |
| 98 | + |
| 99 | +```cmake |
| 100 | +cmake_minimum_required(VERSION 3.16) |
| 101 | +project(HelloPipeline) |
| 102 | +
|
| 103 | +# Use C++17 or newer with itk-wasm |
| 104 | +set(CMAKE_CXX_STANDARD 17) |
| 105 | +
|
| 106 | +# We always want to build against the WebAssemblyInterface module. |
| 107 | +set(itk_components |
| 108 | + WebAssemblyInterface |
| 109 | + ) |
| 110 | +# WASI or native binaries |
| 111 | +if (NOT EMSCRIPTEN) |
| 112 | + # WebAssemblyInterface supports the .iwi, .iwi.cbor itk-wasm format. |
| 113 | + # We can list other ITK IO modules to build against to support other |
| 114 | + # formats when building native executable or WASI WebAssembly. |
| 115 | + # However, this will bloat the size of the WASI WebAssembly binary, so |
| 116 | + # add them judiciously. |
| 117 | + set(itk_components |
| 118 | + WebAssemblyInterface |
| 119 | + ITKIOPNG |
| 120 | + # ITKImageIO # Adds support for all available image IO modules |
| 121 | + ) |
| 122 | +endif() |
| 123 | +find_package(ITK REQUIRED |
| 124 | + COMPONENTS ${itk_components} |
| 125 | + ) |
| 126 | +include(${ITK_USE_FILE}) |
| 127 | +
|
| 128 | +add_executable(HelloPipeline HelloPipeline.cxx) |
| 129 | +target_link_libraries(HelloPipeline PUBLIC ${ITK_LIBRARIES}) |
| 130 | +``` |
| 131 | + |
| 132 | +[Build the WASI binary](../hello_world.html): |
| 133 | + |
| 134 | +```sh |
| 135 | +npx itk-wasm -i itkwasm/wasi build |
| 136 | +``` |
| 137 | + |
| 138 | +Check the generated help output: |
| 139 | + |
| 140 | +```sh |
| 141 | +npx itk-wasm run HelloPipeline.wasi.wasm -- -- --help |
| 142 | +``` |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | +The two `--`'s are to separate arguments for the WASM module from arguments to the `itk-wasm` CLI and the WebAssembly interpreter. |
| 147 | + |
| 148 | +Try running on an [example image](https://bafybeihibtxtdmwuekb64wnv3ras54lz4ojuqv4gabmigpfdha4dsmcr5y.ipfs.w3s.link/ipfs/bafybeihibtxtdmwuekb64wnv3ras54lz4ojuqv4gabmigpfdha4dsmcr5y/cthead1.png). |
| 149 | + |
| 150 | +``` |
| 151 | +> npx itk-wasm run HelloPipeline.wasi.wasm -- -- cthead1.png |
| 152 | +
|
| 153 | +Hello pipeline world! |
| 154 | +
|
| 155 | +Input image: Image (0x2b910) |
| 156 | + RTTI typeinfo: itk::Image<unsigned char, 2u> |
| 157 | + Reference Count: 1 |
| 158 | + Modified Time: 54 |
| 159 | + Debug: Off |
| 160 | + Object Name: |
| 161 | + Observers: |
| 162 | + none |
| 163 | + Source: (none) |
| 164 | + Source output name: (none) |
| 165 | + Release Data: Off |
| 166 | + Data Released: False |
| 167 | + Global Release Data: Off |
| 168 | + PipelineMTime: 22 |
| 169 | + UpdateMTime: 53 |
| 170 | + RealTimeStamp: 0 seconds |
| 171 | + LargestPossibleRegion: |
| 172 | + Dimension: 2 |
| 173 | + Index: [0, 0] |
| 174 | + Size: [256, 256] |
| 175 | + BufferedRegion: |
| 176 | + Dimension: 2 |
| 177 | + Index: [0, 0] |
| 178 | + Size: [256, 256] |
| 179 | + RequestedRegion: |
| 180 | + Dimension: 2 |
| 181 | + Index: [0, 0] |
| 182 | + Size: [256, 256] |
| 183 | + Spacing: [1, 1] |
| 184 | + Origin: [0, 0] |
| 185 | + Direction: |
| 186 | +1 0 |
| 187 | +0 1 |
| 188 | +
|
| 189 | + IndexToPointMatrix: |
| 190 | +1 0 |
| 191 | +0 1 |
| 192 | +
|
| 193 | + PointToIndexMatrix: |
| 194 | +1 0 |
| 195 | +0 1 |
| 196 | +
|
| 197 | + Inverse Direction: |
| 198 | +1 0 |
| 199 | +0 1 |
| 200 | +
|
| 201 | + PixelContainer: |
| 202 | + ImportImageContainer (0x2ba60) |
| 203 | + RTTI typeinfo: itk::ImportImageContainer<unsigned long, unsigned char> |
| 204 | + Reference Count: 1 |
| 205 | + Modified Time: 50 |
| 206 | + Debug: Off |
| 207 | + Object Name: |
| 208 | + Observers: |
| 209 | + none |
| 210 | + Pointer: 0x2c070 |
| 211 | + Container manages memory: true |
| 212 | + Size: 65536 |
| 213 | + Capacity: 65536 |
| 214 | +``` |
| 215 | + |
| 216 | +And with the `--quiet` flag: |
| 217 | + |
| 218 | +``` |
| 219 | +> npx itk-wasm run HelloPipeline.wasi.wasm -- -- --quiet cthead1.png |
| 220 | +
|
| 221 | +Hello pipeline world! |
| 222 | +``` |
| 223 | + |
| 224 | +Congratulations! You just executed a C++ pipeline capable of processsing a scientific image in WebAssembly. 🎉 |
0 commit comments