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

Comparing CV/CV.xs (file contents):
Revision 1.22 by root, Sun Aug 14 02:25:44 2005 UTC vs.
Revision 1.34 by root, Tue Mar 7 16:26:25 2006 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
14#define IW 80 /* MUST match Schnauer.pm! */ 19#define IW 80 /* MUST match Schnauzer.pm! */
15#define IH 60 /* MUST match Schnauer.pm! */ 20#define IH 60 /* MUST match Schnauzer.pm! */
16 21
17#define RAND (seed = (seed + 7141) * 54773 % 134456) 22#define RAND (seed = (seed + 7141) * 54773 % 134456)
18 23
19#define LINELENGTH 240 24#define LINELENGTH 240
20 25
124 129
125MODULE = Gtk2::CV PACKAGE = Gtk2::CV 130MODULE = Gtk2::CV PACKAGE = Gtk2::CV
126 131
127PROTOTYPES: ENABLE 132PROTOTYPES: ENABLE
128 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
129# missing in Gtk2 perl module 193# missing in Gtk2 perl module
130 194
131gboolean 195gboolean
132gdk_net_wm_supports (GdkAtom property) 196gdk_net_wm_supports (GdkAtom property)
133 CODE: 197 CODE:
138#endif 202#endif
139 OUTPUT: 203 OUTPUT:
140 RETVAL 204 RETVAL
141 205
142GdkPixbuf_noinc * 206GdkPixbuf_noinc *
143transpose (GdkPixbuf *pb) 207dealpha_expose (GdkPixbuf *pb)
144 CODE: 208 CODE:
145{ 209{
146 int w = gdk_pixbuf_get_width (pb); 210 int w = gdk_pixbuf_get_width (pb);
147 int h = gdk_pixbuf_get_height (pb); 211 int h = gdk_pixbuf_get_height (pb);
148 int bpp = gdk_pixbuf_get_n_channels (pb); 212 int bpp = gdk_pixbuf_get_n_channels (pb);
149 int x, y, i; 213 int x, y, i;
150 guchar *src = gdk_pixbuf_get_pixels (pb), *dst; 214 guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
151 int sstr = gdk_pixbuf_get_rowstride (pb), dstr; 215 int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
152 216
153 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, h, w); 217 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
154 218
155 dst = gdk_pixbuf_get_pixels (RETVAL); 219 dst = gdk_pixbuf_get_pixels (RETVAL);
156 dstr = gdk_pixbuf_get_rowstride (RETVAL); 220 dstr = gdk_pixbuf_get_rowstride (RETVAL);
157 221
158 for (y = 0; y < h; y++)
159 for (x = 0; x < w; x++) 222 for (x = 0; x < w; x++)
223 for (y = 0; y < h; y++)
160 for (i = 0; i < bpp; i++) 224 for (i = 0; i < 3; i++)
161 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];
162} 226}
163 OUTPUT: 227 OUTPUT:
164 RETVAL 228 RETVAL
165 229
166GdkPixbuf_noinc * 230GdkPixbuf_noinc *
167flop (GdkPixbuf *pb) 231rotate (GdkPixbuf *pb, int angle)
168 CODE: 232 CODE:
169{ 233 RETVAL = gdk_pixbuf_rotate_simple (pb, angle == 0 ? GDK_PIXBUF_ROTATE_NONE
170 int w = gdk_pixbuf_get_width (pb); 234 : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE
171 int h = gdk_pixbuf_get_height (pb); 235 : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN
172 int bpp = gdk_pixbuf_get_n_channels (pb); 236 : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE
173 int x, y, i; 237 : angle);
174 guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
175 int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
176
177 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, w, h);
178
179 dst = gdk_pixbuf_get_pixels (RETVAL);
180 dstr = gdk_pixbuf_get_rowstride (RETVAL);
181
182 for (y = 0; y < h; y++)
183 for (x = 0; x < w; x++)
184 for (i = 0; i < bpp; i++)
185 dst[(w - 1 - x) * bpp + y * dstr + i] = src[x * bpp + y * sstr + i];
186}
187 OUTPUT: 238 OUTPUT:
188 RETVAL 239 RETVAL
189 240
190GdkPixbuf_noinc * 241GdkPixbuf_noinc *
191load_jpeg (char *path, int thumbnail=0) 242load_jpeg (SV *path, int thumbnail=0)
192 CODE: 243 CODE:
193{ 244{
194 struct jpeg_decompress_struct cinfo; 245 struct jpeg_decompress_struct cinfo;
195 struct jpg_err_mgr jerr; 246 struct jpg_err_mgr jerr;
196 guchar *data; 247 guchar *data;
197 int rs; 248 int rs;
198 FILE *fp; 249 FILE *fp;
199 volatile GdkPixbuf *pb = 0; 250 volatile GdkPixbuf *pb = 0;
251
200 RETVAL = 0; 252 RETVAL = 0;
201 253
202 if (!(fp = fopen (path, "rb"))) 254 fp = fopen (SvPVbyte_nolen (path), "rb");
255
256 if (!fp)
203 XSRETURN_UNDEF; 257 XSRETURN_UNDEF;
204 258
205 cinfo.err = jpeg_std_error (&jerr.err); 259 cinfo.err = jpeg_std_error (&jerr.err);
206 260
207 jerr.err.error_exit = cv_error_exit; 261 jerr.err.error_exit = cv_error_exit;
278 jpeg_destroy_decompress (&cinfo); 332 jpeg_destroy_decompress (&cinfo);
279} 333}
280 OUTPUT: 334 OUTPUT:
281 RETVAL 335 RETVAL
282 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 (x && y)
361 for (y = 0; y < h; y++)
362 {
363 guchar *pa_ = pa + y * sa;
364 guchar *pb_ = pb + y * sb;
365
366 for (x = 0; x < w; x++)
367 {
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.)));
379}
380
283############################################################################# 381#############################################################################
284 382
285MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer 383MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer
286 384
287SV * 385SV *
288foldcase (SV *pathsv) 386foldcase (SV *pathsv)
289 PROTOTYPE: $ 387 PROTOTYPE: $
290 CODE: 388 CODE:
291{ 389{
292 STRLEN plen; 390 STRLEN plen;
293 U8 *path = SvPVutf8 (pathsv, plen); 391 U8 *path = (U8 *)SvPVutf8 (pathsv, plen);
294 U8 *pend = path + plen; 392 U8 *pend = path + plen;
295 U8 dst [plen * 6 * 3], *dstp = dst; 393 U8 dst [plen * 6 * 3], *dstp = dst;
296 394
297 while (path < pend) 395 while (path < pend)
298 { 396 {
319 dstp += cl; 417 dstp += cl;
320 path += is_utf8_char (path); 418 path += is_utf8_char (path);
321 } 419 }
322 } 420 }
323 421
324 RETVAL = newSVpvn (dst, dstp - dst); 422 RETVAL = newSVpvn ((const char *)dst, dstp - dst);
325} 423}
326 OUTPUT: 424 OUTPUT:
327 RETVAL 425 RETVAL
328 426
329GdkPixbuf_noinc * 427GdkPixbuf_noinc *
330p7_to_pb (int w, int h, guchar *src) 428p7_to_pb (int w, int h, SV *src_sv)
331 PROTOTYPE: @ 429 PROTOTYPE: @
332 CODE: 430 CODE:
333{ 431{
334 int x, y; 432 int x, y;
335 guchar *dst, *d; 433 guchar *dst, *d;
336 int dstr; 434 int dstr;
435 guchar *src = (guchar *)SvPVbyte_nolen (src_sv);
337 436
338 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h); 437 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
339 dst = gdk_pixbuf_get_pixels (RETVAL); 438 dst = gdk_pixbuf_get_pixels (RETVAL);
340 dstr = gdk_pixbuf_get_rowstride (RETVAL); 439 dstr = gdk_pixbuf_get_rowstride (RETVAL);
341 440
350 } 449 }
351} 450}
352 OUTPUT: 451 OUTPUT:
353 RETVAL 452 RETVAL
354 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
355SV * 502SV *
356pb_to_p7 (GdkPixbuf *pb) 503pb_to_hv84 (GdkPixbuf *pb)
357 CODE: 504 CODE:
358{ 505{
359 int w = gdk_pixbuf_get_width (pb); 506 int w = gdk_pixbuf_get_width (pb);
360 int h = gdk_pixbuf_get_height (pb); 507 int h = gdk_pixbuf_get_height (pb);
361 int x, y; 508 int x, y;
362 guchar *dst; 509 guchar *dst;
363 int bpp = gdk_pixbuf_get_n_channels (pb); 510 int bpp = gdk_pixbuf_get_n_channels (pb);
364 guchar *src = gdk_pixbuf_get_pixels (pb); 511 guchar *src = gdk_pixbuf_get_pixels (pb);
365 int sstr = gdk_pixbuf_get_rowstride (pb); 512 int sstr = gdk_pixbuf_get_rowstride (pb);
366 int Er[IW], Eg[IW], Eb[IW];
367 int seed = 77;
368
369 RETVAL = newSV (w * h);
370 SvPOK_only (RETVAL);
371 SvCUR_set (RETVAL, w * h);
372
373 dst = SvPVX (RETVAL);
374
375 memset (Er, 0, sizeof (int) * IW);
376 memset (Eg, 0, sizeof (int) * IW);
377 memset (Eb, 0, sizeof (int) * IW);
378
379 /* some primitive error distribution + random dithering */
380
381 for (y = 0; y < h; y++)
382 {
383 int er = 0, eg = 0, eb = 0;
384
385 for (x = 0; x < w; x++)
386 {
387 int r, g, b;
388 guchar *p = src + x * bpp + y * sstr;
389
390 r = ((p[0] + er + Er[x]) * 7 + (RAND & 127) + 64) / 255;
391 g = ((p[1] + eg + Eg[x]) * 7 + (RAND & 127) + 64) / 255;
392 b = ((p[2] + eb + Eb[x]) * 3 + (RAND & 127) + 64) / 255;
393
394 r = r > 7 ? 7 : r < 0 ? 0 : r;
395 g = g > 7 ? 7 : g < 0 ? 0 : g;
396 b = b > 3 ? 3 : b < 0 ? 0 : b;
397
398 er += p[0] - (r * 255 + 4) / 7;
399 eg += p[1] - (g * 255 + 4) / 7;
400 eb += p[2] - (b * 255 + 2) / 3;
401
402 Er[x] = er >> 1; er -= (er + 1) >> 1;
403 Eg[x] = eg >> 1; eg -= (eg + 1) >> 1;
404 Eb[x] = eb >> 1; eb -= (eb + 1) >> 1;
405
406 *dst++ = r << 5 | g << 2 | b;
407 }
408 }
409}
410 OUTPUT:
411 RETVAL
412
413#############################################################################
414
415MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
416
417void
418dump_ascii85 (PerlIO *fp, GdkPixbuf *pb)
419 CODE:
420{
421 int w = gdk_pixbuf_get_width (pb);
422 int h = gdk_pixbuf_get_height (pb);
423 int x, y, i;
424 guchar *dst;
425 int bpp = gdk_pixbuf_get_n_channels (pb);
426 guchar *src = gdk_pixbuf_get_pixels (pb);
427 int sstr = gdk_pixbuf_get_rowstride (pb);
428
429 a85_init ();
430
431 for (y = 0; y < h; y++)
432 for (x = 0; x < w; x++)
433 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
434 a85_push (fp, src [x * bpp + y * sstr + i]);
435
436 a85_finish (fp);
437}
438
439void
440dump_binary (PerlIO *fp, GdkPixbuf *pb)
441 CODE:
442{
443 int w = gdk_pixbuf_get_width (pb);
444 int h = gdk_pixbuf_get_height (pb);
445 int x, y, i;
446 guchar *dst;
447 int bpp = gdk_pixbuf_get_n_channels (pb);
448 guchar *src = gdk_pixbuf_get_pixels (pb);
449 int sstr = gdk_pixbuf_get_rowstride (pb);
450
451 for (y = 0; y < h; y++)
452 for (x = 0; x < w; x++)
453 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
454 PerlIO_putc (fp, src [x * bpp + y * sstr + i]);
455}
456
457#############################################################################
458
459MODULE = Gtk2::CV PACKAGE = Gtk2::CV
460
461SV *
462pb_to_hv84 (GdkPixbuf *pb)
463 CODE:
464{
465 int w = gdk_pixbuf_get_width (pb);
466 int h = gdk_pixbuf_get_height (pb);
467 int x, y;
468 guchar *dst;
469 int bpp = gdk_pixbuf_get_n_channels (pb);
470 guchar *src = gdk_pixbuf_get_pixels (pb);
471 int sstr = gdk_pixbuf_get_rowstride (pb);
472 513
473 RETVAL = newSV (6 * 8 * 12 / 8); 514 RETVAL = newSV (6 * 8 * 12 / 8);
474 SvPOK_only (RETVAL); 515 SvPOK_only (RETVAL);
475 SvCUR_set (RETVAL, 6 * 8 * 12 / 8); 516 SvCUR_set (RETVAL, 6 * 8 * 12 / 8);
476 517
477 dst = SvPVX (RETVAL); 518 dst = (guchar *)SvPVX (RETVAL);
478 519
479 /* some primitive error distribution + random dithering */ 520 /* some primitive error distribution + random dithering */
480 521
481 for (y = 0; y < h; y++) 522 for (y = 0; y < h; y++)
482 { 523 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines