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.31 by root, Wed Feb 15 08:08:37 2006 UTC

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 7
8#include <magic.h>
9
8#include <jpeglib.h> 10#include <jpeglib.h>
11#include <glib.h>
12#include <gtk/gtk.h>
9#include <gdk-pixbuf/gdk-pixbuf.h> 13#include <gdk-pixbuf/gdk-pixbuf.h>
10 14
11#include <gperl.h> 15#include <gperl.h>
12#include <gtk2perl.h> 16#include <gtk2perl.h>
13 17
124 128
125MODULE = Gtk2::CV PACKAGE = Gtk2::CV 129MODULE = Gtk2::CV PACKAGE = Gtk2::CV
126 130
127PROTOTYPES: ENABLE 131PROTOTYPES: ENABLE
128 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_MIME);
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
129# missing in Gtk2 perl module 171# missing in Gtk2 perl module
130 172
131gboolean 173gboolean
132gdk_net_wm_supports (GdkAtom property) 174gdk_net_wm_supports (GdkAtom property)
133 CODE: 175 CODE:
138#endif 180#endif
139 OUTPUT: 181 OUTPUT:
140 RETVAL 182 RETVAL
141 183
142GdkPixbuf_noinc * 184GdkPixbuf_noinc *
143transpose (GdkPixbuf *pb) 185dealpha_expose (GdkPixbuf *pb)
144 CODE: 186 CODE:
145{ 187{
146 int w = gdk_pixbuf_get_width (pb); 188 int w = gdk_pixbuf_get_width (pb);
147 int h = gdk_pixbuf_get_height (pb); 189 int h = gdk_pixbuf_get_height (pb);
148 int bpp = gdk_pixbuf_get_n_channels (pb); 190 int bpp = gdk_pixbuf_get_n_channels (pb);
149 int x, y, i; 191 int x, y, i;
150 guchar *src = gdk_pixbuf_get_pixels (pb), *dst; 192 guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
151 int sstr = gdk_pixbuf_get_rowstride (pb), dstr; 193 int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
152 194
153 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, h, w); 195 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
154 196
155 dst = gdk_pixbuf_get_pixels (RETVAL); 197 dst = gdk_pixbuf_get_pixels (RETVAL);
156 dstr = gdk_pixbuf_get_rowstride (RETVAL); 198 dstr = gdk_pixbuf_get_rowstride (RETVAL);
157 199
158 for (y = 0; y < h; y++)
159 for (x = 0; x < w; x++) 200 for (x = 0; x < w; x++)
201 for (y = 0; y < h; y++)
160 for (i = 0; i < bpp; i++) 202 for (i = 0; i < 3; i++)
161 dst[y * bpp + x * dstr + i] = src[x * bpp + y * sstr + i]; 203 dst[x * 3 + y * dstr + i] = src[x * bpp + y * sstr + i];
162} 204}
163 OUTPUT: 205 OUTPUT:
164 RETVAL 206 RETVAL
165 207
166GdkPixbuf_noinc * 208GdkPixbuf_noinc *
167flop (GdkPixbuf *pb) 209rotate (GdkPixbuf *pb, int angle)
168 CODE: 210 CODE:
169{ 211 RETVAL = gdk_pixbuf_rotate_simple (pb, angle == 0 ? GDK_PIXBUF_ROTATE_NONE
170 int w = gdk_pixbuf_get_width (pb); 212 : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE
171 int h = gdk_pixbuf_get_height (pb); 213 : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN
172 int bpp = gdk_pixbuf_get_n_channels (pb); 214 : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE
173 int x, y, i; 215 : 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: 216 OUTPUT:
188 RETVAL 217 RETVAL
189 218
190GdkPixbuf_noinc * 219GdkPixbuf_noinc *
191load_jpeg (char *path, int thumbnail=0) 220load_jpeg (SV *path, int thumbnail=0)
192 CODE: 221 CODE:
193{ 222{
194 struct jpeg_decompress_struct cinfo; 223 struct jpeg_decompress_struct cinfo;
195 struct jpg_err_mgr jerr; 224 struct jpg_err_mgr jerr;
196 guchar *data; 225 guchar *data;
197 int rs; 226 int rs;
198 FILE *fp; 227 FILE *fp;
199 volatile GdkPixbuf *pb = 0; 228 volatile GdkPixbuf *pb = 0;
229
200 RETVAL = 0; 230 RETVAL = 0;
201 231
202 if (!(fp = fopen (path, "rb"))) 232 fp = fopen (SvPVbyte_nolen (path), "rb");
233
234 if (!fp)
203 XSRETURN_UNDEF; 235 XSRETURN_UNDEF;
204 236
205 cinfo.err = jpeg_std_error (&jerr.err); 237 cinfo.err = jpeg_std_error (&jerr.err);
206 238
207 jerr.err.error_exit = cv_error_exit; 239 jerr.err.error_exit = cv_error_exit;
288foldcase (SV *pathsv) 320foldcase (SV *pathsv)
289 PROTOTYPE: $ 321 PROTOTYPE: $
290 CODE: 322 CODE:
291{ 323{
292 STRLEN plen; 324 STRLEN plen;
293 U8 *path = SvPVutf8 (pathsv, plen); 325 U8 *path = (U8 *)SvPVutf8 (pathsv, plen);
294 U8 *pend = path + plen; 326 U8 *pend = path + plen;
295 U8 dst [plen * 6 * 3], *dstp = dst; 327 U8 dst [plen * 6 * 3], *dstp = dst;
296 328
297 while (path < pend) 329 while (path < pend)
298 { 330 {
319 dstp += cl; 351 dstp += cl;
320 path += is_utf8_char (path); 352 path += is_utf8_char (path);
321 } 353 }
322 } 354 }
323 355
324 RETVAL = newSVpvn (dst, dstp - dst); 356 RETVAL = newSVpvn ((const char *)dst, dstp - dst);
325} 357}
326 OUTPUT: 358 OUTPUT:
327 RETVAL 359 RETVAL
328 360
329GdkPixbuf_noinc * 361GdkPixbuf_noinc *
330p7_to_pb (int w, int h, guchar *src) 362p7_to_pb (int w, int h, SV *src_sv)
331 PROTOTYPE: @ 363 PROTOTYPE: @
332 CODE: 364 CODE:
333{ 365{
334 int x, y; 366 int x, y;
335 guchar *dst, *d; 367 guchar *dst, *d;
336 int dstr; 368 int dstr;
369 guchar *src = (guchar *)SvPVbyte_nolen (src_sv);
337 370
338 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h); 371 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
339 dst = gdk_pixbuf_get_pixels (RETVAL); 372 dst = gdk_pixbuf_get_pixels (RETVAL);
340 dstr = gdk_pixbuf_get_rowstride (RETVAL); 373 dstr = gdk_pixbuf_get_rowstride (RETVAL);
341 374
350 } 383 }
351} 384}
352 OUTPUT: 385 OUTPUT:
353 RETVAL 386 RETVAL
354 387
388#############################################################################
389
390MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
391
392void
393dump_ascii85 (PerlIO *fp, GdkPixbuf *pb)
394 CODE:
395{
396 int w = gdk_pixbuf_get_width (pb);
397 int h = gdk_pixbuf_get_height (pb);
398 int x, y, i;
399 guchar *dst;
400 int bpp = gdk_pixbuf_get_n_channels (pb);
401 guchar *src = gdk_pixbuf_get_pixels (pb);
402 int sstr = gdk_pixbuf_get_rowstride (pb);
403
404 a85_init ();
405
406 for (y = 0; y < h; y++)
407 for (x = 0; x < w; x++)
408 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
409 a85_push (fp, src [x * bpp + y * sstr + i]);
410
411 a85_finish (fp);
412}
413
414void
415dump_binary (PerlIO *fp, GdkPixbuf *pb)
416 CODE:
417{
418 int w = gdk_pixbuf_get_width (pb);
419 int h = gdk_pixbuf_get_height (pb);
420 int x, y, i;
421 guchar *dst;
422 int bpp = gdk_pixbuf_get_n_channels (pb);
423 guchar *src = gdk_pixbuf_get_pixels (pb);
424 int sstr = gdk_pixbuf_get_rowstride (pb);
425
426 for (y = 0; y < h; y++)
427 for (x = 0; x < w; x++)
428 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
429 PerlIO_putc (fp, src [x * bpp + y * sstr + i]);
430}
431
432#############################################################################
433
434MODULE = Gtk2::CV PACKAGE = Gtk2::CV
435
355SV * 436SV *
356pb_to_p7 (GdkPixbuf *pb) 437pb_to_hv84 (GdkPixbuf *pb)
357 CODE: 438 CODE:
358{ 439{
359 int w = gdk_pixbuf_get_width (pb); 440 int w = gdk_pixbuf_get_width (pb);
360 int h = gdk_pixbuf_get_height (pb); 441 int h = gdk_pixbuf_get_height (pb);
361 int x, y; 442 int x, y;
362 guchar *dst; 443 guchar *dst;
363 int bpp = gdk_pixbuf_get_n_channels (pb); 444 int bpp = gdk_pixbuf_get_n_channels (pb);
364 guchar *src = gdk_pixbuf_get_pixels (pb); 445 guchar *src = gdk_pixbuf_get_pixels (pb);
365 int sstr = gdk_pixbuf_get_rowstride (pb); 446 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 447
473 RETVAL = newSV (6 * 8 * 12 / 8); 448 RETVAL = newSV (6 * 8 * 12 / 8);
474 SvPOK_only (RETVAL); 449 SvPOK_only (RETVAL);
475 SvCUR_set (RETVAL, 6 * 8 * 12 / 8); 450 SvCUR_set (RETVAL, 6 * 8 * 12 / 8);
476 451
477 dst = SvPVX (RETVAL); 452 dst = (guchar *)SvPVX (RETVAL);
478 453
479 /* some primitive error distribution + random dithering */ 454 /* some primitive error distribution + random dithering */
480 455
481 for (y = 0; y < h; y++) 456 for (y = 0; y < h; y++)
482 { 457 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines