… | |
… | |
7 | #include <math.h> |
7 | #include <math.h> |
8 | |
8 | |
9 | #include <magic.h> |
9 | #include <magic.h> |
10 | |
10 | |
11 | #include <jpeglib.h> |
11 | #include <jpeglib.h> |
|
|
12 | #include <jerror.h> |
|
|
13 | |
12 | #include <glib.h> |
14 | #include <glib.h> |
13 | #include <gtk/gtk.h> |
15 | #include <gtk/gtk.h> |
|
|
16 | #include <gdk/gdkx.h> |
14 | #include <gdk-pixbuf/gdk-pixbuf.h> |
17 | #include <gdk-pixbuf/gdk-pixbuf.h> |
15 | |
18 | |
16 | #include <gperl.h> |
19 | #include <gperl.h> |
17 | #include <gtk2perl.h> |
20 | #include <gtk2perl.h> |
18 | |
21 | |
19 | #include <assert.h> |
22 | #include <assert.h> |
20 | |
23 | |
|
|
24 | #if WEBP |
|
|
25 | #include <webp/decode.h> |
|
|
26 | #endif |
|
|
27 | |
21 | #include "perlmulticore.h" |
28 | #include "perlmulticore.h" |
22 | |
29 | |
23 | #define IW 80 /* MUST match Schnauzer.pm! */ |
30 | #define IW 80 /* MUST match Schnauzer.pm! */ |
24 | #define IH 60 /* MUST match Schnauzer.pm! */ |
31 | #define IH 60 /* MUST match Schnauzer.pm! */ |
25 | |
32 | |
… | |
… | |
28 | #define LINELENGTH 240 |
35 | #define LINELENGTH 240 |
29 | |
36 | |
30 | #define ELLIPSIS "\xe2\x80\xa6" |
37 | #define ELLIPSIS "\xe2\x80\xa6" |
31 | |
38 | |
32 | typedef char *octet_string; |
39 | typedef char *octet_string; |
|
|
40 | |
|
|
41 | static magic_t magic_cookie[2]; /* !mime, mime */ |
33 | |
42 | |
34 | struct jpg_err_mgr |
43 | struct jpg_err_mgr |
35 | { |
44 | { |
36 | struct jpeg_error_mgr err; |
45 | struct jpeg_error_mgr err; |
37 | jmp_buf setjmp_buffer; |
46 | jmp_buf setjmp_buffer; |
… | |
… | |
156 | if (!--a85_cnt) |
165 | if (!--a85_cnt) |
157 | { |
166 | { |
158 | a85_cnt = 4; |
167 | a85_cnt = 4; |
159 | if (a85_val) |
168 | if (a85_val) |
160 | { |
169 | { |
161 | a85_ptr[4] = (a85_val % 85) + 33; a85_val /= 85; |
170 | a85_ptr[4] = (a85_val % 85) + 33; a85_val /= 85; |
162 | a85_ptr[3] = (a85_val % 85) + 33; a85_val /= 85; |
171 | a85_ptr[3] = (a85_val % 85) + 33; a85_val /= 85; |
163 | a85_ptr[2] = (a85_val % 85) + 33; a85_val /= 85; |
172 | a85_ptr[2] = (a85_val % 85) + 33; a85_val /= 85; |
164 | a85_ptr[1] = (a85_val % 85) + 33; a85_val /= 85; |
173 | a85_ptr[1] = (a85_val % 85) + 33; a85_val /= 85; |
165 | a85_ptr[0] = (a85_val ) + 33; |
174 | a85_ptr[0] = (a85_val ) + 33; |
166 | |
175 | |
167 | a85_ptr += 5; |
176 | a85_ptr += 5; |
168 | } |
177 | } |
… | |
… | |
192 | PerlIO_write (fp, a85_buf, a85_ptr - a85_buf); |
201 | PerlIO_write (fp, a85_buf, a85_ptr - a85_buf); |
193 | } |
202 | } |
194 | |
203 | |
195 | ///////////////////////////////////////////////////////////////////////////// |
204 | ///////////////////////////////////////////////////////////////////////////// |
196 | |
205 | |
|
|
206 | static void cv_ms_init (j_decompress_ptr cinfo) |
|
|
207 | { |
|
|
208 | } |
|
|
209 | |
|
|
210 | static void cv_ms_term (j_decompress_ptr cinfo) |
|
|
211 | { |
|
|
212 | } |
|
|
213 | |
|
|
214 | static boolean cv_ms_fill (j_decompress_ptr cinfo) |
|
|
215 | { |
|
|
216 | ERREXIT (cinfo, JERR_INPUT_EMPTY); |
|
|
217 | |
|
|
218 | return TRUE; |
|
|
219 | } |
|
|
220 | |
|
|
221 | static void cv_ms_skip (j_decompress_ptr cinfo, long num_bytes) |
|
|
222 | { |
|
|
223 | if (num_bytes > 0) |
|
|
224 | { |
|
|
225 | struct jpeg_source_mgr *src = (struct jpeg_source_mgr *)cinfo->src; |
|
|
226 | |
|
|
227 | src->next_input_byte += num_bytes; |
|
|
228 | src->bytes_in_buffer -= num_bytes; |
|
|
229 | } |
|
|
230 | } |
|
|
231 | |
|
|
232 | static void cv_jpeg_mem_src (j_decompress_ptr cinfo, void *buf, size_t buflen) |
|
|
233 | { |
|
|
234 | struct jpeg_source_mgr *src; |
|
|
235 | |
|
|
236 | if (!cinfo->src) |
|
|
237 | cinfo->src = (struct jpeg_source_mgr *) |
|
|
238 | (*cinfo->mem->alloc_small) ( |
|
|
239 | (j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof (struct jpeg_source_mgr) |
|
|
240 | ); |
|
|
241 | |
|
|
242 | src = (struct jpeg_source_mgr *)cinfo->src; |
|
|
243 | src->init_source = cv_ms_init; |
|
|
244 | src->fill_input_buffer = cv_ms_fill; |
|
|
245 | src->skip_input_data = cv_ms_skip; |
|
|
246 | src->resync_to_restart = jpeg_resync_to_restart; |
|
|
247 | src->term_source = cv_ms_term; |
|
|
248 | src->next_input_byte = (JOCTET *)buf; |
|
|
249 | src->bytes_in_buffer = buflen; |
|
|
250 | } |
|
|
251 | |
|
|
252 | ///////////////////////////////////////////////////////////////////////////// |
|
|
253 | |
197 | MODULE = Gtk2::CV PACKAGE = Gtk2::CV |
254 | MODULE = Gtk2::CV PACKAGE = Gtk2::CV |
198 | |
255 | |
199 | PROTOTYPES: ENABLE |
256 | PROTOTYPES: ENABLE |
200 | |
257 | |
201 | # missing function in perl. really :) |
258 | # missing function in perl. really :) |
… | |
… | |
214 | |
271 | |
215 | OUTPUT: |
272 | OUTPUT: |
216 | RETVAL |
273 | RETVAL |
217 | |
274 | |
218 | const char * |
275 | const char * |
219 | magic (octet_string path) |
276 | magic (SV *path_or_data) |
|
|
277 | ALIAS: |
|
|
278 | magic = 0 |
|
|
279 | magic_mime = 1 |
|
|
280 | magic_buffer = 2 |
|
|
281 | magic_buffer_mime = 3 |
220 | CODE: |
282 | CODE: |
221 | { |
283 | { |
222 | static magic_t cookie; |
284 | STRLEN len; |
|
|
285 | char *data = SvPVbyte (path_or_data, len); |
223 | |
286 | |
224 | if (!cookie) |
287 | if (!magic_cookie[0]) |
225 | { |
288 | { |
226 | cookie = magic_open (MAGIC_SYMLINK); |
289 | magic_cookie[0] = magic_open (MAGIC_SYMLINK); |
227 | |
290 | magic_cookie[1] = magic_open (MAGIC_SYMLINK | MAGIC_MIME_TYPE); |
228 | if (cookie) |
|
|
229 | magic_load (cookie, 0); |
291 | magic_load (magic_cookie[0], 0); |
230 | else |
|
|
231 | XSRETURN_UNDEF; |
|
|
232 | } |
|
|
233 | |
|
|
234 | RETVAL = magic_file (cookie, path); |
|
|
235 | } |
|
|
236 | OUTPUT: |
|
|
237 | RETVAL |
|
|
238 | |
|
|
239 | const char * |
|
|
240 | magic_mime (octet_string path) |
|
|
241 | CODE: |
|
|
242 | { |
|
|
243 | static magic_t cookie; |
|
|
244 | |
|
|
245 | if (!cookie) |
|
|
246 | { |
|
|
247 | cookie = magic_open (MAGIC_MIME | MAGIC_SYMLINK); |
|
|
248 | |
|
|
249 | if (cookie) |
|
|
250 | magic_load (cookie, 0); |
292 | magic_load (magic_cookie[1], 0); |
251 | else |
|
|
252 | XSRETURN_UNDEF; |
|
|
253 | } |
293 | } |
254 | |
294 | |
255 | perlinterp_release (); |
295 | perlinterp_release (); |
256 | RETVAL = magic_file (cookie, path); |
296 | |
|
|
297 | RETVAL = ix & 2 |
|
|
298 | ? magic_buffer (magic_cookie[ix & 1], data, len) |
|
|
299 | : magic_file (magic_cookie[ix & 1], data); |
|
|
300 | |
257 | perlinterp_acquire (); |
301 | perlinterp_acquire (); |
258 | } |
302 | } |
259 | OUTPUT: |
303 | OUTPUT: |
260 | RETVAL |
304 | RETVAL |
261 | |
305 | |
… | |
… | |
316 | : angle); |
360 | : angle); |
317 | perlinterp_acquire (); |
361 | perlinterp_acquire (); |
318 | OUTPUT: |
362 | OUTPUT: |
319 | RETVAL |
363 | RETVAL |
320 | |
364 | |
|
|
365 | const char * |
|
|
366 | filetype (SV *image_data) |
|
|
367 | CODE: |
|
|
368 | { |
|
|
369 | STRLEN data_len; |
|
|
370 | U8 *data = SvPVbyte (image_data, data_len); |
|
|
371 | |
|
|
372 | if (data_len >= 20 |
|
|
373 | && data[0] == 0xff |
|
|
374 | && data[1] == 0xd8 |
|
|
375 | && data[2] == 0xff) |
|
|
376 | RETVAL = "jpg"; |
|
|
377 | else if (data_len >= 12 |
|
|
378 | && data[ 0] == (U8)'R' |
|
|
379 | && data[ 1] == (U8)'I' |
|
|
380 | && data[ 2] == (U8)'F' |
|
|
381 | && data[ 3] == (U8)'F' |
|
|
382 | && data[ 8] == (U8)'W' |
|
|
383 | && data[ 9] == (U8)'E' |
|
|
384 | && data[10] == (U8)'B' |
|
|
385 | && data[11] == (U8)'P') |
|
|
386 | RETVAL = "webp"; |
|
|
387 | else if (data_len >= 16 |
|
|
388 | && data[ 0] == 0x89 |
|
|
389 | && data[ 1] == (U8)'P' |
|
|
390 | && data[ 2] == (U8)'N' |
|
|
391 | && data[ 3] == (U8)'G' |
|
|
392 | && data[ 4] == 0x0d |
|
|
393 | && data[ 5] == 0x0a |
|
|
394 | && data[ 6] == 0x1a |
|
|
395 | && data[ 7] == 0x0a) |
|
|
396 | RETVAL = "png"; |
|
|
397 | else |
|
|
398 | RETVAL = "other"; |
|
|
399 | } |
|
|
400 | OUTPUT: |
|
|
401 | RETVAL |
|
|
402 | |
321 | GdkPixbuf_noinc * |
403 | GdkPixbuf_noinc * |
|
|
404 | decode_webp (SV *image_data, int thumbnail = 0, int iw = 0, int ih = 0) |
|
|
405 | CODE: |
|
|
406 | { |
|
|
407 | #if WEBP |
|
|
408 | guchar *data; |
|
|
409 | STRLEN data_size; |
|
|
410 | int alpha; |
|
|
411 | WebPDecoderConfig config; |
|
|
412 | int inw, inh; |
|
|
413 | |
|
|
414 | data = SvPVbyte (image_data, data_size); |
|
|
415 | |
|
|
416 | perlinterp_release (); |
|
|
417 | |
|
|
418 | RETVAL = 0; |
|
|
419 | |
|
|
420 | if (!WebPInitDecoderConfig (&config)) |
|
|
421 | goto err; |
|
|
422 | |
|
|
423 | config.options.use_threads = 1; |
|
|
424 | |
|
|
425 | if (WebPGetFeatures (data, data_size, &config.input) != VP8_STATUS_OK) |
|
|
426 | goto err; |
|
|
427 | |
|
|
428 | inw = config.input.width; |
|
|
429 | inh = config.input.height; |
|
|
430 | |
|
|
431 | if (thumbnail) |
|
|
432 | { |
|
|
433 | if (inw * ih > inh * iw) |
|
|
434 | ih = (iw * inh + inw - 1) / inw; |
|
|
435 | else |
|
|
436 | iw = (ih * inw + inh - 1) / inh; |
|
|
437 | |
|
|
438 | config.options.bypass_filtering = 1; |
|
|
439 | config.options.no_fancy_upsampling = 1; |
|
|
440 | |
|
|
441 | config.options.use_scaling = 1; |
|
|
442 | config.options.scaled_width = iw; |
|
|
443 | config.options.scaled_height = ih; |
|
|
444 | } |
|
|
445 | else |
|
|
446 | { |
|
|
447 | iw = inw; |
|
|
448 | ih = inh; |
|
|
449 | } |
|
|
450 | |
|
|
451 | alpha = config.input.has_alpha; |
|
|
452 | |
|
|
453 | RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, !!alpha, 8, iw, ih); |
|
|
454 | if (!RETVAL) |
|
|
455 | goto err; |
|
|
456 | |
|
|
457 | config.output.colorspace = alpha ? MODE_RGBA : MODE_RGB; |
|
|
458 | config.output.u.RGBA.rgba = gdk_pixbuf_get_pixels (RETVAL); |
|
|
459 | config.output.u.RGBA.stride = gdk_pixbuf_get_rowstride (RETVAL); |
|
|
460 | config.output.u.RGBA.size = gdk_pixbuf_get_byte_length (RETVAL); |
|
|
461 | config.output.is_external_memory = 1; |
|
|
462 | |
|
|
463 | if (WebPDecode (data, data_size, &config) != VP8_STATUS_OK) |
|
|
464 | { |
|
|
465 | g_object_unref (RETVAL); |
|
|
466 | RETVAL = 0; |
|
|
467 | goto err; |
|
|
468 | } |
|
|
469 | |
|
|
470 | err: |
|
|
471 | perlinterp_acquire (); |
|
|
472 | #else |
|
|
473 | croak ("load_webp: webp not enabled at compile time"); |
|
|
474 | #endif |
|
|
475 | } |
|
|
476 | OUTPUT: |
|
|
477 | RETVAL |
|
|
478 | |
|
|
479 | GdkPixbuf_noinc * |
322 | load_jpeg (SV *path, int thumbnail = 0, int iw = 0, int ih = 0) |
480 | decode_jpeg (SV *image_data, int thumbnail = 0, int iw = 0, int ih = 0) |
323 | CODE: |
481 | CODE: |
324 | { |
482 | { |
325 | struct jpeg_decompress_struct cinfo; |
483 | struct jpeg_decompress_struct cinfo; |
326 | struct jpg_err_mgr jerr; |
484 | struct jpg_err_mgr jerr; |
327 | guchar *data; |
|
|
328 | int rs; |
485 | int rs; |
329 | FILE *fp; |
|
|
330 | volatile GdkPixbuf *pb = 0; |
486 | volatile GdkPixbuf *pb = 0; |
|
|
487 | STRLEN data_len; |
|
|
488 | guchar *data = SvPVbyte (image_data, data_len); |
331 | |
489 | |
332 | RETVAL = 0; |
490 | RETVAL = 0; |
333 | |
|
|
334 | fp = fopen (SvPVbyte_nolen (path), "rb"); |
|
|
335 | |
|
|
336 | if (!fp) |
|
|
337 | XSRETURN_UNDEF; |
|
|
338 | |
491 | |
339 | perlinterp_release (); |
492 | perlinterp_release (); |
340 | |
493 | |
341 | cinfo.err = jpeg_std_error (&jerr.err); |
494 | cinfo.err = jpeg_std_error (&jerr.err); |
342 | |
495 | |
343 | jerr.err.error_exit = cv_error_exit; |
496 | jerr.err.error_exit = cv_error_exit; |
344 | jerr.err.output_message = cv_error_output; |
497 | jerr.err.output_message = cv_error_output; |
345 | |
498 | |
346 | if ((rs = setjmp (jerr.setjmp_buffer))) |
499 | if ((rs = setjmp (jerr.setjmp_buffer))) |
347 | { |
500 | { |
348 | fclose (fp); |
|
|
349 | jpeg_destroy_decompress (&cinfo); |
501 | jpeg_destroy_decompress (&cinfo); |
350 | |
502 | |
351 | if (pb) |
503 | if (pb) |
352 | g_object_unref ((gpointer)pb); |
504 | g_object_unref ((gpointer)pb); |
353 | |
505 | |
354 | perlinterp_acquire (); |
506 | perlinterp_acquire (); |
355 | XSRETURN_UNDEF; |
507 | XSRETURN_UNDEF; |
356 | } |
508 | } |
357 | |
509 | |
358 | jpeg_create_decompress (&cinfo); |
510 | jpeg_create_decompress (&cinfo); |
|
|
511 | cv_jpeg_mem_src (&cinfo, data, data_len); |
359 | |
512 | |
360 | jpeg_stdio_src (&cinfo, fp); |
|
|
361 | jpeg_read_header (&cinfo, TRUE); |
513 | jpeg_read_header (&cinfo, TRUE); |
362 | |
514 | |
363 | cinfo.dct_method = JDCT_DEFAULT; |
515 | cinfo.dct_method = JDCT_DEFAULT; |
364 | cinfo.do_fancy_upsampling = FALSE; /* worse quality, but nobody compained so far, and gdk-pixbuf does the same */ |
516 | cinfo.do_fancy_upsampling = FALSE; /* worse quality, but nobody compained so far, and gdk-pixbuf does the same */ |
365 | cinfo.do_block_smoothing = FALSE; |
517 | cinfo.do_block_smoothing = FALSE; |
… | |
… | |
425 | U32 c = data [0]; |
577 | U32 c = data [0]; |
426 | U32 m = data [1]; |
578 | U32 m = data [1]; |
427 | U32 y = data [2]; |
579 | U32 y = data [2]; |
428 | U32 k = data [3]; |
580 | U32 k = data [3]; |
429 | |
581 | |
|
|
582 | if (0) |
|
|
583 | if (cinfo.Adobe_transform == 2) |
|
|
584 | { |
|
|
585 | c ^= 0xff; |
|
|
586 | m ^= 0xff; |
|
|
587 | y ^= 0xff; |
|
|
588 | k ^= 0xff; |
|
|
589 | } |
|
|
590 | |
430 | data [0] = c * k / 255; |
591 | data [0] = (c * k + 0x80) / 0xff; |
431 | data [1] = m * k / 255; |
592 | data [1] = (m * k + 0x80) / 0xff; |
432 | data [2] = y * k / 255; |
593 | data [2] = (y * k + 0x80) / 0xff; |
433 | data [3] = 255; |
594 | data [3] = 0xff; |
434 | |
595 | |
435 | data += 4; |
596 | data += 4; |
436 | } |
597 | } |
437 | } |
598 | } |
438 | |
599 | |
439 | jpeg_finish_decompress (&cinfo); |
600 | jpeg_finish_decompress (&cinfo); |
440 | fclose (fp); |
|
|
441 | jpeg_destroy_decompress (&cinfo); |
601 | jpeg_destroy_decompress (&cinfo); |
442 | perlinterp_acquire (); |
602 | perlinterp_acquire (); |
443 | } |
603 | } |
444 | OUTPUT: |
604 | OUTPUT: |
445 | RETVAL |
605 | RETVAL |