1 | #include "EXTERN.h" |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
2 | #include "perl.h" |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
|
|
5 | #include <string.h> |
|
|
6 | #include <setjmp.h> |
|
|
7 | |
|
|
8 | #include <jpeglib.h> |
|
|
9 | #include <glib.h> |
|
|
10 | #include <gtk/gtk.h> |
5 | #include <gdk-pixbuf/gdk-pixbuf.h> |
11 | #include <gdk-pixbuf/gdk-pixbuf.h> |
6 | |
12 | |
7 | #include <gperl.h> |
13 | #include <gperl.h> |
8 | #include <gtk2perl.h> |
14 | #include <gtk2perl.h> |
9 | |
15 | |
10 | #define IW 80 |
16 | #define IW 80 /* MUST match Schnauer.pm! */ |
|
|
17 | #define IH 60 /* MUST match Schnauer.pm! */ |
11 | |
18 | |
12 | #define RAND (seed = (seed + 7141) * 54773 % 134456) |
19 | #define RAND (seed = (seed + 7141) * 54773 % 134456) |
13 | |
20 | |
14 | #define LINELENGTH 240 |
21 | #define LINELENGTH 240 |
15 | |
22 | |
16 | #define ELLIPSIS "\xe2\x80\xa6" |
23 | #define ELLIPSIS "\xe2\x80\xa6" |
|
|
24 | |
|
|
25 | struct jpg_err_mgr |
|
|
26 | { |
|
|
27 | struct jpeg_error_mgr err; |
|
|
28 | jmp_buf setjmp_buffer; |
|
|
29 | }; |
|
|
30 | |
|
|
31 | static void |
|
|
32 | cv_error_exit (j_common_ptr cinfo) |
|
|
33 | { |
|
|
34 | longjmp (((struct jpg_err_mgr *)cinfo->err)->setjmp_buffer, 99); |
|
|
35 | } |
|
|
36 | |
|
|
37 | static void |
|
|
38 | cv_error_output (j_common_ptr cinfo) |
|
|
39 | { |
|
|
40 | return; |
|
|
41 | } |
|
|
42 | |
|
|
43 | static void |
|
|
44 | rgb_to_hsv (unsigned int r, unsigned int g, unsigned int b, |
|
|
45 | unsigned int *h, unsigned int *s, unsigned int *v) |
|
|
46 | { |
|
|
47 | unsigned int mx = r; if (g > mx) mx = g; if (b > mx) mx = b; |
|
|
48 | unsigned int mn = r; if (g < mn) mn = g; if (b < mn) mn = b; |
|
|
49 | unsigned int delta = mx - mn; |
|
|
50 | |
|
|
51 | *v = mx; |
|
|
52 | |
|
|
53 | *s = mx ? delta * 255 / mx : 0; |
|
|
54 | |
|
|
55 | if (delta == 0) |
|
|
56 | *h = 0; |
|
|
57 | else |
|
|
58 | { |
|
|
59 | if (r == mx) |
|
|
60 | *h = ((int)g - (int)b) * 255 / (int)(delta * 3); |
|
|
61 | else if (g == mx) |
|
|
62 | *h = ((int)b - (int)r) * 255 / (int)(delta * 3) + 52; |
|
|
63 | else if (b == mx) |
|
|
64 | *h = ((int)r - (int)g) * 255 / (int)(delta * 3) + 103; |
|
|
65 | |
|
|
66 | *h &= 255; |
|
|
67 | } |
|
|
68 | } |
17 | |
69 | |
18 | static guint32 a85_val; |
70 | static guint32 a85_val; |
19 | static guint a85_cnt; |
71 | static guint a85_cnt; |
20 | static guchar a85_buf[LINELENGTH], *a85_ptr; |
72 | static guchar a85_buf[LINELENGTH], *a85_ptr; |
21 | |
73 | |
… | |
… | |
68 | *a85_ptr++ = '\n'; |
120 | *a85_ptr++ = '\n'; |
69 | |
121 | |
70 | PerlIO_write (fp, a85_buf, a85_ptr - a85_buf); |
122 | PerlIO_write (fp, a85_buf, a85_ptr - a85_buf); |
71 | } |
123 | } |
72 | |
124 | |
73 | static void |
|
|
74 | rgb_to_hsv (unsigned int r, unsigned int g, unsigned int b, |
|
|
75 | unsigned int *h, unsigned int *s, unsigned int *v) |
|
|
76 | { |
|
|
77 | unsigned int mx = r; if (g > mx) mx = g; if (b > mx) mx = b; |
|
|
78 | unsigned int mn = r; if (g < mn) mn = g; if (b < mn) mn = b; |
|
|
79 | unsigned int delta = mx - mn; |
|
|
80 | |
|
|
81 | *v = mx; |
|
|
82 | |
|
|
83 | *s = mx ? delta * 255 / mx : 0; |
|
|
84 | |
|
|
85 | if (delta == 0) |
|
|
86 | *h = 0; |
|
|
87 | else |
|
|
88 | { |
|
|
89 | if (r == mx) |
|
|
90 | *h = ((int)g - (int)b) * 255 / (int)(delta * 3); |
|
|
91 | else if (g == mx) |
|
|
92 | *h = ((int)b - (int)r) * 255 / (int)(delta * 3) + 52; |
|
|
93 | else if (b == mx) |
|
|
94 | *h = ((int)r - (int)g) * 255 / (int)(delta * 3) + 103; |
|
|
95 | |
|
|
96 | *h &= 255; |
|
|
97 | } |
|
|
98 | } |
|
|
99 | |
|
|
100 | ///////////////////////////////////////////////////////////////////////////// |
125 | ///////////////////////////////////////////////////////////////////////////// |
101 | |
126 | |
102 | MODULE = Gtk2::CV PACKAGE = Gtk2::CV |
127 | MODULE = Gtk2::CV PACKAGE = Gtk2::CV |
103 | |
128 | |
104 | PROTOTYPES: ENABLE |
129 | PROTOTYPES: ENABLE |
|
|
130 | |
|
|
131 | # missing in Gtk2 perl module |
|
|
132 | |
|
|
133 | gboolean |
|
|
134 | gdk_net_wm_supports (GdkAtom property) |
|
|
135 | CODE: |
|
|
136 | #if defined(GDK_WINDOWING_X11) && !defined(GDK_MULTIHEAD_SAFE) |
|
|
137 | RETVAL = gdk_net_wm_supports (property); |
|
|
138 | #else |
|
|
139 | RETVAL = 0; |
|
|
140 | #endif |
|
|
141 | OUTPUT: |
|
|
142 | RETVAL |
105 | |
143 | |
106 | GdkPixbuf_noinc * |
144 | GdkPixbuf_noinc * |
107 | transpose (GdkPixbuf *pb) |
145 | transpose (GdkPixbuf *pb) |
108 | CODE: |
146 | CODE: |
109 | { |
147 | { |
… | |
… | |
149 | dst[(w - 1 - x) * bpp + y * dstr + i] = src[x * bpp + y * sstr + i]; |
187 | dst[(w - 1 - x) * bpp + y * dstr + i] = src[x * bpp + y * sstr + i]; |
150 | } |
188 | } |
151 | OUTPUT: |
189 | OUTPUT: |
152 | RETVAL |
190 | RETVAL |
153 | |
191 | |
|
|
192 | GdkPixbuf_noinc * |
|
|
193 | load_jpeg (SV *path, int thumbnail=0) |
|
|
194 | CODE: |
|
|
195 | { |
|
|
196 | struct jpeg_decompress_struct cinfo; |
|
|
197 | struct jpg_err_mgr jerr; |
|
|
198 | guchar *data; |
|
|
199 | int rs; |
|
|
200 | FILE *fp; |
|
|
201 | volatile GdkPixbuf *pb = 0; |
|
|
202 | gchar *filename; |
|
|
203 | |
|
|
204 | RETVAL = 0; |
|
|
205 | |
|
|
206 | filename = g_filename_from_utf8 (SvPVutf8_nolen (path), -1, 0, 0, 0); |
|
|
207 | fp = fopen (filename, "rb"); |
|
|
208 | g_free (filename); |
|
|
209 | |
|
|
210 | if (!fp) |
|
|
211 | XSRETURN_UNDEF; |
|
|
212 | |
|
|
213 | cinfo.err = jpeg_std_error (&jerr.err); |
|
|
214 | |
|
|
215 | jerr.err.error_exit = cv_error_exit; |
|
|
216 | jerr.err.output_message = cv_error_output; |
|
|
217 | |
|
|
218 | if ((rs = setjmp (jerr.setjmp_buffer))) |
|
|
219 | { |
|
|
220 | fclose (fp); |
|
|
221 | jpeg_destroy_decompress (&cinfo); |
|
|
222 | |
|
|
223 | if (pb) |
|
|
224 | g_object_unref ((gpointer)pb); |
|
|
225 | |
|
|
226 | XSRETURN_UNDEF; |
|
|
227 | } |
|
|
228 | |
|
|
229 | jpeg_create_decompress (&cinfo); |
|
|
230 | |
|
|
231 | jpeg_stdio_src (&cinfo, fp); |
|
|
232 | jpeg_read_header (&cinfo, TRUE); |
|
|
233 | |
|
|
234 | cinfo.dct_method = JDCT_DEFAULT; |
|
|
235 | cinfo.do_fancy_upsampling = FALSE; /* worse quality, but nobody compained so far, and gdk-pixbuf does the same */ |
|
|
236 | cinfo.do_block_smoothing = FALSE; |
|
|
237 | cinfo.out_color_space = JCS_RGB; |
|
|
238 | cinfo.quantize_colors = FALSE; |
|
|
239 | |
|
|
240 | cinfo.scale_num = 1; |
|
|
241 | cinfo.scale_denom = 1; |
|
|
242 | |
|
|
243 | jpeg_calc_output_dimensions (&cinfo); |
|
|
244 | |
|
|
245 | if (thumbnail) |
|
|
246 | { |
|
|
247 | cinfo.dct_method = JDCT_FASTEST; |
|
|
248 | cinfo.do_fancy_upsampling = FALSE; |
|
|
249 | |
|
|
250 | while (cinfo.scale_denom < 8 |
|
|
251 | && cinfo.output_width >= IW*4 |
|
|
252 | && cinfo.output_height >= IH*4) |
|
|
253 | { |
|
|
254 | cinfo.scale_denom <<= 1; |
|
|
255 | jpeg_calc_output_dimensions (&cinfo); |
|
|
256 | } |
|
|
257 | } |
|
|
258 | |
|
|
259 | pb = RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, cinfo.output_width, cinfo.output_height); |
|
|
260 | if (!RETVAL) |
|
|
261 | longjmp (jerr.setjmp_buffer, 2); |
|
|
262 | |
|
|
263 | data = gdk_pixbuf_get_pixels (RETVAL); |
|
|
264 | rs = gdk_pixbuf_get_rowstride (RETVAL); |
|
|
265 | |
|
|
266 | if (cinfo.output_components != 3) |
|
|
267 | longjmp (jerr.setjmp_buffer, 3); |
|
|
268 | |
|
|
269 | jpeg_start_decompress (&cinfo); |
|
|
270 | |
|
|
271 | while (cinfo.output_scanline < cinfo.output_height) |
|
|
272 | { |
|
|
273 | int remaining = cinfo.output_height - cinfo.output_scanline; |
|
|
274 | JSAMPROW rp[4]; |
|
|
275 | |
|
|
276 | rp [0] = data + cinfo.output_scanline * rs; |
|
|
277 | rp [1] = (guchar *)rp [0] + rs; |
|
|
278 | rp [2] = (guchar *)rp [1] + rs; |
|
|
279 | rp [3] = (guchar *)rp [2] + rs; |
|
|
280 | |
|
|
281 | jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4); |
|
|
282 | } |
|
|
283 | |
|
|
284 | jpeg_finish_decompress (&cinfo); |
|
|
285 | fclose (fp); |
|
|
286 | jpeg_destroy_decompress (&cinfo); |
|
|
287 | } |
|
|
288 | OUTPUT: |
|
|
289 | RETVAL |
|
|
290 | |
154 | ############################################################################# |
291 | ############################################################################# |
155 | |
292 | |
156 | MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer |
293 | MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer |
|
|
294 | |
|
|
295 | SV * |
|
|
296 | foldcase (SV *pathsv) |
|
|
297 | PROTOTYPE: $ |
|
|
298 | CODE: |
|
|
299 | { |
|
|
300 | STRLEN plen; |
|
|
301 | U8 *path = SvPVutf8 (pathsv, plen); |
|
|
302 | U8 *pend = path + plen; |
|
|
303 | U8 dst [plen * 6 * 3], *dstp = dst; |
|
|
304 | |
|
|
305 | while (path < pend) |
|
|
306 | { |
|
|
307 | U8 ch = *path; |
|
|
308 | |
|
|
309 | if (ch >= 'a' && ch <= 'z') |
|
|
310 | *dstp++ = *path++; |
|
|
311 | else if (ch >= '0' && ch <= '9') |
|
|
312 | { |
|
|
313 | STRLEN el, nl = 0; |
|
|
314 | while (*path >= '0' && *path <= '9' && path < pend) |
|
|
315 | path++, nl++; |
|
|
316 | |
|
|
317 | for (el = nl; el < 6; el++) |
|
|
318 | *dstp++ = '0'; |
|
|
319 | |
|
|
320 | memcpy (dstp, path - nl, nl); |
|
|
321 | dstp += nl; |
|
|
322 | } |
|
|
323 | else |
|
|
324 | { |
|
|
325 | STRLEN cl; |
|
|
326 | to_utf8_fold (path, dstp, &cl); |
|
|
327 | dstp += cl; |
|
|
328 | path += is_utf8_char (path); |
|
|
329 | } |
|
|
330 | } |
|
|
331 | |
|
|
332 | RETVAL = newSVpvn (dst, dstp - dst); |
|
|
333 | } |
|
|
334 | OUTPUT: |
|
|
335 | RETVAL |
157 | |
336 | |
158 | GdkPixbuf_noinc * |
337 | GdkPixbuf_noinc * |
159 | p7_to_pb (int w, int h, guchar *src) |
338 | p7_to_pb (int w, int h, guchar *src) |
|
|
339 | PROTOTYPE: @ |
160 | CODE: |
340 | CODE: |
161 | { |
341 | { |
162 | int x, y; |
342 | int x, y; |
163 | guchar *dst, *d; |
343 | guchar *dst, *d; |
164 | int dstr; |
344 | int dstr; |
… | |
… | |
213 | for (x = 0; x < w; x++) |
393 | for (x = 0; x < w; x++) |
214 | { |
394 | { |
215 | int r, g, b; |
395 | int r, g, b; |
216 | guchar *p = src + x * bpp + y * sstr; |
396 | guchar *p = src + x * bpp + y * sstr; |
217 | |
397 | |
218 | r = ((p[0] + er + Er[x]) * 7 + 128) / 255; |
398 | r = ((p[0] + er + Er[x]) * 7 + (RAND & 127) + 64) / 255; |
219 | g = ((p[1] + eg + Eg[x]) * 7 + 128) / 255; |
399 | g = ((p[1] + eg + Eg[x]) * 7 + (RAND & 127) + 64) / 255; |
220 | b = ((p[2] + eb + Eb[x]) * 3 + 128) / 255; |
400 | b = ((p[2] + eb + Eb[x]) * 3 + (RAND & 127) + 64) / 255; |
221 | |
401 | |
222 | r = r > 7 ? 7 : r < 0 ? 0 : r; |
402 | r = r > 7 ? 7 : r < 0 ? 0 : r; |
223 | g = g > 7 ? 7 : g < 0 ? 0 : g; |
403 | g = g > 7 ? 7 : g < 0 ? 0 : g; |
224 | b = b > 3 ? 3 : b < 0 ? 0 : b; |
404 | b = b > 3 ? 3 : b < 0 ? 0 : b; |
225 | |
405 | |
226 | er += p[0] - (r * 255 + 4) / 7; |
406 | er += p[0] - (r * 255 + 4) / 7; |
227 | eg += p[1] - (g * 255 + 4) / 7; |
407 | eg += p[1] - (g * 255 + 4) / 7; |
228 | eb += p[2] - (b * 255 + 2) / 3; |
408 | eb += p[2] - (b * 255 + 2) / 3; |
229 | |
409 | |
230 | Er[x] = er / 2; er -= er / 2 + RAND % 7 - 3; |
410 | Er[x] = er >> 1; er -= (er + 1) >> 1; |
231 | Eg[x] = eg / 2; eg -= eg / 2 + RAND % 7 - 3; |
411 | Eg[x] = eg >> 1; eg -= (eg + 1) >> 1; |
232 | Eb[x] = eb / 2; eb -= eb / 2 + RAND % 7 - 3; |
412 | Eb[x] = eb >> 1; eb -= (eb + 1) >> 1; |
233 | |
413 | |
234 | *dst++ = r << 5 | g << 2 | b; |
414 | *dst++ = r << 5 | g << 2 | b; |
235 | } |
415 | } |
236 | } |
416 | } |
237 | } |
417 | } |
… | |
… | |
368 | } |
548 | } |
369 | } |
549 | } |
370 | OUTPUT: |
550 | OUTPUT: |
371 | RETVAL |
551 | RETVAL |
372 | |
552 | |
|
|
553 | ############################################################################# |
373 | |
554 | |
|
|
555 | MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Plugin::RCluster |
374 | |
556 | |
|
|
557 | SV * |
|
|
558 | make_histograms (SV *ar) |
|
|
559 | CODE: |
|
|
560 | { |
|
|
561 | int i; |
|
|
562 | AV *av, *result; |
|
|
563 | |
|
|
564 | if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV) |
|
|
565 | croak ("Not an array ref as first argument to make_histogram"); |
|
|
566 | |
|
|
567 | av = (AV *) SvRV (ar); |
|
|
568 | result = newAV (); |
|
|
569 | |
|
|
570 | for (i = 0; i <= av_len (av); ++i) |
|
|
571 | { |
|
|
572 | const int HISTSIZE = 64; |
|
|
573 | |
|
|
574 | int j; |
|
|
575 | SV *sv = *av_fetch (av, i, 1); |
|
|
576 | STRLEN len; |
|
|
577 | char *buf = SvPVbyte (sv, len); |
|
|
578 | |
|
|
579 | int tmphist[HISTSIZE]; |
|
|
580 | float *hist; |
|
|
581 | |
|
|
582 | SV *histsv = newSV (HISTSIZE * sizeof (float) + 1); |
|
|
583 | SvPOK_on (histsv); |
|
|
584 | SvCUR_set (histsv, HISTSIZE * sizeof (float)); |
|
|
585 | hist = (float *)SvPVX (histsv); |
|
|
586 | |
|
|
587 | Zero (tmphist, sizeof (tmphist), char); |
|
|
588 | |
|
|
589 | for (j = len; j--; ) |
|
|
590 | { |
|
|
591 | unsigned int idx |
|
|
592 | = ((*buf & 0xc0) >> 2) |
|
|
593 | | ((*buf & 0x18) >> 1) |
|
|
594 | | (*buf & 0x03); |
|
|
595 | |
|
|
596 | ++tmphist[idx]; |
|
|
597 | ++buf; |
|
|
598 | } |
|
|
599 | |
|
|
600 | for (j = 0; j < HISTSIZE; ++j) |
|
|
601 | hist[j] = (float)tmphist[j] / (len + 1e-30); |
|
|
602 | |
|
|
603 | av_push (result, histsv); |
|
|
604 | } |
|
|
605 | |
|
|
606 | RETVAL = newRV_noinc ((SV *)result); |
|
|
607 | } |
|
|
608 | OUTPUT: |
|
|
609 | RETVAL |
|
|
610 | |
|
|
611 | |