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

Comparing CV/CV.xs (file contents):
Revision 1.9 by root, Tue Apr 6 23:53:20 2004 UTC vs.
Revision 1.31 by root, Wed Feb 15 08:08:37 2006 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 <magic.h>
9
10#include <jpeglib.h>
11#include <glib.h>
12#include <gtk/gtk.h>
5#include <gdk-pixbuf/gdk-pixbuf.h> 13#include <gdk-pixbuf/gdk-pixbuf.h>
6 14
7#include <gperl.h> 15#include <gperl.h>
8#include <gtk2perl.h> 16#include <gtk2perl.h>
9 17
10#define IW 80 18#define IW 80 /* MUST match Schnauer.pm! */
19#define IH 60 /* MUST match Schnauer.pm! */
11 20
12#define RAND (seed = (seed + 7141) * 54773 % 134456) 21#define RAND (seed = (seed + 7141) * 54773 % 134456)
13 22
14#define LINELENGTH 240 23#define LINELENGTH 240
15 24
16#define ELLIPSIS "\xe2\x80\xa6" 25#define ELLIPSIS "\xe2\x80\xa6"
26
27struct jpg_err_mgr
28{
29 struct jpeg_error_mgr err;
30 jmp_buf setjmp_buffer;
31};
32
33static void
34cv_error_exit (j_common_ptr cinfo)
35{
36 longjmp (((struct jpg_err_mgr *)cinfo->err)->setjmp_buffer, 99);
37}
38
39static void
40cv_error_output (j_common_ptr cinfo)
41{
42 return;
43}
44
45static void
46rgb_to_hsv (unsigned int r, unsigned int g, unsigned int b,
47 unsigned int *h, unsigned int *s, unsigned int *v)
48{
49 unsigned int mx = r; if (g > mx) mx = g; if (b > mx) mx = b;
50 unsigned int mn = r; if (g < mn) mn = g; if (b < mn) mn = b;
51 unsigned int delta = mx - mn;
52
53 *v = mx;
54
55 *s = mx ? delta * 255 / mx : 0;
56
57 if (delta == 0)
58 *h = 0;
59 else
60 {
61 if (r == mx)
62 *h = ((int)g - (int)b) * 255 / (int)(delta * 3);
63 else if (g == mx)
64 *h = ((int)b - (int)r) * 255 / (int)(delta * 3) + 52;
65 else if (b == mx)
66 *h = ((int)r - (int)g) * 255 / (int)(delta * 3) + 103;
67
68 *h &= 255;
69 }
70}
17 71
18static guint32 a85_val; 72static guint32 a85_val;
19static guint a85_cnt; 73static guint a85_cnt;
20static guchar a85_buf[LINELENGTH], *a85_ptr; 74static guchar a85_buf[LINELENGTH], *a85_ptr;
21 75
68 *a85_ptr++ = '\n'; 122 *a85_ptr++ = '\n';
69 123
70 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf); 124 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf);
71} 125}
72 126
73static void
74rgb_to_hsv (unsigned int r, unsigned int g, unsigned int b,
75 unsigned int *h, unsigned int *s, unsigned int *v)
76{
77 unsigned int mx = r; if (g > mx) mx = g; if (b > mx) mx = b;
78 unsigned int mn = r; if (g < mn) mn = g; if (b < mn) mn = b;
79 unsigned int delta = mx - mn;
80
81 *v = mx;
82
83 *s = mx ? delta * 255 / mx : 0;
84
85 if (delta == 0)
86 *h = 0;
87 else
88 {
89 if (r == mx)
90 *h = ((int)g - (int)b) * 255 / (int)(delta * 3);
91 else if (g == mx)
92 *h = ((int)b - (int)r) * 255 / (int)(delta * 3) + 52;
93 else if (b == mx)
94 *h = ((int)r - (int)g) * 255 / (int)(delta * 3) + 103;
95
96 *h &= 255;
97 }
98}
99
100///////////////////////////////////////////////////////////////////////////// 127/////////////////////////////////////////////////////////////////////////////
101 128
102MODULE = Gtk2::CV PACKAGE = Gtk2::CV 129MODULE = Gtk2::CV PACKAGE = Gtk2::CV
103 130
104PROTOTYPES: ENABLE 131PROTOTYPES: ENABLE
105 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
106GdkPixbuf_noinc * 184GdkPixbuf_noinc *
107transpose (GdkPixbuf *pb) 185dealpha_expose (GdkPixbuf *pb)
108 CODE: 186 CODE:
109{ 187{
110 int w = gdk_pixbuf_get_width (pb); 188 int w = gdk_pixbuf_get_width (pb);
111 int h = gdk_pixbuf_get_height (pb); 189 int h = gdk_pixbuf_get_height (pb);
112 int bpp = gdk_pixbuf_get_n_channels (pb); 190 int bpp = gdk_pixbuf_get_n_channels (pb);
113 int x, y, i; 191 int x, y, i;
114 guchar *src = gdk_pixbuf_get_pixels (pb), *dst; 192 guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
115 int sstr = gdk_pixbuf_get_rowstride (pb), dstr; 193 int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
116 194
117 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, h, w); 195 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
118 196
119 dst = gdk_pixbuf_get_pixels (RETVAL); 197 dst = gdk_pixbuf_get_pixels (RETVAL);
120 dstr = gdk_pixbuf_get_rowstride (RETVAL); 198 dstr = gdk_pixbuf_get_rowstride (RETVAL);
121 199
122 for (y = 0; y < h; y++)
123 for (x = 0; x < w; x++) 200 for (x = 0; x < w; x++)
201 for (y = 0; y < h; y++)
124 for (i = 0; i < bpp; i++) 202 for (i = 0; i < 3; i++)
125 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];
126} 204}
127 OUTPUT: 205 OUTPUT:
128 RETVAL 206 RETVAL
129 207
130GdkPixbuf_noinc * 208GdkPixbuf_noinc *
131flop (GdkPixbuf *pb) 209rotate (GdkPixbuf *pb, int angle)
132 CODE: 210 CODE:
133{ 211 RETVAL = gdk_pixbuf_rotate_simple (pb, angle == 0 ? GDK_PIXBUF_ROTATE_NONE
134 int w = gdk_pixbuf_get_width (pb); 212 : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE
135 int h = gdk_pixbuf_get_height (pb); 213 : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN
136 int bpp = gdk_pixbuf_get_n_channels (pb); 214 : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE
137 int x, y, i; 215 : angle);
138 guchar *src = gdk_pixbuf_get_pixels (pb), *dst; 216 OUTPUT:
139 int sstr = gdk_pixbuf_get_rowstride (pb), dstr; 217 RETVAL
140 218
141 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, w, h); 219GdkPixbuf_noinc *
220load_jpeg (SV *path, int thumbnail=0)
221 CODE:
222{
223 struct jpeg_decompress_struct cinfo;
224 struct jpg_err_mgr jerr;
225 guchar *data;
226 int rs;
227 FILE *fp;
228 volatile GdkPixbuf *pb = 0;
142 229
230 RETVAL = 0;
231
232 fp = fopen (SvPVbyte_nolen (path), "rb");
233
234 if (!fp)
235 XSRETURN_UNDEF;
236
237 cinfo.err = jpeg_std_error (&jerr.err);
238
239 jerr.err.error_exit = cv_error_exit;
240 jerr.err.output_message = cv_error_output;
241
242 if ((rs = setjmp (jerr.setjmp_buffer)))
243 {
244 fclose (fp);
245 jpeg_destroy_decompress (&cinfo);
246
247 if (pb)
248 g_object_unref ((gpointer)pb);
249
250 XSRETURN_UNDEF;
251 }
252
253 jpeg_create_decompress (&cinfo);
254
255 jpeg_stdio_src (&cinfo, fp);
256 jpeg_read_header (&cinfo, TRUE);
257
258 cinfo.dct_method = JDCT_DEFAULT;
259 cinfo.do_fancy_upsampling = FALSE; /* worse quality, but nobody compained so far, and gdk-pixbuf does the same */
260 cinfo.do_block_smoothing = FALSE;
261 cinfo.out_color_space = JCS_RGB;
262 cinfo.quantize_colors = FALSE;
263
264 cinfo.scale_num = 1;
265 cinfo.scale_denom = 1;
266
267 jpeg_calc_output_dimensions (&cinfo);
268
269 if (thumbnail)
270 {
271 cinfo.dct_method = JDCT_FASTEST;
272 cinfo.do_fancy_upsampling = FALSE;
273
274 while (cinfo.scale_denom < 8
275 && cinfo.output_width >= IW*4
276 && cinfo.output_height >= IH*4)
277 {
278 cinfo.scale_denom <<= 1;
279 jpeg_calc_output_dimensions (&cinfo);
280 }
281 }
282
283 pb = RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, cinfo.output_width, cinfo.output_height);
284 if (!RETVAL)
285 longjmp (jerr.setjmp_buffer, 2);
286
143 dst = gdk_pixbuf_get_pixels (RETVAL); 287 data = gdk_pixbuf_get_pixels (RETVAL);
144 dstr = gdk_pixbuf_get_rowstride (RETVAL); 288 rs = gdk_pixbuf_get_rowstride (RETVAL);
145 289
146 for (y = 0; y < h; y++) 290 if (cinfo.output_components != 3)
147 for (x = 0; x < w; x++) 291 longjmp (jerr.setjmp_buffer, 3);
148 for (i = 0; i < bpp; i++) 292
149 dst[(w - 1 - x) * bpp + y * dstr + i] = src[x * bpp + y * sstr + i]; 293 jpeg_start_decompress (&cinfo);
294
295 while (cinfo.output_scanline < cinfo.output_height)
296 {
297 int remaining = cinfo.output_height - cinfo.output_scanline;
298 JSAMPROW rp[4];
299
300 rp [0] = data + cinfo.output_scanline * rs;
301 rp [1] = (guchar *)rp [0] + rs;
302 rp [2] = (guchar *)rp [1] + rs;
303 rp [3] = (guchar *)rp [2] + rs;
304
305 jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4);
306 }
307
308 jpeg_finish_decompress (&cinfo);
309 fclose (fp);
310 jpeg_destroy_decompress (&cinfo);
150} 311}
151 OUTPUT: 312 OUTPUT:
152 RETVAL 313 RETVAL
153 314
154############################################################################# 315#############################################################################
155 316
156MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer 317MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer
157 318
319SV *
320foldcase (SV *pathsv)
321 PROTOTYPE: $
322 CODE:
323{
324 STRLEN plen;
325 U8 *path = (U8 *)SvPVutf8 (pathsv, plen);
326 U8 *pend = path + plen;
327 U8 dst [plen * 6 * 3], *dstp = dst;
328
329 while (path < pend)
330 {
331 U8 ch = *path;
332
333 if (ch >= 'a' && ch <= 'z')
334 *dstp++ = *path++;
335 else if (ch >= '0' && ch <= '9')
336 {
337 STRLEN el, nl = 0;
338 while (*path >= '0' && *path <= '9' && path < pend)
339 path++, nl++;
340
341 for (el = nl; el < 6; el++)
342 *dstp++ = '0';
343
344 memcpy (dstp, path - nl, nl);
345 dstp += nl;
346 }
347 else
348 {
349 STRLEN cl;
350 to_utf8_fold (path, dstp, &cl);
351 dstp += cl;
352 path += is_utf8_char (path);
353 }
354 }
355
356 RETVAL = newSVpvn ((const char *)dst, dstp - dst);
357}
358 OUTPUT:
359 RETVAL
360
158GdkPixbuf_noinc * 361GdkPixbuf_noinc *
159p7_to_pb (int w, int h, guchar *src) 362p7_to_pb (int w, int h, SV *src_sv)
363 PROTOTYPE: @
160 CODE: 364 CODE:
161{ 365{
162 int x, y; 366 int x, y;
163 guchar *dst, *d; 367 guchar *dst, *d;
164 int dstr; 368 int dstr;
369 guchar *src = (guchar *)SvPVbyte_nolen (src_sv);
165 370
166 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h); 371 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
167 dst = gdk_pixbuf_get_pixels (RETVAL); 372 dst = gdk_pixbuf_get_pixels (RETVAL);
168 dstr = gdk_pixbuf_get_rowstride (RETVAL); 373 dstr = gdk_pixbuf_get_rowstride (RETVAL);
169 374
178 } 383 }
179} 384}
180 OUTPUT: 385 OUTPUT:
181 RETVAL 386 RETVAL
182 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
183SV * 436SV *
184pb_to_p7 (GdkPixbuf *pb) 437pb_to_hv84 (GdkPixbuf *pb)
185 CODE: 438 CODE:
186{ 439{
187 int w = gdk_pixbuf_get_width (pb); 440 int w = gdk_pixbuf_get_width (pb);
188 int h = gdk_pixbuf_get_height (pb); 441 int h = gdk_pixbuf_get_height (pb);
189 int x, y; 442 int x, y;
190 guchar *dst; 443 guchar *dst;
191 int bpp = gdk_pixbuf_get_n_channels (pb); 444 int bpp = gdk_pixbuf_get_n_channels (pb);
192 guchar *src = gdk_pixbuf_get_pixels (pb); 445 guchar *src = gdk_pixbuf_get_pixels (pb);
193 int sstr = gdk_pixbuf_get_rowstride (pb); 446 int sstr = gdk_pixbuf_get_rowstride (pb);
194 int Er[IW], Eg[IW], Eb[IW];
195 int seed = 77;
196
197 RETVAL = newSV (w * h);
198 SvPOK_only (RETVAL);
199 SvCUR_set (RETVAL, w * h);
200
201 dst = SvPVX (RETVAL);
202
203 memset (Er, 0, sizeof (int) * IW);
204 memset (Eg, 0, sizeof (int) * IW);
205 memset (Eb, 0, sizeof (int) * IW);
206
207 /* some primitive error distribution + random dithering */
208
209 for (y = 0; y < h; y++)
210 {
211 int er = 0, eg = 0, eb = 0;
212
213 for (x = 0; x < w; x++)
214 {
215 int r, g, b;
216 guchar *p = src + x * bpp + y * sstr;
217
218 r = ((p[0] + er + Er[x]) * 7 + 128) / 255;
219 g = ((p[1] + eg + Eg[x]) * 7 + 128) / 255;
220 b = ((p[2] + eb + Eb[x]) * 3 + 128) / 255;
221
222 r = r > 7 ? 7 : r < 0 ? 0 : r;
223 g = g > 7 ? 7 : g < 0 ? 0 : g;
224 b = b > 3 ? 3 : b < 0 ? 0 : b;
225
226 er += p[0] - (r * 255 + 4) / 7;
227 eg += p[1] - (g * 255 + 4) / 7;
228 eb += p[2] - (b * 255 + 2) / 3;
229
230 Er[x] = er / 2; er -= er / 2 + RAND % 7 - 3;
231 Eg[x] = eg / 2; eg -= eg / 2 + RAND % 7 - 3;
232 Eb[x] = eb / 2; eb -= eb / 2 + RAND % 7 - 3;
233
234 *dst++ = r << 5 | g << 2 | b;
235 }
236 }
237}
238 OUTPUT:
239 RETVAL
240
241#############################################################################
242
243MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
244
245void
246dump_ascii85 (PerlIO *fp, GdkPixbuf *pb)
247 CODE:
248{
249 int w = gdk_pixbuf_get_width (pb);
250 int h = gdk_pixbuf_get_height (pb);
251 int x, y, i;
252 guchar *dst;
253 int bpp = gdk_pixbuf_get_n_channels (pb);
254 guchar *src = gdk_pixbuf_get_pixels (pb);
255 int sstr = gdk_pixbuf_get_rowstride (pb);
256
257 a85_init ();
258
259 for (y = 0; y < h; y++)
260 for (x = 0; x < w; x++)
261 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
262 a85_push (fp, src [x * bpp + y * sstr + i]);
263
264 a85_finish (fp);
265}
266
267void
268dump_binary (PerlIO *fp, GdkPixbuf *pb)
269 CODE:
270{
271 int w = gdk_pixbuf_get_width (pb);
272 int h = gdk_pixbuf_get_height (pb);
273 int x, y, i;
274 guchar *dst;
275 int bpp = gdk_pixbuf_get_n_channels (pb);
276 guchar *src = gdk_pixbuf_get_pixels (pb);
277 int sstr = gdk_pixbuf_get_rowstride (pb);
278
279 for (y = 0; y < h; y++)
280 for (x = 0; x < w; x++)
281 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
282 PerlIO_putc (fp, src [x * bpp + y * sstr + i]);
283}
284
285#############################################################################
286
287MODULE = Gtk2::CV PACKAGE = Gtk2::CV
288
289SV *
290pb_to_hv84 (GdkPixbuf *pb)
291 CODE:
292{
293 int w = gdk_pixbuf_get_width (pb);
294 int h = gdk_pixbuf_get_height (pb);
295 int x, y;
296 guchar *dst;
297 int bpp = gdk_pixbuf_get_n_channels (pb);
298 guchar *src = gdk_pixbuf_get_pixels (pb);
299 int sstr = gdk_pixbuf_get_rowstride (pb);
300 447
301 RETVAL = newSV (6 * 8 * 12 / 8); 448 RETVAL = newSV (6 * 8 * 12 / 8);
302 SvPOK_only (RETVAL); 449 SvPOK_only (RETVAL);
303 SvCUR_set (RETVAL, 6 * 8 * 12 / 8); 450 SvCUR_set (RETVAL, 6 * 8 * 12 / 8);
304 451
305 dst = SvPVX (RETVAL); 452 dst = (guchar *)SvPVX (RETVAL);
306 453
307 /* some primitive error distribution + random dithering */ 454 /* some primitive error distribution + random dithering */
308 455
309 for (y = 0; y < h; y++) 456 for (y = 0; y < h; y++)
310 { 457 {
368 } 515 }
369} 516}
370 OUTPUT: 517 OUTPUT:
371 RETVAL 518 RETVAL
372 519
520#############################################################################
373 521
522MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Plugin::RCluster
374 523
524SV *
525make_histograms (SV *ar)
526 CODE:
527{
528 int i;
529 AV *av, *result;
530
531 if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV)
532 croak ("Not an array ref as first argument to make_histogram");
533
534 av = (AV *) SvRV (ar);
535 result = newAV ();
536
537 for (i = 0; i <= av_len (av); ++i)
538 {
539 const int HISTSIZE = 64;
540
541 int j;
542 SV *sv = *av_fetch (av, i, 1);
543 STRLEN len;
544 char *buf = SvPVbyte (sv, len);
545
546 int tmphist[HISTSIZE];
547 float *hist;
548
549 SV *histsv = newSV (HISTSIZE * sizeof (float) + 1);
550 SvPOK_on (histsv);
551 SvCUR_set (histsv, HISTSIZE * sizeof (float));
552 hist = (float *)SvPVX (histsv);
553
554 Zero (tmphist, sizeof (tmphist), char);
555
556 for (j = len; j--; )
557 {
558 unsigned int idx
559 = ((*buf & 0xc0) >> 2)
560 | ((*buf & 0x18) >> 1)
561 | (*buf & 0x03);
562
563 ++tmphist[idx];
564 ++buf;
565 }
566
567 for (j = 0; j < HISTSIZE; ++j)
568 hist[j] = (float)tmphist[j] / (len + 1e-30);
569
570 av_push (result, histsv);
571 }
572
573 RETVAL = newRV_noinc ((SV *)result);
574}
575 OUTPUT:
576 RETVAL
577
578

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines