C Program Image Processing using Quadtrees

How to write a C Program to Image Processing Using Quadtrees in C Programming Language ?


Program is written by Alan Blair.

Solution:


/*Image Processing using Quadtrees
 * Written by: Alan Blair
 */

// System include files
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

// Local include files
#include "quadtree.h"

// Constants
#define MAXLENGTH    200

Lnode * makenode( struct country);
Lnode * push(Lnode *new_node, Lnode *head);

typedef struct country Country	{
			int num;
			char *filename;
			int dim;
			struct QTnode *image;
			struct country *prev;
			struct country *next;
		}

// Main function
int main( void )
{
  char command[MAXLENGTH];
  char c;
  char s[MAXLENGTH];
  int i = 1;
  int k = 1;
  int d = 0;
  QTnode* image;
  int dim;
  int undo = 0;

		

	makenode(country);

 /********************************************************************
  * YOU WILL NEED TO COMPLETE THE FOLLOWING SECTION FOR STAGES 2 - 5 *
  *******************************************************************/

  printPrompt();

  while( fgets( command, MAXLENGTH, stdin ) != NULL ) {

    int  imgNum;
    char *p;

    if(( p=strrchr( command, '\n')) != NULL ) {
      *p = '\0'; // remove '\n' at end of line
    }
    // find the first non-space character in the command
    p = command;
    while(isspace(*p)) {
      p++;
    }
    c = tolower(*p);

    if( isdigit(c)) {
      if( sscanf( command, "%d", &imgNum ) == 1 ) {

        // INSERT CODE FOR <k> COMMAND
      }
    }
    else switch( c ) {

    case 'h': // help

      printf(" A - Add image\n" );
      printf(" I - Index\n" );
      printf(" P - Print image\n" );
      printf(" F - Forward\n" );
      printf(" B - Back\n" );
      printf("<k>- make image number k the current image\n");
      printf(" D - Delete image\n" );
      printf(" L - Look for image\n" );
      printf(" R - Rotate image counterclockwise\n" );
      printf(" M - Mirror image (reflect vertically)\n" );
      printf("NE - zoom into North East corner\n" );
      printf("NW - zoom into North West corner\n" );
      printf("SW - zoom into South West corner\n" );
      printf("SE - zoom into South East corner\n" );
      printf(" O - zoom Out\n" );
      printf(" U - Undo\n" );
      printf(" H - Help\n" );
      printf(" Q - Quit\n" );
      break;

      // INSERT CODE FOR OTHER COMMANDS

	case 'a': //Add Image
		int undo = 1;
		
		while (isAlpha(p) != TRUE)	{	
		p++
		}
 
		image = getimage(p, dim );
		
			list = push(makeNode('A'), list);
		
		}

		
		break;
	
	case 'i': //Index
		while (country->prev != NULL)	{
			country = country->prev;
			i++;
		}

		while(country->next != NULL)	{
			printf("  %d [%d] %s\n", i, dim, filename);
			country = country->next;
			i--;
			if (country->next = NULL)	{
				printf("*  %d [%d] %s\n", i, dim, filename);
			}
		}
		break;
 

	case 'p'://Print Image
		printImage(qt, &dim );
		break;

	case 'f': //Forward image
	int undo = 2;
	forward(node)
	break;

	case 'b': //Back image
	int undo = 3;
	country = country->prev;
	break;

	case 'k': //Make k current image
	int undo = 4;
	break;

	case 'd': //Delete Image
	int undo = 5;
	
	
	Lnode *tmp = country;
	
	country->prev->next = country->next;
	country->next->prev = country->prev;
	free(tmp);

	break;

	case 'NE':
	int undo = 6;	
	NE(image);
	int undo = 
	break;

	case 'NW':
	int undo = 6;
	NW(image);
	break;

	case 'SE':
	int undo = 6;
	SE(image);
	break;
	
	case 'SW':
	int undo = 6;
	SW(image);
	break;

	case 'u': //undo
	if (undo == 1)	{
		deleteLnode(node);
	}

	if (undo == 2)	{
	back(node);
	}
	
	if (undo == 3)	{
	forward(node);
	}

	if (undo == 4)	{
	}

	if (undo == 5)	{
	}
	
	if (undo == 6)	{
		zoomOut(image);
	}
	
	

    case 'q': // quit program
      printf("Bye!\n");
      return 0;
      break;

    default:
      printf("Unrecognized command: %s\n", command );
      break;
    }

    printPrompt();
  }

  return 0;
}

	
Lnode * makenode( struct country)	{
	Lnode *node = (Lnode *)malloc(sizeof(Lnode));
	node->num = i;
	node->dim = NULL;
	node->filename = NULL;
	node->qtnode = NULL;
	node->prev = NULL;
	node->next = NULL;
	return(node);
}

Lnode * push(Lnode *new_node, Lnode *head, struct country)	{
	new_node = country;
	node->num = i;
	new_node->next = head;
	return(new_node);
}

Lnode * forward(Lnode *node)	{
	node = node->prev;
	return(node);
}

Lnode * back(Lnode *node)	{
	node = node->next;
	return(node);
}

Lnode * knode(Lnode *node, int k, int i)	{
	Lnode *node = head;
	while( node != NULL && node->i != k)	{
		node = node->next;
	}

	if(node = NULL)	{
		printf("Invalid number entered");
		node = head;	
	}
}

Lnode * deleteLnode(Lnode *node)	{
	Lnode *tmp = country;
	
	country->prev->next = country->next;
	country->next->prev = country->prev;
	free(tmp);

}

Lnode * lookImage(*node, *p, *filename)	{
while(node != NULL)	{
	while(filename !=NULL)		{
		filename++;
		if (p == filaname)	{
		return(node);
		}
}

node = node->next
}
if (node == NULL)	{
	printf("No match with the search %s\n", p);
node = head;
return(node);
}
return(node);
}


QTnode * rotate(QTnode *image)	{
	while()
}

QTnode * NE(QTnode *image)	{
	image = image->NE;
	return(*image);
}

QTnode * NW(QTnode *image)	{
	image = image->NW;
	return(*image);
}

QTnode * SE(QTnode *image)	{
	image = image->SE;
	return(*image);
}

QTnode * SW(QTnode *image)	{
	image = image->SW;
	return(*image);
}

QTnode * zoomOut(QTnode *image)	{
	image = image->out;
	return(image);
}/*
 * COMP1917: Computing 1
 * Assignment 2: Image Processing using Quadtrees
 * File: hw2.c
 * Written by: Alan Blair
 */

// System include files
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

// Local include files
#include "quadtree.h"

// Constants
#define MAXLENGTH    200

Lnode * makenode( struct country);
Lnode * push(Lnode *new_node, Lnode *head);

typedef struct country Country	{
			int num;
			char *filename;
			int dim;
			struct QTnode *image;
			struct country *prev;
			struct country *next;
		}

// Main function
int main( void )
{
  char command[MAXLENGTH];
  char c;
  char s[MAXLENGTH];
  int i = 1;
  int k = 1;
  int d = 0;
  QTnode* image;
  int dim;
  int undo = 0;

		

	makenode(country);

 /********************************************************************
  * YOU WILL NEED TO COMPLETE THE FOLLOWING SECTION FOR STAGES 2 - 5 *
  *******************************************************************/

  printPrompt();

  while( fgets( command, MAXLENGTH, stdin ) != NULL ) {

    int  imgNum;
    char *p;

    if(( p=strrchr( command, '\n')) != NULL ) {
      *p = '\0'; // remove '\n' at end of line
    }
    // find the first non-space character in the command
    p = command;
    while(isspace(*p)) {
      p++;
    }
    c = tolower(*p);

    if( isdigit(c)) {
      if( sscanf( command, "%d", &imgNum ) == 1 ) {

        // INSERT CODE FOR <k> COMMAND
      }
    }
    else switch( c ) {

    case 'h': // help

      printf(" A - Add image\n" );
      printf(" I - Index\n" );
      printf(" P - Print image\n" );
      printf(" F - Forward\n" );
      printf(" B - Back\n" );
      printf("<k>- make image number k the current image\n");
      printf(" D - Delete image\n" );
      printf(" L - Look for image\n" );
      printf(" R - Rotate image counterclockwise\n" );
      printf(" M - Mirror image (reflect vertically)\n" );
      printf("NE - zoom into North East corner\n" );
      printf("NW - zoom into North West corner\n" );
      printf("SW - zoom into South West corner\n" );
      printf("SE - zoom into South East corner\n" );
      printf(" O - zoom Out\n" );
      printf(" U - Undo\n" );
      printf(" H - Help\n" );
      printf(" Q - Quit\n" );
      break;

      // INSERT CODE FOR OTHER COMMANDS

	case 'a': //Add Image
		int undo = 1;
		
		while (isAlpha(p) != TRUE)	{	
		p++
		}
 
		image = getimage(p, dim );
		
			list = push(makeNode('A'), list);
		
		}

		
		break;
	
	case 'i': //Index
		while (country->prev != NULL)	{
			country = country->prev;
			i++;
		}

		while(country->next != NULL)	{
			printf("  %d [%d] %s\n", i, dim, filename);
			country = country->next;
			i--;
			if (country->next = NULL)	{
				printf("*  %d [%d] %s\n", i, dim, filename);
			}
		}
		break;
 

	case 'p'://Print Image
		printImage(qt, &dim );
		break;

	case 'f': //Forward image
	int undo = 2;
	forward(node)
	break;

	case 'b': //Back image
	int undo = 3;
	country = country->prev;
	break;

	case 'k': //Make k current image
	int undo = 4;
	break;

	case 'd': //Delete Image
	int undo = 5;
	
	
	Lnode *tmp = country;
	
	country->prev->next = country->next;
	country->next->prev = country->prev;
	free(tmp);

	break;

	case 'NE':
	int undo = 6;	
	NE(image);
	int undo = 
	break;

	case 'NW':
	int undo = 6;
	NW(image);
	break;

	case 'SE':
	int undo = 6;
	SE(image);
	break;
	
	case 'SW':
	int undo = 6;
	SW(image);
	break;

	case 'u': //undo
	if (undo == 1)	{
		deleteLnode(node);
	}

	if (undo == 2)	{
	back(node);
	}
	
	if (undo == 3)	{
	forward(node);
	}

	if (undo == 4)	{
	}

	if (undo == 5)	{
	}
	
	if (undo == 6)	{
		zoomOut(image);
	}
	
	

    case 'q': // quit program
      printf("Bye!\n");
      return 0;
      break;

    default:
      printf("Unrecognized command: %s\n", command );
      break;
    }

    printPrompt();
  }

  return 0;
}

	
Lnode * makenode( struct country)	{
	Lnode *node = (Lnode *)malloc(sizeof(Lnode));
	node->num = i;
	node->dim = NULL;
	node->filename = NULL;
	node->qtnode = NULL;
	node->prev = NULL;
	node->next = NULL;
	return(node);
}

Lnode * push(Lnode *new_node, Lnode *head, struct country)	{
	new_node = country;
	node->num = i;
	new_node->next = head;
	return(new_node);
}

Lnode * forward(Lnode *node)	{
	node = node->prev;
	return(node);
}

Lnode * back(Lnode *node)	{
	node = node->next;
	return(node);
}

Lnode * knode(Lnode *node, int k, int i)	{
	Lnode *node = head;
	while( node != NULL && node->i != k)	{
		node = node->next;
	}

	if(node = NULL)	{
		printf("Invalid number entered");
		node = head;	
	}
}

Lnode * deleteLnode(Lnode *node)	{
	Lnode *tmp = country;
	
	country->prev->next = country->next;
	country->next->prev = country->prev;
	free(tmp);

}

Lnode * lookImage(*node, *p, *filename)	{
while(node != NULL)	{
	while(filename !=NULL)		{
		filename++;
		if (p == filaname)	{
		return(node);
		}
}

node = node->next
}
if (node == NULL)	{
	printf("No match with the search %s\n", p);
node = head;
return(node);
}
return(node);
}


QTnode * rotate(QTnode *image)	{
	while()
}

QTnode * NE(QTnode *image)	{
	image = image->NE;
	return(*image);
}

QTnode * NW(QTnode *image)	{
	image = image->NW;
	return(*image);
}

QTnode * SE(QTnode *image)	{
	image = image->SE;
	return(*image);
}

QTnode * SW(QTnode *image)	{
	image = image->SW;
	return(*image);
}

QTnode * zoomOut(QTnode *image)	{
	image = image->out;
	return(image);
}


Learn More :