… | |
… | |
14 | #include <gdk-pixbuf/gdk-pixbuf.h> |
14 | #include <gdk-pixbuf/gdk-pixbuf.h> |
15 | |
15 | |
16 | #include <gperl.h> |
16 | #include <gperl.h> |
17 | #include <gtk2perl.h> |
17 | #include <gtk2perl.h> |
18 | |
18 | |
|
|
19 | #include <assert.h> |
|
|
20 | |
|
|
21 | #include "perlmulticore.h" |
|
|
22 | |
19 | #define IW 80 /* MUST match Schnauzer.pm! */ |
23 | #define IW 80 /* MUST match Schnauzer.pm! */ |
20 | #define IH 60 /* MUST match Schnauzer.pm! */ |
24 | #define IH 60 /* MUST match Schnauzer.pm! */ |
21 | |
25 | |
22 | #define RAND (seed = (seed + 7141) * 54773 % 134456) |
26 | #define RAND (seed = (seed + 7141) * 54773 % 134456) |
23 | |
27 | |
24 | #define LINELENGTH 240 |
28 | #define LINELENGTH 240 |
25 | |
29 | |
26 | #define ELLIPSIS "\xe2\x80\xa6" |
30 | #define ELLIPSIS "\xe2\x80\xa6" |
|
|
31 | |
|
|
32 | typedef char *octet_string; |
27 | |
33 | |
28 | struct jpg_err_mgr |
34 | struct jpg_err_mgr |
29 | { |
35 | { |
30 | struct jpeg_error_mgr err; |
36 | struct jpeg_error_mgr err; |
31 | jmp_buf setjmp_buffer; |
37 | jmp_buf setjmp_buffer; |
32 | }; |
38 | }; |
33 | |
39 | |
34 | static void |
40 | static void |
35 | cv_error_exit (j_common_ptr cinfo) |
41 | cv_error_exit (j_common_ptr cinfo) |
36 | { |
42 | { |
|
|
43 | cinfo->err->output_message (cinfo); |
37 | longjmp (((struct jpg_err_mgr *)cinfo->err)->setjmp_buffer, 99); |
44 | longjmp (((struct jpg_err_mgr *)cinfo->err)->setjmp_buffer, 99); |
38 | } |
45 | } |
39 | |
46 | |
40 | static void |
47 | static void |
41 | cv_error_output (j_common_ptr cinfo) |
48 | cv_error_output (j_common_ptr cinfo) |
42 | { |
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); |
43 | return; |
55 | return; |
44 | } |
56 | } |
45 | |
57 | |
46 | static void |
58 | static void |
47 | 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, |
… | |
… | |
144 | if (!--a85_cnt) |
156 | if (!--a85_cnt) |
145 | { |
157 | { |
146 | a85_cnt = 4; |
158 | a85_cnt = 4; |
147 | if (a85_val) |
159 | if (a85_val) |
148 | { |
160 | { |
149 | a85_ptr[4] = (a85_val % 85) + 33; a85_val /= 85; |
161 | a85_ptr[4] = (a85_val % 85) + 33; a85_val /= 85; |
150 | a85_ptr[3] = (a85_val % 85) + 33; a85_val /= 85; |
162 | a85_ptr[3] = (a85_val % 85) + 33; a85_val /= 85; |
151 | a85_ptr[2] = (a85_val % 85) + 33; a85_val /= 85; |
163 | a85_ptr[2] = (a85_val % 85) + 33; a85_val /= 85; |
152 | a85_ptr[1] = (a85_val % 85) + 33; a85_val /= 85; |
164 | a85_ptr[1] = (a85_val % 85) + 33; a85_val /= 85; |
153 | a85_ptr[0] = (a85_val ) + 33; |
165 | a85_ptr[0] = (a85_val ) + 33; |
154 | |
166 | |
155 | a85_ptr += 5; |
167 | a85_ptr += 5; |
156 | } |
168 | } |
… | |
… | |
202 | |
214 | |
203 | OUTPUT: |
215 | OUTPUT: |
204 | RETVAL |
216 | RETVAL |
205 | |
217 | |
206 | const char * |
218 | const char * |
207 | magic (const char *path) |
219 | magic (octet_string path) |
208 | CODE: |
220 | CODE: |
209 | { |
221 | { |
210 | static magic_t cookie; |
222 | static magic_t cookie; |
211 | |
223 | |
212 | if (!cookie) |
224 | if (!cookie) |
213 | { |
225 | { |
214 | cookie = magic_open (MAGIC_NONE); |
226 | cookie = magic_open (MAGIC_SYMLINK); |
215 | |
227 | |
216 | if (cookie) |
228 | if (cookie) |
217 | magic_load (cookie, 0); |
229 | magic_load (cookie, 0); |
218 | else |
230 | else |
219 | XSRETURN_UNDEF; |
231 | XSRETURN_UNDEF; |
… | |
… | |
223 | } |
235 | } |
224 | OUTPUT: |
236 | OUTPUT: |
225 | RETVAL |
237 | RETVAL |
226 | |
238 | |
227 | const char * |
239 | const char * |
228 | magic_mime (const char *path) |
240 | magic_mime (octet_string path) |
229 | CODE: |
241 | CODE: |
230 | { |
242 | { |
231 | static magic_t cookie; |
243 | static magic_t cookie; |
232 | |
244 | |
233 | if (!cookie) |
245 | if (!cookie) |
234 | { |
246 | { |
235 | cookie = magic_open (MAGIC_MIME); |
247 | cookie = magic_open (MAGIC_MIME | MAGIC_SYMLINK); |
236 | |
248 | |
237 | if (cookie) |
249 | if (cookie) |
238 | magic_load (cookie, 0); |
250 | magic_load (cookie, 0); |
239 | else |
251 | else |
240 | XSRETURN_UNDEF; |
252 | XSRETURN_UNDEF; |
241 | } |
253 | } |
242 | |
254 | |
|
|
255 | perlinterp_release (); |
243 | RETVAL = magic_file (cookie, path); |
256 | RETVAL = magic_file (cookie, path); |
|
|
257 | perlinterp_acquire (); |
244 | } |
258 | } |
245 | OUTPUT: |
259 | OUTPUT: |
246 | RETVAL |
260 | RETVAL |
247 | |
261 | |
248 | # missing/broken in Gtk2 perl module |
262 | # missing/broken in Gtk2 perl module |
… | |
… | |
264 | RETVAL |
278 | RETVAL |
265 | |
279 | |
266 | GdkPixbuf_noinc * |
280 | GdkPixbuf_noinc * |
267 | dealpha_expose (GdkPixbuf *pb) |
281 | dealpha_expose (GdkPixbuf *pb) |
268 | CODE: |
282 | CODE: |
|
|
283 | perlinterp_release (); |
269 | { |
284 | { |
270 | int w = gdk_pixbuf_get_width (pb); |
285 | int w = gdk_pixbuf_get_width (pb); |
271 | int h = gdk_pixbuf_get_height (pb); |
286 | int h = gdk_pixbuf_get_height (pb); |
272 | int bpp = gdk_pixbuf_get_n_channels (pb); |
287 | int bpp = gdk_pixbuf_get_n_channels (pb); |
273 | int x, y, i; |
288 | int x, y, i; |
… | |
… | |
282 | for (x = 0; x < w; x++) |
297 | for (x = 0; x < w; x++) |
283 | for (y = 0; y < h; y++) |
298 | for (y = 0; y < h; y++) |
284 | for (i = 0; i < 3; i++) |
299 | for (i = 0; i < 3; i++) |
285 | 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]; |
286 | } |
301 | } |
|
|
302 | perlinterp_acquire (); |
287 | OUTPUT: |
303 | OUTPUT: |
288 | RETVAL |
304 | RETVAL |
289 | |
305 | |
290 | GdkPixbuf_noinc * |
306 | GdkPixbuf_noinc * |
291 | rotate (GdkPixbuf *pb, int angle) |
307 | rotate (GdkPixbuf *pb, int angle) |
292 | CODE: |
308 | CODE: |
|
|
309 | perlinterp_release (); |
|
|
310 | if (angle < 0) |
|
|
311 | angle += 360; |
293 | 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 |
294 | : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE |
313 | : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE |
295 | : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN |
314 | : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN |
296 | : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE |
315 | : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE |
297 | : angle); |
316 | : angle); |
|
|
317 | perlinterp_acquire (); |
298 | OUTPUT: |
318 | OUTPUT: |
299 | RETVAL |
319 | RETVAL |
300 | |
320 | |
301 | GdkPixbuf_noinc * |
321 | GdkPixbuf_noinc * |
302 | load_jpeg (SV *path, int thumbnail=0) |
322 | load_jpeg (SV *path, int thumbnail = 0, int iw = 0, int ih = 0) |
303 | CODE: |
323 | CODE: |
304 | { |
324 | { |
305 | struct jpeg_decompress_struct cinfo; |
325 | struct jpeg_decompress_struct cinfo; |
306 | struct jpg_err_mgr jerr; |
326 | struct jpg_err_mgr jerr; |
307 | guchar *data; |
327 | guchar *data; |
… | |
… | |
314 | fp = fopen (SvPVbyte_nolen (path), "rb"); |
334 | fp = fopen (SvPVbyte_nolen (path), "rb"); |
315 | |
335 | |
316 | if (!fp) |
336 | if (!fp) |
317 | XSRETURN_UNDEF; |
337 | XSRETURN_UNDEF; |
318 | |
338 | |
|
|
339 | perlinterp_release (); |
|
|
340 | |
319 | cinfo.err = jpeg_std_error (&jerr.err); |
341 | cinfo.err = jpeg_std_error (&jerr.err); |
320 | |
342 | |
321 | jerr.err.error_exit = cv_error_exit; |
343 | jerr.err.error_exit = cv_error_exit; |
322 | jerr.err.output_message = cv_error_output; |
344 | jerr.err.output_message = cv_error_output; |
323 | |
345 | |
… | |
… | |
327 | jpeg_destroy_decompress (&cinfo); |
349 | jpeg_destroy_decompress (&cinfo); |
328 | |
350 | |
329 | if (pb) |
351 | if (pb) |
330 | g_object_unref ((gpointer)pb); |
352 | g_object_unref ((gpointer)pb); |
331 | |
353 | |
|
|
354 | perlinterp_acquire (); |
332 | XSRETURN_UNDEF; |
355 | XSRETURN_UNDEF; |
333 | } |
356 | } |
334 | |
357 | |
335 | jpeg_create_decompress (&cinfo); |
358 | jpeg_create_decompress (&cinfo); |
336 | |
359 | |
… | |
… | |
352 | { |
375 | { |
353 | cinfo.dct_method = JDCT_FASTEST; |
376 | cinfo.dct_method = JDCT_FASTEST; |
354 | cinfo.do_fancy_upsampling = FALSE; |
377 | cinfo.do_fancy_upsampling = FALSE; |
355 | |
378 | |
356 | while (cinfo.scale_denom < 8 |
379 | while (cinfo.scale_denom < 8 |
357 | && cinfo.output_width >= IW*4 |
380 | && cinfo.output_width >= iw*4 |
358 | && cinfo.output_height >= IH*4) |
381 | && cinfo.output_height >= ih*4) |
359 | { |
382 | { |
360 | cinfo.scale_denom <<= 1; |
383 | cinfo.scale_denom <<= 1; |
361 | jpeg_calc_output_dimensions (&cinfo); |
384 | jpeg_calc_output_dimensions (&cinfo); |
362 | } |
385 | } |
363 | } |
386 | } |
364 | |
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 | |
365 | 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); |
366 | if (!RETVAL) |
398 | if (!RETVAL) |
367 | longjmp (jerr.setjmp_buffer, 2); |
399 | longjmp (jerr.setjmp_buffer, 2); |
368 | |
400 | |
369 | data = gdk_pixbuf_get_pixels (RETVAL); |
401 | data = gdk_pixbuf_get_pixels (RETVAL); |
370 | rs = gdk_pixbuf_get_rowstride (RETVAL); |
402 | rs = gdk_pixbuf_get_rowstride (RETVAL); |
371 | |
|
|
372 | if (cinfo.output_components != 3) |
|
|
373 | longjmp (jerr.setjmp_buffer, 3); |
|
|
374 | |
403 | |
375 | jpeg_start_decompress (&cinfo); |
404 | jpeg_start_decompress (&cinfo); |
376 | |
405 | |
377 | while (cinfo.output_scanline < cinfo.output_height) |
406 | while (cinfo.output_scanline < cinfo.output_height) |
378 | { |
407 | { |
… | |
… | |
385 | rp [3] = (guchar *)rp [2] + rs; |
414 | rp [3] = (guchar *)rp [2] + rs; |
386 | |
415 | |
387 | jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4); |
416 | jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4); |
388 | } |
417 | } |
389 | |
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 | |
390 | jpeg_finish_decompress (&cinfo); |
448 | jpeg_finish_decompress (&cinfo); |
391 | fclose (fp); |
449 | fclose (fp); |
392 | jpeg_destroy_decompress (&cinfo); |
450 | jpeg_destroy_decompress (&cinfo); |
|
|
451 | perlinterp_acquire (); |
393 | } |
452 | } |
394 | OUTPUT: |
453 | OUTPUT: |
395 | RETVAL |
454 | RETVAL |
396 | |
455 | |
397 | void |
456 | void |
398 | compare (GdkPixbuf *a, GdkPixbuf *b) |
457 | compare (GdkPixbuf *a, GdkPixbuf *b) |
399 | PPCODE: |
458 | PPCODE: |
|
|
459 | perlinterp_release (); |
400 | { |
460 | { |
401 | int w = gdk_pixbuf_get_width (a); |
461 | int w = gdk_pixbuf_get_width (a); |
402 | int h = gdk_pixbuf_get_height (a); |
462 | int h = gdk_pixbuf_get_height (a); |
|
|
463 | |
403 | int sa = gdk_pixbuf_get_rowstride (a); |
464 | int sa = gdk_pixbuf_get_rowstride (a); |
404 | int sb = gdk_pixbuf_get_rowstride (b); |
465 | int sb = gdk_pixbuf_get_rowstride (b); |
405 | |
466 | |
406 | guchar *pa = gdk_pixbuf_get_pixels (a); |
467 | guchar *pa = gdk_pixbuf_get_pixels (a); |
407 | guchar *pb = gdk_pixbuf_get_pixels (b); |
468 | guchar *pb = gdk_pixbuf_get_pixels (b); |
… | |
… | |
430 | 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)); |
431 | 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)); |
432 | 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)); |
433 | } |
494 | } |
434 | } |
495 | } |
|
|
496 | |
|
|
497 | perlinterp_acquire (); |
435 | |
498 | |
436 | EXTEND (SP, 2); |
499 | EXTEND (SP, 2); |
437 | PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.))))); |
500 | PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.))))); |
438 | PUSHs (sv_2mortal (newSVnv (peak / 255.))); |
501 | PUSHs (sv_2mortal (newSVnv (peak / 255.))); |
439 | } |
502 | } |