Code:
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
#include <cstdio>
#define HEADERSIZE 18
#define W_LSB 12
#define W_MSB 13
#define H_LSB 14
#define H_MSB 15
#define HASHSIZE 211 //This is the first prime past 204
#define N 230
int T[HASHSIZE];
struct node
{
int key;
struct node *next;
};
node * ListStart = NULL;
int storage_attempts = 0, retrieval_attempts = 0;
int hash(unsigned char key, int startnum);
void hashtable(unsigned char *I);
int findData(int key);
int hash(int key);
void addToOverflow(int key);
void printTable();
void printOverflow();
int openTGA(char *arg1);
void loadTGA(int total, unsigned char *I, char *arg1);
int main(int argc, char** argv)
{
int total = 0;
total = openTGA(argv[1]); //return MxN
unsigned char I[total];
loadTGA(total, I, argv[1]);
hashtable(I);
//getchar();
}
//---------------------End Main---------------------//
int openTGA(char *arg1)
{
int i;
unsigned char header[HEADERSIZE];
FILE *fp;
printf("Opening Input Image '%s' ... ", arg1);fflush(stdout);
fp = fopen(arg1,"rb");
if(!fp)
{
printf("\n\nARGH! File couldn't be opened.\n");fflush(stdout);
}
printf("Done.");fflush(stdout);
printf("\n\nReading %d Byte Header ... ", HEADERSIZE);fflush(stdout);
fread(header, HEADERSIZE, 1, fp);
printf("Done.");fflush(stdout);
printf("\n\nHeader Contents: ");fflush(stdout);
for(i=1;i<HEADERSIZE;i++)
{
printf("|%d|", header[i]);fflush(stdout);
}
int m = (header[W_MSB]*256) + header[W_LSB];
int n = (header[H_MSB]*256) + header[H_LSB];
printf("\n\nDimensions Extracted: Width=%d, Height=%d.", m, n);
fflush(stdout);
fclose(fp);
return (m*n);
}
void loadTGA(int total, unsigned char *I, char *arg1)
{
int x = 0;
unsigned char header[HEADERSIZE];
unsigned char Rt, Gt, Bt;
FILE * fp;
fp = fopen(arg1,"rb");
if(!fp)
{
printf("\n\nARGH! File couldn't be opened.\n");fflush(stdout);
}
printf("Done.");fflush(stdout);
printf("\n\nReading %d Byte Header ... ", HEADERSIZE);fflush(stdout);
fread(header, HEADERSIZE, 1, fp);
printf("Done.");fflush(stdout);
printf("\n\nReading RGB Data & Calculating Greyscale ... ");fflush(stdout);
for(x=0;x<total;x++)
{
fscanf(fp, "%c", &Rt);
fscanf(fp, "%c", &Gt);
fscanf(fp, "%c", &Bt);
I[x] = (Rt+Gt+Bt)/3;
}
fclose(fp);
printf("Done.");fflush(stdout);
fclose(fp);
}
void hashtable(unsigned char *I)
{
int table_count = 0, overflow_count = 0, x=0, k = 0;
for(x=0; x<N; x++)
{
k = I[x];
if(T[hash(k)] == '\0')
{
// Empty Space, Insert New Item
T[hash(k)] = k;
}
else if(T[hash(k)] != k)
{
// Collision, add item to overflow
addToOverflow(k);
}
}
// Print hash table
printf("\nHASH TABLE\n\n");
printTable();
}
int hash(int key)
{
int i;
i = key % HASHSIZE;
return i;
}
void addToOverflow(int key)
{
node * temp;
temp = new node;
temp->key = key;
temp->next = NULL;
if(ListStart == NULL)
{
ListStart = temp;
}
else
{
temp->next = ListStart;
ListStart = temp;
}
}
int findData(int key)
{
retrieval_attempts = 1;
int i = key % HASHSIZE;
if(T[i] == key)
{
// Hash location is correct, return data
return T[i];
}
else
{
// Hash location is incorrect, search linked list
node * current;
current = ListStart;
do
{
if(current->key == key)
{
retrieval_attempts++;
return current->key;
}
else if(current->next != NULL)
{
retrieval_attempts++;
current = current->next;
}
else
{
retrieval_attempts++;
return -1;
}
} while(current != NULL);
}
}
void printTable()
{
int temp = 0, counter = 0;
for(int i=0; i<HASHSIZE; i++)
{
temp=T[i];
printf("%7d", temp);
if (temp!=0)
{
counter++;
}
if(i%10==0) printf("\n");
}
printf("\n\n=%d=\n\n", counter);
}