예제 #1
0
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);
        }
      }
    }
}
예제 #2
0
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);
  }
}
예제 #3
0
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);

    }
}
예제 #4
0
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);
    }
}
예제 #5
0
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);
    }
}
예제 #6
0
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);
      }
    }
}
예제 #7
0
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);
    }
}
예제 #8
0
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)
	}

}
예제 #9
0
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;
}
예제 #10
0
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);
    }
}
예제 #11
0
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;
}
예제 #12
0
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;
}
예제 #13
0
/// 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);
		
	}
}
예제 #14
0
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;
	}
}
예제 #15
0
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;

}
예제 #16
0
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;

}