comparison OrthancFramework/Sources/Images/PamReader.cpp @ 4325:b96aedfa8cc1

unit tests now running in WebAssembly
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 24 Nov 2020 16:21:29 +0100
parents 50b0c69b653a
children d9473bd5ed43
comparison
equal deleted inserted replaced
4324:433e94d08e36 4325:b96aedfa8cc1
230 { 230 {
231 uint16_t* pixel = reinterpret_cast<uint16_t*>(GetRow(h)); 231 uint16_t* pixel = reinterpret_cast<uint16_t*>(GetRow(h));
232 232
233 for (unsigned int w = 0; w < width; ++w, ++pixel) 233 for (unsigned int w = 0; w < width; ++w, ++pixel)
234 { 234 {
235 #if defined(__EMSCRIPTEN__) // For WebAssembly 235 /**
236 /* 236 * This is Little-Endian computer, and PAM uses
237 237 * Big-Endian. Need to do a 16-bit swap. We DON'T use
238 crash (2019-08-05): 238 * "htobe16()", as the latter only works if the "pixel"
239 239 * pointer is 16-bit aligned (which is not the case if
240 Uncaught abort(alignment fault) at Error 240 * "offset" is an odd number), and the trick that was used
241 at jsStackTrace 241 * in Orthanc <= 1.8.0 (i.e. make a "memcpy()" to a local
242 at stackTrace 242 * uint16_t variable) doesn't seem work for WebAssembly. We
243 at abort 243 * thus use a plain old C implementation. Check out issue
244 at alignfault 244 * #99: https://bugs.orthanc-server.com/show_bug.cgi?id=99
245 at SAFE_HEAP_LOAD_i32_2_2 (wasm-function[251132]:39) 245 *
246 at __ZN7Orthanc9PamReader12ParseContentEv (wasm-function[11457]:8088) 246 * Here is the crash log on WebAssembly (2019-08-05):
247 247 *
248 Web Assembly IS LITTLE ENDIAN! 248 * Uncaught abort(alignment fault) at Error
249 249 * at jsStackTrace
250 Perhaps in htobe16 ? 250 * at stackTrace
251 */ 251 * at abort
252 * at alignfault
253 * at SAFE_HEAP_LOAD_i32_2_2 (wasm-function[251132]:39)
254 * at __ZN7Orthanc9PamReader12ParseContentEv (wasm-function[11457]:8088)
255 **/
252 uint8_t* srcdst = reinterpret_cast<uint8_t*>(pixel); 256 uint8_t* srcdst = reinterpret_cast<uint8_t*>(pixel);
253 uint8_t tmp = srcdst[0]; 257 uint8_t tmp = srcdst[0];
254 srcdst[0] = srcdst[1]; 258 srcdst[0] = srcdst[1];
255 srcdst[1] = tmp; 259 srcdst[1] = tmp;
256 #else
257 // memcpy() is necessary to avoid segmentation fault if the
258 // "pixel" pointer is not 16-bit aligned (which is the case
259 // if "offset" is an odd number). Check out issue #99:
260 // https://bitbucket.org/sjodogne/orthanc/issues/99
261 uint16_t v = htobe16(*pixel);
262 memcpy(pixel, &v, sizeof(v));
263 #endif
264 } 260 }
265 } 261 }
266 } 262 }
267 } 263 }
268 264