Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define HEADERSIZE 18
- #define W_LSB 12
- #define W_MSB 13
- #define H_LSB 14
- #define H_MSB 15
- //#define HASHSIZE 262147
- #define N 300000
- int hash(int key), primecalc(int m, int n);
- int main(int argc, char** argv, int startnum)
- {
- int m, n;
- FILE *fp;
- unsigned char header[HEADERSIZE];
- FILE *greyscalefp;
- unsigned char greyscaleheader[HEADERSIZE];
- //system("cls");
- printf("Opening Input Image '%s' ... ", argv[1]);fflush(stdout);
- fp = fopen(argv[1],"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(int i=1;i<HEADERSIZE;i++)
- {
- printf("|%d|", header[i]);fflush(stdout);
- }
- m = (header[W_MSB]*256) + header[W_LSB];
- n = (header[H_MSB]*256) + header[H_LSB];
- printf("\n\nDimensions Extracted: Width=%d, Height=%d.", m, n);
- //return (m, n);
- fflush(stdout);
- //Run Primecalc with those numbers
- primecalc(m, n);
- unsigned char Rt, Gt, Bt;
- unsigned char I[m][n];
- printf("\n\nReading RGB Data & Calculating Greyscale ... ");fflush(stdout);
- for(int i=0;i<m;i++)
- for(int j=0;j<n;j++)
- {
- fscanf(fp, "%c", &Rt);
- fscanf(fp, "%c", &Gt);
- fscanf(fp, "%c", &Bt);
- I[i][j] = (Rt+Gt+Bt)/3;
- }
- fclose(fp);
- printf("Done.");fflush(stdout);
- printf("\n\nOpening Output File '%s' ... ", argv[2]);fflush(stdout);
- fp = fopen(argv[2],"wb");
- printf("Done.");fflush(stdout);
- printf("\n\nWriting %d Byte Header ... ", HEADERSIZE);fflush(stdout);
- fwrite(header, 1, HEADERSIZE, fp);
- printf("Done.");fflush(stdout);
- printf("\n\nWriting Converted RGB Data ... ");fflush(stdout);
- for(int i=0;i<m;i++)
- for(int j=0;j<n;j++)
- {
- fprintf(fp, "%c", I[i][j]);
- fprintf(fp, "%c", I[i][j]);
- fprintf(fp, "%c", I[i][j]);
- }
- printf("Done.\n");fflush(stdout);
- fclose(fp);
- //---------------------Open Grescale Image---------------------//
- //system("cls");
- printf("Opening Input Greyscal Image '%s' ... ", argv[2]);fflush(stdout);
- greyscalefp = fopen(argv[2],"rb");
- if(!greyscalefp)
- {
- printf("\n\nARGH! File couldn't be opened.\n");fflush(stdout);
- }
- printf("Done.");fflush(stdout);
- //---------------------Hash Table Stuff---------------------//
- printf("\n\nPutting data in Hash Table ... ");fflush(stdout);
- int T[startnum];
- for (int i=0; i<N && !feof(fp); i++)
- {
- int v;
- fscanf(fp, "%c", &I);
- /*
- for(int j=0;j<n;j++)
- {
- fscanf(fp, "%c", &Rt);
- fscanf(fp, "%c", &Gt);
- fscanf(fp, "%c", &Bt);
- I[i][j] = (Rt+Gt+Bt)/3;
- }*/
- int k = hash(v);
- while (true)
- {
- if (k >= startnum)
- {
- printf("\n Value failed to be inserted");fflush(stdout);
- break;
- }
- if (T[k] == '\0')
- {
- T[k] = v;
- break;
- }
- k++;
- }
- fflush(stdout);
- }
- printf("Done.");fflush(stdout);
- //---------------------End Hash Table Stuff---------------------//
- fclose(fp);
- }
- int hash(int key, int startnum)
- {
- return key % startnum;
- }
- int primecalc(int m, int n)
- {
- int startnum = 0, calcnum = 0, counter = 0;
- bool is_prime = false;
- startnum= (m*n);//512*512;
- while (counter < 1)
- {
- is_prime = true;
- // all numbers are divided by 1 so start from 2
- for (calcnum = 2; calcnum <= startnum/2; calcnum++)
- {
- if (startnum % calcnum == 0)
- {
- is_prime = false;
- }
- }
- // 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++;
- }
- system("pause");
- }
Add Comment
Please, Sign In to add comment