ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CV/CV.xs
Revision: 1.69
Committed: Wed Jun 7 17:18:46 2023 UTC (11 months, 1 week ago) by root
Branch: MAIN
Changes since 1.68: +0 -2 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4
5 #include <string.h>
6 #include <setjmp.h>
7 #include <math.h>
8
9 #include <magic.h>
10
11 #include <jpeglib.h>
12 #include <jerror.h>
13
14 #include <glib.h>
15 #include <gtk/gtk.h>
16 #include <gdk/gdkx.h>
17 #include <gdk-pixbuf/gdk-pixbuf.h>
18
19 #include <gperl.h>
20 #include <gtk2perl.h>
21
22 #include <assert.h>
23
24 #if WEBP
25 #include <webp/demux.h>
26 #include <webp/decode.h>
27 #endif
28
29 #if JXL
30 #include <jxl/decode.h>
31 #include "jxl/thread_parallel_runner.h"
32 #endif
33
34 #include "perlmulticore.h"
35
36 #define IW 80 /* MUST match Schnauzer.pm! */
37 #define IH 60 /* MUST match Schnauzer.pm! */
38
39 #define LINELENGTH 240
40
41 #define ELLIPSIS "\xe2\x80\xa6"
42
43 typedef char *octet_string;
44
45 static magic_t magic_cookie[2]; /* !mime, mime */
46
47 struct jpg_err_mgr
48 {
49 struct jpeg_error_mgr err;
50 jmp_buf setjmp_buffer;
51 };
52
53 static void
54 cv_error_exit (j_common_ptr cinfo)
55 {
56 cinfo->err->output_message (cinfo);
57 longjmp (((struct jpg_err_mgr *)cinfo->err)->setjmp_buffer, 99);
58 }
59
60 static void
61 cv_error_output (j_common_ptr cinfo)
62 {
63 char msg[JMSG_LENGTH_MAX];
64
65 cinfo->err->format_message (cinfo, msg);
66
67 fprintf (stderr, "JPEG decoding error: %s\n", msg);
68 return;
69 }
70
71 static void
72 rgb_to_hsv (unsigned int r, unsigned int g, unsigned int b,
73 unsigned int *h, unsigned int *s, unsigned int *v)
74 {
75 unsigned int mx = r; if (g > mx) mx = g; if (b > mx) mx = b;
76 unsigned int mn = r; if (g < mn) mn = g; if (b < mn) mn = b;
77 unsigned int delta = mx - mn;
78
79 *v = mx;
80
81 *s = mx ? delta * 255 / mx : 0;
82
83 if (delta == 0)
84 *h = 0;
85 else
86 {
87 if (r == mx)
88 *h = ((int)g - (int)b) * 255 / (int)(delta * 3);
89 else if (g == mx)
90 *h = ((int)b - (int)r) * 255 / (int)(delta * 3) + 52;
91 else if (b == mx)
92 *h = ((int)r - (int)g) * 255 / (int)(delta * 3) + 103;
93
94 *h &= 255;
95 }
96 }
97
98 struct feature {
99 float v1, v2, v3; // mean, square, cube
100 int n;
101 };
102
103 static void
104 feature_init (struct feature *f)
105 {
106 f->v1 = 0.;
107 f->v2 = 0.;
108 f->v3 = 0.;
109 f->n = 0;
110 }
111
112 // didn't find an algorithm to neatly do mean, variance and skew in one pass.
113 // elmex ist schuld.
114 static void
115 feature_update_pass_1 (struct feature *f, unsigned int v)
116 {
117 f->v1 += v;
118 f->n += 1;
119 }
120
121 static void
122 feature_finish_pass_1 (struct feature *f)
123 {
124 if (f->n < 1)
125 return;
126
127 f->v1 /= f->n;
128 }
129
130 static void
131 feature_update_pass_2 (struct feature *f, unsigned int v)
132 {
133 float d = v - f->v1;
134
135 f->v2 += d * d;
136 f->v3 += d * d * d;
137 }
138
139 static void
140 feature_finish_pass_2 (struct feature *f)
141 {
142 if (f->n < 1)
143 return;
144
145 f->v2 /= f->n;
146 f->v3 /= f->n;
147
148 f->v1 /= 255.;
149 f->v2 /= 255. * 255.; f->v2 = sqrtf (f->v2);
150 f->v3 /= 255. * 255. * 255.; f->v3 = powf (fabsf (f->v3), 1./3.);
151 }
152
153 static guint32 a85_val;
154 static guint a85_cnt;
155 static guchar a85_buf[LINELENGTH], *a85_ptr;
156
157 static void
158 a85_init (void)
159 {
160 a85_cnt = 4;
161 a85_ptr = a85_buf;
162 }
163
164 static void
165 a85_push (PerlIO *fp, guchar c)
166 {
167 a85_val = a85_val << 8 | c;
168
169 if (!--a85_cnt)
170 {
171 a85_cnt = 4;
172 if (a85_val)
173 {
174 a85_ptr[4] = (a85_val % 85) + 33; a85_val /= 85;
175 a85_ptr[3] = (a85_val % 85) + 33; a85_val /= 85;
176 a85_ptr[2] = (a85_val % 85) + 33; a85_val /= 85;
177 a85_ptr[1] = (a85_val % 85) + 33; a85_val /= 85;
178 a85_ptr[0] = (a85_val ) + 33;
179
180 a85_ptr += 5;
181 }
182 else
183 *a85_ptr++ = 'z';
184
185 if (a85_ptr >= a85_buf + sizeof (a85_buf) - 7)
186 {
187 *a85_ptr++ = '\n';
188 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf);
189 a85_ptr = a85_buf;
190 }
191 }
192
193 }
194
195 static void
196 a85_finish (PerlIO *fp)
197 {
198 while (a85_cnt != 4)
199 a85_push (fp, 0);
200
201 *a85_ptr++ = '~'; // probably buggy end-marker
202 *a85_ptr++ = '>'; // probably buggy end-marker
203 *a85_ptr++ = '\n';
204
205 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf);
206 }
207
208 /////////////////////////////////////////////////////////////////////////////
209 // memory source for libjpeg
210
211 static void cv_ms_init (j_decompress_ptr cinfo)
212 {
213 }
214
215 static void cv_ms_term (j_decompress_ptr cinfo)
216 {
217 }
218
219 static boolean cv_ms_fill (j_decompress_ptr cinfo)
220 {
221 // unexpected EOF, warn and generate fake EOI marker
222
223 WARNMS (cinfo, JWRN_JPEG_EOF);
224
225 struct jpeg_source_mgr *src = (struct jpeg_source_mgr *)cinfo->src;
226
227 static const JOCTET eoi[] = { 0xFF, JPEG_EOI };
228
229 src->next_input_byte = eoi;
230 src->bytes_in_buffer = sizeof (eoi);
231
232 return TRUE;
233 }
234
235 static void cv_ms_skip (j_decompress_ptr cinfo, long num_bytes)
236 {
237 struct jpeg_source_mgr *src = (struct jpeg_source_mgr *)cinfo->src;
238
239 src->next_input_byte += num_bytes;
240 src->bytes_in_buffer -= num_bytes;
241 }
242
243 static void cv_jpeg_mem_src (j_decompress_ptr cinfo, void *buf, size_t buflen)
244 {
245 struct jpeg_source_mgr *src;
246
247 if (!cinfo->src)
248 cinfo->src = (struct jpeg_source_mgr *)
249 (*cinfo->mem->alloc_small) (
250 (j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof (struct jpeg_source_mgr)
251 );
252
253 src = (struct jpeg_source_mgr *)cinfo->src;
254 src->init_source = cv_ms_init;
255 src->fill_input_buffer = cv_ms_fill;
256 src->skip_input_data = cv_ms_skip;
257 src->resync_to_restart = jpeg_resync_to_restart;
258 src->term_source = cv_ms_term;
259 src->next_input_byte = (JOCTET *)buf;
260 src->bytes_in_buffer = buflen;
261 }
262
263 /////////////////////////////////////////////////////////////////////////////
264
265 /* great, the jpeg-xl reference implementaton requires us to parse bmff files */
266
267 struct bmff_box
268 {
269 char type[4];
270 const uint8_t *ptr;
271 size_t size;
272 };
273
274 static int
275 bmff_parse_box (struct bmff_box *box, const uint8_t **next_in, size_t *avail_in)
276 {
277 if (*avail_in < 8)
278 return 0;
279
280 box->size = ((*next_in)[0] << 24)
281 | ((*next_in)[1] << 16)
282 | ((*next_in)[2] << 8)
283 | ((*next_in)[3] );
284
285 if (box->size < 8)
286 return 0;
287
288 if (*avail_in < box->size)
289 return 0;
290
291 memcpy (box->type, *next_in + 4, 4);
292 box->ptr = *next_in + 8;
293
294 *next_in += box->size;
295 *avail_in -= box->size;
296
297 box->size -= 8;
298
299 return 1;
300 }
301
302 /////////////////////////////////////////////////////////////////////////////
303
304 MODULE = Gtk2::CV PACKAGE = Gtk2::CV
305
306 PROTOTYPES: ENABLE
307
308 void
309 _exit (int code)
310
311 # calculate the common prefix length of two strings
312 # missing function in perl. really :)
313 int
314 common_prefix_length (a, b)
315 unsigned char *a = (unsigned char *)SvPVutf8_nolen ($arg);
316 unsigned char *b = (unsigned char *)SvPVutf8_nolen ($arg);
317 CODE:
318 RETVAL = 0;
319
320 while (*a == *b && *a)
321 {
322 RETVAL += (*a & 0xc0) != 0x80;
323 a++, b++;
324 }
325
326 OUTPUT:
327 RETVAL
328
329 const char *
330 magic (SV *path_or_data)
331 ALIAS:
332 magic = 0
333 magic_mime = 1
334 magic_buffer = 2
335 magic_buffer_mime = 3
336 CODE:
337 {
338 STRLEN len;
339 char *data = SvPVbyte (path_or_data, len);
340
341 if (!magic_cookie[0])
342 {
343 magic_cookie[0] = magic_open (MAGIC_SYMLINK);
344 magic_cookie[1] = magic_open (MAGIC_SYMLINK | MAGIC_MIME_TYPE);
345 magic_load (magic_cookie[0], 0);
346 magic_load (magic_cookie[1], 0);
347 }
348
349 perlinterp_release ();
350
351 RETVAL = ix & 2
352 ? magic_buffer (magic_cookie[ix & 1], data, len)
353 : magic_file (magic_cookie[ix & 1], data);
354
355 perlinterp_acquire ();
356 }
357 OUTPUT:
358 RETVAL
359
360 # missing/broken in Gtk2 perl module
361
362 void
363 gdk_window_clear_hints (GdkWindow *window)
364 CODE:
365 gdk_window_set_geometry_hints (window, 0, 0);
366
367 gboolean
368 gdk_net_wm_supports (GdkAtom property)
369 CODE:
370 #if defined(GDK_WINDOWING_X11) && !defined(GDK_MULTIHEAD_SAFE)
371 RETVAL = gdk_net_wm_supports (property);
372 #else
373 RETVAL = 0;
374 #endif
375 OUTPUT:
376 RETVAL
377
378 GdkPixbuf_noinc *
379 dealpha_expose (GdkPixbuf *pb)
380 CODE:
381 perlinterp_release ();
382 {
383 int w = gdk_pixbuf_get_width (pb);
384 int h = gdk_pixbuf_get_height (pb);
385 int bpp = gdk_pixbuf_get_n_channels (pb);
386 int x, y, i;
387 guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
388 int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
389
390 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
391
392 dst = gdk_pixbuf_get_pixels (RETVAL);
393 dstr = gdk_pixbuf_get_rowstride (RETVAL);
394
395 for (x = 0; x < w; x++)
396 for (y = 0; y < h; y++)
397 for (i = 0; i < 3; i++)
398 dst[x * 3 + y * dstr + i] = src[x * bpp + y * sstr + i];
399 }
400 perlinterp_acquire ();
401 OUTPUT:
402 RETVAL
403
404 GdkPixbuf_noinc *
405 rotate (GdkPixbuf *pb, int angle)
406 CODE:
407 perlinterp_release ();
408 if (angle < 0)
409 angle += 360;
410 RETVAL = gdk_pixbuf_rotate_simple (pb, angle == 0 ? GDK_PIXBUF_ROTATE_NONE
411 : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE
412 : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN
413 : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE
414 : angle);
415 perlinterp_acquire ();
416 OUTPUT:
417 RETVAL
418
419 const char *
420 filetype (SV *image_data)
421 CODE:
422 {
423 STRLEN data_len;
424 U8 *data = SvPVbyte (image_data, data_len);
425 static const unsigned char jxl_header[] = {
426 0, 0, 0, 0x0c, 0x4a, 0x58, 0x4c, 0x20, 0x0d, 0xa, 0x87, 0x0a
427 };
428
429 if (data_len >= 20
430 && data[0] == 0xff
431 && data[1] == 0xd8
432 && data[2] == 0xff)
433 RETVAL = "image/jpeg";
434 else if (data_len >= 12
435 && data[ 0] == (U8)'R'
436 && data[ 1] == (U8)'I'
437 && data[ 2] == (U8)'F'
438 && data[ 3] == (U8)'F'
439 && data[ 8] == (U8)'W'
440 && data[ 9] == (U8)'E'
441 && data[10] == (U8)'B'
442 && data[11] == (U8)'P')
443 RETVAL = "image/webp";
444 else if (data_len >= 16
445 && data[ 0] == 0x89
446 && data[ 1] == (U8)'P'
447 && data[ 2] == (U8)'N'
448 && data[ 3] == (U8)'G'
449 && data[ 4] == 0x0d
450 && data[ 5] == 0x0a
451 && data[ 6] == 0x1a
452 && data[ 7] == 0x0a)
453 RETVAL = "image/png";
454 else if (data_len >= sizeof (jxl_header) && memcmp (data, jxl_header, sizeof (jxl_header)) == 0)
455 RETVAL = "image/jxl"; // todo: might want to use JxlSignatureCheck
456 else if (data_len >= 2
457 && data[0] == 0xff
458 && data[1] == 0x0a)
459 RETVAL = "image/jxl";
460 else if (data_len >= 13
461 && data[0] == 'G'
462 && data[1] == 'I'
463 && data[2] == 'F'
464 && data[3] == '8'
465 //&& (data[4] == '7' || data[4] == '9')
466 && data[5] == 'a')
467 {
468 RETVAL = "image/gif";
469
470 // now see if its animated - we require the netscape application header for this
471 int ofs = 13;
472
473 if (data[10] & 0x80)
474 ofs += (1 << ((data[10] & 7) + 1)) * 3;
475
476 if (data_len >= ofs + 2 + 1 + 11)
477 {
478
479 // skip a graphic control extension block. we assume
480 // there is at most one such block - while the NAB
481 // has to come firstz, some files do not obey this
482 if (data[ofs] == 0x21 && data[ofs + 1] == 0xf9)
483 ofs += 3 + data[ofs + 2] + 1;
484
485 if (data_len >= ofs + 2 + 1 + 11)
486 if (!memcmp (data + ofs, "\x21\xff\x0bNETSCAPE2.0", sizeof ("\x21\xff\x0bNETSCAPE2.0") - 1))
487 RETVAL = "video/gif";
488 }
489 }
490 else if (data_len >= 0x8000 + 6
491 && data[0x8000+1] == (U8)'B'
492 && data[0x8000+2] == (U8)'E'
493 && data[0x8000+3] == (U8)'A'
494 && data[0x8000+4] == (U8)'0'
495 && data[0x8000+5] == (U8)'1')
496 RETVAL = "video/iso-bluray";
497 else if (data_len >= 0x8000 + 6
498 && data[0x8000+1] == (U8)'C'
499 && data[0x8000+2] == (U8)'D'
500 && data[0x8000+3] == (U8)'0'
501 && data[0x8000+4] == (U8)'0'
502 && data[0x8000+5] == (U8)'1')
503 RETVAL = "video/iso9660";
504
505 else
506 XSRETURN_UNDEF;
507 }
508 OUTPUT:
509 RETVAL
510
511 GdkPixbuf_noinc *
512 decode_webp (SV *image_data, int thumbnail = 0, int iw = 0, int ih = 0)
513 CODE:
514 {
515 #if WEBP
516 STRLEN data_size;
517 int alpha;
518 WebPData data;
519 WebPDemuxer *demux;
520 WebPIterator iter;
521 WebPDecoderConfig config;
522 int inw, inh;
523
524 data.bytes = (uint8_t *)SvPVbyte (image_data, data_size);
525 data.size = data_size;
526
527 perlinterp_release ();
528
529 RETVAL = 0;
530
531 if (!(demux = WebPDemux (&data)))
532 goto err_demux;
533
534 if (!WebPDemuxGetFrame (demux, 1, &iter))
535 goto err_iter;
536
537 if (!WebPInitDecoderConfig (&config))
538 goto err_iter;
539
540 config.options.use_threads = 1;
541
542 if (WebPGetFeatures (iter.fragment.bytes, iter.fragment.size, &config.input) != VP8_STATUS_OK)
543 goto err_iter;
544
545 inw = config.input.width;
546 inh = config.input.height;
547
548 if (thumbnail)
549 {
550 if (inw * ih > inh * iw)
551 ih = (iw * inh + inw - 1) / inw;
552 else
553 iw = (ih * inw + inh - 1) / inh;
554
555 config.options.bypass_filtering = 1;
556 config.options.no_fancy_upsampling = 1;
557
558 config.options.use_scaling = 1;
559 config.options.scaled_width = iw;
560 config.options.scaled_height = ih;
561 }
562 else
563 {
564 iw = inw;
565 ih = inh;
566 }
567
568 alpha = !!config.input.has_alpha;
569
570 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, alpha, 8, iw, ih);
571 if (!RETVAL)
572 goto err_iter;
573
574 config.output.colorspace = alpha ? MODE_RGBA : MODE_RGB;
575 config.output.u.RGBA.rgba = gdk_pixbuf_get_pixels (RETVAL);
576 config.output.u.RGBA.stride = gdk_pixbuf_get_rowstride (RETVAL);
577 config.output.u.RGBA.size = gdk_pixbuf_get_byte_length (RETVAL);
578 config.output.is_external_memory = 1;
579
580 if (WebPDecode (iter.fragment.bytes, iter.fragment.size, &config) != VP8_STATUS_OK)
581 {
582 g_object_unref (RETVAL);
583 RETVAL = 0;
584 goto err_iter;
585 }
586
587 err_iter:
588 WebPDemuxReleaseIterator (&iter);
589 err_demux:
590 WebPDemuxDelete (demux);
591
592 perlinterp_acquire ();
593 #else
594 croak ("load_webp: webp not enabled at compile time");
595 #endif
596 }
597 OUTPUT:
598 RETVAL
599
600 GdkPixbuf_noinc *
601 decode_jxl (SV *image_data, int thumbnail = 0, int iw = 0, int ih = 0)
602 CODE:
603 {
604 #if JXL
605 JxlDecoder *dec;
606 JxlBasicInfo info;
607 const uint8_t *next_in = (uint8_t *)SvPVbyte_nolen (image_data);
608 size_t avail_in = SvCUR (image_data);
609 const char *error = 0;
610 JxlDecoderStatus status;
611 static void *runner_cache;
612 void *runner = 0;
613
614 RETVAL = 0;
615
616 if (runner_cache)
617 runner = runner_cache;
618 else
619 runner = JxlThreadParallelRunnerCreate (0, JxlThreadParallelRunnerDefaultNumWorkerThreads ());
620
621 runner_cache = 0;
622
623 perlinterp_release ();
624
625 dec = JxlDecoderCreate (0);
626
627 error = "JxlDecoderCreate failed";
628 if (!dec)
629 goto done;
630
631 status = JxlDecoderSetParallelRunner (dec, JxlThreadParallelRunner, runner);
632 error = "JxlDecoderSetParallelRunner failed";
633 if (status != JXL_DEC_SUCCESS)
634 goto done;
635
636 error = "JxlDecoderSubscribeEvents failed";
637 status = JxlDecoderSubscribeEvents (dec, JXL_DEC_BASIC_INFO | JXL_DEC_FULL_IMAGE);
638 if (status != JXL_DEC_SUCCESS)
639 goto done;
640
641 status = JxlDecoderSetInput (dec, next_in, avail_in);
642 error = "JxlDecoderSetInput failed";
643 if (status != JXL_DEC_SUCCESS)
644 goto done;
645
646 for (;;)
647 {
648 status = JxlDecoderProcessInput (dec);
649
650 switch (status)
651 {
652 case JXL_DEC_FULL_IMAGE:
653 error = 0;
654 goto done;
655
656 case JXL_DEC_ERROR:
657 error = "JxlDecoderProcessInput failed";
658 goto done;
659
660 case JXL_DEC_NEED_MORE_INPUT:
661 error = "incomplete file";
662 goto done;
663
664 case JXL_DEC_SUCCESS:
665 error = "incomplete decode";
666 goto done;
667
668 case JXL_DEC_BASIC_INFO:
669 {
670 status = JxlDecoderGetBasicInfo (dec, &info);
671 error = "JxlDecoderGetBasicInfo failed";
672 if (status != JXL_DEC_SUCCESS)
673 goto done;
674
675 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, !!info.alpha_bits, 8, info.xsize, info.ysize);
676 error = "unable to allocate pixbuf";
677 if (!RETVAL)
678 goto done;
679
680 JxlPixelFormat format = {
681 info.alpha_bits ? 4 : 3,
682 JXL_TYPE_UINT8,
683 JXL_NATIVE_ENDIAN,
684 gdk_pixbuf_get_rowstride (RETVAL)
685 };
686
687 // cannot use gdk_pixbuf_get_byte_length because that does
688 // not return the size of the buffer, but the size of the buffer without
689 // the last padding bytes. the internal buffer is rowstride * ysize,
690 // and this is what the jxl decoder needs. none of this is documented
691 // in either library, of course.
692
693 status = JxlDecoderSetImageOutBuffer (
694 dec,
695 &format,
696 gdk_pixbuf_get_pixels (RETVAL),
697 gdk_pixbuf_get_rowstride (RETVAL) * info.ysize
698 );
699 error = "JxlDecoderSetImageOutBuffer failed";
700 if (status != JXL_DEC_SUCCESS)
701 goto done;
702 }
703 break;
704
705 default:
706 error = "unexpected event";
707 goto done;
708 }
709 }
710
711 done:
712 if (dec)
713 JxlDecoderDestroy (dec);
714
715 perlinterp_acquire ();
716
717 if (runner_cache)
718 JxlThreadParallelRunnerDestroy (runner);
719
720 runner_cache = runner;
721
722 if (error)
723 {
724 if (RETVAL)
725 g_object_unref (RETVAL);
726
727 croak ("load_jxl: %s (status %d)", error, status);
728 }
729 #else
730 croak ("load_jxl: jpeg-xl not enabled at compile time");
731 #endif
732 }
733 OUTPUT:
734 RETVAL
735
736 GdkPixbuf_noinc *
737 decode_jpeg (SV *image_data, int thumbnail = 0, int iw = 0, int ih = 0)
738 CODE:
739 {
740 struct jpeg_decompress_struct cinfo;
741 struct jpg_err_mgr jerr;
742 int rs;
743 volatile GdkPixbuf *pb = 0;
744 STRLEN data_len;
745 guchar *data = SvPVbyte (image_data, data_len);
746
747 RETVAL = 0;
748
749 perlinterp_release ();
750
751 cinfo.err = jpeg_std_error (&jerr.err);
752
753 jerr.err.error_exit = cv_error_exit;
754 jerr.err.output_message = cv_error_output;
755
756 if ((rs = setjmp (jerr.setjmp_buffer)))
757 {
758 jpeg_destroy_decompress (&cinfo);
759
760 if (pb)
761 g_object_unref ((gpointer)pb);
762
763 perlinterp_acquire ();
764 XSRETURN_UNDEF;
765 }
766
767 if (!data_len)
768 longjmp (jerr.setjmp_buffer, 4);
769
770 jpeg_create_decompress (&cinfo);
771 cv_jpeg_mem_src (&cinfo, data, data_len);
772
773 jpeg_read_header (&cinfo, TRUE);
774
775 cinfo.dct_method = JDCT_DEFAULT;
776 cinfo.do_fancy_upsampling = FALSE; /* worse quality, but nobody compained so far, and gdk-pixbuf does the same */
777 cinfo.do_block_smoothing = FALSE;
778 cinfo.out_color_space = JCS_RGB;
779 cinfo.quantize_colors = FALSE;
780
781 cinfo.scale_num = 1;
782 cinfo.scale_denom = 1;
783
784 jpeg_calc_output_dimensions (&cinfo);
785
786 if (thumbnail)
787 {
788 cinfo.dct_method = JDCT_FASTEST;
789 cinfo.do_fancy_upsampling = FALSE;
790
791 while (cinfo.scale_denom < 8
792 && cinfo.output_width >= iw*4
793 && cinfo.output_height >= ih*4)
794 {
795 cinfo.scale_denom <<= 1;
796 jpeg_calc_output_dimensions (&cinfo);
797 }
798 }
799
800 if (cinfo.output_components != 3)
801 longjmp (jerr.setjmp_buffer, 3);
802
803 if (cinfo.jpeg_color_space == JCS_YCCK || cinfo.jpeg_color_space == JCS_CMYK)
804 {
805 cinfo.out_color_space = JCS_CMYK;
806 cinfo.output_components = 4;
807 }
808
809 pb = RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, cinfo.output_components == 4, 8, cinfo.output_width, cinfo.output_height);
810 if (!RETVAL)
811 longjmp (jerr.setjmp_buffer, 2);
812
813 data = gdk_pixbuf_get_pixels (RETVAL);
814 rs = gdk_pixbuf_get_rowstride (RETVAL);
815
816 jpeg_start_decompress (&cinfo);
817
818 while (cinfo.output_scanline < cinfo.output_height)
819 {
820 int remaining = cinfo.output_height - cinfo.output_scanline;
821 JSAMPROW rp[4];
822
823 rp [0] = data + cinfo.output_scanline * rs;
824 rp [1] = (guchar *)rp [0] + rs;
825 rp [2] = (guchar *)rp [1] + rs;
826 rp [3] = (guchar *)rp [2] + rs;
827
828 jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4);
829 }
830
831 if (cinfo.out_color_space == JCS_CMYK)
832 {
833 guchar *end = data + cinfo.output_height * rs;
834
835 while (data < end)
836 {
837 U32 c = data [0];
838 U32 m = data [1];
839 U32 y = data [2];
840 U32 k = data [3];
841
842 if (0)
843 if (cinfo.Adobe_transform == 2)
844 {
845 c ^= 0xff;
846 m ^= 0xff;
847 y ^= 0xff;
848 k ^= 0xff;
849 }
850
851 data [0] = (c * k + 0x80) / 0xff;
852 data [1] = (m * k + 0x80) / 0xff;
853 data [2] = (y * k + 0x80) / 0xff;
854 data [3] = 0xff;
855
856 data += 4;
857 }
858 }
859
860 jpeg_finish_decompress (&cinfo);
861 jpeg_destroy_decompress (&cinfo);
862 perlinterp_acquire ();
863 }
864 OUTPUT:
865 RETVAL
866
867 void
868 compare (GdkPixbuf *a, GdkPixbuf *b)
869 PPCODE:
870 perlinterp_release ();
871 {
872 int w = gdk_pixbuf_get_width (a);
873 int h = gdk_pixbuf_get_height (a);
874
875 int sa = gdk_pixbuf_get_rowstride (a);
876 int sb = gdk_pixbuf_get_rowstride (b);
877
878 guchar *pa = gdk_pixbuf_get_pixels (a);
879 guchar *pb = gdk_pixbuf_get_pixels (b);
880
881 int x, y;
882
883 assert (w == gdk_pixbuf_get_width (b));
884 assert (h == gdk_pixbuf_get_height (b));
885
886 assert (gdk_pixbuf_get_n_channels (a) == 3);
887 assert (gdk_pixbuf_get_n_channels (b) == 3);
888
889 double diff = 0.;
890 int peak = 0;
891
892 if (w && h)
893 for (y = 0; y < h; y++)
894 {
895 guchar *pa_ = pa + y * sa;
896 guchar *pb_ = pb + y * sb;
897
898 for (x = 0; x < w; x++)
899 {
900 int d;
901
902 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
903 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
904 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
905 }
906 }
907
908 perlinterp_acquire ();
909
910 EXTEND (SP, 2);
911 PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.)))));
912 PUSHs (sv_2mortal (newSVnv (peak / 255.)));
913 }
914
915 #############################################################################
916
917 MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer
918
919 # currently only works for filenames (octet strings)
920
921 SV *
922 foldcase (SV *pathsv)
923 PROTOTYPE: $
924 CODE:
925 {
926 STRLEN plen;
927 U8 *path = (U8 *)SvPV (pathsv, plen);
928 U8 *pend = path + plen;
929 U8 dst [plen * 8 * 3], *dstp = dst;
930
931 while (path < pend)
932 {
933 U8 ch = *path;
934
935 if (ch >= 'a' && ch <= 'z')
936 *dstp++ = *path++;
937 else if (ch >= 'A' && ch <= 'Z')
938 *dstp++ = *path++ + ('a' - 'A');
939 else if (ch >= '0' && ch <= '9')
940 {
941 /* version sort, up to 8 digits */
942 STRLEN el, nl = 0;
943 while (*path >= '0' && *path <= '9' && path < pend)
944 path++, nl++;
945
946 for (el = nl; el < 8; el++)
947 *dstp++ = '0';
948
949 memcpy (dstp, path - nl, nl);
950 dstp += nl;
951 }
952 else
953 *dstp++ = *path++;
954 #if 0
955 else
956 {
957 STRLEN cl;
958 to_utf8_fold (path, dstp, &cl);
959 dstp += cl;
960 path += is_utf8_char (path);
961 }
962 #endif
963 }
964
965 RETVAL = newSVpvn ((const char *)dst, dstp - dst);
966 }
967 OUTPUT:
968 RETVAL
969
970 GdkPixbuf_noinc *
971 p7_to_pb (int w, int h, SV *src_sv)
972 PROTOTYPE: @
973 CODE:
974 {
975 int x, y;
976 guchar *dst, *d;
977 int dstr;
978 guchar *src = (guchar *)SvPVbyte_nolen (src_sv);
979
980 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
981 dst = gdk_pixbuf_get_pixels (RETVAL);
982 dstr = gdk_pixbuf_get_rowstride (RETVAL);
983
984 for (y = 0; y < h; y++)
985 for (d = dst + y * dstr, x = 0; x < w; x++)
986 {
987 *d++ = (((*src >> 5) & 7) * 255 + 4) / 7;
988 *d++ = (((*src >> 2) & 7) * 255 + 4) / 7;
989 *d++ = (((*src >> 0) & 3) * 255 + 2) / 3;
990
991 src++;
992 }
993 }
994 OUTPUT:
995 RETVAL
996
997 #############################################################################
998
999 MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
1000
1001 void
1002 dump_ascii85 (PerlIO *fp, GdkPixbuf *pb)
1003 CODE:
1004 {
1005 int w = gdk_pixbuf_get_width (pb);
1006 int h = gdk_pixbuf_get_height (pb);
1007 int x, y, i;
1008 guchar *dst;
1009 int bpp = gdk_pixbuf_get_n_channels (pb);
1010 guchar *src = gdk_pixbuf_get_pixels (pb);
1011 int sstr = gdk_pixbuf_get_rowstride (pb);
1012
1013 a85_init ();
1014
1015 for (y = 0; y < h; y++)
1016 for (x = 0; x < w; x++)
1017 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
1018 a85_push (fp, src [x * bpp + y * sstr + i]);
1019
1020 a85_finish (fp);
1021 }
1022
1023 void
1024 dump_binary (PerlIO *fp, GdkPixbuf *pb)
1025 CODE:
1026 {
1027 int w = gdk_pixbuf_get_width (pb);
1028 int h = gdk_pixbuf_get_height (pb);
1029 int x, y, i;
1030 guchar *dst;
1031 int bpp = gdk_pixbuf_get_n_channels (pb);
1032 guchar *src = gdk_pixbuf_get_pixels (pb);
1033 int sstr = gdk_pixbuf_get_rowstride (pb);
1034
1035 for (y = 0; y < h; y++)
1036 for (x = 0; x < w; x++)
1037 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
1038 PerlIO_putc (fp, src [x * bpp + y * sstr + i]);
1039 }
1040
1041 #############################################################################
1042
1043 MODULE = Gtk2::CV PACKAGE = Gtk2::CV
1044
1045 SV *
1046 pb_to_hv84 (GdkPixbuf *pb)
1047 CODE:
1048 {
1049 int w = gdk_pixbuf_get_width (pb);
1050 int h = gdk_pixbuf_get_height (pb);
1051 int x, y;
1052 guchar *dst;
1053 int bpp = gdk_pixbuf_get_n_channels (pb);
1054 guchar *src = gdk_pixbuf_get_pixels (pb);
1055 int sstr = gdk_pixbuf_get_rowstride (pb);
1056
1057 RETVAL = newSV (6 * 8 * 12 / 8);
1058 SvPOK_only (RETVAL);
1059 SvCUR_set (RETVAL, 6 * 8 * 12 / 8);
1060
1061 dst = (guchar *)SvPVX (RETVAL);
1062
1063 /* some primitive error distribution + random dithering */
1064
1065 for (y = 0; y < h; y++)
1066 {
1067 guchar *p = src + y * sstr;
1068
1069 for (x = 0; x < w; x += 2)
1070 {
1071 unsigned int r, g, b, h, s, v, H, V1, V2;
1072
1073 if (bpp == 3)
1074 r = *p++, g = *p++, b = *p++;
1075 else if (bpp == 1)
1076 r = g = b = *p++;
1077 else
1078 abort ();
1079
1080 rgb_to_hsv (r, g, b, &h, &s, &v);
1081
1082 H = (h * 15 / 255) << 4;
1083 V1 = v;
1084
1085 if (bpp == 3)
1086 r = *p++, g = *p++, b = *p++;
1087 else if (bpp == 1)
1088 r = g = b = *p++;
1089 else
1090 abort ();
1091
1092 rgb_to_hsv (r, g, b, &h, &s, &v);
1093
1094 H |= h * 15 / 255;
1095 V2 = v;
1096
1097 *dst++ = H;
1098 *dst++ = V1;
1099 *dst++ = V2;
1100 }
1101 }
1102 }
1103 OUTPUT:
1104 RETVAL
1105
1106 SV *
1107 hv84_to_av (unsigned char *hv84)
1108 CODE:
1109 {
1110 int i = 72 / 3;
1111 AV *av = newAV ();
1112
1113 RETVAL = (SV *)newRV_noinc ((SV *)av);
1114 while (i--)
1115 {
1116 int h = *hv84++;
1117 int v1 = *hv84++;
1118 int v2 = *hv84++;
1119
1120 av_push (av, newSViv (v1));
1121 av_push (av, newSViv ((h >> 4) * 255 / 15));
1122 av_push (av, newSViv (v2));
1123 av_push (av, newSViv ((h & 15) * 255 / 15));
1124 }
1125 }
1126 OUTPUT:
1127 RETVAL
1128
1129 #############################################################################
1130
1131 MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Plugin::RCluster
1132
1133 SV *
1134 extract_features (SV *ar)
1135 CODE:
1136 {
1137 int i;
1138 AV *av, *result;
1139
1140 if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV)
1141 croak ("Not an array ref as first argument to extract_features");
1142
1143 av = (AV *) SvRV (ar);
1144 result = newAV ();
1145
1146 for (i = 0; i <= av_len (av); ++i)
1147 {
1148 SV *sv = *av_fetch (av, i, 1);
1149 SV *histsv = newSV (9 * sizeof (float) + 1);
1150
1151 SvPOK_on (histsv);
1152 SvCUR_set (histsv, 9 * sizeof (float));
1153 float *hist = (float *)SvPVX (histsv);
1154
1155 struct feature f_h, f_s, f_v;
1156 feature_init (&f_h);
1157 feature_init (&f_s);
1158 feature_init (&f_v);
1159
1160 {
1161 STRLEN len;
1162 unsigned char *buf = (unsigned char *)SvPVbyte (sv, len);
1163 while (len >= 3)
1164 {
1165 unsigned int r, g, b, h, s, v;
1166 r = *buf++; g = *buf++; b = *buf++;
1167 rgb_to_hsv (r, g, b, &h, &s, &v);
1168
1169 feature_update_pass_1 (&f_h, h);
1170 feature_update_pass_1 (&f_s, s);
1171 feature_update_pass_1 (&f_v, v);
1172
1173 len -= 3;
1174 }
1175
1176 feature_finish_pass_1 (&f_h);
1177 feature_finish_pass_1 (&f_s);
1178 feature_finish_pass_1 (&f_v);
1179 }
1180
1181 {
1182 STRLEN len;
1183 unsigned char *buf = (unsigned char *)SvPVbyte (sv, len);
1184 while (len >= 3)
1185 {
1186 unsigned int r, g, b, h, s, v;
1187 r = *buf++; g = *buf++; b = *buf++;
1188 rgb_to_hsv (r, g, b, &h, &s, &v);
1189
1190 feature_update_pass_2 (&f_h, h);
1191 feature_update_pass_2 (&f_s, s);
1192 feature_update_pass_2 (&f_v, v);
1193
1194 len -= 3;
1195 }
1196
1197 feature_finish_pass_2 (&f_h);
1198 feature_finish_pass_2 (&f_s);
1199 feature_finish_pass_2 (&f_v);
1200 }
1201
1202 hist [0] = f_h.v1 * 2.; hist [1] = f_h.v2 * 2.; hist [2] = f_h.v3 * 2.;
1203 hist [3] = f_s.v1 ; hist [4] = f_s.v2 ; hist [5] = f_s.v3 ;
1204 hist [6] = f_v.v1 * .5; hist [7] = f_v.v2 * .5; hist [8] = f_v.v3 * .5;
1205
1206 av_push (result, histsv);
1207 }
1208
1209 RETVAL = newRV_noinc ((SV *)result);
1210 }
1211 OUTPUT:
1212 RETVAL
1213