gdk-pixbuf-scale.c 11.2 KB
Newer Older
1 2 3 4 5 6 7
/* GdkPixbuf library - Scaling and compositing functions
 *
 * Copyright (C) 1999 The Free Software Foundation
 *
 * Author: Owen Taylor <otaylor@redhat.com>
 *
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9 10 11 12 13 14
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * 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 19 20 21 22 23 24 25
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <config.h>
#include <math.h>
#include "gdk-pixbuf-private.h"
26
#include "pixops/pixops.h"
27 28


29 30 31 32 33

/**
 * gdk_pixbuf_scale:
 * @src: a #GdkPixbuf
 * @dest: the #GdkPixbuf into which to render the results
34 35 36 37
 * @dest_x: the left coordinate for region to render
 * @dest_y: the top coordinate for region to render
 * @dest_width: the width of the region to render
 * @dest_height: the height of the region to render
38 39 40 41
 * @offset_x: the offset in the X direction (currently rounded to an integer)
 * @offset_y: the offset in the Y direction (currently rounded to an integer)
 * @scale_x: the scale factor in the X direction
 * @scale_y: the scale factor in the Y direction
42
 * @interp_type: the interpolation type for the transformation.
43
 * 
44 45 46 47 48 49 50 51 52
 * Creates a transformation of the source image @src by scaling by
 * @scale_x and @scale_y then translating by @offset_x and @offset_y,
 * then renders the rectangle (@dest_x, @dest_y, @dest_width,
 * @dest_height) of the resulting image onto the destination image
 * replacing the previous contents.
 *
 * Try to use gdk_pixbuf_scale_simple() first, this function is
 * the industrial-strength power tool you can fall back to if
 * gdk_pixbuf_scale_simple() isn't powerful enough.
53 54
 **/
void
55 56 57 58 59 60 61 62 63 64
gdk_pixbuf_scale (const GdkPixbuf *src,
		  GdkPixbuf       *dest,
		  int              dest_x,
		  int              dest_y,
		  int              dest_width,
		  int              dest_height,
		  double           offset_x,
		  double           offset_y,
		  double           scale_x,
		  double           scale_y,
65
		  GdkInterpType    interp_type)
66
{
67 68 69 70 71
  g_return_if_fail (src != NULL);
  g_return_if_fail (dest != NULL);
  g_return_if_fail (dest_x >= 0 && dest_x + dest_width <= dest->width);
  g_return_if_fail (dest_y >= 0 && dest_y + dest_height <= dest->height);

72 73
  offset_x = floor (offset_x + 0.5);
  offset_y = floor (offset_y + 0.5);
74
  
75
  pixops_scale (dest->pixels + dest_y * dest->rowstride + dest_x * dest->n_channels,
76 77
		dest_x - offset_x, dest_y - offset_y, 
		dest_x + dest_width - offset_x, dest_y + dest_height - offset_y,
78 79 80
		dest->rowstride, dest->n_channels, dest->has_alpha,
		src->pixels, src->width, src->height,
		src->rowstride, src->n_channels, src->has_alpha,
81
		scale_x, scale_y, (PixopsInterpType)interp_type);
82 83 84 85 86 87
}

/**
 * gdk_pixbuf_composite:
 * @src: a #GdkPixbuf
 * @dest: the #GdkPixbuf into which to render the results
88 89 90 91
 * @dest_x: the left coordinate for region to render
 * @dest_y: the top coordinate for region to render
 * @dest_width: the width of the region to render
 * @dest_height: the height of the region to render
92 93 94 95
 * @offset_x: the offset in the X direction (currently rounded to an integer)
 * @offset_y: the offset in the Y direction (currently rounded to an integer)
 * @scale_x: the scale factor in the X direction
 * @scale_y: the scale factor in the Y direction
96
 * @interp_type: the interpolation type for the transformation.
97 98
 * @overall_alpha: overall alpha for source image (0..255)
 * 
99 100 101
 * Creates a transformation of the source image @src by scaling by
 * @scale_x and @scale_y then translating by @offset_x and @offset_y,
 * then composites the rectangle (@dest_x, @dest_y, @dest_width,
102
 * @dest_height) of the resulting image onto the destination image.
103 104
 **/
void
105 106 107 108 109 110 111 112 113 114
gdk_pixbuf_composite (const GdkPixbuf *src,
		      GdkPixbuf       *dest,
		      int              dest_x,
		      int              dest_y,
		      int              dest_width,
		      int              dest_height,
		      double           offset_x,
		      double           offset_y,
		      double           scale_x,
		      double           scale_y,
115
		      GdkInterpType    interp_type,
116
		      int              overall_alpha)
117
{
118 119 120 121 122 123
  g_return_if_fail (src != NULL);
  g_return_if_fail (dest != NULL);
  g_return_if_fail (dest_x >= 0 && dest_x + dest_width <= dest->width);
  g_return_if_fail (dest_y >= 0 && dest_y + dest_height <= dest->height);
  g_return_if_fail (overall_alpha >= 0 && overall_alpha <= 255);

124 125
  offset_x = floor (offset_x + 0.5);
  offset_y = floor (offset_y + 0.5);
126
  pixops_composite (dest->pixels + dest_y * dest->rowstride + dest_x * dest->n_channels,
127 128
		    dest_x - offset_x, dest_y - offset_y, 
		    dest_x + dest_width - offset_x, dest_y + dest_height - offset_y,
129 130 131
		    dest->rowstride, dest->n_channels, dest->has_alpha,
		    src->pixels, src->width, src->height,
		    src->rowstride, src->n_channels, src->has_alpha,
132
		    scale_x, scale_y, (PixopsInterpType)interp_type, overall_alpha);
133 134 135 136 137 138
}

/**
 * gdk_pixbuf_composite_color:
 * @src: a #GdkPixbuf
 * @dest: the #GdkPixbuf into which to render the results
139 140 141 142
 * @dest_x: the left coordinate for region to render
 * @dest_y: the top coordinate for region to render
 * @dest_width: the width of the region to render
 * @dest_height: the height of the region to render
143 144 145 146
 * @offset_x: the offset in the X direction (currently rounded to an integer)
 * @offset_y: the offset in the Y direction (currently rounded to an integer)
 * @scale_x: the scale factor in the X direction
 * @scale_y: the scale factor in the Y direction
147
 * @interp_type: the interpolation type for the transformation.
148 149 150 151 152 153 154
 * @overall_alpha: overall alpha for source image (0..255)
 * @check_x: the X offset for the checkboard (origin of checkboard is at -@check_x, -@check_y)
 * @check_y: the Y offset for the checkboard 
 * @check_size: the size of checks in the checkboard (must be a power of two)
 * @color1: the color of check at upper left
 * @color2: the color of the other check
 * 
155 156 157 158 159 160 161 162 163 164
 * Creates a transformation of the source image @src by scaling by
 * @scale_x and @scale_y then translating by @offset_x and @offset_y,
 * then composites the rectangle (@dest_x ,@dest_y, @dest_width,
 * @dest_height) of the resulting image with a checkboard of the
 * colors @color1 and @color2 and renders it onto the destination
 * image.
 *
 * See gdk_pixbuf_composite_color_simple() for a simpler variant of this
 * function suitable for many tasks.
 * 
165 166
 **/
void
167 168 169 170 171 172 173 174 175 176
gdk_pixbuf_composite_color (const GdkPixbuf *src,
			    GdkPixbuf       *dest,
			    int              dest_x,
			    int              dest_y,
			    int              dest_width,
			    int              dest_height,
			    double           offset_x,
			    double           offset_y,
			    double           scale_x,
			    double           scale_y,
177
			    GdkInterpType    interp_type,
178 179 180 181
			    int              overall_alpha,
			    int              check_x,
			    int              check_y,
			    int              check_size,
182 183
			    guint32          color1,
			    guint32          color2)
184
{
185 186 187 188 189 190
  g_return_if_fail (src != NULL);
  g_return_if_fail (dest != NULL);
  g_return_if_fail (dest_x >= 0 && dest_x + dest_width <= dest->width);
  g_return_if_fail (dest_y >= 0 && dest_y + dest_height <= dest->height);
  g_return_if_fail (overall_alpha >= 0 && overall_alpha <= 255);

191 192
  offset_x = floor (offset_x + 0.5);
  offset_y = floor (offset_y + 0.5);
193
  
194
  pixops_composite_color (dest->pixels + dest_y * dest->rowstride + dest_x * dest->n_channels,
195 196
			  dest_x - offset_x, dest_y - offset_y, 
			  dest_x + dest_width - offset_x, dest_y + dest_height - offset_y,
197 198 199
			  dest->rowstride, dest->n_channels, dest->has_alpha,
			  src->pixels, src->width, src->height,
			  src->rowstride, src->n_channels, src->has_alpha,
200
			  scale_x, scale_y, (PixopsInterpType)interp_type, overall_alpha, check_x, check_y,
201
			  check_size, color1, color2);
202 203 204 205 206 207 208
}

/**
 * gdk_pixbuf_scale_simple:
 * @src: a #GdkPixbuf
 * @dest_width: the width of destination image
 * @dest_height: the height of destination image
209
 * @interp_type: the interpolation type for the transformation.
210 211 212 213 214 215 216 217 218 219 220 221 222
 *
 * Create a new #GdkPixbuf containing a copy of @src scaled to
 * @dest_width x @dest_height. Leaves @src unaffected.  @interp_type
 * should be #GDK_INTERP_NEAREST if you want maximum speed (but when
 * scaling down #GDK_INTERP_NEAREST is usually unusably ugly).  The
 * default @interp_type should be #GDK_INTERP_BILINEAR which offers
 * reasonable quality and speed.
 *
 * You can scale a sub-portion of @src by creating a sub-pixbuf
 * pointing into @src; see gdk_pixbuf_new_subpixbuf().
 *
 * For more complicated scaling/compositing see gdk_pixbuf_scale()
 * and gdk_pixbuf_composite().
223
 * 
224
 * Return value: the new #GdkPixbuf, or %NULL if not enough memory could be
225
 * allocated for it.
226 227
 **/
GdkPixbuf *
228 229 230
gdk_pixbuf_scale_simple (const GdkPixbuf *src,
			 int              dest_width,
			 int              dest_height,
231
			 GdkInterpType    interp_type)
232
{
233 234 235 236 237 238 239 240 241
  GdkPixbuf *dest;

  g_return_val_if_fail (src != NULL, NULL);
  g_return_val_if_fail (dest_width > 0, NULL);
  g_return_val_if_fail (dest_height > 0, NULL);

  dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB, src->has_alpha, 8, dest_width, dest_height);
  if (!dest)
    return NULL;
242 243

  gdk_pixbuf_scale (src, dest,  0, 0, dest_width, dest_height, 0, 0,
244 245 246
		    (double) dest_width / src->width,
		    (double) dest_height / src->height,
		    interp_type);
247 248 249 250 251 252 253 254 255

  return dest;
}

/**
 * gdk_pixbuf_composite_color_simple:
 * @src: a #GdkPixbuf
 * @dest_width: the width of destination image
 * @dest_height: the height of destination image
256
 * @interp_type: the interpolation type for the transformation.
257 258 259 260 261
 * @overall_alpha: overall alpha for source image (0..255)
 * @check_size: the size of checks in the checkboard (must be a power of two)
 * @color1: the color of check at upper left
 * @color2: the color of the other check
 * 
262 263 264
 * Creates a new #GdkPixbuf by scaling @src to @dest_width x
 * @dest_height and compositing the result with a checkboard of colors
 * @color1 and @color2.
265
 * 
266
 * Return value: the new #GdkPixbuf, or %NULL if not enough memory could be
267
 * allocated for it.
268 269
 **/
GdkPixbuf *
270 271 272
gdk_pixbuf_composite_color_simple (const GdkPixbuf *src,
				   int              dest_width,
				   int              dest_height,
273
				   GdkInterpType    interp_type,
274 275
				   int              overall_alpha,
				   int              check_size,
276 277
				   guint32          color1,
				   guint32          color2)
278
{
279 280 281 282 283 284 285 286 287 288
  GdkPixbuf *dest;

  g_return_val_if_fail (src != NULL, NULL);
  g_return_val_if_fail (dest_width > 0, NULL);
  g_return_val_if_fail (dest_height > 0, NULL);
  g_return_val_if_fail (overall_alpha >= 0 && overall_alpha <= 255, NULL);

  dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB, src->has_alpha, 8, dest_width, dest_height);
  if (!dest)
    return NULL;
289 290

  gdk_pixbuf_composite_color (src, dest, 0, 0, dest_width, dest_height, 0, 0,
291 292 293
			      (double) dest_width / src->width,
			      (double) dest_height / src->height,
			      interp_type, overall_alpha, 0, 0, check_size, color1, color2);
294 295 296

  return dest;
}