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

Comparing CV/CV.xs (file contents):
Revision 1.10 by root, Tue Feb 8 10:34:19 2005 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
68 *a85_ptr++ = '\n'; 120 *a85_ptr++ = '\n';
69 121
70 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf); 122 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf);
71} 123}
72 124
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///////////////////////////////////////////////////////////////////////////// 125/////////////////////////////////////////////////////////////////////////////
101 126
102MODULE = Gtk2::CV PACKAGE = Gtk2::CV 127MODULE = Gtk2::CV PACKAGE = Gtk2::CV
103 128
104PROTOTYPES: 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
105 168
106GdkPixbuf_noinc * 169GdkPixbuf_noinc *
107transpose (GdkPixbuf *pb) 170transpose (GdkPixbuf *pb)
108 CODE: 171 CODE:
109{ 172{
149 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];
150} 213}
151 OUTPUT: 214 OUTPUT:
152 RETVAL 215 RETVAL
153 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
154############################################################################# 316#############################################################################
155 317
156MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer 318MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer
157 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
158GdkPixbuf_noinc * 362GdkPixbuf_noinc *
159p7_to_pb (int w, int h, guchar *src) 363p7_to_pb (int w, int h, SV *src_sv)
364 PROTOTYPE: @
160 CODE: 365 CODE:
161{ 366{
162 int x, y; 367 int x, y;
163 guchar *dst, *d; 368 guchar *dst, *d;
164 int dstr; 369 int dstr;
370 guchar *src = (guchar *)SvPVbyte_nolen (src_sv);
165 371
166 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h); 372 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
167 dst = gdk_pixbuf_get_pixels (RETVAL); 373 dst = gdk_pixbuf_get_pixels (RETVAL);
168 dstr = gdk_pixbuf_get_rowstride (RETVAL); 374 dstr = gdk_pixbuf_get_rowstride (RETVAL);
169 375
196 402
197 RETVAL = newSV (w * h); 403 RETVAL = newSV (w * h);
198 SvPOK_only (RETVAL); 404 SvPOK_only (RETVAL);
199 SvCUR_set (RETVAL, w * h); 405 SvCUR_set (RETVAL, w * h);
200 406
201 dst = SvPVX (RETVAL); 407 dst = (guchar *)SvPVX (RETVAL);
202 408
203 memset (Er, 0, sizeof (int) * IW); 409 memset (Er, 0, sizeof (int) * IW);
204 memset (Eg, 0, sizeof (int) * IW); 410 memset (Eg, 0, sizeof (int) * IW);
205 memset (Eb, 0, sizeof (int) * IW); 411 memset (Eb, 0, sizeof (int) * IW);
206 412
213 for (x = 0; x < w; x++) 419 for (x = 0; x < w; x++)
214 { 420 {
215 int r, g, b; 421 int r, g, b;
216 guchar *p = src + x * bpp + y * sstr; 422 guchar *p = src + x * bpp + y * sstr;
217 423
218 r = ((p[0] + er + Er[x]) * 7 + 128) / 255; 424 r = ((p[0] + er + Er[x]) * 7 + (RAND & 127) + 64) / 255;
219 g = ((p[1] + eg + Eg[x]) * 7 + 128) / 255; 425 g = ((p[1] + eg + Eg[x]) * 7 + (RAND & 127) + 64) / 255;
220 b = ((p[2] + eb + Eb[x]) * 3 + 128) / 255; 426 b = ((p[2] + eb + Eb[x]) * 3 + (RAND & 127) + 64) / 255;
221 427
222 r = r > 7 ? 7 : r < 0 ? 0 : r; 428 r = r > 7 ? 7 : r < 0 ? 0 : r;
223 g = g > 7 ? 7 : g < 0 ? 0 : g; 429 g = g > 7 ? 7 : g < 0 ? 0 : g;
224 b = b > 3 ? 3 : b < 0 ? 0 : b; 430 b = b > 3 ? 3 : b < 0 ? 0 : b;
225 431
226 er += p[0] - (r * 255 + 4) / 7; 432 er += p[0] - (r * 255 + 4) / 7;
227 eg += p[1] - (g * 255 + 4) / 7; 433 eg += p[1] - (g * 255 + 4) / 7;
228 eb += p[2] - (b * 255 + 2) / 3; 434 eb += p[2] - (b * 255 + 2) / 3;
229 435
230 Er[x] = er / 2; er -= er / 2 + RAND % 7 - 3; 436 Er[x] = er >> 1; er -= (er + 1) >> 1;
231 Eg[x] = eg / 2; eg -= eg / 2 + RAND % 7 - 3; 437 Eg[x] = eg >> 1; eg -= (eg + 1) >> 1;
232 Eb[x] = eb / 2; eb -= eb / 2 + RAND % 7 - 3; 438 Eb[x] = eb >> 1; eb -= (eb + 1) >> 1;
233 439
234 *dst++ = r << 5 | g << 2 | b; 440 *dst++ = r << 5 | g << 2 | b;
235 } 441 }
236 } 442 }
237} 443}
238 OUTPUT: 444 OUTPUT:
239 RETVAL 445 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 = 1;
257 int j = 0;
258 SV *sv = *av_fetch (av, i, 1);
259 STRLEN len;
260 char *buf = SvPVbyte (sv, len);
261 int tmphist[256];
262 char *hist;
263
264 SV *histsv = newSV (257);//("", 0);
265 sv_upgrade (histsv, SVt_PV);
266 SvCUR_set (histsv, 256);
267 hist = SvPV_nolen (histsv);
268
269 Zero (tmphist, 256, int);
270
271 while (len--)
272 ++tmphist[*buf++];
273
274 for (j = 0; j < 256; ++j)
275 if (tmphist[j] > bigst)
276 bigst = tmphist[j];
277
278 for (j = 0; j < 256; ++j)
279 hist[j] = tmphist[j] * 255 / bigst;
280
281 av_push (result, histsv);
282 }
283
284 RETVAL = newRV_noinc ((SV *)result);
285}
286 OUTPUT:
287 RETVAL
288
289
290 446
291############################################################################# 447#############################################################################
292 448
293MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript 449MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
294 450
350 506
351 RETVAL = newSV (6 * 8 * 12 / 8); 507 RETVAL = newSV (6 * 8 * 12 / 8);
352 SvPOK_only (RETVAL); 508 SvPOK_only (RETVAL);
353 SvCUR_set (RETVAL, 6 * 8 * 12 / 8); 509 SvCUR_set (RETVAL, 6 * 8 * 12 / 8);
354 510
355 dst = SvPVX (RETVAL); 511 dst = (guchar *)SvPVX (RETVAL);
356 512
357 /* some primitive error distribution + random dithering */ 513 /* some primitive error distribution + random dithering */
358 514
359 for (y = 0; y < h; y++) 515 for (y = 0; y < h; y++)
360 { 516 {
418 } 574 }
419} 575}
420 OUTPUT: 576 OUTPUT:
421 RETVAL 577 RETVAL
422 578
579#############################################################################
423 580
581MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Plugin::RCluster
424 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