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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines