void igtl_export igtl_qtdata_convert_byte_order(igtl_qtdata_element* qtdatalist, int nitem) { igtl_qtdata_element* elem; int i; int j; int k; igtl_int32* tmp; for (i = 0; i < nitem; i ++) { elem = &(qtdatalist[i]); if (igtl_is_little_endian()) { for (j = 0; j < 3; j ++) { tmp = (igtl_int32*)&(elem->position[j]); *tmp = BYTE_SWAP_INT32(*tmp); } for (k = 0; k < 4; k ++) { tmp = (igtl_int32*)&(elem->quaternion[k]); *tmp = BYTE_SWAP_INT32(*tmp); } } } }
void igtl_export igtl_image_convert_byte_order(igtl_image_header * header) { int i; igtl_uint32 tmp[12]; if (igtl_is_little_endian()) { header->header_version = BYTE_SWAP_INT16(header->header_version); for (i = 0; i < 3; i ++) { header->size[i] = BYTE_SWAP_INT16(header->size[i]); header->subvol_size[i] = BYTE_SWAP_INT16(header->subvol_size[i]); header->subvol_offset[i] = BYTE_SWAP_INT16(header->subvol_offset[i]); } memcpy(tmp, header->matrix, sizeof(igtl_uint32)*12); /* * TODO: The following loop may cause segmentation fault, when it is compiled * with '-ftree-vectorize' optimization option on 64-bit Linux. * ('-ftree-vectorize' becomes active, when '-O3' optimization is specified.) * -- code has been updated on June 24 -- needs test */ for (i = 0; i < 12; i ++) { tmp[i] = BYTE_SWAP_INT32(tmp[i]); } memcpy(header->matrix, tmp, sizeof(igtl_uint32)*12); } }
void igtl_export igtl_query_convert_byte_order(igtl_query_header* header) { if (igtl_is_little_endian()) { header->queryID = BYTE_SWAP_INT32(header->queryID); header->deviceUIDLength = BYTE_SWAP_INT16(header->deviceUIDLength); } }
void igtl_export igtl_header_convert_byte_order(igtl_header * header) { if (igtl_is_little_endian()) { header->version = BYTE_SWAP_INT16(header->version); header->timestamp = BYTE_SWAP_INT64(header->timestamp); header->body_size = BYTE_SWAP_INT64(header->body_size); header->crc = BYTE_SWAP_INT64(header->crc); } }
void igtl_export igtl_extended_header_convert_byte_order(igtl_extended_header * extended_header) { if (igtl_is_little_endian()) { extended_header->extended_header_size = BYTE_SWAP_INT16(extended_header->extended_header_size); extended_header->meta_data_header_size = BYTE_SWAP_INT16(extended_header->meta_data_header_size); extended_header->meta_data_size = BYTE_SWAP_INT32(extended_header->meta_data_size); extended_header->message_id = BYTE_SWAP_INT32(extended_header->message_id); } }
void igtl_export igtl_command_convert_byte_order(igtl_command_header* header) { if (igtl_is_little_endian()) { header->encoding = BYTE_SWAP_INT16(header->encoding); header->length = BYTE_SWAP_INT16(header->length); } }
void igtl_export igtl_stt_qtdata_convert_byte_order(igtl_stt_qtdata* stt_qtdata) { igtl_int32* tmp; if (igtl_is_little_endian()) { tmp = (igtl_int32*)&(stt_qtdata->resolution); *tmp = BYTE_SWAP_INT32(*tmp); } }
int igtl_export igtl_bind_pack(igtl_bind_info * info, void * byte_array, int type) { char * ptr; igtl_uint64 tmp64; switch (type) { case IGTL_TYPE_PREFIX_NONE: return igtl_bind_pack_normal(info, byte_array); break; case IGTL_TYPE_PREFIX_GET: return igtl_bind_pack_request(info, byte_array); break; case IGTL_TYPE_PREFIX_STT: /* * STT_BIND message has the same format as GET_BIND, except that it * has the time resolution field. */ ptr = (char *) byte_array; /* Get time resolution */ if (igtl_is_little_endian()) { memcpy(&tmp64, ptr, sizeof(igtl_uint64)); info->resol = BYTE_SWAP_INT64(tmp64); } else { memcpy(&(info->resol), ptr, sizeof(igtl_uint64)); } ptr += sizeof(igtl_uint64); /* Generate rest of the message */ return igtl_bind_pack_request(info, ptr); break; case IGTL_TYPE_PREFIX_STP: return 1; break; case IGTL_TYPE_PREFIX_RTS: * (igtl_uint8 * ) byte_array = info->status; return 1; break; default: return 0; break; } }
igtl_uint64 igtl_export igtl_image_get_crc(igtl_image_header * header, void* image) { igtl_uint64 crc; igtl_uint64 img_size; /* calculate image size (we do not call igtl_image_get_data_size() * because header has already been serialized. */ igtl_uint64 si; igtl_uint64 sj; igtl_uint64 sk; igtl_uint64 sp; if (igtl_is_little_endian()) { si = BYTE_SWAP_INT16(header->subvol_size[0]); sj = BYTE_SWAP_INT16(header->subvol_size[1]); sk = BYTE_SWAP_INT16(header->subvol_size[2]); } else { si = header->subvol_size[0]; sj = header->subvol_size[1]; sk = header->subvol_size[2]; } switch (header->scalar_type) { case IGTL_IMAGE_STYPE_TYPE_INT8: case IGTL_IMAGE_STYPE_TYPE_UINT8: sp = 1; break; case IGTL_IMAGE_STYPE_TYPE_INT16: case IGTL_IMAGE_STYPE_TYPE_UINT16: sp = 2; break; case IGTL_IMAGE_STYPE_TYPE_INT32: case IGTL_IMAGE_STYPE_TYPE_UINT32: sp = 4; break; default: sp = 0; break; } img_size = si*sj*sk*sp; crc = crc64(0, 0, 0); crc = crc64((unsigned char*) header, IGTL_IMAGE_HEADER_SIZE, crc); crc = crc64((unsigned char*) image, (int)img_size, crc); return crc; }
int igtl_export igtl_bind_unpack(int type, void * byte_array, igtl_bind_info * info, igtl_uint64 size) { char * ptr; switch (type) { case IGTL_TYPE_PREFIX_NONE: return igtl_bind_unpack_normal(byte_array, info); break; case IGTL_TYPE_PREFIX_GET: return igtl_bind_unpack_request(byte_array, info, size); break; case IGTL_TYPE_PREFIX_STT: /* * STT_BIND message has the same format as GET_BIND, except that it * has the time resolution field. */ /* Obtain time resolution */ ptr = byte_array; if (igtl_is_little_endian()) { memcpy(&(info->resol), ptr, sizeof(igtl_uint64)); info->resol = BYTE_SWAP_INT64(info->resol); } else { memcpy(&(info->resol), ptr, sizeof(igtl_uint64)); } /* Unpack rest of the message */ ptr += sizeof(igtl_uint64); return igtl_bind_unpack_request(ptr, info, size-sizeof(igtl_uint64)); break; case IGTL_TYPE_PREFIX_STP: return 1; break; case IGTL_TYPE_PREFIX_RTS: info->status = * (igtl_uint8 *) byte_array; return 1; break; default: return 0; break; } }
void igtl_export igtl_transform_convert_byte_order(igtl_float32* transform) { int i; igtl_uint32 tmp[12]; if (igtl_is_little_endian()) { memcpy(tmp, transform, sizeof(igtl_uint32)*12); for (i = 0; i < 12; i ++) { tmp[i] = BYTE_SWAP_INT32(tmp[i]); } memcpy(transform, tmp, sizeof(igtl_uint32)*12); } }
void igtl_export igtl_lbmeta_convert_byte_order(igtl_lbmeta_element* metalist, int nitem) { igtl_lbmeta_element* elem; int i; for (i = 0; i < nitem; i ++) { elem = &(metalist[i]); if (igtl_is_little_endian()) { elem->size[0] = BYTE_SWAP_INT16(elem->size[0]); elem->size[1] = BYTE_SWAP_INT16(elem->size[1]); elem->size[2] = BYTE_SWAP_INT16(elem->size[2]); } } }
void igtlM_export igtl_US_Tag_convert_byte_order(igtl_US_tag * tag) { //igtl_uint32 tmp; if(igtl_is_little_endian()) { tag->Type = BYTE_SWAP_INT32(tag->Type); tag->Txf = BYTE_SWAP_INT32(tag->Txf); tag->Sf = BYTE_SWAP_INT32(tag->Sf); tag->FPS = BYTE_SWAP_INT32(tag->FPS); tag->Lden = BYTE_SWAP_INT32(tag->Lden); tag->SAng = BYTE_SWAP_INT32(tag->SAng); tag->ProbeID = BYTE_SWAP_INT32(tag->ProbeID); tag->EA = BYTE_SWAP_INT32(tag->EA); tag->Elements = BYTE_SWAP_INT32(tag->Elements); tag->Pitch = BYTE_SWAP_INT32(tag->Pitch); tag->Radius = BYTE_SWAP_INT32(tag->Radius); tag->Probe_Angle = BYTE_SWAP_INT32(tag->Probe_Angle); tag->TxOffset = BYTE_SWAP_INT32(tag->TxOffset); //____________________________________________________ tag->Motor_Radius = BYTE_SWAP_INT32(tag->Motor_Radius); tag->Frames3D = BYTE_SWAP_INT32(tag->Frames3D); tag->Frame_Index = BYTE_SWAP_INT32(tag->Frame_Index); tag->Focus_Spacing = BYTE_SWAP_INT32(tag->Focus_Spacing); tag->Focus_Depth = BYTE_SWAP_INT32(tag->Focus_Depth); tag->Extra_int32_1 = BYTE_SWAP_INT32(tag->Extra_int32_1); tag->Extra_int32_2 = BYTE_SWAP_INT32(tag->Extra_int32_2); tag->Extra_int32_3 = BYTE_SWAP_INT32(tag->Extra_int32_3); tag->Extra_int32_4 = BYTE_SWAP_INT32(tag->Extra_int32_4); tag->Extra_int32_5 = BYTE_SWAP_INT32(tag->Extra_int32_5); tag->Motor_Dir = tag->Motor_Dir; tag->Focus_Count = tag->Focus_Count; tag->Extra_int8_1 = tag->Extra_int8_1; tag->Extra_int8_2 = tag->Extra_int8_2; tag->Extra_int8_3 = tag->Extra_int8_3; } }
void igtl_export igtl_colortable_convert_byte_order(igtl_colortable_header* header, void* table) { int i; int n; igtl_uint16* tmp; if (igtl_is_little_endian()) { if (header->mapType == IGTL_COLORTABLE_MAP_UINT16) { n = (header->indexType == IGTL_COLORTABLE_INDEX_UINT16)? 256*256 : 256; tmp = (igtl_uint16*) table; for (i = 0; i < n; i ++) { tmp[i] = BYTE_SWAP_INT16(tmp[i]); } } } }
void igtl_export igtl_us_status_convert_byte_order(igtl_us_status_message * message) { igtl_int64* tmp64 = (igtl_int64*)message; igtl_int32* tmp32 = (igtl_int32*)message; // igtl_uint16* tmp16 = (igtl_uint16*)message; if (igtl_is_little_endian()) { int i; for (i = 0; i < 6; i++)//Update number when adding more variables to message tmp64[i] = BYTE_SWAP_INT64(tmp64[i]); for (i = 0; i < 1; i ++) tmp32[12 + i] = BYTE_SWAP_INT32(tmp32[12 + i]);//6*64 bit adressed as 32 bit = 12 // tmp16[34] = BYTE_SWAP_INT16(tmp16[34]);//3*64 bit + 8*32 bit (adressed as 16 bit = 28) } }
void igtl_export igtl_point_convert_byte_order(igtl_point_element* pointlist, int nitem) { igtl_point_element* elem; int i; int j; igtl_int32* tmp; for (i = 0; i < nitem; i ++) { elem = &(pointlist[i]); if (igtl_is_little_endian()) { for (j = 0; j < 3; j ++) { tmp = (igtl_int32*)&(elem->position[j]); *tmp = BYTE_SWAP_INT32(*tmp); } tmp = (igtl_int32*)&(elem->radius); *tmp = BYTE_SWAP_INT32(*tmp); } } }
void igtl_export igtl_qtrans_convert_byte_order(igtl_qtrans* pos) { igtl_uint32 tmp[4]; if (igtl_is_little_endian()) { /* qtrans */ memcpy((void*)tmp, (void*)(pos->qtrans), sizeof(igtl_float32)*3); tmp[0] = BYTE_SWAP_INT32(tmp[0]); tmp[1] = BYTE_SWAP_INT32(tmp[1]); tmp[2] = BYTE_SWAP_INT32(tmp[2]); memcpy((void*)(pos->qtrans), (void*)tmp, sizeof(igtl_float32)*3); /* quaternion */ memcpy((void*)tmp, (void*)(pos->quaternion), sizeof(igtl_float32)*4); tmp[0] = BYTE_SWAP_INT32(tmp[0]); tmp[1] = BYTE_SWAP_INT32(tmp[1]); tmp[2] = BYTE_SWAP_INT32(tmp[2]); tmp[3] = BYTE_SWAP_INT32(tmp[3]); memcpy((void*)(pos->quaternion), (void*)tmp, sizeof(igtl_float32)*4); } }
int igtl_polydata_convert_byteorder_topology(igtl_uint32 * dst, igtl_uint32 * src, igtl_uint32 size) { igtl_uint32 * ptr32_src; igtl_uint32 * ptr32_src_end; igtl_uint32 * ptr32_dst; if (size == 0) { return 1; } if (!src || !dst) { /* Return error, if either src or dst is NULL despite size>0 */ return 0; } if (!igtl_is_little_endian()) { memcpy((void*)dst, (void*)src, size); } else { ptr32_src = (igtl_uint32 *) src; ptr32_src_end = ptr32_src + (size/sizeof(igtl_uint32)); ptr32_dst = (igtl_uint32 *) dst; while (ptr32_src < ptr32_src_end) { *ptr32_dst = BYTE_SWAP_INT32(*ptr32_src); ptr32_dst ++; ptr32_src ++; } } return 1; }
/// For USMessage version 1.0 void igtlM_export igtl_US_Tag1_convert_byte_order(igtl_US_tag1 * tag) { //igtl_uint32 tmp; if(igtl_is_little_endian()) { tag->Type = BYTE_SWAP_INT32(tag->Type); tag->Txf = BYTE_SWAP_INT32(tag->Txf); tag->Sf = BYTE_SWAP_INT32(tag->Sf); tag->FPS = BYTE_SWAP_INT32(tag->FPS); tag->Lden = BYTE_SWAP_INT32(tag->Lden); tag->SAng = BYTE_SWAP_INT32(tag->SAng); tag->ProbeID = BYTE_SWAP_INT32(tag->ProbeID); tag->EA = BYTE_SWAP_INT32(tag->EA); tag->Elements = BYTE_SWAP_INT32(tag->Elements); tag->Pitch = BYTE_SWAP_INT32(tag->Pitch); tag->Radius = BYTE_SWAP_INT32(tag->Radius); tag->Probe_Angle = BYTE_SWAP_INT32(tag->Probe_Angle); tag->TxOffset = BYTE_SWAP_INT32(tag->TxOffset); } }
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; }
/* * Function to unpack BIND message */ int igtl_bind_unpack_normal(void * byte_array, igtl_bind_info * info) { igtl_uint16 i; igtl_uint16 ncmessages; igtl_uint16 nametable_size; size_t namelen; char * ptr; char * ptr2; igtl_uint64 tmp64; if (byte_array == NULL || info == NULL) { return 0; } /* Number of child messages */ ptr = (char *) byte_array; if (igtl_is_little_endian()) { ncmessages = BYTE_SWAP_INT16(*((igtl_uint16*)ptr)); } else { ncmessages = *((igtl_uint16*)ptr); } /* Allocate an array of bind_info, if neccessary */ if (ncmessages != info->ncmessages) { if (igtl_bind_alloc_info(info, ncmessages) == 0) { return 0; } } /* Pointer to the first element in the BIND header section */ ptr += sizeof(igtl_uint16); /* Extract types and body sizes from the BIND header section */ for (i = 0; i < ncmessages; i ++) { /* Type of child message */ strncpy(info->child_info_array[i].type, (char*)ptr, IGTL_HEADER_TYPE_SIZE); info->child_info_array[i].type[IGTL_HEADER_TYPE_SIZE] = '\0'; /* Body size of child message */ ptr += IGTL_HEADER_TYPE_SIZE; if (igtl_is_little_endian()) { memcpy(&tmp64, ptr, sizeof(igtl_uint64)); info->child_info_array[i].size = BYTE_SWAP_INT64(tmp64); } else { memcpy(&(info->child_info_array[i].size), ptr, sizeof(igtl_uint64)); } ptr += sizeof(igtl_uint64); } /* Name table size */ if (igtl_is_little_endian()) { nametable_size = BYTE_SWAP_INT16(*((igtl_uint16 *) ptr)); } else { nametable_size = *((igtl_uint16 *) ptr); } /* * Check if the name table field is aligned to WORD (The length of * the field must be even. */ if (nametable_size % 2 != 0) { return 0; } ptr += sizeof(igtl_uint16); ptr2 = ptr; /* Extract device names from the name table section */ if (nametable_size > 0) { for (i = 0; i < ncmessages; i ++) { strncpy(info->child_info_array[i].name, ptr, IGTL_HEADER_NAME_SIZE); info->child_info_array[i].name[IGTL_HEADER_NAME_SIZE] = '\0'; namelen = strlen(info->child_info_array[i].name); ptr += namelen + 1; } } ptr = ptr2 + nametable_size; /* Set pointers to the child message bodies */ for (i = 0; i < ncmessages; i ++) { info->child_info_array[i].ptr = ptr; /* Note: a padding byte is added, if the size of the child message body is odd. */ ptr += info->child_info_array[i].size + (info->child_info_array[i].size % 2); } /** TODO: check the total size of the message? **/ return 1; }
int igtl_export igtl_ndarray_pack(igtl_ndarray_info * info, void * byte_array, int type) { 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; } ptr = (char *) byte_array; /*** Type field ***/ * (igtl_uint8 *) ptr = info->type; ptr ++; /*** Dimension field ***/ * (igtl_uint8 *) ptr = info->dim; ptr ++; /*** Size array field ***/ size = (igtl_uint16 *) ptr; if (igtl_is_little_endian()) { for (i = 0; i < info->dim; i ++) { size[i] = BYTE_SWAP_INT16(info->size[i]); } } else { memcpy(ptr, info->size, sizeof(igtl_uint16) * info->dim); } dim = info->dim; 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(ptr, info->array, (size_t) (len * igtl_ndarray_get_nbyte(info->type))); } else if (igtl_ndarray_get_nbyte(info->type) == 2) /* 16-bit */ { ptr16_src = (igtl_uint16 *) info->array; ptr16_src_end = ptr16_src + len; ptr16_dst = (igtl_uint16 *) ptr; 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 *) info->array; ptr32_src_end = ptr32_src + len; ptr32_dst = (igtl_uint32 *) ptr; 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 *) info->array; /* 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 *) ptr; while (ptr64_src < ptr64_src_end) { *ptr64_dst = BYTE_SWAP_INT64(*ptr64_src); ptr64_dst ++; ptr64_src ++; } } return 1; }
/* * Function to unpack STT_BIND and GET_BIND messages */ int igtl_bind_pack_request(igtl_bind_info * info, void * byte_array) { char * ptr; igtl_uint32 i; igtl_uint32 nc; igtl_uint16 * nts; size_t wb; size_t len; igtl_uint16 tmp16; ptr = (char *) byte_array; nc = info->ncmessages; /* If requesting all available, no body is generated. */ if (info->request_all == 1) { return 1; } /* Validate info */ if (info->ncmessages == 0 || info->child_info_array == NULL) { return 0; } /* Number of child messages */ if (igtl_is_little_endian()) { /* *((igtl_uint16*) ptr) = BYTE_SWAP_INT16(info->ncmessages); */ tmp16 = BYTE_SWAP_INT16(info->ncmessages); memcpy(ptr, &tmp16, sizeof(igtl_uint16)); } else { /* *((igtl_uint16*) ptr) = info->ncmessages; */ memcpy(ptr, &(info->ncmessages), sizeof(igtl_uint16)); } ptr += sizeof(igtl_uint16); /* BIND header section */ for (i = 0; i < nc; i ++) { /*memset(ptr, 0, IGTL_HEADER_TYPE_SIZE); */ /* Fill with 0 */ strncpy((char*)ptr, info->child_info_array[i].type, IGTL_HEADER_TYPE_SIZE); ptr += IGTL_HEADER_TYPE_SIZE; } /* Name table section */ nts = (igtl_uint16 *) ptr; /* save address for name table size field */ ptr += sizeof(igtl_uint16); wb = 0; for (i = 0; i < nc; i ++) { len = strlen(info->child_info_array[i].name); if (len > IGTL_HEADER_NAME_SIZE) { return 0; } /* copy string + NULL-terminator */ strncpy(ptr, info->child_info_array[i].name, len+1); ptr += (len + 1); wb += len + 1; } /* Substitute name table size */ *nts = (igtl_uint16) wb; if (igtl_is_little_endian()) { *nts = BYTE_SWAP_INT16(*nts); } return 1; }
/* * Function to unpack BIND message */ int igtl_bind_pack_normal(igtl_bind_info * info, void * byte_array) { char * ptr; igtl_uint32 i; igtl_uint32 nc; igtl_uint16 * nts; size_t wb; size_t len; igtl_uint16 tmp16; igtl_uint64 tmp64; ptr = (char *) byte_array; nc = info->ncmessages; /* Validate info */ if (info->ncmessages == 0 || info->child_info_array == NULL) { return 0; } /* Number of child messages */ if (igtl_is_little_endian()) { /* *((igtl_uint16*) ptr) = BYTE_SWAP_INT16(info->ncmessages); */ tmp16 = BYTE_SWAP_INT16(info->ncmessages); memcpy(ptr, &tmp16, sizeof(igtl_uint16)); } else { /* *((igtl_uint16*) ptr) = info->ncmessages; */ memcpy(ptr, &(info->ncmessages), sizeof(igtl_uint16)); } ptr += sizeof(igtl_uint16); /* BIND header section */ for (i = 0; i < nc; i ++) { /* Fill with 0 */ memset(ptr, 0, IGTL_HEADER_TYPE_SIZE); strncpy((char*)ptr, info->child_info_array[i].type, IGTL_HEADER_TYPE_SIZE); ptr += IGTL_HEADER_TYPE_SIZE; /* 2011-04-29 * Since ptr is not alined to double-word order at this point, * "* ((igtl_uint64 *) ptr) = <value>" may fail in some archtecture. * In the following section, memcpy() is called instaed of using * ((igtl_uint64 *) ptr) * expression. */ if (igtl_is_little_endian()) { tmp64 = BYTE_SWAP_INT64(info->child_info_array[i].size); memcpy(ptr, &tmp64, sizeof(igtl_uint64)); } else { memcpy(ptr, &(info->child_info_array[i].size), sizeof(igtl_uint64)); } ptr += sizeof(igtl_uint64); } /* Name table section */ nts = (igtl_uint16 *) ptr; /* save address for name table size field */ ptr += sizeof(igtl_uint16); wb = 0; for (i = 0; i < nc; i ++) { len = strlen(info->child_info_array[i].name); if (len > IGTL_HEADER_NAME_SIZE) { return 0; } /* copy string + NULL-terminator */ strncpy(ptr, info->child_info_array[i].name, len+1); ptr += (len + 1); wb += len + 1; } /* Add padding if the size of the name table section is not even */ if (wb % 2 > 0) { *((igtl_uint8*)ptr) = 0; ptr ++; wb ++; } /* Substitute name table size */ *nts = (igtl_uint16) wb; if (igtl_is_little_endian()) { *nts = BYTE_SWAP_INT16(*nts); } return 1; }
int igtl_export igtl_polydata_unpack(int type, void * byte_array, igtl_polydata_info * info, igtl_uint64 size) { /* size = number of ponits (not number of bytes). In case of vertices, this is specfied by size_vertices in igtl_polydata_header. */ igtl_polydata_header * header; char * ptr; igtl_uint32 * ptr32_src; igtl_uint32 * ptr32_src_end; igtl_uint32 * ptr32_dst; igtl_uint32 s; igtl_polydata_attribute_header * att_header; igtl_polydata_attribute * att; int total_name_length; int name_length; char name_buf[IGTL_POLY_MAX_ATTR_NAME_LEN+1]; unsigned int i; int n; if (byte_array == NULL || info == NULL || size == 0) { return 0; } /* POLYDATA header */ header = (igtl_polydata_header *) byte_array; if (igtl_is_little_endian()) { info->header.npoints = BYTE_SWAP_INT32(header->npoints); info->header.nvertices = BYTE_SWAP_INT32(header->nvertices); info->header.size_vertices = BYTE_SWAP_INT32(header->size_vertices); info->header.nlines = BYTE_SWAP_INT32(header->nlines); info->header.size_lines = BYTE_SWAP_INT32(header->size_lines); info->header.npolygons = BYTE_SWAP_INT32(header->npolygons); info->header.size_polygons = BYTE_SWAP_INT32(header->size_polygons); info->header.ntriangle_strips = BYTE_SWAP_INT32(header->ntriangle_strips); info->header.size_triangle_strips = BYTE_SWAP_INT32(header->size_triangle_strips); info->header.nattributes = BYTE_SWAP_INT32(header->nattributes); } else { memcpy(&(info->header), header, sizeof(igtl_polydata_header)); } /* Allocate memory to read data */ /* TODO: compare the size of info before copying the header. */ /* If the size doesn't change, avoid reallocation of memory. */ if (igtl_polydata_alloc_info(info) == 0) { return 0; } /* POINT section */ ptr = (char*) byte_array + sizeof(igtl_polydata_header); if (!igtl_is_little_endian()) { memcpy(info->points, ptr, sizeof(igtl_float32)*info->header.npoints*3); } else { ptr32_src = (igtl_uint32 *) ptr; ptr32_src_end = ptr32_src + info->header.npoints*3; ptr32_dst = (igtl_uint32 *) info->points; while (ptr32_src < ptr32_src_end) { *ptr32_dst = BYTE_SWAP_INT32(*ptr32_src); ptr32_dst ++; ptr32_src ++; } } ptr += sizeof(igtl_float32)*info->header.npoints*3; /* Check size parameters */ if (info->header.size_vertices%sizeof(igtl_uint32) != 0 || info->header.size_lines%sizeof(igtl_uint32) != 0 || info->header.size_polygons%sizeof(igtl_uint32) != 0 || info->header.size_triangle_strips%sizeof(igtl_uint32)) { /* More than one of size parameters is multiples of 4 (size of 32-bit value) */ return 0; } /* VERTICES section */ igtl_polydata_convert_byteorder_topology(info->vertices, (igtl_uint32*)ptr, info->header.size_vertices); ptr += info->header.size_vertices; /* LINES section */ igtl_polydata_convert_byteorder_topology(info->lines, (igtl_uint32*)ptr, info->header.size_lines); ptr += info->header.size_lines; /* POLYGONS section */ igtl_polydata_convert_byteorder_topology(info->polygons, (igtl_uint32*)ptr, info->header.size_polygons); ptr += info->header.size_polygons; /* TRIANGLE_STRIPS section */ igtl_polydata_convert_byteorder_topology(info->triangle_strips, (igtl_uint32*)ptr, info->header.size_triangle_strips); ptr += info->header.size_triangle_strips; /* Attribute header */ for (i = 0; i < info->header.nattributes; i ++) { att = &(info->attributes[i]); att_header = (igtl_polydata_attribute_header *) ptr; att->type = att_header->type; att->ncomponents = att_header->ncomponents; if (igtl_is_little_endian()) { att->n = BYTE_SWAP_INT32(att_header->n); } else { att->n = att_header->n; } ptr += sizeof(igtl_polydata_attribute_header); } /* Attribute names */ total_name_length = 0; name_buf[IGTL_POLY_MAX_ATTR_NAME_LEN] = '\0'; for (i = 0; i < info->header.nattributes; i ++) { name_length = strlen(ptr); if (name_length <= IGTL_POLY_MAX_ATTR_NAME_LEN) { info->attributes[i].name = malloc(name_length+1); strcpy(info->attributes[i].name, ptr); } else { /* invalid name length */ return 0; } total_name_length += (name_length+1); ptr += (name_length+1); } if (total_name_length % 2 > 0) { /* add padding */ ptr ++; } /* Attributes */ for (i = 0; i < info->header.nattributes; i ++) { if (info->attributes[i].type == IGTL_POLY_ATTR_TYPE_SCALAR) { n = info->attributes[i].ncomponents * info->attributes[i].n; s = n * sizeof(igtl_float32); } else if (info->attributes[i].type == IGTL_POLY_ATTR_TYPE_NORMAL) { n = 3 * info->attributes[i].n; s = n * sizeof(igtl_float32); } else if (info->attributes[i].type == IGTL_POLY_ATTR_TYPE_VECTOR) { n = 3 * info->attributes[i].n; s = n * sizeof(igtl_float32); } else /* TENSOR */ { n = 9 * info->attributes[i].n; s = n * sizeof(igtl_float32); } info->attributes[i].data = (igtl_float32*)malloc((size_t)s); ptr32_dst = (igtl_uint32*)info->attributes[i].data; ptr32_src = (igtl_uint32*)ptr; ptr32_src_end = ptr32_src + n; while (ptr32_src < ptr32_src_end) { *ptr32_dst = BYTE_SWAP_INT32(*ptr32_src); ptr32_dst ++; ptr32_src ++; } ptr += s; } return 1; }
int igtl_export igtl_polydata_pack(igtl_polydata_info * info, void * byte_array, int type) { /* size = number of ponits (not number of bytes). In case of vertices, this is specfied by size_vertices in igtl_polydata_header. */ igtl_polydata_header * header; char * ptr; igtl_uint32 * ptr32_src; igtl_uint32 * ptr32_src_end; igtl_uint32 * ptr32_dst; igtl_polydata_attribute_header * att_header; igtl_polydata_attribute * att; int total_name_length; int name_length; unsigned int i; int n; int size; if (byte_array == NULL || info == NULL) { return 0; } /* POLYDATA header */ header = (igtl_polydata_header *) byte_array; if (igtl_is_little_endian()) { header->npoints = BYTE_SWAP_INT32(info->header.npoints); header->nvertices = BYTE_SWAP_INT32(info->header.nvertices); header->size_vertices = BYTE_SWAP_INT32(info->header.size_vertices); header->nlines = BYTE_SWAP_INT32(info->header.nlines); header->size_lines = BYTE_SWAP_INT32(info->header.size_lines); header->npolygons = BYTE_SWAP_INT32(info->header.npolygons); header->size_polygons = BYTE_SWAP_INT32(info->header.size_polygons); header->ntriangle_strips = BYTE_SWAP_INT32(info->header.ntriangle_strips); header->size_triangle_strips = BYTE_SWAP_INT32(info->header.size_triangle_strips); header->nattributes = BYTE_SWAP_INT32(info->header.nattributes); } else { memcpy(header, &(info->header), sizeof(igtl_polydata_header)); } /* POINT section */ ptr = (char*) byte_array + sizeof(igtl_polydata_header); if (!igtl_is_little_endian()) { memcpy((void*)ptr, (void*)info->points, sizeof(igtl_float32)*info->header.npoints*3); } else { ptr32_src = (igtl_uint32 *) info->points; ptr32_src_end = ptr32_src + info->header.npoints*3; ptr32_dst = (igtl_uint32 *) ptr; while (ptr32_src < ptr32_src_end) { *ptr32_dst = BYTE_SWAP_INT32(*ptr32_src); ptr32_dst ++; ptr32_src ++; } } ptr += sizeof(igtl_float32)*info->header.npoints*3; /* Check size parameters */ if (info->header.size_vertices%sizeof(igtl_uint32) != 0 || info->header.size_lines%sizeof(igtl_uint32) != 0 || info->header.size_polygons%sizeof(igtl_uint32) != 0 || info->header.size_triangle_strips%sizeof(igtl_uint32)) { /* More than one of size parameters is multiples of 4 (size of 32-bit value) */ return 0; } /* VERTICES section */ igtl_polydata_convert_byteorder_topology((igtl_uint32*)ptr, info->vertices, info->header.size_vertices); ptr += info->header.size_vertices; /* LINES section */ igtl_polydata_convert_byteorder_topology((igtl_uint32*)ptr, info->lines, info->header.size_lines); ptr += info->header.size_lines; /* POLYGONS section */ igtl_polydata_convert_byteorder_topology((igtl_uint32*)ptr, info->polygons, info->header.size_polygons); ptr += info->header.size_polygons; /* TRIANGLE_STRIPS section */ igtl_polydata_convert_byteorder_topology((igtl_uint32*)ptr, info->triangle_strips, info->header.size_triangle_strips); ptr += info->header.size_triangle_strips; /* Attribute header */ for (i = 0; i < info->header.nattributes; i ++) { att = &(info->attributes[i]); att_header = (igtl_polydata_attribute_header *) ptr; att_header->type = att->type; if (att->type == IGTL_POLY_ATTR_TYPE_SCALAR) { att_header->ncomponents = att->ncomponents; } else if (att->type == IGTL_POLY_ATTR_TYPE_VECTOR) { att_header->ncomponents = 3; } else if (att->type == IGTL_POLY_ATTR_TYPE_NORMAL) { att_header->ncomponents = 3; } else /* att->type == IGTL_POLY_ATTR_TYPE_TENSOR */ { att_header->ncomponents = 9; } if (igtl_is_little_endian()) { att_header->n = BYTE_SWAP_INT32(att->n); } else { att_header->n = att->n; } ptr += sizeof(igtl_polydata_attribute_header); } /* Attribute names */ total_name_length = 0; for (i = 0; i < info->header.nattributes; i ++) { name_length = strlen(info->attributes[i].name); if (name_length > IGTL_POLY_MAX_ATTR_NAME_LEN) { return 0; } strcpy(ptr, info->attributes[i].name); total_name_length += (name_length+1); ptr += (name_length+1); } if (total_name_length % 2 > 0) { /* add padding */ *(char*)ptr = '\0'; ptr ++; } /* Attributes */ for (i = 0; i < info->header.nattributes; i ++) { if (info->attributes[i].type == IGTL_POLY_ATTR_TYPE_SCALAR) { n = info->attributes[i].ncomponents * info->attributes[i].n; size = n * sizeof(igtl_float32); } else if (info->attributes[i].type == IGTL_POLY_ATTR_TYPE_NORMAL) { n = 3 * info->attributes[i].n; size = n * sizeof(igtl_float32); } else if (info->attributes[i].type == IGTL_POLY_ATTR_TYPE_VECTOR) { n = 3 * info->attributes[i].n; size = n * sizeof(igtl_float32); } else /* TENSOR */ { n = 9 * info->attributes[i].n; size = n * sizeof(igtl_float32); } if (igtl_is_little_endian()) { ptr32_dst = (igtl_uint32*)ptr; ptr32_src = (igtl_uint32*)info->attributes[i].data; ptr32_src_end = ptr32_src + n; while (ptr32_src < ptr32_src_end) { *ptr32_dst = BYTE_SWAP_INT32(*ptr32_src); ptr32_dst ++; ptr32_src ++; } } else { memcpy(ptr, info->attributes[i].data, size); } ptr += size; } return 1; }