4 * Copyright (C) 2008 The Geeqie Team
8 * This software is released under the GNU General Public License (GNU GPL).
9 * Please read the included file COPYING for more information.
10 * This software comes with no warranty of any kind, use at your own risk!
18 * These functions are intended to find images with similar color content. For
19 * example when an image was saved at different compression levels or dimensions
20 * (scaled down/up) the contents are similar, but these files do not match by file
21 * size, dimensions, or checksum.
23 * These functions create a 32 x 32 array for each color channel (red, green, blue).
24 * The array represents the average color of each corresponding part of the
25 * image. (imagine the image cut into 1024 rectangles, or a 32 x 32 grid.
26 * Each grid is then processed for the average color value, this is what
27 * is stored in the array)
29 * To compare two images, generate a ImageSimilarityData for each image, then
30 * pass them to the compare function. The return value is the percent match
31 * of the two images. (for this, simple comparisons are used, basically the return
32 * is an average of the corresponding array differences)
34 * for image_sim_compare(), the return is 0.0 to 1.0:
35 * 1.0 for exact matches (an image is compared to itself)
36 * 0.0 for exact opposite images (compare an all black to an all white image)
37 * generally only a match of > 0.85 are significant at all, and >.95 is useful to
38 * find images that have been re-saved to other formats, dimensions, or compression.
43 * The experimental (alternate) algorithm is only for testing of new techniques to
44 * improve the result, and hopes to reduce false positives.
47 static gint alternate_enabled = FALSE;
49 void image_sim_alternate_set(gint enable)
51 alternate_enabled = enable;
54 gint image_sim_alternate_enabled(void)
56 return alternate_enabled;
59 ImageSimilarityData *image_sim_new(void)
61 ImageSimilarityData *sd = g_new0(ImageSimilarityData, 1);
66 void image_sim_free(ImageSimilarityData *sd)
72 static void image_sim_channel_expand(guint8 *pix, gint len)
78 /* set the start values */
82 for (i = 0; i < len; i++)
84 if (pix[i] < l) l = pix[i];
85 if (pix[i] > h) h = pix[i];
88 /* calc scale from range */
91 scale = 255.0 / (gdouble)(h - l);
98 for (i = 0; i < len; i++)
100 pix[i] = (guint8)((gdouble)pix[i] - l * scale);
105 static int image_sim_channel_eq_sort_cb(const void *a, const void *b)
107 gint *pa = (void *)a;
108 gint *pb = (void *)b;
109 if (pa[1] < pb[1]) return -1;
110 if (pa[1] > pb[1]) return 1;
114 static void image_sim_channel_equal(guint8 *pix, gint len)
120 buf = g_new0(gint, len * 2);
123 for (i = 0; i < len; i++)
127 buf[p] = (gint)pix[i];
131 qsort (buf, len, sizeof(gint) * 2, image_sim_channel_eq_sort_cb);
134 for (i = 0; i < len; i++)
140 pix[n] = (guint8)(255 * i / len);
146 static void image_sim_channel_norm(guint8 *pix, gint len)
154 for (i = 0; i < len; i++)
156 if (pix[i] < l) l = pix[i];
157 if (pix[i] > h) h = pix[i];
160 scale = (h-l !=0) ? 255.0 / (gdouble)(h - l) : 1.0;
162 for (i = 0; i < len; i++)
164 pix[i] = (guint8)((gdouble)(pix[i] - l) * scale);
169 * define these to enable various components of the experimental compare functions
171 * Convert the thumbprint to greyscale (ignore all color information when comparing)
172 * #define ALTERNATE_USES_GREYSCALE 1
174 * Take into account the difference in change from one pixel to the next
175 * #define ALTERNATE_INCLUDE_COMPARE_CHANGE 1
178 void image_sim_alternate_processing(ImageSimilarityData *sd)
180 #ifdef ALTERNATE_USES_GREYSCALE
184 if (!alternate_enabled) return;
186 image_sim_channel_norm(sd->avg_r, sizeof(sd->avg_r));
187 image_sim_channel_norm(sd->avg_g, sizeof(sd->avg_g));
188 image_sim_channel_norm(sd->avg_b, sizeof(sd->avg_b));
190 image_sim_channel_equal(sd->avg_r, sizeof(sd->avg_r));
191 image_sim_channel_equal(sd->avg_g, sizeof(sd->avg_g));
192 image_sim_channel_equal(sd->avg_b, sizeof(sd->avg_b));
194 #ifdef ALTERNATE_USES_GREYSCALE
195 for (i = 0; i < sizeof(sd->avg_r); i++)
199 n = (guint8)((gint)(sd->avg_r[i] + sd->avg_g[i] + sd->avg_b[i]) / 3);
200 sd->avg_r[i] = sd->avg_g[i] = sd->avg_b[i] = n;
205 void image_sim_fill_data(ImageSimilarityData *sd, GdkPixbuf *pixbuf)
219 gint x_small = FALSE; /* if less than 32 w or h, set TRUE */
220 gint y_small = FALSE;
222 if (!sd || !pixbuf) return;
224 w = gdk_pixbuf_get_width(pixbuf);
225 h = gdk_pixbuf_get_height(pixbuf);
226 rs = gdk_pixbuf_get_rowstride(pixbuf);
227 pix = gdk_pixbuf_get_pixels(pixbuf);
228 has_alpha = gdk_pixbuf_get_has_alpha(pixbuf);
230 p_step = has_alpha ? 4 : 3;
247 for (ys = 0; ys < 32; ys++)
249 if (y_small) j = (gdouble)h / 32 * ys;
253 for (xs = 0; xs < 32; xs++)
259 if (x_small) i = (gdouble)w / 32 * xs;
263 for (y = j; y < j + y_inc; y++)
265 p = pix + (y * rs) + (i * p_step);
266 for (x = i; x < i + x_inc; x++)
294 ImageSimilarityData *image_sim_new_from_pixbuf(GdkPixbuf *pixbuf)
296 ImageSimilarityData *sd;
298 sd = image_sim_new();
299 image_sim_fill_data(sd, pixbuf);
304 #ifdef ALTERNATE_INCLUDE_COMPARE_CHANGE
305 static gdouble alternate_image_sim_compare_fast(ImageSimilarityData *a, ImageSimilarityData *b, gdouble min)
312 if (!a || !b || !a->filled || !b->filled) return 0.0;
319 for (j = 0; j < 1024; j+= 32)
321 for (i = j; i < j + 32; i++)
326 cr = abs(a->avg_r[i] - b->avg_r[i]);
327 cg = abs(a->avg_g[i] - b->avg_g[i]);
328 cb = abs(a->avg_b[i] - b->avg_b[i]);
331 sim += cd + abs(cd - ld);
334 /* check for abort, if so return 0.0 */
335 if ((gdouble)sim / (255.0 * 1024.0 * 4.0) > min) return 0.0;
338 return (1.0 - ((gdouble)sim / (255.0 * 1024.0 * 4.0)) );
342 gdouble image_sim_compare(ImageSimilarityData *a, ImageSimilarityData *b)
347 if (!a || !b || !a->filled || !b->filled) return 0.0;
351 for (i = 0; i < 1024; i++)
353 sim += abs(a->avg_r[i] - b->avg_r[i]);
354 sim += abs(a->avg_g[i] - b->avg_g[i]);
355 sim += abs(a->avg_b[i] - b->avg_b[i]);
358 return 1.0 - ((gdouble)sim / (255.0 * 1024.0 * 3.0));
361 /* this uses a cutoff point so that it can abort early when it gets to
362 * a point that can simply no longer make the cut-off point.
364 gdouble image_sim_compare_fast(ImageSimilarityData *a, ImageSimilarityData *b, gdouble min)
370 #ifdef ALTERNATE_INCLUDE_COMPARE_CHANGE
371 if (alternate_enabled) return alternate_image_sim_compare_fast(a, b, min);
374 if (!a || !b || !a->filled || !b->filled) return 0.0;
379 for (j = 0; j < 1024; j+= 32)
381 for (i = j; i < j + 32; i++)
383 sim += abs(a->avg_r[i] - b->avg_r[i]);
384 sim += abs(a->avg_g[i] - b->avg_g[i]);
385 sim += abs(a->avg_b[i] - b->avg_b[i]);
387 /* check for abort, if so return 0.0 */
388 if ((gdouble)sim / (255.0 * 1024.0 * 3.0) > min) return 0.0;
391 return (1.0 - ((gdouble)sim / (255.0 * 1024.0 * 3.0)) );