… | |
… | |
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; |
… | |
… | |
202 | |
208 | |
203 | OUTPUT: |
209 | OUTPUT: |
204 | RETVAL |
210 | RETVAL |
205 | |
211 | |
206 | const char * |
212 | const char * |
207 | magic (const char *path) |
213 | magic (octet_string path) |
208 | CODE: |
214 | CODE: |
209 | { |
215 | { |
210 | static magic_t cookie; |
216 | static magic_t cookie; |
211 | |
217 | |
212 | if (!cookie) |
218 | if (!cookie) |
213 | { |
219 | { |
214 | cookie = magic_open (MAGIC_NONE); |
220 | cookie = magic_open (MAGIC_SYMLINK); |
215 | |
221 | |
216 | if (cookie) |
222 | if (cookie) |
217 | magic_load (cookie, 0); |
223 | magic_load (cookie, 0); |
218 | else |
224 | else |
219 | XSRETURN_UNDEF; |
225 | XSRETURN_UNDEF; |
… | |
… | |
223 | } |
229 | } |
224 | OUTPUT: |
230 | OUTPUT: |
225 | RETVAL |
231 | RETVAL |
226 | |
232 | |
227 | const char * |
233 | const char * |
228 | magic_mime (const char *path) |
234 | magic_mime (octet_string path) |
229 | CODE: |
235 | CODE: |
230 | { |
236 | { |
231 | static magic_t cookie; |
237 | static magic_t cookie; |
232 | |
238 | |
233 | if (!cookie) |
239 | if (!cookie) |
234 | { |
240 | { |
235 | cookie = magic_open (MAGIC_MIME); |
241 | cookie = magic_open (MAGIC_MIME | MAGIC_SYMLINK); |
236 | |
242 | |
237 | if (cookie) |
243 | if (cookie) |
238 | magic_load (cookie, 0); |
244 | magic_load (cookie, 0); |
239 | else |
245 | else |
240 | XSRETURN_UNDEF; |
246 | XSRETURN_UNDEF; |
241 | } |
247 | } |
242 | |
248 | |
|
|
249 | perlinterp_release (); |
243 | RETVAL = magic_file (cookie, path); |
250 | RETVAL = magic_file (cookie, path); |
|
|
251 | perlinterp_acquire (); |
244 | } |
252 | } |
245 | OUTPUT: |
253 | OUTPUT: |
246 | RETVAL |
254 | RETVAL |
247 | |
255 | |
248 | # missing in Gtk2 perl module |
256 | # missing/broken in Gtk2 perl module |
|
|
257 | |
|
|
258 | void |
|
|
259 | gdk_window_clear_hints (GdkWindow *window) |
|
|
260 | CODE: |
|
|
261 | gdk_window_set_geometry_hints (window, 0, 0); |
249 | |
262 | |
250 | gboolean |
263 | gboolean |
251 | gdk_net_wm_supports (GdkAtom property) |
264 | gdk_net_wm_supports (GdkAtom property) |
252 | CODE: |
265 | CODE: |
253 | #if defined(GDK_WINDOWING_X11) && !defined(GDK_MULTIHEAD_SAFE) |
266 | #if defined(GDK_WINDOWING_X11) && !defined(GDK_MULTIHEAD_SAFE) |
… | |
… | |
259 | RETVAL |
272 | RETVAL |
260 | |
273 | |
261 | GdkPixbuf_noinc * |
274 | GdkPixbuf_noinc * |
262 | dealpha_expose (GdkPixbuf *pb) |
275 | dealpha_expose (GdkPixbuf *pb) |
263 | CODE: |
276 | CODE: |
|
|
277 | perlinterp_release (); |
264 | { |
278 | { |
265 | int w = gdk_pixbuf_get_width (pb); |
279 | int w = gdk_pixbuf_get_width (pb); |
266 | int h = gdk_pixbuf_get_height (pb); |
280 | int h = gdk_pixbuf_get_height (pb); |
267 | int bpp = gdk_pixbuf_get_n_channels (pb); |
281 | int bpp = gdk_pixbuf_get_n_channels (pb); |
268 | int x, y, i; |
282 | int x, y, i; |
… | |
… | |
277 | for (x = 0; x < w; x++) |
291 | for (x = 0; x < w; x++) |
278 | for (y = 0; y < h; y++) |
292 | for (y = 0; y < h; y++) |
279 | for (i = 0; i < 3; i++) |
293 | for (i = 0; i < 3; i++) |
280 | dst[x * 3 + y * dstr + i] = src[x * bpp + y * sstr + i]; |
294 | dst[x * 3 + y * dstr + i] = src[x * bpp + y * sstr + i]; |
281 | } |
295 | } |
|
|
296 | perlinterp_acquire (); |
282 | OUTPUT: |
297 | OUTPUT: |
283 | RETVAL |
298 | RETVAL |
284 | |
299 | |
285 | GdkPixbuf_noinc * |
300 | GdkPixbuf_noinc * |
286 | rotate (GdkPixbuf *pb, int angle) |
301 | rotate (GdkPixbuf *pb, int angle) |
287 | CODE: |
302 | CODE: |
|
|
303 | perlinterp_release (); |
|
|
304 | if (angle < 0) |
|
|
305 | angle += 360; |
288 | RETVAL = gdk_pixbuf_rotate_simple (pb, angle == 0 ? GDK_PIXBUF_ROTATE_NONE |
306 | RETVAL = gdk_pixbuf_rotate_simple (pb, angle == 0 ? GDK_PIXBUF_ROTATE_NONE |
289 | : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE |
307 | : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE |
290 | : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN |
308 | : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN |
291 | : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE |
309 | : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE |
292 | : angle); |
310 | : angle); |
|
|
311 | perlinterp_acquire (); |
293 | OUTPUT: |
312 | OUTPUT: |
294 | RETVAL |
313 | RETVAL |
295 | |
314 | |
296 | GdkPixbuf_noinc * |
315 | GdkPixbuf_noinc * |
297 | load_jpeg (SV *path, int thumbnail=0) |
316 | load_jpeg (SV *path, int thumbnail=0) |
… | |
… | |
309 | fp = fopen (SvPVbyte_nolen (path), "rb"); |
328 | fp = fopen (SvPVbyte_nolen (path), "rb"); |
310 | |
329 | |
311 | if (!fp) |
330 | if (!fp) |
312 | XSRETURN_UNDEF; |
331 | XSRETURN_UNDEF; |
313 | |
332 | |
|
|
333 | perlinterp_release (); |
|
|
334 | |
314 | cinfo.err = jpeg_std_error (&jerr.err); |
335 | cinfo.err = jpeg_std_error (&jerr.err); |
315 | |
336 | |
316 | jerr.err.error_exit = cv_error_exit; |
337 | jerr.err.error_exit = cv_error_exit; |
317 | jerr.err.output_message = cv_error_output; |
338 | jerr.err.output_message = cv_error_output; |
318 | |
339 | |
… | |
… | |
322 | jpeg_destroy_decompress (&cinfo); |
343 | jpeg_destroy_decompress (&cinfo); |
323 | |
344 | |
324 | if (pb) |
345 | if (pb) |
325 | g_object_unref ((gpointer)pb); |
346 | g_object_unref ((gpointer)pb); |
326 | |
347 | |
|
|
348 | perlinterp_acquire (); |
327 | XSRETURN_UNDEF; |
349 | XSRETURN_UNDEF; |
328 | } |
350 | } |
329 | |
351 | |
330 | jpeg_create_decompress (&cinfo); |
352 | jpeg_create_decompress (&cinfo); |
331 | |
353 | |
… | |
… | |
383 | } |
405 | } |
384 | |
406 | |
385 | jpeg_finish_decompress (&cinfo); |
407 | jpeg_finish_decompress (&cinfo); |
386 | fclose (fp); |
408 | fclose (fp); |
387 | jpeg_destroy_decompress (&cinfo); |
409 | jpeg_destroy_decompress (&cinfo); |
|
|
410 | perlinterp_acquire (); |
388 | } |
411 | } |
389 | OUTPUT: |
412 | OUTPUT: |
390 | RETVAL |
413 | RETVAL |
391 | |
414 | |
392 | void |
415 | void |
393 | compare (GdkPixbuf *a, GdkPixbuf *b) |
416 | compare (GdkPixbuf *a, GdkPixbuf *b) |
394 | PPCODE: |
417 | PPCODE: |
|
|
418 | perlinterp_release (); |
395 | { |
419 | { |
396 | int w = gdk_pixbuf_get_width (a); |
420 | int w = gdk_pixbuf_get_width (a); |
397 | int h = gdk_pixbuf_get_height (a); |
421 | int h = gdk_pixbuf_get_height (a); |
|
|
422 | |
398 | int sa = gdk_pixbuf_get_rowstride (a); |
423 | int sa = gdk_pixbuf_get_rowstride (a); |
399 | int sb = gdk_pixbuf_get_rowstride (b); |
424 | int sb = gdk_pixbuf_get_rowstride (b); |
400 | |
425 | |
401 | guchar *pa = gdk_pixbuf_get_pixels (a); |
426 | guchar *pa = gdk_pixbuf_get_pixels (a); |
402 | guchar *pb = gdk_pixbuf_get_pixels (b); |
427 | guchar *pb = gdk_pixbuf_get_pixels (b); |
… | |
… | |
426 | d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d)); |
451 | d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d)); |
427 | d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d)); |
452 | d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d)); |
428 | } |
453 | } |
429 | } |
454 | } |
430 | |
455 | |
|
|
456 | perlinterp_acquire (); |
|
|
457 | |
431 | EXTEND (SP, 2); |
458 | EXTEND (SP, 2); |
432 | PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.))))); |
459 | PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.))))); |
433 | PUSHs (sv_2mortal (newSVnv (peak / 255.))); |
460 | PUSHs (sv_2mortal (newSVnv (peak / 255.))); |
434 | } |
461 | } |
435 | |
462 | |
436 | ############################################################################# |
463 | ############################################################################# |
437 | |
464 | |
438 | MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer |
465 | MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer |
439 | |
466 | |
|
|
467 | # currently only works for filenames (octet strings) |
|
|
468 | |
440 | SV * |
469 | SV * |
441 | foldcase (SV *pathsv) |
470 | foldcase (SV *pathsv) |
442 | PROTOTYPE: $ |
471 | PROTOTYPE: $ |
443 | CODE: |
472 | CODE: |
444 | { |
473 | { |
445 | STRLEN plen; |
474 | STRLEN plen; |
446 | U8 *path = (U8 *)SvPVutf8 (pathsv, plen); |
475 | U8 *path = (U8 *)SvPV (pathsv, plen); |
447 | U8 *pend = path + plen; |
476 | U8 *pend = path + plen; |
448 | U8 dst [plen * 6 * 3], *dstp = dst; |
477 | U8 dst [plen * 6 * 3], *dstp = dst; |
449 | |
478 | |
450 | while (path < pend) |
479 | while (path < pend) |
451 | { |
480 | { |
452 | U8 ch = *path; |
481 | U8 ch = *path; |
453 | |
482 | |
454 | if (ch >= 'a' && ch <= 'z') |
483 | if (ch >= 'a' && ch <= 'z') |
455 | *dstp++ = *path++; |
484 | *dstp++ = *path++; |
|
|
485 | else if (ch >= 'A' && ch <= 'Z') |
|
|
486 | *dstp++ = *path++ + ('a' - 'A'); |
456 | else if (ch >= '0' && ch <= '9') |
487 | else if (ch >= '0' && ch <= '9') |
457 | { |
488 | { |
458 | STRLEN el, nl = 0; |
489 | STRLEN el, nl = 0; |
459 | while (*path >= '0' && *path <= '9' && path < pend) |
490 | while (*path >= '0' && *path <= '9' && path < pend) |
460 | path++, nl++; |
491 | path++, nl++; |
… | |
… | |
463 | *dstp++ = '0'; |
494 | *dstp++ = '0'; |
464 | |
495 | |
465 | memcpy (dstp, path - nl, nl); |
496 | memcpy (dstp, path - nl, nl); |
466 | dstp += nl; |
497 | dstp += nl; |
467 | } |
498 | } |
|
|
499 | else |
|
|
500 | *dstp++ = *path++; |
|
|
501 | #if 0 |
468 | else |
502 | else |
469 | { |
503 | { |
470 | STRLEN cl; |
504 | STRLEN cl; |
471 | to_utf8_fold (path, dstp, &cl); |
505 | to_utf8_fold (path, dstp, &cl); |
472 | dstp += cl; |
506 | dstp += cl; |
473 | path += is_utf8_char (path); |
507 | path += is_utf8_char (path); |
474 | } |
508 | } |
|
|
509 | #endif |
475 | } |
510 | } |
476 | |
511 | |
477 | RETVAL = newSVpvn ((const char *)dst, dstp - dst); |
512 | RETVAL = newSVpvn ((const char *)dst, dstp - dst); |
478 | } |
513 | } |
479 | OUTPUT: |
514 | OUTPUT: |
… | |
… | |
648 | { |
683 | { |
649 | int i; |
684 | int i; |
650 | AV *av, *result; |
685 | AV *av, *result; |
651 | |
686 | |
652 | if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV) |
687 | if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV) |
653 | croak ("Not an array ref as first argument to make_histogram"); |
688 | croak ("Not an array ref as first argument to extract_features"); |
654 | |
689 | |
655 | av = (AV *) SvRV (ar); |
690 | av = (AV *) SvRV (ar); |
656 | result = newAV (); |
691 | result = newAV (); |
657 | |
692 | |
658 | for (i = 0; i <= av_len (av); ++i) |
693 | for (i = 0; i <= av_len (av); ++i) |
… | |
… | |
721 | RETVAL = newRV_noinc ((SV *)result); |
756 | RETVAL = newRV_noinc ((SV *)result); |
722 | } |
757 | } |
723 | OUTPUT: |
758 | OUTPUT: |
724 | RETVAL |
759 | RETVAL |
725 | |
760 | |
726 | SV * |
|
|
727 | make_histograms (SV *ar) |
|
|
728 | CODE: |
|
|
729 | { |
|
|
730 | int i; |
|
|
731 | AV *av, *result; |
|
|
732 | |
|
|
733 | if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV) |
|
|
734 | croak ("Not an array ref as first argument to make_histogram"); |
|
|
735 | |
|
|
736 | av = (AV *) SvRV (ar); |
|
|
737 | result = newAV (); |
|
|
738 | |
|
|
739 | for (i = 0; i <= av_len (av); ++i) |
|
|
740 | { |
|
|
741 | const int HISTSIZE = 64; |
|
|
742 | |
|
|
743 | int j; |
|
|
744 | SV *sv = *av_fetch (av, i, 1); |
|
|
745 | STRLEN len; |
|
|
746 | char *buf = SvPVbyte (sv, len); |
|
|
747 | |
|
|
748 | int tmphist[HISTSIZE]; |
|
|
749 | float *hist; |
|
|
750 | |
|
|
751 | SV *histsv = newSV (HISTSIZE * sizeof (float) + 1); |
|
|
752 | SvPOK_on (histsv); |
|
|
753 | SvCUR_set (histsv, HISTSIZE * sizeof (float)); |
|
|
754 | hist = (float *)SvPVX (histsv); |
|
|
755 | |
|
|
756 | Zero (tmphist, sizeof (tmphist), char); |
|
|
757 | |
|
|
758 | for (j = len; j--; ) |
|
|
759 | { |
|
|
760 | unsigned int idx |
|
|
761 | = ((*buf & 0xc0) >> 2) |
|
|
762 | | ((*buf & 0x18) >> 1) |
|
|
763 | | (*buf & 0x03); |
|
|
764 | |
|
|
765 | ++tmphist[idx]; |
|
|
766 | ++buf; |
|
|
767 | } |
|
|
768 | |
|
|
769 | for (j = 0; j < HISTSIZE; ++j) |
|
|
770 | hist[j] = (float)tmphist[j] / (len + 1e-30); |
|
|
771 | |
|
|
772 | av_push (result, histsv); |
|
|
773 | } |
|
|
774 | |
|
|
775 | RETVAL = newRV_noinc ((SV *)result); |
|
|
776 | } |
|
|
777 | OUTPUT: |
|
|
778 | RETVAL |
|
|
779 | |
|
|
780 | |
|
|