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

Comparing CV/CV.xs (file contents):
Revision 1.37 by root, Thu Jun 22 22:01:26 2006 UTC vs.
Revision 1.49 by root, Wed May 24 19:22:32 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,
144 if (!--a85_cnt) 156 if (!--a85_cnt)
145 { 157 {
146 a85_cnt = 4; 158 a85_cnt = 4;
147 if (a85_val) 159 if (a85_val)
148 { 160 {
149 a85_ptr[4] = (a85_val % 85) + 33; a85_val /= 85; 161 a85_ptr[4] = (a85_val % 85) + 33; a85_val /= 85;
150 a85_ptr[3] = (a85_val % 85) + 33; a85_val /= 85; 162 a85_ptr[3] = (a85_val % 85) + 33; a85_val /= 85;
151 a85_ptr[2] = (a85_val % 85) + 33; a85_val /= 85; 163 a85_ptr[2] = (a85_val % 85) + 33; a85_val /= 85;
152 a85_ptr[1] = (a85_val % 85) + 33; a85_val /= 85; 164 a85_ptr[1] = (a85_val % 85) + 33; a85_val /= 85;
153 a85_ptr[0] = (a85_val ) + 33; 165 a85_ptr[0] = (a85_val ) + 33;
154 166
155 a85_ptr += 5; 167 a85_ptr += 5;
156 } 168 }
202 214
203 OUTPUT: 215 OUTPUT:
204 RETVAL 216 RETVAL
205 217
206const char * 218const char *
207magic (const char *path) 219magic (octet_string path)
208 CODE: 220 CODE:
209{ 221{
210 static magic_t cookie; 222 static magic_t cookie;
211 223
212 if (!cookie) 224 if (!cookie)
213 { 225 {
214 cookie = magic_open (MAGIC_NONE); 226 cookie = magic_open (MAGIC_SYMLINK);
215 227
216 if (cookie) 228 if (cookie)
217 magic_load (cookie, 0); 229 magic_load (cookie, 0);
218 else 230 else
219 XSRETURN_UNDEF; 231 XSRETURN_UNDEF;
223} 235}
224 OUTPUT: 236 OUTPUT:
225 RETVAL 237 RETVAL
226 238
227const char * 239const char *
228magic_mime (const char *path) 240magic_mime (octet_string path)
229 CODE: 241 CODE:
230{ 242{
231 static magic_t cookie; 243 static magic_t cookie;
232 244
233 if (!cookie) 245 if (!cookie)
234 { 246 {
235 cookie = magic_open (MAGIC_MIME); 247 cookie = magic_open (MAGIC_MIME | MAGIC_SYMLINK);
236 248
237 if (cookie) 249 if (cookie)
238 magic_load (cookie, 0); 250 magic_load (cookie, 0);
239 else 251 else
240 XSRETURN_UNDEF; 252 XSRETURN_UNDEF;
241 } 253 }
242 254
255 perlinterp_release ();
243 RETVAL = magic_file (cookie, path); 256 RETVAL = magic_file (cookie, path);
257 perlinterp_acquire ();
244} 258}
245 OUTPUT: 259 OUTPUT:
246 RETVAL 260 RETVAL
247 261
248# 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);
249 268
250gboolean 269gboolean
251gdk_net_wm_supports (GdkAtom property) 270gdk_net_wm_supports (GdkAtom property)
252 CODE: 271 CODE:
253#if defined(GDK_WINDOWING_X11) && !defined(GDK_MULTIHEAD_SAFE) 272#if defined(GDK_WINDOWING_X11) && !defined(GDK_MULTIHEAD_SAFE)
259 RETVAL 278 RETVAL
260 279
261GdkPixbuf_noinc * 280GdkPixbuf_noinc *
262dealpha_expose (GdkPixbuf *pb) 281dealpha_expose (GdkPixbuf *pb)
263 CODE: 282 CODE:
283 perlinterp_release ();
264{ 284{
265 int w = gdk_pixbuf_get_width (pb); 285 int w = gdk_pixbuf_get_width (pb);
266 int h = gdk_pixbuf_get_height (pb); 286 int h = gdk_pixbuf_get_height (pb);
267 int bpp = gdk_pixbuf_get_n_channels (pb); 287 int bpp = gdk_pixbuf_get_n_channels (pb);
268 int x, y, i; 288 int x, y, i;
277 for (x = 0; x < w; x++) 297 for (x = 0; x < w; x++)
278 for (y = 0; y < h; y++) 298 for (y = 0; y < h; y++)
279 for (i = 0; i < 3; i++) 299 for (i = 0; i < 3; i++)
280 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];
281} 301}
302 perlinterp_acquire ();
282 OUTPUT: 303 OUTPUT:
283 RETVAL 304 RETVAL
284 305
285GdkPixbuf_noinc * 306GdkPixbuf_noinc *
286rotate (GdkPixbuf *pb, int angle) 307rotate (GdkPixbuf *pb, int angle)
287 CODE: 308 CODE:
309 perlinterp_release ();
310 if (angle < 0)
311 angle += 360;
288 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
289 : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE 313 : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE
290 : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN 314 : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN
291 : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE 315 : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE
292 : angle); 316 : angle);
317 perlinterp_acquire ();
293 OUTPUT: 318 OUTPUT:
294 RETVAL 319 RETVAL
295 320
296GdkPixbuf_noinc * 321GdkPixbuf_noinc *
297load_jpeg (SV *path, int thumbnail=0) 322load_jpeg (SV *path, int thumbnail = 0, int iw = 0, int ih = 0)
298 CODE: 323 CODE:
299{ 324{
300 struct jpeg_decompress_struct cinfo; 325 struct jpeg_decompress_struct cinfo;
301 struct jpg_err_mgr jerr; 326 struct jpg_err_mgr jerr;
302 guchar *data; 327 guchar *data;
309 fp = fopen (SvPVbyte_nolen (path), "rb"); 334 fp = fopen (SvPVbyte_nolen (path), "rb");
310 335
311 if (!fp) 336 if (!fp)
312 XSRETURN_UNDEF; 337 XSRETURN_UNDEF;
313 338
339 perlinterp_release ();
340
314 cinfo.err = jpeg_std_error (&jerr.err); 341 cinfo.err = jpeg_std_error (&jerr.err);
315 342
316 jerr.err.error_exit = cv_error_exit; 343 jerr.err.error_exit = cv_error_exit;
317 jerr.err.output_message = cv_error_output; 344 jerr.err.output_message = cv_error_output;
318 345
322 jpeg_destroy_decompress (&cinfo); 349 jpeg_destroy_decompress (&cinfo);
323 350
324 if (pb) 351 if (pb)
325 g_object_unref ((gpointer)pb); 352 g_object_unref ((gpointer)pb);
326 353
354 perlinterp_acquire ();
327 XSRETURN_UNDEF; 355 XSRETURN_UNDEF;
328 } 356 }
329 357
330 jpeg_create_decompress (&cinfo); 358 jpeg_create_decompress (&cinfo);
331 359
347 { 375 {
348 cinfo.dct_method = JDCT_FASTEST; 376 cinfo.dct_method = JDCT_FASTEST;
349 cinfo.do_fancy_upsampling = FALSE; 377 cinfo.do_fancy_upsampling = FALSE;
350 378
351 while (cinfo.scale_denom < 8 379 while (cinfo.scale_denom < 8
352 && cinfo.output_width >= IW*4 380 && cinfo.output_width >= iw*4
353 && cinfo.output_height >= IH*4) 381 && cinfo.output_height >= ih*4)
354 { 382 {
355 cinfo.scale_denom <<= 1; 383 cinfo.scale_denom <<= 1;
356 jpeg_calc_output_dimensions (&cinfo); 384 jpeg_calc_output_dimensions (&cinfo);
357 } 385 }
358 } 386 }
359 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
360 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);
361 if (!RETVAL) 398 if (!RETVAL)
362 longjmp (jerr.setjmp_buffer, 2); 399 longjmp (jerr.setjmp_buffer, 2);
363 400
364 data = gdk_pixbuf_get_pixels (RETVAL); 401 data = gdk_pixbuf_get_pixels (RETVAL);
365 rs = gdk_pixbuf_get_rowstride (RETVAL); 402 rs = gdk_pixbuf_get_rowstride (RETVAL);
366
367 if (cinfo.output_components != 3)
368 longjmp (jerr.setjmp_buffer, 3);
369 403
370 jpeg_start_decompress (&cinfo); 404 jpeg_start_decompress (&cinfo);
371 405
372 while (cinfo.output_scanline < cinfo.output_height) 406 while (cinfo.output_scanline < cinfo.output_height)
373 { 407 {
380 rp [3] = (guchar *)rp [2] + rs; 414 rp [3] = (guchar *)rp [2] + rs;
381 415
382 jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4); 416 jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4);
383 } 417 }
384 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
385 jpeg_finish_decompress (&cinfo); 448 jpeg_finish_decompress (&cinfo);
386 fclose (fp); 449 fclose (fp);
387 jpeg_destroy_decompress (&cinfo); 450 jpeg_destroy_decompress (&cinfo);
451 perlinterp_acquire ();
388} 452}
389 OUTPUT: 453 OUTPUT:
390 RETVAL 454 RETVAL
391 455
392void 456void
393compare (GdkPixbuf *a, GdkPixbuf *b) 457compare (GdkPixbuf *a, GdkPixbuf *b)
394 PPCODE: 458 PPCODE:
459 perlinterp_release ();
395{ 460{
396 int w = gdk_pixbuf_get_width (a); 461 int w = gdk_pixbuf_get_width (a);
397 int h = gdk_pixbuf_get_height (a); 462 int h = gdk_pixbuf_get_height (a);
463
398 int sa = gdk_pixbuf_get_rowstride (a); 464 int sa = gdk_pixbuf_get_rowstride (a);
399 int sb = gdk_pixbuf_get_rowstride (b); 465 int sb = gdk_pixbuf_get_rowstride (b);
400 466
401 guchar *pa = gdk_pixbuf_get_pixels (a); 467 guchar *pa = gdk_pixbuf_get_pixels (a);
402 guchar *pb = gdk_pixbuf_get_pixels (b); 468 guchar *pb = gdk_pixbuf_get_pixels (b);
426 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));
427 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));
428 } 494 }
429 } 495 }
430 496
497 perlinterp_acquire ();
498
431 EXTEND (SP, 2); 499 EXTEND (SP, 2);
432 PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.))))); 500 PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.)))));
433 PUSHs (sv_2mortal (newSVnv (peak / 255.))); 501 PUSHs (sv_2mortal (newSVnv (peak / 255.)));
434} 502}
435 503
436############################################################################# 504#############################################################################
437 505
438MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer 506MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer
439 507
508# currently only works for filenames (octet strings)
509
440SV * 510SV *
441foldcase (SV *pathsv) 511foldcase (SV *pathsv)
442 PROTOTYPE: $ 512 PROTOTYPE: $
443 CODE: 513 CODE:
444{ 514{
445 STRLEN plen; 515 STRLEN plen;
446 U8 *path = (U8 *)SvPVutf8 (pathsv, plen); 516 U8 *path = (U8 *)SvPV (pathsv, plen);
447 U8 *pend = path + plen; 517 U8 *pend = path + plen;
448 U8 dst [plen * 6 * 3], *dstp = dst; 518 U8 dst [plen * 6 * 3], *dstp = dst;
449 519
450 while (path < pend) 520 while (path < pend)
451 { 521 {
452 U8 ch = *path; 522 U8 ch = *path;
453 523
454 if (ch >= 'a' && ch <= 'z') 524 if (ch >= 'a' && ch <= 'z')
455 *dstp++ = *path++; 525 *dstp++ = *path++;
526 else if (ch >= 'A' && ch <= 'Z')
527 *dstp++ = *path++ + ('a' - 'A');
456 else if (ch >= '0' && ch <= '9') 528 else if (ch >= '0' && ch <= '9')
457 { 529 {
458 STRLEN el, nl = 0; 530 STRLEN el, nl = 0;
459 while (*path >= '0' && *path <= '9' && path < pend) 531 while (*path >= '0' && *path <= '9' && path < pend)
460 path++, nl++; 532 path++, nl++;
463 *dstp++ = '0'; 535 *dstp++ = '0';
464 536
465 memcpy (dstp, path - nl, nl); 537 memcpy (dstp, path - nl, nl);
466 dstp += nl; 538 dstp += nl;
467 } 539 }
540 else
541 *dstp++ = *path++;
542#if 0
468 else 543 else
469 { 544 {
470 STRLEN cl; 545 STRLEN cl;
471 to_utf8_fold (path, dstp, &cl); 546 to_utf8_fold (path, dstp, &cl);
472 dstp += cl; 547 dstp += cl;
473 path += is_utf8_char (path); 548 path += is_utf8_char (path);
474 } 549 }
550#endif
475 } 551 }
476 552
477 RETVAL = newSVpvn ((const char *)dst, dstp - dst); 553 RETVAL = newSVpvn ((const char *)dst, dstp - dst);
478} 554}
479 OUTPUT: 555 OUTPUT:
648{ 724{
649 int i; 725 int i;
650 AV *av, *result; 726 AV *av, *result;
651 727
652 if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV) 728 if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV)
653 croak ("Not an array ref as first argument to make_histogram"); 729 croak ("Not an array ref as first argument to extract_features");
654 730
655 av = (AV *) SvRV (ar); 731 av = (AV *) SvRV (ar);
656 result = newAV (); 732 result = newAV ();
657 733
658 for (i = 0; i <= av_len (av); ++i) 734 for (i = 0; i <= av_len (av); ++i)
711 feature_finish_pass_2 (&f_v); 787 feature_finish_pass_2 (&f_v);
712 } 788 }
713 789
714 hist [0] = f_h.v1 * 2.; hist [1] = f_h.v2 * 2.; hist [2] = f_h.v3 * 2.; 790 hist [0] = f_h.v1 * 2.; hist [1] = f_h.v2 * 2.; hist [2] = f_h.v3 * 2.;
715 hist [3] = f_s.v1 ; hist [4] = f_s.v2 ; hist [5] = f_s.v3 ; 791 hist [3] = f_s.v1 ; hist [4] = f_s.v2 ; hist [5] = f_s.v3 ;
716 hist [6] = f_v.v1 ; hist [7] = f_v.v2 ; hist [8] = f_v.v3 ; 792 hist [6] = f_v.v1 * .5; hist [7] = f_v.v2 * .5; hist [8] = f_v.v3 * .5;
717 793
718 av_push (result, histsv); 794 av_push (result, histsv);
719 } 795 }
720 796
721 RETVAL = newRV_noinc ((SV *)result); 797 RETVAL = newRV_noinc ((SV *)result);
722} 798}
723 OUTPUT: 799 OUTPUT:
724 RETVAL 800 RETVAL
725 801
726SV *
727make_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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines