[cairo] Rotation in cairo

Hung Nguyen Vu vuhung16plus+shape at gmail.com
Mon Aug 4 07:30:49 PDT 2008

On Mon, Aug 4, 2008 at 8:52 PM, Adrian Johnson <ajohnson at redneon.com> wrote:
>>cairo_image_surface_get_height (image);
>>   cairo_translate (cr, w * 0.5, h * 0.5);
>>   cairo_rotate (cr, M_PI / 2.0);
>>   cairo_translate (cr, -w * 0.5, -h * 0.5);
>>   cairo_set_source_surface (cr, image, w*0.5, h*0.5);
> You will probably want to leave the set_source_surface offset at 0, 0 since
> you have already restored the origin to the original position after the
> rotation.
I have another question about this. Please read on.

> Here's the updated code that correctly rotates the image. I have also
> swapped the width and height when creating the destination surface and in
> the first translate so that the rotated image fits exactly into the
> destination surface.
I reflect all your answer in a new code. My next target is:

* Draw a small image on top of another image while discard transparent color

In this case, the target image[1] has white( 255, 255, 255) as the
transparent color.

Code: http://aoclife.ddo.jp/~vuhung/tmp/cairo-rotate.c
Input 1: http://aoclife.ddo.jp/~vuhung/tmp/Flag.png
Input 2: http://point-line.info/blog/images/shinobi-icon.png
Output: http://aoclife.ddo.jp/~vuhung/tmp/Flag-icon-rotate.png

How can I "delete" all the white color in the output image.

/* gcc `pkg-config --libs --cflags cairo` cairo-rotate.c -o cairo-rotate */
#include <cairo.h>
#include <math.h>
main (int argc, char *argv[])

  int source_w, source_h;
  int target_w, target_h;

  cairo_surface_t *source_image;
  cairo_surface_t *target_image;

  cairo_t *cr;

  //  shinobi-icon.png PNG 50x50+0+0 PseudoClass 16c 8-bit 518 0.000u 0:01
  // (Sample) wget http://point-line.info/blog/images/shinobi-icon.png
  source_image = cairo_image_surface_create_from_png ("shinobi-icon.png");
  source_w = cairo_image_surface_get_width (source_image);
  source_h = cairo_image_surface_get_height (source_image);

  // (Sample) wget http://aoclife.ddo.jp/~vuhung/tmp/Flag.png
  target_image = cairo_image_surface_create_from_png("Flag.png");
  target_w = cairo_image_surface_get_width(target_image);
  target_h = cairo_image_surface_get_height(target_image);
  cr = cairo_create (target_image);

  // I want to make the white color in shinobi-icon transparent. How?
  cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
  cairo_translate (cr, source_h * 0.5, source_w * 0.5);
  cairo_rotate (cr, M_PI / 4.0);
  cairo_translate (cr, -source_w * 0.5, -source_h * 0.5);
  // I want to put the icon shinobi-icon in the center of Flag.png. How?
  cairo_set_source_surface (cr, source_image, target_w/2, target_h/2);
  cairo_set_operator (cr, CAIRO_OPERATOR_ATOP);
  cairo_paint (cr);
  cairo_surface_write_to_png (target_image, "Flag-icon-rotate.png");
  cairo_surface_destroy (source_image);
  cairo_surface_destroy (target_image);

  return 0;

Best Regards,
Nguyen Hung Vu ( Nguyễn Vũ Hưng )
vuhung16plus{remove}@gmail.dot.com , YIM: vuhung16 , Skype: vuhung16dg
Japan through an eye of a gaijin:

More information about the cairo mailing list