ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CV/CV.xs
Revision: 1.4
Committed: Sat Nov 8 18:22:21 2003 UTC (20 years, 6 months ago) by root
Branch: MAIN
Changes since 1.3: +80 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #include "EXTERN.h"
2     #include "perl.h"
3     #include "XSUB.h"
4    
5 root 1.2 #include <gdk-pixbuf/gdk-pixbuf.h>
6    
7     #include <gperl.h>
8     #include <gtk2perl.h>
9    
10 root 1.4 static guint32 a85_val;
11     static guint a85_cnt;
12     static guchar a85_buf[80], *a85_ptr;
13    
14     static void
15     a85_init (void)
16     {
17     a85_cnt = 4;
18     a85_ptr = a85_buf;
19     }
20    
21     static void
22     a85_push (PerlIO *fp, guchar c)
23     {
24     a85_val = a85_val << 8 | c;
25    
26     if (!--a85_cnt)
27     {
28     a85_cnt = 4;
29     if (a85_val)
30     {
31     a85_ptr[4] = (a85_val % 85) + 33; a85_val /= 85;
32     a85_ptr[3] = (a85_val % 85) + 33; a85_val /= 85;
33     a85_ptr[2] = (a85_val % 85) + 33; a85_val /= 85;
34     a85_ptr[1] = (a85_val % 85) + 33; a85_val /= 85;
35     a85_ptr[0] = (a85_val ) + 33;
36    
37     a85_ptr += 5;
38     }
39     else
40     *a85_ptr++ = 'z';
41    
42     if (a85_ptr >= a85_buf + sizeof (a85_buf) - 7)
43     {
44     *a85_ptr++ = '\n';
45     PerlIO_write (fp, a85_buf, a85_ptr - a85_buf);
46     a85_ptr = a85_buf;
47     }
48     }
49    
50     }
51    
52     a85_finish (PerlIO *fp)
53     {
54     while (a85_cnt != 4)
55     a85_push (fp, 0);
56    
57     *a85_ptr++ = '~'; // probably buggy end-marker
58     *a85_ptr++ = '>'; // probably buggy end-marker
59     *a85_ptr++ = '\n';
60    
61     PerlIO_write (fp, a85_buf, a85_ptr - a85_buf);
62     }
63    
64 root 1.1 MODULE = Gtk2::CV PACKAGE = Gtk2::CV::ImageWindow
65    
66     PROTOTYPES: ENABLE
67 root 1.2
68 root 1.3 GdkPixbuf_noinc *
69 root 1.2 transpose (GdkPixbuf *pb)
70     CODE:
71     {
72     int w = gdk_pixbuf_get_width (pb);
73     int h = gdk_pixbuf_get_height (pb);
74     int bpp = gdk_pixbuf_get_has_alpha (pb) ? 4 : 3;
75     int x, y, i;
76     guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
77     int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
78    
79     RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, h, w);
80    
81     dst = gdk_pixbuf_get_pixels (RETVAL);
82     dstr = gdk_pixbuf_get_rowstride (RETVAL);
83    
84 root 1.3 for (y = 0; y < h; y++)
85     for (x = 0; x < w; x++)
86 root 1.2 for (i = 0; i < bpp; i++)
87     dst[y * bpp + x * dstr + i] = src[x * bpp + y * sstr + i];
88     }
89     OUTPUT:
90     RETVAL
91    
92 root 1.3 GdkPixbuf_noinc *
93 root 1.2 flop (GdkPixbuf *pb)
94     CODE:
95     {
96     int w = gdk_pixbuf_get_width (pb);
97     int h = gdk_pixbuf_get_height (pb);
98     int bpp = gdk_pixbuf_get_has_alpha (pb) ? 4 : 3;
99     int x, y, i;
100     guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
101     int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
102    
103     RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, w, h);
104    
105     dst = gdk_pixbuf_get_pixels (RETVAL);
106     dstr = gdk_pixbuf_get_rowstride (RETVAL);
107    
108 root 1.3 for (y = 0; y < h; y++)
109     for (x = 0; x < w; x++)
110 root 1.2 for (i = 0; i < bpp; i++)
111 root 1.4 dst[(w - 1 - x) * bpp + y * dstr + i] = src[x * bpp + y * sstr + i];
112 root 1.2 }
113     OUTPUT:
114     RETVAL
115    
116     MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer
117    
118 root 1.3 GdkPixbuf_noinc *
119 root 1.2 p7_to_pb (int w, int h, guchar *src)
120     CODE:
121     {
122     int x, y;
123     guchar *dst, *d;
124     int dstr;
125    
126     RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
127     dst = gdk_pixbuf_get_pixels (RETVAL);
128     dstr = gdk_pixbuf_get_rowstride (RETVAL);
129    
130     for (y = 0; y < h; y++)
131     for (d = dst + y * dstr, x = 0; x < w; x++)
132     {
133 root 1.3 *d++ = (((*src >> 5) & 7) * 255 + 4) / 7;
134     *d++ = (((*src >> 2) & 7) * 255 + 4) / 7;
135     *d++ = (((*src >> 0) & 3) * 255 + 2) / 3;
136 root 1.2
137     src++;
138     }
139 root 1.3 }
140     OUTPUT:
141     RETVAL
142    
143     SV *
144     pb_to_p7 (GdkPixbuf *pb)
145     CODE:
146     {
147     int w = gdk_pixbuf_get_width (pb);
148     int h = gdk_pixbuf_get_height (pb);
149     int x, y;
150     guchar *dst;
151     int bpp = gdk_pixbuf_get_has_alpha (pb) ? 4 : 3;
152     guchar *src = gdk_pixbuf_get_pixels (pb);
153     int sstr = gdk_pixbuf_get_rowstride (pb);
154    
155     RETVAL = newSV (w * h);
156     SvPOK_only (RETVAL);
157     SvCUR_set (RETVAL, w * h);
158    
159     dst = SvPVX (RETVAL);
160    
161     for (y = 0; y < h; y++)
162     {
163     /* use a very primitive form of error distribution. */
164     int er = 0, eg = 0, eb = 0;
165    
166     for (x = 0; x < w; x++)
167     {
168     int r, g, b;
169     guchar *p = src + x * bpp + y * sstr;
170    
171     r = ((p[0] + er) * 7 + 128) / 255;
172     g = ((p[1] + eg) * 7 + 128) / 255;
173     b = ((p[2] + eb) * 3 + 128) / 255;
174    
175     r = r > 7 ? 7 : r < 0 ? 0 : r;
176     g = g > 7 ? 7 : g < 0 ? 0 : g;
177     b = b > 3 ? 3 : b < 0 ? 0 : b;
178    
179     er += p[0] - (r * 255 + 4) / 7;
180     eg += p[1] - (g * 255 + 4) / 7;
181     eb += p[2] - (b * 255 + 2) / 3;
182    
183     *dst++ = r << 5 | g << 2 | b;
184     }
185     }
186 root 1.2 }
187     OUTPUT:
188     RETVAL
189 root 1.4
190     MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
191    
192     void
193     dump_pb (PerlIO *fp, GdkPixbuf *pb)
194     CODE:
195     {
196     int w = gdk_pixbuf_get_width (pb);
197     int h = gdk_pixbuf_get_height (pb);
198     int x, y, i;
199     guchar *dst;
200     int bpp = gdk_pixbuf_get_has_alpha (pb) ? 4 : 3;
201     guchar *src = gdk_pixbuf_get_pixels (pb);
202     int sstr = gdk_pixbuf_get_rowstride (pb);
203    
204     a85_init ();
205    
206     for (y = 0; y < h; y++)
207     for (x = 0; x < w; x++)
208     for (i = 0; i < 3; i++)
209     a85_push (fp, src [x * bpp + y * sstr + i]);
210    
211     a85_finish (fp);
212     }
213    
214 root 1.2
215 root 1.1
216