int main( int argc, char * argv [] ) { /*** Message structures and byte array ***/ igtl_header header; void * body; igtl_ndarray_info info; size_t body_size; igtl_uint16 size[3]; int i, j, k; igtl_float64 * array; int rh; /* Comparison result for header */ int rb; /* Comparison result for body */ int s; /*** Generate test data ***/ igtl_ndarray_init_info(&info); info.type = IGTL_NDARRAY_STYPE_TYPE_FLOAT64; /* Array size is 5x4x3 */ info.dim = 3; size[0] = 5; size[1] = 4; size[2] = 3; if (igtl_ndarray_alloc_info(&info, size) == 0) { return EXIT_FAILURE; } /* Generate dummy array */ array = (igtl_float64 *) info.array; for (i = 0; i < 5; i ++) { for (j = 0; j < 4; j ++) { for (k = 0; k < 3; k ++) { array[i*(4*3) + j*3 + k] = (igtl_float64) (i*(4*3) + j*3 + k); } } } /** Allocate memory for pack **/ body_size = (size_t)igtl_ndarray_get_size(&info, IGTL_TYPE_PREFIX_NONE); body = malloc(body_size); if (body == NULL) { igtl_ndarray_free_info(&info); return EXIT_FAILURE; } igtl_ndarray_pack(&info, body, IGTL_TYPE_PREFIX_NONE); /*** Set OpenIGTLink header ***/ header.version = 1; strncpy( (char*)&(header.name), "NDARRAY", 12 ); strncpy( (char*)&(header.device_name), "DeviceName", 20 ); header.timestamp = 1234567892; header.body_size = body_size; header.crc = igtl_ndarray_get_crc(&info, IGTL_TYPE_PREFIX_NONE, body); igtl_header_convert_byte_order( &(header) ); /* Dumping data -- for testing */ /* FILE *fp; fp = fopen("ndarray.bin", "w"); fwrite(&(header), IGTL_HEADER_SIZE, 1, fp); fwrite(body, body_size, 1, fp); fclose(fp); */ rh = memcmp((const void*)&header, (const void*)test_ndarray_message_header, IGTL_HEADER_SIZE); rb = memcmp((const void*)body, (const void*)test_ndarray_message_body, body_size); igtl_ndarray_free_info(&info); free(body); if (rh == 0 && rb == 0) { return EXIT_SUCCESS; } else { /* Print first 256 bytes as HEX values in STDERR for debug */ s = IGTL_HEADER_SIZE + body_size; if (s > 256) { s = 256; } fprintf(stdout, "\n===== First %d bytes of the test message =====\n", s); igtl_message_dump_hex(stdout, (const void*)&header, s); return EXIT_FAILURE; } }
int igtl_export igtl_ndarray_unpack(int type, void * byte_array, igtl_ndarray_info * info, igtl_uint64 pack_size) { char * ptr; igtl_uint16 dim; igtl_uint16 * size; igtl_uint16 i; igtl_uint64 len; igtl_uint16 * ptr16_src; igtl_uint16 * ptr16_src_end; igtl_uint16 * ptr16_dst; igtl_uint32 * ptr32_src; igtl_uint32 * ptr32_src_end; igtl_uint32 * ptr32_dst; igtl_uint64 * ptr64_src; igtl_uint64 * ptr64_src_end; igtl_uint64 * ptr64_dst; if (byte_array == NULL || info == NULL) { return 0; } igtl_ndarray_init_info(info); ptr = (char *) byte_array; /*** Type field ***/ info->type = * (igtl_uint8 *) ptr; ptr ++; /*** Dimension field ***/ info->dim = * (igtl_uint8 *) ptr; ptr ++; /*** Size array field ***/ size = (igtl_uint16 *) ptr; dim = info->dim; if (igtl_is_little_endian()) { /* Change byte order -- this overwrites memory area for the pack !!*/ for (i = 0; i < dim; i ++) { size[i] = BYTE_SWAP_INT16(size[i]); } } igtl_ndarray_alloc_info(info, size); memcpy(info->size, size, sizeof(igtl_uint16) * dim); if (igtl_is_little_endian()) { /* Resotore the overwritten memory area */ /* Don't use size[] array after this !! */ for (i = 0; i < dim; i ++) { size[i] = BYTE_SWAP_INT16(size[i]); } } ptr += sizeof(igtl_uint16) * dim; /*** N-D array field ***/ /* Calculate number of elements in N-D array */ len = 1; for (i = 0; i < dim; i ++) { len *= info->size[i]; } /* Copy array */ if (igtl_ndarray_get_nbyte(info->type) == 1 || !igtl_is_little_endian()) { /* If single-byte data type is used or the program runs on a big-endian machine, just copy the array from the pack to the strucutre */ memcpy(info->array, ptr, (size_t)(len * igtl_ndarray_get_nbyte(info->type))); } else if (igtl_ndarray_get_nbyte(info->type) == 2) /* 16-bit */ { ptr16_src = (igtl_uint16 *) ptr; ptr16_src_end = ptr16_src + len; ptr16_dst = (igtl_uint16 *) info->array; while (ptr16_src < ptr16_src_end) { *ptr16_dst = BYTE_SWAP_INT16(*ptr16_src); ptr16_dst ++; ptr16_src ++; } } else if (igtl_ndarray_get_nbyte(info->type) == 4) /* 32-bit */ { ptr32_src = (igtl_uint32 *) ptr; ptr32_src_end = ptr32_src + len; ptr32_dst = (igtl_uint32 *) info->array; while (ptr32_src < ptr32_src_end) { *ptr32_dst = BYTE_SWAP_INT32(*ptr32_src); ptr32_dst ++; ptr32_src ++; } } else /* 64-bit or Complex type */ { ptr64_src = (igtl_uint64 *) ptr; /* Adding number of elements to the pointer -- 64-bit: len * 1; Complex: len * 2*/ ptr64_src_end = ptr64_src + len * igtl_ndarray_get_nbyte(info->type)/8; ptr64_dst = (igtl_uint64 *) info->array; while (ptr64_src < ptr64_src_end) { *ptr64_dst = BYTE_SWAP_INT64(*ptr64_src); ptr64_dst ++; ptr64_src ++; } } /* TODO: check if the pack size is valid */ return 1; }