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

Comparing CV/CV.xs (file contents):
Revision 1.45 by root, Fri Aug 29 18:35:25 2014 UTC vs.
Revision 1.49 by root, Wed May 24 19:22:32 2017 UTC

16#include <gperl.h> 16#include <gperl.h>
17#include <gtk2perl.h> 17#include <gtk2perl.h>
18 18
19#include <assert.h> 19#include <assert.h>
20 20
21#include "perlmulticore.h"
22
21#define IW 80 /* MUST match Schnauzer.pm! */ 23#define IW 80 /* MUST match Schnauzer.pm! */
22#define IH 60 /* MUST match Schnauzer.pm! */ 24#define IH 60 /* MUST match Schnauzer.pm! */
23 25
24#define RAND (seed = (seed + 7141) * 54773 % 134456) 26#define RAND (seed = (seed + 7141) * 54773 % 134456)
25 27
36}; 38};
37 39
38static void 40static void
39cv_error_exit (j_common_ptr cinfo) 41cv_error_exit (j_common_ptr cinfo)
40{ 42{
43 cinfo->err->output_message (cinfo);
41 longjmp (((struct jpg_err_mgr *)cinfo->err)->setjmp_buffer, 99); 44 longjmp (((struct jpg_err_mgr *)cinfo->err)->setjmp_buffer, 99);
42} 45}
43 46
44static void 47static void
45cv_error_output (j_common_ptr cinfo) 48cv_error_output (j_common_ptr cinfo)
46{ 49{
50 char msg[JMSG_LENGTH_MAX];
51
52 cinfo->err->format_message (cinfo, msg);
53
54 fprintf (stderr, "JPEG decoding error: %s\n", msg);
47 return; 55 return;
48} 56}
49 57
50static void 58static void
51rgb_to_hsv (unsigned int r, unsigned int g, unsigned int b, 59rgb_to_hsv (unsigned int r, unsigned int g, unsigned int b,
148 if (!--a85_cnt) 156 if (!--a85_cnt)
149 { 157 {
150 a85_cnt = 4; 158 a85_cnt = 4;
151 if (a85_val) 159 if (a85_val)
152 { 160 {
153 a85_ptr[4] = (a85_val % 85) + 33; a85_val /= 85; 161 a85_ptr[4] = (a85_val % 85) + 33; a85_val /= 85;
154 a85_ptr[3] = (a85_val % 85) + 33; a85_val /= 85; 162 a85_ptr[3] = (a85_val % 85) + 33; a85_val /= 85;
155 a85_ptr[2] = (a85_val % 85) + 33; a85_val /= 85; 163 a85_ptr[2] = (a85_val % 85) + 33; a85_val /= 85;
156 a85_ptr[1] = (a85_val % 85) + 33; a85_val /= 85; 164 a85_ptr[1] = (a85_val % 85) + 33; a85_val /= 85;
157 a85_ptr[0] = (a85_val ) + 33; 165 a85_ptr[0] = (a85_val ) + 33;
158 166
159 a85_ptr += 5; 167 a85_ptr += 5;
160 } 168 }
242 magic_load (cookie, 0); 250 magic_load (cookie, 0);
243 else 251 else
244 XSRETURN_UNDEF; 252 XSRETURN_UNDEF;
245 } 253 }
246 254
255 perlinterp_release ();
247 RETVAL = magic_file (cookie, path); 256 RETVAL = magic_file (cookie, path);
257 perlinterp_acquire ();
248} 258}
249 OUTPUT: 259 OUTPUT:
250 RETVAL 260 RETVAL
251 261
252# missing/broken in Gtk2 perl module 262# missing/broken in Gtk2 perl module
268 RETVAL 278 RETVAL
269 279
270GdkPixbuf_noinc * 280GdkPixbuf_noinc *
271dealpha_expose (GdkPixbuf *pb) 281dealpha_expose (GdkPixbuf *pb)
272 CODE: 282 CODE:
283 perlinterp_release ();
273{ 284{
274 int w = gdk_pixbuf_get_width (pb); 285 int w = gdk_pixbuf_get_width (pb);
275 int h = gdk_pixbuf_get_height (pb); 286 int h = gdk_pixbuf_get_height (pb);
276 int bpp = gdk_pixbuf_get_n_channels (pb); 287 int bpp = gdk_pixbuf_get_n_channels (pb);
277 int x, y, i; 288 int x, y, i;
286 for (x = 0; x < w; x++) 297 for (x = 0; x < w; x++)
287 for (y = 0; y < h; y++) 298 for (y = 0; y < h; y++)
288 for (i = 0; i < 3; i++) 299 for (i = 0; i < 3; i++)
289 dst[x * 3 + y * dstr + i] = src[x * bpp + y * sstr + i]; 300 dst[x * 3 + y * dstr + i] = src[x * bpp + y * sstr + i];
290} 301}
302 perlinterp_acquire ();
291 OUTPUT: 303 OUTPUT:
292 RETVAL 304 RETVAL
293 305
294GdkPixbuf_noinc * 306GdkPixbuf_noinc *
295rotate (GdkPixbuf *pb, int angle) 307rotate (GdkPixbuf *pb, int angle)
296 CODE: 308 CODE:
309 perlinterp_release ();
297 if (angle < 0) 310 if (angle < 0)
298 angle += 360; 311 angle += 360;
299 RETVAL = gdk_pixbuf_rotate_simple (pb, angle == 0 ? GDK_PIXBUF_ROTATE_NONE 312 RETVAL = gdk_pixbuf_rotate_simple (pb, angle == 0 ? GDK_PIXBUF_ROTATE_NONE
300 : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE 313 : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE
301 : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN 314 : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN
302 : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE 315 : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE
303 : angle); 316 : angle);
317 perlinterp_acquire ();
304 OUTPUT: 318 OUTPUT:
305 RETVAL 319 RETVAL
306 320
307GdkPixbuf_noinc * 321GdkPixbuf_noinc *
308load_jpeg (SV *path, int thumbnail=0) 322load_jpeg (SV *path, int thumbnail = 0, int iw = 0, int ih = 0)
309 CODE: 323 CODE:
310{ 324{
311 struct jpeg_decompress_struct cinfo; 325 struct jpeg_decompress_struct cinfo;
312 struct jpg_err_mgr jerr; 326 struct jpg_err_mgr jerr;
313 guchar *data; 327 guchar *data;
320 fp = fopen (SvPVbyte_nolen (path), "rb"); 334 fp = fopen (SvPVbyte_nolen (path), "rb");
321 335
322 if (!fp) 336 if (!fp)
323 XSRETURN_UNDEF; 337 XSRETURN_UNDEF;
324 338
339 perlinterp_release ();
340
325 cinfo.err = jpeg_std_error (&jerr.err); 341 cinfo.err = jpeg_std_error (&jerr.err);
326 342
327 jerr.err.error_exit = cv_error_exit; 343 jerr.err.error_exit = cv_error_exit;
328 jerr.err.output_message = cv_error_output; 344 jerr.err.output_message = cv_error_output;
329 345
333 jpeg_destroy_decompress (&cinfo); 349 jpeg_destroy_decompress (&cinfo);
334 350
335 if (pb) 351 if (pb)
336 g_object_unref ((gpointer)pb); 352 g_object_unref ((gpointer)pb);
337 353
354 perlinterp_acquire ();
338 XSRETURN_UNDEF; 355 XSRETURN_UNDEF;
339 } 356 }
340 357
341 jpeg_create_decompress (&cinfo); 358 jpeg_create_decompress (&cinfo);
342 359
358 { 375 {
359 cinfo.dct_method = JDCT_FASTEST; 376 cinfo.dct_method = JDCT_FASTEST;
360 cinfo.do_fancy_upsampling = FALSE; 377 cinfo.do_fancy_upsampling = FALSE;
361 378
362 while (cinfo.scale_denom < 8 379 while (cinfo.scale_denom < 8
363 && cinfo.output_width >= IW*4 380 && cinfo.output_width >= iw*4
364 && cinfo.output_height >= IH*4) 381 && cinfo.output_height >= ih*4)
365 { 382 {
366 cinfo.scale_denom <<= 1; 383 cinfo.scale_denom <<= 1;
367 jpeg_calc_output_dimensions (&cinfo); 384 jpeg_calc_output_dimensions (&cinfo);
368 } 385 }
369 } 386 }
370 387
388 if (cinfo.output_components != 3)
389 longjmp (jerr.setjmp_buffer, 3);
390
391 if (cinfo.jpeg_color_space == JCS_YCCK || cinfo.jpeg_color_space == JCS_CMYK)
392 {
393 cinfo.out_color_space = JCS_CMYK;
394 cinfo.output_components = 4;
395 }
396
371 pb = RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, cinfo.output_width, cinfo.output_height); 397 pb = RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, cinfo.output_components == 4, 8, cinfo.output_width, cinfo.output_height);
372 if (!RETVAL) 398 if (!RETVAL)
373 longjmp (jerr.setjmp_buffer, 2); 399 longjmp (jerr.setjmp_buffer, 2);
374 400
375 data = gdk_pixbuf_get_pixels (RETVAL); 401 data = gdk_pixbuf_get_pixels (RETVAL);
376 rs = gdk_pixbuf_get_rowstride (RETVAL); 402 rs = gdk_pixbuf_get_rowstride (RETVAL);
377
378 if (cinfo.output_components != 3)
379 longjmp (jerr.setjmp_buffer, 3);
380 403
381 jpeg_start_decompress (&cinfo); 404 jpeg_start_decompress (&cinfo);
382 405
383 while (cinfo.output_scanline < cinfo.output_height) 406 while (cinfo.output_scanline < cinfo.output_height)
384 { 407 {
391 rp [3] = (guchar *)rp [2] + rs; 414 rp [3] = (guchar *)rp [2] + rs;
392 415
393 jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4); 416 jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4);
394 } 417 }
395 418
419 if (cinfo.out_color_space == JCS_CMYK)
420 {
421 guchar *end = data + cinfo.output_height * rs;
422
423 while (data < end)
424 {
425 U32 c = data [0];
426 U32 m = data [1];
427 U32 y = data [2];
428 U32 k = data [3];
429
430 if (0)
431 if (cinfo.Adobe_transform == 2)
432 {
433 c ^= 0xff;
434 m ^= 0xff;
435 y ^= 0xff;
436 k ^= 0xff;
437 }
438
439 data [0] = (c * k + 0x80) / 0xff;
440 data [1] = (m * k + 0x80) / 0xff;
441 data [2] = (y * k + 0x80) / 0xff;
442 data [3] = 0xff;
443
444 data += 4;
445 }
446 }
447
396 jpeg_finish_decompress (&cinfo); 448 jpeg_finish_decompress (&cinfo);
397 fclose (fp); 449 fclose (fp);
398 jpeg_destroy_decompress (&cinfo); 450 jpeg_destroy_decompress (&cinfo);
451 perlinterp_acquire ();
399} 452}
400 OUTPUT: 453 OUTPUT:
401 RETVAL 454 RETVAL
402 455
403void 456void
404compare (GdkPixbuf *a, GdkPixbuf *b) 457compare (GdkPixbuf *a, GdkPixbuf *b)
405 PPCODE: 458 PPCODE:
459 perlinterp_release ();
406{ 460{
407 int w = gdk_pixbuf_get_width (a); 461 int w = gdk_pixbuf_get_width (a);
408 int h = gdk_pixbuf_get_height (a); 462 int h = gdk_pixbuf_get_height (a);
409 463
410 int sa = gdk_pixbuf_get_rowstride (a); 464 int sa = gdk_pixbuf_get_rowstride (a);
437 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d)); 491 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
438 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d)); 492 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
439 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d)); 493 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
440 } 494 }
441 } 495 }
496
497 perlinterp_acquire ();
442 498
443 EXTEND (SP, 2); 499 EXTEND (SP, 2);
444 PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.))))); 500 PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.)))));
445 PUSHs (sv_2mortal (newSVnv (peak / 255.))); 501 PUSHs (sv_2mortal (newSVnv (peak / 255.)));
446} 502}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines