Guest User

Untitled

a guest
May 26th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define HEADERSIZE 18
  5.  
  6. #define W_LSB 12
  7. #define W_MSB 13
  8. #define H_LSB 14
  9. #define H_MSB 15
  10.  
  11. //#define HASHSIZE 262147
  12. #define N 300000
  13.  
  14. int hash(int key), primecalc(int m, int n);
  15.  
  16. int main(int argc, char** argv, int startnum)
  17. {
  18.  
  19. int m, n;
  20. FILE *fp;
  21. unsigned char header[HEADERSIZE];
  22.  
  23. FILE *greyscalefp;
  24. unsigned char greyscaleheader[HEADERSIZE];
  25.  
  26. //system("cls");
  27.  
  28. printf("Opening Input Image '%s' ... ", argv[1]);fflush(stdout);
  29. fp = fopen(argv[1],"rb");
  30. if(!fp)
  31. {
  32. printf("\n\nARGH! File couldn't be opened.\n");fflush(stdout);
  33. }
  34. printf("Done.");fflush(stdout);
  35.  
  36.  
  37. printf("\n\nReading %d Byte Header ... ", HEADERSIZE);fflush(stdout);
  38. fread(header, HEADERSIZE, 1, fp);
  39. printf("Done.");fflush(stdout);
  40.  
  41. printf("\n\nHeader Contents: ");fflush(stdout);
  42. for(int i=1;i<HEADERSIZE;i++)
  43. {
  44. printf("|%d|", header[i]);fflush(stdout);
  45. }
  46.  
  47. m = (header[W_MSB]*256) + header[W_LSB];
  48. n = (header[H_MSB]*256) + header[H_LSB];
  49.  
  50. printf("\n\nDimensions Extracted: Width=%d, Height=%d.", m, n);
  51. //return (m, n);
  52. fflush(stdout);
  53.  
  54. //Run Primecalc with those numbers
  55. primecalc(m, n);
  56.  
  57. unsigned char Rt, Gt, Bt;
  58. unsigned char I[m][n];
  59.  
  60. printf("\n\nReading RGB Data & Calculating Greyscale ... ");fflush(stdout);
  61. for(int i=0;i<m;i++)
  62. for(int j=0;j<n;j++)
  63. {
  64. fscanf(fp, "%c", &Rt);
  65. fscanf(fp, "%c", &Gt);
  66. fscanf(fp, "%c", &Bt);
  67.  
  68. I[i][j] = (Rt+Gt+Bt)/3;
  69. }
  70. fclose(fp);
  71. printf("Done.");fflush(stdout);
  72.  
  73. printf("\n\nOpening Output File '%s' ... ", argv[2]);fflush(stdout);
  74. fp = fopen(argv[2],"wb");
  75. printf("Done.");fflush(stdout);
  76.  
  77. printf("\n\nWriting %d Byte Header ... ", HEADERSIZE);fflush(stdout);
  78. fwrite(header, 1, HEADERSIZE, fp);
  79. printf("Done.");fflush(stdout);
  80.  
  81. printf("\n\nWriting Converted RGB Data ... ");fflush(stdout);
  82. for(int i=0;i<m;i++)
  83. for(int j=0;j<n;j++)
  84. {
  85. fprintf(fp, "%c", I[i][j]);
  86. fprintf(fp, "%c", I[i][j]);
  87. fprintf(fp, "%c", I[i][j]);
  88. }
  89. printf("Done.\n");fflush(stdout);
  90. fclose(fp);
  91.  
  92. //---------------------Open Grescale Image---------------------//
  93.  
  94.  
  95.  
  96. //system("cls");
  97.  
  98. printf("Opening Input Greyscal Image '%s' ... ", argv[2]);fflush(stdout);
  99. greyscalefp = fopen(argv[2],"rb");
  100. if(!greyscalefp)
  101. {
  102. printf("\n\nARGH! File couldn't be opened.\n");fflush(stdout);
  103. }
  104. printf("Done.");fflush(stdout);
  105.  
  106.  
  107.  
  108.  
  109.  
  110. //---------------------Hash Table Stuff---------------------//
  111. printf("\n\nPutting data in Hash Table ... ");fflush(stdout);
  112. int T[startnum];
  113.  
  114. for (int i=0; i<N && !feof(fp); i++)
  115. {
  116. int v;
  117. fscanf(fp, "%c", &I);
  118.  
  119. /*
  120. for(int j=0;j<n;j++)
  121. {
  122. fscanf(fp, "%c", &Rt);
  123. fscanf(fp, "%c", &Gt);
  124. fscanf(fp, "%c", &Bt);
  125.  
  126. I[i][j] = (Rt+Gt+Bt)/3;
  127. }*/
  128.  
  129.  
  130.  
  131. int k = hash(v);
  132. while (true)
  133. {
  134. if (k >= startnum)
  135. {
  136. printf("\n Value failed to be inserted");fflush(stdout);
  137. break;
  138. }
  139.  
  140. if (T[k] == '\0')
  141. {
  142. T[k] = v;
  143. break;
  144. }
  145.  
  146. k++;
  147. }
  148.  
  149. fflush(stdout);
  150. }
  151. printf("Done.");fflush(stdout);
  152. //---------------------End Hash Table Stuff---------------------//
  153.  
  154.  
  155.  
  156. fclose(fp);
  157. }
  158.  
  159. int hash(int key, int startnum)
  160. {
  161. return key % startnum;
  162. }
  163.  
  164. int primecalc(int m, int n)
  165. {
  166. int startnum = 0, calcnum = 0, counter = 0;
  167. bool is_prime = false;
  168.  
  169. startnum= (m*n);//512*512;
  170.  
  171. while (counter < 1)
  172. {
  173. is_prime = true;
  174.  
  175. // all numbers are divided by 1 so start from 2
  176. for (calcnum = 2; calcnum <= startnum/2; calcnum++)
  177. {
  178.  
  179. if (startnum % calcnum == 0)
  180. {
  181. is_prime = false;
  182. }
  183.  
  184. }
  185.  
  186. // only when the "for" cycle is done we know for sure if "i" is prime
  187. if (is_prime)
  188. {
  189. printf("%d\n", startnum);
  190. counter++;
  191. return(startnum);
  192. }
  193.  
  194. startnum++;
  195. }
  196. system("pause");
  197. }
Add Comment
Please, Sign In to add comment