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

Comparing CV/CV.xs (file contents):
Revision 1.17 by root, Wed Jul 20 06:20:36 2005 UTC vs.
Revision 1.35 by root, Tue Mar 7 16:45:53 2006 UTC

1#include "EXTERN.h" 1#include "EXTERN.h"
2#include "perl.h" 2#include "perl.h"
3#include "XSUB.h" 3#include "XSUB.h"
4 4
5#include <string.h>
5#include <setjmp.h> 6#include <setjmp.h>
7#include <math.h>
8
9#include <magic.h>
6 10
7#include <jpeglib.h> 11#include <jpeglib.h>
12#include <glib.h>
13#include <gtk/gtk.h>
8#include <gdk-pixbuf/gdk-pixbuf.h> 14#include <gdk-pixbuf/gdk-pixbuf.h>
9 15
10#include <gperl.h> 16#include <gperl.h>
11#include <gtk2perl.h> 17#include <gtk2perl.h>
12 18
13#define IW 80 /* MUST match Schnauer.pm! */ 19#define IW 80 /* MUST match Schnauzer.pm! */
14#define IH 60 /* MUST match Schnauer.pm! */ 20#define IH 60 /* MUST match Schnauzer.pm! */
15 21
16#define RAND (seed = (seed + 7141) * 54773 % 134456) 22#define RAND (seed = (seed + 7141) * 54773 % 134456)
17 23
18#define LINELENGTH 240 24#define LINELENGTH 240
19 25
123 129
124MODULE = Gtk2::CV PACKAGE = Gtk2::CV 130MODULE = Gtk2::CV PACKAGE = Gtk2::CV
125 131
126PROTOTYPES: ENABLE 132PROTOTYPES: ENABLE
127 133
134# missing function in perl. really :)
135int
136common_prefix_length (a, b)
137 unsigned char *a = (unsigned char *)SvPVutf8_nolen ($arg);
138 unsigned char *b = (unsigned char *)SvPVutf8_nolen ($arg);
139 CODE:
140 RETVAL = 0;
141
142 while (*a == *b && *a)
143 {
144 RETVAL += (*a & 0xc0) != 0x80;
145 a++, b++;
146 }
147
148 OUTPUT:
149 RETVAL
150
151const char *
152magic (const char *path)
153 CODE:
154{
155 static magic_t cookie;
156
157 if (!cookie)
158 {
159 cookie = magic_open (MAGIC_NONE);
160
161 if (cookie)
162 magic_load (cookie, 0);
163 else
164 XSRETURN_UNDEF;
165 }
166
167 RETVAL = magic_file (cookie, path);
168}
169 OUTPUT:
170 RETVAL
171
172const char *
173magic_mime (const char *path)
174 CODE:
175{
176 static magic_t cookie;
177
178 if (!cookie)
179 {
180 cookie = magic_open (MAGIC_MIME);
181
182 if (cookie)
183 magic_load (cookie, 0);
184 else
185 XSRETURN_UNDEF;
186 }
187
188 RETVAL = magic_file (cookie, path);
189}
190 OUTPUT:
191 RETVAL
192
193# missing in Gtk2 perl module
194
195gboolean
196gdk_net_wm_supports (GdkAtom property)
197 CODE:
198#if defined(GDK_WINDOWING_X11) && !defined(GDK_MULTIHEAD_SAFE)
199 RETVAL = gdk_net_wm_supports (property);
200#else
201 RETVAL = 0;
202#endif
203 OUTPUT:
204 RETVAL
205
128GdkPixbuf_noinc * 206GdkPixbuf_noinc *
129transpose (GdkPixbuf *pb) 207dealpha_expose (GdkPixbuf *pb)
130 CODE: 208 CODE:
131{ 209{
132 int w = gdk_pixbuf_get_width (pb); 210 int w = gdk_pixbuf_get_width (pb);
133 int h = gdk_pixbuf_get_height (pb); 211 int h = gdk_pixbuf_get_height (pb);
134 int bpp = gdk_pixbuf_get_n_channels (pb); 212 int bpp = gdk_pixbuf_get_n_channels (pb);
135 int x, y, i; 213 int x, y, i;
136 guchar *src = gdk_pixbuf_get_pixels (pb), *dst; 214 guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
137 int sstr = gdk_pixbuf_get_rowstride (pb), dstr; 215 int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
138 216
139 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, h, w); 217 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
140 218
141 dst = gdk_pixbuf_get_pixels (RETVAL); 219 dst = gdk_pixbuf_get_pixels (RETVAL);
142 dstr = gdk_pixbuf_get_rowstride (RETVAL); 220 dstr = gdk_pixbuf_get_rowstride (RETVAL);
143 221
144 for (y = 0; y < h; y++)
145 for (x = 0; x < w; x++) 222 for (x = 0; x < w; x++)
223 for (y = 0; y < h; y++)
146 for (i = 0; i < bpp; i++) 224 for (i = 0; i < 3; i++)
147 dst[y * bpp + x * dstr + i] = src[x * bpp + y * sstr + i]; 225 dst[x * 3 + y * dstr + i] = src[x * bpp + y * sstr + i];
148} 226}
149 OUTPUT: 227 OUTPUT:
150 RETVAL 228 RETVAL
151 229
152GdkPixbuf_noinc * 230GdkPixbuf_noinc *
153flop (GdkPixbuf *pb) 231rotate (GdkPixbuf *pb, int angle)
154 CODE: 232 CODE:
155{ 233 RETVAL = gdk_pixbuf_rotate_simple (pb, angle == 0 ? GDK_PIXBUF_ROTATE_NONE
156 int w = gdk_pixbuf_get_width (pb); 234 : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE
157 int h = gdk_pixbuf_get_height (pb); 235 : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN
158 int bpp = gdk_pixbuf_get_n_channels (pb); 236 : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE
159 int x, y, i; 237 : angle);
160 guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
161 int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
162
163 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, w, h);
164
165 dst = gdk_pixbuf_get_pixels (RETVAL);
166 dstr = gdk_pixbuf_get_rowstride (RETVAL);
167
168 for (y = 0; y < h; y++)
169 for (x = 0; x < w; x++)
170 for (i = 0; i < bpp; i++)
171 dst[(w - 1 - x) * bpp + y * dstr + i] = src[x * bpp + y * sstr + i];
172}
173 OUTPUT: 238 OUTPUT:
174 RETVAL 239 RETVAL
175 240
176GdkPixbuf_noinc * 241GdkPixbuf_noinc *
177load_jpeg (char *path, int thumbnail=0) 242load_jpeg (SV *path, int thumbnail=0)
178 CODE: 243 CODE:
179{ 244{
180 struct jpeg_decompress_struct cinfo; 245 struct jpeg_decompress_struct cinfo;
181 struct jpg_err_mgr jerr; 246 struct jpg_err_mgr jerr;
182 guchar *data; 247 guchar *data;
183 int rs; 248 int rs;
184 FILE *fp; 249 FILE *fp;
185 volatile GdkPixbuf *pb = 0; 250 volatile GdkPixbuf *pb = 0;
251
186 RETVAL = 0; 252 RETVAL = 0;
187 253
188 if (!(fp = fopen (path, "rb"))) 254 fp = fopen (SvPVbyte_nolen (path), "rb");
255
256 if (!fp)
189 XSRETURN_UNDEF; 257 XSRETURN_UNDEF;
190 258
191 cinfo.err = jpeg_std_error (&jerr.err); 259 cinfo.err = jpeg_std_error (&jerr.err);
192 260
193 jerr.err.error_exit = cv_error_exit; 261 jerr.err.error_exit = cv_error_exit;
224 { 292 {
225 cinfo.dct_method = JDCT_FASTEST; 293 cinfo.dct_method = JDCT_FASTEST;
226 cinfo.do_fancy_upsampling = FALSE; 294 cinfo.do_fancy_upsampling = FALSE;
227 295
228 while (cinfo.scale_denom < 8 296 while (cinfo.scale_denom < 8
229 && (cinfo.output_width >> 1) >= IW 297 && cinfo.output_width >= IW*4
230 && (cinfo.output_height >> 1) >= IH) 298 && cinfo.output_height >= IH*4)
231 { 299 {
232 cinfo.scale_denom <<= 1; 300 cinfo.scale_denom <<= 1;
233 jpeg_calc_output_dimensions (&cinfo); 301 jpeg_calc_output_dimensions (&cinfo);
234 } 302 }
235 } 303 }
264 jpeg_destroy_decompress (&cinfo); 332 jpeg_destroy_decompress (&cinfo);
265} 333}
266 OUTPUT: 334 OUTPUT:
267 RETVAL 335 RETVAL
268 336
337void
338compare (GdkPixbuf *a, GdkPixbuf *b)
339 PPCODE:
340{
341 int w = gdk_pixbuf_get_width (a);
342 int h = gdk_pixbuf_get_height (a);
343 int sa = gdk_pixbuf_get_rowstride (a);
344 int sb = gdk_pixbuf_get_rowstride (b);
345
346 guchar *pa = gdk_pixbuf_get_pixels (a);
347 guchar *pb = gdk_pixbuf_get_pixels (b);
348
349 int x, y;
350
351 assert (w == gdk_pixbuf_get_width (b));
352 assert (h == gdk_pixbuf_get_height (b));
353
354 assert (gdk_pixbuf_get_n_channels (a) == 3);
355 assert (gdk_pixbuf_get_n_channels (b) == 3);
356
357 double diff = 0.;
358 int peak = 0;
359
360 if (w && h)
361 for (y = 0; y < h; y++)
362 {
363 guchar *pa_ = pa + y * sa;
364 guchar *pb_ = pb + y * sb;
365
366 for (x = 0; x < w; x++)
367 {
368 int d;
369
370 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));
372 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
373 }
374 }
375
376 EXTEND (SP, 2);
377 PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.)))));
378 PUSHs (sv_2mortal (newSVnv (peak / 255.)));
379}
380
269############################################################################# 381#############################################################################
270 382
271MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer 383MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer
272 384
385SV *
386foldcase (SV *pathsv)
387 PROTOTYPE: $
388 CODE:
389{
390 STRLEN plen;
391 U8 *path = (U8 *)SvPVutf8 (pathsv, plen);
392 U8 *pend = path + plen;
393 U8 dst [plen * 6 * 3], *dstp = dst;
394
395 while (path < pend)
396 {
397 U8 ch = *path;
398
399 if (ch >= 'a' && ch <= 'z')
400 *dstp++ = *path++;
401 else if (ch >= '0' && ch <= '9')
402 {
403 STRLEN el, nl = 0;
404 while (*path >= '0' && *path <= '9' && path < pend)
405 path++, nl++;
406
407 for (el = nl; el < 6; el++)
408 *dstp++ = '0';
409
410 memcpy (dstp, path - nl, nl);
411 dstp += nl;
412 }
413 else
414 {
415 STRLEN cl;
416 to_utf8_fold (path, dstp, &cl);
417 dstp += cl;
418 path += is_utf8_char (path);
419 }
420 }
421
422 RETVAL = newSVpvn ((const char *)dst, dstp - dst);
423}
424 OUTPUT:
425 RETVAL
426
273GdkPixbuf_noinc * 427GdkPixbuf_noinc *
274p7_to_pb (int w, int h, guchar *src) 428p7_to_pb (int w, int h, SV *src_sv)
429 PROTOTYPE: @
275 CODE: 430 CODE:
276{ 431{
277 int x, y; 432 int x, y;
278 guchar *dst, *d; 433 guchar *dst, *d;
279 int dstr; 434 int dstr;
435 guchar *src = (guchar *)SvPVbyte_nolen (src_sv);
280 436
281 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h); 437 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
282 dst = gdk_pixbuf_get_pixels (RETVAL); 438 dst = gdk_pixbuf_get_pixels (RETVAL);
283 dstr = gdk_pixbuf_get_rowstride (RETVAL); 439 dstr = gdk_pixbuf_get_rowstride (RETVAL);
284 440
293 } 449 }
294} 450}
295 OUTPUT: 451 OUTPUT:
296 RETVAL 452 RETVAL
297 453
454#############################################################################
455
456MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
457
458void
459dump_ascii85 (PerlIO *fp, GdkPixbuf *pb)
460 CODE:
461{
462 int w = gdk_pixbuf_get_width (pb);
463 int h = gdk_pixbuf_get_height (pb);
464 int x, y, i;
465 guchar *dst;
466 int bpp = gdk_pixbuf_get_n_channels (pb);
467 guchar *src = gdk_pixbuf_get_pixels (pb);
468 int sstr = gdk_pixbuf_get_rowstride (pb);
469
470 a85_init ();
471
472 for (y = 0; y < h; y++)
473 for (x = 0; x < w; x++)
474 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
475 a85_push (fp, src [x * bpp + y * sstr + i]);
476
477 a85_finish (fp);
478}
479
480void
481dump_binary (PerlIO *fp, GdkPixbuf *pb)
482 CODE:
483{
484 int w = gdk_pixbuf_get_width (pb);
485 int h = gdk_pixbuf_get_height (pb);
486 int x, y, i;
487 guchar *dst;
488 int bpp = gdk_pixbuf_get_n_channels (pb);
489 guchar *src = gdk_pixbuf_get_pixels (pb);
490 int sstr = gdk_pixbuf_get_rowstride (pb);
491
492 for (y = 0; y < h; y++)
493 for (x = 0; x < w; x++)
494 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
495 PerlIO_putc (fp, src [x * bpp + y * sstr + i]);
496}
497
498#############################################################################
499
500MODULE = Gtk2::CV PACKAGE = Gtk2::CV
501
298SV * 502SV *
299pb_to_p7 (GdkPixbuf *pb) 503pb_to_hv84 (GdkPixbuf *pb)
300 CODE: 504 CODE:
301{ 505{
302 int w = gdk_pixbuf_get_width (pb); 506 int w = gdk_pixbuf_get_width (pb);
303 int h = gdk_pixbuf_get_height (pb); 507 int h = gdk_pixbuf_get_height (pb);
304 int x, y; 508 int x, y;
305 guchar *dst; 509 guchar *dst;
306 int bpp = gdk_pixbuf_get_n_channels (pb); 510 int bpp = gdk_pixbuf_get_n_channels (pb);
307 guchar *src = gdk_pixbuf_get_pixels (pb); 511 guchar *src = gdk_pixbuf_get_pixels (pb);
308 int sstr = gdk_pixbuf_get_rowstride (pb); 512 int sstr = gdk_pixbuf_get_rowstride (pb);
309 int Er[IW], Eg[IW], Eb[IW];
310 int seed = 77;
311 513
312 RETVAL = newSV (w * h); 514 RETVAL = newSV (6 * 8 * 12 / 8);
313 SvPOK_only (RETVAL); 515 SvPOK_only (RETVAL);
314 SvCUR_set (RETVAL, w * h); 516 SvCUR_set (RETVAL, 6 * 8 * 12 / 8);
315 517
316 dst = SvPVX (RETVAL); 518 dst = (guchar *)SvPVX (RETVAL);
317
318 memset (Er, 0, sizeof (int) * IW);
319 memset (Eg, 0, sizeof (int) * IW);
320 memset (Eb, 0, sizeof (int) * IW);
321 519
322 /* some primitive error distribution + random dithering */ 520 /* some primitive error distribution + random dithering */
323 521
324 for (y = 0; y < h; y++) 522 for (y = 0; y < h; y++)
325 { 523 {
326 int er = 0, eg = 0, eb = 0; 524 guchar *p = src + y * sstr;
327 525
328 for (x = 0; x < w; x++) 526 for (x = 0; x < w; x += 2)
329 { 527 {
330 int r, g, b; 528 unsigned int r, g, b, h, s, v, H, V1, V2;
331 guchar *p = src + x * bpp + y * sstr;
332 529
333 r = ((p[0] + er + Er[x]) * 7 + (RAND & 127) + 64) / 255; 530 if (bpp == 3)
334 g = ((p[1] + eg + Eg[x]) * 7 + (RAND & 127) + 64) / 255; 531 r = *p++, g = *p++, b = *p++;
335 b = ((p[2] + eb + Eb[x]) * 3 + (RAND & 127) + 64) / 255; 532 else if (bpp == 1)
533 r = g = b = *p++;
534 else
535 abort ();
336 536
337 r = r > 7 ? 7 : r < 0 ? 0 : r; 537 rgb_to_hsv (r, g, b, &h, &s, &v);
338 g = g > 7 ? 7 : g < 0 ? 0 : g;
339 b = b > 3 ? 3 : b < 0 ? 0 : b;
340 538
341 er += p[0] - (r * 255 + 4) / 7; 539 H = (h * 15 / 255) << 4;
342 eg += p[1] - (g * 255 + 4) / 7; 540 V1 = v;
343 eb += p[2] - (b * 255 + 2) / 3;
344 541
345 Er[x] = er >> 1; er -= (er + 1) >> 1; 542 if (bpp == 3)
346 Eg[x] = eg >> 1; eg -= (eg + 1) >> 1; 543 r = *p++, g = *p++, b = *p++;
347 Eb[x] = eb >> 1; eb -= (eb + 1) >> 1; 544 else if (bpp == 1)
545 r = g = b = *p++;
546 else
547 abort ();
348 548
349 *dst++ = r << 5 | g << 2 | b; 549 rgb_to_hsv (r, g, b, &h, &s, &v);
550
551 H |= h * 15 / 255;
552 V2 = v;
553
554 *dst++ = H;
555 *dst++ = V1;
556 *dst++ = V2;
350 } 557 }
351 } 558 }
352} 559}
353 OUTPUT: 560 OUTPUT:
354 RETVAL 561 RETVAL
355 562
356SV * 563SV *
564hv84_to_av (unsigned char *hv84)
565 CODE:
566{
567 int i = 72 / 3;
568 AV *av = newAV ();
569
570 RETVAL = (SV *)newRV_noinc ((SV *)av);
571 while (i--)
572 {
573 int h = *hv84++;
574 int v1 = *hv84++;
575 int v2 = *hv84++;
576
577 av_push (av, newSViv (v1));
578 av_push (av, newSViv ((h >> 4) * 255 / 15));
579 av_push (av, newSViv (v2));
580 av_push (av, newSViv ((h & 15) * 255 / 15));
581 }
582}
583 OUTPUT:
584 RETVAL
585
586#############################################################################
587
588MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Plugin::RCluster
589
590SV *
357make_histogram (SV *ar) 591make_histograms (SV *ar)
358 CODE: 592 CODE:
359{ 593{
360 int i; 594 int i;
361 AV *av, *result; 595 AV *av, *result;
362 596
405 RETVAL = newRV_noinc ((SV *)result); 639 RETVAL = newRV_noinc ((SV *)result);
406} 640}
407 OUTPUT: 641 OUTPUT:
408 RETVAL 642 RETVAL
409 643
410#############################################################################
411 644
412MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
413
414void
415dump_ascii85 (PerlIO *fp, GdkPixbuf *pb)
416 CODE:
417{
418 int w = gdk_pixbuf_get_width (pb);
419 int h = gdk_pixbuf_get_height (pb);
420 int x, y, i;
421 guchar *dst;
422 int bpp = gdk_pixbuf_get_n_channels (pb);
423 guchar *src = gdk_pixbuf_get_pixels (pb);
424 int sstr = gdk_pixbuf_get_rowstride (pb);
425
426 a85_init ();
427
428 for (y = 0; y < h; y++)
429 for (x = 0; x < w; x++)
430 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
431 a85_push (fp, src [x * bpp + y * sstr + i]);
432
433 a85_finish (fp);
434}
435
436void
437dump_binary (PerlIO *fp, GdkPixbuf *pb)
438 CODE:
439{
440 int w = gdk_pixbuf_get_width (pb);
441 int h = gdk_pixbuf_get_height (pb);
442 int x, y, i;
443 guchar *dst;
444 int bpp = gdk_pixbuf_get_n_channels (pb);
445 guchar *src = gdk_pixbuf_get_pixels (pb);
446 int sstr = gdk_pixbuf_get_rowstride (pb);
447
448 for (y = 0; y < h; y++)
449 for (x = 0; x < w; x++)
450 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
451 PerlIO_putc (fp, src [x * bpp + y * sstr + i]);
452}
453
454#############################################################################
455
456MODULE = Gtk2::CV PACKAGE = Gtk2::CV
457
458SV *
459pb_to_hv84 (GdkPixbuf *pb)
460 CODE:
461{
462 int w = gdk_pixbuf_get_width (pb);
463 int h = gdk_pixbuf_get_height (pb);
464 int x, y;
465 guchar *dst;
466 int bpp = gdk_pixbuf_get_n_channels (pb);
467 guchar *src = gdk_pixbuf_get_pixels (pb);
468 int sstr = gdk_pixbuf_get_rowstride (pb);
469
470 RETVAL = newSV (6 * 8 * 12 / 8);
471 SvPOK_only (RETVAL);
472 SvCUR_set (RETVAL, 6 * 8 * 12 / 8);
473
474 dst = SvPVX (RETVAL);
475
476 /* some primitive error distribution + random dithering */
477
478 for (y = 0; y < h; y++)
479 {
480 guchar *p = src + y * sstr;
481
482 for (x = 0; x < w; x += 2)
483 {
484 unsigned int r, g, b, h, s, v, H, V1, V2;
485
486 if (bpp == 3)
487 r = *p++, g = *p++, b = *p++;
488 else if (bpp == 1)
489 r = g = b = *p++;
490 else
491 abort ();
492
493 rgb_to_hsv (r, g, b, &h, &s, &v);
494
495 H = (h * 15 / 255) << 4;
496 V1 = v;
497
498 if (bpp == 3)
499 r = *p++, g = *p++, b = *p++;
500 else if (bpp == 1)
501 r = g = b = *p++;
502 else
503 abort ();
504
505 rgb_to_hsv (r, g, b, &h, &s, &v);
506
507 H |= h * 15 / 255;
508 V2 = v;
509
510 *dst++ = H;
511 *dst++ = V1;
512 *dst++ = V2;
513 }
514 }
515}
516 OUTPUT:
517 RETVAL
518
519SV *
520hv84_to_av (unsigned char *hv84)
521 CODE:
522{
523 int i = 72 / 3;
524 AV *av = newAV ();
525
526 RETVAL = (SV *)newRV_noinc ((SV *)av);
527 while (i--)
528 {
529 int h = *hv84++;
530 int v1 = *hv84++;
531 int v2 = *hv84++;
532
533 av_push (av, newSViv (v1));
534 av_push (av, newSViv ((h >> 4) * 255 / 15));
535 av_push (av, newSViv (v2));
536 av_push (av, newSViv ((h & 15) * 255 / 15));
537 }
538}
539 OUTPUT:
540 RETVAL
541
542
543

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines