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.30 by root, Wed Dec 28 20:56:20 2005 UTC

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