-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHash.h
159 lines (127 loc) · 5.56 KB
/
Hash.h
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
// Header file for the class defined for hashing operations on the images (objects).
#ifndef HASH_H
#define HASH_H
#include<list>
#include "Image.h"
#include "LinkedList.h"
#include "Logger.h"
// Function to calculate the sum of the four-digit folding of the projection profile of the image.
inline int sumFourDigitFolding(Image* Horizontal, Image* Vertical)
{
// Computing the horizontal projection profile of the image.
logger("Computing the horizontal projection profile of the image...");
int *HorizontalProjection=new int[Horizontal->Height];
for(unsigned int i=0; i<Horizontal->Height; i++)
{
HorizontalProjection[i]=0;
}
for(unsigned int i=0; i<Horizontal->Height; i++)
{
for(unsigned int j=0; j<Horizontal->Width; j++)
{
if(Horizontal->Data[i][j]==0) // Condition to check whether the pixel is black or not.
{
Horizontal->Data[i][j]=1; // Converting all the black pixels to '1'.
}
else if(Horizontal->Data[i][j]==255) // Condition to check whether the pixel is white or not.
{
Horizontal->Data[i][j]=0; // Converting all the white pixels to '0'.
}
}
}
// Calculating the sum of all '1' for every row of the image.
for(unsigned int i=0; i<Horizontal->Height; i++)
{
for(unsigned int j=0; j<Horizontal->Width; j++)
{
HorizontalProjection[i]+=Horizontal->Data[i][j];
}
}
// Computing the vertical projection profile of the image.
logger("Computing the vertical projection profile of the image...");
int *VerticalProjection=new int[Vertical->Width];
for(unsigned int i=0; i<Vertical->Width; i++)
{
VerticalProjection[i]=0;
}
for(unsigned int i=0; i<Vertical->Height; i++)
{
for(unsigned int j=0; j<Vertical->Width; j++)
{
if(Vertical->Data[i][j]==0) // Condition to check whether the pixel is black or not.
{
Vertical->Data[i][j]=1; // Converting all the black pixels to '1'.
}
else if(Vertical->Data[i][j]==255) // Condition to check whether the pixel is white or not.
{
Vertical->Data[i][j]=0; // Converting all the white pixels to '0'.
}
}
}
// Calculating the sum of all '1' for every column of the image.
for(unsigned int i=0; i<Vertical->Width; i++)
{
for(unsigned int j=0; j<Vertical->Height; j++)
{
VerticalProjection[i]+=Vertical->Data[j][i];
}
}
// Combining both the horizontal and vertical projection profiles into a single array.
logger("Combining both the horizontal and vertical projection profiles into a single array...");
int *MergedArray=new int[Horizontal->Height+Vertical->Width];
for(unsigned int i=0; i<Horizontal->Height+Vertical->Width; i++)
{
MergedArray[i]=0;
}
for(unsigned int i=0; i<Horizontal->Height; i++)
{
MergedArray[i]=HorizontalProjection[i];
}
for(unsigned int i=0; i<Vertical->Width; i++)
{
MergedArray[i+Vertical->Height]=VerticalProjection[i];
}
unsigned int Size=Horizontal->Height+Vertical->Width;
// Calculating the sum of the four-digit folding of the projection profile of the image.
logger("Calculating the sum of the four-digit folding of the projection profile of the image...");
int Remainder=Size%4; // Checking if the array can be divided into four equal segments or not.
// If the array cannot be divided into four equal segments, zeros are added to the end of the array, so that it can be divided into four equal segments.
if(Remainder!=0)
{
int* Temporary=new int[Size+4-Remainder]; // Creating a temporary array to store the modified array into.
for(unsigned int i=0; i<Size; i++)
{
Temporary[i]=MergedArray[i]; // Copying the elements of the previous array into the newly created array.
}
// Adding zeros to the end of the newly created array.
for(unsigned int i=Size; i<Size+4-Remainder; i++)
{
Temporary[i]=0;
}
delete[] MergedArray;
MergedArray=Temporary; // Assigning the modified array to the original array.
Size=Size+4-Remainder; // Updating the size of the array.
}
// Calculating the sum of the four-digit folding of the projection profile of the image.
int Sum=0;
for(unsigned int i=0; i<Size; i=i+4) // Iterating through the array in steps of four.
{
Sum+=MergedArray[i]*1000+MergedArray[i+1]*100+MergedArray[i+2]*10+MergedArray[i+3];
}
return Sum;
}
// Defining the class to store image objects in a hash table.
class Hash
{
std::list<Image> *Table;
int Size;
public:
Hash();
Hash(int Size);
int hashFunction(int Key);
void insert(Image* PGM);
void display();
bool search(std::string FileName);
void remove(int Key);
};
#endif