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

Comparing CV/CV.xs (file contents):
Revision 1.18 by root, Wed Jul 20 09:06:37 2005 UTC vs.
Revision 1.32 by root, Wed Feb 15 08:36:55 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines