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

Comparing CV/CV.xs (file contents):
Revision 1.27 by root, Sun Aug 21 02:23:52 2005 UTC vs.
Revision 1.47 by root, Tue Jun 7 06:59:57 2016 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>
9#include <glib.h> 12#include <glib.h>
10#include <gtk/gtk.h> 13#include <gtk/gtk.h>
11#include <gdk-pixbuf/gdk-pixbuf.h> 14#include <gdk-pixbuf/gdk-pixbuf.h>
12 15
13#include <gperl.h> 16#include <gperl.h>
14#include <gtk2perl.h> 17#include <gtk2perl.h>
15 18
19#include <assert.h>
20
21#include "perlmulticore.h"
22
16#define IW 80 /* MUST match Schnauer.pm! */ 23#define IW 80 /* MUST match Schnauzer.pm! */
17#define IH 60 /* MUST match Schnauer.pm! */ 24#define IH 60 /* MUST match Schnauzer.pm! */
18 25
19#define RAND (seed = (seed + 7141) * 54773 % 134456) 26#define RAND (seed = (seed + 7141) * 54773 % 134456)
20 27
21#define LINELENGTH 240 28#define LINELENGTH 240
22 29
23#define ELLIPSIS "\xe2\x80\xa6" 30#define ELLIPSIS "\xe2\x80\xa6"
31
32typedef char *octet_string;
24 33
25struct jpg_err_mgr 34struct jpg_err_mgr
26{ 35{
27 struct jpeg_error_mgr err; 36 struct jpeg_error_mgr err;
28 jmp_buf setjmp_buffer; 37 jmp_buf setjmp_buffer;
65 74
66 *h &= 255; 75 *h &= 255;
67 } 76 }
68} 77}
69 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
70static guint32 a85_val; 134static guint32 a85_val;
71static guint a85_cnt; 135static guint a85_cnt;
72static guchar a85_buf[LINELENGTH], *a85_ptr; 136static guchar a85_buf[LINELENGTH], *a85_ptr;
73 137
74static void 138static void
126 190
127MODULE = Gtk2::CV PACKAGE = Gtk2::CV 191MODULE = Gtk2::CV PACKAGE = Gtk2::CV
128 192
129PROTOTYPES: ENABLE 193PROTOTYPES: ENABLE
130 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
131# missing in Gtk2 perl module 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);
132 262
133gboolean 263gboolean
134gdk_net_wm_supports (GdkAtom property) 264gdk_net_wm_supports (GdkAtom property)
135 CODE: 265 CODE:
136#if defined(GDK_WINDOWING_X11) && !defined(GDK_MULTIHEAD_SAFE) 266#if defined(GDK_WINDOWING_X11) && !defined(GDK_MULTIHEAD_SAFE)
142 RETVAL 272 RETVAL
143 273
144GdkPixbuf_noinc * 274GdkPixbuf_noinc *
145dealpha_expose (GdkPixbuf *pb) 275dealpha_expose (GdkPixbuf *pb)
146 CODE: 276 CODE:
277 perlinterp_release ();
147{ 278{
148 int w = gdk_pixbuf_get_width (pb); 279 int w = gdk_pixbuf_get_width (pb);
149 int h = gdk_pixbuf_get_height (pb); 280 int h = gdk_pixbuf_get_height (pb);
150 int bpp = gdk_pixbuf_get_n_channels (pb); 281 int bpp = gdk_pixbuf_get_n_channels (pb);
151 int x, y, i; 282 int x, y, i;
160 for (x = 0; x < w; x++) 291 for (x = 0; x < w; x++)
161 for (y = 0; y < h; y++) 292 for (y = 0; y < h; y++)
162 for (i = 0; i < 3; i++) 293 for (i = 0; i < 3; i++)
163 dst[x * 3 + y * dstr + i] = src[x * bpp + y * sstr + i]; 294 dst[x * 3 + y * dstr + i] = src[x * bpp + y * sstr + i];
164} 295}
296 perlinterp_acquire ();
165 OUTPUT: 297 OUTPUT:
166 RETVAL 298 RETVAL
167 299
168GdkPixbuf_noinc * 300GdkPixbuf_noinc *
169transpose (GdkPixbuf *pb) 301rotate (GdkPixbuf *pb, int angle)
170 CODE: 302 CODE:
171{ 303 perlinterp_release ();
172 int w = gdk_pixbuf_get_width (pb); 304 if (angle < 0)
173 int h = gdk_pixbuf_get_height (pb); 305 angle += 360;
174 int bpp = gdk_pixbuf_get_n_channels (pb); 306 RETVAL = gdk_pixbuf_rotate_simple (pb, angle == 0 ? GDK_PIXBUF_ROTATE_NONE
175 int x, y, i; 307 : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE
176 guchar *src = gdk_pixbuf_get_pixels (pb), *dst; 308 : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN
177 int sstr = gdk_pixbuf_get_rowstride (pb), dstr; 309 : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE
178 310 : angle);
179 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, h, w); 311 perlinterp_acquire ();
180
181 dst = gdk_pixbuf_get_pixels (RETVAL);
182 dstr = gdk_pixbuf_get_rowstride (RETVAL);
183
184 for (y = 0; y < h; y++)
185 for (x = 0; x < w; x++)
186 for (i = 0; i < bpp; i++)
187 dst[y * bpp + x * dstr + i] = src[x * bpp + y * sstr + i];
188}
189 OUTPUT: 312 OUTPUT:
190 RETVAL 313 RETVAL
191 314
192GdkPixbuf_noinc * 315GdkPixbuf_noinc *
193flop (GdkPixbuf *pb) 316load_jpeg (SV *path, int thumbnail = 0, int iw = 0, int ih = 0)
194 CODE:
195{
196 int w = gdk_pixbuf_get_width (pb);
197 int h = gdk_pixbuf_get_height (pb);
198 int bpp = gdk_pixbuf_get_n_channels (pb);
199 int x, y, i;
200 guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
201 int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
202
203 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, w, h);
204
205 dst = gdk_pixbuf_get_pixels (RETVAL);
206 dstr = gdk_pixbuf_get_rowstride (RETVAL);
207
208 for (y = 0; y < h; y++)
209 for (x = 0; x < w; x++)
210 for (i = 0; i < bpp; i++)
211 dst[(w - 1 - x) * bpp + y * dstr + i] = src[x * bpp + y * sstr + i];
212}
213 OUTPUT:
214 RETVAL
215
216GdkPixbuf_noinc *
217load_jpeg (SV *path, int thumbnail=0)
218 CODE: 317 CODE:
219{ 318{
220 struct jpeg_decompress_struct cinfo; 319 struct jpeg_decompress_struct cinfo;
221 struct jpg_err_mgr jerr; 320 struct jpg_err_mgr jerr;
222 guchar *data; 321 guchar *data;
223 int rs; 322 int rs;
224 FILE *fp; 323 FILE *fp;
225 volatile GdkPixbuf *pb = 0; 324 volatile GdkPixbuf *pb = 0;
226 gchar *filename;
227 325
228 RETVAL = 0; 326 RETVAL = 0;
229 327
230 filename = g_filename_from_utf8 (SvPVutf8_nolen (path), -1, 0, 0, 0);
231 fp = fopen (filename, "rb"); 328 fp = fopen (SvPVbyte_nolen (path), "rb");
232 g_free (filename);
233 329
234 if (!fp) 330 if (!fp)
235 XSRETURN_UNDEF; 331 XSRETURN_UNDEF;
236 332
333 perlinterp_release ();
334
237 cinfo.err = jpeg_std_error (&jerr.err); 335 cinfo.err = jpeg_std_error (&jerr.err);
238 336
239 jerr.err.error_exit = cv_error_exit; 337 jerr.err.error_exit = cv_error_exit;
240 jerr.err.output_message = cv_error_output; 338 jerr.err.output_message = cv_error_output;
241 339
245 jpeg_destroy_decompress (&cinfo); 343 jpeg_destroy_decompress (&cinfo);
246 344
247 if (pb) 345 if (pb)
248 g_object_unref ((gpointer)pb); 346 g_object_unref ((gpointer)pb);
249 347
348 perlinterp_acquire ();
250 XSRETURN_UNDEF; 349 XSRETURN_UNDEF;
251 } 350 }
252 351
253 jpeg_create_decompress (&cinfo); 352 jpeg_create_decompress (&cinfo);
254 353
270 { 369 {
271 cinfo.dct_method = JDCT_FASTEST; 370 cinfo.dct_method = JDCT_FASTEST;
272 cinfo.do_fancy_upsampling = FALSE; 371 cinfo.do_fancy_upsampling = FALSE;
273 372
274 while (cinfo.scale_denom < 8 373 while (cinfo.scale_denom < 8
275 && cinfo.output_width >= IW*4 374 && cinfo.output_width >= iw*4
276 && cinfo.output_height >= IH*4) 375 && cinfo.output_height >= ih*4)
277 { 376 {
278 cinfo.scale_denom <<= 1; 377 cinfo.scale_denom <<= 1;
279 jpeg_calc_output_dimensions (&cinfo); 378 jpeg_calc_output_dimensions (&cinfo);
280 } 379 }
281 } 380 }
306 } 405 }
307 406
308 jpeg_finish_decompress (&cinfo); 407 jpeg_finish_decompress (&cinfo);
309 fclose (fp); 408 fclose (fp);
310 jpeg_destroy_decompress (&cinfo); 409 jpeg_destroy_decompress (&cinfo);
410 perlinterp_acquire ();
311} 411}
312 OUTPUT: 412 OUTPUT:
313 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}
314 462
315############################################################################# 463#############################################################################
316 464
317MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer 465MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer
466
467# currently only works for filenames (octet strings)
318 468
319SV * 469SV *
320foldcase (SV *pathsv) 470foldcase (SV *pathsv)
321 PROTOTYPE: $ 471 PROTOTYPE: $
322 CODE: 472 CODE:
323{ 473{
324 STRLEN plen; 474 STRLEN plen;
325 U8 *path = (U8 *)SvPVutf8 (pathsv, plen); 475 U8 *path = (U8 *)SvPV (pathsv, plen);
326 U8 *pend = path + plen; 476 U8 *pend = path + plen;
327 U8 dst [plen * 6 * 3], *dstp = dst; 477 U8 dst [plen * 6 * 3], *dstp = dst;
328 478
329 while (path < pend) 479 while (path < pend)
330 { 480 {
331 U8 ch = *path; 481 U8 ch = *path;
332 482
333 if (ch >= 'a' && ch <= 'z') 483 if (ch >= 'a' && ch <= 'z')
334 *dstp++ = *path++; 484 *dstp++ = *path++;
485 else if (ch >= 'A' && ch <= 'Z')
486 *dstp++ = *path++ + ('a' - 'A');
335 else if (ch >= '0' && ch <= '9') 487 else if (ch >= '0' && ch <= '9')
336 { 488 {
337 STRLEN el, nl = 0; 489 STRLEN el, nl = 0;
338 while (*path >= '0' && *path <= '9' && path < pend) 490 while (*path >= '0' && *path <= '9' && path < pend)
339 path++, nl++; 491 path++, nl++;
342 *dstp++ = '0'; 494 *dstp++ = '0';
343 495
344 memcpy (dstp, path - nl, nl); 496 memcpy (dstp, path - nl, nl);
345 dstp += nl; 497 dstp += nl;
346 } 498 }
499 else
500 *dstp++ = *path++;
501#if 0
347 else 502 else
348 { 503 {
349 STRLEN cl; 504 STRLEN cl;
350 to_utf8_fold (path, dstp, &cl); 505 to_utf8_fold (path, dstp, &cl);
351 dstp += cl; 506 dstp += cl;
352 path += is_utf8_char (path); 507 path += is_utf8_char (path);
353 } 508 }
509#endif
354 } 510 }
355 511
356 RETVAL = newSVpvn ((const char *)dst, dstp - dst); 512 RETVAL = newSVpvn ((const char *)dst, dstp - dst);
357} 513}
358 OUTPUT: 514 OUTPUT:
383 } 539 }
384} 540}
385 OUTPUT: 541 OUTPUT:
386 RETVAL 542 RETVAL
387 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
388SV * 592SV *
389pb_to_p7 (GdkPixbuf *pb) 593pb_to_hv84 (GdkPixbuf *pb)
390 CODE: 594 CODE:
391{ 595{
392 int w = gdk_pixbuf_get_width (pb); 596 int w = gdk_pixbuf_get_width (pb);
393 int h = gdk_pixbuf_get_height (pb); 597 int h = gdk_pixbuf_get_height (pb);
394 int x, y; 598 int x, y;
395 guchar *dst; 599 guchar *dst;
396 int bpp = gdk_pixbuf_get_n_channels (pb); 600 int bpp = gdk_pixbuf_get_n_channels (pb);
397 guchar *src = gdk_pixbuf_get_pixels (pb); 601 guchar *src = gdk_pixbuf_get_pixels (pb);
398 int sstr = gdk_pixbuf_get_rowstride (pb); 602 int sstr = gdk_pixbuf_get_rowstride (pb);
399 int Er[IW], Eg[IW], Eb[IW];
400 int seed = 77;
401
402 RETVAL = newSV (w * h);
403 SvPOK_only (RETVAL);
404 SvCUR_set (RETVAL, w * h);
405
406 dst = (guchar *)SvPVX (RETVAL);
407
408 memset (Er, 0, sizeof (int) * IW);
409 memset (Eg, 0, sizeof (int) * IW);
410 memset (Eb, 0, sizeof (int) * IW);
411
412 /* some primitive error distribution + random dithering */
413
414 for (y = 0; y < h; y++)
415 {
416 int er = 0, eg = 0, eb = 0;
417
418 for (x = 0; x < w; x++)
419 {
420 int r, g, b;
421 guchar *p = src + x * bpp + y * sstr;
422
423 r = ((p[0] + er + Er[x]) * 7 + (RAND & 127) + 64) / 255;
424 g = ((p[1] + eg + Eg[x]) * 7 + (RAND & 127) + 64) / 255;
425 b = ((p[2] + eb + Eb[x]) * 3 + (RAND & 127) + 64) / 255;
426
427 r = r > 7 ? 7 : r < 0 ? 0 : r;
428 g = g > 7 ? 7 : g < 0 ? 0 : g;
429 b = b > 3 ? 3 : b < 0 ? 0 : b;
430
431 er += p[0] - (r * 255 + 4) / 7;
432 eg += p[1] - (g * 255 + 4) / 7;
433 eb += p[2] - (b * 255 + 2) / 3;
434
435 Er[x] = er >> 1; er -= (er + 1) >> 1;
436 Eg[x] = eg >> 1; eg -= (eg + 1) >> 1;
437 Eb[x] = eb >> 1; eb -= (eb + 1) >> 1;
438
439 *dst++ = r << 5 | g << 2 | b;
440 }
441 }
442}
443 OUTPUT:
444 RETVAL
445
446#############################################################################
447
448MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
449
450void
451dump_ascii85 (PerlIO *fp, GdkPixbuf *pb)
452 CODE:
453{
454 int w = gdk_pixbuf_get_width (pb);
455 int h = gdk_pixbuf_get_height (pb);
456 int x, y, i;
457 guchar *dst;
458 int bpp = gdk_pixbuf_get_n_channels (pb);
459 guchar *src = gdk_pixbuf_get_pixels (pb);
460 int sstr = gdk_pixbuf_get_rowstride (pb);
461
462 a85_init ();
463
464 for (y = 0; y < h; y++)
465 for (x = 0; x < w; x++)
466 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
467 a85_push (fp, src [x * bpp + y * sstr + i]);
468
469 a85_finish (fp);
470}
471
472void
473dump_binary (PerlIO *fp, GdkPixbuf *pb)
474 CODE:
475{
476 int w = gdk_pixbuf_get_width (pb);
477 int h = gdk_pixbuf_get_height (pb);
478 int x, y, i;
479 guchar *dst;
480 int bpp = gdk_pixbuf_get_n_channels (pb);
481 guchar *src = gdk_pixbuf_get_pixels (pb);
482 int sstr = gdk_pixbuf_get_rowstride (pb);
483
484 for (y = 0; y < h; y++)
485 for (x = 0; x < w; x++)
486 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
487 PerlIO_putc (fp, src [x * bpp + y * sstr + i]);
488}
489
490#############################################################################
491
492MODULE = Gtk2::CV PACKAGE = Gtk2::CV
493
494SV *
495pb_to_hv84 (GdkPixbuf *pb)
496 CODE:
497{
498 int w = gdk_pixbuf_get_width (pb);
499 int h = gdk_pixbuf_get_height (pb);
500 int x, y;
501 guchar *dst;
502 int bpp = gdk_pixbuf_get_n_channels (pb);
503 guchar *src = gdk_pixbuf_get_pixels (pb);
504 int sstr = gdk_pixbuf_get_rowstride (pb);
505 603
506 RETVAL = newSV (6 * 8 * 12 / 8); 604 RETVAL = newSV (6 * 8 * 12 / 8);
507 SvPOK_only (RETVAL); 605 SvPOK_only (RETVAL);
508 SvCUR_set (RETVAL, 6 * 8 * 12 / 8); 606 SvCUR_set (RETVAL, 6 * 8 * 12 / 8);
509 607
578############################################################################# 676#############################################################################
579 677
580MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Plugin::RCluster 678MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Plugin::RCluster
581 679
582SV * 680SV *
583make_histograms (SV *ar) 681extract_features (SV *ar)
584 CODE: 682 CODE:
585{ 683{
586 int i; 684 int i;
587 AV *av, *result; 685 AV *av, *result;
588 686
589 if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV) 687 if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV)
590 croak ("Not an array ref as first argument to make_histogram"); 688 croak ("Not an array ref as first argument to extract_features");
591 689
592 av = (AV *) SvRV (ar); 690 av = (AV *) SvRV (ar);
593 result = newAV (); 691 result = newAV ();
594 692
595 for (i = 0; i <= av_len (av); ++i) 693 for (i = 0; i <= av_len (av); ++i)
596 { 694 {
597 const int HISTSIZE = 64;
598
599 int j;
600 SV *sv = *av_fetch (av, i, 1); 695 SV *sv = *av_fetch (av, i, 1);
601 STRLEN len;
602 char *buf = SvPVbyte (sv, len);
603
604 int tmphist[HISTSIZE];
605 float *hist;
606
607 SV *histsv = newSV (HISTSIZE * sizeof (float) + 1); 696 SV *histsv = newSV (9 * sizeof (float) + 1);
697
608 SvPOK_on (histsv); 698 SvPOK_on (histsv);
609 SvCUR_set (histsv, HISTSIZE * sizeof (float)); 699 SvCUR_set (histsv, 9 * sizeof (float));
610 hist = (float *)SvPVX (histsv); 700 float *hist = (float *)SvPVX (histsv);
611 701
612 Zero (tmphist, sizeof (tmphist), char); 702 struct feature f_h, f_s, f_v;
703 feature_init (&f_h);
704 feature_init (&f_s);
705 feature_init (&f_v);
613 706
614 for (j = len; j--; )
615 {
616 unsigned int idx
617 = ((*buf & 0xc0) >> 2)
618 | ((*buf & 0x18) >> 1)
619 | (*buf & 0x03);
620
621 ++tmphist[idx];
622 ++buf;
623 }
624 707 {
625 for (j = 0; j < HISTSIZE; ++j) 708 STRLEN len;
626 hist[j] = (float)tmphist[j] / (len + 1e-30); 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;
627 752
628 av_push (result, histsv); 753 av_push (result, histsv);
629 } 754 }
630 755
631 RETVAL = newRV_noinc ((SV *)result); 756 RETVAL = newRV_noinc ((SV *)result);
632} 757}
633 OUTPUT: 758 OUTPUT:
634 RETVAL 759 RETVAL
635 760
636

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines