Re: pixbuf rotation

On 2/3/2009 2:41 AM, Roei Azachi wrote:
Aron, Thank you very much fr your replay.
if it is possible, can you specify the function I need to use for each step you mentioned?
I don't have very much experience in cairo.
Again, Thanks a lot,

Aron Rubin wrote:
Roei Azachi wrote:
Is there a way to rotate a pixbuf not by a multiple of 90 degrees (using
I would like to rotate it by 10 degrees...

create a cairo image
make a cairo context on that new image
set the transform to be the rotation you want
load your original image
set the cairo source to be that original image
cairo paint
get the image back out (or not depending on your case)
Assuming you want to rotate to an image and not on screen it would look something like the attached code. If you wanted to rotate the image for display you could cut out the intermediate image.


#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <glib.h>
#include <gtk/gtk.h>
#include <gdk/gdkcairo.h>

gboolean drawing_expose( GtkWidget *da, GdkEventExpose *event,
			 cairo_surface_t *img ) {
  cairo_t *cr = gdk_cairo_create( da->window );
  cairo_rectangle( cr, event->area.x, event->area.y,
		   event->area.width, event->area.height );
  cairo_clip( cr );
  cairo_set_source_surface( cr, img, 0, 0 );
  cairo_paint( cr );
  cairo_destroy( cr );
  return( FALSE );

cairo_surface_t *load_and_rotate_png( const char *filename, double angle_rad ) {
  cairo_t *cr;
  cairo_surface_t *iimg, *oimg;
  iimg = cairo_image_surface_create_from_png( filename );
  oimg = cairo_image_surface_create( CAIRO_FORMAT_RGB24,
				     cairo_image_surface_get_width( iimg ),
				     cairo_image_surface_get_height( iimg ) );
  cr = cairo_create( oimg );
  cairo_rotate( cr, angle_rad );
  cairo_set_source_surface( cr, iimg, 0, 0 );
  cairo_paint( cr );
  cairo_destroy( cr );

  cairo_surface_destroy( iimg );
  return( oimg );

void quit( void *unused ) {
  gtk_exit( 0 );

#define DEGTORAD (M_PI/180.0)

int main( int argc, char *argv[] ) {
  GtkWidget *win, *vbox, *da;
  cairo_surface_t *img;
  gtk_init( &argc, &argv );
  win = gtk_window_new( GTK_WINDOW_TOPLEVEL );
  gtk_widget_set_name( win, "Cairo Test" );
  vbox = gtk_vbox_new( FALSE, 0 );
  gtk_container_add( GTK_CONTAINER(win), vbox );
  gtk_widget_show( vbox );
  g_signal_connect( win, "destroy", G_CALLBACK(quit), NULL );
  // Create the drawing area
  da = gtk_drawing_area_new();
  img = load_and_rotate_png( argc > 1 ? argv[1] : "height.png",
			     (argc > 2 ? atof( argv[2] ) : 10.0)*DEGTORAD );
  g_signal_connect( da, "expose-event", G_CALLBACK(drawing_expose), img );
  //gtk_drawing_area_size( GTK_DRAWING_AREA(pie), 512, 512);
  gtk_box_pack_start( GTK_BOX(vbox), da, TRUE, TRUE, 0 );
  gtk_widget_show( da );
  gtk_widget_show_all( win );
  exit( 0 );

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]