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

Comparing CV/CV.xs (file contents):
Revision 1.43 by root, Sun May 3 08:16:47 2009 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 }
213{ 221{
214 static magic_t cookie; 222 static magic_t cookie;
215 223
216 if (!cookie) 224 if (!cookie)
217 { 225 {
218 cookie = magic_open (MAGIC_NONE); 226 cookie = magic_open (MAGIC_SYMLINK);
219 227
220 if (cookie) 228 if (cookie)
221 magic_load (cookie, 0); 229 magic_load (cookie, 0);
222 else 230 else
223 XSRETURN_UNDEF; 231 XSRETURN_UNDEF;
234{ 242{
235 static magic_t cookie; 243 static magic_t cookie;
236 244
237 if (!cookie) 245 if (!cookie)
238 { 246 {
239 cookie = magic_open (MAGIC_MIME); 247 cookie = magic_open (MAGIC_MIME | MAGIC_SYMLINK);
240 248
241 if (cookie) 249 if (cookie)
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 ();
310 if (angle < 0)
311 angle += 360;
297 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
298 : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE 313 : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE
299 : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN 314 : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN
300 : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE 315 : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE
301 : angle); 316 : angle);
317 perlinterp_acquire ();
302 OUTPUT: 318 OUTPUT:
303 RETVAL 319 RETVAL
304 320
305GdkPixbuf_noinc * 321GdkPixbuf_noinc *
306load_jpeg (SV *path, int thumbnail=0) 322load_jpeg (SV *path, int thumbnail = 0, int iw = 0, int ih = 0)
307 CODE: 323 CODE:
308{ 324{
309 struct jpeg_decompress_struct cinfo; 325 struct jpeg_decompress_struct cinfo;
310 struct jpg_err_mgr jerr; 326 struct jpg_err_mgr jerr;
311 guchar *data; 327 guchar *data;
318 fp = fopen (SvPVbyte_nolen (path), "rb"); 334 fp = fopen (SvPVbyte_nolen (path), "rb");
319 335
320 if (!fp) 336 if (!fp)
321 XSRETURN_UNDEF; 337 XSRETURN_UNDEF;
322 338
339 perlinterp_release ();
340
323 cinfo.err = jpeg_std_error (&jerr.err); 341 cinfo.err = jpeg_std_error (&jerr.err);
324 342
325 jerr.err.error_exit = cv_error_exit; 343 jerr.err.error_exit = cv_error_exit;
326 jerr.err.output_message = cv_error_output; 344 jerr.err.output_message = cv_error_output;
327 345
331 jpeg_destroy_decompress (&cinfo); 349 jpeg_destroy_decompress (&cinfo);
332 350
333 if (pb) 351 if (pb)
334 g_object_unref ((gpointer)pb); 352 g_object_unref ((gpointer)pb);
335 353
354 perlinterp_acquire ();
336 XSRETURN_UNDEF; 355 XSRETURN_UNDEF;
337 } 356 }
338 357
339 jpeg_create_decompress (&cinfo); 358 jpeg_create_decompress (&cinfo);
340 359
356 { 375 {
357 cinfo.dct_method = JDCT_FASTEST; 376 cinfo.dct_method = JDCT_FASTEST;
358 cinfo.do_fancy_upsampling = FALSE; 377 cinfo.do_fancy_upsampling = FALSE;
359 378
360 while (cinfo.scale_denom < 8 379 while (cinfo.scale_denom < 8
361 && cinfo.output_width >= IW*4 380 && cinfo.output_width >= iw*4
362 && cinfo.output_height >= IH*4) 381 && cinfo.output_height >= ih*4)
363 { 382 {
364 cinfo.scale_denom <<= 1; 383 cinfo.scale_denom <<= 1;
365 jpeg_calc_output_dimensions (&cinfo); 384 jpeg_calc_output_dimensions (&cinfo);
366 } 385 }
367 } 386 }
368 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
369 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);
370 if (!RETVAL) 398 if (!RETVAL)
371 longjmp (jerr.setjmp_buffer, 2); 399 longjmp (jerr.setjmp_buffer, 2);
372 400
373 data = gdk_pixbuf_get_pixels (RETVAL); 401 data = gdk_pixbuf_get_pixels (RETVAL);
374 rs = gdk_pixbuf_get_rowstride (RETVAL); 402 rs = gdk_pixbuf_get_rowstride (RETVAL);
375
376 if (cinfo.output_components != 3)
377 longjmp (jerr.setjmp_buffer, 3);
378 403
379 jpeg_start_decompress (&cinfo); 404 jpeg_start_decompress (&cinfo);
380 405
381 while (cinfo.output_scanline < cinfo.output_height) 406 while (cinfo.output_scanline < cinfo.output_height)
382 { 407 {
389 rp [3] = (guchar *)rp [2] + rs; 414 rp [3] = (guchar *)rp [2] + rs;
390 415
391 jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4); 416 jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4);
392 } 417 }
393 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
394 jpeg_finish_decompress (&cinfo); 448 jpeg_finish_decompress (&cinfo);
395 fclose (fp); 449 fclose (fp);
396 jpeg_destroy_decompress (&cinfo); 450 jpeg_destroy_decompress (&cinfo);
451 perlinterp_acquire ();
397} 452}
398 OUTPUT: 453 OUTPUT:
399 RETVAL 454 RETVAL
400 455
401void 456void
402compare (GdkPixbuf *a, GdkPixbuf *b) 457compare (GdkPixbuf *a, GdkPixbuf *b)
403 PPCODE: 458 PPCODE:
459 perlinterp_release ();
404{ 460{
405 int w = gdk_pixbuf_get_width (a); 461 int w = gdk_pixbuf_get_width (a);
406 int h = gdk_pixbuf_get_height (a); 462 int h = gdk_pixbuf_get_height (a);
407 463
408 int sa = gdk_pixbuf_get_rowstride (a); 464 int sa = gdk_pixbuf_get_rowstride (a);
435 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));
436 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));
437 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));
438 } 494 }
439 } 495 }
496
497 perlinterp_acquire ();
440 498
441 EXTEND (SP, 2); 499 EXTEND (SP, 2);
442 PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.))))); 500 PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.)))));
443 PUSHs (sv_2mortal (newSVnv (peak / 255.))); 501 PUSHs (sv_2mortal (newSVnv (peak / 255.)));
444} 502}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines