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

Comparing CV/CV.xs (file contents):
Revision 1.39 by root, Fri Jun 23 09:09:14 2006 UTC vs.
Revision 1.50 by root, Sat Dec 23 03:30:28 2017 UTC

9#include <magic.h> 9#include <magic.h>
10 10
11#include <jpeglib.h> 11#include <jpeglib.h>
12#include <glib.h> 12#include <glib.h>
13#include <gtk/gtk.h> 13#include <gtk/gtk.h>
14#include <gdk/gdkx.h>
14#include <gdk-pixbuf/gdk-pixbuf.h> 15#include <gdk-pixbuf/gdk-pixbuf.h>
15 16
16#include <gperl.h> 17#include <gperl.h>
17#include <gtk2perl.h> 18#include <gtk2perl.h>
18 19
20#include <assert.h>
21
22#if WEBP
23#include <webp/decode.h>
24#endif
25
26#include "perlmulticore.h"
27
19#define IW 80 /* MUST match Schnauzer.pm! */ 28#define IW 80 /* MUST match Schnauzer.pm! */
20#define IH 60 /* MUST match Schnauzer.pm! */ 29#define IH 60 /* MUST match Schnauzer.pm! */
21 30
22#define RAND (seed = (seed + 7141) * 54773 % 134456) 31#define RAND (seed = (seed + 7141) * 54773 % 134456)
23 32
24#define LINELENGTH 240 33#define LINELENGTH 240
25 34
26#define ELLIPSIS "\xe2\x80\xa6" 35#define ELLIPSIS "\xe2\x80\xa6"
36
37typedef char *octet_string;
27 38
28struct jpg_err_mgr 39struct jpg_err_mgr
29{ 40{
30 struct jpeg_error_mgr err; 41 struct jpeg_error_mgr err;
31 jmp_buf setjmp_buffer; 42 jmp_buf setjmp_buffer;
32}; 43};
33 44
34static void 45static void
35cv_error_exit (j_common_ptr cinfo) 46cv_error_exit (j_common_ptr cinfo)
36{ 47{
48 cinfo->err->output_message (cinfo);
37 longjmp (((struct jpg_err_mgr *)cinfo->err)->setjmp_buffer, 99); 49 longjmp (((struct jpg_err_mgr *)cinfo->err)->setjmp_buffer, 99);
38} 50}
39 51
40static void 52static void
41cv_error_output (j_common_ptr cinfo) 53cv_error_output (j_common_ptr cinfo)
42{ 54{
55 char msg[JMSG_LENGTH_MAX];
56
57 cinfo->err->format_message (cinfo, msg);
58
59 fprintf (stderr, "JPEG decoding error: %s\n", msg);
43 return; 60 return;
44} 61}
45 62
46static void 63static void
47rgb_to_hsv (unsigned int r, unsigned int g, unsigned int b, 64rgb_to_hsv (unsigned int r, unsigned int g, unsigned int b,
144 if (!--a85_cnt) 161 if (!--a85_cnt)
145 { 162 {
146 a85_cnt = 4; 163 a85_cnt = 4;
147 if (a85_val) 164 if (a85_val)
148 { 165 {
149 a85_ptr[4] = (a85_val % 85) + 33; a85_val /= 85; 166 a85_ptr[4] = (a85_val % 85) + 33; a85_val /= 85;
150 a85_ptr[3] = (a85_val % 85) + 33; a85_val /= 85; 167 a85_ptr[3] = (a85_val % 85) + 33; a85_val /= 85;
151 a85_ptr[2] = (a85_val % 85) + 33; a85_val /= 85; 168 a85_ptr[2] = (a85_val % 85) + 33; a85_val /= 85;
152 a85_ptr[1] = (a85_val % 85) + 33; a85_val /= 85; 169 a85_ptr[1] = (a85_val % 85) + 33; a85_val /= 85;
153 a85_ptr[0] = (a85_val ) + 33; 170 a85_ptr[0] = (a85_val ) + 33;
154 171
155 a85_ptr += 5; 172 a85_ptr += 5;
156 } 173 }
202 219
203 OUTPUT: 220 OUTPUT:
204 RETVAL 221 RETVAL
205 222
206const char * 223const char *
207magic (const char *path) 224magic (octet_string path)
208 CODE: 225 CODE:
209{ 226{
210 static magic_t cookie; 227 static magic_t cookie;
211 228
212 if (!cookie) 229 if (!cookie)
213 { 230 {
214 cookie = magic_open (MAGIC_NONE); 231 cookie = magic_open (MAGIC_SYMLINK);
215 232
216 if (cookie) 233 if (cookie)
217 magic_load (cookie, 0); 234 magic_load (cookie, 0);
218 else 235 else
219 XSRETURN_UNDEF; 236 XSRETURN_UNDEF;
223} 240}
224 OUTPUT: 241 OUTPUT:
225 RETVAL 242 RETVAL
226 243
227const char * 244const char *
228magic_mime (const char *path) 245magic_mime (octet_string path)
229 CODE: 246 CODE:
230{ 247{
231 static magic_t cookie; 248 static magic_t cookie;
232 249
233 if (!cookie) 250 if (!cookie)
234 { 251 {
235 cookie = magic_open (MAGIC_MIME); 252 cookie = magic_open (MAGIC_MIME | MAGIC_SYMLINK);
236 253
237 if (cookie) 254 if (cookie)
238 magic_load (cookie, 0); 255 magic_load (cookie, 0);
239 else 256 else
240 XSRETURN_UNDEF; 257 XSRETURN_UNDEF;
241 } 258 }
242 259
260 perlinterp_release ();
243 RETVAL = magic_file (cookie, path); 261 RETVAL = magic_file (cookie, path);
262 perlinterp_acquire ();
244} 263}
245 OUTPUT: 264 OUTPUT:
246 RETVAL 265 RETVAL
247 266
248# missing in Gtk2 perl module 267# missing/broken in Gtk2 perl module
268
269void
270gdk_window_clear_hints (GdkWindow *window)
271 CODE:
272 gdk_window_set_geometry_hints (window, 0, 0);
249 273
250gboolean 274gboolean
251gdk_net_wm_supports (GdkAtom property) 275gdk_net_wm_supports (GdkAtom property)
252 CODE: 276 CODE:
253#if defined(GDK_WINDOWING_X11) && !defined(GDK_MULTIHEAD_SAFE) 277#if defined(GDK_WINDOWING_X11) && !defined(GDK_MULTIHEAD_SAFE)
259 RETVAL 283 RETVAL
260 284
261GdkPixbuf_noinc * 285GdkPixbuf_noinc *
262dealpha_expose (GdkPixbuf *pb) 286dealpha_expose (GdkPixbuf *pb)
263 CODE: 287 CODE:
288 perlinterp_release ();
264{ 289{
265 int w = gdk_pixbuf_get_width (pb); 290 int w = gdk_pixbuf_get_width (pb);
266 int h = gdk_pixbuf_get_height (pb); 291 int h = gdk_pixbuf_get_height (pb);
267 int bpp = gdk_pixbuf_get_n_channels (pb); 292 int bpp = gdk_pixbuf_get_n_channels (pb);
268 int x, y, i; 293 int x, y, i;
277 for (x = 0; x < w; x++) 302 for (x = 0; x < w; x++)
278 for (y = 0; y < h; y++) 303 for (y = 0; y < h; y++)
279 for (i = 0; i < 3; i++) 304 for (i = 0; i < 3; i++)
280 dst[x * 3 + y * dstr + i] = src[x * bpp + y * sstr + i]; 305 dst[x * 3 + y * dstr + i] = src[x * bpp + y * sstr + i];
281} 306}
307 perlinterp_acquire ();
282 OUTPUT: 308 OUTPUT:
283 RETVAL 309 RETVAL
284 310
285GdkPixbuf_noinc * 311GdkPixbuf_noinc *
286rotate (GdkPixbuf *pb, int angle) 312rotate (GdkPixbuf *pb, int angle)
287 CODE: 313 CODE:
314 perlinterp_release ();
315 if (angle < 0)
316 angle += 360;
288 RETVAL = gdk_pixbuf_rotate_simple (pb, angle == 0 ? GDK_PIXBUF_ROTATE_NONE 317 RETVAL = gdk_pixbuf_rotate_simple (pb, angle == 0 ? GDK_PIXBUF_ROTATE_NONE
289 : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE 318 : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE
290 : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN 319 : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN
291 : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE 320 : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE
292 : angle); 321 : angle);
322 perlinterp_acquire ();
293 OUTPUT: 323 OUTPUT:
294 RETVAL 324 RETVAL
295 325
296GdkPixbuf_noinc * 326GdkPixbuf_noinc *
297load_jpeg (SV *path, int thumbnail=0) 327load_webp (SV *image_data, int thumbnail = 0, int iw = 0, int ih = 0)
328 CODE:
329{
330#if WEBP
331 STRLEN data_size;
332 guchar *data = SvPVbyte (image_data, data_size);
333 int alpha;
334 WebPDecoderConfig config;
335
336 perlinterp_release ();
337
338 RETVAL = 0;
339
340 if (!WebPInitDecoderConfig (&config))
341 goto err;
342
343 config.options.use_threads = 1;
344
345 if (thumbnail)
346 {
347 config.options.bypass_filtering = 1;
348 config.options.no_fancy_upsampling = 1;
349
350 config.options.use_scaling = 1;
351 config.options.scaled_width = iw;
352 config.options.scaled_height = ih;
353 }
354 else
355 {
356 iw = config.input.width;
357 ih = config.input.height;
358 }
359
360 if (WebPGetFeatures (data, data_size, &config.input) != VP8_STATUS_OK)
361 goto err;
362
363 alpha = config.input.has_alpha;
364
365 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, !!alpha, 8, iw, ih);
366 if (!RETVAL)
367 goto err;
368
369 config.output.colorspace = alpha ? MODE_RGBA : MODE_RGB;
370 config.output.u.RGBA.rgba = gdk_pixbuf_get_pixels (RETVAL);
371 config.output.u.RGBA.stride = gdk_pixbuf_get_rowstride (RETVAL);
372 config.output.u.RGBA.size = gdk_pixbuf_get_byte_length (RETVAL);
373 config.output.is_external_memory = 1;
374
375 if (WebPDecode (data, data_size, &config) != VP8_STATUS_OK)
376 {
377 g_object_unref (RETVAL);
378 RETVAL = 0;
379 goto err;
380 }
381
382 err:
383 perlinterp_acquire ();
384#else
385 croak ("load_webp: webp not enabled at compile time");
386#endif
387}
388 OUTPUT:
389 RETVAL
390
391GdkPixbuf_noinc *
392load_jpeg (SV *path, int thumbnail = 0, int iw = 0, int ih = 0)
298 CODE: 393 CODE:
299{ 394{
300 struct jpeg_decompress_struct cinfo; 395 struct jpeg_decompress_struct cinfo;
301 struct jpg_err_mgr jerr; 396 struct jpg_err_mgr jerr;
302 guchar *data; 397 guchar *data;
309 fp = fopen (SvPVbyte_nolen (path), "rb"); 404 fp = fopen (SvPVbyte_nolen (path), "rb");
310 405
311 if (!fp) 406 if (!fp)
312 XSRETURN_UNDEF; 407 XSRETURN_UNDEF;
313 408
409 perlinterp_release ();
410
314 cinfo.err = jpeg_std_error (&jerr.err); 411 cinfo.err = jpeg_std_error (&jerr.err);
315 412
316 jerr.err.error_exit = cv_error_exit; 413 jerr.err.error_exit = cv_error_exit;
317 jerr.err.output_message = cv_error_output; 414 jerr.err.output_message = cv_error_output;
318 415
322 jpeg_destroy_decompress (&cinfo); 419 jpeg_destroy_decompress (&cinfo);
323 420
324 if (pb) 421 if (pb)
325 g_object_unref ((gpointer)pb); 422 g_object_unref ((gpointer)pb);
326 423
424 perlinterp_acquire ();
327 XSRETURN_UNDEF; 425 XSRETURN_UNDEF;
328 } 426 }
329 427
330 jpeg_create_decompress (&cinfo); 428 jpeg_create_decompress (&cinfo);
331 429
347 { 445 {
348 cinfo.dct_method = JDCT_FASTEST; 446 cinfo.dct_method = JDCT_FASTEST;
349 cinfo.do_fancy_upsampling = FALSE; 447 cinfo.do_fancy_upsampling = FALSE;
350 448
351 while (cinfo.scale_denom < 8 449 while (cinfo.scale_denom < 8
352 && cinfo.output_width >= IW*4 450 && cinfo.output_width >= iw*4
353 && cinfo.output_height >= IH*4) 451 && cinfo.output_height >= ih*4)
354 { 452 {
355 cinfo.scale_denom <<= 1; 453 cinfo.scale_denom <<= 1;
356 jpeg_calc_output_dimensions (&cinfo); 454 jpeg_calc_output_dimensions (&cinfo);
357 } 455 }
358 } 456 }
359 457
458 if (cinfo.output_components != 3)
459 longjmp (jerr.setjmp_buffer, 3);
460
461 if (cinfo.jpeg_color_space == JCS_YCCK || cinfo.jpeg_color_space == JCS_CMYK)
462 {
463 cinfo.out_color_space = JCS_CMYK;
464 cinfo.output_components = 4;
465 }
466
360 pb = RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, cinfo.output_width, cinfo.output_height); 467 pb = RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, cinfo.output_components == 4, 8, cinfo.output_width, cinfo.output_height);
361 if (!RETVAL) 468 if (!RETVAL)
362 longjmp (jerr.setjmp_buffer, 2); 469 longjmp (jerr.setjmp_buffer, 2);
363 470
364 data = gdk_pixbuf_get_pixels (RETVAL); 471 data = gdk_pixbuf_get_pixels (RETVAL);
365 rs = gdk_pixbuf_get_rowstride (RETVAL); 472 rs = gdk_pixbuf_get_rowstride (RETVAL);
366
367 if (cinfo.output_components != 3)
368 longjmp (jerr.setjmp_buffer, 3);
369 473
370 jpeg_start_decompress (&cinfo); 474 jpeg_start_decompress (&cinfo);
371 475
372 while (cinfo.output_scanline < cinfo.output_height) 476 while (cinfo.output_scanline < cinfo.output_height)
373 { 477 {
380 rp [3] = (guchar *)rp [2] + rs; 484 rp [3] = (guchar *)rp [2] + rs;
381 485
382 jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4); 486 jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4);
383 } 487 }
384 488
489 if (cinfo.out_color_space == JCS_CMYK)
490 {
491 guchar *end = data + cinfo.output_height * rs;
492
493 while (data < end)
494 {
495 U32 c = data [0];
496 U32 m = data [1];
497 U32 y = data [2];
498 U32 k = data [3];
499
500 if (0)
501 if (cinfo.Adobe_transform == 2)
502 {
503 c ^= 0xff;
504 m ^= 0xff;
505 y ^= 0xff;
506 k ^= 0xff;
507 }
508
509 data [0] = (c * k + 0x80) / 0xff;
510 data [1] = (m * k + 0x80) / 0xff;
511 data [2] = (y * k + 0x80) / 0xff;
512 data [3] = 0xff;
513
514 data += 4;
515 }
516 }
517
385 jpeg_finish_decompress (&cinfo); 518 jpeg_finish_decompress (&cinfo);
386 fclose (fp); 519 fclose (fp);
387 jpeg_destroy_decompress (&cinfo); 520 jpeg_destroy_decompress (&cinfo);
521 perlinterp_acquire ();
388} 522}
389 OUTPUT: 523 OUTPUT:
390 RETVAL 524 RETVAL
391 525
392void 526void
393compare (GdkPixbuf *a, GdkPixbuf *b) 527compare (GdkPixbuf *a, GdkPixbuf *b)
394 PPCODE: 528 PPCODE:
529 perlinterp_release ();
395{ 530{
396 int w = gdk_pixbuf_get_width (a); 531 int w = gdk_pixbuf_get_width (a);
397 int h = gdk_pixbuf_get_height (a); 532 int h = gdk_pixbuf_get_height (a);
533
398 int sa = gdk_pixbuf_get_rowstride (a); 534 int sa = gdk_pixbuf_get_rowstride (a);
399 int sb = gdk_pixbuf_get_rowstride (b); 535 int sb = gdk_pixbuf_get_rowstride (b);
400 536
401 guchar *pa = gdk_pixbuf_get_pixels (a); 537 guchar *pa = gdk_pixbuf_get_pixels (a);
402 guchar *pb = gdk_pixbuf_get_pixels (b); 538 guchar *pb = gdk_pixbuf_get_pixels (b);
426 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d)); 562 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
427 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d)); 563 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
428 } 564 }
429 } 565 }
430 566
567 perlinterp_acquire ();
568
431 EXTEND (SP, 2); 569 EXTEND (SP, 2);
432 PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.))))); 570 PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.)))));
433 PUSHs (sv_2mortal (newSVnv (peak / 255.))); 571 PUSHs (sv_2mortal (newSVnv (peak / 255.)));
434} 572}
435 573
436############################################################################# 574#############################################################################
437 575
438MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer 576MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer
439 577
578# currently only works for filenames (octet strings)
579
440SV * 580SV *
441foldcase (SV *pathsv) 581foldcase (SV *pathsv)
442 PROTOTYPE: $ 582 PROTOTYPE: $
443 CODE: 583 CODE:
444{ 584{
445 STRLEN plen; 585 STRLEN plen;
446 U8 *path = (U8 *)SvPVutf8 (pathsv, plen); 586 U8 *path = (U8 *)SvPV (pathsv, plen);
447 U8 *pend = path + plen; 587 U8 *pend = path + plen;
448 U8 dst [plen * 6 * 3], *dstp = dst; 588 U8 dst [plen * 6 * 3], *dstp = dst;
449 589
450 while (path < pend) 590 while (path < pend)
451 { 591 {
452 U8 ch = *path; 592 U8 ch = *path;
453 593
454 if (ch >= 'a' && ch <= 'z') 594 if (ch >= 'a' && ch <= 'z')
455 *dstp++ = *path++; 595 *dstp++ = *path++;
596 else if (ch >= 'A' && ch <= 'Z')
597 *dstp++ = *path++ + ('a' - 'A');
456 else if (ch >= '0' && ch <= '9') 598 else if (ch >= '0' && ch <= '9')
457 { 599 {
458 STRLEN el, nl = 0; 600 STRLEN el, nl = 0;
459 while (*path >= '0' && *path <= '9' && path < pend) 601 while (*path >= '0' && *path <= '9' && path < pend)
460 path++, nl++; 602 path++, nl++;
463 *dstp++ = '0'; 605 *dstp++ = '0';
464 606
465 memcpy (dstp, path - nl, nl); 607 memcpy (dstp, path - nl, nl);
466 dstp += nl; 608 dstp += nl;
467 } 609 }
610 else
611 *dstp++ = *path++;
612#if 0
468 else 613 else
469 { 614 {
470 STRLEN cl; 615 STRLEN cl;
471 to_utf8_fold (path, dstp, &cl); 616 to_utf8_fold (path, dstp, &cl);
472 dstp += cl; 617 dstp += cl;
473 path += is_utf8_char (path); 618 path += is_utf8_char (path);
474 } 619 }
620#endif
475 } 621 }
476 622
477 RETVAL = newSVpvn ((const char *)dst, dstp - dst); 623 RETVAL = newSVpvn ((const char *)dst, dstp - dst);
478} 624}
479 OUTPUT: 625 OUTPUT:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines