summaryrefslogtreecommitdiff
path: root/wimmel.c
blob: 03fc1d45a6d7355e941e9aeab695948bfe23efe6 (plain)
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
#include <stdlib.h>
#include <glib.h>
#include <gdk-pixbuf/gdk-pixbuf.h>

#include "util.h"

#ifndef N_THREADS
#define N_THREADS 4
#endif

static void
find_image_in_wimmel(GdkPixbuf *wimmel, GdkPixbuf *match, GdkPixbuf *output)
{
	int wimmel_width, wimmel_height;
	int width, height;
	int i, j, k, n;
	int difference, barrier;

	wimmel_width = gdk_pixbuf_get_width(wimmel);
	wimmel_height = gdk_pixbuf_get_height(wimmel);
	width = gdk_pixbuf_get_width(match);
	height = gdk_pixbuf_get_height(match);

	barrier = width * height / 2;

	int stride = gdk_pixbuf_get_rowstride(wimmel);
	int nch = gdk_pixbuf_get_n_channels(wimmel);
	guchar *row = gdk_pixbuf_get_pixels(wimmel);
	guchar *pix, *pix_m;

	int stride_m = gdk_pixbuf_get_rowstride(match);

	for (j = 0; j < wimmel_height - height; ++j, row += stride) {
		for (i = 0, pix = row; i < wimmel_width - width; ++i, pix += nch) {

			difference = 0;
			guchar *row_m = gdk_pixbuf_get_pixels(match);
			for (n = 0; n < height; ++n, row_m += stride_m) {
				for (k = 0, pix_m = row_m; k < width; ++k, pix_m += nch) {

					pix = row + n * stride + (i + k) * nch;

					if (abs((int)pix[0] - (int)pix_m[0]) > 50 ||
					    abs((int)pix[1] - (int)pix_m[1]) > 50 ||
					    abs((int)pix[2] - (int)pix_m[2]) > 50) {
						difference++;
						if (difference > barrier)
							break;
					}
				}
			}

			if (difference < barrier) {
				g_print("kleeblatt @ %d, %d; difference: %d\n", i, j, difference);
				color_t color = COLOR(255, 0, 0, 0);
				for (k = i; k < i+width; k++) {
					put_pixel(output, POINT(k, j), color);
					put_pixel(output, POINT(k, j+height), color);
				}
				for (n = j; n < j+height; n++) {
					put_pixel(output, POINT(i, n), color);
					put_pixel(output, POINT(i+width, n), color);
				}
			}
		}
	}
}
struct find_params {
	GdkPixbuf *wimmel;
	GdkPixbuf *match;
	GdkPixbuf *output;
};

static gpointer
find_thread(gpointer data)
{
	struct find_params *params = data;

	find_image_in_wimmel(params->wimmel, params->match, params->output);
	g_object_unref(params->wimmel);
	g_object_unref(params->match);
	g_object_unref(params->output);

	free(params);

	return NULL;
}

int
main(int argc, char *argv[])
{
	char *file;
	GdkPixbuf *wimmel, *output;
	GdkPixbuf *match_tmp, *match;
	int i;
	int x1, y1, mwidth, mheight, width, height;
	GThread *thread[N_THREADS];

	g_type_init();

	if (argc < 5)
		exit(EXIT_FAILURE);
	
	file = argv[1];
	x1 = atoi(argv[2]);
	y1 = atoi(argv[3]);
	mwidth = atoi(argv[4]);
	mheight = atoi(argv[5]);

	wimmel = gdk_pixbuf_new_from_file(file, NULL);
	if (!wimmel)
		exit(EXIT_FAILURE);

	output = gdk_pixbuf_copy(wimmel);

	match_tmp = gdk_pixbuf_new_subpixbuf(wimmel, x1, y1, mwidth, mheight);
	match = gdk_pixbuf_copy(match_tmp);
	g_object_unref(match_tmp);

	width = gdk_pixbuf_get_width(wimmel);
	height = gdk_pixbuf_get_height(wimmel);

	int step_y = height / N_THREADS;
	int y;
	for (i = 0, y = 0; i < N_THREADS; ++i, y += step_y) {
		struct find_params *params = malloc(sizeof (struct find_params));;
		if (!params)
			exit(EXIT_FAILURE);
		
		int h = step_y + mheight;

		if (y + h > height)
			h = height - y;

		params->wimmel = gdk_pixbuf_new_subpixbuf(wimmel, 0, y, width, h);
		params->output = gdk_pixbuf_new_subpixbuf(output, 0, y, width, h);
		params->match = g_object_ref(match);

		thread[i] = g_thread_create(find_thread, params, TRUE, NULL);
	}

	for (i = 0; i < N_THREADS; ++i)
		g_thread_join(thread[i]);

	gdk_pixbuf_save(output, "output.png", "png", NULL, NULL);
	g_object_unref(wimmel);
	g_object_unref(output);
	g_object_unref(match);
}