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

Comparing CV/CV.xs (file contents):
Revision 1.7 by root, Wed Nov 12 23:54:54 2003 UTC vs.
Revision 1.26 by root, Sun Aug 21 02:18:30 2005 UTC

1#include "EXTERN.h" 1#include "EXTERN.h"
2#include "perl.h" 2#include "perl.h"
3#include "XSUB.h" 3#include "XSUB.h"
4 4
5#include <string.h>
6#include <setjmp.h>
7
8#include <jpeglib.h>
9#include <glib.h>
10#include <gtk/gtk.h>
5#include <gdk-pixbuf/gdk-pixbuf.h> 11#include <gdk-pixbuf/gdk-pixbuf.h>
6 12
7#include <gperl.h> 13#include <gperl.h>
8#include <gtk2perl.h> 14#include <gtk2perl.h>
9 15
10#define IW 80 16#define IW 80 /* MUST match Schnauer.pm! */
17#define IH 60 /* MUST match Schnauer.pm! */
11 18
12#define RAND (seed = (seed + 7141) * 54773 % 134456) 19#define RAND (seed = (seed + 7141) * 54773 % 134456)
13 20
14#define LINELENGTH 240 21#define LINELENGTH 240
15 22
16#define ELLIPSIS "\xe2\x80\xa6" 23#define ELLIPSIS "\xe2\x80\xa6"
24
25struct jpg_err_mgr
26{
27 struct jpeg_error_mgr err;
28 jmp_buf setjmp_buffer;
29};
30
31static void
32cv_error_exit (j_common_ptr cinfo)
33{
34 longjmp (((struct jpg_err_mgr *)cinfo->err)->setjmp_buffer, 99);
35}
36
37static void
38cv_error_output (j_common_ptr cinfo)
39{
40 return;
41}
42
43static void
44rgb_to_hsv (unsigned int r, unsigned int g, unsigned int b,
45 unsigned int *h, unsigned int *s, unsigned int *v)
46{
47 unsigned int mx = r; if (g > mx) mx = g; if (b > mx) mx = b;
48 unsigned int mn = r; if (g < mn) mn = g; if (b < mn) mn = b;
49 unsigned int delta = mx - mn;
50
51 *v = mx;
52
53 *s = mx ? delta * 255 / mx : 0;
54
55 if (delta == 0)
56 *h = 0;
57 else
58 {
59 if (r == mx)
60 *h = ((int)g - (int)b) * 255 / (int)(delta * 3);
61 else if (g == mx)
62 *h = ((int)b - (int)r) * 255 / (int)(delta * 3) + 52;
63 else if (b == mx)
64 *h = ((int)r - (int)g) * 255 / (int)(delta * 3) + 103;
65
66 *h &= 255;
67 }
68}
17 69
18static guint32 a85_val; 70static guint32 a85_val;
19static guint a85_cnt; 71static guint a85_cnt;
20static guchar a85_buf[LINELENGTH], *a85_ptr; 72static guchar a85_buf[LINELENGTH], *a85_ptr;
21 73
55 } 107 }
56 } 108 }
57 109
58} 110}
59 111
112static void
60a85_finish (PerlIO *fp) 113a85_finish (PerlIO *fp)
61{ 114{
62 while (a85_cnt != 4) 115 while (a85_cnt != 4)
63 a85_push (fp, 0); 116 a85_push (fp, 0);
64 117
69 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf); 122 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf);
70} 123}
71 124
72///////////////////////////////////////////////////////////////////////////// 125/////////////////////////////////////////////////////////////////////////////
73 126
74MODULE = Gtk2::CV PACKAGE = Gtk2::CV::ImageWindow 127MODULE = Gtk2::CV PACKAGE = Gtk2::CV
75 128
76PROTOTYPES: ENABLE 129PROTOTYPES: ENABLE
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
144GdkPixbuf_noinc *
145dealpha_expose (GdkPixbuf *pb)
146 CODE:
147{
148 int w = gdk_pixbuf_get_width (pb);
149 int h = gdk_pixbuf_get_height (pb);
150 fprintf (stderr, "new %d %d\n", w, h);
151 int bpp = gdk_pixbuf_get_n_channels (pb);
152 int x, y, i;
153 guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
154 int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
155
156 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
157
158 dst = gdk_pixbuf_get_pixels (RETVAL);
159 dstr = gdk_pixbuf_get_rowstride (RETVAL);
160
161 for (x = 0; x < w; x++)
162 for (y = 0; y < h; y++)
163 for (i = 0; i < 3; i++)
164 dst[x * 3 + y * dstr + i] = src[x * bpp + y * sstr + i];
165}
166 OUTPUT:
167 RETVAL
77 168
78GdkPixbuf_noinc * 169GdkPixbuf_noinc *
79transpose (GdkPixbuf *pb) 170transpose (GdkPixbuf *pb)
80 CODE: 171 CODE:
81{ 172{
121 dst[(w - 1 - x) * bpp + y * dstr + i] = src[x * bpp + y * sstr + i]; 212 dst[(w - 1 - x) * bpp + y * dstr + i] = src[x * bpp + y * sstr + i];
122} 213}
123 OUTPUT: 214 OUTPUT:
124 RETVAL 215 RETVAL
125 216
217GdkPixbuf_noinc *
218load_jpeg (SV *path, int thumbnail=0)
219 CODE:
220{
221 struct jpeg_decompress_struct cinfo;
222 struct jpg_err_mgr jerr;
223 guchar *data;
224 int rs;
225 FILE *fp;
226 volatile GdkPixbuf *pb = 0;
227 gchar *filename;
228
229 RETVAL = 0;
230
231 filename = g_filename_from_utf8 (SvPVutf8_nolen (path), -1, 0, 0, 0);
232 fp = fopen (filename, "rb");
233 g_free (filename);
234
235 if (!fp)
236 XSRETURN_UNDEF;
237
238 cinfo.err = jpeg_std_error (&jerr.err);
239
240 jerr.err.error_exit = cv_error_exit;
241 jerr.err.output_message = cv_error_output;
242
243 if ((rs = setjmp (jerr.setjmp_buffer)))
244 {
245 fclose (fp);
246 jpeg_destroy_decompress (&cinfo);
247
248 if (pb)
249 g_object_unref ((gpointer)pb);
250
251 XSRETURN_UNDEF;
252 }
253
254 jpeg_create_decompress (&cinfo);
255
256 jpeg_stdio_src (&cinfo, fp);
257 jpeg_read_header (&cinfo, TRUE);
258
259 cinfo.dct_method = JDCT_DEFAULT;
260 cinfo.do_fancy_upsampling = FALSE; /* worse quality, but nobody compained so far, and gdk-pixbuf does the same */
261 cinfo.do_block_smoothing = FALSE;
262 cinfo.out_color_space = JCS_RGB;
263 cinfo.quantize_colors = FALSE;
264
265 cinfo.scale_num = 1;
266 cinfo.scale_denom = 1;
267
268 jpeg_calc_output_dimensions (&cinfo);
269
270 if (thumbnail)
271 {
272 cinfo.dct_method = JDCT_FASTEST;
273 cinfo.do_fancy_upsampling = FALSE;
274
275 while (cinfo.scale_denom < 8
276 && cinfo.output_width >= IW*4
277 && cinfo.output_height >= IH*4)
278 {
279 cinfo.scale_denom <<= 1;
280 jpeg_calc_output_dimensions (&cinfo);
281 }
282 }
283
284 pb = RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, cinfo.output_width, cinfo.output_height);
285 if (!RETVAL)
286 longjmp (jerr.setjmp_buffer, 2);
287
288 data = gdk_pixbuf_get_pixels (RETVAL);
289 rs = gdk_pixbuf_get_rowstride (RETVAL);
290
291 if (cinfo.output_components != 3)
292 longjmp (jerr.setjmp_buffer, 3);
293
294 jpeg_start_decompress (&cinfo);
295
296 while (cinfo.output_scanline < cinfo.output_height)
297 {
298 int remaining = cinfo.output_height - cinfo.output_scanline;
299 JSAMPROW rp[4];
300
301 rp [0] = data + cinfo.output_scanline * rs;
302 rp [1] = (guchar *)rp [0] + rs;
303 rp [2] = (guchar *)rp [1] + rs;
304 rp [3] = (guchar *)rp [2] + rs;
305
306 jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4);
307 }
308
309 jpeg_finish_decompress (&cinfo);
310 fclose (fp);
311 jpeg_destroy_decompress (&cinfo);
312}
313 OUTPUT:
314 RETVAL
315
126############################################################################# 316#############################################################################
127 317
128MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer 318MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer
129 319
320SV *
321foldcase (SV *pathsv)
322 PROTOTYPE: $
323 CODE:
324{
325 STRLEN plen;
326 U8 *path = (U8 *)SvPVutf8 (pathsv, plen);
327 U8 *pend = path + plen;
328 U8 dst [plen * 6 * 3], *dstp = dst;
329
330 while (path < pend)
331 {
332 U8 ch = *path;
333
334 if (ch >= 'a' && ch <= 'z')
335 *dstp++ = *path++;
336 else if (ch >= '0' && ch <= '9')
337 {
338 STRLEN el, nl = 0;
339 while (*path >= '0' && *path <= '9' && path < pend)
340 path++, nl++;
341
342 for (el = nl; el < 6; el++)
343 *dstp++ = '0';
344
345 memcpy (dstp, path - nl, nl);
346 dstp += nl;
347 }
348 else
349 {
350 STRLEN cl;
351 to_utf8_fold (path, dstp, &cl);
352 dstp += cl;
353 path += is_utf8_char (path);
354 }
355 }
356
357 RETVAL = newSVpvn ((const char *)dst, dstp - dst);
358}
359 OUTPUT:
360 RETVAL
361
130GdkPixbuf_noinc * 362GdkPixbuf_noinc *
131p7_to_pb (int w, int h, guchar *src) 363p7_to_pb (int w, int h, SV *src_sv)
364 PROTOTYPE: @
132 CODE: 365 CODE:
133{ 366{
134 int x, y; 367 int x, y;
135 guchar *dst, *d; 368 guchar *dst, *d;
136 int dstr; 369 int dstr;
370 guchar *src = (guchar *)SvPVbyte_nolen (src_sv);
137 371
138 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h); 372 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
139 dst = gdk_pixbuf_get_pixels (RETVAL); 373 dst = gdk_pixbuf_get_pixels (RETVAL);
140 dstr = gdk_pixbuf_get_rowstride (RETVAL); 374 dstr = gdk_pixbuf_get_rowstride (RETVAL);
141 375
168 402
169 RETVAL = newSV (w * h); 403 RETVAL = newSV (w * h);
170 SvPOK_only (RETVAL); 404 SvPOK_only (RETVAL);
171 SvCUR_set (RETVAL, w * h); 405 SvCUR_set (RETVAL, w * h);
172 406
173 dst = SvPVX (RETVAL); 407 dst = (guchar *)SvPVX (RETVAL);
174 408
175 memset (Er, 0, sizeof (int) * IW); 409 memset (Er, 0, sizeof (int) * IW);
176 memset (Eg, 0, sizeof (int) * IW); 410 memset (Eg, 0, sizeof (int) * IW);
177 memset (Eb, 0, sizeof (int) * IW); 411 memset (Eb, 0, sizeof (int) * IW);
178 412
185 for (x = 0; x < w; x++) 419 for (x = 0; x < w; x++)
186 { 420 {
187 int r, g, b; 421 int r, g, b;
188 guchar *p = src + x * bpp + y * sstr; 422 guchar *p = src + x * bpp + y * sstr;
189 423
190 r = ((p[0] + er + Er[x]) * 7 + 128) / 255; 424 r = ((p[0] + er + Er[x]) * 7 + (RAND & 127) + 64) / 255;
191 g = ((p[1] + eg + Eg[x]) * 7 + 128) / 255; 425 g = ((p[1] + eg + Eg[x]) * 7 + (RAND & 127) + 64) / 255;
192 b = ((p[2] + eb + Eb[x]) * 3 + 128) / 255; 426 b = ((p[2] + eb + Eb[x]) * 3 + (RAND & 127) + 64) / 255;
193 427
194 r = r > 7 ? 7 : r < 0 ? 0 : r; 428 r = r > 7 ? 7 : r < 0 ? 0 : r;
195 g = g > 7 ? 7 : g < 0 ? 0 : g; 429 g = g > 7 ? 7 : g < 0 ? 0 : g;
196 b = b > 3 ? 3 : b < 0 ? 0 : b; 430 b = b > 3 ? 3 : b < 0 ? 0 : b;
197 431
198 er += p[0] - (r * 255 + 4) / 7; 432 er += p[0] - (r * 255 + 4) / 7;
199 eg += p[1] - (g * 255 + 4) / 7; 433 eg += p[1] - (g * 255 + 4) / 7;
200 eb += p[2] - (b * 255 + 2) / 3; 434 eb += p[2] - (b * 255 + 2) / 3;
201 435
202 Er[x] = er / 2; er -= er / 2 + RAND % 7 - 3; 436 Er[x] = er >> 1; er -= (er + 1) >> 1;
203 Eg[x] = eg / 2; eg -= eg / 2 + RAND % 7 - 3; 437 Eg[x] = eg >> 1; eg -= (eg + 1) >> 1;
204 Eb[x] = eb / 2; eb -= eb / 2 + RAND % 7 - 3; 438 Eb[x] = eb >> 1; eb -= (eb + 1) >> 1;
205 439
206 *dst++ = r << 5 | g << 2 | b; 440 *dst++ = r << 5 | g << 2 | b;
207 } 441 }
208 } 442 }
209} 443}
252 for (x = 0; x < w; x++) 486 for (x = 0; x < w; x++)
253 for (i = 0; i < (bpp < 3 ? 1 : 3); i++) 487 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
254 PerlIO_putc (fp, src [x * bpp + y * sstr + i]); 488 PerlIO_putc (fp, src [x * bpp + y * sstr + i]);
255} 489}
256 490
491#############################################################################
257 492
493MODULE = Gtk2::CV PACKAGE = Gtk2::CV
258 494
495SV *
496pb_to_hv84 (GdkPixbuf *pb)
497 CODE:
498{
499 int w = gdk_pixbuf_get_width (pb);
500 int h = gdk_pixbuf_get_height (pb);
501 int x, y;
502 guchar *dst;
503 int bpp = gdk_pixbuf_get_n_channels (pb);
504 guchar *src = gdk_pixbuf_get_pixels (pb);
505 int sstr = gdk_pixbuf_get_rowstride (pb);
259 506
507 RETVAL = newSV (6 * 8 * 12 / 8);
508 SvPOK_only (RETVAL);
509 SvCUR_set (RETVAL, 6 * 8 * 12 / 8);
510
511 dst = (guchar *)SvPVX (RETVAL);
512
513 /* some primitive error distribution + random dithering */
514
515 for (y = 0; y < h; y++)
516 {
517 guchar *p = src + y * sstr;
518
519 for (x = 0; x < w; x += 2)
520 {
521 unsigned int r, g, b, h, s, v, H, V1, V2;
522
523 if (bpp == 3)
524 r = *p++, g = *p++, b = *p++;
525 else if (bpp == 1)
526 r = g = b = *p++;
527 else
528 abort ();
529
530 rgb_to_hsv (r, g, b, &h, &s, &v);
531
532 H = (h * 15 / 255) << 4;
533 V1 = v;
534
535 if (bpp == 3)
536 r = *p++, g = *p++, b = *p++;
537 else if (bpp == 1)
538 r = g = b = *p++;
539 else
540 abort ();
541
542 rgb_to_hsv (r, g, b, &h, &s, &v);
543
544 H |= h * 15 / 255;
545 V2 = v;
546
547 *dst++ = H;
548 *dst++ = V1;
549 *dst++ = V2;
550 }
551 }
552}
553 OUTPUT:
554 RETVAL
555
556SV *
557hv84_to_av (unsigned char *hv84)
558 CODE:
559{
560 int i = 72 / 3;
561 AV *av = newAV ();
562
563 RETVAL = (SV *)newRV_noinc ((SV *)av);
564 while (i--)
565 {
566 int h = *hv84++;
567 int v1 = *hv84++;
568 int v2 = *hv84++;
569
570 av_push (av, newSViv (v1));
571 av_push (av, newSViv ((h >> 4) * 255 / 15));
572 av_push (av, newSViv (v2));
573 av_push (av, newSViv ((h & 15) * 255 / 15));
574 }
575}
576 OUTPUT:
577 RETVAL
578
579#############################################################################
580
581MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Plugin::RCluster
582
583SV *
584make_histograms (SV *ar)
585 CODE:
586{
587 int i;
588 AV *av, *result;
589
590 if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV)
591 croak ("Not an array ref as first argument to make_histogram");
592
593 av = (AV *) SvRV (ar);
594 result = newAV ();
595
596 for (i = 0; i <= av_len (av); ++i)
597 {
598 const int HISTSIZE = 64;
599
600 int j;
601 SV *sv = *av_fetch (av, i, 1);
602 STRLEN len;
603 char *buf = SvPVbyte (sv, len);
604
605 int tmphist[HISTSIZE];
606 float *hist;
607
608 SV *histsv = newSV (HISTSIZE * sizeof (float) + 1);
609 SvPOK_on (histsv);
610 SvCUR_set (histsv, HISTSIZE * sizeof (float));
611 hist = (float *)SvPVX (histsv);
612
613 Zero (tmphist, sizeof (tmphist), char);
614
615 for (j = len; j--; )
616 {
617 unsigned int idx
618 = ((*buf & 0xc0) >> 2)
619 | ((*buf & 0x18) >> 1)
620 | (*buf & 0x03);
621
622 ++tmphist[idx];
623 ++buf;
624 }
625
626 for (j = 0; j < HISTSIZE; ++j)
627 hist[j] = (float)tmphist[j] / (len + 1e-30);
628
629 av_push (result, histsv);
630 }
631
632 RETVAL = newRV_noinc ((SV *)result);
633}
634 OUTPUT:
635 RETVAL
636
637

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines