aboutsummaryrefslogtreecommitdiff
path: root/deduper/libpuzzle/man/libpuzzle.3
blob: 98cfcbbbc72c08a2679beeac7adbb51e5d55d8d2 (plain) (blame)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
.\"
.\" Copyright (c) 2007-2014 Frank DENIS <j at pureftpd.org>
.\"
.\" Permission to use, copy, modify, and distribute this software for any
.\" purpose with or without fee is hereby granted, provided that the above
.\" copyright notice and this permission notice appear in all copies.
.\"
.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\"
.Dd $Mdocdate: March 31 2011 $
.Dt LIBPUZZLE 3
.Sh NAME
.Nm puzzle_init_cvec ,
.Nm puzzle_init_dvec ,
.Nm puzzle_fill_dvec_from_file ,
.Nm puzzle_fill_cvec_from_file ,
.Nm puzzle_fill_dvec_from_mem ,
.Nm puzzle_fill_cvec_from_mem ,
.Nm puzzle_fill_cvec_from_dvec ,
.Nm puzzle_free_cvec ,
.Nm puzzle_free_dvec ,
.Nm puzzle_init_compressed_cvec ,
.Nm puzzle_free_compressed_cvec ,
.Nm puzzle_compress_cvec ,
.Nm puzzle_uncompress_cvec ,
.Nm puzzle_vector_normalized_distance
.Nd compute comparable signatures of bitmap images.
.Sh SYNOPSIS
.Fd #include <puzzle.h>
.Ft void
.Fn puzzle_init_context "PuzzleContext *context"
.Ft void
.Fn puzzle_free_context "PuzzleContext *context"
.Ft void
.Fn puzzle_init_cvec "PuzzleContext *context" "PuzzleCvec *cvec"
.Ft void
.Fn puzzle_init_dvec "PuzzleContext *context" "PuzzleDvec *dvec"
.Ft int
.Fn puzzle_fill_dvec_from_file "PuzzleContext *context" "PuzzleDvec * dvec" "const char *file"
.Ft int
.Fn puzzle_fill_cvec_from_file "PuzzleContext *context" "PuzzleCvec * cvec" "const char *file"
.Ft int
.Fn puzzle_fill_dvec_from_mem "PuzzleContext *context" "PuzzleDvec * dvec" "const void *mem" "size_t size"
.Ft int
.Fn puzzle_fill_cvec_from_mem "PuzzleContext *context" "PuzzleCvec * cvec" "const void *mem" "size_t size"
.Ft int
.Fn puzzle_fill_cvec_from_dvec "PuzzleContext *context" "PuzzleCvec * cvec" "const PuzzleDvec *dvec"
.Ft void
.Fn puzzle_free_cvec "PuzzleContext *context" "PuzzleCvec *cvec"
.Ft void
.Fn puzzle_free_dvec "PuzzleContext *context" "PuzzleDvec *dvec"
.Ft void
.Fn puzzle_init_compressed_cvec "PuzzleContext *context" "PuzzleCompressedCvec * compressed_cvec"
.Ft void
.Fn puzzle_free_compressed_cvec "PuzzleContext *context" "PuzzleCompressedCvec * compressed_cvec"
.Ft int
.Fn puzzle_compress_cvec "PuzzleContext *context" "PuzzleCompressedCvec * compressed_cvec" "const PuzzleCvec * cvec"
.Ft int
.Fn puzzle_uncompress_cvec "PuzzleContext *context" "PuzzleCompressedCvec * compressed_cvec" "PuzzleCvec * const cvec"
.Ft double
.Fn puzzle_vector_normalized_distance "PuzzleContext *context" "const PuzzleCvec * cvec1" "const PuzzleCvec * cvec2" "int fix_for_texts"
.Sh DESCRIPTION
The Puzzle library computes a signature out of a bitmap picture.
Signatures are comparable and similar pictures have similar signatures.
.Pp
After a picture has been loaded and uncompressed, featureless parts of
the image are skipped (autocrop), unless that step has been explicitely
disabled, see
.Xr puzzle_set 3
.Sh LIBPUZZLE CONTEXT
Every public function requires a
.Va PuzzleContext
object, that stores every required tunables.
.Pp
Any application using libpuzzle should initialize a
.Va PuzzleContext
object with
.Fn puzzle_init_context
and free it after use with
.Fn puzzle_free_context
.Bd \-literal \-offset indent
PuzzleContext context;

puzzle_init_context(&context);
 ...
puzzle_free_context(&context);
.Ed
.Sh DVEC AND CVEC VECTORS
The next step is to divide the cropped image into a grid and to compute
the average intensity of soft\(hyedged pixels in every block. The result is a
.Va PuzzleDvec
object.
.Pp
.Va PuzzleDvec
objects should be initialized before use, with
.Fn puzzle_init_dvec
and freed after use with
.Fn puzzle_free_dvec
.Pp
The
.Va PuzzleDvec
structure has two important fields:
.Va vec
is the pointer to the first element of the array containing the average
intensities, and
.Va sizeof_compressed_vec
is the number of elements.
.Pp
.Va PuzzleDvec
objects are not comparable, so what you usually want is to transform these
objects into
.Va PuzzleCvec
objects.
.Pp
A
.Va PuzzleCvec
object is a vector with relationships between adjacent blocks from a
.Va PuzzleDvec
object.
.Pp
The
.Fn puzzle_fill_cvec_from_dvec
fills a
.Va PuzzleCvec
object from a
.Va PuzzleDvec
object.
.Pp
But just like the other structure,
.Va PuzzleCvec
objects must be initialized and freed with
.Fn puzzle_init_cvec
and
.Fn puzzle_free_cvec
.Pp
.Va PuzzleCvec
objects have a vector whoose first element is in the
.Va vec
field, and the number of elements is in the
.Va sizeof_vec
field
.Sh LOADING PICTURES
.Va PuzzleDvec
and
.Va PuzzleCvec
objects can be computed from a bitmap picture file, with
.Fn puzzle_fill_dvec_from_file
and
.Fn puzzle_fill_cvec_from_file
.Pp
.Em GIF
,
.Em PNG
and
.Em JPEG
files formats are currently supported and automatically recognized.
.Pp
Here's a simple example that creates a
.Va PuzzleCvec
objects out of a file.
.Bd \-literal \-offset indent
PuzzleContext context;
PuzzleCvec cvec;

puzzle_init_context(&context);
puzzle_init_cvec(&context, &cvec);
puzzle_fill_cvec_from_file(&context, &cvec, "test\-picture.jpg");
 ...
puzzle_free_cvec(&context, &cvec);
puzzle_free_context(&context);
.Ed
.Sh COMPARING VECTORS
In order to check whether two pictures are similar, you need to compare their
.Va PuzzleCvec
signatures, using
.Fn puzzle_vector_normalized_distance
.Pp
That function returns a distance, between 0.0 and 1.0. The lesser, the nearer.
.Pp
Tests on common pictures show that a normalized distance of 0.6 (also defined as
.Va PUZZLE_CVEC_SIMILARITY_THRESHOLD
) means that both pictures are visually similar.
.Pp
If that threshold is not right for your set of pictures, you can experiment
with
.Va PUZZLE_CVEC_SIMILARITY_HIGH_THRESHOLD
,
.Va PUZZLE_CVEC_SIMILARITY_LOW_THRESHOLD
and
.Va PUZZLE_CVEC_SIMILARITY_LOWER_THRESHOLD
or with your own value.
.Pp
If the
.Fa fix_for_texts
of
.Fn puzzle_vector_normalized_distance
is
.Em 1
, a fix is applied to the computation in order to deal with bitmap pictures
that contain text. That fix is recommended, as it allows using the same
threshold for that kind of picture as for generic pictures.
.Pp
If
.Fa fix_for_texts
is
.Em 0
, that special way of computing the normalized distance is disabled.
.Bd \-literal \-offset indent
PuzzleContext context;
PuzzleCvec cvec1, cvec2;
double d;

puzzle_init_context(&context);
puzzle_init_cvec(&context, &cvec1);
puzzle_init_cvec(&context, &cvec2);
puzzle_fill_cvec_from_file(&context, &cvec1, "test\-picture\-1.jpg");
puzzle_fill_cvec_from_file(&context, &cvec2, "test\-picture\-2.jpg");
d = puzzle_vector_normalized_distance(&context, &cvec1, &cvec2, 1);
if (d < PUZZLE_CVEC_SIMILARITY_THRESHOLD) {
  puts("Pictures are similar");
}
puzzle_free_cvec(&context, &cvec2);
puzzle_free_cvec(&context, &cvec1);
puzzle_free_context(&context);
.Ed
.Sh CVEC COMPRESSION
In order to reduce storage needs,
.Va PuzzleCvec
objects can be compressed to 1/3 of their original size.
.Pp
.Va PuzzleCompressedCvec
structures hold the compressed data. Before and after use, these structures
have to be passed to
.Fn puzzle_init_compressed_cvec
and
.Fn puzzle_free_compressed_cvec
.Pp
.Fn puzzle_compress_cvec
compresses a
.Va PuzzleCvec
object into a
.Va PuzzleCompressedCvec
object.
.Pp
And
.Fn puzzle_uncompress_cvec
uncompresses a
.Va PuzzleCompressedCvec
object into a
.Va PuzzleCvec
object.
.Bd \-literal \-offset indent
PuzzleContext context;
PuzzleCvec cvec;
PuzzleCompressedCvec c_cvec;
 ...
puzzle_init_compressed_cvec(&context, &c_cvec);
puzzle_compress_cvec(&context, &c_cvec, &cvec);
 ...
puzzle_free_compressed_cvec(&context, &c_cvec); 
.Ed
The
.Va PuzzleCompressedCvec
structure has two important fields:
.Va vec
that is a pointer to the first element of the compressed data, and
.Va sizeof_compressed_vec
that contains the number of elements.
.Sh RETURN VALUE
Functions return
.Em 0
on success, and
.Em \-1
if something went wrong.
.Sh AUTHORS
.Nf
Frank DENIS
libpuzzle at pureftpd dot org
.Fi
.Sh ACKNOWLEDGMENTS
.Nf
Xerox Research Center
H. CHI WONG
Marschall BERN
David GOLDBERG
Sameh SCHAFIK
.Fi
.Sh SEE ALSO
.Xr puzzle_set 3
.Xr puzzle\-diff 8