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

Comparing CV/CV.xs (file contents):
Revision 1.23 by root, Tue Aug 16 23:50:39 2005 UTC vs.
Revision 1.32 by root, Wed Feb 15 08:36:55 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
8#include <magic.h>
7 9
8#include <jpeglib.h> 10#include <jpeglib.h>
9#include <glib.h> 11#include <glib.h>
10#include <gtk/gtk.h> 12#include <gtk/gtk.h>
11#include <gdk-pixbuf/gdk-pixbuf.h> 13#include <gdk-pixbuf/gdk-pixbuf.h>
126 128
127MODULE = Gtk2::CV PACKAGE = Gtk2::CV 129MODULE = Gtk2::CV PACKAGE = Gtk2::CV
128 130
129PROTOTYPES: ENABLE 131PROTOTYPES: ENABLE
130 132
133# missing function in perl. really :)
134int
135common_prefix_length (a, b)
136 unsigned char *a = (unsigned char *)SvPVutf8_nolen ($arg);
137 unsigned char *b = (unsigned char *)SvPVutf8_nolen ($arg);
138 CODE:
139 RETVAL = 0;
140
141 while (*a == *b && *a)
142 {
143 RETVAL += (*a & 0xc0) != 0x80;
144 a++, b++;
145 }
146
147 OUTPUT:
148 RETVAL
149
150const char *
151magic (const char *path)
152 CODE:
153{
154 static magic_t cookie;
155
156 if (!cookie)
157 {
158 cookie = magic_open (MAGIC_NONE);
159
160 if (cookie)
161 magic_load (cookie, 0);
162 else
163 XSRETURN_UNDEF;
164 }
165
166 RETVAL = magic_file (cookie, path);
167}
168 OUTPUT:
169 RETVAL
170
171const char *
172magic_mime (const char *path)
173 CODE:
174{
175 static magic_t cookie;
176
177 if (!cookie)
178 {
179 cookie = magic_open (MAGIC_MIME);
180
181 if (cookie)
182 magic_load (cookie, 0);
183 else
184 XSRETURN_UNDEF;
185 }
186
187 RETVAL = magic_file (cookie, path);
188}
189 OUTPUT:
190 RETVAL
191
131# missing in Gtk2 perl module 192# missing in Gtk2 perl module
132 193
133gboolean 194gboolean
134gdk_net_wm_supports (GdkAtom property) 195gdk_net_wm_supports (GdkAtom property)
135 CODE: 196 CODE:
140#endif 201#endif
141 OUTPUT: 202 OUTPUT:
142 RETVAL 203 RETVAL
143 204
144GdkPixbuf_noinc * 205GdkPixbuf_noinc *
145transpose (GdkPixbuf *pb) 206dealpha_expose (GdkPixbuf *pb)
146 CODE: 207 CODE:
147{ 208{
148 int w = gdk_pixbuf_get_width (pb); 209 int w = gdk_pixbuf_get_width (pb);
149 int h = gdk_pixbuf_get_height (pb); 210 int h = gdk_pixbuf_get_height (pb);
150 int bpp = gdk_pixbuf_get_n_channels (pb); 211 int bpp = gdk_pixbuf_get_n_channels (pb);
151 int x, y, i; 212 int x, y, i;
152 guchar *src = gdk_pixbuf_get_pixels (pb), *dst; 213 guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
153 int sstr = gdk_pixbuf_get_rowstride (pb), dstr; 214 int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
154 215
155 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, h, w); 216 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
156 217
157 dst = gdk_pixbuf_get_pixels (RETVAL); 218 dst = gdk_pixbuf_get_pixels (RETVAL);
158 dstr = gdk_pixbuf_get_rowstride (RETVAL); 219 dstr = gdk_pixbuf_get_rowstride (RETVAL);
159 220
160 for (y = 0; y < h; y++)
161 for (x = 0; x < w; x++) 221 for (x = 0; x < w; x++)
222 for (y = 0; y < h; y++)
162 for (i = 0; i < bpp; i++) 223 for (i = 0; i < 3; i++)
163 dst[y * bpp + x * dstr + i] = src[x * bpp + y * sstr + i]; 224 dst[x * 3 + y * dstr + i] = src[x * bpp + y * sstr + i];
164} 225}
165 OUTPUT: 226 OUTPUT:
166 RETVAL 227 RETVAL
167 228
168GdkPixbuf_noinc * 229GdkPixbuf_noinc *
169flop (GdkPixbuf *pb) 230rotate (GdkPixbuf *pb, int angle)
170 CODE: 231 CODE:
171{ 232 RETVAL = gdk_pixbuf_rotate_simple (pb, angle == 0 ? GDK_PIXBUF_ROTATE_NONE
172 int w = gdk_pixbuf_get_width (pb); 233 : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE
173 int h = gdk_pixbuf_get_height (pb); 234 : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN
174 int bpp = gdk_pixbuf_get_n_channels (pb); 235 : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE
175 int x, y, i; 236 : 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: 237 OUTPUT:
190 RETVAL 238 RETVAL
191 239
192GdkPixbuf_noinc * 240GdkPixbuf_noinc *
193load_jpeg (SV *path, int thumbnail=0) 241load_jpeg (SV *path, int thumbnail=0)
197 struct jpg_err_mgr jerr; 245 struct jpg_err_mgr jerr;
198 guchar *data; 246 guchar *data;
199 int rs; 247 int rs;
200 FILE *fp; 248 FILE *fp;
201 volatile GdkPixbuf *pb = 0; 249 volatile GdkPixbuf *pb = 0;
202 gchar *filename;
203 250
204 RETVAL = 0; 251 RETVAL = 0;
205 252
206 filename = g_filename_from_utf8 (SvPVutf8_nolen (path), -1, 0, 0, 0);
207 fp = fopen (filename, "rb"); 253 fp = fopen (SvPVbyte_nolen (path), "rb");
208 g_free (filename);
209 254
210 if (!fp) 255 if (!fp)
211 XSRETURN_UNDEF; 256 XSRETURN_UNDEF;
212 257
213 cinfo.err = jpeg_std_error (&jerr.err); 258 cinfo.err = jpeg_std_error (&jerr.err);
296foldcase (SV *pathsv) 341foldcase (SV *pathsv)
297 PROTOTYPE: $ 342 PROTOTYPE: $
298 CODE: 343 CODE:
299{ 344{
300 STRLEN plen; 345 STRLEN plen;
301 U8 *path = SvPVutf8 (pathsv, plen); 346 U8 *path = (U8 *)SvPVutf8 (pathsv, plen);
302 U8 *pend = path + plen; 347 U8 *pend = path + plen;
303 U8 dst [plen * 6 * 3], *dstp = dst; 348 U8 dst [plen * 6 * 3], *dstp = dst;
304 349
305 while (path < pend) 350 while (path < pend)
306 { 351 {
327 dstp += cl; 372 dstp += cl;
328 path += is_utf8_char (path); 373 path += is_utf8_char (path);
329 } 374 }
330 } 375 }
331 376
332 RETVAL = newSVpvn (dst, dstp - dst); 377 RETVAL = newSVpvn ((const char *)dst, dstp - dst);
333} 378}
334 OUTPUT: 379 OUTPUT:
335 RETVAL 380 RETVAL
336 381
337GdkPixbuf_noinc * 382GdkPixbuf_noinc *
338p7_to_pb (int w, int h, guchar *src) 383p7_to_pb (int w, int h, SV *src_sv)
339 PROTOTYPE: @ 384 PROTOTYPE: @
340 CODE: 385 CODE:
341{ 386{
342 int x, y; 387 int x, y;
343 guchar *dst, *d; 388 guchar *dst, *d;
344 int dstr; 389 int dstr;
390 guchar *src = (guchar *)SvPVbyte_nolen (src_sv);
345 391
346 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h); 392 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
347 dst = gdk_pixbuf_get_pixels (RETVAL); 393 dst = gdk_pixbuf_get_pixels (RETVAL);
348 dstr = gdk_pixbuf_get_rowstride (RETVAL); 394 dstr = gdk_pixbuf_get_rowstride (RETVAL);
349 395
358 } 404 }
359} 405}
360 OUTPUT: 406 OUTPUT:
361 RETVAL 407 RETVAL
362 408
409#############################################################################
410
411MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
412
413void
414dump_ascii85 (PerlIO *fp, GdkPixbuf *pb)
415 CODE:
416{
417 int w = gdk_pixbuf_get_width (pb);
418 int h = gdk_pixbuf_get_height (pb);
419 int x, y, i;
420 guchar *dst;
421 int bpp = gdk_pixbuf_get_n_channels (pb);
422 guchar *src = gdk_pixbuf_get_pixels (pb);
423 int sstr = gdk_pixbuf_get_rowstride (pb);
424
425 a85_init ();
426
427 for (y = 0; y < h; y++)
428 for (x = 0; x < w; x++)
429 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
430 a85_push (fp, src [x * bpp + y * sstr + i]);
431
432 a85_finish (fp);
433}
434
435void
436dump_binary (PerlIO *fp, GdkPixbuf *pb)
437 CODE:
438{
439 int w = gdk_pixbuf_get_width (pb);
440 int h = gdk_pixbuf_get_height (pb);
441 int x, y, i;
442 guchar *dst;
443 int bpp = gdk_pixbuf_get_n_channels (pb);
444 guchar *src = gdk_pixbuf_get_pixels (pb);
445 int sstr = gdk_pixbuf_get_rowstride (pb);
446
447 for (y = 0; y < h; y++)
448 for (x = 0; x < w; x++)
449 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
450 PerlIO_putc (fp, src [x * bpp + y * sstr + i]);
451}
452
453#############################################################################
454
455MODULE = Gtk2::CV PACKAGE = Gtk2::CV
456
363SV * 457SV *
364pb_to_p7 (GdkPixbuf *pb) 458pb_to_hv84 (GdkPixbuf *pb)
365 CODE: 459 CODE:
366{ 460{
367 int w = gdk_pixbuf_get_width (pb); 461 int w = gdk_pixbuf_get_width (pb);
368 int h = gdk_pixbuf_get_height (pb); 462 int h = gdk_pixbuf_get_height (pb);
369 int x, y; 463 int x, y;
370 guchar *dst; 464 guchar *dst;
371 int bpp = gdk_pixbuf_get_n_channels (pb); 465 int bpp = gdk_pixbuf_get_n_channels (pb);
372 guchar *src = gdk_pixbuf_get_pixels (pb); 466 guchar *src = gdk_pixbuf_get_pixels (pb);
373 int sstr = gdk_pixbuf_get_rowstride (pb); 467 int sstr = gdk_pixbuf_get_rowstride (pb);
374 int Er[IW], Eg[IW], Eb[IW];
375 int seed = 77;
376
377 RETVAL = newSV (w * h);
378 SvPOK_only (RETVAL);
379 SvCUR_set (RETVAL, w * h);
380
381 dst = SvPVX (RETVAL);
382
383 memset (Er, 0, sizeof (int) * IW);
384 memset (Eg, 0, sizeof (int) * IW);
385 memset (Eb, 0, sizeof (int) * IW);
386
387 /* some primitive error distribution + random dithering */
388
389 for (y = 0; y < h; y++)
390 {
391 int er = 0, eg = 0, eb = 0;
392
393 for (x = 0; x < w; x++)
394 {
395 int r, g, b;
396 guchar *p = src + x * bpp + y * sstr;
397
398 r = ((p[0] + er + Er[x]) * 7 + (RAND & 127) + 64) / 255;
399 g = ((p[1] + eg + Eg[x]) * 7 + (RAND & 127) + 64) / 255;
400 b = ((p[2] + eb + Eb[x]) * 3 + (RAND & 127) + 64) / 255;
401
402 r = r > 7 ? 7 : r < 0 ? 0 : r;
403 g = g > 7 ? 7 : g < 0 ? 0 : g;
404 b = b > 3 ? 3 : b < 0 ? 0 : b;
405
406 er += p[0] - (r * 255 + 4) / 7;
407 eg += p[1] - (g * 255 + 4) / 7;
408 eb += p[2] - (b * 255 + 2) / 3;
409
410 Er[x] = er >> 1; er -= (er + 1) >> 1;
411 Eg[x] = eg >> 1; eg -= (eg + 1) >> 1;
412 Eb[x] = eb >> 1; eb -= (eb + 1) >> 1;
413
414 *dst++ = r << 5 | g << 2 | b;
415 }
416 }
417}
418 OUTPUT:
419 RETVAL
420
421#############################################################################
422
423MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
424
425void
426dump_ascii85 (PerlIO *fp, GdkPixbuf *pb)
427 CODE:
428{
429 int w = gdk_pixbuf_get_width (pb);
430 int h = gdk_pixbuf_get_height (pb);
431 int x, y, i;
432 guchar *dst;
433 int bpp = gdk_pixbuf_get_n_channels (pb);
434 guchar *src = gdk_pixbuf_get_pixels (pb);
435 int sstr = gdk_pixbuf_get_rowstride (pb);
436
437 a85_init ();
438
439 for (y = 0; y < h; y++)
440 for (x = 0; x < w; x++)
441 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
442 a85_push (fp, src [x * bpp + y * sstr + i]);
443
444 a85_finish (fp);
445}
446
447void
448dump_binary (PerlIO *fp, GdkPixbuf *pb)
449 CODE:
450{
451 int w = gdk_pixbuf_get_width (pb);
452 int h = gdk_pixbuf_get_height (pb);
453 int x, y, i;
454 guchar *dst;
455 int bpp = gdk_pixbuf_get_n_channels (pb);
456 guchar *src = gdk_pixbuf_get_pixels (pb);
457 int sstr = gdk_pixbuf_get_rowstride (pb);
458
459 for (y = 0; y < h; y++)
460 for (x = 0; x < w; x++)
461 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
462 PerlIO_putc (fp, src [x * bpp + y * sstr + i]);
463}
464
465#############################################################################
466
467MODULE = Gtk2::CV PACKAGE = Gtk2::CV
468
469SV *
470pb_to_hv84 (GdkPixbuf *pb)
471 CODE:
472{
473 int w = gdk_pixbuf_get_width (pb);
474 int h = gdk_pixbuf_get_height (pb);
475 int x, y;
476 guchar *dst;
477 int bpp = gdk_pixbuf_get_n_channels (pb);
478 guchar *src = gdk_pixbuf_get_pixels (pb);
479 int sstr = gdk_pixbuf_get_rowstride (pb);
480 468
481 RETVAL = newSV (6 * 8 * 12 / 8); 469 RETVAL = newSV (6 * 8 * 12 / 8);
482 SvPOK_only (RETVAL); 470 SvPOK_only (RETVAL);
483 SvCUR_set (RETVAL, 6 * 8 * 12 / 8); 471 SvCUR_set (RETVAL, 6 * 8 * 12 / 8);
484 472
485 dst = SvPVX (RETVAL); 473 dst = (guchar *)SvPVX (RETVAL);
486 474
487 /* some primitive error distribution + random dithering */ 475 /* some primitive error distribution + random dithering */
488 476
489 for (y = 0; y < h; y++) 477 for (y = 0; y < h; y++)
490 { 478 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines