… | |
… | |
38 | }; |
38 | }; |
39 | |
39 | |
40 | static void |
40 | static void |
41 | cv_error_exit (j_common_ptr cinfo) |
41 | cv_error_exit (j_common_ptr cinfo) |
42 | { |
42 | { |
|
|
43 | cinfo->err->output_message (cinfo); |
43 | longjmp (((struct jpg_err_mgr *)cinfo->err)->setjmp_buffer, 99); |
44 | longjmp (((struct jpg_err_mgr *)cinfo->err)->setjmp_buffer, 99); |
44 | } |
45 | } |
45 | |
46 | |
46 | static void |
47 | static void |
47 | cv_error_output (j_common_ptr cinfo) |
48 | cv_error_output (j_common_ptr cinfo) |
48 | { |
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); |
49 | return; |
55 | return; |
50 | } |
56 | } |
51 | |
57 | |
52 | static void |
58 | static void |
53 | rgb_to_hsv (unsigned int r, unsigned int g, unsigned int b, |
59 | rgb_to_hsv (unsigned int r, unsigned int g, unsigned int b, |
… | |
… | |
311 | perlinterp_acquire (); |
317 | perlinterp_acquire (); |
312 | OUTPUT: |
318 | OUTPUT: |
313 | RETVAL |
319 | RETVAL |
314 | |
320 | |
315 | GdkPixbuf_noinc * |
321 | GdkPixbuf_noinc * |
316 | load_jpeg (SV *path, int thumbnail=0) |
322 | load_jpeg (SV *path, int thumbnail = 0, int iw = 0, int ih = 0) |
317 | CODE: |
323 | CODE: |
318 | { |
324 | { |
319 | struct jpeg_decompress_struct cinfo; |
325 | struct jpeg_decompress_struct cinfo; |
320 | struct jpg_err_mgr jerr; |
326 | struct jpg_err_mgr jerr; |
321 | guchar *data; |
327 | guchar *data; |
… | |
… | |
369 | { |
375 | { |
370 | cinfo.dct_method = JDCT_FASTEST; |
376 | cinfo.dct_method = JDCT_FASTEST; |
371 | cinfo.do_fancy_upsampling = FALSE; |
377 | cinfo.do_fancy_upsampling = FALSE; |
372 | |
378 | |
373 | while (cinfo.scale_denom < 8 |
379 | while (cinfo.scale_denom < 8 |
374 | && cinfo.output_width >= IW*4 |
380 | && cinfo.output_width >= iw*4 |
375 | && cinfo.output_height >= IH*4) |
381 | && cinfo.output_height >= ih*4) |
376 | { |
382 | { |
377 | cinfo.scale_denom <<= 1; |
383 | cinfo.scale_denom <<= 1; |
378 | jpeg_calc_output_dimensions (&cinfo); |
384 | jpeg_calc_output_dimensions (&cinfo); |
379 | } |
385 | } |
380 | } |
386 | } |
381 | |
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 | |
382 | 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); |
383 | if (!RETVAL) |
398 | if (!RETVAL) |
384 | longjmp (jerr.setjmp_buffer, 2); |
399 | longjmp (jerr.setjmp_buffer, 2); |
385 | |
400 | |
386 | data = gdk_pixbuf_get_pixels (RETVAL); |
401 | data = gdk_pixbuf_get_pixels (RETVAL); |
387 | rs = gdk_pixbuf_get_rowstride (RETVAL); |
402 | rs = gdk_pixbuf_get_rowstride (RETVAL); |
388 | |
|
|
389 | if (cinfo.output_components != 3) |
|
|
390 | longjmp (jerr.setjmp_buffer, 3); |
|
|
391 | |
403 | |
392 | jpeg_start_decompress (&cinfo); |
404 | jpeg_start_decompress (&cinfo); |
393 | |
405 | |
394 | while (cinfo.output_scanline < cinfo.output_height) |
406 | while (cinfo.output_scanline < cinfo.output_height) |
395 | { |
407 | { |
… | |
… | |
400 | rp [1] = (guchar *)rp [0] + rs; |
412 | rp [1] = (guchar *)rp [0] + rs; |
401 | rp [2] = (guchar *)rp [1] + rs; |
413 | rp [2] = (guchar *)rp [1] + rs; |
402 | rp [3] = (guchar *)rp [2] + rs; |
414 | rp [3] = (guchar *)rp [2] + rs; |
403 | |
415 | |
404 | jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4); |
416 | jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4); |
|
|
417 | } |
|
|
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 | data [0] = c * k / 255; |
|
|
431 | data [1] = m * k / 255; |
|
|
432 | data [2] = y * k / 255; |
|
|
433 | data [3] = 255; |
|
|
434 | |
|
|
435 | data += 4; |
|
|
436 | } |
405 | } |
437 | } |
406 | |
438 | |
407 | jpeg_finish_decompress (&cinfo); |
439 | jpeg_finish_decompress (&cinfo); |
408 | fclose (fp); |
440 | fclose (fp); |
409 | jpeg_destroy_decompress (&cinfo); |
441 | jpeg_destroy_decompress (&cinfo); |