-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_retrieval.c
195 lines (170 loc) · 5.02 KB
/
image_retrieval.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
Author: Bassel Ashi
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <float.h>
#include "worker.h"
int main(int argc, char **argv)
{
char ch;
char path[PATHLENGTH];
char pathCount[PATHLENGTH];
char *startdir = ".";
char *image_file = NULL;
while ((ch = getopt(argc, argv, "d:")) != -1)
{
switch (ch)
{
case 'd':
startdir = optarg;
break;
default:
fprintf(stderr, "Usage: queryone [-d DIRECTORY_NAME] FILE_NAME\n");
exit(1);
}
}
if (optind != argc - 1)
{
fprintf(stderr, "Usage: queryone [-d DIRECTORY_NAME] FILE_NAME\n");
exit(1);
}
else
image_file = argv[optind];
// Counting existing sub-directories
//-----------------------------------------------------------------------------------------
// Open the directory provided by the user (or current working directory)
DIR *dirp;
if ((dirp = opendir(startdir)) == NULL)
{
perror("opendir");
exit(1);
}
DIR *dirpCount;
if ((dirpCount = opendir(startdir)) == NULL)
{
perror("opendir");
exit(1);
}
/* For each entry in the directory, eliminate . and .., and check
* to make sure that the entry is a directory, then call run_worker
* to process the image files contained in the directory.
*/
struct dirent *dpCount;
int count = 0;
while ((dpCount = readdir(dirpCount)) != NULL)
{
if (strcmp(dpCount->d_name, ".") == 0 ||
strcmp(dpCount->d_name, "..") == 0 ||
strcmp(dpCount->d_name, ".svn") == 0)
{
continue;
}
strncpy(pathCount, startdir, PATHLENGTH);
strncat(pathCount, "/", PATHLENGTH - strlen(pathCount) - 1);
strncat(pathCount, dpCount->d_name, PATHLENGTH - strlen(pathCount) - 1);
struct stat sbufCount;
if (stat(pathCount, &sbufCount) == -1)
{
//This should only fail if we got the path wrong
// or we don't have permissions on this entry.
perror("stat");
exit(1);
}
// Only call process_dir if it is a directory
// Otherwise ignore it.
if (S_ISDIR(sbufCount.st_mode))
{
count++;
}
}
//-----------------------------------------------------------------------------------------
Image *img = read_image(image_file); // Read given image
// Invalid image
if (img == NULL)
{
fprintf(stderr, "Invalid image input!\n");
exit(1);
}
struct dirent *dp;
CompRecord CRec;
strcpy(CRec.filename, "");
CRec.distance = FLT_MAX;
int fd[2];
int fds[count];
int i = 0;
while ((dp = readdir(dirp)) != NULL)
{
if (strcmp(dp->d_name, ".") == 0 ||
strcmp(dp->d_name, "..") == 0 ||
strcmp(dp->d_name, ".svn") == 0)
{
continue;
}
strncpy(path, startdir, PATHLENGTH);
strncat(path, "/", PATHLENGTH - strlen(path) - 1);
strncat(path, dp->d_name, PATHLENGTH - strlen(path) - 1);
struct stat sbuf;
if (stat(path, &sbuf) == -1)
{
//This should only fail if we got the path wrong
// or we don't have permissions on this entry.
perror("stat");
exit(1);
}
// Only call process_dir if it is a directory
// Otherwise ignore it.
if (S_ISDIR(sbuf.st_mode))
{
if (pipe(fd) == -1)
{ // Error
fprintf(stderr, "Error creating a pipe!\n");
exit(1);
}
fds[i] = fd[0];
i++;
int r = fork();
if (r < 0)
{ // Error
fprintf(stderr, "Error creating a process!\n");
exit(1);
}
else if (r > 0)
{ // Parent
close(fd[1]);
}
else
{ // Child
printf("Processing all images in directory: %s \n", path);
close(fd[0]);
process_dir(path, img, fd[1]);
free(img->p);
free(img);
close(fd[1]);
return 0;
}
}
}
CompRecord tempCR;
for (int j = 0; j < i; j++)
{
while (read(fds[j], &tempCR, sizeof(tempCR)) > 0)
{
// Compare found CR with current CR
if (tempCR.distance < CRec.distance)
{
CRec = tempCR;
}
}
close(fds[j]);
}
free(img->p);
free(img);
printf("The most similar image is %s with a distance of %f\n", CRec.filename, CRec.distance);
return 0;
}