1 | /* |
---|
2 | * RGB code for drawing stuff on the panel |
---|
3 | * |
---|
4 | * Code taken from gdk-pixbuf, so parts of it are authored by: |
---|
5 | * Cody Russell <bratsche@dfw.net> |
---|
6 | * Actually I don't think it contains much if any of this code any more. |
---|
7 | * Rest is: |
---|
8 | * George Lebl <jirka@5z.com> |
---|
9 | */ |
---|
10 | #include <config.h> |
---|
11 | #include <stdio.h> |
---|
12 | #include <string.h> |
---|
13 | #include <glib.h> |
---|
14 | #include <gmodule.h> |
---|
15 | #include <gdk-pixbuf/gdk-pixbuf.h> |
---|
16 | #include <libart_lgpl/art_misc.h> |
---|
17 | #include <libart_lgpl/art_filterlevel.h> |
---|
18 | #include <libart_lgpl/art_affine.h> |
---|
19 | #include <libart_lgpl/art_rgb_affine.h> |
---|
20 | #include <libart_lgpl/art_rgb_rgba_affine.h> |
---|
21 | #include "rgb-stuff.h" |
---|
22 | |
---|
23 | void |
---|
24 | combine_rgb_rgba(guchar *dest, int dx, int dy, int dw, int dh, int drs, |
---|
25 | guchar *rgba, int rw, int rh, int rrs) |
---|
26 | { |
---|
27 | int r, g, b, a; |
---|
28 | int i,j; |
---|
29 | guchar *imgrow; |
---|
30 | guchar *dstrow; |
---|
31 | |
---|
32 | for(j=0;j<dh;j++) { |
---|
33 | imgrow = rgba + j * rrs; |
---|
34 | dstrow = dest + (dy+j) * drs + (dx*3); |
---|
35 | for(i=0;i<dw;i++) { |
---|
36 | r = *(imgrow++); |
---|
37 | g = *(imgrow++); |
---|
38 | b = *(imgrow++); |
---|
39 | a = *(imgrow++); |
---|
40 | *dstrow = (r*a + *dstrow * (255-a))>>8; |
---|
41 | dstrow++; |
---|
42 | *dstrow = (g*a + *dstrow * (255-a))>>8; |
---|
43 | dstrow++; |
---|
44 | *dstrow = (b*a + *dstrow * (255-a))>>8; |
---|
45 | dstrow++; |
---|
46 | } |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | void |
---|
51 | tile_rgb(guchar *dest, int dw, int dh, int offx, int offy, int drs, |
---|
52 | guchar *tile, int w, int h, int rowstride, int has_alpha) |
---|
53 | { |
---|
54 | guchar *p; |
---|
55 | int i,j,x,y,a; |
---|
56 | guchar *imgrow; |
---|
57 | int off; |
---|
58 | |
---|
59 | p = dest; |
---|
60 | y = offy % h; |
---|
61 | off = drs - (dw*3); /*the space after width ends until next row*/ |
---|
62 | for(j=0;j<dh;j++) { |
---|
63 | x = offx % w; |
---|
64 | imgrow = tile + y * rowstride + (has_alpha?/*x*4*/x<<2:x*3); |
---|
65 | for(i=0;i<dw;i++) { |
---|
66 | *(p++) = *(imgrow++); |
---|
67 | *(p++) = *(imgrow++); |
---|
68 | *(p++) = *(imgrow++); |
---|
69 | if(has_alpha) { |
---|
70 | a = *(imgrow++); |
---|
71 | if(a!=255) { |
---|
72 | guchar *pp = p-3; |
---|
73 | pp[0] = (pp[0]*a)>>8; |
---|
74 | pp[1] = (pp[1]*a)>>8; |
---|
75 | pp[2] = (pp[2]*a)>>8; |
---|
76 | } |
---|
77 | } |
---|
78 | x++; |
---|
79 | if(x>=w) { |
---|
80 | x = 0; |
---|
81 | imgrow = tile + y * rowstride; |
---|
82 | } |
---|
83 | } |
---|
84 | p += off; |
---|
85 | y++; |
---|
86 | if(y>=h) |
---|
87 | y = 0; |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | void |
---|
92 | tile_rgb_pixbuf(guchar *dest, int dw, int dh, int offx, int offy, int drs, |
---|
93 | GdkPixbuf *pbuf, int scale_w, int scale_h, int rotate) |
---|
94 | { |
---|
95 | int i,j; |
---|
96 | |
---|
97 | gdouble scaleaff[6]; |
---|
98 | gdouble affine[6]; |
---|
99 | |
---|
100 | scaleaff[1] = scaleaff[2] = scaleaff[4] = scaleaff[5] = 0; |
---|
101 | |
---|
102 | scaleaff[0] = scale_w / (double)(gdk_pixbuf_get_width(pbuf)); |
---|
103 | scaleaff[3] = scale_h / (double)(gdk_pixbuf_get_height(pbuf)); |
---|
104 | |
---|
105 | if(rotate) { |
---|
106 | int tmp; |
---|
107 | |
---|
108 | art_affine_rotate(affine,270); |
---|
109 | art_affine_multiply(scaleaff,scaleaff,affine); |
---|
110 | art_affine_translate(affine,0,scale_w); |
---|
111 | art_affine_multiply(scaleaff,scaleaff,affine); |
---|
112 | |
---|
113 | tmp = scale_h; |
---|
114 | scale_h = scale_w; |
---|
115 | scale_w = tmp; |
---|
116 | } |
---|
117 | |
---|
118 | for(i=-(offx%scale_w);i<dw;i+=scale_w) { |
---|
119 | for(j=-(offy%scale_h);j<dh;j+=scale_h) { |
---|
120 | art_affine_translate(affine,i,j); |
---|
121 | art_affine_multiply(affine,scaleaff,affine); |
---|
122 | if(gdk_pixbuf_get_has_alpha(pbuf)) |
---|
123 | art_rgb_rgba_affine(dest, |
---|
124 | 0,0,dw,dh,drs, |
---|
125 | gdk_pixbuf_get_pixels(pbuf), |
---|
126 | gdk_pixbuf_get_width(pbuf), |
---|
127 | gdk_pixbuf_get_height(pbuf), |
---|
128 | gdk_pixbuf_get_rowstride(pbuf), |
---|
129 | affine,ART_FILTER_NEAREST,NULL); |
---|
130 | else |
---|
131 | art_rgb_affine(dest, |
---|
132 | 0,0,dw,dh,drs, |
---|
133 | gdk_pixbuf_get_pixels(pbuf), |
---|
134 | gdk_pixbuf_get_width(pbuf), |
---|
135 | gdk_pixbuf_get_height(pbuf), |
---|
136 | gdk_pixbuf_get_rowstride(pbuf), |
---|
137 | affine,ART_FILTER_NEAREST,NULL); |
---|
138 | |
---|
139 | } |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | #if 0 |
---|
144 | void |
---|
145 | make_scale_affine(double affine[], int w, int h, int size, int *outw, int *outh) |
---|
146 | { |
---|
147 | int oh, ow; |
---|
148 | oh = h; |
---|
149 | ow = w; |
---|
150 | if(w > h) { |
---|
151 | h = h*((double)size/w); |
---|
152 | w = size; |
---|
153 | } else { |
---|
154 | w = w*((double)size/h); |
---|
155 | h = size; |
---|
156 | } |
---|
157 | w = w > 0 ? w : 1; |
---|
158 | h = h > 0 ? h : 1; |
---|
159 | |
---|
160 | affine[1] = affine[2] = affine[4] = affine[5] = 0; |
---|
161 | |
---|
162 | affine[0] = w / (double)ow; |
---|
163 | affine[3] = h / (double)oh; |
---|
164 | |
---|
165 | if(outw) |
---|
166 | *outw = w; |
---|
167 | if(outh) |
---|
168 | *outh = h; |
---|
169 | } |
---|
170 | #endif |
---|
171 | |
---|
172 | GdkPixbuf * |
---|
173 | scale_pixbuf_to_square (GdkPixbuf *pb, int size, int *outw, int *outh, GdkInterpType interp) |
---|
174 | { |
---|
175 | GdkPixbuf *new_pb; |
---|
176 | int width, height; |
---|
177 | int new_width, new_height; |
---|
178 | |
---|
179 | new_width = width = gdk_pixbuf_get_width (pb); |
---|
180 | new_height = height = gdk_pixbuf_get_height (pb); |
---|
181 | |
---|
182 | if(new_width > new_height) { |
---|
183 | new_height = new_height * (size / (double)new_width); |
---|
184 | new_width = size; |
---|
185 | } else { |
---|
186 | new_width = new_width * (size / (double)new_height); |
---|
187 | new_height = size; |
---|
188 | } |
---|
189 | |
---|
190 | new_width = new_width > 0 ? new_width : 1; |
---|
191 | new_height = new_height > 0 ? new_height : 1; |
---|
192 | |
---|
193 | if(outw) |
---|
194 | *outw = new_width; |
---|
195 | if(outh) |
---|
196 | *outh = new_height; |
---|
197 | |
---|
198 | if (new_width == width && new_height == height) |
---|
199 | return gdk_pixbuf_ref (pb); |
---|
200 | |
---|
201 | new_pb = gdk_pixbuf_new(gdk_pixbuf_get_colorspace(pb), |
---|
202 | gdk_pixbuf_get_has_alpha(pb), |
---|
203 | gdk_pixbuf_get_bits_per_sample(pb), |
---|
204 | new_width, |
---|
205 | new_height); |
---|
206 | |
---|
207 | gdk_pixbuf_scale (pb, new_pb, 0, 0, new_width, new_height, 0.0, 0.0, |
---|
208 | new_width / (double)width, |
---|
209 | new_height / (double)height, |
---|
210 | interp); |
---|
211 | |
---|
212 | return new_pb; |
---|
213 | } |
---|
214 | |
---|
215 | void |
---|
216 | transform_pixbuf(guchar *dst, int x0, int y0, int x1, int y1, int drs, |
---|
217 | GdkPixbuf *pixbuf, double affine[6], |
---|
218 | int level, ArtAlphaGamma *ag) |
---|
219 | { |
---|
220 | gint w, h, rs; |
---|
221 | |
---|
222 | rs = gdk_pixbuf_get_rowstride(pixbuf); |
---|
223 | h = gdk_pixbuf_get_height(pixbuf); |
---|
224 | w = gdk_pixbuf_get_width(pixbuf); |
---|
225 | |
---|
226 | if (gdk_pixbuf_get_has_alpha(pixbuf)) { |
---|
227 | art_rgb_rgba_affine(dst, x0, y0, x1, y1, drs, |
---|
228 | gdk_pixbuf_get_pixels(pixbuf), |
---|
229 | w, h, rs, affine, level, ag); |
---|
230 | } else { |
---|
231 | art_rgb_affine(dst, x0, y0, x1, y1, drs, |
---|
232 | gdk_pixbuf_get_pixels(pixbuf), |
---|
233 | w, h, rs, affine, level, ag); |
---|
234 | } |
---|
235 | } |
---|
236 | |
---|
237 | |
---|
238 | |
---|
239 | #if 0 |
---|
240 | void |
---|
241 | rgb_rotate270(guchar *dest, int drs, guchar *src, int w, int h, int srs) |
---|
242 | { |
---|
243 | guchar *p; |
---|
244 | guchar *sp; |
---|
245 | |
---|
246 | int i,j; |
---|
247 | |
---|
248 | for(j=0;j<h;j++) { |
---|
249 | sp = src + j*srs; |
---|
250 | p = dest + j*3; |
---|
251 | for(i=0;i<w;i++) { |
---|
252 | *(p++) = *(sp++); |
---|
253 | *(p++) = *(sp++); |
---|
254 | *p = *(sp++); |
---|
255 | p += drs-2; |
---|
256 | } |
---|
257 | } |
---|
258 | } |
---|
259 | |
---|
260 | void |
---|
261 | rgba_rotate270(guchar *dest, int drs, guchar *src, int w, int h, int srs) |
---|
262 | { |
---|
263 | guchar *p; |
---|
264 | guchar *sp; |
---|
265 | |
---|
266 | int i,j; |
---|
267 | |
---|
268 | for(j=0;j<h;j++) { |
---|
269 | sp = src + j*srs; |
---|
270 | p = dest + j*4; |
---|
271 | for(i=0;i<w;i++) { |
---|
272 | *(p++) = *(sp++); |
---|
273 | *(p++) = *(sp++); |
---|
274 | *(p++) = *(sp++); |
---|
275 | *p = *(sp++); |
---|
276 | p += drs-3; |
---|
277 | } |
---|
278 | } |
---|
279 | } |
---|
280 | #endif |
---|