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

Comparing CV/CV.xs (file contents):
Revision 1.14 by root, Tue Feb 8 12:51:12 2005 UTC vs.
Revision 1.32 by root, Wed Feb 15 08:36:55 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_NONE);
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
171const char *
172magic_mime (const char *path)
173 CODE:
174{
175 static magic_t cookie;
176
177 if (!cookie)
178 {
179 cookie = magic_open (MAGIC_MIME);
180
181 if (cookie)
182 magic_load (cookie, 0);
183 else
184 XSRETURN_UNDEF;
185 }
186
187 RETVAL = magic_file (cookie, path);
188}
189 OUTPUT:
190 RETVAL
191
192# missing in Gtk2 perl module
193
194gboolean
195gdk_net_wm_supports (GdkAtom property)
196 CODE:
197#if defined(GDK_WINDOWING_X11) && !defined(GDK_MULTIHEAD_SAFE)
198 RETVAL = gdk_net_wm_supports (property);
199#else
200 RETVAL = 0;
201#endif
202 OUTPUT:
203 RETVAL
204
106GdkPixbuf_noinc * 205GdkPixbuf_noinc *
107transpose (GdkPixbuf *pb) 206dealpha_expose (GdkPixbuf *pb)
108 CODE: 207 CODE:
109{ 208{
110 int w = gdk_pixbuf_get_width (pb); 209 int w = gdk_pixbuf_get_width (pb);
111 int h = gdk_pixbuf_get_height (pb); 210 int h = gdk_pixbuf_get_height (pb);
112 int bpp = gdk_pixbuf_get_n_channels (pb); 211 int bpp = gdk_pixbuf_get_n_channels (pb);
113 int x, y, i; 212 int x, y, i;
114 guchar *src = gdk_pixbuf_get_pixels (pb), *dst; 213 guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
115 int sstr = gdk_pixbuf_get_rowstride (pb), dstr; 214 int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
116 215
117 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, h, w); 216 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
118 217
119 dst = gdk_pixbuf_get_pixels (RETVAL); 218 dst = gdk_pixbuf_get_pixels (RETVAL);
120 dstr = gdk_pixbuf_get_rowstride (RETVAL); 219 dstr = gdk_pixbuf_get_rowstride (RETVAL);
121 220
122 for (y = 0; y < h; y++)
123 for (x = 0; x < w; x++) 221 for (x = 0; x < w; x++)
222 for (y = 0; y < h; y++)
124 for (i = 0; i < bpp; i++) 223 for (i = 0; i < 3; i++)
125 dst[y * bpp + x * dstr + i] = src[x * bpp + y * sstr + i]; 224 dst[x * 3 + y * dstr + i] = src[x * bpp + y * sstr + i];
126} 225}
127 OUTPUT: 226 OUTPUT:
128 RETVAL 227 RETVAL
129 228
130GdkPixbuf_noinc * 229GdkPixbuf_noinc *
131flop (GdkPixbuf *pb) 230rotate (GdkPixbuf *pb, int angle)
132 CODE: 231 CODE:
133{ 232 RETVAL = gdk_pixbuf_rotate_simple (pb, angle == 0 ? GDK_PIXBUF_ROTATE_NONE
134 int w = gdk_pixbuf_get_width (pb); 233 : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE
135 int h = gdk_pixbuf_get_height (pb); 234 : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN
136 int bpp = gdk_pixbuf_get_n_channels (pb); 235 : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE
137 int x, y, i; 236 : angle);
138 guchar *src = gdk_pixbuf_get_pixels (pb), *dst; 237 OUTPUT:
139 int sstr = gdk_pixbuf_get_rowstride (pb), dstr; 238 RETVAL
140 239
141 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, w, h); 240GdkPixbuf_noinc *
241load_jpeg (SV *path, int thumbnail=0)
242 CODE:
243{
244 struct jpeg_decompress_struct cinfo;
245 struct jpg_err_mgr jerr;
246 guchar *data;
247 int rs;
248 FILE *fp;
249 volatile GdkPixbuf *pb = 0;
142 250
251 RETVAL = 0;
252
253 fp = fopen (SvPVbyte_nolen (path), "rb");
254
255 if (!fp)
256 XSRETURN_UNDEF;
257
258 cinfo.err = jpeg_std_error (&jerr.err);
259
260 jerr.err.error_exit = cv_error_exit;
261 jerr.err.output_message = cv_error_output;
262
263 if ((rs = setjmp (jerr.setjmp_buffer)))
264 {
265 fclose (fp);
266 jpeg_destroy_decompress (&cinfo);
267
268 if (pb)
269 g_object_unref ((gpointer)pb);
270
271 XSRETURN_UNDEF;
272 }
273
274 jpeg_create_decompress (&cinfo);
275
276 jpeg_stdio_src (&cinfo, fp);
277 jpeg_read_header (&cinfo, TRUE);
278
279 cinfo.dct_method = JDCT_DEFAULT;
280 cinfo.do_fancy_upsampling = FALSE; /* worse quality, but nobody compained so far, and gdk-pixbuf does the same */
281 cinfo.do_block_smoothing = FALSE;
282 cinfo.out_color_space = JCS_RGB;
283 cinfo.quantize_colors = FALSE;
284
285 cinfo.scale_num = 1;
286 cinfo.scale_denom = 1;
287
288 jpeg_calc_output_dimensions (&cinfo);
289
290 if (thumbnail)
291 {
292 cinfo.dct_method = JDCT_FASTEST;
293 cinfo.do_fancy_upsampling = FALSE;
294
295 while (cinfo.scale_denom < 8
296 && cinfo.output_width >= IW*4
297 && cinfo.output_height >= IH*4)
298 {
299 cinfo.scale_denom <<= 1;
300 jpeg_calc_output_dimensions (&cinfo);
301 }
302 }
303
304 pb = RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, cinfo.output_width, cinfo.output_height);
305 if (!RETVAL)
306 longjmp (jerr.setjmp_buffer, 2);
307
143 dst = gdk_pixbuf_get_pixels (RETVAL); 308 data = gdk_pixbuf_get_pixels (RETVAL);
144 dstr = gdk_pixbuf_get_rowstride (RETVAL); 309 rs = gdk_pixbuf_get_rowstride (RETVAL);
145 310
146 for (y = 0; y < h; y++) 311 if (cinfo.output_components != 3)
147 for (x = 0; x < w; x++) 312 longjmp (jerr.setjmp_buffer, 3);
148 for (i = 0; i < bpp; i++) 313
149 dst[(w - 1 - x) * bpp + y * dstr + i] = src[x * bpp + y * sstr + i]; 314 jpeg_start_decompress (&cinfo);
315
316 while (cinfo.output_scanline < cinfo.output_height)
317 {
318 int remaining = cinfo.output_height - cinfo.output_scanline;
319 JSAMPROW rp[4];
320
321 rp [0] = data + cinfo.output_scanline * rs;
322 rp [1] = (guchar *)rp [0] + rs;
323 rp [2] = (guchar *)rp [1] + rs;
324 rp [3] = (guchar *)rp [2] + rs;
325
326 jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4);
327 }
328
329 jpeg_finish_decompress (&cinfo);
330 fclose (fp);
331 jpeg_destroy_decompress (&cinfo);
150} 332}
151 OUTPUT: 333 OUTPUT:
152 RETVAL 334 RETVAL
153 335
154############################################################################# 336#############################################################################
155 337
156MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer 338MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer
157 339
340SV *
341foldcase (SV *pathsv)
342 PROTOTYPE: $
343 CODE:
344{
345 STRLEN plen;
346 U8 *path = (U8 *)SvPVutf8 (pathsv, plen);
347 U8 *pend = path + plen;
348 U8 dst [plen * 6 * 3], *dstp = dst;
349
350 while (path < pend)
351 {
352 U8 ch = *path;
353
354 if (ch >= 'a' && ch <= 'z')
355 *dstp++ = *path++;
356 else if (ch >= '0' && ch <= '9')
357 {
358 STRLEN el, nl = 0;
359 while (*path >= '0' && *path <= '9' && path < pend)
360 path++, nl++;
361
362 for (el = nl; el < 6; el++)
363 *dstp++ = '0';
364
365 memcpy (dstp, path - nl, nl);
366 dstp += nl;
367 }
368 else
369 {
370 STRLEN cl;
371 to_utf8_fold (path, dstp, &cl);
372 dstp += cl;
373 path += is_utf8_char (path);
374 }
375 }
376
377 RETVAL = newSVpvn ((const char *)dst, dstp - dst);
378}
379 OUTPUT:
380 RETVAL
381
158GdkPixbuf_noinc * 382GdkPixbuf_noinc *
159p7_to_pb (int w, int h, guchar *src) 383p7_to_pb (int w, int h, SV *src_sv)
384 PROTOTYPE: @
160 CODE: 385 CODE:
161{ 386{
162 int x, y; 387 int x, y;
163 guchar *dst, *d; 388 guchar *dst, *d;
164 int dstr; 389 int dstr;
390 guchar *src = (guchar *)SvPVbyte_nolen (src_sv);
165 391
166 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h); 392 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
167 dst = gdk_pixbuf_get_pixels (RETVAL); 393 dst = gdk_pixbuf_get_pixels (RETVAL);
168 dstr = gdk_pixbuf_get_rowstride (RETVAL); 394 dstr = gdk_pixbuf_get_rowstride (RETVAL);
169 395
178 } 404 }
179} 405}
180 OUTPUT: 406 OUTPUT:
181 RETVAL 407 RETVAL
182 408
409#############################################################################
410
411MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
412
413void
414dump_ascii85 (PerlIO *fp, GdkPixbuf *pb)
415 CODE:
416{
417 int w = gdk_pixbuf_get_width (pb);
418 int h = gdk_pixbuf_get_height (pb);
419 int x, y, i;
420 guchar *dst;
421 int bpp = gdk_pixbuf_get_n_channels (pb);
422 guchar *src = gdk_pixbuf_get_pixels (pb);
423 int sstr = gdk_pixbuf_get_rowstride (pb);
424
425 a85_init ();
426
427 for (y = 0; y < h; y++)
428 for (x = 0; x < w; x++)
429 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
430 a85_push (fp, src [x * bpp + y * sstr + i]);
431
432 a85_finish (fp);
433}
434
435void
436dump_binary (PerlIO *fp, GdkPixbuf *pb)
437 CODE:
438{
439 int w = gdk_pixbuf_get_width (pb);
440 int h = gdk_pixbuf_get_height (pb);
441 int x, y, i;
442 guchar *dst;
443 int bpp = gdk_pixbuf_get_n_channels (pb);
444 guchar *src = gdk_pixbuf_get_pixels (pb);
445 int sstr = gdk_pixbuf_get_rowstride (pb);
446
447 for (y = 0; y < h; y++)
448 for (x = 0; x < w; x++)
449 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
450 PerlIO_putc (fp, src [x * bpp + y * sstr + i]);
451}
452
453#############################################################################
454
455MODULE = Gtk2::CV PACKAGE = Gtk2::CV
456
183SV * 457SV *
184pb_to_p7 (GdkPixbuf *pb) 458pb_to_hv84 (GdkPixbuf *pb)
185 CODE: 459 CODE:
186{ 460{
187 int w = gdk_pixbuf_get_width (pb); 461 int w = gdk_pixbuf_get_width (pb);
188 int h = gdk_pixbuf_get_height (pb); 462 int h = gdk_pixbuf_get_height (pb);
189 int x, y; 463 int x, y;
190 guchar *dst; 464 guchar *dst;
191 int bpp = gdk_pixbuf_get_n_channels (pb); 465 int bpp = gdk_pixbuf_get_n_channels (pb);
192 guchar *src = gdk_pixbuf_get_pixels (pb); 466 guchar *src = gdk_pixbuf_get_pixels (pb);
193 int sstr = gdk_pixbuf_get_rowstride (pb); 467 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
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 j = len;
272
273 while (len--)
274 ++tmphist[(unsigned char)*buf++ & 0xda];
275
276 len = j;
277
278 bigst = 1;
279 for (j = 0; j < 256; ++j)
280 if (tmphist[j] > bigst)
281 bigst = tmphist[j];
282
283 for (j = 0; j < 256; ++j)
284 hist[j] = tmphist[j] * 255 / (len == 0 ? 1 : len);//bigst;
285
286 av_push (result, histsv);
287 }
288
289 RETVAL = newRV_noinc ((SV *)result);
290}
291 OUTPUT:
292 RETVAL
293
294
295
296#############################################################################
297
298MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
299
300void
301dump_ascii85 (PerlIO *fp, GdkPixbuf *pb)
302 CODE:
303{
304 int w = gdk_pixbuf_get_width (pb);
305 int h = gdk_pixbuf_get_height (pb);
306 int x, y, i;
307 guchar *dst;
308 int bpp = gdk_pixbuf_get_n_channels (pb);
309 guchar *src = gdk_pixbuf_get_pixels (pb);
310 int sstr = gdk_pixbuf_get_rowstride (pb);
311
312 a85_init ();
313
314 for (y = 0; y < h; y++)
315 for (x = 0; x < w; x++)
316 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
317 a85_push (fp, src [x * bpp + y * sstr + i]);
318
319 a85_finish (fp);
320}
321
322void
323dump_binary (PerlIO *fp, GdkPixbuf *pb)
324 CODE:
325{
326 int w = gdk_pixbuf_get_width (pb);
327 int h = gdk_pixbuf_get_height (pb);
328 int x, y, i;
329 guchar *dst;
330 int bpp = gdk_pixbuf_get_n_channels (pb);
331 guchar *src = gdk_pixbuf_get_pixels (pb);
332 int sstr = gdk_pixbuf_get_rowstride (pb);
333
334 for (y = 0; y < h; y++)
335 for (x = 0; x < w; x++)
336 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
337 PerlIO_putc (fp, src [x * bpp + y * sstr + i]);
338}
339
340#############################################################################
341
342MODULE = Gtk2::CV PACKAGE = Gtk2::CV
343
344SV *
345pb_to_hv84 (GdkPixbuf *pb)
346 CODE:
347{
348 int w = gdk_pixbuf_get_width (pb);
349 int h = gdk_pixbuf_get_height (pb);
350 int x, y;
351 guchar *dst;
352 int bpp = gdk_pixbuf_get_n_channels (pb);
353 guchar *src = gdk_pixbuf_get_pixels (pb);
354 int sstr = gdk_pixbuf_get_rowstride (pb);
355 468
356 RETVAL = newSV (6 * 8 * 12 / 8); 469 RETVAL = newSV (6 * 8 * 12 / 8);
357 SvPOK_only (RETVAL); 470 SvPOK_only (RETVAL);
358 SvCUR_set (RETVAL, 6 * 8 * 12 / 8); 471 SvCUR_set (RETVAL, 6 * 8 * 12 / 8);
359 472
360 dst = SvPVX (RETVAL); 473 dst = (guchar *)SvPVX (RETVAL);
361 474
362 /* some primitive error distribution + random dithering */ 475 /* some primitive error distribution + random dithering */
363 476
364 for (y = 0; y < h; y++) 477 for (y = 0; y < h; y++)
365 { 478 {
423 } 536 }
424} 537}
425 OUTPUT: 538 OUTPUT:
426 RETVAL 539 RETVAL
427 540
541#############################################################################
428 542
543MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Plugin::RCluster
429 544
545SV *
546make_histograms (SV *ar)
547 CODE:
548{
549 int i;
550 AV *av, *result;
551
552 if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV)
553 croak ("Not an array ref as first argument to make_histogram");
554
555 av = (AV *) SvRV (ar);
556 result = newAV ();
557
558 for (i = 0; i <= av_len (av); ++i)
559 {
560 const int HISTSIZE = 64;
561
562 int j;
563 SV *sv = *av_fetch (av, i, 1);
564 STRLEN len;
565 char *buf = SvPVbyte (sv, len);
566
567 int tmphist[HISTSIZE];
568 float *hist;
569
570 SV *histsv = newSV (HISTSIZE * sizeof (float) + 1);
571 SvPOK_on (histsv);
572 SvCUR_set (histsv, HISTSIZE * sizeof (float));
573 hist = (float *)SvPVX (histsv);
574
575 Zero (tmphist, sizeof (tmphist), char);
576
577 for (j = len; j--; )
578 {
579 unsigned int idx
580 = ((*buf & 0xc0) >> 2)
581 | ((*buf & 0x18) >> 1)
582 | (*buf & 0x03);
583
584 ++tmphist[idx];
585 ++buf;
586 }
587
588 for (j = 0; j < HISTSIZE; ++j)
589 hist[j] = (float)tmphist[j] / (len + 1e-30);
590
591 av_push (result, histsv);
592 }
593
594 RETVAL = newRV_noinc ((SV *)result);
595}
596 OUTPUT:
597 RETVAL
598
599

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines