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

Comparing CV/CV.xs (file contents):
Revision 1.36 by root, Thu Jun 22 20:50:05 2006 UTC vs.
Revision 1.48 by root, Wed May 24 05:00:57 2017 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;
32}; 38};
33 39
34static void 40static void
35cv_error_exit (j_common_ptr cinfo) 41cv_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
40static void 47static void
41cv_error_output (j_common_ptr cinfo) 48cv_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
46static void 58static void
47rgb_to_hsv (unsigned int r, unsigned int g, unsigned int b, 59rgb_to_hsv (unsigned int r, unsigned int g, unsigned int b,
118 return; 130 return;
119 131
120 f->v2 /= f->n; 132 f->v2 /= f->n;
121 f->v3 /= f->n; 133 f->v3 /= f->n;
122 134
123 // this is a peculiar weightung, but it works fine
124 f->v1 /= 255.; 135 f->v1 /= 255.;
125 f->v2 /= 128. * 128.; 136 f->v2 /= 255. * 255.; f->v2 = sqrtf (f->v2);
126 f->v3 /= 128. * 128. * 64.; 137 f->v3 /= 255. * 255. * 255.; f->v3 = powf (fabsf (f->v3), 1./3.);
127} 138}
128 139
129static guint32 a85_val; 140static guint32 a85_val;
130static guint a85_cnt; 141static guint a85_cnt;
131static guchar a85_buf[LINELENGTH], *a85_ptr; 142static guchar a85_buf[LINELENGTH], *a85_ptr;
203 214
204 OUTPUT: 215 OUTPUT:
205 RETVAL 216 RETVAL
206 217
207const char * 218const char *
208magic (const char *path) 219magic (octet_string path)
209 CODE: 220 CODE:
210{ 221{
211 static magic_t cookie; 222 static magic_t cookie;
212 223
213 if (!cookie) 224 if (!cookie)
214 { 225 {
215 cookie = magic_open (MAGIC_NONE); 226 cookie = magic_open (MAGIC_SYMLINK);
216 227
217 if (cookie) 228 if (cookie)
218 magic_load (cookie, 0); 229 magic_load (cookie, 0);
219 else 230 else
220 XSRETURN_UNDEF; 231 XSRETURN_UNDEF;
224} 235}
225 OUTPUT: 236 OUTPUT:
226 RETVAL 237 RETVAL
227 238
228const char * 239const char *
229magic_mime (const char *path) 240magic_mime (octet_string path)
230 CODE: 241 CODE:
231{ 242{
232 static magic_t cookie; 243 static magic_t cookie;
233 244
234 if (!cookie) 245 if (!cookie)
235 { 246 {
236 cookie = magic_open (MAGIC_MIME); 247 cookie = magic_open (MAGIC_MIME | MAGIC_SYMLINK);
237 248
238 if (cookie) 249 if (cookie)
239 magic_load (cookie, 0); 250 magic_load (cookie, 0);
240 else 251 else
241 XSRETURN_UNDEF; 252 XSRETURN_UNDEF;
242 } 253 }
243 254
255 perlinterp_release ();
244 RETVAL = magic_file (cookie, path); 256 RETVAL = magic_file (cookie, path);
257 perlinterp_acquire ();
245} 258}
246 OUTPUT: 259 OUTPUT:
247 RETVAL 260 RETVAL
248 261
249# missing in Gtk2 perl module 262# missing/broken in Gtk2 perl module
263
264void
265gdk_window_clear_hints (GdkWindow *window)
266 CODE:
267 gdk_window_set_geometry_hints (window, 0, 0);
250 268
251gboolean 269gboolean
252gdk_net_wm_supports (GdkAtom property) 270gdk_net_wm_supports (GdkAtom property)
253 CODE: 271 CODE:
254#if defined(GDK_WINDOWING_X11) && !defined(GDK_MULTIHEAD_SAFE) 272#if defined(GDK_WINDOWING_X11) && !defined(GDK_MULTIHEAD_SAFE)
260 RETVAL 278 RETVAL
261 279
262GdkPixbuf_noinc * 280GdkPixbuf_noinc *
263dealpha_expose (GdkPixbuf *pb) 281dealpha_expose (GdkPixbuf *pb)
264 CODE: 282 CODE:
283 perlinterp_release ();
265{ 284{
266 int w = gdk_pixbuf_get_width (pb); 285 int w = gdk_pixbuf_get_width (pb);
267 int h = gdk_pixbuf_get_height (pb); 286 int h = gdk_pixbuf_get_height (pb);
268 int bpp = gdk_pixbuf_get_n_channels (pb); 287 int bpp = gdk_pixbuf_get_n_channels (pb);
269 int x, y, i; 288 int x, y, i;
278 for (x = 0; x < w; x++) 297 for (x = 0; x < w; x++)
279 for (y = 0; y < h; y++) 298 for (y = 0; y < h; y++)
280 for (i = 0; i < 3; i++) 299 for (i = 0; i < 3; i++)
281 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];
282} 301}
302 perlinterp_acquire ();
283 OUTPUT: 303 OUTPUT:
284 RETVAL 304 RETVAL
285 305
286GdkPixbuf_noinc * 306GdkPixbuf_noinc *
287rotate (GdkPixbuf *pb, int angle) 307rotate (GdkPixbuf *pb, int angle)
288 CODE: 308 CODE:
309 perlinterp_release ();
310 if (angle < 0)
311 angle += 360;
289 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
290 : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE 313 : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE
291 : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN 314 : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN
292 : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE 315 : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE
293 : angle); 316 : angle);
317 perlinterp_acquire ();
294 OUTPUT: 318 OUTPUT:
295 RETVAL 319 RETVAL
296 320
297GdkPixbuf_noinc * 321GdkPixbuf_noinc *
298load_jpeg (SV *path, int thumbnail=0) 322load_jpeg (SV *path, int thumbnail = 0, int iw = 0, int ih = 0)
299 CODE: 323 CODE:
300{ 324{
301 struct jpeg_decompress_struct cinfo; 325 struct jpeg_decompress_struct cinfo;
302 struct jpg_err_mgr jerr; 326 struct jpg_err_mgr jerr;
303 guchar *data; 327 guchar *data;
310 fp = fopen (SvPVbyte_nolen (path), "rb"); 334 fp = fopen (SvPVbyte_nolen (path), "rb");
311 335
312 if (!fp) 336 if (!fp)
313 XSRETURN_UNDEF; 337 XSRETURN_UNDEF;
314 338
339 perlinterp_release ();
340
315 cinfo.err = jpeg_std_error (&jerr.err); 341 cinfo.err = jpeg_std_error (&jerr.err);
316 342
317 jerr.err.error_exit = cv_error_exit; 343 jerr.err.error_exit = cv_error_exit;
318 jerr.err.output_message = cv_error_output; 344 jerr.err.output_message = cv_error_output;
319 345
323 jpeg_destroy_decompress (&cinfo); 349 jpeg_destroy_decompress (&cinfo);
324 350
325 if (pb) 351 if (pb)
326 g_object_unref ((gpointer)pb); 352 g_object_unref ((gpointer)pb);
327 353
354 perlinterp_acquire ();
328 XSRETURN_UNDEF; 355 XSRETURN_UNDEF;
329 } 356 }
330 357
331 jpeg_create_decompress (&cinfo); 358 jpeg_create_decompress (&cinfo);
332 359
348 { 375 {
349 cinfo.dct_method = JDCT_FASTEST; 376 cinfo.dct_method = JDCT_FASTEST;
350 cinfo.do_fancy_upsampling = FALSE; 377 cinfo.do_fancy_upsampling = FALSE;
351 378
352 while (cinfo.scale_denom < 8 379 while (cinfo.scale_denom < 8
353 && cinfo.output_width >= IW*4 380 && cinfo.output_width >= iw*4
354 && cinfo.output_height >= IH*4) 381 && cinfo.output_height >= ih*4)
355 { 382 {
356 cinfo.scale_denom <<= 1; 383 cinfo.scale_denom <<= 1;
357 jpeg_calc_output_dimensions (&cinfo); 384 jpeg_calc_output_dimensions (&cinfo);
358 } 385 }
359 } 386 }
360 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
361 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);
362 if (!RETVAL) 398 if (!RETVAL)
363 longjmp (jerr.setjmp_buffer, 2); 399 longjmp (jerr.setjmp_buffer, 2);
364 400
365 data = gdk_pixbuf_get_pixels (RETVAL); 401 data = gdk_pixbuf_get_pixels (RETVAL);
366 rs = gdk_pixbuf_get_rowstride (RETVAL); 402 rs = gdk_pixbuf_get_rowstride (RETVAL);
367
368 if (cinfo.output_components != 3)
369 longjmp (jerr.setjmp_buffer, 3);
370 403
371 jpeg_start_decompress (&cinfo); 404 jpeg_start_decompress (&cinfo);
372 405
373 while (cinfo.output_scanline < cinfo.output_height) 406 while (cinfo.output_scanline < cinfo.output_height)
374 { 407 {
381 rp [3] = (guchar *)rp [2] + rs; 414 rp [3] = (guchar *)rp [2] + rs;
382 415
383 jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4); 416 jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4);
384 } 417 }
385 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 data [0] = c * k / 255;
431 data [1] = m * k / 255;
432 data [2] = y * k / 255;
433 data [3] = 255;
434
435 data += 4;
436 }
437 }
438
386 jpeg_finish_decompress (&cinfo); 439 jpeg_finish_decompress (&cinfo);
387 fclose (fp); 440 fclose (fp);
388 jpeg_destroy_decompress (&cinfo); 441 jpeg_destroy_decompress (&cinfo);
442 perlinterp_acquire ();
389} 443}
390 OUTPUT: 444 OUTPUT:
391 RETVAL 445 RETVAL
392 446
393void 447void
394compare (GdkPixbuf *a, GdkPixbuf *b) 448compare (GdkPixbuf *a, GdkPixbuf *b)
395 PPCODE: 449 PPCODE:
450 perlinterp_release ();
396{ 451{
397 int w = gdk_pixbuf_get_width (a); 452 int w = gdk_pixbuf_get_width (a);
398 int h = gdk_pixbuf_get_height (a); 453 int h = gdk_pixbuf_get_height (a);
454
399 int sa = gdk_pixbuf_get_rowstride (a); 455 int sa = gdk_pixbuf_get_rowstride (a);
400 int sb = gdk_pixbuf_get_rowstride (b); 456 int sb = gdk_pixbuf_get_rowstride (b);
401 457
402 guchar *pa = gdk_pixbuf_get_pixels (a); 458 guchar *pa = gdk_pixbuf_get_pixels (a);
403 guchar *pb = gdk_pixbuf_get_pixels (b); 459 guchar *pb = gdk_pixbuf_get_pixels (b);
427 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d)); 483 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
428 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d)); 484 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
429 } 485 }
430 } 486 }
431 487
488 perlinterp_acquire ();
489
432 EXTEND (SP, 2); 490 EXTEND (SP, 2);
433 PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.))))); 491 PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.)))));
434 PUSHs (sv_2mortal (newSVnv (peak / 255.))); 492 PUSHs (sv_2mortal (newSVnv (peak / 255.)));
435} 493}
436 494
437############################################################################# 495#############################################################################
438 496
439MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer 497MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer
440 498
499# currently only works for filenames (octet strings)
500
441SV * 501SV *
442foldcase (SV *pathsv) 502foldcase (SV *pathsv)
443 PROTOTYPE: $ 503 PROTOTYPE: $
444 CODE: 504 CODE:
445{ 505{
446 STRLEN plen; 506 STRLEN plen;
447 U8 *path = (U8 *)SvPVutf8 (pathsv, plen); 507 U8 *path = (U8 *)SvPV (pathsv, plen);
448 U8 *pend = path + plen; 508 U8 *pend = path + plen;
449 U8 dst [plen * 6 * 3], *dstp = dst; 509 U8 dst [plen * 6 * 3], *dstp = dst;
450 510
451 while (path < pend) 511 while (path < pend)
452 { 512 {
453 U8 ch = *path; 513 U8 ch = *path;
454 514
455 if (ch >= 'a' && ch <= 'z') 515 if (ch >= 'a' && ch <= 'z')
456 *dstp++ = *path++; 516 *dstp++ = *path++;
517 else if (ch >= 'A' && ch <= 'Z')
518 *dstp++ = *path++ + ('a' - 'A');
457 else if (ch >= '0' && ch <= '9') 519 else if (ch >= '0' && ch <= '9')
458 { 520 {
459 STRLEN el, nl = 0; 521 STRLEN el, nl = 0;
460 while (*path >= '0' && *path <= '9' && path < pend) 522 while (*path >= '0' && *path <= '9' && path < pend)
461 path++, nl++; 523 path++, nl++;
464 *dstp++ = '0'; 526 *dstp++ = '0';
465 527
466 memcpy (dstp, path - nl, nl); 528 memcpy (dstp, path - nl, nl);
467 dstp += nl; 529 dstp += nl;
468 } 530 }
531 else
532 *dstp++ = *path++;
533#if 0
469 else 534 else
470 { 535 {
471 STRLEN cl; 536 STRLEN cl;
472 to_utf8_fold (path, dstp, &cl); 537 to_utf8_fold (path, dstp, &cl);
473 dstp += cl; 538 dstp += cl;
474 path += is_utf8_char (path); 539 path += is_utf8_char (path);
475 } 540 }
541#endif
476 } 542 }
477 543
478 RETVAL = newSVpvn ((const char *)dst, dstp - dst); 544 RETVAL = newSVpvn ((const char *)dst, dstp - dst);
479} 545}
480 OUTPUT: 546 OUTPUT:
649{ 715{
650 int i; 716 int i;
651 AV *av, *result; 717 AV *av, *result;
652 718
653 if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV) 719 if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV)
654 croak ("Not an array ref as first argument to make_histogram"); 720 croak ("Not an array ref as first argument to extract_features");
655 721
656 av = (AV *) SvRV (ar); 722 av = (AV *) SvRV (ar);
657 result = newAV (); 723 result = newAV ();
658 724
659 for (i = 0; i <= av_len (av); ++i) 725 for (i = 0; i <= av_len (av); ++i)
712 feature_finish_pass_2 (&f_v); 778 feature_finish_pass_2 (&f_v);
713 } 779 }
714 780
715 hist [0] = f_h.v1 * 2.; hist [1] = f_h.v2 * 2.; hist [2] = f_h.v3 * 2.; 781 hist [0] = f_h.v1 * 2.; hist [1] = f_h.v2 * 2.; hist [2] = f_h.v3 * 2.;
716 hist [3] = f_s.v1 ; hist [4] = f_s.v2 ; hist [5] = f_s.v3 ; 782 hist [3] = f_s.v1 ; hist [4] = f_s.v2 ; hist [5] = f_s.v3 ;
717 hist [6] = f_v.v1 ; hist [7] = f_v.v2 ; hist [8] = f_v.v3 ; 783 hist [6] = f_v.v1 * .5; hist [7] = f_v.v2 * .5; hist [8] = f_v.v3 * .5;
718 784
719 av_push (result, histsv); 785 av_push (result, histsv);
720 } 786 }
721 787
722 RETVAL = newRV_noinc ((SV *)result); 788 RETVAL = newRV_noinc ((SV *)result);
723} 789}
724 OUTPUT: 790 OUTPUT:
725 RETVAL 791 RETVAL
726 792
727SV *
728make_histograms (SV *ar)
729 CODE:
730{
731 int i;
732 AV *av, *result;
733
734 if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV)
735 croak ("Not an array ref as first argument to make_histogram");
736
737 av = (AV *) SvRV (ar);
738 result = newAV ();
739
740 for (i = 0; i <= av_len (av); ++i)
741 {
742 const int HISTSIZE = 64;
743
744 int j;
745 SV *sv = *av_fetch (av, i, 1);
746 STRLEN len;
747 char *buf = SvPVbyte (sv, len);
748
749 int tmphist[HISTSIZE];
750 float *hist;
751
752 SV *histsv = newSV (HISTSIZE * sizeof (float) + 1);
753 SvPOK_on (histsv);
754 SvCUR_set (histsv, HISTSIZE * sizeof (float));
755 hist = (float *)SvPVX (histsv);
756
757 Zero (tmphist, sizeof (tmphist), char);
758
759 for (j = len; j--; )
760 {
761 unsigned int idx
762 = ((*buf & 0xc0) >> 2)
763 | ((*buf & 0x18) >> 1)
764 | (*buf & 0x03);
765
766 ++tmphist[idx];
767 ++buf;
768 }
769
770 for (j = 0; j < HISTSIZE; ++j)
771 hist[j] = (float)tmphist[j] / (len + 1e-30);
772
773 av_push (result, histsv);
774 }
775
776 RETVAL = newRV_noinc ((SV *)result);
777}
778 OUTPUT:
779 RETVAL
780
781

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines