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

Comparing CV/CV.xs (file contents):
Revision 1.25 by root, Fri Aug 19 00:34:54 2005 UTC vs.
Revision 1.33 by root, Fri Feb 17 08:48:06 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:
140#endif 202#endif
141 OUTPUT: 203 OUTPUT:
142 RETVAL 204 RETVAL
143 205
144GdkPixbuf_noinc * 206GdkPixbuf_noinc *
145transpose (GdkPixbuf *pb) 207dealpha_expose (GdkPixbuf *pb)
146 CODE: 208 CODE:
147{ 209{
148 int w = gdk_pixbuf_get_width (pb); 210 int w = gdk_pixbuf_get_width (pb);
149 int h = gdk_pixbuf_get_height (pb); 211 int h = gdk_pixbuf_get_height (pb);
150 int bpp = gdk_pixbuf_get_n_channels (pb); 212 int bpp = gdk_pixbuf_get_n_channels (pb);
151 int x, y, i; 213 int x, y, i;
152 guchar *src = gdk_pixbuf_get_pixels (pb), *dst; 214 guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
153 int sstr = gdk_pixbuf_get_rowstride (pb), dstr; 215 int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
154 216
155 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, h, w); 217 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
156 218
157 dst = gdk_pixbuf_get_pixels (RETVAL); 219 dst = gdk_pixbuf_get_pixels (RETVAL);
158 dstr = gdk_pixbuf_get_rowstride (RETVAL); 220 dstr = gdk_pixbuf_get_rowstride (RETVAL);
159 221
160 for (y = 0; y < h; y++)
161 for (x = 0; x < w; x++) 222 for (x = 0; x < w; x++)
223 for (y = 0; y < h; y++)
162 for (i = 0; i < bpp; i++) 224 for (i = 0; i < 3; i++)
163 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];
164} 226}
165 OUTPUT: 227 OUTPUT:
166 RETVAL 228 RETVAL
167 229
168GdkPixbuf_noinc * 230GdkPixbuf_noinc *
169flop (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, w, h);
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[(w - 1 - x) * bpp + y * dstr + i] = src[x * bpp + y * sstr + i];
188}
189 OUTPUT: 238 OUTPUT:
190 RETVAL 239 RETVAL
191 240
192GdkPixbuf_noinc * 241GdkPixbuf_noinc *
193load_jpeg (SV *path, int thumbnail=0) 242load_jpeg (SV *path, int thumbnail=0)
197 struct jpg_err_mgr jerr; 246 struct jpg_err_mgr jerr;
198 guchar *data; 247 guchar *data;
199 int rs; 248 int rs;
200 FILE *fp; 249 FILE *fp;
201 volatile GdkPixbuf *pb = 0; 250 volatile GdkPixbuf *pb = 0;
202 gchar *filename;
203 251
204 RETVAL = 0; 252 RETVAL = 0;
205 253
206 filename = g_filename_from_utf8 (SvPVutf8_nolen (path), -1, 0, 0, 0);
207 fp = fopen (filename, "rb"); 254 fp = fopen (SvPVbyte_nolen (path), "rb");
208 g_free (filename);
209 255
210 if (!fp) 256 if (!fp)
211 XSRETURN_UNDEF; 257 XSRETURN_UNDEF;
212 258
213 cinfo.err = jpeg_std_error (&jerr.err); 259 cinfo.err = jpeg_std_error (&jerr.err);
285 fclose (fp); 331 fclose (fp);
286 jpeg_destroy_decompress (&cinfo); 332 jpeg_destroy_decompress (&cinfo);
287} 333}
288 OUTPUT: 334 OUTPUT:
289 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 for (y = 0; y < h; y++)
361 {
362 guchar *pa_ = pa + y * sa;
363 guchar *pb_ = pb + y * sb;
364
365 for (x = 0; x < w; x ++)
366 {
367 int d;
368
369 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
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 }
373 }
374
375 EXTEND (SP, 2);
376 PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.)))));
377 PUSHs (sv_2mortal (newSVnv (peak / 255.)));
378}
290 379
291############################################################################# 380#############################################################################
292 381
293MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer 382MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer
294 383
359 } 448 }
360} 449}
361 OUTPUT: 450 OUTPUT:
362 RETVAL 451 RETVAL
363 452
453#############################################################################
454
455MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
456
457void
458dump_ascii85 (PerlIO *fp, GdkPixbuf *pb)
459 CODE:
460{
461 int w = gdk_pixbuf_get_width (pb);
462 int h = gdk_pixbuf_get_height (pb);
463 int x, y, i;
464 guchar *dst;
465 int bpp = gdk_pixbuf_get_n_channels (pb);
466 guchar *src = gdk_pixbuf_get_pixels (pb);
467 int sstr = gdk_pixbuf_get_rowstride (pb);
468
469 a85_init ();
470
471 for (y = 0; y < h; y++)
472 for (x = 0; x < w; x++)
473 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
474 a85_push (fp, src [x * bpp + y * sstr + i]);
475
476 a85_finish (fp);
477}
478
479void
480dump_binary (PerlIO *fp, GdkPixbuf *pb)
481 CODE:
482{
483 int w = gdk_pixbuf_get_width (pb);
484 int h = gdk_pixbuf_get_height (pb);
485 int x, y, i;
486 guchar *dst;
487 int bpp = gdk_pixbuf_get_n_channels (pb);
488 guchar *src = gdk_pixbuf_get_pixels (pb);
489 int sstr = gdk_pixbuf_get_rowstride (pb);
490
491 for (y = 0; y < h; y++)
492 for (x = 0; x < w; x++)
493 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
494 PerlIO_putc (fp, src [x * bpp + y * sstr + i]);
495}
496
497#############################################################################
498
499MODULE = Gtk2::CV PACKAGE = Gtk2::CV
500
364SV * 501SV *
365pb_to_p7 (GdkPixbuf *pb) 502pb_to_hv84 (GdkPixbuf *pb)
366 CODE: 503 CODE:
367{ 504{
368 int w = gdk_pixbuf_get_width (pb); 505 int w = gdk_pixbuf_get_width (pb);
369 int h = gdk_pixbuf_get_height (pb); 506 int h = gdk_pixbuf_get_height (pb);
370 int x, y; 507 int x, y;
371 guchar *dst; 508 guchar *dst;
372 int bpp = gdk_pixbuf_get_n_channels (pb); 509 int bpp = gdk_pixbuf_get_n_channels (pb);
373 guchar *src = gdk_pixbuf_get_pixels (pb); 510 guchar *src = gdk_pixbuf_get_pixels (pb);
374 int sstr = gdk_pixbuf_get_rowstride (pb); 511 int sstr = gdk_pixbuf_get_rowstride (pb);
375 int Er[IW], Eg[IW], Eb[IW];
376 int seed = 77;
377
378 RETVAL = newSV (w * h);
379 SvPOK_only (RETVAL);
380 SvCUR_set (RETVAL, w * h);
381
382 dst = (guchar *)SvPVX (RETVAL);
383
384 memset (Er, 0, sizeof (int) * IW);
385 memset (Eg, 0, sizeof (int) * IW);
386 memset (Eb, 0, sizeof (int) * IW);
387
388 /* some primitive error distribution + random dithering */
389
390 for (y = 0; y < h; y++)
391 {
392 int er = 0, eg = 0, eb = 0;
393
394 for (x = 0; x < w; x++)
395 {
396 int r, g, b;
397 guchar *p = src + x * bpp + y * sstr;
398
399 r = ((p[0] + er + Er[x]) * 7 + (RAND & 127) + 64) / 255;
400 g = ((p[1] + eg + Eg[x]) * 7 + (RAND & 127) + 64) / 255;
401 b = ((p[2] + eb + Eb[x]) * 3 + (RAND & 127) + 64) / 255;
402
403 r = r > 7 ? 7 : r < 0 ? 0 : r;
404 g = g > 7 ? 7 : g < 0 ? 0 : g;
405 b = b > 3 ? 3 : b < 0 ? 0 : b;
406
407 er += p[0] - (r * 255 + 4) / 7;
408 eg += p[1] - (g * 255 + 4) / 7;
409 eb += p[2] - (b * 255 + 2) / 3;
410
411 Er[x] = er >> 1; er -= (er + 1) >> 1;
412 Eg[x] = eg >> 1; eg -= (eg + 1) >> 1;
413 Eb[x] = eb >> 1; eb -= (eb + 1) >> 1;
414
415 *dst++ = r << 5 | g << 2 | b;
416 }
417 }
418}
419 OUTPUT:
420 RETVAL
421
422#############################################################################
423
424MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
425
426void
427dump_ascii85 (PerlIO *fp, GdkPixbuf *pb)
428 CODE:
429{
430 int w = gdk_pixbuf_get_width (pb);
431 int h = gdk_pixbuf_get_height (pb);
432 int x, y, i;
433 guchar *dst;
434 int bpp = gdk_pixbuf_get_n_channels (pb);
435 guchar *src = gdk_pixbuf_get_pixels (pb);
436 int sstr = gdk_pixbuf_get_rowstride (pb);
437
438 a85_init ();
439
440 for (y = 0; y < h; y++)
441 for (x = 0; x < w; x++)
442 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
443 a85_push (fp, src [x * bpp + y * sstr + i]);
444
445 a85_finish (fp);
446}
447
448void
449dump_binary (PerlIO *fp, GdkPixbuf *pb)
450 CODE:
451{
452 int w = gdk_pixbuf_get_width (pb);
453 int h = gdk_pixbuf_get_height (pb);
454 int x, y, i;
455 guchar *dst;
456 int bpp = gdk_pixbuf_get_n_channels (pb);
457 guchar *src = gdk_pixbuf_get_pixels (pb);
458 int sstr = gdk_pixbuf_get_rowstride (pb);
459
460 for (y = 0; y < h; y++)
461 for (x = 0; x < w; x++)
462 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
463 PerlIO_putc (fp, src [x * bpp + y * sstr + i]);
464}
465
466#############################################################################
467
468MODULE = Gtk2::CV PACKAGE = Gtk2::CV
469
470SV *
471pb_to_hv84 (GdkPixbuf *pb)
472 CODE:
473{
474 int w = gdk_pixbuf_get_width (pb);
475 int h = gdk_pixbuf_get_height (pb);
476 int x, y;
477 guchar *dst;
478 int bpp = gdk_pixbuf_get_n_channels (pb);
479 guchar *src = gdk_pixbuf_get_pixels (pb);
480 int sstr = gdk_pixbuf_get_rowstride (pb);
481 512
482 RETVAL = newSV (6 * 8 * 12 / 8); 513 RETVAL = newSV (6 * 8 * 12 / 8);
483 SvPOK_only (RETVAL); 514 SvPOK_only (RETVAL);
484 SvCUR_set (RETVAL, 6 * 8 * 12 / 8); 515 SvCUR_set (RETVAL, 6 * 8 * 12 / 8);
485 516

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines