source: trunk/third/evolution/shell/e-shell-corba-icon-utils.c @ 18142

Revision 18142, 5.3 KB checked in by ghudson, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18141, which included commits to RCS files with non-trunk default branches.
Line 
1/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2/* e-shell-corba-icon-utils.c
3 *
4 * Copyright (C) 2002  Ximian, Inc.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
19 *
20 * Author: Ettore Perazzoli
21 */
22
23#include "e-shell-corba-icon-utils.h"
24
25
26
27/**
28 * e_store_corba_icon_from_pixbuf:
29 * @pixbuf:
30 * @icon_return:
31 *
32 * Store a CORBA Evolution::Icon in *@icon_return.  @icon_return is not
33 * supposed to point to allocated memory, so all of its pointers are just
34 * overwritten.
35 **/
36void
37e_store_corba_icon_from_pixbuf (GdkPixbuf *pixbuf,
38                                GNOME_Evolution_Icon *icon_return)
39{
40        const char *sp;
41        CORBA_octet *dp;
42        int width, height, total_width, rowstride;
43        int i, j;
44        gboolean has_alpha;
45
46        if (pixbuf == NULL) {
47                icon_return->width = 0;
48                icon_return->height = 0;
49                icon_return->hasAlpha = FALSE;
50                icon_return->rgbaData._length = 0;
51                icon_return->rgbaData._maximum = 0;
52                icon_return->rgbaData._buffer = NULL;
53                return;
54        }
55
56        width     = gdk_pixbuf_get_width (pixbuf);
57        height    = gdk_pixbuf_get_height (pixbuf);
58        rowstride = gdk_pixbuf_get_rowstride (pixbuf);
59        has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
60
61        if (has_alpha)
62                total_width = 4 * width;
63        else
64                total_width = 3 * width;
65
66        icon_return->width = width;
67        icon_return->height = height;
68        icon_return->hasAlpha = has_alpha;
69
70        icon_return->rgbaData._length = icon_return->height * total_width;
71        icon_return->rgbaData._maximum = icon_return->rgbaData._length;
72        icon_return->rgbaData._buffer = CORBA_sequence_CORBA_octet_allocbuf (icon_return->rgbaData._maximum);
73
74        sp = gdk_pixbuf_get_pixels (pixbuf);
75        dp = icon_return->rgbaData._buffer;
76        for (i = 0; i < height; i ++) {
77                for (j = 0; j < total_width; j++)
78                        *(dp ++) = sp[j];
79                sp += rowstride;
80        }
81
82        CORBA_sequence_set_release (& icon_return->rgbaData, TRUE);
83}
84
85/**
86 * e_new_corba_icon_from_pixbuf:
87 * @pixbuf:
88 *
89 * Create a CORBA Evolution::Icon from the specified @pixbuf.
90 *
91 * Return value: The new Evolution::Icon.
92 **/
93GNOME_Evolution_Icon *
94e_new_corba_icon_from_pixbuf (GdkPixbuf *pixbuf)
95{
96        GNOME_Evolution_Icon *icon;
97
98        icon = GNOME_Evolution_Icon__alloc ();
99        e_store_corba_icon_from_pixbuf (pixbuf, icon);
100
101        return icon;
102}
103
104/**
105 * e_new_corba_animated_icon_from_pixbuf_array:
106 * @pixbuf_array:
107 *
108 * Generate a CORBA Evolution::AnimatedIcon from a NULL-terminated
109 * @pixbuf_array.
110 *
111 * Return value: The new Evolution::AnimatedIcon.
112 **/
113GNOME_Evolution_AnimatedIcon *
114e_new_corba_animated_icon_from_pixbuf_array (GdkPixbuf **pixbuf_array)
115{
116        GNOME_Evolution_AnimatedIcon *animated_icon;
117        GdkPixbuf **p;
118        int num_frames;
119        int i;
120
121        g_return_val_if_fail (pixbuf_array != NULL, NULL);
122
123        num_frames = 0;
124        for (p = pixbuf_array; *p != NULL; p++)
125                num_frames++;
126
127        if (num_frames == 0)
128                return NULL;
129
130        animated_icon = GNOME_Evolution_AnimatedIcon__alloc ();
131
132        animated_icon->_length = num_frames;
133        animated_icon->_maximum = num_frames;
134        animated_icon->_buffer = CORBA_sequence_GNOME_Evolution_Icon_allocbuf (animated_icon->_maximum);
135
136        for (i = 0; i < num_frames; i++)
137                e_store_corba_icon_from_pixbuf (pixbuf_array[i], & animated_icon->_buffer[i]);
138
139        CORBA_sequence_set_release (animated_icon, TRUE);
140
141        return animated_icon;
142}
143
144
145/**
146 * e_new_gdk_pixbuf_from_corba_icon:
147 * @icon: A CORBA Evolution::Icon.
148 * @scaled_width: Width of the GdkPixbuf to obtain.
149 * @scaled_height: Width of the GdkPixbuf to obtain.
150 *
151 * If @scaled_width or @scaled_height is -1, do not scale.
152 *
153 * Create a GdkPixbuf for the specified CORBA @icon.
154 *
155 * Return value: The newly created GdkPixbuf.
156 **/
157GdkPixbuf *
158e_new_gdk_pixbuf_from_corba_icon  (const GNOME_Evolution_Icon *icon,
159                                   int scaled_width,
160                                   int scaled_height)
161{
162        GdkPixbuf *pixbuf;
163        GdkPixbuf *scaled_pixbuf;
164        unsigned char *p;
165        int src_offset;
166        int i, j;
167        int rowstride;
168        int total_width;
169
170        g_return_val_if_fail (icon != NULL, NULL);
171
172        if (scaled_width == -1)
173                scaled_width = icon->width;
174
175        if (scaled_height == -1)
176                scaled_height = icon->height;
177
178        pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, icon->hasAlpha, 8, icon->width, icon->height);
179
180        if (icon->hasAlpha)
181                total_width = 4 * icon->width;
182        else
183                total_width = 3 * icon->width;
184
185        rowstride = gdk_pixbuf_get_rowstride (pixbuf);
186        src_offset = 0;
187        p = gdk_pixbuf_get_pixels (pixbuf);
188
189        for (i = 0; i < icon->height; i++) {
190                for (j = 0; j < total_width; j++)
191                        p[j] = icon->rgbaData._buffer[src_offset ++];
192                p += rowstride;
193        }
194
195        if (icon->width == scaled_width && icon->height == scaled_height)
196                return pixbuf;
197               
198        scaled_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, icon->hasAlpha, 8, scaled_width, scaled_height);
199        gdk_pixbuf_scale (pixbuf, scaled_pixbuf,
200                          0, 0, scaled_width, scaled_height,
201                          0, 0, (double) scaled_width / icon->width, (double) scaled_height / icon->height,
202                          GDK_INTERP_HYPER);
203
204        gdk_pixbuf_unref (pixbuf);
205
206        return scaled_pixbuf;
207}
208
Note: See TracBrowser for help on using the repository browser.