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