Ejemplo n.º 1
0
ERROR_CODE _bencoding_list_start_callback(struct bencoding_engine_t *bencoding_engine) {
    /* allocates space for the linked list and for the type
    structure that will encapsulate it */
    struct linked_list_t *list;
    struct type_t *type;

    /* retrieves the bencoding handler from the template engine context
    then uses it to store the (current) value */
    struct bencoding_handler_t *bencoding_handler = (struct bencoding_handler_t *) bencoding_engine->context;

    /* creates a new linked list for the new structure (sequence) context
    and then creates the respective type for the list */
    create_linked_list(&list);
    create_type(&type, LIST_TYPE);
    *type = list_type(list);

    /* adds the sequence type to the sequence stack and then adds the
    current handler key to the key stack, then updates the current sequence
    reference and the current key in the handler */
    append_value_linked_list(bencoding_handler->sequence_stack, (void *) type);
    append_value_linked_list(bencoding_handler->key_stack, (void *) bencoding_handler->key);
    bencoding_handler->sequence = type;
    bencoding_handler->key = NULL;

    /* in case there is no top type defined for the handler sets the
    current type as the top type (base type) */
    if(bencoding_handler->top == NULL) { bencoding_handler->top = type; }

    /* unsets the next key flag to avoid any possible misbehavior, colliding
    with the hash map structure */
    bencoding_handler->next_key = 0;

    /* raises no error */
    RAISE_NO_ERROR;
}
Ejemplo n.º 2
0
void test_linked_list_creation_and_deletion ()
{
  // create and delete simple array list
  linked_list *list = create_linked_list ();
  TEST_ASSERT_NOT_NULL (list);
  delete_linked_list (list);
}
Ejemplo n.º 3
0
static void __alloc(lifo_t *lifo)
{
	*lifo = malloc(sizeof(struct lifo));
	(*lifo)->priv = malloc(sizeof(struct internal));
	(*lifo)->priv->list = create_linked_list
	        (POOF_HEAD | PEEK_HEAD | PUSH_HEAD);
}
Ejemplo n.º 4
0
static void __alloc(dec_t *dec)
{
	(*dec) = malloc(sizeof(struct dequeue));
 	(*dec)->priv = malloc(sizeof(struct dequeue));
	(*dec)->priv->list = create_linked_list
	        (POOF_BOTH | PEEK_BOTH | PUSH_BOTH);
}
Ejemplo n.º 5
0
static void linked_list_should_have_expected_nodes()
{
    Node *head = create_linked_list(10);
    
    int count = get_linked_list_node_count(head);
    
    assert_int_equal(10, count); 
}
int main(int argc, char* argv[])
{
  int a[] = {1,2,3,4,5};
  s_node* root = create_linked_list(a,sizeof(a)/sizeof(a[0]));
  s_node* r = get_element(root, 3);
  print_linked_list(r);
  clear_linked_list(root);
  return 0;
}
int main(int argc, char* argv[])
{
  int arr[10] = {1,2,3,3,1,1,3,3,5,6};
  s_node* root = create_linked_list(arr,10);
  remove_dup(root);
  print_linked_list(root);
  clear_linked_list(root);
  return 0;
}
Ejemplo n.º 8
0
void create_thread_pool(struct thread_pool_t **thread_pool_pointer, size_t number_threads, size_t scheduling_algorithm, size_t maximum_number_threads) {
    /* allocates space for the index */
    size_t index;

    /* retrieves the thread pool size */
    size_t thread_pool_size = sizeof(struct thread_pool_t);

    /* allocates space for the thread pool */
    struct thread_pool_t *thread_pool = (struct thread_pool_t *) MALLOC(thread_pool_size);

    /* sets the number of threads */
    thread_pool->number_threads = number_threads;

    /* sets the scheduling algorithm */
    thread_pool->scheduling_algorithm = scheduling_algorithm;

    /* sets the maximum number of threads */
    thread_pool->maximum_number_threads = maximum_number_threads;

    /* initializes the thread pool current number of threads */
    thread_pool->current_number_threads = 0;

    /* creates the task condition */
    CONDITION_CREATE(thread_pool->task_condition);

    /* creates the task condition lock */
    CRITICAL_SECTION_CREATE(thread_pool->task_condition_lock);

    /* creates the worker threads list */
    create_linked_list(&thread_pool->worker_threads_list);

    /* creates the task queue */
    create_linked_list(&thread_pool->task_queue);

    /* iterates over the number of threas */
    for(index = 0; index < number_threads; index++) {
        /* creates a thread pool elememt for the thread pool */
        create_thread_pool_element(thread_pool);
    }

    /* sets the thread pool in the thread pool pointer */
    *thread_pool_pointer = thread_pool;
}
Ejemplo n.º 9
0
void test_linked_list_add_and_remove ()
{
  linked_list *list = create_linked_list ();
  TEST_ASSERT_NOT_NULL (list);

  // try to append an item and get it back later
  char *str1 = "hello";
  linked_list_append (list, str1);
  TEST_ASSERT_EQUAL_INT (list->item_count, 1);
  TEST_ASSERT_EQUAL_STR (linked_list_get (list, 0), str1);

  // try to append multiple items and get one back later
  char *strs1[] = {"world", "!"};
  linked_list_append_all (list, (void **) strs1, 2);
  TEST_ASSERT_EQUAL_INT (list->item_count, 3);
  TEST_ASSERT_EQUAL_STR (linked_list_get (list, 0), str1);
  TEST_ASSERT_EQUAL_STR (linked_list_get (list, 1), strs1[0]);
  TEST_ASSERT_EQUAL_STR (linked_list_get (list, 2), strs1[1]);

  // try to add an item at a specific position
  char *str2 = " ";
  linked_list_add (list, 1, str2);
  TEST_ASSERT_EQUAL_INT (list->item_count, 4);
  TEST_ASSERT_EQUAL_STR (linked_list_get (list, 0), str1);
  TEST_ASSERT_EQUAL_STR (linked_list_get (list, 1), str2);
  TEST_ASSERT_EQUAL_STR (linked_list_get (list, 2), strs1[0]);
  TEST_ASSERT_EQUAL_STR (linked_list_get (list, 3), strs1[1]);
  
  // try to add multiple items at a specific position
  char *strs2[] = {"WORLD", "?", "\n", "HELLO "};
  linked_list_add_all (list, 2, (void **) strs2, 4);
  TEST_ASSERT_EQUAL_INT (list->item_count, 8);
  TEST_ASSERT_EQUAL_STR (linked_list_get (list, 0), str1);
  TEST_ASSERT_EQUAL_STR (linked_list_get (list, 1), str2);
  TEST_ASSERT_EQUAL_STR (linked_list_get (list, 2), strs2[0]);
  TEST_ASSERT_EQUAL_STR (linked_list_get (list, 3), strs2[1]);
  TEST_ASSERT_EQUAL_STR (linked_list_get (list, 4), strs2[2]);
  TEST_ASSERT_EQUAL_STR (linked_list_get (list, 5), strs2[3]);
  TEST_ASSERT_EQUAL_STR (linked_list_get (list, 6), strs1[0]);
  TEST_ASSERT_EQUAL_STR (linked_list_get (list, 7), strs1[1]);

  // try to remove an item
  linked_list_remove (list, 6);
  TEST_ASSERT_EQUAL_INT (list->item_count, 7);
  TEST_ASSERT_EQUAL_STR (linked_list_get (list, 0), str1);
  TEST_ASSERT_EQUAL_STR (linked_list_get (list, 1), str2);
  TEST_ASSERT_EQUAL_STR (linked_list_get (list, 2), strs2[0]);
  TEST_ASSERT_EQUAL_STR (linked_list_get (list, 3), strs2[1]);
  TEST_ASSERT_EQUAL_STR (linked_list_get (list, 4), strs2[2]);
  TEST_ASSERT_EQUAL_STR (linked_list_get (list, 5), strs2[3]);
  TEST_ASSERT_EQUAL_STR (linked_list_get (list, 6), strs1[1]);

  delete_linked_list (list);
}
Ejemplo n.º 10
0
void test_insert_delete_node()
{
	int input_array[12][10] = {{23,32,57,64},{2,14,20,64,146},{1,3,7,12,35,52,72,91},{23},{NULL},{90,91,92},{67,74,153,262},{23,45},{36,47,53,69},{12,14,25,66,91},{-9,-8,-7,-6,-5,4,3,2,1},{1,2,3,4,5,6,7,8,9}};

	int key[12] = {64,2,25,23,3,3,265,32,268,2,5,-5};

	int output[12][10] = {{23,32,57},{14,20,64,146},{1,3,7,12,25,35,52,72,91},{NULL},{NULL},{3,90,91,92},{67,74,153,262,265},{23,32,45},{36,47,53,69,268},{2,12,14,25,66,91},{-9,-8,-7,-6,-5,4,3,2,1},{-5,1,2,3,4,5,6,7,8,9}};

	int iter_loop;
	for(iter_loop=0;iter_loop<12;iter_loop++)
	{
		NODE *input_list = create_linked_list(input_array[iter_loop]);
		NODE *output_list = create_linked_list(output[iter_loop]);
		input_list = insert_delete_node(input_list,key[iter_loop]);
		NODE *input_list_duplicate = input_list;
		NODE *output_list_duplicate = output_list;
			while(input_list!=NULL || output_list!=NULL)
			{
				if(input_list == NULL || output_list== NULL)
				{
					printf("ACCEPTED\n");
					break;
				}
				if(input_list->data!= output_list->data)
				{
					printf("REJECTED\n");
					delete_linked_list(input_list_duplicate);
					delete_linked_list(output_list_duplicate);
					break;
				}
				input_list = input_list->next;
				output_list = output_list->next;
			}
			if(input_list==NULL && output_list==NULL)
			{
				printf("ACCEPTED\n");
				delete_linked_list(input_list_duplicate);
				delete_linked_list(output_list_duplicate);
			}
	}
}
Ejemplo n.º 11
0
void create_bencoding_handler(struct bencoding_handler_t **bencoding_handler_pointer) {
    /* retrieves the bencoding handler size */
    size_t bencoding_handler_size = sizeof(struct bencoding_handler_t);

    /* allocates space for the bencoding handler */
    struct bencoding_handler_t *bencoding_handler = (struct bencoding_handler_t *) MALLOC(bencoding_handler_size);

    /* initializes the bencoding handler */
    bencoding_handler->sequence = NULL;
    bencoding_handler->top = NULL;
    bencoding_handler->key = NULL;
    bencoding_handler->next_key = 0;

    /* creates the linked lists for both the sequence and key stack
    values (used for runtime) */
    create_linked_list(&bencoding_handler->sequence_stack);
    create_linked_list(&bencoding_handler->key_stack);

    /* sets the bencoding handler in the bencoding handler pointer */
    *bencoding_handler_pointer = bencoding_handler;
}
Ejemplo n.º 12
0
void test_linked_list_clear ()
{
  linked_list *list = create_linked_list ();
  TEST_ASSERT_NOT_NULL (list);
  char *strs1[] = {"test", "content", "random", "word", "code"};
  linked_list_append_all (list, (void **) strs1, 5);
   
  // try to clear the list
  linked_list_clear (list);
  TEST_ASSERT_EQUAL_INT (list->item_count, 0);

  delete_linked_list (list);
}
Ejemplo n.º 13
0
void test_linked_list_to_array ()
{
  linked_list *list = create_linked_list ();
  TEST_ASSERT_NOT_NULL (list);
  char *strs1[] = {"test", "content"};
  linked_list_append_all (list, (void **) strs1, 2);
   
  // try to make an array from list
  char *str1 = linked_list_to_array (list, sizeof (char));
  TEST_ASSERT_EQUAL_INT (str1[0], strs1[0][0]);
  TEST_ASSERT_EQUAL_INT (str1[1], strs1[1][0]);

  delete_linked_list (list);
}
Ejemplo n.º 14
0
void test_sort_linked_list()
{
	int input_array[8][10]  = {{9, 8, 7, 6, 5, 4, 3, 2, 1},{34, 76, 23, 98, 43},{2,1},{1},{NULL},{8, 7, 6, 5, 4, 3, 2, 1},{3,2,1},{236,235,440,100}};
	int output_array[8][10] = {{1, 2, 3, 4, 5, 6, 7, 8, 9},{23, 34, 43, 76, 98},{1,2},{1},{NULL},{1, 2, 3, 4, 5, 6, 7, 8},{1,2,3},{100, 235, 236, 440}};
	int iter_loop;
	for(iter_loop=0;iter_loop<8;iter_loop++)
	{
		NODE *input_list = create_linked_list(input_array[iter_loop]);
		NODE *output_list = create_linked_list(output_array[iter_loop]);
		input_list = sort_linked_list(input_list);
		NODE *head_input_list = input_list;
		NODE *head_output_list = output_list;
		if(input_list == NULL && output_list == NULL)
		{
			printf("ACCEPTED\n");
			break;
		}
		while (input_list!=NULL || output_list!=NULL)
		{
			if(input_list->data != output_list->data)
			{
				printf("REJECTED\n");
				delete_linked_list(head_input_list);
				delete_linked_list(head_output_list);
				break;
			}
			input_list = input_list->next;
			output_list = output_list->next;
		}
		if(input_list == NULL && output_list == NULL)
		{
			printf("ACCEPTED\n");
			delete_linked_list(head_input_list);
			delete_linked_list(head_output_list);
		}
	}
}
Ejemplo n.º 15
0
void test_linked_list_get_and_indexof ()
{
  linked_list *list = create_linked_list ();
  TEST_ASSERT_NOT_NULL (list);
  char *strs1[] = {"test", "content", "random", "word", "code",
		   "free", "software", "no", "ideas"};
  linked_list_append_all (list, (void **) strs1, 9);
  
  // try to get the index of an item
  TEST_ASSERT_EQUAL_INT (linked_list_index_of (list, strs1[2]), 2);
 
  // try to get a item by its index
  TEST_ASSERT_EQUAL_STR (linked_list_get (list, 5), strs1[5]); 

  delete_linked_list (list);
}
Ejemplo n.º 16
0
AdjacencyList* create_adjacency_list(int size) {
	AdjacencyList* new_adj_list;
	int i;

	if(size <= 0)
		return NULL;

	new_adj_list = (AdjacencyList*)malloc(sizeof(AdjacencyList));

	// Initiate the adjacency list
	new_adj_list->heads = (LinkedList**)malloc(sizeof(LinkedList*)*size);
	for(i=0 ; i<size ; i++)
		new_adj_list->heads[i] = create_linked_list();
	new_adj_list->size = size;

	return new_adj_list;
}
void test_string_to_linked_list()
{
	char input[8][30] = {"123,34,456,7893,321",
								"1,2,3,4,5,6,7,8,9",
								"1",
								"12a,123g,456,78R9",
								"214r,1,2,3,4,5,6474y",
								"44561a",
								"1q,2w,3e,4r,5,6,7,8i,9o,7p",
								NULL,
							  };
	int output[8][10] = {{123,34,456,7893,321},{1,2,3,4,5,6,7,8,9},{1},{456},{1,2,3,4,5},{NULL},{5,6,7},{NULL}};
	int iter_loop;
	for(iter_loop=0;iter_loop<8;iter_loop++)
	{
		NODE *input_list = string_to_linked_list(input[iter_loop]);
		NODE *output_list = create_linked_list(output[iter_loop]);
		NODE *head_input_list = input_list;
		NODE *head_output_list = output_list;
		if(input_list==NULL || output_list==NULL)
		{
			printf("ACCEPTED\n");
			continue;
		}
		while(input_list!=NULL || output_list!=NULL)
		{
			if(input_list->data!=output_list->data)
			{
				printf("REJECTED\n");
				delete_linked_list(head_input_list);
				delete_linked_list(head_output_list);
			}
			input_list = input_list->next;
			output_list = output_list->next;
		}
		if(input_list==NULL && output_list==NULL)
		{
			printf("ACCEPTED\n");
			delete_linked_list(head_input_list);
			delete_linked_list(head_output_list);
			
		}
	}
}
Ejemplo n.º 18
0
LinkedList *create_linked_list_from_array(unsigned long data_size, void *array, int n){
	/**
	 * Creates and returns a linked list by reading n objects of size data_size
	 * from the array indicated by the pointer.
	 */
	LinkedList *list = create_linked_list(data_size);
	
	int i;
	for(i = 0; i < n; i++){
		//allocate memory for data copy
		void *item = malloc(data_size);
		void *address = array + i*data_size;
		memcpy(item, address, data_size);
		append_linked_list(list, item);
	}
	
	
	return list;
}
Ejemplo n.º 19
0
struct node * create_list(char *ip)
{
	struct node *start=NULL;
	int num=0,i=0,temp;
	if(ip==NULL)
		return NULL;
	while(ip[i]!='\0')
	{
		num=0;
		while(ip[i]!=','&&ip[i]!='\0')
		{
			temp=ip[i]-'0';
			num=num*10+temp;
			i++;
		}
		start=create_linked_list(start,num);
		i++;
	}
	return start;
}
Ejemplo n.º 20
0
int main(void)
{
	PNODE phead = NULL;//生成一个头指针,先指向空
	int val;
	int ppos;
	phead = create_linked_list();//动态生成单链表,并将头节点地址赋给头指针
	traverse_linked_list(phead);
	/*if ( is_empty_linked_list(phead) )
		printf("链表空\n");
	else	
		printf("链表不空\n");
	*/
	//int len = length_linked_list(phead);
	//printf("len = %d\n",len);
	
	if ( insert_linked_list(phead,3,3,&ppos,&val) )
	{
		printf("在第%d个元素前插入成功,插入的元素是%d\n",ppos,val);
	}
	else
	{
		printf("在第%d个元素前插入元素%d失败\n",ppos,val);
	}
	traverse_linked_list(phead);
	//sort_linked_list(phead);
	 
	if ( delete_linked_list(phead,4,&val,&ppos))
	{
		printf("删除成功,删除的元素是第%d个元素%d\n",ppos,val);
	}
	else
	{
		printf("删除第%d个元素失败\n",ppos);
	}
	
	traverse_linked_list(phead);
	return 0;
}
Ejemplo n.º 21
0
linked_list* connComponents(const image_t *imgGy,int32_t minNumPixels){

	int32_t i,j;
	struct pixel seed;
	uint8_t T;
	uint8_t **dummy;
	int32_t Nset, ClassLabel;
	int32_t dummyClass;
	symbol_t *symbol, *symbol_tmp;
	linked_list *symbols_list;
	int32_t height, width;

	height = imgGy->height;
	width = imgGy->width;

	/* Set up segmented image structure */
	dummy=(uint8_t **) multialloc (sizeof (uint8_t), 2, (int)height, (int)width);
	symbols_list = create_linked_list();
	symbol_tmp = malloc(sizeof(symbol_t));

	/********************** Segment Image ******************/

	/* set threshold  */
	T=1;

	/* Initialize segmentation image */
	for ( i = 0; i < height; i++ ){
		for ( j = 0; j < width; j++ )
		{
			dummy[i][j] = 0;
		}
	}

	dummyClass = -1;

	/* Start class labeling at 1 */
	ClassLabel = 1;

	for ( i = 0; i < height; i++ )
		for ( j = 0; j < width; j++ )
		{
			/* If not yet classified */
			if (dummy[i][j] == 0 && getPixel(imgGy,i,j) == 1)
			{
				seed.row=i;
				seed.col=j;
				/* using this only to find Nset; will also indicate tested regions */
				ConnectedSet(seed, T, imgGy, width, height,
				             &dummyClass, dummy, &Nset, symbol_tmp);
				/* If more than 100 pixels, classify set*/
				if (Nset > minNumPixels)
				{
					symbol = malloc(sizeof(symbol_t));
					symbol->top=(uint16_t)height; symbol->bottom=0; 
					symbol->left=(uint16_t)width; symbol->right=0;

					ConnectedSet(seed, T, imgGy, width,
					             height, &ClassLabel, dummy, &Nset, symbol);

					symbol->height = (uint16_t)(symbol->bottom - symbol->top + 1);
					symbol->width = (uint16_t)(symbol->right - symbol->left + 1);
					symbol->HtW = (uint16_t)(symbol->height/symbol->width);
					symbol->NumBlack = (uint16_t)(Nset);
		
					symbol->class_label = -1;

					push_bottom(symbols_list, symbol);
	
					ClassLabel = ClassLabel + 1;
					if (ClassLabel > 255)
					{
						printf("Error: More than 256 classes.\n");
						printf("Need to increase minimum pixels per class.\n");
						exit(1);
					}
				}
			}
		}

	ClassLabel = ClassLabel - 1;

	
	multifree(dummy, 2);
	free(symbol_tmp);

	return symbols_list;
}
Ejemplo n.º 22
0
void fix_lines_and_remove(image_t* img,params* parameters, uint32_t** last_STAFFLINES, uint32_t *previous_start, uint32_t cutNum){
/*function [result,new_start,STAFFLINES] = fix_lines_and_remove(img,params,last_STAFFLINES,previous_start,cutNum)*/
/*remvoe lines from small portion of staff. also straightens staff.*/
	flex_array_t *stafflines_tmp;	
	uint32_t *yprojection, line_thickness, line_spacing, line_w, *last_STAFFLINES_avg, max_project, sum, h, w,
		i, j, ii, count, **STAFFLINES, dummy, loc, shift, findLine, match, lineBegin,
		lineEnd, found_thickness, middle, tooThick, tooHigh, any_stafflines_zero, now_avg, last_avg, goodLine,
		tooLow, curr, extend, lastDelete, cW, cH, topStop, botStop, thickness, paramThickness, thickness_th,
		topLine, shift_loop,k;
	int16_t *tempData,*temp_array;
	int32_t lineY;
	uint8_t pixelData;
	linked_list *staffLines, *allLINES, *temp;
		
	STAFFLINES=multialloc(sizeof(uint32_t),2,5,2);

	h = img->height;
	w = img->width;
	line_thickness = (uint32_t)(parameters->thickness);
	line_spacing = (uint32_t)(parameters->spacing);
	line_w = (uint32_t)(parameters->thickness + parameters->spacing);
	
	last_STAFFLINES_avg = (uint32_t *)malloc(sizeof(uint32_t)*5);/*mget_spc((uint32_t)5, sizeof(uint32_t));*/
	for(i=0; i<5; i++){
		last_STAFFLINES_avg[i] = (uint32_t)((last_STAFFLINES[i][0] + last_STAFFLINES[i][1] + 1)/2);
	}
	yprojection= (uint32_t *)mget_spc((uint32_t)h, sizeof(uint32_t));
	max_project = 0;
	for(i=0;i<h;i++){
		sum = 0;
		for(j=0;j<w;j++){
			sum += getPixel(img,i,j);
		}
		yprojection[i] = sum;
		if(yprojection[i] > max_project){
			max_project = yprojection[i];		
		}
	}
	count = 0;
	for(i=0;i<h;i++){
    		if (yprojection[i] >= (9*max_project)/10){    /*delete staff line, twiddle with the 80% later (90%)*/
       			count++;
    		}
	}
	stafflines_tmp=make_flex_array(count);
	count = 0;
	for(i=0;i<h;i++){
    		if (yprojection[i] >= (9*max_project)/10){    /*delete staff line, twiddle with the 80% later (90%)*/
			stafflines_tmp->data[count] = i;       			
			count++;
    		}
	}
	free(yprojection);
	staffLines = group(stafflines_tmp, 3);
/*CHANGE: CUTNUM = 1 TO 0*/
	if (cutNum == 0 && staffLines->length == 5 ){
/*END CHANGE*/
		i=0;
	    	while(is_list_empty(staffLines)==0){
			tempData=(int16_t*)pop_top(staffLines);
			STAFFLINES[i][0] = tempData[0];
			STAFFLINES[i][1] = tempData[1];
			i++;
			free(tempData);
		}
	}
	else if ((staffLines->length) == 0){		
		for(i=0;i<5;i++){
			STAFFLINES[i][0] = last_STAFFLINES[i][0];
			STAFFLINES[i][1] = last_STAFFLINES[i][1];
		}
	}
/*CHANGE: CUTNUM = 1 TO 0*/
	else if (cutNum == 0 && (staffLines->length) < 5){
/*END CHANGE*/
	    	/*choose one line, then find closest line in last_STAFFLINES*/
		tempData = (int16_t*)(getIndexData(staffLines, 0));
	    	goodLine = (uint32_t)((tempData[0]+tempData[1]+1)/2);
		
		dummy = abs(last_STAFFLINES_avg[0] - goodLine);
		loc = 0;		
		for(i=1;i<5;i++){
			curr = abs(last_STAFFLINES_avg[i] - goodLine);
			if(curr<dummy){
				dummy = curr;				
				loc = i;
			}
		}
	    	shift = goodLine - last_STAFFLINES_avg[loc];
	    
	    	for(i=0;i<5;i++){
			STAFFLINES[i][0] = last_STAFFLINES[i][0]+shift;
			STAFFLINES[i][1] = last_STAFFLINES[i][1]+shift;
		}
	}
	

	else{
		count = 0;
		for(findLine=0;findLine<5;findLine++){

			match = 0;
			for(i=0;i<(staffLines->length);i++){
				tempData = (int16_t*)(getIndexData(staffLines, i));
			    	lineBegin = (uint32_t)tempData[0];
			    	lineEnd = (uint32_t)tempData[1];
				/*lineBegin is top of line, lineEnd is bottom*/

			    	found_thickness = lineEnd-lineBegin+1;
/*CHANGED: 0.5 TO 1/2*/
			    	middle = (uint32_t)((lineBegin + lineEnd+1)/2);
/*END CHANGED*/

			    	/*determine if the line is of expected location/size*/
				tooThick = 0;
				tooHigh = 0;
				tooLow = 0;
			    	if(found_thickness > (line_thickness+2)) tooThick=1;
				if(middle < (last_STAFFLINES_avg[findLine] - 3)) tooHigh=1;
				if(middle > (last_STAFFLINES_avg[findLine] + 3)) tooLow=1;
/*CHANGED: 1 TO 0*/
			    	if (cutNum == 0){
/*END CHANGED*/
					tooHigh = 0;
					tooLow = 0;
					if(middle < (last_STAFFLINES_avg[0] - 2*line_spacing)){tooHigh=1;}
/*CHANGED + TO - ALSO, avg[5] -> avg[4] */
					if(middle > (last_STAFFLINES_avg[4] + 2*line_spacing)){tooLow=1;}
/*END CHANGED*/
			    	}

			    	if (tooThick || tooHigh || tooLow){
					continue;
			    	}
			    	else{ /*we found good match for staffline*/
					match = 1;
					/*SAVE STAFF LINE LOCATIONS*/
					STAFFLINES[count][0] = lineBegin;
					STAFFLINES[count][1] = lineEnd;
					count++;
					deleteIndexData(staffLines,i);
					break;
			    	}        



			} /*end looping thru found lines*/

			if(!match){ /*CHANGED*/
		    		/*flag that no match was found*/
		    		STAFFLINES[count][0] = 0;
				STAFFLINES[count][1] = 0;
				count++;
			} 

	    	} /*end looping through matching staff lines*/
	    
	    	/*CHANGED BELOW*/
	    
	    	/*check for lines that did not get match*/
		any_stafflines_zero = 0;
		for(i=0;i<5;i++){
			if(STAFFLINES[i][0] == 0){
				any_stafflines_zero = 1;
				break;
			}
		}
	    	if (any_stafflines_zero){
			/*find shift value first*/
			shift = 100; /*big value*/
			for (findLine = 0; findLine<5;findLine++){
			/*loop to find nonzero entry in STAFFLINES, then calculate shift*/
			    if (STAFFLINES[findLine][0]){ /*if nonzero*/
				now_avg = (uint32_t)((STAFFLINES[findLine][0]+STAFFLINES[findLine][1]+1)/2);
				last_avg = last_STAFFLINES_avg[findLine];
				shift = now_avg - last_avg;
				break;
			    }
			}
			if (shift==100){ shift = 0;}
			/*replace any flagged (with 0) entries in STAFFLINES*/
			for(findLine=0;findLine<5;findLine++){
			    	if (STAFFLINES[findLine][0] == 0){
					STAFFLINES[findLine][0] = last_STAFFLINES[findLine][0]+shift;
					STAFFLINES[findLine][1] = last_STAFFLINES[findLine][1]+shift;
			    	}
			}
		}
	}

	extend = (uint32_t)((line_w+2)/4)+1;
	/*create stafflines above*/
	allLINES=create_linked_list();
	lineY = (int32_t)(((STAFFLINES[0][0] + STAFFLINES[0][1]+1)/2) - line_w); /*first above line*/
	while (1){
   		if (lineY < (int32_t)(extend + 2)){
       			break;
		}   		
		else{
			temp_array=malloc(sizeof(int16_t)*2);
			temp_array[0]=(int16_t)lineY;
			temp_array[1]=(int16_t)lineY;
			push_top(allLINES,temp_array);
       			lineY = (int32_t)(lineY - (int32_t)line_w );
   		}
	}
	for(i=0;i<5;i++){
		temp_array=malloc(sizeof(uint32_t)*2);
		temp_array[0]=(int16_t)STAFFLINES[i][0];
		temp_array[1]=(int16_t)STAFFLINES[i][1];
		push_bottom(allLINES,temp_array);
	}
	/*create stafflines below*/
	lineY = (uint32_t)(((STAFFLINES[4][0] + STAFFLINES[4][1]+1)/2) + line_w); /*first above line*/
	while (1){
   		if (lineY > (h - extend - 3)){
       			break;
		}   		
		else{
			temp_array=malloc(sizeof(int16_t)*2);
			temp_array[0]=(int16_t)lineY;
			temp_array[1]=(int16_t)lineY;
			push_bottom(allLINES,temp_array);
			lineY = (uint32_t)(lineY + (int32_t)line_w);
   		}
	}
	/*REMOVE STAFF LINES*/

	while( is_list_empty(allLINES)==0){
		tempData = (int16_t*)pop_top(allLINES);
		lineBegin = tempData[0];
		lineEnd = tempData[1];
		middle = (lineBegin + lineEnd + 1)/2;
		lastDelete = 0;
		free(tempData);
    
    
    		for(j=0;j<w;j++){
        
        		/*top of staff line*/
        		topStop = 0;
/*CHANGED*/
        		for(ii = (lineBegin-1); ii>=(lineBegin-extend); ii--){
/*END CHANGED*/
            			if (ii < 0){
                			break;
            			}
            			if (getPixel(img,ii,j)==0){ /*then erase*/
                			topStop = ii+1;
                			break;
            			}
        		}
    
			/*bottom of staff line*/
			botStop = h-1;
/*CHANGED*/
			for(ii = lineEnd+1; ii<=(lineEnd+extend); ii++){
/*END CHANGED*/
	     	      	 	if (ii > h-1){
	      	          		break;
		    		}
		    		if(getPixel(img,ii,j)==0){
		        		botStop = ii-1;              
		        		break;
		    		}
			}
	    
		
			/*check thickness of line, delete if skinny*/
	       	 	thickness = botStop - topStop + 1;
	       	 	if (parameters->thickness < 3){
		    		paramThickness = parameters->thickness + 1;
			}
			else{
		    		paramThickness = parameters->thickness;
			}
			if (lastDelete){ /*there was a line deletion last iteration*/
		    		thickness_th = paramThickness*2; /*higher threshold*/
			}
			else{
		    		thickness_th = paramThickness*2-2;
			}
			if (thickness <= thickness_th){
				for(ii=topStop; ii<=botStop; ii++){ 
		    			setPixel(img,ii,j,0);
				}
		    		lastDelete = 1;
			}
			else{
		    		lastDelete = 0;
			}
    
    		}
        
	} /*end staff line*/
	topLine = STAFFLINES[0][0];
	if(*previous_start){
    		if(*previous_start<topLine){
        		shift=topLine-(*previous_start);
/*CHANGED H-SHIFT-1 TO H-SHIFT*/
			for(shift_loop=0; shift_loop<(h-shift); shift_loop++){
/*END CHANGED*/
				for(cW=0; cW<w; cW++){
					pixelData=getPixel(img,shift_loop+shift,cW);
		    			setPixel(img,shift_loop,cW,pixelData);
				}
			}
			for(cH=h-shift-1; cH<h; cH++){
				for(cW=0; cW<w; cW++){
		    			setPixel(img,cH,cW,0);
				}
			}      	
		}
    		else if(*previous_start>topLine){
        		shift=*previous_start-topLine;
		
			for(shift_loop=h-1; shift_loop>=shift; shift_loop--){
				for(cW=0; cW<w; cW++){
					pixelData=getPixel(img,shift_loop-shift,cW);
		    			setPixel(img,shift_loop,cW, pixelData);
				}
			}
/*CHANGED: SHIFT-1 TO SHIFT*/
			for(cH=0; cH<shift; cH++){
/*END CHANGED*/
				for(cW=0; cW<w; cW++){
		    			setPixel(img,cH,cW,0);
				}
			}
		}	
	}
	else{
	    	*previous_start=topLine;
	}

	for(i=0; i<5;i++){
		last_STAFFLINES[i][0] = STAFFLINES[i][0];
		last_STAFFLINES[i][1] = STAFFLINES[i][1];
	}
	delete_list(staffLines);
	delete_list(allLINES);
	multifree(STAFFLINES,2);
}
Ejemplo n.º 23
0
linked_list* find_top_bottom(const image_t* img,uint16_t height_min,uint16_t leftCutoff,linked_list* groupings){
    /* finds all vertical lines (stems, measure markers)
     Returns:
    groupings - indices within goodLines array of start and end of each vertical line
       ex: [40 42
            55 58
            100 110]
    
     goodLines - array of structs for each vertical found
       .top
       .bottom
       .left
       .right*/
    
    /*Var Declarations*/
    uint16_t height,width,col,row,test_length,i,real_length;
    linked_list *goodlines_first;/*since unsure of size, trim down later*/
    flex_array_t *starts;
    uint8_t inLine,shift,step;
    uint16_t cursor1,cursor2;
    good_lines_t *lines;
    /*End Var Declarations*/
    
    height=img->height;
    width=img->width;

    goodlines_first=create_linked_list();                                   
   
    real_length=0;
    for (col=0;col<width;col++){
        test_length=0;
        i=0;
        for (row=1;row<height;row++){
            if(!getPixel(img,row-1,col) && getPixel(img,row,col)) test_length++;
        }
        starts=make_flex_array(test_length);
        for (row=1;row<height;row++){
            if(!getPixel(img,row-1,col) && getPixel(img,row,col)) starts->data[i++]=row;;
        }
        
        for (i=0;i<test_length;i++) {/*start = starts;*/
            inLine = 1;
            cursor1=starts->data[i];
            cursor2=col;
            shift = 0;
            
            while(inLine){
                step = 0;
                if ( getPixel(img,cursor1+1,cursor2)==1 ){ /*right below*/
                    cursor1++;
                    step = 1;
                }
    
                if ( cursor2+1 < width && !step ){ /*to the bottom right*/
                    if (getPixel(img,cursor1+1,cursor2+1) ){
                        cursor1++;
                        cursor2++;
                        step = 1;
                        shift++;
                    }
                }
                    
                if ( cursor2-1 >= 0 && !step){ /*to the bottom left*/
                    if (getPixel(img,cursor1+1,cursor2-1) ){
                        cursor1++;
                        cursor2--;
                        step = 1;
                        shift ++;
                    }
                }
                    
                if (!step || shift > 3){ /*can't continue black run*/
                   if (cursor1-(starts->data[i])>=height_min){
                        real_length++;
                        lines=(good_lines_t *)malloc(sizeof(good_lines_t));
                        lines->bottom=cursor1;
                        lines->index=col;
                        lines->left= col<cursor2 ? col : cursor2;
                        lines->top=starts->data[i];
                        lines->right=col>cursor2 ? col: cursor2;
                        lines->left +=leftCutoff-1;
                        lines->right+= leftCutoff-1;
                        push_bottom(goodlines_first,lines);
                   }
                   inLine = 0;
                }
                
            } /*end while in line*/
        
        } /*end for thru each starting location*/
        delete_flex_array(starts);   
        
    } /*end for thru each column*/
    starts=make_flex_array(real_length);
    for (i=0;i<real_length;i++){
        starts->data[i]=((good_lines_t*)getIndexData(goodlines_first,i))->left;
    }
    /*GROUP LINES TOGETHER*/
    fill_group_indices(groupings,starts,5); /*2nd arg chosen to group close lines together*/
    
    delete_flex_array(starts);
    return goodlines_first;

}