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.35 by root, Tue Mar 7 16:45:53 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#include <math.h>
8
9#include <magic.h>
10
11#include <jpeglib.h>
12#include <glib.h>
13#include <gtk/gtk.h>
5#include <gdk-pixbuf/gdk-pixbuf.h> 14#include <gdk-pixbuf/gdk-pixbuf.h>
6 15
7#include <gperl.h> 16#include <gperl.h>
8#include <gtk2perl.h> 17#include <gtk2perl.h>
9 18
10#define IW 80 19#define IW 80 /* MUST match Schnauzer.pm! */
20#define IH 60 /* MUST match Schnauzer.pm! */
11 21
12#define RAND (seed = (seed + 7141) * 54773 % 134456) 22#define RAND (seed = (seed + 7141) * 54773 % 134456)
13 23
14#define LINELENGTH 240 24#define LINELENGTH 240
15 25
16#define ELLIPSIS "\xe2\x80\xa6" 26#define ELLIPSIS "\xe2\x80\xa6"
27
28struct jpg_err_mgr
29{
30 struct jpeg_error_mgr err;
31 jmp_buf setjmp_buffer;
32};
33
34static void
35cv_error_exit (j_common_ptr cinfo)
36{
37 longjmp (((struct jpg_err_mgr *)cinfo->err)->setjmp_buffer, 99);
38}
39
40static void
41cv_error_output (j_common_ptr cinfo)
42{
43 return;
44}
45
46static void
47rgb_to_hsv (unsigned int r, unsigned int g, unsigned int b,
48 unsigned int *h, unsigned int *s, unsigned int *v)
49{
50 unsigned int mx = r; if (g > mx) mx = g; if (b > mx) mx = b;
51 unsigned int mn = r; if (g < mn) mn = g; if (b < mn) mn = b;
52 unsigned int delta = mx - mn;
53
54 *v = mx;
55
56 *s = mx ? delta * 255 / mx : 0;
57
58 if (delta == 0)
59 *h = 0;
60 else
61 {
62 if (r == mx)
63 *h = ((int)g - (int)b) * 255 / (int)(delta * 3);
64 else if (g == mx)
65 *h = ((int)b - (int)r) * 255 / (int)(delta * 3) + 52;
66 else if (b == mx)
67 *h = ((int)r - (int)g) * 255 / (int)(delta * 3) + 103;
68
69 *h &= 255;
70 }
71}
17 72
18static guint32 a85_val; 73static guint32 a85_val;
19static guint a85_cnt; 74static guint a85_cnt;
20static guchar a85_buf[LINELENGTH], *a85_ptr; 75static guchar a85_buf[LINELENGTH], *a85_ptr;
21 76
68 *a85_ptr++ = '\n'; 123 *a85_ptr++ = '\n';
69 124
70 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf); 125 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf);
71} 126}
72 127
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///////////////////////////////////////////////////////////////////////////// 128/////////////////////////////////////////////////////////////////////////////
101 129
102MODULE = Gtk2::CV PACKAGE = Gtk2::CV 130MODULE = Gtk2::CV PACKAGE = Gtk2::CV
103 131
104PROTOTYPES: ENABLE 132PROTOTYPES: ENABLE
105 133
134# missing function in perl. really :)
135int
136common_prefix_length (a, b)
137 unsigned char *a = (unsigned char *)SvPVutf8_nolen ($arg);
138 unsigned char *b = (unsigned char *)SvPVutf8_nolen ($arg);
139 CODE:
140 RETVAL = 0;
141
142 while (*a == *b && *a)
143 {
144 RETVAL += (*a & 0xc0) != 0x80;
145 a++, b++;
146 }
147
148 OUTPUT:
149 RETVAL
150
151const char *
152magic (const char *path)
153 CODE:
154{
155 static magic_t cookie;
156
157 if (!cookie)
158 {
159 cookie = magic_open (MAGIC_NONE);
160
161 if (cookie)
162 magic_load (cookie, 0);
163 else
164 XSRETURN_UNDEF;
165 }
166
167 RETVAL = magic_file (cookie, path);
168}
169 OUTPUT:
170 RETVAL
171
172const char *
173magic_mime (const char *path)
174 CODE:
175{
176 static magic_t cookie;
177
178 if (!cookie)
179 {
180 cookie = magic_open (MAGIC_MIME);
181
182 if (cookie)
183 magic_load (cookie, 0);
184 else
185 XSRETURN_UNDEF;
186 }
187
188 RETVAL = magic_file (cookie, path);
189}
190 OUTPUT:
191 RETVAL
192
193# missing in Gtk2 perl module
194
195gboolean
196gdk_net_wm_supports (GdkAtom property)
197 CODE:
198#if defined(GDK_WINDOWING_X11) && !defined(GDK_MULTIHEAD_SAFE)
199 RETVAL = gdk_net_wm_supports (property);
200#else
201 RETVAL = 0;
202#endif
203 OUTPUT:
204 RETVAL
205
106GdkPixbuf_noinc * 206GdkPixbuf_noinc *
107transpose (GdkPixbuf *pb) 207dealpha_expose (GdkPixbuf *pb)
108 CODE: 208 CODE:
109{ 209{
110 int w = gdk_pixbuf_get_width (pb); 210 int w = gdk_pixbuf_get_width (pb);
111 int h = gdk_pixbuf_get_height (pb); 211 int h = gdk_pixbuf_get_height (pb);
112 int bpp = gdk_pixbuf_get_n_channels (pb); 212 int bpp = gdk_pixbuf_get_n_channels (pb);
113 int x, y, i; 213 int x, y, i;
114 guchar *src = gdk_pixbuf_get_pixels (pb), *dst; 214 guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
115 int sstr = gdk_pixbuf_get_rowstride (pb), dstr; 215 int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
116 216
117 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, h, w); 217 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
118 218
119 dst = gdk_pixbuf_get_pixels (RETVAL); 219 dst = gdk_pixbuf_get_pixels (RETVAL);
120 dstr = gdk_pixbuf_get_rowstride (RETVAL); 220 dstr = gdk_pixbuf_get_rowstride (RETVAL);
121 221
122 for (y = 0; y < h; y++)
123 for (x = 0; x < w; x++) 222 for (x = 0; x < w; x++)
223 for (y = 0; y < h; y++)
124 for (i = 0; i < bpp; i++) 224 for (i = 0; i < 3; i++)
125 dst[y * bpp + x * dstr + i] = src[x * bpp + y * sstr + i]; 225 dst[x * 3 + y * dstr + i] = src[x * bpp + y * sstr + i];
126} 226}
127 OUTPUT: 227 OUTPUT:
128 RETVAL 228 RETVAL
129 229
130GdkPixbuf_noinc * 230GdkPixbuf_noinc *
131flop (GdkPixbuf *pb) 231rotate (GdkPixbuf *pb, int angle)
132 CODE: 232 CODE:
133{ 233 RETVAL = gdk_pixbuf_rotate_simple (pb, angle == 0 ? GDK_PIXBUF_ROTATE_NONE
134 int w = gdk_pixbuf_get_width (pb); 234 : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE
135 int h = gdk_pixbuf_get_height (pb); 235 : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN
136 int bpp = gdk_pixbuf_get_n_channels (pb); 236 : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE
137 int x, y, i; 237 : angle);
138 guchar *src = gdk_pixbuf_get_pixels (pb), *dst; 238 OUTPUT:
139 int sstr = gdk_pixbuf_get_rowstride (pb), dstr; 239 RETVAL
140 240
141 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, w, h); 241GdkPixbuf_noinc *
242load_jpeg (SV *path, int thumbnail=0)
243 CODE:
244{
245 struct jpeg_decompress_struct cinfo;
246 struct jpg_err_mgr jerr;
247 guchar *data;
248 int rs;
249 FILE *fp;
250 volatile GdkPixbuf *pb = 0;
142 251
252 RETVAL = 0;
253
254 fp = fopen (SvPVbyte_nolen (path), "rb");
255
256 if (!fp)
257 XSRETURN_UNDEF;
258
259 cinfo.err = jpeg_std_error (&jerr.err);
260
261 jerr.err.error_exit = cv_error_exit;
262 jerr.err.output_message = cv_error_output;
263
264 if ((rs = setjmp (jerr.setjmp_buffer)))
265 {
266 fclose (fp);
267 jpeg_destroy_decompress (&cinfo);
268
269 if (pb)
270 g_object_unref ((gpointer)pb);
271
272 XSRETURN_UNDEF;
273 }
274
275 jpeg_create_decompress (&cinfo);
276
277 jpeg_stdio_src (&cinfo, fp);
278 jpeg_read_header (&cinfo, TRUE);
279
280 cinfo.dct_method = JDCT_DEFAULT;
281 cinfo.do_fancy_upsampling = FALSE; /* worse quality, but nobody compained so far, and gdk-pixbuf does the same */
282 cinfo.do_block_smoothing = FALSE;
283 cinfo.out_color_space = JCS_RGB;
284 cinfo.quantize_colors = FALSE;
285
286 cinfo.scale_num = 1;
287 cinfo.scale_denom = 1;
288
289 jpeg_calc_output_dimensions (&cinfo);
290
291 if (thumbnail)
292 {
293 cinfo.dct_method = JDCT_FASTEST;
294 cinfo.do_fancy_upsampling = FALSE;
295
296 while (cinfo.scale_denom < 8
297 && cinfo.output_width >= IW*4
298 && cinfo.output_height >= IH*4)
299 {
300 cinfo.scale_denom <<= 1;
301 jpeg_calc_output_dimensions (&cinfo);
302 }
303 }
304
305 pb = RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, cinfo.output_width, cinfo.output_height);
306 if (!RETVAL)
307 longjmp (jerr.setjmp_buffer, 2);
308
143 dst = gdk_pixbuf_get_pixels (RETVAL); 309 data = gdk_pixbuf_get_pixels (RETVAL);
144 dstr = gdk_pixbuf_get_rowstride (RETVAL); 310 rs = gdk_pixbuf_get_rowstride (RETVAL);
145 311
312 if (cinfo.output_components != 3)
313 longjmp (jerr.setjmp_buffer, 3);
314
315 jpeg_start_decompress (&cinfo);
316
317 while (cinfo.output_scanline < cinfo.output_height)
318 {
319 int remaining = cinfo.output_height - cinfo.output_scanline;
320 JSAMPROW rp[4];
321
322 rp [0] = data + cinfo.output_scanline * rs;
323 rp [1] = (guchar *)rp [0] + rs;
324 rp [2] = (guchar *)rp [1] + rs;
325 rp [3] = (guchar *)rp [2] + rs;
326
327 jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4);
328 }
329
330 jpeg_finish_decompress (&cinfo);
331 fclose (fp);
332 jpeg_destroy_decompress (&cinfo);
333}
334 OUTPUT:
335 RETVAL
336
337void
338compare (GdkPixbuf *a, GdkPixbuf *b)
339 PPCODE:
340{
341 int w = gdk_pixbuf_get_width (a);
342 int h = gdk_pixbuf_get_height (a);
343 int sa = gdk_pixbuf_get_rowstride (a);
344 int sb = gdk_pixbuf_get_rowstride (b);
345
346 guchar *pa = gdk_pixbuf_get_pixels (a);
347 guchar *pb = gdk_pixbuf_get_pixels (b);
348
349 int x, y;
350
351 assert (w == gdk_pixbuf_get_width (b));
352 assert (h == gdk_pixbuf_get_height (b));
353
354 assert (gdk_pixbuf_get_n_channels (a) == 3);
355 assert (gdk_pixbuf_get_n_channels (b) == 3);
356
357 double diff = 0.;
358 int peak = 0;
359
360 if (w && h)
146 for (y = 0; y < h; y++) 361 for (y = 0; y < h; y++)
362 {
363 guchar *pa_ = pa + y * sa;
364 guchar *pb_ = pb + y * sb;
365
147 for (x = 0; x < w; x++) 366 for (x = 0; x < w; x++)
148 for (i = 0; i < bpp; i++) 367 {
149 dst[(w - 1 - x) * bpp + y * dstr + i] = src[x * bpp + y * sstr + i]; 368 int d;
369
370 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
371 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
372 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
373 }
374 }
375
376 EXTEND (SP, 2);
377 PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.)))));
378 PUSHs (sv_2mortal (newSVnv (peak / 255.)));
150} 379}
151 OUTPUT:
152 RETVAL
153 380
154############################################################################# 381#############################################################################
155 382
156MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer 383MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer
157 384
385SV *
386foldcase (SV *pathsv)
387 PROTOTYPE: $
388 CODE:
389{
390 STRLEN plen;
391 U8 *path = (U8 *)SvPVutf8 (pathsv, plen);
392 U8 *pend = path + plen;
393 U8 dst [plen * 6 * 3], *dstp = dst;
394
395 while (path < pend)
396 {
397 U8 ch = *path;
398
399 if (ch >= 'a' && ch <= 'z')
400 *dstp++ = *path++;
401 else if (ch >= '0' && ch <= '9')
402 {
403 STRLEN el, nl = 0;
404 while (*path >= '0' && *path <= '9' && path < pend)
405 path++, nl++;
406
407 for (el = nl; el < 6; el++)
408 *dstp++ = '0';
409
410 memcpy (dstp, path - nl, nl);
411 dstp += nl;
412 }
413 else
414 {
415 STRLEN cl;
416 to_utf8_fold (path, dstp, &cl);
417 dstp += cl;
418 path += is_utf8_char (path);
419 }
420 }
421
422 RETVAL = newSVpvn ((const char *)dst, dstp - dst);
423}
424 OUTPUT:
425 RETVAL
426
158GdkPixbuf_noinc * 427GdkPixbuf_noinc *
159p7_to_pb (int w, int h, guchar *src) 428p7_to_pb (int w, int h, SV *src_sv)
429 PROTOTYPE: @
160 CODE: 430 CODE:
161{ 431{
162 int x, y; 432 int x, y;
163 guchar *dst, *d; 433 guchar *dst, *d;
164 int dstr; 434 int dstr;
435 guchar *src = (guchar *)SvPVbyte_nolen (src_sv);
165 436
166 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h); 437 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
167 dst = gdk_pixbuf_get_pixels (RETVAL); 438 dst = gdk_pixbuf_get_pixels (RETVAL);
168 dstr = gdk_pixbuf_get_rowstride (RETVAL); 439 dstr = gdk_pixbuf_get_rowstride (RETVAL);
169 440
178 } 449 }
179} 450}
180 OUTPUT: 451 OUTPUT:
181 RETVAL 452 RETVAL
182 453
454#############################################################################
455
456MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
457
458void
459dump_ascii85 (PerlIO *fp, GdkPixbuf *pb)
460 CODE:
461{
462 int w = gdk_pixbuf_get_width (pb);
463 int h = gdk_pixbuf_get_height (pb);
464 int x, y, i;
465 guchar *dst;
466 int bpp = gdk_pixbuf_get_n_channels (pb);
467 guchar *src = gdk_pixbuf_get_pixels (pb);
468 int sstr = gdk_pixbuf_get_rowstride (pb);
469
470 a85_init ();
471
472 for (y = 0; y < h; y++)
473 for (x = 0; x < w; x++)
474 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
475 a85_push (fp, src [x * bpp + y * sstr + i]);
476
477 a85_finish (fp);
478}
479
480void
481dump_binary (PerlIO *fp, GdkPixbuf *pb)
482 CODE:
483{
484 int w = gdk_pixbuf_get_width (pb);
485 int h = gdk_pixbuf_get_height (pb);
486 int x, y, i;
487 guchar *dst;
488 int bpp = gdk_pixbuf_get_n_channels (pb);
489 guchar *src = gdk_pixbuf_get_pixels (pb);
490 int sstr = gdk_pixbuf_get_rowstride (pb);
491
492 for (y = 0; y < h; y++)
493 for (x = 0; x < w; x++)
494 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
495 PerlIO_putc (fp, src [x * bpp + y * sstr + i]);
496}
497
498#############################################################################
499
500MODULE = Gtk2::CV PACKAGE = Gtk2::CV
501
183SV * 502SV *
184pb_to_p7 (GdkPixbuf *pb) 503pb_to_hv84 (GdkPixbuf *pb)
185 CODE: 504 CODE:
186{ 505{
187 int w = gdk_pixbuf_get_width (pb); 506 int w = gdk_pixbuf_get_width (pb);
188 int h = gdk_pixbuf_get_height (pb); 507 int h = gdk_pixbuf_get_height (pb);
189 int x, y; 508 int x, y;
190 guchar *dst; 509 guchar *dst;
191 int bpp = gdk_pixbuf_get_n_channels (pb); 510 int bpp = gdk_pixbuf_get_n_channels (pb);
192 guchar *src = gdk_pixbuf_get_pixels (pb); 511 guchar *src = gdk_pixbuf_get_pixels (pb);
193 int sstr = gdk_pixbuf_get_rowstride (pb); 512 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 513
356 RETVAL = newSV (6 * 8 * 12 / 8); 514 RETVAL = newSV (6 * 8 * 12 / 8);
357 SvPOK_only (RETVAL); 515 SvPOK_only (RETVAL);
358 SvCUR_set (RETVAL, 6 * 8 * 12 / 8); 516 SvCUR_set (RETVAL, 6 * 8 * 12 / 8);
359 517
360 dst = SvPVX (RETVAL); 518 dst = (guchar *)SvPVX (RETVAL);
361 519
362 /* some primitive error distribution + random dithering */ 520 /* some primitive error distribution + random dithering */
363 521
364 for (y = 0; y < h; y++) 522 for (y = 0; y < h; y++)
365 { 523 {
423 } 581 }
424} 582}
425 OUTPUT: 583 OUTPUT:
426 RETVAL 584 RETVAL
427 585
586#############################################################################
428 587
588MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Plugin::RCluster
429 589
590SV *
591make_histograms (SV *ar)
592 CODE:
593{
594 int i;
595 AV *av, *result;
596
597 if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV)
598 croak ("Not an array ref as first argument to make_histogram");
599
600 av = (AV *) SvRV (ar);
601 result = newAV ();
602
603 for (i = 0; i <= av_len (av); ++i)
604 {
605 const int HISTSIZE = 64;
606
607 int j;
608 SV *sv = *av_fetch (av, i, 1);
609 STRLEN len;
610 char *buf = SvPVbyte (sv, len);
611
612 int tmphist[HISTSIZE];
613 float *hist;
614
615 SV *histsv = newSV (HISTSIZE * sizeof (float) + 1);
616 SvPOK_on (histsv);
617 SvCUR_set (histsv, HISTSIZE * sizeof (float));
618 hist = (float *)SvPVX (histsv);
619
620 Zero (tmphist, sizeof (tmphist), char);
621
622 for (j = len; j--; )
623 {
624 unsigned int idx
625 = ((*buf & 0xc0) >> 2)
626 | ((*buf & 0x18) >> 1)
627 | (*buf & 0x03);
628
629 ++tmphist[idx];
630 ++buf;
631 }
632
633 for (j = 0; j < HISTSIZE; ++j)
634 hist[j] = (float)tmphist[j] / (len + 1e-30);
635
636 av_push (result, histsv);
637 }
638
639 RETVAL = newRV_noinc ((SV *)result);
640}
641 OUTPUT:
642 RETVAL
643
644

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines