ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtimg.C
(Generate patch)

Comparing rxvt-unicode/src/rxvtimg.C (file contents):
Revision 1.13 by root, Mon Jun 4 15:30:41 2012 UTC vs.
Revision 1.19 by root, Tue Jun 5 13:39:26 2012 UTC

31 XRenderFindStandardFormat (s->display->dpy, gdk_pixbuf_get_has_alpha (pb) ? PictStandardARGB32 : PictStandardRGB24), 31 XRenderFindStandardFormat (s->display->dpy, gdk_pixbuf_get_has_alpha (pb) ? PictStandardARGB32 : PictStandardRGB24),
32 gdk_pixbuf_get_width (pb), 32 gdk_pixbuf_get_width (pb),
33 gdk_pixbuf_get_height (pb) 33 gdk_pixbuf_get_height (pb)
34 ); 34 );
35 35
36 img->render (pb, 0, 0, img->w, img->h, 0, 0); 36 img->render_pixbuf (pb, 0, 0, img->w, img->h, 0, 0);
37 37
38 return img; 38 return img;
39} 39}
40 40
41rxvt_img::~rxvt_img () 41rxvt_img::~rxvt_img ()
101 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); 101 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed));
102 102
103 XRenderPictureAttributes pa; 103 XRenderPictureAttributes pa;
104 104
105 pa.repeat = RepeatPad; 105 pa.repeat = RepeatPad;
106 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); 106 Picture src = XRenderCreatePicture (dpy, pm , format, CPRepeat, &pa);
107 Pixmap tmp = XCreatePixmap (dpy, pm, w, h, format->depth); 107 Pixmap tmp = XCreatePixmap (dpy, pm, w, h, format->depth);
108 Picture dst = XRenderCreatePicture (dpy, tmp, format, CPRepeat, &pa); 108 Picture dst = XRenderCreatePicture (dpy, tmp, format, CPRepeat, &pa);
109 XFreePixmap (dpy, tmp); 109 XFreePixmap (dpy, tmp);
110 110
111 if (kernel && params) 111 if (kernel && params)
198 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); 198 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1);
199 199
200 XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); 200 XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
201} 201}
202 202
203void 203bool
204rxvt_img::render (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y) 204rxvt_img::render_pixbuf (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y)
205{ 205{
206 Display *dpy = s->display->dpy;
207
208 if (s->visual->c_class != TrueColor)
209 return false;
210
211 uint32_t red_mask, green_mask, blue_mask, alpha_mask;
212
213 red_mask = (uint32_t)format->direct.redMask << format->direct.red;
214 green_mask = (uint32_t)format->direct.greenMask << format->direct.green;
215 blue_mask = (uint32_t)format->direct.blueMask << format->direct.blue;
216 alpha_mask = (uint32_t)format->direct.alphaMask << format->direct.alpha;
217
218 int width_r = ecb_popcount32 (red_mask);
219 int width_g = ecb_popcount32 (green_mask);
220 int width_b = ecb_popcount32 (blue_mask);
221 int width_a = ecb_popcount32 (alpha_mask);
222
223 if (width_r > 8 || width_g > 8 || width_b > 8 || width_a > 8)
224 return false;
225
226 int sh_r = ecb_ctz32 (red_mask);
227 int sh_g = ecb_ctz32 (green_mask);
228 int sh_b = ecb_ctz32 (blue_mask);
229 int sh_a = ecb_ctz32 (alpha_mask);
230
231 if (width > 32767 || height > 32767)
232 return false;
233
234 XImage *ximage = XCreateImage (dpy, s->visual, format->depth, ZPixmap, 0, 0,
235 width, height, 32, 0);
236 if (!ximage)
237 return false;
238
239 if (height > INT_MAX / ximage->bytes_per_line
240 || !(ximage->data = (char *)malloc (height * ximage->bytes_per_line)))
241 {
242 XDestroyImage (ximage);
243 return false;
244 }
245
246 GC gc = XCreateGC (dpy, pm, 0, 0);
247
248 ximage->byte_order = ecb_big_endian () ? MSBFirst : LSBFirst;
249
250 int rowstride = gdk_pixbuf_get_rowstride (pixbuf);
251 int channels = gdk_pixbuf_get_n_channels (pixbuf);
252 unsigned char *row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels;
253 char *line = ximage->data;
254
255 rgba c (0, 0, 0);
256
257 if (channels == 4 && alpha_mask == 0)
258 {
259 //pix_colors[Color_bg].get (c);
206 //TODO 260 //TODO
261 c.r = 0xffff; c.g = 0xc0c0; c.b = 0xcbcb;//D
262 c.r >>= 8;
263 c.g >>= 8;
264 c.b >>= 8;
265 }
266
267 for (int y = 0; y < height; y++)
268 {
269 for (int x = 0; x < width; x++)
270 {
271 unsigned char *pixel = row + x * channels;
272 uint32_t value;
273 unsigned char r, g, b, a;
274
275 if (channels == 4)
276 {
277 a = pixel[3];
278 r = (pixel[0] * a + c.r * (0xff - a)) / 0xff;
279 g = (pixel[1] * a + c.g * (0xff - a)) / 0xff;
280 b = (pixel[2] * a + c.b * (0xff - a)) / 0xff;
281 }
282 else
283 {
284 a = 0xff;
285 r = pixel[0];
286 g = pixel[1];
287 b = pixel[2];
288 }
289
290 value = ((r >> (8 - width_r)) << sh_r)
291 | ((g >> (8 - width_g)) << sh_g)
292 | ((b >> (8 - width_b)) << sh_b)
293 | ((a >> (8 - width_a)) << sh_a);
294
295 if (ximage->bits_per_pixel == 32)
296 ((uint32_t *)line)[x] = value;
297 else
298 XPutPixel (ximage, x, y, value);
299 }
300
301 row += rowstride;
302 line += ximage->bytes_per_line;
303 }
304
305 XPutImage (dpy, pm, gc, ximage, 0, 0, dst_x, dst_y, width, height);
306 XDestroyImage (ximage);
307 XFreeGC (dpy, gc);
308
309 return true;
207} 310}
208 311
209rxvt_img * 312rxvt_img *
210rxvt_img::clone () 313rxvt_img::clone ()
211{ 314{
315 rxvt_img *img = new rxvt_img (s, format, w, h);
316
212 GC gc = XCreateGC (s->display->dpy, pm, 0, 0); 317 GC gc = XCreateGC (s->display->dpy, pm, 0, 0);
213 Pixmap pm2 = XCreatePixmap (s->display->dpy, pm, w, h, format->depth);
214 XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, w, h, 0, 0); 318 XCopyArea (s->display->dpy, pm, img->pm, gc, 0, 0, w, h, 0, 0);
215 XFreeGC (s->display->dpy, gc); 319 XFreeGC (s->display->dpy, gc);
216 return new rxvt_img (s, format, w, h, pm2);
217} 320}
218 321
219rxvt_img * 322rxvt_img *
220rxvt_img::transform (int new_width, int new_height, int repeat, double matrix[9]) 323rxvt_img::sub_rect (int x, int y, int width, int height, int repeat)
221{ 324{
222 rxvt_img *img = new rxvt_img (s, format, new_width, new_height); 325 rxvt_img *img = new rxvt_img (s, format, width, height);
223 326
224 Display *dpy = s->display->dpy; 327 Display *dpy = s->display->dpy;
225 XRenderPictureAttributes pa; 328 XRenderPictureAttributes pa;
226 pa.repeat = repeat; 329 pa.repeat = repeat;
227 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); 330 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
228 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); 331 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
229 332
333 XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, width, height);
334
335 XRenderFreePicture (dpy, src);
336 XRenderFreePicture (dpy, dst);
337
338 return img;
339}
340
341rxvt_img *
342rxvt_img::transform (int new_width, int new_height, double matrix[9], int repeat)
343{
344 rxvt_img *img = new rxvt_img (s, format, new_width, new_height);
345
346 Display *dpy = s->display->dpy;
347 XRenderPictureAttributes pa;
348 pa.repeat = repeat;
349 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
350 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
351
230 XTransform xfrm; 352 XTransform xfrm;
231 353
232 for (int i = 0; i < 3; ++i) 354 for (int i = 0; i < 3; ++i)
233 for (int j = 0; j < 3; ++j) 355 for (int j = 0; j < 3; ++j)
234 xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); 356 xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]);
235 357
358 XRenderSetPictureFilter (dpy, src, "good", 0, 0);
236 XRenderSetPictureTransform (dpy, src, &xfrm); 359 XRenderSetPictureTransform (dpy, src, &xfrm);
237 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height); 360 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height);
238 361
239 XRenderFreePicture (dpy, src); 362 XRenderFreePicture (dpy, src);
240 XRenderFreePicture (dpy, dst); 363 XRenderFreePicture (dpy, dst);
244 367
245rxvt_img * 368rxvt_img *
246rxvt_img::scale (int new_width, int new_height) 369rxvt_img::scale (int new_width, int new_height)
247{ 370{
248 double matrix[9] = { 371 double matrix[9] = {
249 new_width / (double)w, 0, 0, 372 w / (double)new_width, 0, 0,
250 0, new_height / (double)h, 0, 373 0, h / (double)new_height, 0,
251 0, 0, 1 374 0, 0, 1
252 }; 375 };
253 376
254 return transform (new_width, new_height, RepeatNormal, matrix); 377 return transform (new_width, new_height, matrix);
378}
379
380rxvt_img *
381rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi, int repeat)
382{
383 double s = sin (phi);
384 double c = cos (phi);
385
386 double matrix[9] = {
387 c, -s, -c * x + s * y + x,
388 s, c, -s * x - c * y + y,
389 0, 0, 1
390 };
391
392 return transform (new_width, new_height, matrix, repeat);
255} 393}
256 394
257rxvt_img * 395rxvt_img *
258rxvt_img::convert_to (XRenderPictFormat *new_format) 396rxvt_img::convert_to (XRenderPictFormat *new_format)
259{ 397{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines