1 /* -*- Mode: C; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 3 -*- */
2 
3 /*
4  *  Copyright (C) 2001 The Free Software Foundation
5  *
6  *  This program is released under the terms of the GNU General Public
7  *  License version 2, you can find a copy of the lincense in the file
8  *  COPYING. *
9  *
10  *  $Id: pixbuf_utils.c,v 1.12 2003/06/13 09:43:37 makeinu Exp $
11  */
12 
13 #include "pixbuf_utils.h"
14 
15 #if HAVE_GDK_PIXBUF
16 
17 #include <gdk-pixbuf/gdk-pixbuf-loader.h>
18 
19 
20 /*
21  * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90
22  * counterclockwise.
23  */
24 GdkPixbuf *
pixbuf_copy_rotate_90(GdkPixbuf * src,gboolean counter_clockwise)25 pixbuf_copy_rotate_90 (GdkPixbuf *src,
26                        gboolean counter_clockwise)
27 {
28    GdkPixbuf *dest;
29    gint has_alpha;
30    gint src_w, src_h, src_rs;
31    gint dest_w, dest_h, dest_rs;
32    guchar *src_pix,  *src_p;
33    guchar *dest_pix, *dest_p;
34    gint i, j;
35    gint a;
36 
37    if (!src) return NULL;
38 
39    src_w = gdk_pixbuf_get_width (src);
40    src_h = gdk_pixbuf_get_height (src);
41    has_alpha = gdk_pixbuf_get_has_alpha (src);
42    src_rs = gdk_pixbuf_get_rowstride (src);
43    src_pix = gdk_pixbuf_get_pixels (src);
44 
45    dest_w = src_h;
46    dest_h = src_w;
47    dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB, has_alpha, 8, dest_w, dest_h);
48    dest_rs = gdk_pixbuf_get_rowstride (dest);
49    dest_pix = gdk_pixbuf_get_pixels (dest);
50 
51    a = (has_alpha ? 4 : 3);
52 
53    for (i = 0; i < src_h; i++) {
54       src_p = src_pix + (i * src_rs);
55       for (j = 0; j < src_w; j++) {
56          if (counter_clockwise)
57             dest_p = dest_pix + ((dest_h - j - 1) * dest_rs) + (i * a);
58          else
59             dest_p = dest_pix + (j * dest_rs) + ((dest_w - i - 1) * a);
60 
61          *(dest_p++) = *(src_p++);	/* r */
62          *(dest_p++) = *(src_p++);	/* g */
63          *(dest_p++) = *(src_p++);	/* b */
64          if (has_alpha) *(dest_p) = *(src_p++);	/* a */
65       }
66    }
67 
68    return dest;
69 }
70 
71 
72 /*
73  * Returns a copy of pixbuf mirrored and or flipped.
74  * TO do a 180 degree rotations set both mirror and flipped TRUE
75  * if mirror and flip are FALSE, result is a simple copy.
76  */
77 GdkPixbuf *
pixbuf_copy_mirror(GdkPixbuf * src,gboolean mirror,gboolean flip)78 pixbuf_copy_mirror (GdkPixbuf *src,
79                     gboolean mirror,
80                     gboolean flip)
81 {
82    GdkPixbuf *dest;
83    gint has_alpha;
84    gint w, h;
85    gint    src_rs,   dest_rs;
86    guchar *src_pix, *dest_pix;
87    guchar *src_p,   *dest_p;
88    gint i, j;
89    gint a;
90 
91    if (!src) return NULL;
92 
93    w = gdk_pixbuf_get_width (src);
94    h = gdk_pixbuf_get_height (src);
95    has_alpha = gdk_pixbuf_get_has_alpha (src);
96    src_rs = gdk_pixbuf_get_rowstride (src);
97    src_pix = gdk_pixbuf_get_pixels (src);
98 
99    dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
100    dest_rs = gdk_pixbuf_get_rowstride (dest);
101    dest_pix = gdk_pixbuf_get_pixels (dest);
102 
103    a = has_alpha ? 4 : 3;
104 
105    for (i = 0; i < h; i++)	{
106       src_p = src_pix + (i * src_rs);
107       if (flip)
108          dest_p = dest_pix + ((h - i - 1) * dest_rs);
109       else
110          dest_p = dest_pix + (i * dest_rs);
111 
112       if (mirror) {
113          dest_p += (w - 1) * a;
114          for (j = 0; j < w; j++) {
115             *(dest_p++) = *(src_p++);	/* r */
116             *(dest_p++) = *(src_p++);	/* g */
117             *(dest_p++) = *(src_p++);	/* b */
118             if (has_alpha) *(dest_p) = *(src_p++);	/* a */
119             dest_p -= (a + 3);
120          }
121       } else {
122          for (j = 0; j < w; j++) {
123             *(dest_p++) = *(src_p++);	/* r */
124             *(dest_p++) = *(src_p++);	/* g */
125             *(dest_p++) = *(src_p++);	/* b */
126             if (has_alpha) *(dest_p++) = *(src_p++);	/* a */
127          }
128       }
129    }
130 
131    return dest;
132 }
133 
134 
135 void
pixmap_from_xpm(const char ** data,GdkPixmap ** pixmap,GdkBitmap ** mask)136 pixmap_from_xpm (const char **data,
137                  GdkPixmap **pixmap,
138                  GdkBitmap **mask)
139 {
140    GdkPixbuf *pixbuf;
141 
142    pixbuf = gdk_pixbuf_new_from_xpm_data (data);
143    gdk_pixbuf_render_pixmap_and_mask (pixbuf, pixmap, mask, 127);
144    gdk_pixbuf_unref (pixbuf);
145 }
146 
147 #endif /* HAVE_GDK_PIXBUF */
148