ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CV/CV.xs
(Generate patch)

Comparing CV/CV.xs (file contents):
Revision 1.32 by root, Wed Feb 15 08:36:55 2006 UTC vs.
Revision 1.46 by root, Sat Jul 4 05:31:17 2015 UTC

2#include "perl.h" 2#include "perl.h"
3#include "XSUB.h" 3#include "XSUB.h"
4 4
5#include <string.h> 5#include <string.h>
6#include <setjmp.h> 6#include <setjmp.h>
7#include <math.h>
7 8
8#include <magic.h> 9#include <magic.h>
9 10
10#include <jpeglib.h> 11#include <jpeglib.h>
11#include <glib.h> 12#include <glib.h>
13#include <gdk-pixbuf/gdk-pixbuf.h> 14#include <gdk-pixbuf/gdk-pixbuf.h>
14 15
15#include <gperl.h> 16#include <gperl.h>
16#include <gtk2perl.h> 17#include <gtk2perl.h>
17 18
19#include <assert.h>
20
21#include "perlmulticore.h"
22
18#define IW 80 /* MUST match Schnauer.pm! */ 23#define IW 80 /* MUST match Schnauzer.pm! */
19#define IH 60 /* MUST match Schnauer.pm! */ 24#define IH 60 /* MUST match Schnauzer.pm! */
20 25
21#define RAND (seed = (seed + 7141) * 54773 % 134456) 26#define RAND (seed = (seed + 7141) * 54773 % 134456)
22 27
23#define LINELENGTH 240 28#define LINELENGTH 240
24 29
25#define ELLIPSIS "\xe2\x80\xa6" 30#define ELLIPSIS "\xe2\x80\xa6"
31
32typedef char *octet_string;
26 33
27struct jpg_err_mgr 34struct jpg_err_mgr
28{ 35{
29 struct jpeg_error_mgr err; 36 struct jpeg_error_mgr err;
30 jmp_buf setjmp_buffer; 37 jmp_buf setjmp_buffer;
67 74
68 *h &= 255; 75 *h &= 255;
69 } 76 }
70} 77}
71 78
79struct feature {
80 float v1, v2, v3; // mean, square, cube
81 int n;
82};
83
84static void
85feature_init (struct feature *f)
86{
87 f->v1 = 0.;
88 f->v2 = 0.;
89 f->v3 = 0.;
90 f->n = 0;
91}
92
93// didn't find an algorithm to neatly do mean, variance and skew in one pass.
94// elmex ist schuld.
95static void
96feature_update_pass_1 (struct feature *f, unsigned int v)
97{
98 f->v1 += v;
99 f->n += 1;
100}
101
102static void
103feature_finish_pass_1 (struct feature *f)
104{
105 if (f->n < 1)
106 return;
107
108 f->v1 /= f->n;
109}
110
111static void
112feature_update_pass_2 (struct feature *f, unsigned int v)
113{
114 float d = v - f->v1;
115
116 f->v2 += d * d;
117 f->v3 += d * d * d;
118}
119
120static void
121feature_finish_pass_2 (struct feature *f)
122{
123 if (f->n < 1)
124 return;
125
126 f->v2 /= f->n;
127 f->v3 /= f->n;
128
129 f->v1 /= 255.;
130 f->v2 /= 255. * 255.; f->v2 = sqrtf (f->v2);
131 f->v3 /= 255. * 255. * 255.; f->v3 = powf (fabsf (f->v3), 1./3.);
132}
133
72static guint32 a85_val; 134static guint32 a85_val;
73static guint a85_cnt; 135static guint a85_cnt;
74static guchar a85_buf[LINELENGTH], *a85_ptr; 136static guchar a85_buf[LINELENGTH], *a85_ptr;
75 137
76static void 138static void
146 208
147 OUTPUT: 209 OUTPUT:
148 RETVAL 210 RETVAL
149 211
150const char * 212const char *
151magic (const char *path) 213magic (octet_string path)
152 CODE: 214 CODE:
153{ 215{
154 static magic_t cookie; 216 static magic_t cookie;
155 217
156 if (!cookie) 218 if (!cookie)
157 { 219 {
158 cookie = magic_open (MAGIC_NONE); 220 cookie = magic_open (MAGIC_SYMLINK);
159 221
160 if (cookie) 222 if (cookie)
161 magic_load (cookie, 0); 223 magic_load (cookie, 0);
162 else 224 else
163 XSRETURN_UNDEF; 225 XSRETURN_UNDEF;
167} 229}
168 OUTPUT: 230 OUTPUT:
169 RETVAL 231 RETVAL
170 232
171const char * 233const char *
172magic_mime (const char *path) 234magic_mime (octet_string path)
173 CODE: 235 CODE:
174{ 236{
175 static magic_t cookie; 237 static magic_t cookie;
176 238
177 if (!cookie) 239 if (!cookie)
178 { 240 {
179 cookie = magic_open (MAGIC_MIME); 241 cookie = magic_open (MAGIC_MIME | MAGIC_SYMLINK);
180 242
181 if (cookie) 243 if (cookie)
182 magic_load (cookie, 0); 244 magic_load (cookie, 0);
183 else 245 else
184 XSRETURN_UNDEF; 246 XSRETURN_UNDEF;
185 } 247 }
186 248
249 perlinterp_release ();
187 RETVAL = magic_file (cookie, path); 250 RETVAL = magic_file (cookie, path);
251 perlinterp_acquire ();
188} 252}
189 OUTPUT: 253 OUTPUT:
190 RETVAL 254 RETVAL
191 255
192# missing in Gtk2 perl module 256# missing/broken in Gtk2 perl module
257
258void
259gdk_window_clear_hints (GdkWindow *window)
260 CODE:
261 gdk_window_set_geometry_hints (window, 0, 0);
193 262
194gboolean 263gboolean
195gdk_net_wm_supports (GdkAtom property) 264gdk_net_wm_supports (GdkAtom property)
196 CODE: 265 CODE:
197#if defined(GDK_WINDOWING_X11) && !defined(GDK_MULTIHEAD_SAFE) 266#if defined(GDK_WINDOWING_X11) && !defined(GDK_MULTIHEAD_SAFE)
203 RETVAL 272 RETVAL
204 273
205GdkPixbuf_noinc * 274GdkPixbuf_noinc *
206dealpha_expose (GdkPixbuf *pb) 275dealpha_expose (GdkPixbuf *pb)
207 CODE: 276 CODE:
277 perlinterp_release ();
208{ 278{
209 int w = gdk_pixbuf_get_width (pb); 279 int w = gdk_pixbuf_get_width (pb);
210 int h = gdk_pixbuf_get_height (pb); 280 int h = gdk_pixbuf_get_height (pb);
211 int bpp = gdk_pixbuf_get_n_channels (pb); 281 int bpp = gdk_pixbuf_get_n_channels (pb);
212 int x, y, i; 282 int x, y, i;
221 for (x = 0; x < w; x++) 291 for (x = 0; x < w; x++)
222 for (y = 0; y < h; y++) 292 for (y = 0; y < h; y++)
223 for (i = 0; i < 3; i++) 293 for (i = 0; i < 3; i++)
224 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];
225} 295}
296 perlinterp_acquire ();
226 OUTPUT: 297 OUTPUT:
227 RETVAL 298 RETVAL
228 299
229GdkPixbuf_noinc * 300GdkPixbuf_noinc *
230rotate (GdkPixbuf *pb, int angle) 301rotate (GdkPixbuf *pb, int angle)
231 CODE: 302 CODE:
303 perlinterp_release ();
304 if (angle < 0)
305 angle += 360;
232 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
233 : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE 307 : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE
234 : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN 308 : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN
235 : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE 309 : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE
236 : angle); 310 : angle);
311 perlinterp_acquire ();
237 OUTPUT: 312 OUTPUT:
238 RETVAL 313 RETVAL
239 314
240GdkPixbuf_noinc * 315GdkPixbuf_noinc *
241load_jpeg (SV *path, int thumbnail=0) 316load_jpeg (SV *path, int thumbnail=0)
253 fp = fopen (SvPVbyte_nolen (path), "rb"); 328 fp = fopen (SvPVbyte_nolen (path), "rb");
254 329
255 if (!fp) 330 if (!fp)
256 XSRETURN_UNDEF; 331 XSRETURN_UNDEF;
257 332
333 perlinterp_release ();
334
258 cinfo.err = jpeg_std_error (&jerr.err); 335 cinfo.err = jpeg_std_error (&jerr.err);
259 336
260 jerr.err.error_exit = cv_error_exit; 337 jerr.err.error_exit = cv_error_exit;
261 jerr.err.output_message = cv_error_output; 338 jerr.err.output_message = cv_error_output;
262 339
266 jpeg_destroy_decompress (&cinfo); 343 jpeg_destroy_decompress (&cinfo);
267 344
268 if (pb) 345 if (pb)
269 g_object_unref ((gpointer)pb); 346 g_object_unref ((gpointer)pb);
270 347
348 perlinterp_acquire ();
271 XSRETURN_UNDEF; 349 XSRETURN_UNDEF;
272 } 350 }
273 351
274 jpeg_create_decompress (&cinfo); 352 jpeg_create_decompress (&cinfo);
275 353
327 } 405 }
328 406
329 jpeg_finish_decompress (&cinfo); 407 jpeg_finish_decompress (&cinfo);
330 fclose (fp); 408 fclose (fp);
331 jpeg_destroy_decompress (&cinfo); 409 jpeg_destroy_decompress (&cinfo);
410 perlinterp_acquire ();
332} 411}
333 OUTPUT: 412 OUTPUT:
334 RETVAL 413 RETVAL
414
415void
416compare (GdkPixbuf *a, GdkPixbuf *b)
417 PPCODE:
418 perlinterp_release ();
419{
420 int w = gdk_pixbuf_get_width (a);
421 int h = gdk_pixbuf_get_height (a);
422
423 int sa = gdk_pixbuf_get_rowstride (a);
424 int sb = gdk_pixbuf_get_rowstride (b);
425
426 guchar *pa = gdk_pixbuf_get_pixels (a);
427 guchar *pb = gdk_pixbuf_get_pixels (b);
428
429 int x, y;
430
431 assert (w == gdk_pixbuf_get_width (b));
432 assert (h == gdk_pixbuf_get_height (b));
433
434 assert (gdk_pixbuf_get_n_channels (a) == 3);
435 assert (gdk_pixbuf_get_n_channels (b) == 3);
436
437 double diff = 0.;
438 int peak = 0;
439
440 if (w && h)
441 for (y = 0; y < h; y++)
442 {
443 guchar *pa_ = pa + y * sa;
444 guchar *pb_ = pb + y * sb;
445
446 for (x = 0; x < w; x++)
447 {
448 int d;
449
450 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));
452 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
453 }
454 }
455
456 perlinterp_acquire ();
457
458 EXTEND (SP, 2);
459 PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.)))));
460 PUSHs (sv_2mortal (newSVnv (peak / 255.)));
461}
335 462
336############################################################################# 463#############################################################################
337 464
338MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer 465MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer
466
467# currently only works for filenames (octet strings)
339 468
340SV * 469SV *
341foldcase (SV *pathsv) 470foldcase (SV *pathsv)
342 PROTOTYPE: $ 471 PROTOTYPE: $
343 CODE: 472 CODE:
344{ 473{
345 STRLEN plen; 474 STRLEN plen;
346 U8 *path = (U8 *)SvPVutf8 (pathsv, plen); 475 U8 *path = (U8 *)SvPV (pathsv, plen);
347 U8 *pend = path + plen; 476 U8 *pend = path + plen;
348 U8 dst [plen * 6 * 3], *dstp = dst; 477 U8 dst [plen * 6 * 3], *dstp = dst;
349 478
350 while (path < pend) 479 while (path < pend)
351 { 480 {
352 U8 ch = *path; 481 U8 ch = *path;
353 482
354 if (ch >= 'a' && ch <= 'z') 483 if (ch >= 'a' && ch <= 'z')
355 *dstp++ = *path++; 484 *dstp++ = *path++;
485 else if (ch >= 'A' && ch <= 'Z')
486 *dstp++ = *path++ + ('a' - 'A');
356 else if (ch >= '0' && ch <= '9') 487 else if (ch >= '0' && ch <= '9')
357 { 488 {
358 STRLEN el, nl = 0; 489 STRLEN el, nl = 0;
359 while (*path >= '0' && *path <= '9' && path < pend) 490 while (*path >= '0' && *path <= '9' && path < pend)
360 path++, nl++; 491 path++, nl++;
363 *dstp++ = '0'; 494 *dstp++ = '0';
364 495
365 memcpy (dstp, path - nl, nl); 496 memcpy (dstp, path - nl, nl);
366 dstp += nl; 497 dstp += nl;
367 } 498 }
499 else
500 *dstp++ = *path++;
501#if 0
368 else 502 else
369 { 503 {
370 STRLEN cl; 504 STRLEN cl;
371 to_utf8_fold (path, dstp, &cl); 505 to_utf8_fold (path, dstp, &cl);
372 dstp += cl; 506 dstp += cl;
373 path += is_utf8_char (path); 507 path += is_utf8_char (path);
374 } 508 }
509#endif
375 } 510 }
376 511
377 RETVAL = newSVpvn ((const char *)dst, dstp - dst); 512 RETVAL = newSVpvn ((const char *)dst, dstp - dst);
378} 513}
379 OUTPUT: 514 OUTPUT:
541############################################################################# 676#############################################################################
542 677
543MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Plugin::RCluster 678MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Plugin::RCluster
544 679
545SV * 680SV *
546make_histograms (SV *ar) 681extract_features (SV *ar)
547 CODE: 682 CODE:
548{ 683{
549 int i; 684 int i;
550 AV *av, *result; 685 AV *av, *result;
551 686
552 if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV) 687 if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV)
553 croak ("Not an array ref as first argument to make_histogram"); 688 croak ("Not an array ref as first argument to extract_features");
554 689
555 av = (AV *) SvRV (ar); 690 av = (AV *) SvRV (ar);
556 result = newAV (); 691 result = newAV ();
557 692
558 for (i = 0; i <= av_len (av); ++i) 693 for (i = 0; i <= av_len (av); ++i)
559 { 694 {
560 const int HISTSIZE = 64;
561
562 int j;
563 SV *sv = *av_fetch (av, i, 1); 695 SV *sv = *av_fetch (av, i, 1);
564 STRLEN len;
565 char *buf = SvPVbyte (sv, len);
566
567 int tmphist[HISTSIZE];
568 float *hist;
569
570 SV *histsv = newSV (HISTSIZE * sizeof (float) + 1); 696 SV *histsv = newSV (9 * sizeof (float) + 1);
697
571 SvPOK_on (histsv); 698 SvPOK_on (histsv);
572 SvCUR_set (histsv, HISTSIZE * sizeof (float)); 699 SvCUR_set (histsv, 9 * sizeof (float));
573 hist = (float *)SvPVX (histsv); 700 float *hist = (float *)SvPVX (histsv);
574 701
575 Zero (tmphist, sizeof (tmphist), char); 702 struct feature f_h, f_s, f_v;
703 feature_init (&f_h);
704 feature_init (&f_s);
705 feature_init (&f_v);
576 706
577 for (j = len; j--; )
578 {
579 unsigned int idx
580 = ((*buf & 0xc0) >> 2)
581 | ((*buf & 0x18) >> 1)
582 | (*buf & 0x03);
583
584 ++tmphist[idx];
585 ++buf;
586 }
587 707 {
588 for (j = 0; j < HISTSIZE; ++j) 708 STRLEN len;
589 hist[j] = (float)tmphist[j] / (len + 1e-30); 709 unsigned char *buf = (unsigned char *)SvPVbyte (sv, len);
710 while (len >= 3)
711 {
712 unsigned int r, g, b, h, s, v;
713 r = *buf++; g = *buf++; b = *buf++;
714 rgb_to_hsv (r, g, b, &h, &s, &v);
715
716 feature_update_pass_1 (&f_h, h);
717 feature_update_pass_1 (&f_s, s);
718 feature_update_pass_1 (&f_v, v);
719
720 len -= 3;
721 }
722
723 feature_finish_pass_1 (&f_h);
724 feature_finish_pass_1 (&f_s);
725 feature_finish_pass_1 (&f_v);
726 }
727
728 {
729 STRLEN len;
730 unsigned char *buf = (unsigned char *)SvPVbyte (sv, len);
731 while (len >= 3)
732 {
733 unsigned int r, g, b, h, s, v;
734 r = *buf++; g = *buf++; b = *buf++;
735 rgb_to_hsv (r, g, b, &h, &s, &v);
736
737 feature_update_pass_2 (&f_h, h);
738 feature_update_pass_2 (&f_s, s);
739 feature_update_pass_2 (&f_v, v);
740
741 len -= 3;
742 }
743
744 feature_finish_pass_2 (&f_h);
745 feature_finish_pass_2 (&f_s);
746 feature_finish_pass_2 (&f_v);
747 }
748
749 hist [0] = f_h.v1 * 2.; hist [1] = f_h.v2 * 2.; hist [2] = f_h.v3 * 2.;
750 hist [3] = f_s.v1 ; hist [4] = f_s.v2 ; hist [5] = f_s.v3 ;
751 hist [6] = f_v.v1 * .5; hist [7] = f_v.v2 * .5; hist [8] = f_v.v3 * .5;
590 752
591 av_push (result, histsv); 753 av_push (result, histsv);
592 } 754 }
593 755
594 RETVAL = newRV_noinc ((SV *)result); 756 RETVAL = newRV_noinc ((SV *)result);
595} 757}
596 OUTPUT: 758 OUTPUT:
597 RETVAL 759 RETVAL
598 760
599

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines