Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.00 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5.  
  6. typedef struct {
  7.     char* contents;
  8.     int length;
  9. } Buffer;
  10.  
  11. Buffer* readFile(const char* filename) {
  12.     FILE *fileptr;
  13.     char *buffer;
  14.     long filelen;
  15.    
  16.     fileptr = fopen(filename, "rb");        // Open the file in binary mode
  17.     fseek(fileptr, 0, SEEK_END);            // Jump to the end of the file
  18.     filelen = ftell(fileptr);               // Get the current byte offset in the file
  19.     rewind(fileptr);                        // Jump back to the beginning of the file
  20.    
  21.     buffer = (char *)malloc((filelen)*sizeof(char)); // Enough memory for file
  22.     fread(buffer, filelen, 1, fileptr);     // Read in the entire file
  23.     fclose(fileptr);                        // Close the file
  24.  
  25.     Buffer *ret = malloc(sizeof(Buffer));
  26.     ret->length = filelen;
  27.     ret->contents = buffer;
  28.     return ret;
  29. }
  30.  
  31. const int DESC_BITS = 512;
  32. const int DESC_BYTES = DESC_BITS / 8;
  33. static int DESC_INTS = DESC_BYTES / sizeof(unsigned);  // 512 bits: as integers, so we can use popcnt
  34. const int THRESH = 50;
  35.  
  36. int descriptorDistance(unsigned* a, unsigned* b) {
  37.     int sumBits = 0;
  38.     unsigned compare = 0;
  39.     int i;
  40.     for (i=0; i<DESC_INTS; i++) {
  41.         compare = a[i]^b[i];   
  42.         sumBits += __builtin_popcount(compare);
  43.     }  
  44.    
  45.     return sumBits;
  46. }
  47.  
  48. int main (int argc, char * argv[]) {
  49.    
  50.     Buffer *haystack = readFile("haystack");
  51.     Buffer *needleImage = readFile("needle_image_descriptors");
  52.  
  53.     uint64_t position = 0;
  54.     uint16_t *pNumImages = (uint16_t*)&haystack->contents[position];
  55.     uint16_t numImages = *pNumImages;
  56.     printf("images in haystack: %i\n\n", numImages);
  57.  
  58.     Buffer **allHaystack = (Buffer**)malloc(haystack->length*sizeof(Buffer*));
  59.  
  60.     position += 2;
  61.     int i;
  62.     for(i=0; i<numImages; i++)  {
  63.         uint16_t *pNumDescriptors = (uint16_t*)&haystack->contents[position];
  64.         uint16_t numDescriptors = *pNumDescriptors;
  65.         position += 2;
  66.  
  67.         Buffer *descriptors = (Buffer*)malloc(numDescriptors * sizeof(Buffer));
  68.         descriptors->length = numDescriptors * DESC_BYTES;
  69.         descriptors->contents = (char *)malloc((descriptors->length)*sizeof(char));
  70.         memcpy(descriptors->contents, &haystack->contents[position], descriptors->length);
  71.  
  72.         allHaystack[i] = descriptors;
  73.  
  74.         position += numDescriptors * DESC_BYTES;
  75.     }
  76.  
  77.     printf("Starting comparison\n");
  78.  
  79.     int k,j;
  80.     unsigned distance = 0;
  81.     int matches = 0;
  82.     int step = DESC_INTS * sizeof(int);
  83.     Buffer *haystackImage;
  84.     unsigned* haystackDescriptor;
  85.  
  86.     for(k=0; k<numImages; k++) {
  87.            
  88.         haystackImage = allHaystack[k];
  89.         //printf("Comparing to image %i (%i descriptors)\n", k, haystackImage->length / DESC_BYTES);
  90.         matches = 0;
  91.  
  92.         for(i=0; i<haystackImage->length; i+=step)  {
  93.  
  94.             haystackDescriptor = (unsigned *)&haystackImage->contents[i];
  95.  
  96.             for(j=0; j<needleImage->length; j+=step) {
  97.                 distance = descriptorDistance(haystackDescriptor, (unsigned *)&needleImage->contents[j]);
  98.                 if (distance < THRESH) {
  99.                     matches++;
  100.                 }
  101.             }
  102.         }
  103.  
  104.         if (matches > 3) {
  105.             printf("Image match found: %i (%i matches)\n", k, matches);
  106.         }
  107.     }
  108.  
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement