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

Comparing CV/CV.xs (file contents):
Revision 1.18 by root, Wed Jul 20 09:06:37 2005 UTC vs.
Revision 1.46 by root, Sat Jul 4 05:31:17 2015 UTC

2#include "perl.h" 2#include "perl.h"
3#include "XSUB.h" 3#include "XSUB.h"
4 4
5#include <string.h> 5#include <string.h>
6#include <setjmp.h> 6#include <setjmp.h>
7#include <math.h>
8
9#include <magic.h>
7 10
8#include <jpeglib.h> 11#include <jpeglib.h>
12#include <glib.h>
13#include <gtk/gtk.h>
9#include <gdk-pixbuf/gdk-pixbuf.h> 14#include <gdk-pixbuf/gdk-pixbuf.h>
10 15
11#include <gperl.h> 16#include <gperl.h>
12#include <gtk2perl.h> 17#include <gtk2perl.h>
13 18
19#include <assert.h>
20
21#include "perlmulticore.h"
22
14#define IW 80 /* MUST match Schnauer.pm! */ 23#define IW 80 /* MUST match Schnauzer.pm! */
15#define IH 60 /* MUST match Schnauer.pm! */ 24#define IH 60 /* MUST match Schnauzer.pm! */
16 25
17#define RAND (seed = (seed + 7141) * 54773 % 134456) 26#define RAND (seed = (seed + 7141) * 54773 % 134456)
18 27
19#define LINELENGTH 240 28#define LINELENGTH 240
20 29
21#define ELLIPSIS "\xe2\x80\xa6" 30#define ELLIPSIS "\xe2\x80\xa6"
31
32typedef char *octet_string;
22 33
23struct jpg_err_mgr 34struct jpg_err_mgr
24{ 35{
25 struct jpeg_error_mgr err; 36 struct jpeg_error_mgr err;
26 jmp_buf setjmp_buffer; 37 jmp_buf setjmp_buffer;
63 74
64 *h &= 255; 75 *h &= 255;
65 } 76 }
66} 77}
67 78
79struct feature {
80 float v1, v2, v3; // mean, square, cube
81 int n;
82};
83
84static void
85feature_init (struct feature *f)
86{
87 f->v1 = 0.;
88 f->v2 = 0.;
89 f->v3 = 0.;
90 f->n = 0;
91}
92
93// didn't find an algorithm to neatly do mean, variance and skew in one pass.
94// elmex ist schuld.
95static void
96feature_update_pass_1 (struct feature *f, unsigned int v)
97{
98 f->v1 += v;
99 f->n += 1;
100}
101
102static void
103feature_finish_pass_1 (struct feature *f)
104{
105 if (f->n < 1)
106 return;
107
108 f->v1 /= f->n;
109}
110
111static void
112feature_update_pass_2 (struct feature *f, unsigned int v)
113{
114 float d = v - f->v1;
115
116 f->v2 += d * d;
117 f->v3 += d * d * d;
118}
119
120static void
121feature_finish_pass_2 (struct feature *f)
122{
123 if (f->n < 1)
124 return;
125
126 f->v2 /= f->n;
127 f->v3 /= f->n;
128
129 f->v1 /= 255.;
130 f->v2 /= 255. * 255.; f->v2 = sqrtf (f->v2);
131 f->v3 /= 255. * 255. * 255.; f->v3 = powf (fabsf (f->v3), 1./3.);
132}
133
68static guint32 a85_val; 134static guint32 a85_val;
69static guint a85_cnt; 135static guint a85_cnt;
70static guchar a85_buf[LINELENGTH], *a85_ptr; 136static guchar a85_buf[LINELENGTH], *a85_ptr;
71 137
72static void 138static void
124 190
125MODULE = Gtk2::CV PACKAGE = Gtk2::CV 191MODULE = Gtk2::CV PACKAGE = Gtk2::CV
126 192
127PROTOTYPES: ENABLE 193PROTOTYPES: ENABLE
128 194
195# missing function in perl. really :)
196int
197common_prefix_length (a, b)
198 unsigned char *a = (unsigned char *)SvPVutf8_nolen ($arg);
199 unsigned char *b = (unsigned char *)SvPVutf8_nolen ($arg);
200 CODE:
201 RETVAL = 0;
202
203 while (*a == *b && *a)
204 {
205 RETVAL += (*a & 0xc0) != 0x80;
206 a++, b++;
207 }
208
209 OUTPUT:
210 RETVAL
211
212const char *
213magic (octet_string path)
214 CODE:
215{
216 static magic_t cookie;
217
218 if (!cookie)
219 {
220 cookie = magic_open (MAGIC_SYMLINK);
221
222 if (cookie)
223 magic_load (cookie, 0);
224 else
225 XSRETURN_UNDEF;
226 }
227
228 RETVAL = magic_file (cookie, path);
229}
230 OUTPUT:
231 RETVAL
232
233const char *
234magic_mime (octet_string path)
235 CODE:
236{
237 static magic_t cookie;
238
239 if (!cookie)
240 {
241 cookie = magic_open (MAGIC_MIME | MAGIC_SYMLINK);
242
243 if (cookie)
244 magic_load (cookie, 0);
245 else
246 XSRETURN_UNDEF;
247 }
248
249 perlinterp_release ();
250 RETVAL = magic_file (cookie, path);
251 perlinterp_acquire ();
252}
253 OUTPUT:
254 RETVAL
255
256# missing/broken in Gtk2 perl module
257
258void
259gdk_window_clear_hints (GdkWindow *window)
260 CODE:
261 gdk_window_set_geometry_hints (window, 0, 0);
262
263gboolean
264gdk_net_wm_supports (GdkAtom property)
265 CODE:
266#if defined(GDK_WINDOWING_X11) && !defined(GDK_MULTIHEAD_SAFE)
267 RETVAL = gdk_net_wm_supports (property);
268#else
269 RETVAL = 0;
270#endif
271 OUTPUT:
272 RETVAL
273
129GdkPixbuf_noinc * 274GdkPixbuf_noinc *
130transpose (GdkPixbuf *pb) 275dealpha_expose (GdkPixbuf *pb)
131 CODE: 276 CODE:
277 perlinterp_release ();
132{ 278{
133 int w = gdk_pixbuf_get_width (pb); 279 int w = gdk_pixbuf_get_width (pb);
134 int h = gdk_pixbuf_get_height (pb); 280 int h = gdk_pixbuf_get_height (pb);
135 int bpp = gdk_pixbuf_get_n_channels (pb); 281 int bpp = gdk_pixbuf_get_n_channels (pb);
136 int x, y, i; 282 int x, y, i;
137 guchar *src = gdk_pixbuf_get_pixels (pb), *dst; 283 guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
138 int sstr = gdk_pixbuf_get_rowstride (pb), dstr; 284 int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
139 285
140 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, h, w); 286 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
141 287
142 dst = gdk_pixbuf_get_pixels (RETVAL); 288 dst = gdk_pixbuf_get_pixels (RETVAL);
143 dstr = gdk_pixbuf_get_rowstride (RETVAL); 289 dstr = gdk_pixbuf_get_rowstride (RETVAL);
144 290
145 for (y = 0; y < h; y++)
146 for (x = 0; x < w; x++) 291 for (x = 0; x < w; x++)
292 for (y = 0; y < h; y++)
147 for (i = 0; i < bpp; i++) 293 for (i = 0; i < 3; i++)
148 dst[y * bpp + x * dstr + i] = src[x * bpp + y * sstr + i]; 294 dst[x * 3 + y * dstr + i] = src[x * bpp + y * sstr + i];
149} 295}
296 perlinterp_acquire ();
150 OUTPUT: 297 OUTPUT:
151 RETVAL 298 RETVAL
152 299
153GdkPixbuf_noinc * 300GdkPixbuf_noinc *
154flop (GdkPixbuf *pb) 301rotate (GdkPixbuf *pb, int angle)
155 CODE: 302 CODE:
156{ 303 perlinterp_release ();
157 int w = gdk_pixbuf_get_width (pb); 304 if (angle < 0)
158 int h = gdk_pixbuf_get_height (pb); 305 angle += 360;
159 int bpp = gdk_pixbuf_get_n_channels (pb); 306 RETVAL = gdk_pixbuf_rotate_simple (pb, angle == 0 ? GDK_PIXBUF_ROTATE_NONE
160 int x, y, i; 307 : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE
161 guchar *src = gdk_pixbuf_get_pixels (pb), *dst; 308 : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN
162 int sstr = gdk_pixbuf_get_rowstride (pb), dstr; 309 : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE
163 310 : angle);
164 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, w, h); 311 perlinterp_acquire ();
165
166 dst = gdk_pixbuf_get_pixels (RETVAL);
167 dstr = gdk_pixbuf_get_rowstride (RETVAL);
168
169 for (y = 0; y < h; y++)
170 for (x = 0; x < w; x++)
171 for (i = 0; i < bpp; i++)
172 dst[(w - 1 - x) * bpp + y * dstr + i] = src[x * bpp + y * sstr + i];
173}
174 OUTPUT: 312 OUTPUT:
175 RETVAL 313 RETVAL
176 314
177GdkPixbuf_noinc * 315GdkPixbuf_noinc *
178load_jpeg (char *path, int thumbnail=0) 316load_jpeg (SV *path, int thumbnail=0)
179 CODE: 317 CODE:
180{ 318{
181 struct jpeg_decompress_struct cinfo; 319 struct jpeg_decompress_struct cinfo;
182 struct jpg_err_mgr jerr; 320 struct jpg_err_mgr jerr;
183 guchar *data; 321 guchar *data;
184 int rs; 322 int rs;
185 FILE *fp; 323 FILE *fp;
186 volatile GdkPixbuf *pb = 0; 324 volatile GdkPixbuf *pb = 0;
325
187 RETVAL = 0; 326 RETVAL = 0;
188 327
189 if (!(fp = fopen (path, "rb"))) 328 fp = fopen (SvPVbyte_nolen (path), "rb");
329
330 if (!fp)
190 XSRETURN_UNDEF; 331 XSRETURN_UNDEF;
332
333 perlinterp_release ();
191 334
192 cinfo.err = jpeg_std_error (&jerr.err); 335 cinfo.err = jpeg_std_error (&jerr.err);
193 336
194 jerr.err.error_exit = cv_error_exit; 337 jerr.err.error_exit = cv_error_exit;
195 jerr.err.output_message = cv_error_output; 338 jerr.err.output_message = cv_error_output;
200 jpeg_destroy_decompress (&cinfo); 343 jpeg_destroy_decompress (&cinfo);
201 344
202 if (pb) 345 if (pb)
203 g_object_unref ((gpointer)pb); 346 g_object_unref ((gpointer)pb);
204 347
348 perlinterp_acquire ();
205 XSRETURN_UNDEF; 349 XSRETURN_UNDEF;
206 } 350 }
207 351
208 jpeg_create_decompress (&cinfo); 352 jpeg_create_decompress (&cinfo);
209 353
225 { 369 {
226 cinfo.dct_method = JDCT_FASTEST; 370 cinfo.dct_method = JDCT_FASTEST;
227 cinfo.do_fancy_upsampling = FALSE; 371 cinfo.do_fancy_upsampling = FALSE;
228 372
229 while (cinfo.scale_denom < 8 373 while (cinfo.scale_denom < 8
230 && (cinfo.output_width >> 1) >= IW 374 && cinfo.output_width >= IW*4
231 && (cinfo.output_height >> 1) >= IH) 375 && cinfo.output_height >= IH*4)
232 { 376 {
233 cinfo.scale_denom <<= 1; 377 cinfo.scale_denom <<= 1;
234 jpeg_calc_output_dimensions (&cinfo); 378 jpeg_calc_output_dimensions (&cinfo);
235 } 379 }
236 } 380 }
261 } 405 }
262 406
263 jpeg_finish_decompress (&cinfo); 407 jpeg_finish_decompress (&cinfo);
264 fclose (fp); 408 fclose (fp);
265 jpeg_destroy_decompress (&cinfo); 409 jpeg_destroy_decompress (&cinfo);
410 perlinterp_acquire ();
266} 411}
267 OUTPUT: 412 OUTPUT:
268 RETVAL 413 RETVAL
414
415void
416compare (GdkPixbuf *a, GdkPixbuf *b)
417 PPCODE:
418 perlinterp_release ();
419{
420 int w = gdk_pixbuf_get_width (a);
421 int h = gdk_pixbuf_get_height (a);
422
423 int sa = gdk_pixbuf_get_rowstride (a);
424 int sb = gdk_pixbuf_get_rowstride (b);
425
426 guchar *pa = gdk_pixbuf_get_pixels (a);
427 guchar *pb = gdk_pixbuf_get_pixels (b);
428
429 int x, y;
430
431 assert (w == gdk_pixbuf_get_width (b));
432 assert (h == gdk_pixbuf_get_height (b));
433
434 assert (gdk_pixbuf_get_n_channels (a) == 3);
435 assert (gdk_pixbuf_get_n_channels (b) == 3);
436
437 double diff = 0.;
438 int peak = 0;
439
440 if (w && h)
441 for (y = 0; y < h; y++)
442 {
443 guchar *pa_ = pa + y * sa;
444 guchar *pb_ = pb + y * sb;
445
446 for (x = 0; x < w; x++)
447 {
448 int d;
449
450 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
451 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
452 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
453 }
454 }
455
456 perlinterp_acquire ();
457
458 EXTEND (SP, 2);
459 PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.)))));
460 PUSHs (sv_2mortal (newSVnv (peak / 255.)));
461}
269 462
270############################################################################# 463#############################################################################
271 464
272MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer 465MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer
466
467# currently only works for filenames (octet strings)
273 468
274SV * 469SV *
275foldcase (SV *pathsv) 470foldcase (SV *pathsv)
276 PROTOTYPE: $ 471 PROTOTYPE: $
277 CODE: 472 CODE:
278{ 473{
279 STRLEN plen; 474 STRLEN plen;
280 U8 *path = SvPVutf8 (pathsv, plen); 475 U8 *path = (U8 *)SvPV (pathsv, plen);
281 U8 *pend = path + plen; 476 U8 *pend = path + plen;
282 U8 dst [plen * 6 * 3], *dstp = dst; 477 U8 dst [plen * 6 * 3], *dstp = dst;
283 478
284 while (path < pend) 479 while (path < pend)
285 { 480 {
481 U8 ch = *path;
482
286 if (*path >= '0' && *path <= '9') 483 if (ch >= 'a' && ch <= 'z')
484 *dstp++ = *path++;
485 else if (ch >= 'A' && ch <= 'Z')
486 *dstp++ = *path++ + ('a' - 'A');
487 else if (ch >= '0' && ch <= '9')
287 { 488 {
288 STRLEN el, nl = 0; 489 STRLEN el, nl = 0;
289 while (*path >= '0' && *path <= '9') 490 while (*path >= '0' && *path <= '9' && path < pend)
290 path++, nl++; 491 path++, nl++;
291 492
292 for (el = nl; el < 6; el++) 493 for (el = nl; el < 6; el++)
293 *dstp++ = '0'; 494 *dstp++ = '0';
294 495
295 memcpy (dstp, path - nl, nl); 496 memcpy (dstp, path - nl, nl);
296 dstp += nl; 497 dstp += nl;
297 } 498 }
499 else
500 *dstp++ = *path++;
501#if 0
298 else 502 else
299 { 503 {
300 STRLEN cl; 504 STRLEN cl;
301 to_utf8_fold (path, dstp, &cl); 505 to_utf8_fold (path, dstp, &cl);
302 dstp += cl; 506 dstp += cl;
303 path += is_utf8_char (path); 507 path += is_utf8_char (path);
304 } 508 }
509#endif
305 } 510 }
306 511
307 RETVAL = newSVpvn (dst, dstp - dst); 512 RETVAL = newSVpvn ((const char *)dst, dstp - dst);
308} 513}
309 OUTPUT: 514 OUTPUT:
310 RETVAL 515 RETVAL
311 516
312GdkPixbuf_noinc * 517GdkPixbuf_noinc *
313p7_to_pb (int w, int h, guchar *src) 518p7_to_pb (int w, int h, SV *src_sv)
519 PROTOTYPE: @
314 CODE: 520 CODE:
315{ 521{
316 int x, y; 522 int x, y;
317 guchar *dst, *d; 523 guchar *dst, *d;
318 int dstr; 524 int dstr;
525 guchar *src = (guchar *)SvPVbyte_nolen (src_sv);
319 526
320 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h); 527 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
321 dst = gdk_pixbuf_get_pixels (RETVAL); 528 dst = gdk_pixbuf_get_pixels (RETVAL);
322 dstr = gdk_pixbuf_get_rowstride (RETVAL); 529 dstr = gdk_pixbuf_get_rowstride (RETVAL);
323 530
332 } 539 }
333} 540}
334 OUTPUT: 541 OUTPUT:
335 RETVAL 542 RETVAL
336 543
544#############################################################################
545
546MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
547
548void
549dump_ascii85 (PerlIO *fp, GdkPixbuf *pb)
550 CODE:
551{
552 int w = gdk_pixbuf_get_width (pb);
553 int h = gdk_pixbuf_get_height (pb);
554 int x, y, i;
555 guchar *dst;
556 int bpp = gdk_pixbuf_get_n_channels (pb);
557 guchar *src = gdk_pixbuf_get_pixels (pb);
558 int sstr = gdk_pixbuf_get_rowstride (pb);
559
560 a85_init ();
561
562 for (y = 0; y < h; y++)
563 for (x = 0; x < w; x++)
564 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
565 a85_push (fp, src [x * bpp + y * sstr + i]);
566
567 a85_finish (fp);
568}
569
570void
571dump_binary (PerlIO *fp, GdkPixbuf *pb)
572 CODE:
573{
574 int w = gdk_pixbuf_get_width (pb);
575 int h = gdk_pixbuf_get_height (pb);
576 int x, y, i;
577 guchar *dst;
578 int bpp = gdk_pixbuf_get_n_channels (pb);
579 guchar *src = gdk_pixbuf_get_pixels (pb);
580 int sstr = gdk_pixbuf_get_rowstride (pb);
581
582 for (y = 0; y < h; y++)
583 for (x = 0; x < w; x++)
584 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
585 PerlIO_putc (fp, src [x * bpp + y * sstr + i]);
586}
587
588#############################################################################
589
590MODULE = Gtk2::CV PACKAGE = Gtk2::CV
591
337SV * 592SV *
338pb_to_p7 (GdkPixbuf *pb) 593pb_to_hv84 (GdkPixbuf *pb)
339 CODE: 594 CODE:
340{ 595{
341 int w = gdk_pixbuf_get_width (pb); 596 int w = gdk_pixbuf_get_width (pb);
342 int h = gdk_pixbuf_get_height (pb); 597 int h = gdk_pixbuf_get_height (pb);
343 int x, y; 598 int x, y;
344 guchar *dst; 599 guchar *dst;
345 int bpp = gdk_pixbuf_get_n_channels (pb); 600 int bpp = gdk_pixbuf_get_n_channels (pb);
346 guchar *src = gdk_pixbuf_get_pixels (pb); 601 guchar *src = gdk_pixbuf_get_pixels (pb);
347 int sstr = gdk_pixbuf_get_rowstride (pb); 602 int sstr = gdk_pixbuf_get_rowstride (pb);
348 int Er[IW], Eg[IW], Eb[IW];
349 int seed = 77;
350
351 RETVAL = newSV (w * h);
352 SvPOK_only (RETVAL);
353 SvCUR_set (RETVAL, w * h);
354
355 dst = SvPVX (RETVAL);
356
357 memset (Er, 0, sizeof (int) * IW);
358 memset (Eg, 0, sizeof (int) * IW);
359 memset (Eb, 0, sizeof (int) * IW);
360
361 /* some primitive error distribution + random dithering */
362
363 for (y = 0; y < h; y++)
364 {
365 int er = 0, eg = 0, eb = 0;
366
367 for (x = 0; x < w; x++)
368 {
369 int r, g, b;
370 guchar *p = src + x * bpp + y * sstr;
371
372 r = ((p[0] + er + Er[x]) * 7 + (RAND & 127) + 64) / 255;
373 g = ((p[1] + eg + Eg[x]) * 7 + (RAND & 127) + 64) / 255;
374 b = ((p[2] + eb + Eb[x]) * 3 + (RAND & 127) + 64) / 255;
375
376 r = r > 7 ? 7 : r < 0 ? 0 : r;
377 g = g > 7 ? 7 : g < 0 ? 0 : g;
378 b = b > 3 ? 3 : b < 0 ? 0 : b;
379
380 er += p[0] - (r * 255 + 4) / 7;
381 eg += p[1] - (g * 255 + 4) / 7;
382 eb += p[2] - (b * 255 + 2) / 3;
383
384 Er[x] = er >> 1; er -= (er + 1) >> 1;
385 Eg[x] = eg >> 1; eg -= (eg + 1) >> 1;
386 Eb[x] = eb >> 1; eb -= (eb + 1) >> 1;
387
388 *dst++ = r << 5 | g << 2 | b;
389 }
390 }
391}
392 OUTPUT:
393 RETVAL
394
395SV *
396make_histogram (SV *ar)
397 CODE:
398{
399 int i;
400 AV *av, *result;
401
402 if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV)
403 croak ("Not an array ref as first argument to make_histogram");
404
405 av = (AV *) SvRV (ar);
406 result = newAV ();
407
408 for (i = 0; i <= av_len (av); ++i)
409 {
410 const int HISTSIZE = 64;
411
412 int j;
413 SV *sv = *av_fetch (av, i, 1);
414 STRLEN len;
415 char *buf = SvPVbyte (sv, len);
416
417 int tmphist[HISTSIZE];
418 float *hist;
419
420 SV *histsv = newSV (HISTSIZE * sizeof (float) + 1);
421 SvPOK_on (histsv);
422 SvCUR_set (histsv, HISTSIZE * sizeof (float));
423 hist = (float *)SvPVX (histsv);
424
425 Zero (tmphist, sizeof (tmphist), char);
426
427 for (j = len; j--; )
428 {
429 unsigned int idx
430 = ((*buf & 0xc0) >> 2)
431 | ((*buf & 0x18) >> 1)
432 | (*buf & 0x03);
433
434 ++tmphist[idx];
435 ++buf;
436 }
437
438 for (j = 0; j < HISTSIZE; ++j)
439 hist[j] = (float)tmphist[j] / (len + 1e-30);
440
441 av_push (result, histsv);
442 }
443
444 RETVAL = newRV_noinc ((SV *)result);
445}
446 OUTPUT:
447 RETVAL
448
449#############################################################################
450
451MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
452
453void
454dump_ascii85 (PerlIO *fp, GdkPixbuf *pb)
455 CODE:
456{
457 int w = gdk_pixbuf_get_width (pb);
458 int h = gdk_pixbuf_get_height (pb);
459 int x, y, i;
460 guchar *dst;
461 int bpp = gdk_pixbuf_get_n_channels (pb);
462 guchar *src = gdk_pixbuf_get_pixels (pb);
463 int sstr = gdk_pixbuf_get_rowstride (pb);
464
465 a85_init ();
466
467 for (y = 0; y < h; y++)
468 for (x = 0; x < w; x++)
469 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
470 a85_push (fp, src [x * bpp + y * sstr + i]);
471
472 a85_finish (fp);
473}
474
475void
476dump_binary (PerlIO *fp, GdkPixbuf *pb)
477 CODE:
478{
479 int w = gdk_pixbuf_get_width (pb);
480 int h = gdk_pixbuf_get_height (pb);
481 int x, y, i;
482 guchar *dst;
483 int bpp = gdk_pixbuf_get_n_channels (pb);
484 guchar *src = gdk_pixbuf_get_pixels (pb);
485 int sstr = gdk_pixbuf_get_rowstride (pb);
486
487 for (y = 0; y < h; y++)
488 for (x = 0; x < w; x++)
489 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
490 PerlIO_putc (fp, src [x * bpp + y * sstr + i]);
491}
492
493#############################################################################
494
495MODULE = Gtk2::CV PACKAGE = Gtk2::CV
496
497SV *
498pb_to_hv84 (GdkPixbuf *pb)
499 CODE:
500{
501 int w = gdk_pixbuf_get_width (pb);
502 int h = gdk_pixbuf_get_height (pb);
503 int x, y;
504 guchar *dst;
505 int bpp = gdk_pixbuf_get_n_channels (pb);
506 guchar *src = gdk_pixbuf_get_pixels (pb);
507 int sstr = gdk_pixbuf_get_rowstride (pb);
508 603
509 RETVAL = newSV (6 * 8 * 12 / 8); 604 RETVAL = newSV (6 * 8 * 12 / 8);
510 SvPOK_only (RETVAL); 605 SvPOK_only (RETVAL);
511 SvCUR_set (RETVAL, 6 * 8 * 12 / 8); 606 SvCUR_set (RETVAL, 6 * 8 * 12 / 8);
512 607
513 dst = SvPVX (RETVAL); 608 dst = (guchar *)SvPVX (RETVAL);
514 609
515 /* some primitive error distribution + random dithering */ 610 /* some primitive error distribution + random dithering */
516 611
517 for (y = 0; y < h; y++) 612 for (y = 0; y < h; y++)
518 { 613 {
576 } 671 }
577} 672}
578 OUTPUT: 673 OUTPUT:
579 RETVAL 674 RETVAL
580 675
676#############################################################################
581 677
678MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Plugin::RCluster
582 679
680SV *
681extract_features (SV *ar)
682 CODE:
683{
684 int i;
685 AV *av, *result;
686
687 if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV)
688 croak ("Not an array ref as first argument to extract_features");
689
690 av = (AV *) SvRV (ar);
691 result = newAV ();
692
693 for (i = 0; i <= av_len (av); ++i)
694 {
695 SV *sv = *av_fetch (av, i, 1);
696 SV *histsv = newSV (9 * sizeof (float) + 1);
697
698 SvPOK_on (histsv);
699 SvCUR_set (histsv, 9 * sizeof (float));
700 float *hist = (float *)SvPVX (histsv);
701
702 struct feature f_h, f_s, f_v;
703 feature_init (&f_h);
704 feature_init (&f_s);
705 feature_init (&f_v);
706
707 {
708 STRLEN len;
709 unsigned char *buf = (unsigned char *)SvPVbyte (sv, len);
710 while (len >= 3)
711 {
712 unsigned int r, g, b, h, s, v;
713 r = *buf++; g = *buf++; b = *buf++;
714 rgb_to_hsv (r, g, b, &h, &s, &v);
715
716 feature_update_pass_1 (&f_h, h);
717 feature_update_pass_1 (&f_s, s);
718 feature_update_pass_1 (&f_v, v);
719
720 len -= 3;
721 }
722
723 feature_finish_pass_1 (&f_h);
724 feature_finish_pass_1 (&f_s);
725 feature_finish_pass_1 (&f_v);
726 }
727
728 {
729 STRLEN len;
730 unsigned char *buf = (unsigned char *)SvPVbyte (sv, len);
731 while (len >= 3)
732 {
733 unsigned int r, g, b, h, s, v;
734 r = *buf++; g = *buf++; b = *buf++;
735 rgb_to_hsv (r, g, b, &h, &s, &v);
736
737 feature_update_pass_2 (&f_h, h);
738 feature_update_pass_2 (&f_s, s);
739 feature_update_pass_2 (&f_v, v);
740
741 len -= 3;
742 }
743
744 feature_finish_pass_2 (&f_h);
745 feature_finish_pass_2 (&f_s);
746 feature_finish_pass_2 (&f_v);
747 }
748
749 hist [0] = f_h.v1 * 2.; hist [1] = f_h.v2 * 2.; hist [2] = f_h.v3 * 2.;
750 hist [3] = f_s.v1 ; hist [4] = f_s.v2 ; hist [5] = f_s.v3 ;
751 hist [6] = f_v.v1 * .5; hist [7] = f_v.v2 * .5; hist [8] = f_v.v3 * .5;
752
753 av_push (result, histsv);
754 }
755
756 RETVAL = newRV_noinc ((SV *)result);
757}
758 OUTPUT:
759 RETVAL
760

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines