# Building ITK with emscripten

**URL:** https://discourse.itk.org/t/building-itk-with-emscripten/2919
**Category:** Beginner Questions
**Created:** [April 10, 2020, 4:32am UTC](https://discourse.itk.org/t/building-itk-with-emscripten/2919 "2020-04-10T04:32:34Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![prashanth.dumpuri](https://discourse.itk.org/letter_avatar_proxy/v4/letter/p/b5ac83/32.png) [@prashanth.dumpuri](https://discourse.itk.org/u/prashanth.dumpuri)
#### Post date: [April 10, 2020, 4:32am UTC](https://discourse.itk.org/t/building-itk-with-emscripten/2919/1 "2020-04-10T04:32:34Z")

</div>

All,

'am trying to build ITK-4.9 on Windows 10 machine using emscripten. I’ve written a few filters in C++ using ITK but I now have to run them from a browser. I do not know javascript and therefore cannot use itk-js. My only option is to convert my C++ filters to WASM using emscripten.

I used the following flags and cmake to configure (followed by ninja to build ITK)

> flags=“-Wno-warn-absolute-paths --memory-init-file 0 -s DISABLE\_EXCEPTION\_CATCHING=0 -s ALLOW\_MEMORY\_GROWTH=1 -D\_LARGEFILE64\_SOURCE=1”

> cmake -DCMAKE\_TOOLCHAIN\_FILE=“Emscripten.cmake” -G Ninja "-DCMAKE\_CXX\_FLAGS=“_flags shown above_” -DCMAKE\_C\_FLAGS=“_flags shown above_” -DBUILD\_EXAMPLES=OFF -DBUILD\_TESTING=OFF “C:\ITK-4.9.0”

I got an error at HDF5 build step. So, I followed [section 2 in this link](https://itk.org/Wiki/ITK/Cross_Compiling#In_Linux_Host_for_Windows_Target) to fix that error. I seemed to get past that stage but i now get the following error:

> ninja: build stopped: command failed

The lines above this error are the following:

> InsightToolkit-4.9.1/Modules/Core/Common/include/itkPoint.h:308:48: note: previous declaration is here typedef typename PointContainerType::Element PointType;  
> 3 warnings generated.

This is my first attempt at using ninja and emscripten. I might be posting an incomplete error message above. In CMake, I would look at CMakeError.log but i do not know the equivalent for that file in ninja. Can someone please point me in the right direction and help me build ITK using emscripten?

Thanks in advance

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [April 10, 2020, 1:21pm UTC](https://discourse.itk.org/t/building-itk-with-emscripten/2919/2 "2020-04-10T13:21:03Z")

</div>

ITK 4.9 is quite old now. There have been emscripten-related improvements since then. Can you try 5.1 RC3, or at least [4.13.x](https://github.com/InsightSoftwareConsortium/ITK/commits/release-4.13)?

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [April 10, 2020, 1:43pm UTC](https://discourse.itk.org/t/building-itk-with-emscripten/2919/3 "2020-04-10T13:43:13Z")

</div>

Welcome to the ITK community, @prashanth.dumpuri! ☀

WASM is run through the browser via JavaScript, and itk.js is build on WASM – the best approach is to use itk.js.

---

<div class="post-metadata">

### Author: ![prashanth.dumpuri](https://discourse.itk.org/letter_avatar_proxy/v4/letter/p/b5ac83/32.png) [@prashanth.dumpuri](https://discourse.itk.org/u/prashanth.dumpuri)
#### Post date: [April 10, 2020, 3:15pm UTC](https://discourse.itk.org/t/building-itk-with-emscripten/2919/4 "2020-04-10T15:15:53Z")

</div>

Thanks @dzenanz. I did not try 5.1 but I got errors related to threading in ITK-5.0.1. The errors were in itkMultiThreaderBase.cxx about undeclared variable m\_GlobalMaximumNumberOfThreads. I changed the corresponding header file and created a variable for m\_GlobalMaximumNumberOfThreads but that just led to more errors. Did I do something wrong? If not, have they been fixed in ITK 5.1 for clang compiler?

Thanks @matt.mccormick. I’m eventually going to switch to itk-js but I do not know javacsript programming and you said that you are still working on examples related to segmentation (you replied to my post in itk-js too. thanks again for that). So, while I wait on the examples and teach myself javascript programming, converting the ITK C++ filters I wrote to WASM is my only option.

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [April 10, 2020, 5:58pm UTC](https://discourse.itk.org/t/building-itk-with-emscripten/2919/5 "2020-04-10T17:58:54Z")

</div>

itk.js provides ITK 5.1 RC 3 compiled with Emscripten.

itk.js minimizes the need to learn JavaScript. If you have a segmentation pipeline written ITK / C++, itk.js makes it easy to build it and run it in a browser.

To build it, `npx itk-js build .` as described here:

[https://insightsoftwareconsortium.github.io/itk-js/examples/hello\_world\_node.html](https://insightsoftwareconsortium.github.io/itk-js/examples/hello_world_node.html)

Here is what a simple test pipeline looks like (note that it is only standard ITK /CMake / C++):

[https://github.com/InsightSoftwareConsortium/itk-js/tree/master/test/MedianFilterPipeline](https://github.com/InsightSoftwareConsortium/itk-js/tree/master/test/MedianFilterPipeline)

Here is how to call it:

[https://github.com/InsightSoftwareConsortium/itk-js/blob/16fe9367de0a28a29f3a4c17ad181ff3731ec168/test/Browser/runPipelineBrowserTest.js#L143-L155](https://github.com/InsightSoftwareConsortium/itk-js/blob/16fe9367de0a28a29f3a4c17ad181ff3731ec168/test/Browser/runPipelineBrowserTest.js#L143-L155)

---

<div class="post-metadata">

### Author: ![prashanth.dumpuri](https://discourse.itk.org/letter_avatar_proxy/v4/letter/p/b5ac83/32.png) [@prashanth.dumpuri](https://discourse.itk.org/u/prashanth.dumpuri)
#### Post date: [April 16, 2020, 1:40am UTC](https://discourse.itk.org/t/building-itk-with-emscripten/2919/6 "2020-04-16T01:40:28Z")

</div>

Thanks again, @matt.mccormick. The example link you provided me helped me a ton and i’m up and running.

Is there a way to pass an Arraybuffer (preferably as 16-bit and not 8-bit) to ITK based C++ code. In the example you provided, the input image to C++ is read from the disk. In my application, this file is read on the browser side using UTIF and I have access just to the Arraybuffer from UTIF’s reader. Do you have any examples for extracting pixel intensities from that arraybuffer and passing it to the C++ code?

Prashanth

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [April 17, 2020, 1:30pm UTC](https://discourse.itk.org/t/building-itk-with-emscripten/2919/7 "2020-04-17T13:30:34Z")

</div>

> [@prashanth.dumpuri](#):
>
> Thanks again, @matt.mccormick. The example link you provided me helped me a ton and i’m up and running.

Great!

> [@prashanth.dumpuri](#):
>
> UTIF’s

> [@prashanth.dumpuri](#):
>
> Is there a way to pass an Arraybuffer (preferably as 16-bit and not 8-bit) to ITK based C++ code. In the example you provided, the input image to C++ is read from the disk. In my application, this file is read on the browser side using UTIF and I have access just to the Arraybuffer from UTIF’s reader. Do you have any examples for extracting pixel intensities from that arraybuffer and passing it to the C++ code?

In this case, you can create an [`itk/Image`](https://insightsoftwareconsortium.github.io/itk-js/api/Image.html) with something like:

```auto
const dimension = 2
const componentType = itk.IntTypes.UInt8
# possibly also specify the pixelType and numberOfComponents
const imageType = new itk.ImageType(dimension, componentType)
const image = new itk.Image(imageType)
image.size = [width, height]
image.data = new Uint8Array(arrayBuffer)

```

Then, pass the `image` to `runPipelineBrowser`.

---

<div class="post-metadata">

### Author: ![prashanth.dumpuri](https://discourse.itk.org/letter_avatar_proxy/v4/letter/p/b5ac83/32.png) [@prashanth.dumpuri](https://discourse.itk.org/u/prashanth.dumpuri)
#### Post date: [April 21, 2020, 5:29pm UTC](https://discourse.itk.org/t/building-itk-with-emscripten/2919/8 "2020-04-21T17:29:43Z")

</div>

Thanks @matt.mccormick. arrayBuffer that is passed to me has been encoded. Below is the code that the front-end developer on my team (working on HTML side) gave me.

```
    // Create a new XMLHttpRequest object to read the mock TIFF image as ArrayBuffer
    const xhr = new XMLHttpRequest();
    // Configure it: GET-request for the URL
    xhr.open('GET', 'assets/tif/test.tif', true); // read test.tif bundled in the assets folder
    xhr.responseType = 'arraybuffer'; // get array buffer
    xhr.timeout = 10000; // timeout in ms, 10 seconds 
    const arrayBuffer = xhr.response; // create javascript variable and assign it with pixel intensities

```

In the example code, front-end developer is creating a HTTP request to read a test.tif I gave him. test.tif is a 300x300 scalar image with unsigned char as its pixel type. Therefore, I expected arrayBuffer variable in the last line of the code block above to be a 90,000x1 array. However, when I print the contents of arrayBuffer to a text file it has additional 225 elements. And, it does not have the tiff tags associated with it. It looks like an encoded array. Is there a simple way to decode this array?

I tried to copy itk-js’ appoach for readImageHTTP and use axios reader do but realized that the input file has to be a json with a 90,000x1 array for pixel intensities. I seem to be stuck in a loop with this approach.

'am sure i’m missing something really simple. Can you please point me in the right direction?

Thanks

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [April 23, 2020, 5:57pm UTC](https://discourse.itk.org/t/building-itk-with-emscripten/2919/9 "2020-04-23T17:57:23Z")

</div>

Hi @prashanth.dumpuri,

The function it sounds like you want is [readImageArrayBuffer](https://insightsoftwareconsortium.github.io/itk-js/api/browser_io.html#readImageArrayBuffer-webWorker-arrayBuffer-fileName-mimeType-gt-webWorker-image).

HTH,  
Matt

---

<div class="post-metadata">

### Author: ![prashanth.dumpuri](https://discourse.itk.org/letter_avatar_proxy/v4/letter/p/b5ac83/32.png) [@prashanth.dumpuri](https://discourse.itk.org/u/prashanth.dumpuri)
#### Post date: [April 27, 2020, 4:19pm UTC](https://discourse.itk.org/t/building-itk-with-emscripten/2919/10 "2020-04-27T16:19:43Z")

</div>

Thanks again @matt.mccormick. I looked at source code for readImageArrayBuffer and it looked like even it was expecting a decoded tiff. I could be wrong here but my front end team figured out a way to decode TIFFs and send me just the pixel intensities. I’m using that in my C++ code to create an ITK image and set up my C++ processing pipeline.
