]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/gdk-pixbuf-scale.c
DND updates
[~andy/gtk] / gdk-pixbuf / gdk-pixbuf-scale.c
1 /* GdkPixbuf library - Scaling and compositing functions
2  *
3  * Copyright (C) 1999 The Free Software Foundation
4  *
5  * Author: Owen Taylor <otaylor@redhat.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <config.h>
24 #include <math.h>
25 #include "gdk-pixbuf-private.h"
26 #include "pixops/pixops.h"
27
28 \f
29
30 /**
31  * gdk_pixbuf_scale:
32  * @src: a #GdkPixbuf
33  * @dest: the #GdkPixbuf into which to render the results
34  * @dest_x: the left coordinate for region to render
35  * @dest_y: the top coordinate for region to render
36  * @dest_width: the width of the region to render
37  * @dest_height: the height of the region to render
38  * @offset_x: the offset in the X direction (currently rounded to an integer)
39  * @offset_y: the offset in the Y direction (currently rounded to an integer)
40  * @scale_x: the scale factor in the X direction
41  * @scale_y: the scale factor in the Y direction
42  * @interp_type: the interpolation type for the transformation.
43  * 
44  * Transforms the image by source image by scaling by @scale_x and @scale_y then
45  * translating by @offset_x and @offset_y, then renders the rectangle
46  * (@dest,@dest_y,@dest_width,@dest_height) of the resulting image onto the
47  * destination drawable replacing the previous contents.
48  **/
49 void
50 gdk_pixbuf_scale (const GdkPixbuf *src,
51                   GdkPixbuf       *dest,
52                   int              dest_x,
53                   int              dest_y,
54                   int              dest_width,
55                   int              dest_height,
56                   double           offset_x,
57                   double           offset_y,
58                   double           scale_x,
59                   double           scale_y,
60                   GdkInterpType    interp_type)
61 {
62   g_return_if_fail (src != NULL);
63   g_return_if_fail (dest != NULL);
64   g_return_if_fail (dest_x >= 0 && dest_x + dest_width <= dest->width);
65   g_return_if_fail (dest_y >= 0 && dest_y + dest_height <= dest->height);
66
67   offset_x = floor (offset_x + 0.5);
68   offset_y = floor (offset_y + 0.5);
69   
70   pixops_scale (dest->pixels + dest_y * dest->rowstride + dest_x * dest->n_channels,
71                 dest_x - offset_x, dest_y - offset_y, 
72                 dest_x + dest_width - offset_x, dest_y + dest_height - offset_y,
73                 dest->rowstride, dest->n_channels, dest->has_alpha,
74                 src->pixels, src->width, src->height,
75                 src->rowstride, src->n_channels, src->has_alpha,
76                 scale_x, scale_y, interp_type);
77 }
78
79 /**
80  * gdk_pixbuf_composite:
81  * @src: a #GdkPixbuf
82  * @dest: the #GdkPixbuf into which to render the results
83  * @dest_x: the left coordinate for region to render
84  * @dest_y: the top coordinate for region to render
85  * @dest_width: the width of the region to render
86  * @dest_height: the height of the region to render
87  * @offset_x: the offset in the X direction (currently rounded to an integer)
88  * @offset_y: the offset in the Y direction (currently rounded to an integer)
89  * @scale_x: the scale factor in the X direction
90  * @scale_y: the scale factor in the Y direction
91  * @interp_type: the interpolation type for the transformation.
92  * @overall_alpha: overall alpha for source image (0..255)
93  * 
94  * Transforms the image by source image by scaling by @scale_x and @scale_y then
95  * translating by @offset_x and @offset_y, then composites the rectangle
96  * (@dest,@dest_y,@dest_width,@dest_height) of the resulting image onto the
97  * destination drawable.
98  **/
99 void
100 gdk_pixbuf_composite (const GdkPixbuf *src,
101                       GdkPixbuf       *dest,
102                       int              dest_x,
103                       int              dest_y,
104                       int              dest_width,
105                       int              dest_height,
106                       double           offset_x,
107                       double           offset_y,
108                       double           scale_x,
109                       double           scale_y,
110                       GdkInterpType    interp_type,
111                       int              overall_alpha)
112 {
113   g_return_if_fail (src != NULL);
114   g_return_if_fail (dest != NULL);
115   g_return_if_fail (dest_x >= 0 && dest_x + dest_width <= dest->width);
116   g_return_if_fail (dest_y >= 0 && dest_y + dest_height <= dest->height);
117   g_return_if_fail (overall_alpha >= 0 && overall_alpha <= 255);
118
119   offset_x = floor (offset_x + 0.5);
120   offset_y = floor (offset_y + 0.5);
121   pixops_composite (dest->pixels + dest_y * dest->rowstride + dest_x * dest->n_channels,
122                     dest_x - offset_x, dest_y - offset_y, 
123                     dest_x + dest_width - offset_x, dest_y + dest_height - offset_y,
124                     dest->rowstride, dest->n_channels, dest->has_alpha,
125                     src->pixels, src->width, src->height,
126                     src->rowstride, src->n_channels, src->has_alpha,
127                     scale_x, scale_y, interp_type, overall_alpha);
128 }
129
130 /**
131  * gdk_pixbuf_composite_color:
132  * @src: a #GdkPixbuf
133  * @dest: the #GdkPixbuf into which to render the results
134  * @dest_x: the left coordinate for region to render
135  * @dest_y: the top coordinate for region to render
136  * @dest_width: the width of the region to render
137  * @dest_height: the height of the region to render
138  * @offset_x: the offset in the X direction (currently rounded to an integer)
139  * @offset_y: the offset in the Y direction (currently rounded to an integer)
140  * @scale_x: the scale factor in the X direction
141  * @scale_y: the scale factor in the Y direction
142  * @interp_type: the interpolation type for the transformation.
143  * @overall_alpha: overall alpha for source image (0..255)
144  * @check_x: the X offset for the checkboard (origin of checkboard is at -@check_x, -@check_y)
145  * @check_y: the Y offset for the checkboard 
146  * @check_size: the size of checks in the checkboard (must be a power of two)
147  * @color1: the color of check at upper left
148  * @color2: the color of the other check
149  * 
150  * Transforms the image by source image by scaling by @scale_x and @scale_y then
151  * translating by @offset_x and @offset_y, then composites the rectangle
152  * (@dest,@dest_y,@dest_width,@dest_height) of the resulting image with
153  * a checkboard of the colors @color1 and @color2 and renders it onto the
154  * destination drawable.
155  **/
156 void
157 gdk_pixbuf_composite_color (const GdkPixbuf *src,
158                             GdkPixbuf       *dest,
159                             int              dest_x,
160                             int              dest_y,
161                             int              dest_width,
162                             int              dest_height,
163                             double           offset_x,
164                             double           offset_y,
165                             double           scale_x,
166                             double           scale_y,
167                             GdkInterpType    interp_type,
168                             int              overall_alpha,
169                             int              check_x,
170                             int              check_y,
171                             int              check_size,
172                             guint32          color1,
173                             guint32          color2)
174 {
175   g_return_if_fail (src != NULL);
176   g_return_if_fail (dest != NULL);
177   g_return_if_fail (dest_x >= 0 && dest_x + dest_width <= dest->width);
178   g_return_if_fail (dest_y >= 0 && dest_y + dest_height <= dest->height);
179   g_return_if_fail (overall_alpha >= 0 && overall_alpha <= 255);
180
181   offset_x = floor (offset_x + 0.5);
182   offset_y = floor (offset_y + 0.5);
183   
184   pixops_composite_color (dest->pixels + dest_y * dest->rowstride + dest_x * dest->n_channels,
185                           dest_x - offset_x, dest_y - offset_y, 
186                           dest_x + dest_width - offset_x, dest_y + dest_height - offset_y,
187                           dest->rowstride, dest->n_channels, dest->has_alpha,
188                           src->pixels, src->width, src->height,
189                           src->rowstride, src->n_channels, src->has_alpha,
190                           scale_x, scale_y, interp_type, overall_alpha, check_x, check_y,
191                           check_size, color1, color2);
192 }
193
194 /**
195  * gdk_pixbuf_scale_simple:
196  * @src: a #GdkPixbuf
197  * @dest_width: the width of destination image
198  * @dest_height: the height of destination image
199  * @interp_type: the interpolation type for the transformation.
200  * 
201  * Scale the #GdkPixbuf @src to @dest_width x @dest_height and render the result into
202  * a new #GdkPixbuf.
203  * 
204  * Return value: the new #GdkPixbuf, or NULL if not enough memory could be
205  * allocated for it.
206  **/
207 GdkPixbuf *
208 gdk_pixbuf_scale_simple (const GdkPixbuf *src,
209                          int              dest_width,
210                          int              dest_height,
211                          GdkInterpType    interp_type)
212 {
213   GdkPixbuf *dest;
214
215   g_return_val_if_fail (src != NULL, NULL);
216   g_return_val_if_fail (dest_width > 0, NULL);
217   g_return_val_if_fail (dest_height > 0, NULL);
218
219   dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB, src->has_alpha, 8, dest_width, dest_height);
220   if (!dest)
221     return NULL;
222
223   gdk_pixbuf_scale (src, dest,  0, 0, dest_width, dest_height, 0, 0,
224                     (double) dest_width / src->width,
225                     (double) dest_height / src->height,
226                     interp_type);
227
228   return dest;
229 }
230
231 /**
232  * gdk_pixbuf_composite_color_simple:
233  * @src: a #GdkPixbuf
234  * @dest_width: the width of destination image
235  * @dest_height: the height of destination image
236  * @interp_type: the interpolation type for the transformation.
237  * @overall_alpha: overall alpha for source image (0..255)
238  * @check_size: the size of checks in the checkboard (must be a power of two)
239  * @color1: the color of check at upper left
240  * @color2: the color of the other check
241  * 
242  * Scale the #GdkPixbuf @src to @dest_width x @dest_height composite the result with
243  * a checkboard of colors @color1 and @color2 and render the result into
244  * a new #GdkPixbuf.
245  * 
246  * Return value: the new #GdkPixbuf, or NULL if not enough memory could be
247  * allocated for it.
248  **/
249 GdkPixbuf *
250 gdk_pixbuf_composite_color_simple (const GdkPixbuf *src,
251                                    int              dest_width,
252                                    int              dest_height,
253                                    GdkInterpType    interp_type,
254                                    int              overall_alpha,
255                                    int              check_size,
256                                    guint32          color1,
257                                    guint32          color2)
258 {
259   GdkPixbuf *dest;
260
261   g_return_val_if_fail (src != NULL, NULL);
262   g_return_val_if_fail (dest_width > 0, NULL);
263   g_return_val_if_fail (dest_height > 0, NULL);
264   g_return_val_if_fail (overall_alpha >= 0 && overall_alpha <= 255, NULL);
265
266   dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB, src->has_alpha, 8, dest_width, dest_height);
267   if (!dest)
268     return NULL;
269
270   gdk_pixbuf_composite_color (src, dest, 0, 0, dest_width, dest_height, 0, 0,
271                               (double) dest_width / src->width,
272                               (double) dest_height / src->height,
273                               interp_type, overall_alpha, 0, 0, check_size, color1, color2);
274
275   return dest;
276 }