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 <setjmp.h> |
|
|
6 | |
|
|
7 | #include <jpeglib.h> |
5 | #include <gdk-pixbuf/gdk-pixbuf.h> |
8 | #include <gdk-pixbuf/gdk-pixbuf.h> |
6 | |
9 | |
7 | #include <gperl.h> |
10 | #include <gperl.h> |
8 | #include <gtk2perl.h> |
11 | #include <gtk2perl.h> |
9 | |
12 | |
10 | #define IW 80 |
13 | #define IW 80 /* MUST match Schnauer.pm! */ |
|
|
14 | #define IH 60 /* MUST match Schnauer.pm! */ |
11 | |
15 | |
12 | #define RAND (seed = (seed + 7141) * 54773 % 134456) |
16 | #define RAND (seed = (seed + 7141) * 54773 % 134456) |
13 | |
17 | |
14 | #define LINELENGTH 240 |
18 | #define LINELENGTH 240 |
15 | |
19 | |
16 | #define ELLIPSIS "\xe2\x80\xa6" |
20 | #define ELLIPSIS "\xe2\x80\xa6" |
|
|
21 | |
|
|
22 | struct jpg_err_mgr |
|
|
23 | { |
|
|
24 | struct jpeg_error_mgr err; |
|
|
25 | jmp_buf setjmp_buffer; |
|
|
26 | }; |
|
|
27 | |
|
|
28 | static void |
|
|
29 | cv_error_exit (j_common_ptr cinfo) |
|
|
30 | { |
|
|
31 | longjmp (((struct jpg_err_mgr *)cinfo->err)->setjmp_buffer, 99); |
|
|
32 | } |
|
|
33 | |
|
|
34 | static void |
|
|
35 | cv_error_output (j_common_ptr cinfo) |
|
|
36 | { |
|
|
37 | return; |
|
|
38 | } |
|
|
39 | |
|
|
40 | static void |
|
|
41 | rgb_to_hsv (unsigned int r, unsigned int g, unsigned int b, |
|
|
42 | unsigned int *h, unsigned int *s, unsigned int *v) |
|
|
43 | { |
|
|
44 | unsigned int mx = r; if (g > mx) mx = g; if (b > mx) mx = b; |
|
|
45 | unsigned int mn = r; if (g < mn) mn = g; if (b < mn) mn = b; |
|
|
46 | unsigned int delta = mx - mn; |
|
|
47 | |
|
|
48 | *v = mx; |
|
|
49 | |
|
|
50 | *s = mx ? delta * 255 / mx : 0; |
|
|
51 | |
|
|
52 | if (delta == 0) |
|
|
53 | *h = 0; |
|
|
54 | else |
|
|
55 | { |
|
|
56 | if (r == mx) |
|
|
57 | *h = ((int)g - (int)b) * 255 / (int)(delta * 3); |
|
|
58 | else if (g == mx) |
|
|
59 | *h = ((int)b - (int)r) * 255 / (int)(delta * 3) + 52; |
|
|
60 | else if (b == mx) |
|
|
61 | *h = ((int)r - (int)g) * 255 / (int)(delta * 3) + 103; |
|
|
62 | |
|
|
63 | *h &= 255; |
|
|
64 | } |
|
|
65 | } |
17 | |
66 | |
18 | static guint32 a85_val; |
67 | static guint32 a85_val; |
19 | static guint a85_cnt; |
68 | static guint a85_cnt; |
20 | static guchar a85_buf[LINELENGTH], *a85_ptr; |
69 | static guchar a85_buf[LINELENGTH], *a85_ptr; |
21 | |
70 | |
… | |
… | |
68 | *a85_ptr++ = '\n'; |
117 | *a85_ptr++ = '\n'; |
69 | |
118 | |
70 | PerlIO_write (fp, a85_buf, a85_ptr - a85_buf); |
119 | PerlIO_write (fp, a85_buf, a85_ptr - a85_buf); |
71 | } |
120 | } |
72 | |
121 | |
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 | ///////////////////////////////////////////////////////////////////////////// |
122 | ///////////////////////////////////////////////////////////////////////////// |
101 | |
123 | |
102 | MODULE = Gtk2::CV PACKAGE = Gtk2::CV |
124 | MODULE = Gtk2::CV PACKAGE = Gtk2::CV |
103 | |
125 | |
104 | PROTOTYPES: ENABLE |
126 | PROTOTYPES: ENABLE |
… | |
… | |
145 | |
167 | |
146 | for (y = 0; y < h; y++) |
168 | for (y = 0; y < h; y++) |
147 | for (x = 0; x < w; x++) |
169 | for (x = 0; x < w; x++) |
148 | for (i = 0; i < bpp; i++) |
170 | for (i = 0; i < bpp; i++) |
149 | dst[(w - 1 - x) * bpp + y * dstr + i] = src[x * bpp + y * sstr + i]; |
171 | dst[(w - 1 - x) * bpp + y * dstr + i] = src[x * bpp + y * sstr + i]; |
|
|
172 | } |
|
|
173 | OUTPUT: |
|
|
174 | RETVAL |
|
|
175 | |
|
|
176 | GdkPixbuf_noinc * |
|
|
177 | load_jpeg (char *path, int thumbnail=0) |
|
|
178 | CODE: |
|
|
179 | { |
|
|
180 | struct jpeg_decompress_struct cinfo; |
|
|
181 | struct jpg_err_mgr jerr; |
|
|
182 | guchar *data; |
|
|
183 | int rs; |
|
|
184 | FILE *fp; |
|
|
185 | volatile GdkPixbuf *pb = 0; |
|
|
186 | RETVAL = 0; |
|
|
187 | |
|
|
188 | if (!(fp = fopen (path, "rb"))) |
|
|
189 | XSRETURN_UNDEF; |
|
|
190 | |
|
|
191 | cinfo.err = jpeg_std_error (&jerr.err); |
|
|
192 | |
|
|
193 | jerr.err.error_exit = cv_error_exit; |
|
|
194 | jerr.err.output_message = cv_error_output; |
|
|
195 | |
|
|
196 | if ((rs = setjmp (jerr.setjmp_buffer))) |
|
|
197 | { |
|
|
198 | fclose (fp); |
|
|
199 | jpeg_destroy_decompress (&cinfo); |
|
|
200 | |
|
|
201 | if (pb) |
|
|
202 | g_object_unref ((gpointer)pb); |
|
|
203 | |
|
|
204 | XSRETURN_UNDEF; |
|
|
205 | } |
|
|
206 | |
|
|
207 | jpeg_create_decompress (&cinfo); |
|
|
208 | |
|
|
209 | jpeg_stdio_src (&cinfo, fp); |
|
|
210 | jpeg_read_header (&cinfo, TRUE); |
|
|
211 | |
|
|
212 | cinfo.dct_method = JDCT_DEFAULT; |
|
|
213 | cinfo.do_fancy_upsampling = FALSE; /* worse quality, but nobody compained so far, and gdk-pixbuf does the same */ |
|
|
214 | cinfo.do_block_smoothing = FALSE; |
|
|
215 | cinfo.out_color_space = JCS_RGB; |
|
|
216 | cinfo.quantize_colors = FALSE; |
|
|
217 | |
|
|
218 | cinfo.scale_num = 1; |
|
|
219 | cinfo.scale_denom = 1; |
|
|
220 | |
|
|
221 | jpeg_calc_output_dimensions (&cinfo); |
|
|
222 | |
|
|
223 | if (thumbnail) |
|
|
224 | { |
|
|
225 | cinfo.dct_method = JDCT_FASTEST; |
|
|
226 | cinfo.do_fancy_upsampling = FALSE; |
|
|
227 | |
|
|
228 | while (cinfo.scale_denom < 8 |
|
|
229 | && (cinfo.output_width >> 1) >= IW |
|
|
230 | && (cinfo.output_height >> 1) >= IH) |
|
|
231 | { |
|
|
232 | cinfo.scale_denom <<= 1; |
|
|
233 | jpeg_calc_output_dimensions (&cinfo); |
|
|
234 | } |
|
|
235 | } |
|
|
236 | |
|
|
237 | pb = RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, cinfo.output_width, cinfo.output_height); |
|
|
238 | if (!RETVAL) |
|
|
239 | longjmp (jerr.setjmp_buffer, 2); |
|
|
240 | |
|
|
241 | data = gdk_pixbuf_get_pixels (RETVAL); |
|
|
242 | rs = gdk_pixbuf_get_rowstride (RETVAL); |
|
|
243 | |
|
|
244 | if (cinfo.output_components != 3) |
|
|
245 | longjmp (jerr.setjmp_buffer, 3); |
|
|
246 | |
|
|
247 | jpeg_start_decompress (&cinfo); |
|
|
248 | |
|
|
249 | while (cinfo.output_scanline < cinfo.output_height) |
|
|
250 | { |
|
|
251 | int remaining = cinfo.output_height - cinfo.output_scanline; |
|
|
252 | JSAMPROW rp[4]; |
|
|
253 | |
|
|
254 | rp [0] = data + cinfo.output_scanline * rs; |
|
|
255 | rp [1] = (guchar *)rp [0] + rs; |
|
|
256 | rp [2] = (guchar *)rp [1] + rs; |
|
|
257 | rp [3] = (guchar *)rp [2] + rs; |
|
|
258 | |
|
|
259 | jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4); |
|
|
260 | } |
|
|
261 | |
|
|
262 | jpeg_finish_decompress (&cinfo); |
|
|
263 | fclose (fp); |
|
|
264 | jpeg_destroy_decompress (&cinfo); |
150 | } |
265 | } |
151 | OUTPUT: |
266 | OUTPUT: |
152 | RETVAL |
267 | RETVAL |
153 | |
268 | |
154 | ############################################################################# |
269 | ############################################################################# |
… | |
… | |
213 | for (x = 0; x < w; x++) |
328 | for (x = 0; x < w; x++) |
214 | { |
329 | { |
215 | int r, g, b; |
330 | int r, g, b; |
216 | guchar *p = src + x * bpp + y * sstr; |
331 | guchar *p = src + x * bpp + y * sstr; |
217 | |
332 | |
218 | r = ((p[0] + er + Er[x]) * 7 + 128) / 255; |
333 | r = ((p[0] + er + Er[x]) * 7 + (RAND & 127) + 64) / 255; |
219 | g = ((p[1] + eg + Eg[x]) * 7 + 128) / 255; |
334 | g = ((p[1] + eg + Eg[x]) * 7 + (RAND & 127) + 64) / 255; |
220 | b = ((p[2] + eb + Eb[x]) * 3 + 128) / 255; |
335 | b = ((p[2] + eb + Eb[x]) * 3 + (RAND & 127) + 64) / 255; |
221 | |
336 | |
222 | r = r > 7 ? 7 : r < 0 ? 0 : r; |
337 | r = r > 7 ? 7 : r < 0 ? 0 : r; |
223 | g = g > 7 ? 7 : g < 0 ? 0 : g; |
338 | g = g > 7 ? 7 : g < 0 ? 0 : g; |
224 | b = b > 3 ? 3 : b < 0 ? 0 : b; |
339 | b = b > 3 ? 3 : b < 0 ? 0 : b; |
225 | |
340 | |
226 | er += p[0] - (r * 255 + 4) / 7; |
341 | er += p[0] - (r * 255 + 4) / 7; |
227 | eg += p[1] - (g * 255 + 4) / 7; |
342 | eg += p[1] - (g * 255 + 4) / 7; |
228 | eb += p[2] - (b * 255 + 2) / 3; |
343 | eb += p[2] - (b * 255 + 2) / 3; |
229 | |
344 | |
230 | Er[x] = er / 2; er -= er / 2 + RAND % 7 - 3; |
345 | Er[x] = er >> 1; er -= (er + 1) >> 1; |
231 | Eg[x] = eg / 2; eg -= eg / 2 + RAND % 7 - 3; |
346 | Eg[x] = eg >> 1; eg -= (eg + 1) >> 1; |
232 | Eb[x] = eb / 2; eb -= eb / 2 + RAND % 7 - 3; |
347 | Eb[x] = eb >> 1; eb -= (eb + 1) >> 1; |
233 | |
348 | |
234 | *dst++ = r << 5 | g << 2 | b; |
349 | *dst++ = r << 5 | g << 2 | b; |
235 | } |
350 | } |
236 | } |
351 | } |
237 | } |
352 | } |
… | |
… | |
251 | av = (AV *) SvRV (ar); |
366 | av = (AV *) SvRV (ar); |
252 | result = newAV (); |
367 | result = newAV (); |
253 | |
368 | |
254 | for (i = 0; i <= av_len (av); ++i) |
369 | for (i = 0; i <= av_len (av); ++i) |
255 | { |
370 | { |
|
|
371 | const int HISTSIZE = 64; |
|
|
372 | |
256 | int bigst, j; |
373 | int j; |
257 | SV *sv = *av_fetch (av, i, 1); |
374 | SV *sv = *av_fetch (av, i, 1); |
258 | STRLEN len; |
375 | STRLEN len; |
259 | char *buf = SvPVbyte (sv, len); |
376 | char *buf = SvPVbyte (sv, len); |
260 | |
377 | |
261 | int tmphist[256]; |
378 | int tmphist[HISTSIZE]; |
262 | char *hist; |
379 | float *hist; |
263 | |
380 | |
264 | SV *histsv = newSV (257); |
381 | SV *histsv = newSV (HISTSIZE * sizeof (float) + 1); |
265 | sv_upgrade (histsv, SVt_PV); |
|
|
266 | SvCUR_set (histsv, 256); |
382 | SvPOK_on (histsv); |
|
|
383 | SvCUR_set (histsv, HISTSIZE * sizeof (float)); |
267 | hist = SvPV_nolen (histsv); |
384 | hist = (float *)SvPVX (histsv); |
268 | |
385 | |
269 | Zero (tmphist, 256, int); |
386 | Zero (tmphist, sizeof (tmphist), char); |
270 | |
387 | |
271 | while (len--) |
|
|
272 | ++tmphist[(unsigned char)*buf++]; |
|
|
273 | |
|
|
274 | bigst = 1; |
|
|
275 | for (j = 0; j < 256; ++j) |
388 | for (j = len; j--; ) |
276 | if (tmphist[j] > bigst) |
389 | { |
|
|
390 | unsigned int idx |
|
|
391 | = ((*buf & 0xc0) >> 2) |
|
|
392 | | ((*buf & 0x18) >> 1) |
|
|
393 | | (*buf & 0x03); |
|
|
394 | |
277 | bigst = tmphist[j]; |
395 | ++tmphist[idx]; |
|
|
396 | ++buf; |
|
|
397 | } |
278 | |
398 | |
279 | for (j = 0; j < 256; ++j) |
399 | for (j = 0; j < HISTSIZE; ++j) |
280 | hist[j] = tmphist[j] * 255 / bigst; |
400 | hist[j] = (float)tmphist[j] / (len + 1e-30); |
281 | |
401 | |
282 | av_push (result, histsv); |
402 | av_push (result, histsv); |
283 | } |
403 | } |
284 | |
404 | |
285 | RETVAL = newRV_noinc ((SV *)result); |
405 | RETVAL = newRV_noinc ((SV *)result); |
286 | } |
406 | } |
287 | OUTPUT: |
407 | OUTPUT: |
288 | RETVAL |
408 | RETVAL |
289 | |
|
|
290 | |
|
|
291 | |
409 | |
292 | ############################################################################# |
410 | ############################################################################# |
293 | |
411 | |
294 | MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript |
412 | MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript |
295 | |
413 | |