ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CV/CV.xs
Revision: 1.3
Committed: Wed Nov 5 00:22:22 2003 UTC (20 years, 6 months ago) by root
Branch: MAIN
Changes since 1.2: +58 -11 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.1 MODULE = Gtk2::CV PACKAGE = Gtk2::CV::ImageWindow
11    
12     PROTOTYPES: ENABLE
13 root 1.2
14 root 1.3 GdkPixbuf_noinc *
15 root 1.2 transpose (GdkPixbuf *pb)
16     CODE:
17     {
18     int w = gdk_pixbuf_get_width (pb);
19     int h = gdk_pixbuf_get_height (pb);
20     int bpp = gdk_pixbuf_get_has_alpha (pb) ? 4 : 3;
21     int x, y, i;
22     guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
23     int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
24    
25     RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, h, w);
26    
27     dst = gdk_pixbuf_get_pixels (RETVAL);
28     dstr = gdk_pixbuf_get_rowstride (RETVAL);
29    
30 root 1.3 for (y = 0; y < h; y++)
31     for (x = 0; x < w; x++)
32 root 1.2 for (i = 0; i < bpp; i++)
33     dst[y * bpp + x * dstr + i] = src[x * bpp + y * sstr + i];
34     }
35     OUTPUT:
36     RETVAL
37    
38 root 1.3 GdkPixbuf_noinc *
39 root 1.2 flop (GdkPixbuf *pb)
40     CODE:
41     {
42     int w = gdk_pixbuf_get_width (pb);
43     int h = gdk_pixbuf_get_height (pb);
44     int bpp = gdk_pixbuf_get_has_alpha (pb) ? 4 : 3;
45     int x, y, i;
46     guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
47     int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
48    
49     RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, w, h);
50    
51     dst = gdk_pixbuf_get_pixels (RETVAL);
52     dstr = gdk_pixbuf_get_rowstride (RETVAL);
53    
54 root 1.3 for (y = 0; y < h; y++)
55     for (x = 0; x < w; x++)
56 root 1.2 for (i = 0; i < bpp; i++)
57 root 1.3 dst[(w - x) * bpp + y * dstr + i] = src[x * bpp + y * sstr + i];
58 root 1.2 }
59     OUTPUT:
60     RETVAL
61    
62     MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer
63    
64 root 1.3 GdkPixbuf_noinc *
65 root 1.2 p7_to_pb (int w, int h, guchar *src)
66     CODE:
67     {
68     int x, y;
69     guchar *dst, *d;
70     int dstr;
71    
72     RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
73     dst = gdk_pixbuf_get_pixels (RETVAL);
74     dstr = gdk_pixbuf_get_rowstride (RETVAL);
75    
76     for (y = 0; y < h; y++)
77     for (d = dst + y * dstr, x = 0; x < w; x++)
78     {
79 root 1.3 *d++ = (((*src >> 5) & 7) * 255 + 4) / 7;
80     *d++ = (((*src >> 2) & 7) * 255 + 4) / 7;
81     *d++ = (((*src >> 0) & 3) * 255 + 2) / 3;
82 root 1.2
83     src++;
84     }
85 root 1.3 }
86     OUTPUT:
87     RETVAL
88    
89     SV *
90     pb_to_p7 (GdkPixbuf *pb)
91     CODE:
92     {
93     int w = gdk_pixbuf_get_width (pb);
94     int h = gdk_pixbuf_get_height (pb);
95     int x, y;
96     guchar *dst;
97     int bpp = gdk_pixbuf_get_has_alpha (pb) ? 4 : 3;
98     guchar *src = gdk_pixbuf_get_pixels (pb);
99     int sstr = gdk_pixbuf_get_rowstride (pb);
100    
101     RETVAL = newSV (w * h);
102     SvPOK_only (RETVAL);
103     SvCUR_set (RETVAL, w * h);
104    
105     dst = SvPVX (RETVAL);
106    
107     for (y = 0; y < h; y++)
108     {
109     /* use a very primitive form of error distribution. */
110     int er = 0, eg = 0, eb = 0;
111    
112     for (x = 0; x < w; x++)
113     {
114     int r, g, b;
115     guchar *p = src + x * bpp + y * sstr;
116    
117     r = ((p[0] + er) * 7 + 128) / 255;
118     g = ((p[1] + eg) * 7 + 128) / 255;
119     b = ((p[2] + eb) * 3 + 128) / 255;
120    
121     r = r > 7 ? 7 : r < 0 ? 0 : r;
122     g = g > 7 ? 7 : g < 0 ? 0 : g;
123     b = b > 3 ? 3 : b < 0 ? 0 : b;
124    
125     er += p[0] - (r * 255 + 4) / 7;
126     eg += p[1] - (g * 255 + 4) / 7;
127     eb += p[2] - (b * 255 + 2) / 3;
128    
129     *dst++ = r << 5 | g << 2 | b;
130     }
131     }
132 root 1.2 }
133     OUTPUT:
134     RETVAL
135    
136 root 1.1
137