… | |
… | |
14 | #include <gdk-pixbuf/gdk-pixbuf.h> |
14 | #include <gdk-pixbuf/gdk-pixbuf.h> |
15 | |
15 | |
16 | #include <gperl.h> |
16 | #include <gperl.h> |
17 | #include <gtk2perl.h> |
17 | #include <gtk2perl.h> |
18 | |
18 | |
|
|
19 | #include <assert.h> |
|
|
20 | |
|
|
21 | #include "perlmulticore.h" |
|
|
22 | |
19 | #define IW 80 /* MUST match Schnauzer.pm! */ |
23 | #define IW 80 /* MUST match Schnauzer.pm! */ |
20 | #define IH 60 /* MUST match Schnauzer.pm! */ |
24 | #define IH 60 /* MUST match Schnauzer.pm! */ |
21 | |
25 | |
22 | #define RAND (seed = (seed + 7141) * 54773 % 134456) |
26 | #define RAND (seed = (seed + 7141) * 54773 % 134456) |
23 | |
27 | |
24 | #define LINELENGTH 240 |
28 | #define LINELENGTH 240 |
25 | |
29 | |
26 | #define ELLIPSIS "\xe2\x80\xa6" |
30 | #define ELLIPSIS "\xe2\x80\xa6" |
|
|
31 | |
|
|
32 | typedef char *octet_string; |
27 | |
33 | |
28 | struct jpg_err_mgr |
34 | struct jpg_err_mgr |
29 | { |
35 | { |
30 | struct jpeg_error_mgr err; |
36 | struct jpeg_error_mgr err; |
31 | jmp_buf setjmp_buffer; |
37 | jmp_buf setjmp_buffer; |
32 | }; |
38 | }; |
33 | |
39 | |
34 | static void |
40 | static void |
35 | cv_error_exit (j_common_ptr cinfo) |
41 | cv_error_exit (j_common_ptr cinfo) |
36 | { |
42 | { |
|
|
43 | cinfo->err->output_message (cinfo); |
37 | longjmp (((struct jpg_err_mgr *)cinfo->err)->setjmp_buffer, 99); |
44 | longjmp (((struct jpg_err_mgr *)cinfo->err)->setjmp_buffer, 99); |
38 | } |
45 | } |
39 | |
46 | |
40 | static void |
47 | static void |
41 | cv_error_output (j_common_ptr cinfo) |
48 | cv_error_output (j_common_ptr cinfo) |
42 | { |
49 | { |
|
|
50 | char msg[JMSG_LENGTH_MAX]; |
|
|
51 | |
|
|
52 | cinfo->err->format_message (cinfo, msg); |
|
|
53 | |
|
|
54 | fprintf (stderr, "JPEG decoding error: %s\n", msg); |
43 | return; |
55 | return; |
44 | } |
56 | } |
45 | |
57 | |
46 | static void |
58 | static void |
47 | rgb_to_hsv (unsigned int r, unsigned int g, unsigned int b, |
59 | rgb_to_hsv (unsigned int r, unsigned int g, unsigned int b, |
… | |
… | |
68 | |
80 | |
69 | *h &= 255; |
81 | *h &= 255; |
70 | } |
82 | } |
71 | } |
83 | } |
72 | |
84 | |
|
|
85 | struct feature { |
|
|
86 | float v1, v2, v3; // mean, square, cube |
|
|
87 | int n; |
|
|
88 | }; |
|
|
89 | |
|
|
90 | static void |
|
|
91 | feature_init (struct feature *f) |
|
|
92 | { |
|
|
93 | f->v1 = 0.; |
|
|
94 | f->v2 = 0.; |
|
|
95 | f->v3 = 0.; |
|
|
96 | f->n = 0; |
|
|
97 | } |
|
|
98 | |
|
|
99 | // didn't find an algorithm to neatly do mean, variance and skew in one pass. |
|
|
100 | // elmex ist schuld. |
|
|
101 | static void |
|
|
102 | feature_update_pass_1 (struct feature *f, unsigned int v) |
|
|
103 | { |
|
|
104 | f->v1 += v; |
|
|
105 | f->n += 1; |
|
|
106 | } |
|
|
107 | |
|
|
108 | static void |
|
|
109 | feature_finish_pass_1 (struct feature *f) |
|
|
110 | { |
|
|
111 | if (f->n < 1) |
|
|
112 | return; |
|
|
113 | |
|
|
114 | f->v1 /= f->n; |
|
|
115 | } |
|
|
116 | |
|
|
117 | static void |
|
|
118 | feature_update_pass_2 (struct feature *f, unsigned int v) |
|
|
119 | { |
|
|
120 | float d = v - f->v1; |
|
|
121 | |
|
|
122 | f->v2 += d * d; |
|
|
123 | f->v3 += d * d * d; |
|
|
124 | } |
|
|
125 | |
|
|
126 | static void |
|
|
127 | feature_finish_pass_2 (struct feature *f) |
|
|
128 | { |
|
|
129 | if (f->n < 1) |
|
|
130 | return; |
|
|
131 | |
|
|
132 | f->v2 /= f->n; |
|
|
133 | f->v3 /= f->n; |
|
|
134 | |
|
|
135 | f->v1 /= 255.; |
|
|
136 | f->v2 /= 255. * 255.; f->v2 = sqrtf (f->v2); |
|
|
137 | f->v3 /= 255. * 255. * 255.; f->v3 = powf (fabsf (f->v3), 1./3.); |
|
|
138 | } |
|
|
139 | |
73 | static guint32 a85_val; |
140 | static guint32 a85_val; |
74 | static guint a85_cnt; |
141 | static guint a85_cnt; |
75 | static guchar a85_buf[LINELENGTH], *a85_ptr; |
142 | static guchar a85_buf[LINELENGTH], *a85_ptr; |
76 | |
143 | |
77 | static void |
144 | static void |
… | |
… | |
89 | if (!--a85_cnt) |
156 | if (!--a85_cnt) |
90 | { |
157 | { |
91 | a85_cnt = 4; |
158 | a85_cnt = 4; |
92 | if (a85_val) |
159 | if (a85_val) |
93 | { |
160 | { |
94 | a85_ptr[4] = (a85_val % 85) + 33; a85_val /= 85; |
161 | a85_ptr[4] = (a85_val % 85) + 33; a85_val /= 85; |
95 | a85_ptr[3] = (a85_val % 85) + 33; a85_val /= 85; |
162 | a85_ptr[3] = (a85_val % 85) + 33; a85_val /= 85; |
96 | a85_ptr[2] = (a85_val % 85) + 33; a85_val /= 85; |
163 | a85_ptr[2] = (a85_val % 85) + 33; a85_val /= 85; |
97 | a85_ptr[1] = (a85_val % 85) + 33; a85_val /= 85; |
164 | a85_ptr[1] = (a85_val % 85) + 33; a85_val /= 85; |
98 | a85_ptr[0] = (a85_val ) + 33; |
165 | a85_ptr[0] = (a85_val ) + 33; |
99 | |
166 | |
100 | a85_ptr += 5; |
167 | a85_ptr += 5; |
101 | } |
168 | } |
… | |
… | |
147 | |
214 | |
148 | OUTPUT: |
215 | OUTPUT: |
149 | RETVAL |
216 | RETVAL |
150 | |
217 | |
151 | const char * |
218 | const char * |
152 | magic (const char *path) |
219 | magic (octet_string path) |
153 | CODE: |
220 | CODE: |
154 | { |
221 | { |
155 | static magic_t cookie; |
222 | static magic_t cookie; |
156 | |
223 | |
157 | if (!cookie) |
224 | if (!cookie) |
158 | { |
225 | { |
159 | cookie = magic_open (MAGIC_NONE); |
226 | cookie = magic_open (MAGIC_SYMLINK); |
160 | |
227 | |
161 | if (cookie) |
228 | if (cookie) |
162 | magic_load (cookie, 0); |
229 | magic_load (cookie, 0); |
163 | else |
230 | else |
164 | XSRETURN_UNDEF; |
231 | XSRETURN_UNDEF; |
… | |
… | |
168 | } |
235 | } |
169 | OUTPUT: |
236 | OUTPUT: |
170 | RETVAL |
237 | RETVAL |
171 | |
238 | |
172 | const char * |
239 | const char * |
173 | magic_mime (const char *path) |
240 | magic_mime (octet_string path) |
174 | CODE: |
241 | CODE: |
175 | { |
242 | { |
176 | static magic_t cookie; |
243 | static magic_t cookie; |
177 | |
244 | |
178 | if (!cookie) |
245 | if (!cookie) |
179 | { |
246 | { |
180 | cookie = magic_open (MAGIC_MIME); |
247 | cookie = magic_open (MAGIC_MIME | MAGIC_SYMLINK); |
181 | |
248 | |
182 | if (cookie) |
249 | if (cookie) |
183 | magic_load (cookie, 0); |
250 | magic_load (cookie, 0); |
184 | else |
251 | else |
185 | XSRETURN_UNDEF; |
252 | XSRETURN_UNDEF; |
186 | } |
253 | } |
187 | |
254 | |
|
|
255 | perlinterp_release (); |
188 | RETVAL = magic_file (cookie, path); |
256 | RETVAL = magic_file (cookie, path); |
|
|
257 | perlinterp_acquire (); |
189 | } |
258 | } |
190 | OUTPUT: |
259 | OUTPUT: |
191 | RETVAL |
260 | RETVAL |
192 | |
261 | |
193 | # missing in Gtk2 perl module |
262 | # missing/broken in Gtk2 perl module |
|
|
263 | |
|
|
264 | void |
|
|
265 | gdk_window_clear_hints (GdkWindow *window) |
|
|
266 | CODE: |
|
|
267 | gdk_window_set_geometry_hints (window, 0, 0); |
194 | |
268 | |
195 | gboolean |
269 | gboolean |
196 | gdk_net_wm_supports (GdkAtom property) |
270 | gdk_net_wm_supports (GdkAtom property) |
197 | CODE: |
271 | CODE: |
198 | #if defined(GDK_WINDOWING_X11) && !defined(GDK_MULTIHEAD_SAFE) |
272 | #if defined(GDK_WINDOWING_X11) && !defined(GDK_MULTIHEAD_SAFE) |
… | |
… | |
204 | RETVAL |
278 | RETVAL |
205 | |
279 | |
206 | GdkPixbuf_noinc * |
280 | GdkPixbuf_noinc * |
207 | dealpha_expose (GdkPixbuf *pb) |
281 | dealpha_expose (GdkPixbuf *pb) |
208 | CODE: |
282 | CODE: |
|
|
283 | perlinterp_release (); |
209 | { |
284 | { |
210 | int w = gdk_pixbuf_get_width (pb); |
285 | int w = gdk_pixbuf_get_width (pb); |
211 | int h = gdk_pixbuf_get_height (pb); |
286 | int h = gdk_pixbuf_get_height (pb); |
212 | int bpp = gdk_pixbuf_get_n_channels (pb); |
287 | int bpp = gdk_pixbuf_get_n_channels (pb); |
213 | int x, y, i; |
288 | int x, y, i; |
… | |
… | |
222 | for (x = 0; x < w; x++) |
297 | for (x = 0; x < w; x++) |
223 | for (y = 0; y < h; y++) |
298 | for (y = 0; y < h; y++) |
224 | for (i = 0; i < 3; i++) |
299 | for (i = 0; i < 3; i++) |
225 | dst[x * 3 + y * dstr + i] = src[x * bpp + y * sstr + i]; |
300 | dst[x * 3 + y * dstr + i] = src[x * bpp + y * sstr + i]; |
226 | } |
301 | } |
|
|
302 | perlinterp_acquire (); |
227 | OUTPUT: |
303 | OUTPUT: |
228 | RETVAL |
304 | RETVAL |
229 | |
305 | |
230 | GdkPixbuf_noinc * |
306 | GdkPixbuf_noinc * |
231 | rotate (GdkPixbuf *pb, int angle) |
307 | rotate (GdkPixbuf *pb, int angle) |
232 | CODE: |
308 | CODE: |
|
|
309 | perlinterp_release (); |
|
|
310 | if (angle < 0) |
|
|
311 | angle += 360; |
233 | RETVAL = gdk_pixbuf_rotate_simple (pb, angle == 0 ? GDK_PIXBUF_ROTATE_NONE |
312 | RETVAL = gdk_pixbuf_rotate_simple (pb, angle == 0 ? GDK_PIXBUF_ROTATE_NONE |
234 | : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE |
313 | : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE |
235 | : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN |
314 | : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN |
236 | : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE |
315 | : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE |
237 | : angle); |
316 | : angle); |
|
|
317 | perlinterp_acquire (); |
238 | OUTPUT: |
318 | OUTPUT: |
239 | RETVAL |
319 | RETVAL |
240 | |
320 | |
241 | GdkPixbuf_noinc * |
321 | GdkPixbuf_noinc * |
242 | load_jpeg (SV *path, int thumbnail=0) |
322 | load_jpeg (SV *path, int thumbnail = 0, int iw = 0, int ih = 0) |
243 | CODE: |
323 | CODE: |
244 | { |
324 | { |
245 | struct jpeg_decompress_struct cinfo; |
325 | struct jpeg_decompress_struct cinfo; |
246 | struct jpg_err_mgr jerr; |
326 | struct jpg_err_mgr jerr; |
247 | guchar *data; |
327 | guchar *data; |
… | |
… | |
254 | fp = fopen (SvPVbyte_nolen (path), "rb"); |
334 | fp = fopen (SvPVbyte_nolen (path), "rb"); |
255 | |
335 | |
256 | if (!fp) |
336 | if (!fp) |
257 | XSRETURN_UNDEF; |
337 | XSRETURN_UNDEF; |
258 | |
338 | |
|
|
339 | perlinterp_release (); |
|
|
340 | |
259 | cinfo.err = jpeg_std_error (&jerr.err); |
341 | cinfo.err = jpeg_std_error (&jerr.err); |
260 | |
342 | |
261 | jerr.err.error_exit = cv_error_exit; |
343 | jerr.err.error_exit = cv_error_exit; |
262 | jerr.err.output_message = cv_error_output; |
344 | jerr.err.output_message = cv_error_output; |
263 | |
345 | |
… | |
… | |
267 | jpeg_destroy_decompress (&cinfo); |
349 | jpeg_destroy_decompress (&cinfo); |
268 | |
350 | |
269 | if (pb) |
351 | if (pb) |
270 | g_object_unref ((gpointer)pb); |
352 | g_object_unref ((gpointer)pb); |
271 | |
353 | |
|
|
354 | perlinterp_acquire (); |
272 | XSRETURN_UNDEF; |
355 | XSRETURN_UNDEF; |
273 | } |
356 | } |
274 | |
357 | |
275 | jpeg_create_decompress (&cinfo); |
358 | jpeg_create_decompress (&cinfo); |
276 | |
359 | |
… | |
… | |
292 | { |
375 | { |
293 | cinfo.dct_method = JDCT_FASTEST; |
376 | cinfo.dct_method = JDCT_FASTEST; |
294 | cinfo.do_fancy_upsampling = FALSE; |
377 | cinfo.do_fancy_upsampling = FALSE; |
295 | |
378 | |
296 | while (cinfo.scale_denom < 8 |
379 | while (cinfo.scale_denom < 8 |
297 | && cinfo.output_width >= IW*4 |
380 | && cinfo.output_width >= iw*4 |
298 | && cinfo.output_height >= IH*4) |
381 | && cinfo.output_height >= ih*4) |
299 | { |
382 | { |
300 | cinfo.scale_denom <<= 1; |
383 | cinfo.scale_denom <<= 1; |
301 | jpeg_calc_output_dimensions (&cinfo); |
384 | jpeg_calc_output_dimensions (&cinfo); |
302 | } |
385 | } |
303 | } |
386 | } |
304 | |
387 | |
|
|
388 | if (cinfo.output_components != 3) |
|
|
389 | longjmp (jerr.setjmp_buffer, 3); |
|
|
390 | |
|
|
391 | if (cinfo.jpeg_color_space == JCS_YCCK || cinfo.jpeg_color_space == JCS_CMYK) |
|
|
392 | { |
|
|
393 | cinfo.out_color_space = JCS_CMYK; |
|
|
394 | cinfo.output_components = 4; |
|
|
395 | } |
|
|
396 | |
305 | pb = RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, cinfo.output_width, cinfo.output_height); |
397 | pb = RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, cinfo.output_components == 4, 8, cinfo.output_width, cinfo.output_height); |
306 | if (!RETVAL) |
398 | if (!RETVAL) |
307 | longjmp (jerr.setjmp_buffer, 2); |
399 | longjmp (jerr.setjmp_buffer, 2); |
308 | |
400 | |
309 | data = gdk_pixbuf_get_pixels (RETVAL); |
401 | data = gdk_pixbuf_get_pixels (RETVAL); |
310 | rs = gdk_pixbuf_get_rowstride (RETVAL); |
402 | rs = gdk_pixbuf_get_rowstride (RETVAL); |
311 | |
|
|
312 | if (cinfo.output_components != 3) |
|
|
313 | longjmp (jerr.setjmp_buffer, 3); |
|
|
314 | |
403 | |
315 | jpeg_start_decompress (&cinfo); |
404 | jpeg_start_decompress (&cinfo); |
316 | |
405 | |
317 | while (cinfo.output_scanline < cinfo.output_height) |
406 | while (cinfo.output_scanline < cinfo.output_height) |
318 | { |
407 | { |
… | |
… | |
325 | rp [3] = (guchar *)rp [2] + rs; |
414 | rp [3] = (guchar *)rp [2] + rs; |
326 | |
415 | |
327 | jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4); |
416 | jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4); |
328 | } |
417 | } |
329 | |
418 | |
|
|
419 | if (cinfo.out_color_space == JCS_CMYK) |
|
|
420 | { |
|
|
421 | guchar *end = data + cinfo.output_height * rs; |
|
|
422 | |
|
|
423 | while (data < end) |
|
|
424 | { |
|
|
425 | U32 c = data [0]; |
|
|
426 | U32 m = data [1]; |
|
|
427 | U32 y = data [2]; |
|
|
428 | U32 k = data [3]; |
|
|
429 | |
|
|
430 | if (0) |
|
|
431 | if (cinfo.Adobe_transform == 2) |
|
|
432 | { |
|
|
433 | c ^= 0xff; |
|
|
434 | m ^= 0xff; |
|
|
435 | y ^= 0xff; |
|
|
436 | k ^= 0xff; |
|
|
437 | } |
|
|
438 | |
|
|
439 | data [0] = (c * k + 0x80) / 0xff; |
|
|
440 | data [1] = (m * k + 0x80) / 0xff; |
|
|
441 | data [2] = (y * k + 0x80) / 0xff; |
|
|
442 | data [3] = 0xff; |
|
|
443 | |
|
|
444 | data += 4; |
|
|
445 | } |
|
|
446 | } |
|
|
447 | |
330 | jpeg_finish_decompress (&cinfo); |
448 | jpeg_finish_decompress (&cinfo); |
331 | fclose (fp); |
449 | fclose (fp); |
332 | jpeg_destroy_decompress (&cinfo); |
450 | jpeg_destroy_decompress (&cinfo); |
|
|
451 | perlinterp_acquire (); |
333 | } |
452 | } |
334 | OUTPUT: |
453 | OUTPUT: |
335 | RETVAL |
454 | RETVAL |
336 | |
455 | |
337 | void |
456 | void |
338 | compare (GdkPixbuf *a, GdkPixbuf *b) |
457 | compare (GdkPixbuf *a, GdkPixbuf *b) |
339 | PPCODE: |
458 | PPCODE: |
|
|
459 | perlinterp_release (); |
340 | { |
460 | { |
341 | int w = gdk_pixbuf_get_width (a); |
461 | int w = gdk_pixbuf_get_width (a); |
342 | int h = gdk_pixbuf_get_height (a); |
462 | int h = gdk_pixbuf_get_height (a); |
|
|
463 | |
343 | int sa = gdk_pixbuf_get_rowstride (a); |
464 | int sa = gdk_pixbuf_get_rowstride (a); |
344 | int sb = gdk_pixbuf_get_rowstride (b); |
465 | int sb = gdk_pixbuf_get_rowstride (b); |
345 | |
466 | |
346 | guchar *pa = gdk_pixbuf_get_pixels (a); |
467 | guchar *pa = gdk_pixbuf_get_pixels (a); |
347 | guchar *pb = gdk_pixbuf_get_pixels (b); |
468 | guchar *pb = gdk_pixbuf_get_pixels (b); |
… | |
… | |
371 | d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d)); |
492 | d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d)); |
372 | d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d)); |
493 | d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d)); |
373 | } |
494 | } |
374 | } |
495 | } |
375 | |
496 | |
|
|
497 | perlinterp_acquire (); |
|
|
498 | |
376 | EXTEND (SP, 2); |
499 | EXTEND (SP, 2); |
377 | PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.))))); |
500 | PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.))))); |
378 | PUSHs (sv_2mortal (newSVnv (peak / 255.))); |
501 | PUSHs (sv_2mortal (newSVnv (peak / 255.))); |
379 | } |
502 | } |
380 | |
503 | |
381 | ############################################################################# |
504 | ############################################################################# |
382 | |
505 | |
383 | MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer |
506 | MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer |
384 | |
507 | |
|
|
508 | # currently only works for filenames (octet strings) |
|
|
509 | |
385 | SV * |
510 | SV * |
386 | foldcase (SV *pathsv) |
511 | foldcase (SV *pathsv) |
387 | PROTOTYPE: $ |
512 | PROTOTYPE: $ |
388 | CODE: |
513 | CODE: |
389 | { |
514 | { |
390 | STRLEN plen; |
515 | STRLEN plen; |
391 | U8 *path = (U8 *)SvPVutf8 (pathsv, plen); |
516 | U8 *path = (U8 *)SvPV (pathsv, plen); |
392 | U8 *pend = path + plen; |
517 | U8 *pend = path + plen; |
393 | U8 dst [plen * 6 * 3], *dstp = dst; |
518 | U8 dst [plen * 6 * 3], *dstp = dst; |
394 | |
519 | |
395 | while (path < pend) |
520 | while (path < pend) |
396 | { |
521 | { |
397 | U8 ch = *path; |
522 | U8 ch = *path; |
398 | |
523 | |
399 | if (ch >= 'a' && ch <= 'z') |
524 | if (ch >= 'a' && ch <= 'z') |
400 | *dstp++ = *path++; |
525 | *dstp++ = *path++; |
|
|
526 | else if (ch >= 'A' && ch <= 'Z') |
|
|
527 | *dstp++ = *path++ + ('a' - 'A'); |
401 | else if (ch >= '0' && ch <= '9') |
528 | else if (ch >= '0' && ch <= '9') |
402 | { |
529 | { |
403 | STRLEN el, nl = 0; |
530 | STRLEN el, nl = 0; |
404 | while (*path >= '0' && *path <= '9' && path < pend) |
531 | while (*path >= '0' && *path <= '9' && path < pend) |
405 | path++, nl++; |
532 | path++, nl++; |
… | |
… | |
408 | *dstp++ = '0'; |
535 | *dstp++ = '0'; |
409 | |
536 | |
410 | memcpy (dstp, path - nl, nl); |
537 | memcpy (dstp, path - nl, nl); |
411 | dstp += nl; |
538 | dstp += nl; |
412 | } |
539 | } |
|
|
540 | else |
|
|
541 | *dstp++ = *path++; |
|
|
542 | #if 0 |
413 | else |
543 | else |
414 | { |
544 | { |
415 | STRLEN cl; |
545 | STRLEN cl; |
416 | to_utf8_fold (path, dstp, &cl); |
546 | to_utf8_fold (path, dstp, &cl); |
417 | dstp += cl; |
547 | dstp += cl; |
418 | path += is_utf8_char (path); |
548 | path += is_utf8_char (path); |
419 | } |
549 | } |
|
|
550 | #endif |
420 | } |
551 | } |
421 | |
552 | |
422 | RETVAL = newSVpvn ((const char *)dst, dstp - dst); |
553 | RETVAL = newSVpvn ((const char *)dst, dstp - dst); |
423 | } |
554 | } |
424 | OUTPUT: |
555 | OUTPUT: |
… | |
… | |
586 | ############################################################################# |
717 | ############################################################################# |
587 | |
718 | |
588 | MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Plugin::RCluster |
719 | MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Plugin::RCluster |
589 | |
720 | |
590 | SV * |
721 | SV * |
591 | make_histograms (SV *ar) |
722 | extract_features (SV *ar) |
592 | CODE: |
723 | CODE: |
593 | { |
724 | { |
594 | int i; |
725 | int i; |
595 | AV *av, *result; |
726 | AV *av, *result; |
596 | |
727 | |
597 | if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV) |
728 | if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV) |
598 | croak ("Not an array ref as first argument to make_histogram"); |
729 | croak ("Not an array ref as first argument to extract_features"); |
599 | |
730 | |
600 | av = (AV *) SvRV (ar); |
731 | av = (AV *) SvRV (ar); |
601 | result = newAV (); |
732 | result = newAV (); |
602 | |
733 | |
603 | for (i = 0; i <= av_len (av); ++i) |
734 | for (i = 0; i <= av_len (av); ++i) |
604 | { |
735 | { |
605 | const int HISTSIZE = 64; |
|
|
606 | |
|
|
607 | int j; |
|
|
608 | SV *sv = *av_fetch (av, i, 1); |
736 | SV *sv = *av_fetch (av, i, 1); |
609 | STRLEN len; |
|
|
610 | char *buf = SvPVbyte (sv, len); |
|
|
611 | |
|
|
612 | int tmphist[HISTSIZE]; |
|
|
613 | float *hist; |
|
|
614 | |
|
|
615 | SV *histsv = newSV (HISTSIZE * sizeof (float) + 1); |
737 | SV *histsv = newSV (9 * sizeof (float) + 1); |
|
|
738 | |
616 | SvPOK_on (histsv); |
739 | SvPOK_on (histsv); |
617 | SvCUR_set (histsv, HISTSIZE * sizeof (float)); |
740 | SvCUR_set (histsv, 9 * sizeof (float)); |
618 | hist = (float *)SvPVX (histsv); |
741 | float *hist = (float *)SvPVX (histsv); |
619 | |
742 | |
620 | Zero (tmphist, sizeof (tmphist), char); |
743 | struct feature f_h, f_s, f_v; |
|
|
744 | feature_init (&f_h); |
|
|
745 | feature_init (&f_s); |
|
|
746 | feature_init (&f_v); |
621 | |
747 | |
622 | for (j = len; j--; ) |
|
|
623 | { |
|
|
624 | unsigned int idx |
|
|
625 | = ((*buf & 0xc0) >> 2) |
|
|
626 | | ((*buf & 0x18) >> 1) |
|
|
627 | | (*buf & 0x03); |
|
|
628 | |
|
|
629 | ++tmphist[idx]; |
|
|
630 | ++buf; |
|
|
631 | } |
|
|
632 | |
748 | { |
633 | for (j = 0; j < HISTSIZE; ++j) |
749 | STRLEN len; |
634 | hist[j] = (float)tmphist[j] / (len + 1e-30); |
750 | unsigned char *buf = (unsigned char *)SvPVbyte (sv, len); |
|
|
751 | while (len >= 3) |
|
|
752 | { |
|
|
753 | unsigned int r, g, b, h, s, v; |
|
|
754 | r = *buf++; g = *buf++; b = *buf++; |
|
|
755 | rgb_to_hsv (r, g, b, &h, &s, &v); |
|
|
756 | |
|
|
757 | feature_update_pass_1 (&f_h, h); |
|
|
758 | feature_update_pass_1 (&f_s, s); |
|
|
759 | feature_update_pass_1 (&f_v, v); |
|
|
760 | |
|
|
761 | len -= 3; |
|
|
762 | } |
|
|
763 | |
|
|
764 | feature_finish_pass_1 (&f_h); |
|
|
765 | feature_finish_pass_1 (&f_s); |
|
|
766 | feature_finish_pass_1 (&f_v); |
|
|
767 | } |
|
|
768 | |
|
|
769 | { |
|
|
770 | STRLEN len; |
|
|
771 | unsigned char *buf = (unsigned char *)SvPVbyte (sv, len); |
|
|
772 | while (len >= 3) |
|
|
773 | { |
|
|
774 | unsigned int r, g, b, h, s, v; |
|
|
775 | r = *buf++; g = *buf++; b = *buf++; |
|
|
776 | rgb_to_hsv (r, g, b, &h, &s, &v); |
|
|
777 | |
|
|
778 | feature_update_pass_2 (&f_h, h); |
|
|
779 | feature_update_pass_2 (&f_s, s); |
|
|
780 | feature_update_pass_2 (&f_v, v); |
|
|
781 | |
|
|
782 | len -= 3; |
|
|
783 | } |
|
|
784 | |
|
|
785 | feature_finish_pass_2 (&f_h); |
|
|
786 | feature_finish_pass_2 (&f_s); |
|
|
787 | feature_finish_pass_2 (&f_v); |
|
|
788 | } |
|
|
789 | |
|
|
790 | hist [0] = f_h.v1 * 2.; hist [1] = f_h.v2 * 2.; hist [2] = f_h.v3 * 2.; |
|
|
791 | hist [3] = f_s.v1 ; hist [4] = f_s.v2 ; hist [5] = f_s.v3 ; |
|
|
792 | hist [6] = f_v.v1 * .5; hist [7] = f_v.v2 * .5; hist [8] = f_v.v3 * .5; |
635 | |
793 | |
636 | av_push (result, histsv); |
794 | av_push (result, histsv); |
637 | } |
795 | } |
638 | |
796 | |
639 | RETVAL = newRV_noinc ((SV *)result); |
797 | RETVAL = newRV_noinc ((SV *)result); |
640 | } |
798 | } |
641 | OUTPUT: |
799 | OUTPUT: |
642 | RETVAL |
800 | RETVAL |
643 | |
801 | |
644 | |
|
|