Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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 //first prime past 204
- //#define N 262144 //512*512 (Max possible collisions)
- //#define N has been replaced by int total and passed to hashtable instead
- int T[HASHSIZE];
- struct node
- {
- int key;
- struct node *next;
- };
- node *ListStart = NULL;
- int storage_attempts = 0, retrieval_attempts = 0;
- int openTGA(char *arg1);
- void loadTGA(int total, unsigned char *I, char *arg1);
- void hashtable(int total, unsigned char *I);
- int hash(int key);
- void addToOverflow(int key);
- int findData(int key);
- void printTable();
- void printOverflow();
- //int primecalc(int m, int n);
- 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(total, I);
- }
- //---------------------openTGA---------------------//
- 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);
- }
- //---------------------Open loadTGA---------------------//
- 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);
- }
- //---------------------Open hashtable---------------------//
- void hashtable(int total, unsigned char *I)
- {
- int table_count = 0, overflow_count = 0, x=0, k = 0;
- for(x=0; x<total; 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 and overflow
- printf("\nHASH TABLE\n\n");fflush(stdout);
- printTable();
- printf("\n\nOVERFLOW\n\n");fflush(stdout);
- printOverflow();
- }
- //---------------------Open hash---------------------//
- int hash(int key)
- {
- int i;
- i = key % HASHSIZE;
- return i;
- }
- //---------------------Open addToOverflow---------------------//
- void addToOverflow(int key)
- {
- node *temp = new node;
- temp->key = key;
- temp->next = NULL;
- if(ListStart == NULL)
- {
- ListStart = temp;
- }
- else
- {
- temp->next = ListStart;
- ListStart = temp;
- }
- }
- //---------------------Open findData---------------------//
- 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);
- }
- }
- //---------------------Open printTable---------------------//
- void printTable()
- {
- int temp = 0, counter = 0;
- for(int i=0; i<HASHSIZE; i++)
- {
- temp=T[i];
- printf("%7d", temp);fflush(stdout);
- if (temp!=0)
- {
- counter++;
- }
- if(i%10==0) printf("\n");fflush(stdout);
- }
- printf("\n\n=%d=\n\n", counter);fflush(stdout);
- }
- //---------------------Open printOverflow---------------------//
- void printOverflow()
- {
- int count = 1;
- for(node *current = ListStart; current != NULL; current = current->next)
- {
- printf("%7d", current->key);fflush(stdout);
- if(count%10==0) printf("\n");fflush(stdout);
- count++;
- }
- }
- /*int primecalc(int m, int n)
- {
- int startnum = 0, calcnum = 0, counter = 0, is_prime = 0;
- startnum = (m*n);
- while (counter < 1)
- {
- is_prime = 1;
- // all numbers are divided by 1 so start from 2
- for (calcnum = 2; calcnum <= startnum/2; calcnum++)
- {
- if (startnum % calcnum == 0)
- {
- is_prime = 0;
- }
- }
- // only when the "for" cycle is done we know for sure if "i" is prime
- if (is_prime)
- {
- printf("%d\n", startnum);
- counter++;
- return startnum;
- }
- startnum++;
- }
- }*/
Add Comment
Please, Sign In to add comment