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.31 by root, Wed Feb 15 08:08:37 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_MIME);
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
171# missing in Gtk2 perl module
172
173gboolean
174gdk_net_wm_supports (GdkAtom property)
175 CODE:
176#if defined(GDK_WINDOWING_X11) && !defined(GDK_MULTIHEAD_SAFE)
177 RETVAL = gdk_net_wm_supports (property);
178#else
179 RETVAL = 0;
180#endif
181 OUTPUT:
182 RETVAL
183
129GdkPixbuf_noinc * 184GdkPixbuf_noinc *
130transpose (GdkPixbuf *pb) 185dealpha_expose (GdkPixbuf *pb)
131 CODE: 186 CODE:
132{ 187{
133 int w = gdk_pixbuf_get_width (pb); 188 int w = gdk_pixbuf_get_width (pb);
134 int h = gdk_pixbuf_get_height (pb); 189 int h = gdk_pixbuf_get_height (pb);
135 int bpp = gdk_pixbuf_get_n_channels (pb); 190 int bpp = gdk_pixbuf_get_n_channels (pb);
136 int x, y, i; 191 int x, y, i;
137 guchar *src = gdk_pixbuf_get_pixels (pb), *dst; 192 guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
138 int sstr = gdk_pixbuf_get_rowstride (pb), dstr; 193 int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
139 194
140 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, h, w); 195 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
141 196
142 dst = gdk_pixbuf_get_pixels (RETVAL); 197 dst = gdk_pixbuf_get_pixels (RETVAL);
143 dstr = gdk_pixbuf_get_rowstride (RETVAL); 198 dstr = gdk_pixbuf_get_rowstride (RETVAL);
144 199
145 for (y = 0; y < h; y++)
146 for (x = 0; x < w; x++) 200 for (x = 0; x < w; x++)
201 for (y = 0; y < h; y++)
147 for (i = 0; i < bpp; i++) 202 for (i = 0; i < 3; i++)
148 dst[y * bpp + x * dstr + i] = src[x * bpp + y * sstr + i]; 203 dst[x * 3 + y * dstr + i] = src[x * bpp + y * sstr + i];
149} 204}
150 OUTPUT: 205 OUTPUT:
151 RETVAL 206 RETVAL
152 207
153GdkPixbuf_noinc * 208GdkPixbuf_noinc *
154flop (GdkPixbuf *pb) 209rotate (GdkPixbuf *pb, int angle)
155 CODE: 210 CODE:
156{ 211 RETVAL = gdk_pixbuf_rotate_simple (pb, angle == 0 ? GDK_PIXBUF_ROTATE_NONE
157 int w = gdk_pixbuf_get_width (pb); 212 : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE
158 int h = gdk_pixbuf_get_height (pb); 213 : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN
159 int bpp = gdk_pixbuf_get_n_channels (pb); 214 : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE
160 int x, y, i; 215 : 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: 216 OUTPUT:
175 RETVAL 217 RETVAL
176 218
177GdkPixbuf_noinc * 219GdkPixbuf_noinc *
178load_jpeg (char *path, int thumbnail=0) 220load_jpeg (SV *path, int thumbnail=0)
179 CODE: 221 CODE:
180{ 222{
181 struct jpeg_decompress_struct cinfo; 223 struct jpeg_decompress_struct cinfo;
182 struct jpg_err_mgr jerr; 224 struct jpg_err_mgr jerr;
183 guchar *data; 225 guchar *data;
184 int rs; 226 int rs;
185 FILE *fp; 227 FILE *fp;
186 volatile GdkPixbuf *pb = 0; 228 volatile GdkPixbuf *pb = 0;
229
187 RETVAL = 0; 230 RETVAL = 0;
188 231
189 if (!(fp = fopen (path, "rb"))) 232 fp = fopen (SvPVbyte_nolen (path), "rb");
233
234 if (!fp)
190 XSRETURN_UNDEF; 235 XSRETURN_UNDEF;
191 236
192 cinfo.err = jpeg_std_error (&jerr.err); 237 cinfo.err = jpeg_std_error (&jerr.err);
193 238
194 jerr.err.error_exit = cv_error_exit; 239 jerr.err.error_exit = cv_error_exit;
225 { 270 {
226 cinfo.dct_method = JDCT_FASTEST; 271 cinfo.dct_method = JDCT_FASTEST;
227 cinfo.do_fancy_upsampling = FALSE; 272 cinfo.do_fancy_upsampling = FALSE;
228 273
229 while (cinfo.scale_denom < 8 274 while (cinfo.scale_denom < 8
230 && (cinfo.output_width >> 1) >= IW 275 && cinfo.output_width >= IW*4
231 && (cinfo.output_height >> 1) >= IH) 276 && cinfo.output_height >= IH*4)
232 { 277 {
233 cinfo.scale_denom <<= 1; 278 cinfo.scale_denom <<= 1;
234 jpeg_calc_output_dimensions (&cinfo); 279 jpeg_calc_output_dimensions (&cinfo);
235 } 280 }
236 } 281 }
275foldcase (SV *pathsv) 320foldcase (SV *pathsv)
276 PROTOTYPE: $ 321 PROTOTYPE: $
277 CODE: 322 CODE:
278{ 323{
279 STRLEN plen; 324 STRLEN plen;
280 U8 *path = SvPVutf8 (pathsv, plen); 325 U8 *path = (U8 *)SvPVutf8 (pathsv, plen);
281 U8 *pend = path + plen; 326 U8 *pend = path + plen;
282 U8 dst [plen * 6 * 3], *dstp = dst; 327 U8 dst [plen * 6 * 3], *dstp = dst;
283 328
284 while (path < pend) 329 while (path < pend)
285 { 330 {
331 U8 ch = *path;
332
286 if (*path >= '0' && *path <= '9') 333 if (ch >= 'a' && ch <= 'z')
334 *dstp++ = *path++;
335 else if (ch >= '0' && ch <= '9')
287 { 336 {
288 STRLEN el, nl = 0; 337 STRLEN el, nl = 0;
289 while (*path >= '0' && *path <= '9') 338 while (*path >= '0' && *path <= '9' && path < pend)
290 path++, nl++; 339 path++, nl++;
291 340
292 for (el = nl; el < 6; el++) 341 for (el = nl; el < 6; el++)
293 *dstp++ = '0'; 342 *dstp++ = '0';
294 343
302 dstp += cl; 351 dstp += cl;
303 path += is_utf8_char (path); 352 path += is_utf8_char (path);
304 } 353 }
305 } 354 }
306 355
307 RETVAL = newSVpvn (dst, dstp - dst); 356 RETVAL = newSVpvn ((const char *)dst, dstp - dst);
308} 357}
309 OUTPUT: 358 OUTPUT:
310 RETVAL 359 RETVAL
311 360
312GdkPixbuf_noinc * 361GdkPixbuf_noinc *
313p7_to_pb (int w, int h, guchar *src) 362p7_to_pb (int w, int h, SV *src_sv)
363 PROTOTYPE: @
314 CODE: 364 CODE:
315{ 365{
316 int x, y; 366 int x, y;
317 guchar *dst, *d; 367 guchar *dst, *d;
318 int dstr; 368 int dstr;
369 guchar *src = (guchar *)SvPVbyte_nolen (src_sv);
319 370
320 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h); 371 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
321 dst = gdk_pixbuf_get_pixels (RETVAL); 372 dst = gdk_pixbuf_get_pixels (RETVAL);
322 dstr = gdk_pixbuf_get_rowstride (RETVAL); 373 dstr = gdk_pixbuf_get_rowstride (RETVAL);
323 374
332 } 383 }
333} 384}
334 OUTPUT: 385 OUTPUT:
335 RETVAL 386 RETVAL
336 387
388#############################################################################
389
390MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
391
392void
393dump_ascii85 (PerlIO *fp, GdkPixbuf *pb)
394 CODE:
395{
396 int w = gdk_pixbuf_get_width (pb);
397 int h = gdk_pixbuf_get_height (pb);
398 int x, y, i;
399 guchar *dst;
400 int bpp = gdk_pixbuf_get_n_channels (pb);
401 guchar *src = gdk_pixbuf_get_pixels (pb);
402 int sstr = gdk_pixbuf_get_rowstride (pb);
403
404 a85_init ();
405
406 for (y = 0; y < h; y++)
407 for (x = 0; x < w; x++)
408 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
409 a85_push (fp, src [x * bpp + y * sstr + i]);
410
411 a85_finish (fp);
412}
413
414void
415dump_binary (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 for (y = 0; y < h; y++)
427 for (x = 0; x < w; x++)
428 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
429 PerlIO_putc (fp, src [x * bpp + y * sstr + i]);
430}
431
432#############################################################################
433
434MODULE = Gtk2::CV PACKAGE = Gtk2::CV
435
337SV * 436SV *
338pb_to_p7 (GdkPixbuf *pb) 437pb_to_hv84 (GdkPixbuf *pb)
339 CODE: 438 CODE:
340{ 439{
341 int w = gdk_pixbuf_get_width (pb); 440 int w = gdk_pixbuf_get_width (pb);
342 int h = gdk_pixbuf_get_height (pb); 441 int h = gdk_pixbuf_get_height (pb);
343 int x, y; 442 int x, y;
344 guchar *dst; 443 guchar *dst;
345 int bpp = gdk_pixbuf_get_n_channels (pb); 444 int bpp = gdk_pixbuf_get_n_channels (pb);
346 guchar *src = gdk_pixbuf_get_pixels (pb); 445 guchar *src = gdk_pixbuf_get_pixels (pb);
347 int sstr = gdk_pixbuf_get_rowstride (pb); 446 int sstr = gdk_pixbuf_get_rowstride (pb);
348 int Er[IW], Eg[IW], Eb[IW];
349 int seed = 77;
350 447
351 RETVAL = newSV (w * h); 448 RETVAL = newSV (6 * 8 * 12 / 8);
352 SvPOK_only (RETVAL); 449 SvPOK_only (RETVAL);
353 SvCUR_set (RETVAL, w * h); 450 SvCUR_set (RETVAL, 6 * 8 * 12 / 8);
354 451
355 dst = SvPVX (RETVAL); 452 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 453
361 /* some primitive error distribution + random dithering */ 454 /* some primitive error distribution + random dithering */
362 455
363 for (y = 0; y < h; y++) 456 for (y = 0; y < h; y++)
364 { 457 {
365 int er = 0, eg = 0, eb = 0; 458 guchar *p = src + y * sstr;
366 459
367 for (x = 0; x < w; x++) 460 for (x = 0; x < w; x += 2)
368 { 461 {
369 int r, g, b; 462 unsigned int r, g, b, h, s, v, H, V1, V2;
370 guchar *p = src + x * bpp + y * sstr;
371 463
372 r = ((p[0] + er + Er[x]) * 7 + (RAND & 127) + 64) / 255; 464 if (bpp == 3)
373 g = ((p[1] + eg + Eg[x]) * 7 + (RAND & 127) + 64) / 255; 465 r = *p++, g = *p++, b = *p++;
374 b = ((p[2] + eb + Eb[x]) * 3 + (RAND & 127) + 64) / 255; 466 else if (bpp == 1)
467 r = g = b = *p++;
468 else
469 abort ();
375 470
376 r = r > 7 ? 7 : r < 0 ? 0 : r; 471 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 472
380 er += p[0] - (r * 255 + 4) / 7; 473 H = (h * 15 / 255) << 4;
381 eg += p[1] - (g * 255 + 4) / 7; 474 V1 = v;
382 eb += p[2] - (b * 255 + 2) / 3;
383 475
384 Er[x] = er >> 1; er -= (er + 1) >> 1; 476 if (bpp == 3)
385 Eg[x] = eg >> 1; eg -= (eg + 1) >> 1; 477 r = *p++, g = *p++, b = *p++;
386 Eb[x] = eb >> 1; eb -= (eb + 1) >> 1; 478 else if (bpp == 1)
479 r = g = b = *p++;
480 else
481 abort ();
387 482
388 *dst++ = r << 5 | g << 2 | b; 483 rgb_to_hsv (r, g, b, &h, &s, &v);
484
485 H |= h * 15 / 255;
486 V2 = v;
487
488 *dst++ = H;
489 *dst++ = V1;
490 *dst++ = V2;
389 } 491 }
390 } 492 }
391} 493}
392 OUTPUT: 494 OUTPUT:
393 RETVAL 495 RETVAL
394 496
395SV * 497SV *
498hv84_to_av (unsigned char *hv84)
499 CODE:
500{
501 int i = 72 / 3;
502 AV *av = newAV ();
503
504 RETVAL = (SV *)newRV_noinc ((SV *)av);
505 while (i--)
506 {
507 int h = *hv84++;
508 int v1 = *hv84++;
509 int v2 = *hv84++;
510
511 av_push (av, newSViv (v1));
512 av_push (av, newSViv ((h >> 4) * 255 / 15));
513 av_push (av, newSViv (v2));
514 av_push (av, newSViv ((h & 15) * 255 / 15));
515 }
516}
517 OUTPUT:
518 RETVAL
519
520#############################################################################
521
522MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Plugin::RCluster
523
524SV *
396make_histogram (SV *ar) 525make_histograms (SV *ar)
397 CODE: 526 CODE:
398{ 527{
399 int i; 528 int i;
400 AV *av, *result; 529 AV *av, *result;
401 530
444 RETVAL = newRV_noinc ((SV *)result); 573 RETVAL = newRV_noinc ((SV *)result);
445} 574}
446 OUTPUT: 575 OUTPUT:
447 RETVAL 576 RETVAL
448 577
449#############################################################################
450 578
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