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

Comparing CV/CV.xs (file contents):
Revision 1.33 by root, Fri Feb 17 08:48:06 2006 UTC vs.
Revision 1.46 by root, Sat Jul 4 05:31:17 2015 UTC

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
32typedef char *octet_string;
27 33
28struct jpg_err_mgr 34struct 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;
68 74
69 *h &= 255; 75 *h &= 255;
70 } 76 }
71} 77}
72 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
73static guint32 a85_val; 134static guint32 a85_val;
74static guint a85_cnt; 135static guint a85_cnt;
75static guchar a85_buf[LINELENGTH], *a85_ptr; 136static guchar a85_buf[LINELENGTH], *a85_ptr;
76 137
77static void 138static void
147 208
148 OUTPUT: 209 OUTPUT:
149 RETVAL 210 RETVAL
150 211
151const char * 212const char *
152magic (const char *path) 213magic (octet_string path)
153 CODE: 214 CODE:
154{ 215{
155 static magic_t cookie; 216 static magic_t cookie;
156 217
157 if (!cookie) 218 if (!cookie)
158 { 219 {
159 cookie = magic_open (MAGIC_NONE); 220 cookie = magic_open (MAGIC_SYMLINK);
160 221
161 if (cookie) 222 if (cookie)
162 magic_load (cookie, 0); 223 magic_load (cookie, 0);
163 else 224 else
164 XSRETURN_UNDEF; 225 XSRETURN_UNDEF;
168} 229}
169 OUTPUT: 230 OUTPUT:
170 RETVAL 231 RETVAL
171 232
172const char * 233const char *
173magic_mime (const char *path) 234magic_mime (octet_string path)
174 CODE: 235 CODE:
175{ 236{
176 static magic_t cookie; 237 static magic_t cookie;
177 238
178 if (!cookie) 239 if (!cookie)
179 { 240 {
180 cookie = magic_open (MAGIC_MIME); 241 cookie = magic_open (MAGIC_MIME | MAGIC_SYMLINK);
181 242
182 if (cookie) 243 if (cookie)
183 magic_load (cookie, 0); 244 magic_load (cookie, 0);
184 else 245 else
185 XSRETURN_UNDEF; 246 XSRETURN_UNDEF;
186 } 247 }
187 248
249 perlinterp_release ();
188 RETVAL = magic_file (cookie, path); 250 RETVAL = magic_file (cookie, path);
251 perlinterp_acquire ();
189} 252}
190 OUTPUT: 253 OUTPUT:
191 RETVAL 254 RETVAL
192 255
193# 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);
194 262
195gboolean 263gboolean
196gdk_net_wm_supports (GdkAtom property) 264gdk_net_wm_supports (GdkAtom property)
197 CODE: 265 CODE:
198#if defined(GDK_WINDOWING_X11) && !defined(GDK_MULTIHEAD_SAFE) 266#if defined(GDK_WINDOWING_X11) && !defined(GDK_MULTIHEAD_SAFE)
204 RETVAL 272 RETVAL
205 273
206GdkPixbuf_noinc * 274GdkPixbuf_noinc *
207dealpha_expose (GdkPixbuf *pb) 275dealpha_expose (GdkPixbuf *pb)
208 CODE: 276 CODE:
277 perlinterp_release ();
209{ 278{
210 int w = gdk_pixbuf_get_width (pb); 279 int w = gdk_pixbuf_get_width (pb);
211 int h = gdk_pixbuf_get_height (pb); 280 int h = gdk_pixbuf_get_height (pb);
212 int bpp = gdk_pixbuf_get_n_channels (pb); 281 int bpp = gdk_pixbuf_get_n_channels (pb);
213 int x, y, i; 282 int x, y, i;
222 for (x = 0; x < w; x++) 291 for (x = 0; x < w; x++)
223 for (y = 0; y < h; y++) 292 for (y = 0; y < h; y++)
224 for (i = 0; i < 3; i++) 293 for (i = 0; i < 3; i++)
225 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];
226} 295}
296 perlinterp_acquire ();
227 OUTPUT: 297 OUTPUT:
228 RETVAL 298 RETVAL
229 299
230GdkPixbuf_noinc * 300GdkPixbuf_noinc *
231rotate (GdkPixbuf *pb, int angle) 301rotate (GdkPixbuf *pb, int angle)
232 CODE: 302 CODE:
303 perlinterp_release ();
304 if (angle < 0)
305 angle += 360;
233 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
234 : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE 307 : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE
235 : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN 308 : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN
236 : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE 309 : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE
237 : angle); 310 : angle);
311 perlinterp_acquire ();
238 OUTPUT: 312 OUTPUT:
239 RETVAL 313 RETVAL
240 314
241GdkPixbuf_noinc * 315GdkPixbuf_noinc *
242load_jpeg (SV *path, int thumbnail=0) 316load_jpeg (SV *path, int thumbnail=0)
254 fp = fopen (SvPVbyte_nolen (path), "rb"); 328 fp = fopen (SvPVbyte_nolen (path), "rb");
255 329
256 if (!fp) 330 if (!fp)
257 XSRETURN_UNDEF; 331 XSRETURN_UNDEF;
258 332
333 perlinterp_release ();
334
259 cinfo.err = jpeg_std_error (&jerr.err); 335 cinfo.err = jpeg_std_error (&jerr.err);
260 336
261 jerr.err.error_exit = cv_error_exit; 337 jerr.err.error_exit = cv_error_exit;
262 jerr.err.output_message = cv_error_output; 338 jerr.err.output_message = cv_error_output;
263 339
267 jpeg_destroy_decompress (&cinfo); 343 jpeg_destroy_decompress (&cinfo);
268 344
269 if (pb) 345 if (pb)
270 g_object_unref ((gpointer)pb); 346 g_object_unref ((gpointer)pb);
271 347
348 perlinterp_acquire ();
272 XSRETURN_UNDEF; 349 XSRETURN_UNDEF;
273 } 350 }
274 351
275 jpeg_create_decompress (&cinfo); 352 jpeg_create_decompress (&cinfo);
276 353
328 } 405 }
329 406
330 jpeg_finish_decompress (&cinfo); 407 jpeg_finish_decompress (&cinfo);
331 fclose (fp); 408 fclose (fp);
332 jpeg_destroy_decompress (&cinfo); 409 jpeg_destroy_decompress (&cinfo);
410 perlinterp_acquire ();
333} 411}
334 OUTPUT: 412 OUTPUT:
335 RETVAL 413 RETVAL
336 414
337void 415void
338compare (GdkPixbuf *a, GdkPixbuf *b) 416compare (GdkPixbuf *a, GdkPixbuf *b)
339 PPCODE: 417 PPCODE:
418 perlinterp_release ();
340{ 419{
341 int w = gdk_pixbuf_get_width (a); 420 int w = gdk_pixbuf_get_width (a);
342 int h = gdk_pixbuf_get_height (a); 421 int h = gdk_pixbuf_get_height (a);
422
343 int sa = gdk_pixbuf_get_rowstride (a); 423 int sa = gdk_pixbuf_get_rowstride (a);
344 int sb = gdk_pixbuf_get_rowstride (b); 424 int sb = gdk_pixbuf_get_rowstride (b);
345 425
346 guchar *pa = gdk_pixbuf_get_pixels (a); 426 guchar *pa = gdk_pixbuf_get_pixels (a);
347 guchar *pb = gdk_pixbuf_get_pixels (b); 427 guchar *pb = gdk_pixbuf_get_pixels (b);
355 assert (gdk_pixbuf_get_n_channels (b) == 3); 435 assert (gdk_pixbuf_get_n_channels (b) == 3);
356 436
357 double diff = 0.; 437 double diff = 0.;
358 int peak = 0; 438 int peak = 0;
359 439
440 if (w && h)
360 for (y = 0; y < h; y++) 441 for (y = 0; y < h; y++)
361 { 442 {
362 guchar *pa_ = pa + y * sa; 443 guchar *pa_ = pa + y * sa;
363 guchar *pb_ = pb + y * sb; 444 guchar *pb_ = pb + y * sb;
364 445
365 for (x = 0; x < w; x ++) 446 for (x = 0; x < w; x++)
366 { 447 {
367 int d; 448 int d;
368 449
369 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d)); 450 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
370 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));
371 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));
372 } 453 }
373 } 454 }
455
456 perlinterp_acquire ();
374 457
375 EXTEND (SP, 2); 458 EXTEND (SP, 2);
376 PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.))))); 459 PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.)))));
377 PUSHs (sv_2mortal (newSVnv (peak / 255.))); 460 PUSHs (sv_2mortal (newSVnv (peak / 255.)));
378} 461}
379 462
380############################################################################# 463#############################################################################
381 464
382MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer 465MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer
383 466
467# currently only works for filenames (octet strings)
468
384SV * 469SV *
385foldcase (SV *pathsv) 470foldcase (SV *pathsv)
386 PROTOTYPE: $ 471 PROTOTYPE: $
387 CODE: 472 CODE:
388{ 473{
389 STRLEN plen; 474 STRLEN plen;
390 U8 *path = (U8 *)SvPVutf8 (pathsv, plen); 475 U8 *path = (U8 *)SvPV (pathsv, plen);
391 U8 *pend = path + plen; 476 U8 *pend = path + plen;
392 U8 dst [plen * 6 * 3], *dstp = dst; 477 U8 dst [plen * 6 * 3], *dstp = dst;
393 478
394 while (path < pend) 479 while (path < pend)
395 { 480 {
396 U8 ch = *path; 481 U8 ch = *path;
397 482
398 if (ch >= 'a' && ch <= 'z') 483 if (ch >= 'a' && ch <= 'z')
399 *dstp++ = *path++; 484 *dstp++ = *path++;
485 else if (ch >= 'A' && ch <= 'Z')
486 *dstp++ = *path++ + ('a' - 'A');
400 else if (ch >= '0' && ch <= '9') 487 else if (ch >= '0' && ch <= '9')
401 { 488 {
402 STRLEN el, nl = 0; 489 STRLEN el, nl = 0;
403 while (*path >= '0' && *path <= '9' && path < pend) 490 while (*path >= '0' && *path <= '9' && path < pend)
404 path++, nl++; 491 path++, nl++;
407 *dstp++ = '0'; 494 *dstp++ = '0';
408 495
409 memcpy (dstp, path - nl, nl); 496 memcpy (dstp, path - nl, nl);
410 dstp += nl; 497 dstp += nl;
411 } 498 }
499 else
500 *dstp++ = *path++;
501#if 0
412 else 502 else
413 { 503 {
414 STRLEN cl; 504 STRLEN cl;
415 to_utf8_fold (path, dstp, &cl); 505 to_utf8_fold (path, dstp, &cl);
416 dstp += cl; 506 dstp += cl;
417 path += is_utf8_char (path); 507 path += is_utf8_char (path);
418 } 508 }
509#endif
419 } 510 }
420 511
421 RETVAL = newSVpvn ((const char *)dst, dstp - dst); 512 RETVAL = newSVpvn ((const char *)dst, dstp - dst);
422} 513}
423 OUTPUT: 514 OUTPUT:
585############################################################################# 676#############################################################################
586 677
587MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Plugin::RCluster 678MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Plugin::RCluster
588 679
589SV * 680SV *
590make_histograms (SV *ar) 681extract_features (SV *ar)
591 CODE: 682 CODE:
592{ 683{
593 int i; 684 int i;
594 AV *av, *result; 685 AV *av, *result;
595 686
596 if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV) 687 if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV)
597 croak ("Not an array ref as first argument to make_histogram"); 688 croak ("Not an array ref as first argument to extract_features");
598 689
599 av = (AV *) SvRV (ar); 690 av = (AV *) SvRV (ar);
600 result = newAV (); 691 result = newAV ();
601 692
602 for (i = 0; i <= av_len (av); ++i) 693 for (i = 0; i <= av_len (av); ++i)
603 { 694 {
604 const int HISTSIZE = 64;
605
606 int j;
607 SV *sv = *av_fetch (av, i, 1); 695 SV *sv = *av_fetch (av, i, 1);
608 STRLEN len;
609 char *buf = SvPVbyte (sv, len);
610
611 int tmphist[HISTSIZE];
612 float *hist;
613
614 SV *histsv = newSV (HISTSIZE * sizeof (float) + 1); 696 SV *histsv = newSV (9 * sizeof (float) + 1);
697
615 SvPOK_on (histsv); 698 SvPOK_on (histsv);
616 SvCUR_set (histsv, HISTSIZE * sizeof (float)); 699 SvCUR_set (histsv, 9 * sizeof (float));
617 hist = (float *)SvPVX (histsv); 700 float *hist = (float *)SvPVX (histsv);
618 701
619 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);
620 706
621 for (j = len; j--; )
622 {
623 unsigned int idx
624 = ((*buf & 0xc0) >> 2)
625 | ((*buf & 0x18) >> 1)
626 | (*buf & 0x03);
627
628 ++tmphist[idx];
629 ++buf;
630 }
631 707 {
632 for (j = 0; j < HISTSIZE; ++j) 708 STRLEN len;
633 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;
634 752
635 av_push (result, histsv); 753 av_push (result, histsv);
636 } 754 }
637 755
638 RETVAL = newRV_noinc ((SV *)result); 756 RETVAL = newRV_noinc ((SV *)result);
639} 757}
640 OUTPUT: 758 OUTPUT:
641 RETVAL 759 RETVAL
642 760
643

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines