Guest User

Untitled

a guest
May 27th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.95 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <cstdlib>
  4. #include <cstdio>
  5.  
  6. #define HEADERSIZE 18
  7.  
  8. #define W_LSB 12
  9. #define W_MSB 13
  10. #define H_LSB 14
  11. #define H_MSB 15
  12.  
  13. #define HASHSIZE 211 //first prime past 204
  14. //#define N 262144 //512*512 (Max possible collisions)
  15. //#define N has been replaced by int total and passed to hashtable instead
  16.  
  17. int T[HASHSIZE];
  18.  
  19. struct node
  20. {
  21. int key;
  22. struct node *next;
  23. };
  24.  
  25. node *ListStart = NULL;
  26.  
  27. int storage_attempts = 0, retrieval_attempts = 0;
  28.  
  29. int openTGA(char *arg1);
  30. void loadTGA(int total, unsigned char *I, char *arg1);
  31. void hashtable(int total, unsigned char *I);
  32. int hash(int key);
  33. void addToOverflow(int key);
  34. int findData(int key);
  35. void printTable();
  36. void printOverflow();
  37. //int primecalc(int m, int n);
  38.  
  39. int main(int argc, char** argv)
  40. {
  41.  
  42. int total = 0;
  43.  
  44. total = openTGA(argv[1]); //return MxN
  45.  
  46. unsigned char I[total];
  47.  
  48. loadTGA(total, I, argv[1]);
  49.  
  50. hashtable(total, I);
  51.  
  52. }
  53.  
  54. //---------------------openTGA---------------------//
  55.  
  56. int openTGA(char *arg1)
  57. {
  58. int i;
  59. unsigned char header[HEADERSIZE];
  60. FILE *fp;
  61.  
  62. printf("Opening Input Image '%s' ... ", arg1);fflush(stdout);
  63. fp = fopen(arg1,"rb");
  64. if(!fp)
  65. {
  66. printf("\n\nARGH! File couldn't be opened.\n");fflush(stdout);
  67. }
  68. printf("Done.");fflush(stdout);
  69.  
  70.  
  71. printf("\n\nReading %d Byte Header ... ", HEADERSIZE);fflush(stdout);
  72. fread(header, HEADERSIZE, 1, fp);
  73. printf("Done.");fflush(stdout);
  74.  
  75.  
  76. printf("\n\nHeader Contents: ");fflush(stdout);
  77. for(i=1;i<HEADERSIZE;i++)
  78. {
  79. printf("|%d|", header[i]);fflush(stdout);
  80. }
  81.  
  82. int m = (header[W_MSB]*256) + header[W_LSB];
  83. int n = (header[H_MSB]*256) + header[H_LSB];
  84.  
  85. printf("\n\nDimensions Extracted: Width=%d, Height=%d.", m, n);
  86. fflush(stdout);
  87. fclose(fp);
  88.  
  89. return (m*n);
  90. }
  91.  
  92. //---------------------Open loadTGA---------------------//
  93.  
  94. void loadTGA(int total, unsigned char *I, char *arg1)
  95. {
  96.  
  97. int x = 0;
  98. unsigned char header[HEADERSIZE];
  99. unsigned char Rt, Gt, Bt;
  100.  
  101. FILE * fp;
  102.  
  103. fp = fopen(arg1,"rb");
  104. if(!fp)
  105. {
  106. printf("\n\nARGH! File couldn't be opened.\n");fflush(stdout);
  107. }
  108. printf("Done.");fflush(stdout);
  109.  
  110.  
  111. printf("\n\nReading %d Byte Header ... ", HEADERSIZE);fflush(stdout);
  112. fread(header, HEADERSIZE, 1, fp);
  113. printf("Done.");fflush(stdout);
  114.  
  115.  
  116. printf("\n\nReading RGB Data & Calculating Greyscale ... ");fflush(stdout);
  117. for(x=0;x<total;x++)
  118. {
  119. fscanf(fp, "%c", &Rt);
  120. fscanf(fp, "%c", &Gt);
  121. fscanf(fp, "%c", &Bt);
  122.  
  123. I[x] = (Rt+Gt+Bt)/3;
  124. }
  125. fclose(fp);
  126. printf("Done.");fflush(stdout);
  127.  
  128. }
  129.  
  130. //---------------------Open hashtable---------------------//
  131.  
  132. void hashtable(int total, unsigned char *I)
  133. {
  134. int table_count = 0, overflow_count = 0, x=0, k = 0;
  135.  
  136. for(x=0; x<total; x++)
  137. {
  138.  
  139. k = I[x];
  140.  
  141. if(T[hash(k)] == '\0')
  142. {
  143. // Empty Space, Insert New Item
  144. T[hash(k)] = k;
  145. }
  146. else if(T[hash(k)] != k)
  147. {
  148. // Collision, add item to overflow
  149. addToOverflow(k);
  150. }
  151. }
  152.  
  153. // Print hash table and overflow
  154. printf("\nHASH TABLE\n\n");fflush(stdout);
  155. printTable();
  156. printf("\n\nOVERFLOW\n\n");fflush(stdout);
  157. printOverflow();
  158.  
  159. }
  160.  
  161. //---------------------Open hash---------------------//
  162.  
  163. int hash(int key)
  164. {
  165. int i;
  166.  
  167. i = key % HASHSIZE;
  168.  
  169. return i;
  170. }
  171.  
  172. //---------------------Open addToOverflow---------------------//
  173.  
  174. void addToOverflow(int key)
  175. {
  176. node *temp = new node;
  177.  
  178. temp->key = key;
  179. temp->next = NULL;
  180.  
  181. if(ListStart == NULL)
  182. {
  183. ListStart = temp;
  184. }
  185. else
  186. {
  187. temp->next = ListStart;
  188.  
  189. ListStart = temp;
  190. }
  191. }
  192.  
  193. //---------------------Open findData---------------------//
  194.  
  195. int findData(int key)
  196. {
  197. retrieval_attempts = 1;
  198.  
  199. int i = key % HASHSIZE;
  200.  
  201. if(T[i] == key)
  202. {
  203. // Hash location is correct, return data
  204. return T[i];
  205. }
  206. else
  207. {
  208. // Hash location is incorrect, search linked list
  209. node * current;
  210.  
  211. current = ListStart;
  212.  
  213. do
  214. {
  215. if(current->key == key)
  216. {
  217. retrieval_attempts++;
  218.  
  219. return current->key;
  220. }
  221. else if(current->next != NULL)
  222. {
  223. retrieval_attempts++;
  224.  
  225. current = current->next;
  226. }
  227. else
  228. {
  229. retrieval_attempts++;
  230.  
  231. return -1;
  232. }
  233. } while(current != NULL);
  234. }
  235. }
  236.  
  237. //---------------------Open printTable---------------------//
  238.  
  239. void printTable()
  240. {
  241. int temp = 0, counter = 0;
  242. for(int i=0; i<HASHSIZE; i++)
  243. {
  244. temp=T[i];
  245. printf("%7d", temp);fflush(stdout);
  246. if (temp!=0)
  247. {
  248. counter++;
  249. }
  250.  
  251. if(i%10==0) printf("\n");fflush(stdout);
  252. }
  253. printf("\n\n=%d=\n\n", counter);fflush(stdout);
  254. }
  255.  
  256. //---------------------Open printOverflow---------------------//
  257.  
  258. void printOverflow()
  259. {
  260. int count = 1;
  261.  
  262. for(node *current = ListStart; current != NULL; current = current->next)
  263. {
  264. printf("%7d", current->key);fflush(stdout);
  265.  
  266. if(count%10==0) printf("\n");fflush(stdout);
  267.  
  268. count++;
  269. }
  270. }
  271.  
  272. /*int primecalc(int m, int n)
  273. {
  274. int startnum = 0, calcnum = 0, counter = 0, is_prime = 0;
  275.  
  276. startnum = (m*n);
  277.  
  278. while (counter < 1)
  279. {
  280. is_prime = 1;
  281.  
  282. // all numbers are divided by 1 so start from 2
  283. for (calcnum = 2; calcnum <= startnum/2; calcnum++)
  284. {
  285.  
  286. if (startnum % calcnum == 0)
  287. {
  288. is_prime = 0;
  289. }
  290.  
  291. }
  292.  
  293. // only when the "for" cycle is done we know for sure if "i" is prime
  294. if (is_prime)
  295. {
  296. printf("%d\n", startnum);
  297. counter++;
  298. return startnum;
  299. }
  300.  
  301. startnum++;
  302. }
  303. }*/
Add Comment
Please, Sign In to add comment