/* ----------------------------- MNI Header ----------------------------------- @NAME : steal_element @INPUT : group element - element to remove previous - pointer to previous element or NULL if beginning of group element list @OUTPUT : (none) @RETURNS : (nothing) @DESCRIPTION: Steal an element from a group (remove it without deleting it). The caller must free the element themselves. @METHOD : @GLOBALS : @CALLS : @CREATED : November 6, 1998 (Peter Neelin) @MODIFIED : ---------------------------------------------------------------------------- */ static void steal_element(Acr_Group group, Acr_Element element, Acr_Element previous) { Acr_Element next; /* Get pointer to next element */ next = acr_get_element_next(element); /* Update the previous element or list head */ if (previous != NULL) acr_set_element_next(previous, next); else group->list_head = next; /* Check for an element at the tail */ if (next == NULL) group->list_tail = previous; /* Update the group fields */ group->nelements--; group->implicit_total_length -= acr_get_element_total_length(element, ACR_IMPLICIT_VR); group->explicit_total_length -= acr_get_element_total_length(element, ACR_EXPLICIT_VR); /* Update the group length element */ update_group_length_element(group, acr_get_element_vr_encoding(group->list_head)); }
/* ----------------------------- MNI Header ----------------------------------- @NAME : insert_element @INPUT : group element - element to insert previous - pointer to previous element or NULL if beginning of group element list @OUTPUT : (none) @RETURNS : Acr_Status @DESCRIPTION: Insert an element into a group. @METHOD : @GLOBALS : @CALLS : @CREATED : June 17, 1997 (Peter Neelin) @MODIFIED : ---------------------------------------------------------------------------- */ static Acr_Status insert_element(Acr_Group group, Acr_Element element, Acr_Element previous) { Acr_Element next; long length; /* Update the pointers */ if (previous != NULL) { /* Middle or tail of list */ next = acr_get_element_next(previous); acr_set_element_next(previous, element); } else { /* Head of list */ next = group->list_head; group->list_head = element; } acr_set_element_next(element, next); /* Check for the tail */ if (next == NULL) { group->list_tail = element; } /* Update the group fields */ group->nelements++; length = acr_get_element_total_length(element, ACR_IMPLICIT_VR); if (length <= 0) { return (ACR_OTHER_ERROR); } group->implicit_total_length += length; length = acr_get_element_total_length(element, ACR_EXPLICIT_VR); if (length <= 0) { return (ACR_OTHER_ERROR); } group->explicit_total_length += length; /* Update the length element */ update_group_length_element(group, acr_get_element_vr_encoding(group->list_head)); return (ACR_OK); }
/* ----------------------------- MNI Header ----------------------------------- @NAME : save_transferred_object @INPUT : group_list - list of acr-nema groups that make up object file_prefix - prefix for file names @OUTPUT : new_file_name - name for newly created file data_info - information about data object @RETURNS : (nothing) @DESCRIPTION: Routine to save the object in a file. @METHOD : @GLOBALS : @CALLS : @CREATED : November 24, 1993 (Peter Neelin) @MODIFIED : ---------------------------------------------------------------------------- */ public void save_transferred_object(Acr_Group group_list, char *file_prefix, char **new_file_name, Data_Object_Info *data_info) { Acr_Group group; Acr_Element element; char temp_name[256]; char patient_name[256]; int study_id, acquisition_id, image_id; Acr_File *afp; FILE *fp; Acr_Status status; Acr_VR_encoding_type vr_encoding; Acr_byte_order byte_order; static int file_counter = 0; /* Get the VR encoding state and byte order */ element = acr_get_group_element_list(group_list); vr_encoding = acr_get_element_vr_encoding(element); byte_order = acr_get_element_byte_order(element); /* Get data info */ get_identification_info(group_list, &(data_info->study_id), &(data_info->acq_id), &(data_info->rec_num), &(data_info->image_type)); /* Get number of echos, echo number, number of dynamic scans and dynamic_scan_number */ data_info->num_echoes = acr_find_int(group_list, SPI_Number_of_echoes, 1); data_info->echo_number = acr_find_int(group_list, ACR_Echo_number, 1); data_info->num_dyn_scans = acr_find_int(group_list, ACR_Acquisitions_in_series, 1); data_info->dyn_scan_number = acr_find_int(group_list, ACR_Series, 1); /* Look for patient name */ element = acr_find_group_element(group_list, ACR_Patient_name); if (element != NULL) { string_to_filename(acr_get_element_string(element), patient_name, sizeof(patient_name)); } if ((element == NULL) || (strlen(patient_name) == 0)) (void) strcpy(patient_name, "unknown"); /* Look for study and image numbers */ study_id = data_info->study_id; acquisition_id = data_info->acq_id; image_id = acr_find_int(group_list, ACR_Image, 0); /* Create the new file name */ (void) sprintf(temp_name, "%s-%04d-%s_%d_%d_%d.dcm", file_prefix, file_counter++, patient_name, study_id, acquisition_id, image_id); /* Create the file and write out the data */ fp = fopen(temp_name, "w"); if (fp == NULL) { (void) fprintf(stderr, "Error opening file for write: %s\n", temp_name); } else { /* Set up the output stream */ afp = acr_file_initialize(fp, 0, acr_stdio_write); acr_set_vr_encoding(afp, vr_encoding); acr_set_byte_order(afp, byte_order); /* Loop over groups */ group = group_list; status = ACR_OK; while ((group != NULL) && (status == ACR_OK)) { /* Write out the group */ status = acr_output_group(afp, group); if (status != ACR_OK) { (void) fprintf(stderr, "Error writing file %s\n", temp_name); } group = acr_get_group_next(group); } /* Close the file */ acr_file_free(afp); (void) fclose(fp); } /* Copy the name */ *new_file_name = strdup(temp_name); return; }