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

Comparing CV/CV.xs (file contents):
Revision 1.13 by root, Tue Feb 8 11:03:30 2005 UTC vs.
Revision 1.22 by root, Sun Aug 14 02:25:44 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>
5#include <gdk-pixbuf/gdk-pixbuf.h> 9#include <gdk-pixbuf/gdk-pixbuf.h>
6 10
7#include <gperl.h> 11#include <gperl.h>
8#include <gtk2perl.h> 12#include <gtk2perl.h>
9 13
10#define IW 80 14#define IW 80 /* MUST match Schnauer.pm! */
15#define IH 60 /* MUST match Schnauer.pm! */
11 16
12#define RAND (seed = (seed + 7141) * 54773 % 134456) 17#define RAND (seed = (seed + 7141) * 54773 % 134456)
13 18
14#define LINELENGTH 240 19#define LINELENGTH 240
15 20
16#define ELLIPSIS "\xe2\x80\xa6" 21#define ELLIPSIS "\xe2\x80\xa6"
22
23struct jpg_err_mgr
24{
25 struct jpeg_error_mgr err;
26 jmp_buf setjmp_buffer;
27};
28
29static void
30cv_error_exit (j_common_ptr cinfo)
31{
32 longjmp (((struct jpg_err_mgr *)cinfo->err)->setjmp_buffer, 99);
33}
34
35static void
36cv_error_output (j_common_ptr cinfo)
37{
38 return;
39}
40
41static void
42rgb_to_hsv (unsigned int r, unsigned int g, unsigned int b,
43 unsigned int *h, unsigned int *s, unsigned int *v)
44{
45 unsigned int mx = r; if (g > mx) mx = g; if (b > mx) mx = b;
46 unsigned int mn = r; if (g < mn) mn = g; if (b < mn) mn = b;
47 unsigned int delta = mx - mn;
48
49 *v = mx;
50
51 *s = mx ? delta * 255 / mx : 0;
52
53 if (delta == 0)
54 *h = 0;
55 else
56 {
57 if (r == mx)
58 *h = ((int)g - (int)b) * 255 / (int)(delta * 3);
59 else if (g == mx)
60 *h = ((int)b - (int)r) * 255 / (int)(delta * 3) + 52;
61 else if (b == mx)
62 *h = ((int)r - (int)g) * 255 / (int)(delta * 3) + 103;
63
64 *h &= 255;
65 }
66}
17 67
18static guint32 a85_val; 68static guint32 a85_val;
19static guint a85_cnt; 69static guint a85_cnt;
20static guchar a85_buf[LINELENGTH], *a85_ptr; 70static guchar a85_buf[LINELENGTH], *a85_ptr;
21 71
68 *a85_ptr++ = '\n'; 118 *a85_ptr++ = '\n';
69 119
70 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf); 120 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf);
71} 121}
72 122
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///////////////////////////////////////////////////////////////////////////// 123/////////////////////////////////////////////////////////////////////////////
101 124
102MODULE = Gtk2::CV PACKAGE = Gtk2::CV 125MODULE = Gtk2::CV PACKAGE = Gtk2::CV
103 126
104PROTOTYPES: ENABLE 127PROTOTYPES: ENABLE
128
129# missing in Gtk2 perl module
130
131gboolean
132gdk_net_wm_supports (GdkAtom property)
133 CODE:
134#if defined(GDK_WINDOWING_X11) && !defined(GDK_MULTIHEAD_SAFE)
135 RETVAL = gdk_net_wm_supports (property);
136#else
137 RETVAL = 0;
138#endif
139 OUTPUT:
140 RETVAL
105 141
106GdkPixbuf_noinc * 142GdkPixbuf_noinc *
107transpose (GdkPixbuf *pb) 143transpose (GdkPixbuf *pb)
108 CODE: 144 CODE:
109{ 145{
149 dst[(w - 1 - x) * bpp + y * dstr + i] = src[x * bpp + y * sstr + i]; 185 dst[(w - 1 - x) * bpp + y * dstr + i] = src[x * bpp + y * sstr + i];
150} 186}
151 OUTPUT: 187 OUTPUT:
152 RETVAL 188 RETVAL
153 189
190GdkPixbuf_noinc *
191load_jpeg (char *path, int thumbnail=0)
192 CODE:
193{
194 struct jpeg_decompress_struct cinfo;
195 struct jpg_err_mgr jerr;
196 guchar *data;
197 int rs;
198 FILE *fp;
199 volatile GdkPixbuf *pb = 0;
200 RETVAL = 0;
201
202 if (!(fp = fopen (path, "rb")))
203 XSRETURN_UNDEF;
204
205 cinfo.err = jpeg_std_error (&jerr.err);
206
207 jerr.err.error_exit = cv_error_exit;
208 jerr.err.output_message = cv_error_output;
209
210 if ((rs = setjmp (jerr.setjmp_buffer)))
211 {
212 fclose (fp);
213 jpeg_destroy_decompress (&cinfo);
214
215 if (pb)
216 g_object_unref ((gpointer)pb);
217
218 XSRETURN_UNDEF;
219 }
220
221 jpeg_create_decompress (&cinfo);
222
223 jpeg_stdio_src (&cinfo, fp);
224 jpeg_read_header (&cinfo, TRUE);
225
226 cinfo.dct_method = JDCT_DEFAULT;
227 cinfo.do_fancy_upsampling = FALSE; /* worse quality, but nobody compained so far, and gdk-pixbuf does the same */
228 cinfo.do_block_smoothing = FALSE;
229 cinfo.out_color_space = JCS_RGB;
230 cinfo.quantize_colors = FALSE;
231
232 cinfo.scale_num = 1;
233 cinfo.scale_denom = 1;
234
235 jpeg_calc_output_dimensions (&cinfo);
236
237 if (thumbnail)
238 {
239 cinfo.dct_method = JDCT_FASTEST;
240 cinfo.do_fancy_upsampling = FALSE;
241
242 while (cinfo.scale_denom < 8
243 && cinfo.output_width >= IW*4
244 && cinfo.output_height >= IH*4)
245 {
246 cinfo.scale_denom <<= 1;
247 jpeg_calc_output_dimensions (&cinfo);
248 }
249 }
250
251 pb = RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, cinfo.output_width, cinfo.output_height);
252 if (!RETVAL)
253 longjmp (jerr.setjmp_buffer, 2);
254
255 data = gdk_pixbuf_get_pixels (RETVAL);
256 rs = gdk_pixbuf_get_rowstride (RETVAL);
257
258 if (cinfo.output_components != 3)
259 longjmp (jerr.setjmp_buffer, 3);
260
261 jpeg_start_decompress (&cinfo);
262
263 while (cinfo.output_scanline < cinfo.output_height)
264 {
265 int remaining = cinfo.output_height - cinfo.output_scanline;
266 JSAMPROW rp[4];
267
268 rp [0] = data + cinfo.output_scanline * rs;
269 rp [1] = (guchar *)rp [0] + rs;
270 rp [2] = (guchar *)rp [1] + rs;
271 rp [3] = (guchar *)rp [2] + rs;
272
273 jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4);
274 }
275
276 jpeg_finish_decompress (&cinfo);
277 fclose (fp);
278 jpeg_destroy_decompress (&cinfo);
279}
280 OUTPUT:
281 RETVAL
282
154############################################################################# 283#############################################################################
155 284
156MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer 285MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer
286
287SV *
288foldcase (SV *pathsv)
289 PROTOTYPE: $
290 CODE:
291{
292 STRLEN plen;
293 U8 *path = SvPVutf8 (pathsv, plen);
294 U8 *pend = path + plen;
295 U8 dst [plen * 6 * 3], *dstp = dst;
296
297 while (path < pend)
298 {
299 U8 ch = *path;
300
301 if (ch >= 'a' && ch <= 'z')
302 *dstp++ = *path++;
303 else if (ch >= '0' && ch <= '9')
304 {
305 STRLEN el, nl = 0;
306 while (*path >= '0' && *path <= '9' && path < pend)
307 path++, nl++;
308
309 for (el = nl; el < 6; el++)
310 *dstp++ = '0';
311
312 memcpy (dstp, path - nl, nl);
313 dstp += nl;
314 }
315 else
316 {
317 STRLEN cl;
318 to_utf8_fold (path, dstp, &cl);
319 dstp += cl;
320 path += is_utf8_char (path);
321 }
322 }
323
324 RETVAL = newSVpvn (dst, dstp - dst);
325}
326 OUTPUT:
327 RETVAL
157 328
158GdkPixbuf_noinc * 329GdkPixbuf_noinc *
159p7_to_pb (int w, int h, guchar *src) 330p7_to_pb (int w, int h, guchar *src)
331 PROTOTYPE: @
160 CODE: 332 CODE:
161{ 333{
162 int x, y; 334 int x, y;
163 guchar *dst, *d; 335 guchar *dst, *d;
164 int dstr; 336 int dstr;
213 for (x = 0; x < w; x++) 385 for (x = 0; x < w; x++)
214 { 386 {
215 int r, g, b; 387 int r, g, b;
216 guchar *p = src + x * bpp + y * sstr; 388 guchar *p = src + x * bpp + y * sstr;
217 389
218 r = ((p[0] + er + Er[x]) * 7 + 128) / 255; 390 r = ((p[0] + er + Er[x]) * 7 + (RAND & 127) + 64) / 255;
219 g = ((p[1] + eg + Eg[x]) * 7 + 128) / 255; 391 g = ((p[1] + eg + Eg[x]) * 7 + (RAND & 127) + 64) / 255;
220 b = ((p[2] + eb + Eb[x]) * 3 + 128) / 255; 392 b = ((p[2] + eb + Eb[x]) * 3 + (RAND & 127) + 64) / 255;
221 393
222 r = r > 7 ? 7 : r < 0 ? 0 : r; 394 r = r > 7 ? 7 : r < 0 ? 0 : r;
223 g = g > 7 ? 7 : g < 0 ? 0 : g; 395 g = g > 7 ? 7 : g < 0 ? 0 : g;
224 b = b > 3 ? 3 : b < 0 ? 0 : b; 396 b = b > 3 ? 3 : b < 0 ? 0 : b;
225 397
226 er += p[0] - (r * 255 + 4) / 7; 398 er += p[0] - (r * 255 + 4) / 7;
227 eg += p[1] - (g * 255 + 4) / 7; 399 eg += p[1] - (g * 255 + 4) / 7;
228 eb += p[2] - (b * 255 + 2) / 3; 400 eb += p[2] - (b * 255 + 2) / 3;
229 401
230 Er[x] = er / 2; er -= er / 2 + RAND % 7 - 3; 402 Er[x] = er >> 1; er -= (er + 1) >> 1;
231 Eg[x] = eg / 2; eg -= eg / 2 + RAND % 7 - 3; 403 Eg[x] = eg >> 1; eg -= (eg + 1) >> 1;
232 Eb[x] = eb / 2; eb -= eb / 2 + RAND % 7 - 3; 404 Eb[x] = eb >> 1; eb -= (eb + 1) >> 1;
233 405
234 *dst++ = r << 5 | g << 2 | b; 406 *dst++ = r << 5 | g << 2 | b;
235 } 407 }
236 } 408 }
237} 409}
238 OUTPUT: 410 OUTPUT:
239 RETVAL 411 RETVAL
240
241SV *
242make_histogram (SV *ar)
243 CODE:
244{
245 int i;
246 AV *av, *result;
247
248 if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV)
249 croak ("Not an array ref as first argument to make_histogram");
250
251 av = (AV *) SvRV (ar);
252 result = newAV ();
253
254 for (i = 0; i <= av_len (av); ++i)
255 {
256 int bigst, j;
257 SV *sv = *av_fetch (av, i, 1);
258 STRLEN len;
259 char *buf = SvPVbyte (sv, len);
260
261 int tmphist[256];
262 char *hist;
263
264 SV *histsv = newSV (257);
265 SvPOK_on (histsv);
266 SvCUR_set (histsv, 256);
267 hist = SvPVX (histsv);
268
269 Zero (tmphist, 256, int);
270
271 while (len--)
272 ++tmphist[(unsigned char)*buf++];
273
274 bigst = 1;
275 for (j = 0; j < 256; ++j)
276 if (tmphist[j] > bigst)
277 bigst = tmphist[j];
278
279 for (j = 0; j < 256; ++j)
280 hist[j] = tmphist[j] * 255 / bigst;
281
282 av_push (result, histsv);
283 }
284
285 RETVAL = newRV_noinc ((SV *)result);
286}
287 OUTPUT:
288 RETVAL
289
290
291 412
292############################################################################# 413#############################################################################
293 414
294MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript 415MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
295 416
419 } 540 }
420} 541}
421 OUTPUT: 542 OUTPUT:
422 RETVAL 543 RETVAL
423 544
545#############################################################################
424 546
547MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Plugin::RCluster
425 548
549SV *
550make_histograms (SV *ar)
551 CODE:
552{
553 int i;
554 AV *av, *result;
555
556 if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV)
557 croak ("Not an array ref as first argument to make_histogram");
558
559 av = (AV *) SvRV (ar);
560 result = newAV ();
561
562 for (i = 0; i <= av_len (av); ++i)
563 {
564 const int HISTSIZE = 64;
565
566 int j;
567 SV *sv = *av_fetch (av, i, 1);
568 STRLEN len;
569 char *buf = SvPVbyte (sv, len);
570
571 int tmphist[HISTSIZE];
572 float *hist;
573
574 SV *histsv = newSV (HISTSIZE * sizeof (float) + 1);
575 SvPOK_on (histsv);
576 SvCUR_set (histsv, HISTSIZE * sizeof (float));
577 hist = (float *)SvPVX (histsv);
578
579 Zero (tmphist, sizeof (tmphist), char);
580
581 for (j = len; j--; )
582 {
583 unsigned int idx
584 = ((*buf & 0xc0) >> 2)
585 | ((*buf & 0x18) >> 1)
586 | (*buf & 0x03);
587
588 ++tmphist[idx];
589 ++buf;
590 }
591
592 for (j = 0; j < HISTSIZE; ++j)
593 hist[j] = (float)tmphist[j] / (len + 1e-30);
594
595 av_push (result, histsv);
596 }
597
598 RETVAL = newRV_noinc ((SV *)result);
599}
600 OUTPUT:
601 RETVAL
602
603

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines