ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CV/CV.xs
(Generate patch)

Comparing CV/CV.xs (file contents):
Revision 1.3 by root, Wed Nov 5 00:22:22 2003 UTC vs.
Revision 1.6 by root, Wed Nov 12 00:59:01 2003 UTC

5#include <gdk-pixbuf/gdk-pixbuf.h> 5#include <gdk-pixbuf/gdk-pixbuf.h>
6 6
7#include <gperl.h> 7#include <gperl.h>
8#include <gtk2perl.h> 8#include <gtk2perl.h>
9 9
10#define IW 80
11
12#define RAND (seed = (seed + 7141) * 54773 % 134456)
13
14#define ELLIPSIS "\xe2\x80\xa6"
15
16static guint32 a85_val;
17static guint a85_cnt;
18static guchar a85_buf[80], *a85_ptr;
19
20static void
21a85_init (void)
22{
23 a85_cnt = 4;
24 a85_ptr = a85_buf;
25}
26
27static void
28a85_push (PerlIO *fp, guchar c)
29{
30 a85_val = a85_val << 8 | c;
31
32 if (!--a85_cnt)
33 {
34 a85_cnt = 4;
35 if (a85_val)
36 {
37 a85_ptr[4] = (a85_val % 85) + 33; a85_val /= 85;
38 a85_ptr[3] = (a85_val % 85) + 33; a85_val /= 85;
39 a85_ptr[2] = (a85_val % 85) + 33; a85_val /= 85;
40 a85_ptr[1] = (a85_val % 85) + 33; a85_val /= 85;
41 a85_ptr[0] = (a85_val ) + 33;
42
43 a85_ptr += 5;
44 }
45 else
46 *a85_ptr++ = 'z';
47
48 if (a85_ptr >= a85_buf + sizeof (a85_buf) - 7)
49 {
50 *a85_ptr++ = '\n';
51 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf);
52 a85_ptr = a85_buf;
53 }
54 }
55
56}
57
58a85_finish (PerlIO *fp)
59{
60 while (a85_cnt != 4)
61 a85_push (fp, 0);
62
63 *a85_ptr++ = '~'; // probably buggy end-marker
64 *a85_ptr++ = '>'; // probably buggy end-marker
65 *a85_ptr++ = '\n';
66
67 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf);
68}
69
70/////////////////////////////////////////////////////////////////////////////
71
10MODULE = Gtk2::CV PACKAGE = Gtk2::CV::ImageWindow 72MODULE = Gtk2::CV PACKAGE = Gtk2::CV::ImageWindow
11 73
12PROTOTYPES: ENABLE 74PROTOTYPES: ENABLE
13 75
14GdkPixbuf_noinc * 76GdkPixbuf_noinc *
52 dstr = gdk_pixbuf_get_rowstride (RETVAL); 114 dstr = gdk_pixbuf_get_rowstride (RETVAL);
53 115
54 for (y = 0; y < h; y++) 116 for (y = 0; y < h; y++)
55 for (x = 0; x < w; x++) 117 for (x = 0; x < w; x++)
56 for (i = 0; i < bpp; i++) 118 for (i = 0; i < bpp; i++)
57 dst[(w - x) * bpp + y * dstr + i] = src[x * bpp + y * sstr + i]; 119 dst[(w - 1 - x) * bpp + y * dstr + i] = src[x * bpp + y * sstr + i];
58} 120}
59 OUTPUT: 121 OUTPUT:
60 RETVAL 122 RETVAL
123
124#############################################################################
61 125
62MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer 126MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer
63 127
64GdkPixbuf_noinc * 128GdkPixbuf_noinc *
65p7_to_pb (int w, int h, guchar *src) 129p7_to_pb (int w, int h, guchar *src)
95 int x, y; 159 int x, y;
96 guchar *dst; 160 guchar *dst;
97 int bpp = gdk_pixbuf_get_has_alpha (pb) ? 4 : 3; 161 int bpp = gdk_pixbuf_get_has_alpha (pb) ? 4 : 3;
98 guchar *src = gdk_pixbuf_get_pixels (pb); 162 guchar *src = gdk_pixbuf_get_pixels (pb);
99 int sstr = gdk_pixbuf_get_rowstride (pb); 163 int sstr = gdk_pixbuf_get_rowstride (pb);
164 int Er[IW], Eg[IW], Eb[IW];
165 int seed = 77;
100 166
101 RETVAL = newSV (w * h); 167 RETVAL = newSV (w * h);
102 SvPOK_only (RETVAL); 168 SvPOK_only (RETVAL);
103 SvCUR_set (RETVAL, w * h); 169 SvCUR_set (RETVAL, w * h);
104 170
105 dst = SvPVX (RETVAL); 171 dst = SvPVX (RETVAL);
106 172
173 memset (Er, 0, sizeof (int) * IW);
174 memset (Eg, 0, sizeof (int) * IW);
175 memset (Eb, 0, sizeof (int) * IW);
176
177 /* some primitive error distribution + random dithering */
178
107 for (y = 0; y < h; y++) 179 for (y = 0; y < h; y++)
108 { 180 {
109 /* use a very primitive form of error distribution. */
110 int er = 0, eg = 0, eb = 0; 181 int er = 0, eg = 0, eb = 0;
111 182
112 for (x = 0; x < w; x++) 183 for (x = 0; x < w; x++)
113 { 184 {
114 int r, g, b; 185 int r, g, b;
115 guchar *p = src + x * bpp + y * sstr; 186 guchar *p = src + x * bpp + y * sstr;
116 187
117 r = ((p[0] + er) * 7 + 128) / 255; 188 r = ((p[0] + er + Er[x]) * 7 + 128) / 255;
118 g = ((p[1] + eg) * 7 + 128) / 255; 189 g = ((p[1] + eg + Eg[x]) * 7 + 128) / 255;
119 b = ((p[2] + eb) * 3 + 128) / 255; 190 b = ((p[2] + eb + Eb[x]) * 3 + 128) / 255;
120 191
121 r = r > 7 ? 7 : r < 0 ? 0 : r; 192 r = r > 7 ? 7 : r < 0 ? 0 : r;
122 g = g > 7 ? 7 : g < 0 ? 0 : g; 193 g = g > 7 ? 7 : g < 0 ? 0 : g;
123 b = b > 3 ? 3 : b < 0 ? 0 : b; 194 b = b > 3 ? 3 : b < 0 ? 0 : b;
124 195
125 er += p[0] - (r * 255 + 4) / 7; 196 er += p[0] - (r * 255 + 4) / 7;
126 eg += p[1] - (g * 255 + 4) / 7; 197 eg += p[1] - (g * 255 + 4) / 7;
127 eb += p[2] - (b * 255 + 2) / 3; 198 eb += p[2] - (b * 255 + 2) / 3;
128 199
200 Er[x] = er / 2; er -= er / 2 + RAND % 7 - 3;
201 Eg[x] = eg / 2; eg -= eg / 2 + RAND % 7 - 3;
202 Eb[x] = eb / 2; eb -= eb / 2 + RAND % 7 - 3;
203
129 *dst++ = r << 5 | g << 2 | b; 204 *dst++ = r << 5 | g << 2 | b;
130 } 205 }
131 } 206 }
132} 207}
133 OUTPUT: 208 OUTPUT:
134 RETVAL 209 RETVAL
135 210
211#############################################################################
136 212
213MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
137 214
215void
216dump_pb (PerlIO *fp, GdkPixbuf *pb)
217 CODE:
218{
219 int w = gdk_pixbuf_get_width (pb);
220 int h = gdk_pixbuf_get_height (pb);
221 int x, y, i;
222 guchar *dst;
223 int bpp = gdk_pixbuf_get_has_alpha (pb) ? 4 : 3;
224 guchar *src = gdk_pixbuf_get_pixels (pb);
225 int sstr = gdk_pixbuf_get_rowstride (pb);
226
227 a85_init ();
228
229 for (y = 0; y < h; y++)
230 for (x = 0; x < w; x++)
231 for (i = 0; i < 3; i++)
232 a85_push (fp, src [x * bpp + y * sstr + i]);
233
234 a85_finish (fp);
235}
236
237
238
239

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines