Scaling

Name

Scaling -- Scaling pixbufs and scaling and compositing pixbufs

Synopsis


#include <gdk-pixbuf/gdk-pixbuf.h>


void        gdk_pixbuf_scale                (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,
                                             ArtFilterLevel filter_level);
void        gdk_pixbuf_composite            (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,
                                             ArtFilterLevel filter_level,
                                             int overall_alpha);
void        gdk_pixbuf_composite_color      (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,
                                             ArtFilterLevel filter_level,
                                             int overall_alpha,
                                             int check_x,
                                             int check_y,
                                             int check_size,
                                             art_u32 color1,
                                             art_u32 color2);
GdkPixbuf*  gdk_pixbuf_scale_simple         (GdkPixbuf *src,
                                             int dest_width,
                                             int dest_height,
                                             ArtFilterLevel filter_level);
GdkPixbuf*  gdk_pixbuf_composite_color_simple
                                            (GdkPixbuf *src,
                                             int dest_width,
                                             int dest_height,
                                             ArtFilterLevel filter_level,
                                             int overall_alpha,
                                             int check_size,
                                             art_u32 color1,
                                             art_u32 color2);

Description

gdk-pixbuf contains functions to scale pixbufs, to scale pixbufs and composite against an existing image, and to scale pixbufs and composite against a solid color or checkerboard. (Compositing a checkerboard is a common way to show an image with an alpha-channel in image-viewing and editing software.)

Since the full-featured functions (gdk_pixbuf_scale(), gdk_pixbuf_composite(), and gdk_pixbuf_composite_color()) are rather complex to use and have many arguments, two simple convenience functions are provided, gdk_pixbuf_scale_simple() and gdk_pixbuf_composite_color_simple() which create a new pixbuf of a given size, scale an original image to fit, and then return the new pixmap.

The following example demonstrates handling an expose event by rendering the appropriate area of a source image (which is scaled to fit the widget) onto the widget's window. The source image is rendered against a checkerboard, which provides a visual representation of the alpha channel if the image has one. If the image doesn't have an alpha channel, calling gdk_pixbuf_composite_color() function has exactly the same effect as calling gdk_pixbuf_scale().

gboolean
expose_cb (GtkWidget *widget, GdkEventExpose *event, gpointer data)
{
  GdkPixbuf *dest;

  gdk_window_set_back_pixmap (widget->window, NULL, FALSE);
  
  dest = gdk_pixbuf_new (ART_PIX_RGB, FALSE, 8, event->area.width, event->area.height);

  gdk_pixbuf_composite_color (pixbuf, dest,
                              0, 0, event->area.width, event->area.height,
                              -event->area.x, -event->area.y,
                              (double) widget->allocation.width / pixbuf->art_pixbuf->width,
                              (double) widget->allocation.height / pixbuf->art_pixbuf->height,
                              filter_level, 255,
                               event->area.x, event->area.y, 16, 0xaaaaaa, 0x555555);

  gdk_pixbuf_render_to_drawable (dest, widget->window, widget->style->fg_gc[GTK_STATE_NORMAL],
                                 0, 0, event->area.x, event->area.y,
                                 event->area.width, event->area.height,
                                 GDK_RGB_DITHER_NORMAL, event->area.x, event->area.y);
  
  gdk_pixbuf_unref (dest);
  
  return TRUE;
}

Details

gdk_pixbuf_scale ()

void        gdk_pixbuf_scale                (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,
                                             ArtFilterLevel filter_level);

Transforms the image by source image by scaling by scale_x and scale_y then translating by offset_x and offset_y, then renders the rectangle (dest,dest_y,dest_width,dest_height) of the resulting image onto the destination drawable replacing the previous contents.

src : a GdkPixbuf
dest : the GdkPixbuf into which to render the results
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
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
filter_level : the filter quality for the transformation.


gdk_pixbuf_composite ()

void        gdk_pixbuf_composite            (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,
                                             ArtFilterLevel filter_level,
                                             int overall_alpha);

Transforms the image by source image by scaling by scale_x and scale_y then translating by offset_x and offset_y, then composites the rectangle (dest,dest_y,dest_width,dest_height) of the resulting image onto the destination drawable.

src : a GdkPixbuf
dest : the GdkPixbuf into which to render the results
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
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
filter_level : the filter quality for the transformation.
overall_alpha : overall alpha for source image (0..255)


gdk_pixbuf_composite_color ()

void        gdk_pixbuf_composite_color      (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,
                                             ArtFilterLevel filter_level,
                                             int overall_alpha,
                                             int check_x,
                                             int check_y,
                                             int check_size,
                                             art_u32 color1,
                                             art_u32 color2);

Transforms the image by source image by scaling by scale_x and scale_y then translating by offset_x and offset_y, then composites the rectangle (dest,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 drawable.

src : a GdkPixbuf
dest : the GdkPixbuf into which to render the results
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
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
filter_level : the filter quality for the transformation.
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


gdk_pixbuf_scale_simple ()

GdkPixbuf*  gdk_pixbuf_scale_simple         (GdkPixbuf *src,
                                             int dest_width,
                                             int dest_height,
                                             ArtFilterLevel filter_level);

Scale the GdkPixbuf src to dest_width x dest_height and render the result into a new GdkPixbuf.

src : a GdkPixbuf
dest_width : the width of destination image
dest_height : the height of destination image
filter_level : the filter quality for the transformation.
Returns : the new GdkPixbuf


gdk_pixbuf_composite_color_simple ()

GdkPixbuf*  gdk_pixbuf_composite_color_simple
                                            (GdkPixbuf *src,
                                             int dest_width,
                                             int dest_height,
                                             ArtFilterLevel filter_level,
                                             int overall_alpha,
                                             int check_size,
                                             art_u32 color1,
                                             art_u32 color2);

Scale the GdkPixbuf src to dest_width x dest_height composite the result with a checkboard of colors color1 and color2 and render the result into a new GdkPixbuf.

src : a GdkPixbuf
dest_width : the width of destination image
dest_height : the height of destination image
filter_level : the filter quality for the transformation.
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
Returns : the new GdkPixbuf