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.20 by root, Fri Jul 22 06:14:25 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
149 dst[(w - 1 - x) * bpp + y * dstr + i] = src[x * bpp + y * sstr + i]; 172 dst[(w - 1 - x) * bpp + y * dstr + i] = src[x * bpp + y * sstr + i];
150} 173}
151 OUTPUT: 174 OUTPUT:
152 RETVAL 175 RETVAL
153 176
177GdkPixbuf_noinc *
178load_jpeg (char *path, int thumbnail=0)
179 CODE:
180{
181 struct jpeg_decompress_struct cinfo;
182 struct jpg_err_mgr jerr;
183 guchar *data;
184 int rs;
185 FILE *fp;
186 volatile GdkPixbuf *pb = 0;
187 RETVAL = 0;
188
189 if (!(fp = fopen (path, "rb")))
190 XSRETURN_UNDEF;
191
192 cinfo.err = jpeg_std_error (&jerr.err);
193
194 jerr.err.error_exit = cv_error_exit;
195 jerr.err.output_message = cv_error_output;
196
197 if ((rs = setjmp (jerr.setjmp_buffer)))
198 {
199 fclose (fp);
200 jpeg_destroy_decompress (&cinfo);
201
202 if (pb)
203 g_object_unref ((gpointer)pb);
204
205 XSRETURN_UNDEF;
206 }
207
208 jpeg_create_decompress (&cinfo);
209
210 jpeg_stdio_src (&cinfo, fp);
211 jpeg_read_header (&cinfo, TRUE);
212
213 cinfo.dct_method = JDCT_DEFAULT;
214 cinfo.do_fancy_upsampling = FALSE; /* worse quality, but nobody compained so far, and gdk-pixbuf does the same */
215 cinfo.do_block_smoothing = FALSE;
216 cinfo.out_color_space = JCS_RGB;
217 cinfo.quantize_colors = FALSE;
218
219 cinfo.scale_num = 1;
220 cinfo.scale_denom = 1;
221
222 jpeg_calc_output_dimensions (&cinfo);
223
224 if (thumbnail)
225 {
226 cinfo.dct_method = JDCT_FASTEST;
227 cinfo.do_fancy_upsampling = FALSE;
228
229 while (cinfo.scale_denom < 8
230 && (cinfo.output_width >> 1) >= IW
231 && (cinfo.output_height >> 1) >= IH)
232 {
233 cinfo.scale_denom <<= 1;
234 jpeg_calc_output_dimensions (&cinfo);
235 }
236 }
237
238 pb = RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, cinfo.output_width, cinfo.output_height);
239 if (!RETVAL)
240 longjmp (jerr.setjmp_buffer, 2);
241
242 data = gdk_pixbuf_get_pixels (RETVAL);
243 rs = gdk_pixbuf_get_rowstride (RETVAL);
244
245 if (cinfo.output_components != 3)
246 longjmp (jerr.setjmp_buffer, 3);
247
248 jpeg_start_decompress (&cinfo);
249
250 while (cinfo.output_scanline < cinfo.output_height)
251 {
252 int remaining = cinfo.output_height - cinfo.output_scanline;
253 JSAMPROW rp[4];
254
255 rp [0] = data + cinfo.output_scanline * rs;
256 rp [1] = (guchar *)rp [0] + rs;
257 rp [2] = (guchar *)rp [1] + rs;
258 rp [3] = (guchar *)rp [2] + rs;
259
260 jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4);
261 }
262
263 jpeg_finish_decompress (&cinfo);
264 fclose (fp);
265 jpeg_destroy_decompress (&cinfo);
266}
267 OUTPUT:
268 RETVAL
269
154############################################################################# 270#############################################################################
155 271
156MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer 272MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer
273
274SV *
275foldcase (SV *pathsv)
276 PROTOTYPE: $
277 CODE:
278{
279 STRLEN plen;
280 U8 *path = SvPVutf8 (pathsv, plen);
281 U8 *pend = path + plen;
282 U8 dst [plen * 6 * 3], *dstp = dst;
283
284 while (path < pend)
285 {
286 U8 ch = *path;
287
288 if (ch >= 'a' && ch <= 'z')
289 *dstp++ = *path++;
290 else if (ch >= '0' && ch <= '9')
291 {
292 STRLEN el, nl = 0;
293 while (*path >= '0' && *path <= '9' && path < pend)
294 path++, nl++;
295
296 for (el = nl; el < 6; el++)
297 *dstp++ = '0';
298
299 memcpy (dstp, path - nl, nl);
300 dstp += nl;
301 }
302 else
303 {
304 STRLEN cl;
305 to_utf8_fold (path, dstp, &cl);
306 dstp += cl;
307 path += is_utf8_char (path);
308 }
309 }
310
311 RETVAL = newSVpvn (dst, dstp - dst);
312}
313 OUTPUT:
314 RETVAL
157 315
158GdkPixbuf_noinc * 316GdkPixbuf_noinc *
159p7_to_pb (int w, int h, guchar *src) 317p7_to_pb (int w, int h, guchar *src)
160 CODE: 318 CODE:
161{ 319{
213 for (x = 0; x < w; x++) 371 for (x = 0; x < w; x++)
214 { 372 {
215 int r, g, b; 373 int r, g, b;
216 guchar *p = src + x * bpp + y * sstr; 374 guchar *p = src + x * bpp + y * sstr;
217 375
218 r = ((p[0] + er + Er[x]) * 7 + 128) / 255; 376 r = ((p[0] + er + Er[x]) * 7 + (RAND & 127) + 64) / 255;
219 g = ((p[1] + eg + Eg[x]) * 7 + 128) / 255; 377 g = ((p[1] + eg + Eg[x]) * 7 + (RAND & 127) + 64) / 255;
220 b = ((p[2] + eb + Eb[x]) * 3 + 128) / 255; 378 b = ((p[2] + eb + Eb[x]) * 3 + (RAND & 127) + 64) / 255;
221 379
222 r = r > 7 ? 7 : r < 0 ? 0 : r; 380 r = r > 7 ? 7 : r < 0 ? 0 : r;
223 g = g > 7 ? 7 : g < 0 ? 0 : g; 381 g = g > 7 ? 7 : g < 0 ? 0 : g;
224 b = b > 3 ? 3 : b < 0 ? 0 : b; 382 b = b > 3 ? 3 : b < 0 ? 0 : b;
225 383
226 er += p[0] - (r * 255 + 4) / 7; 384 er += p[0] - (r * 255 + 4) / 7;
227 eg += p[1] - (g * 255 + 4) / 7; 385 eg += p[1] - (g * 255 + 4) / 7;
228 eb += p[2] - (b * 255 + 2) / 3; 386 eb += p[2] - (b * 255 + 2) / 3;
229 387
230 Er[x] = er / 2; er -= er / 2 + RAND % 7 - 3; 388 Er[x] = er >> 1; er -= (er + 1) >> 1;
231 Eg[x] = eg / 2; eg -= eg / 2 + RAND % 7 - 3; 389 Eg[x] = eg >> 1; eg -= (eg + 1) >> 1;
232 Eb[x] = eb / 2; eb -= eb / 2 + RAND % 7 - 3; 390 Eb[x] = eb >> 1; eb -= (eb + 1) >> 1;
233 391
234 *dst++ = r << 5 | g << 2 | b; 392 *dst++ = r << 5 | g << 2 | b;
235 } 393 }
236 } 394 }
237} 395}
238 OUTPUT: 396 OUTPUT:
239 RETVAL 397 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 398
291############################################################################# 399#############################################################################
292 400
293MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript 401MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
294 402
418 } 526 }
419} 527}
420 OUTPUT: 528 OUTPUT:
421 RETVAL 529 RETVAL
422 530
531#############################################################################
423 532
533MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Plugin::RCluster
424 534
535SV *
536make_histograms (SV *ar)
537 CODE:
538{
539 int i;
540 AV *av, *result;
541
542 if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV)
543 croak ("Not an array ref as first argument to make_histogram");
544
545 av = (AV *) SvRV (ar);
546 result = newAV ();
547
548 for (i = 0; i <= av_len (av); ++i)
549 {
550 const int HISTSIZE = 64;
551
552 int j;
553 SV *sv = *av_fetch (av, i, 1);
554 STRLEN len;
555 char *buf = SvPVbyte (sv, len);
556
557 int tmphist[HISTSIZE];
558 float *hist;
559
560 SV *histsv = newSV (HISTSIZE * sizeof (float) + 1);
561 SvPOK_on (histsv);
562 SvCUR_set (histsv, HISTSIZE * sizeof (float));
563 hist = (float *)SvPVX (histsv);
564
565 Zero (tmphist, sizeof (tmphist), char);
566
567 for (j = len; j--; )
568 {
569 unsigned int idx
570 = ((*buf & 0xc0) >> 2)
571 | ((*buf & 0x18) >> 1)
572 | (*buf & 0x03);
573
574 ++tmphist[idx];
575 ++buf;
576 }
577
578 for (j = 0; j < HISTSIZE; ++j)
579 hist[j] = (float)tmphist[j] / (len + 1e-30);
580
581 av_push (result, histsv);
582 }
583
584 RETVAL = newRV_noinc ((SV *)result);
585}
586 OUTPUT:
587 RETVAL
588
589

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines