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.35 by root, Tue Mar 7 16:45:53 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>
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
16#define IW 80 /* MUST match Schnauer.pm! */ 19#define IW 80 /* MUST match Schnauzer.pm! */
17#define IH 60 /* MUST match Schnauer.pm! */ 20#define IH 60 /* MUST match Schnauzer.pm! */
18 21
19#define RAND (seed = (seed + 7141) * 54773 % 134456) 22#define RAND (seed = (seed + 7141) * 54773 % 134456)
20 23
21#define LINELENGTH 240 24#define LINELENGTH 240
22 25
126 129
127MODULE = Gtk2::CV PACKAGE = Gtk2::CV 130MODULE = Gtk2::CV PACKAGE = Gtk2::CV
128 131
129PROTOTYPES: ENABLE 132PROTOTYPES: ENABLE
130 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
131# missing in Gtk2 perl module 193# missing in Gtk2 perl module
132 194
133gboolean 195gboolean
134gdk_net_wm_supports (GdkAtom property) 196gdk_net_wm_supports (GdkAtom property)
135 CODE: 197 CODE:
164} 226}
165 OUTPUT: 227 OUTPUT:
166 RETVAL 228 RETVAL
167 229
168GdkPixbuf_noinc * 230GdkPixbuf_noinc *
169transpose (GdkPixbuf *pb) 231rotate (GdkPixbuf *pb, int angle)
170 CODE: 232 CODE:
171{ 233 RETVAL = gdk_pixbuf_rotate_simple (pb, angle == 0 ? GDK_PIXBUF_ROTATE_NONE
172 int w = gdk_pixbuf_get_width (pb); 234 : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE
173 int h = gdk_pixbuf_get_height (pb); 235 : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN
174 int bpp = gdk_pixbuf_get_n_channels (pb); 236 : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE
175 int x, y, i; 237 : angle);
176 guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
177 int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
178
179 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, h, w);
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:
190 RETVAL
191
192GdkPixbuf_noinc *
193flop (GdkPixbuf *pb)
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: 238 OUTPUT:
214 RETVAL 239 RETVAL
215 240
216GdkPixbuf_noinc * 241GdkPixbuf_noinc *
217load_jpeg (SV *path, int thumbnail=0) 242load_jpeg (SV *path, int thumbnail=0)
221 struct jpg_err_mgr jerr; 246 struct jpg_err_mgr jerr;
222 guchar *data; 247 guchar *data;
223 int rs; 248 int rs;
224 FILE *fp; 249 FILE *fp;
225 volatile GdkPixbuf *pb = 0; 250 volatile GdkPixbuf *pb = 0;
226 gchar *filename;
227 251
228 RETVAL = 0; 252 RETVAL = 0;
229 253
230 filename = g_filename_from_utf8 (SvPVutf8_nolen (path), -1, 0, 0, 0);
231 fp = fopen (filename, "rb"); 254 fp = fopen (SvPVbyte_nolen (path), "rb");
232 g_free (filename);
233 255
234 if (!fp) 256 if (!fp)
235 XSRETURN_UNDEF; 257 XSRETURN_UNDEF;
236 258
237 cinfo.err = jpeg_std_error (&jerr.err); 259 cinfo.err = jpeg_std_error (&jerr.err);
309 fclose (fp); 331 fclose (fp);
310 jpeg_destroy_decompress (&cinfo); 332 jpeg_destroy_decompress (&cinfo);
311} 333}
312 OUTPUT: 334 OUTPUT:
313 RETVAL 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)
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}
314 380
315############################################################################# 381#############################################################################
316 382
317MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer 383MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer
318 384
383 } 449 }
384} 450}
385 OUTPUT: 451 OUTPUT:
386 RETVAL 452 RETVAL
387 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
388SV * 502SV *
389pb_to_p7 (GdkPixbuf *pb) 503pb_to_hv84 (GdkPixbuf *pb)
390 CODE: 504 CODE:
391{ 505{
392 int w = gdk_pixbuf_get_width (pb); 506 int w = gdk_pixbuf_get_width (pb);
393 int h = gdk_pixbuf_get_height (pb); 507 int h = gdk_pixbuf_get_height (pb);
394 int x, y; 508 int x, y;
395 guchar *dst; 509 guchar *dst;
396 int bpp = gdk_pixbuf_get_n_channels (pb); 510 int bpp = gdk_pixbuf_get_n_channels (pb);
397 guchar *src = gdk_pixbuf_get_pixels (pb); 511 guchar *src = gdk_pixbuf_get_pixels (pb);
398 int sstr = gdk_pixbuf_get_rowstride (pb); 512 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 513
506 RETVAL = newSV (6 * 8 * 12 / 8); 514 RETVAL = newSV (6 * 8 * 12 / 8);
507 SvPOK_only (RETVAL); 515 SvPOK_only (RETVAL);
508 SvCUR_set (RETVAL, 6 * 8 * 12 / 8); 516 SvCUR_set (RETVAL, 6 * 8 * 12 / 8);
509 517

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines